1 // SPDX-License-Identifier: GPL-2.0-only
2 /*******************************************************************************
3 This is the driver for the ST MAC 10/100/1000 on-chip Ethernet controllers.
4 ST Ethernet IPs are built around a Synopsys IP Core.
6 Copyright(C) 2007-2011 STMicroelectronics Ltd
9 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
11 Documentation available at:
12 http://www.stlinux.com
14 https://bugzilla.stlinux.com/
15 *******************************************************************************/
17 #include <linux/clk.h>
18 #include <linux/kernel.h>
19 #include <linux/interrupt.h>
21 #include <linux/tcp.h>
22 #include <linux/skbuff.h>
23 #include <linux/ethtool.h>
24 #include <linux/if_ether.h>
25 #include <linux/crc32.h>
26 #include <linux/mii.h>
28 #include <linux/if_vlan.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/slab.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/prefetch.h>
33 #include <linux/pinctrl/consumer.h>
34 #ifdef CONFIG_DEBUG_FS
35 #include <linux/debugfs.h>
36 #include <linux/seq_file.h>
37 #endif /* CONFIG_DEBUG_FS */
38 #include <linux/net_tstamp.h>
39 #include <linux/phylink.h>
40 #include <linux/udp.h>
41 #include <linux/bpf_trace.h>
42 #include <net/pkt_cls.h>
43 #include <net/xdp_sock_drv.h>
44 #include "stmmac_ptp.h"
46 #include "stmmac_xdp.h"
47 #include <linux/reset.h>
48 #include <linux/of_mdio.h>
49 #include "dwmac1000.h"
53 #define STMMAC_ALIGN(x) ALIGN(ALIGN(x, SMP_CACHE_BYTES), 16)
54 #define TSO_MAX_BUFF_SIZE (SZ_16K - 1)
56 /* Module parameters */
58 static int watchdog = TX_TIMEO;
59 module_param(watchdog, int, 0644);
60 MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
62 static int debug = -1;
63 module_param(debug, int, 0644);
64 MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
66 static int phyaddr = -1;
67 module_param(phyaddr, int, 0444);
68 MODULE_PARM_DESC(phyaddr, "Physical device address");
70 #define STMMAC_TX_THRESH(x) ((x)->dma_tx_size / 4)
71 #define STMMAC_RX_THRESH(x) ((x)->dma_rx_size / 4)
73 /* Limit to make sure XDP TX and slow path can coexist */
74 #define STMMAC_XSK_TX_BUDGET_MAX 256
75 #define STMMAC_TX_XSK_AVAIL 16
76 #define STMMAC_RX_FILL_BATCH 16
78 #define STMMAC_XDP_PASS 0
79 #define STMMAC_XDP_CONSUMED BIT(0)
80 #define STMMAC_XDP_TX BIT(1)
81 #define STMMAC_XDP_REDIRECT BIT(2)
83 static int flow_ctrl = FLOW_AUTO;
84 module_param(flow_ctrl, int, 0644);
85 MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
87 static int pause = PAUSE_TIME;
88 module_param(pause, int, 0644);
89 MODULE_PARM_DESC(pause, "Flow Control Pause Time");
92 static int tc = TC_DEFAULT;
93 module_param(tc, int, 0644);
94 MODULE_PARM_DESC(tc, "DMA threshold control value");
96 #define DEFAULT_BUFSIZE 1536
97 static int buf_sz = DEFAULT_BUFSIZE;
98 module_param(buf_sz, int, 0644);
99 MODULE_PARM_DESC(buf_sz, "DMA buffer size");
101 #define STMMAC_RX_COPYBREAK 256
103 static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
104 NETIF_MSG_LINK | NETIF_MSG_IFUP |
105 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
107 #define STMMAC_DEFAULT_LPI_TIMER 1000
108 static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
109 module_param(eee_timer, int, 0644);
110 MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
111 #define STMMAC_LPI_T(x) (jiffies + usecs_to_jiffies(x))
113 /* By default the driver will use the ring mode to manage tx and rx descriptors,
114 * but allow user to force to use the chain instead of the ring
116 static unsigned int chain_mode;
117 module_param(chain_mode, int, 0444);
118 MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
120 static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
121 /* For MSI interrupts handling */
122 static irqreturn_t stmmac_mac_interrupt(int irq, void *dev_id);
123 static irqreturn_t stmmac_safety_interrupt(int irq, void *dev_id);
124 static irqreturn_t stmmac_msi_intr_tx(int irq, void *data);
125 static irqreturn_t stmmac_msi_intr_rx(int irq, void *data);
126 static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue);
127 static void stmmac_flush_tx_descriptors(struct stmmac_priv *priv, int queue);
129 #ifdef CONFIG_DEBUG_FS
130 static const struct net_device_ops stmmac_netdev_ops;
131 static void stmmac_init_fs(struct net_device *dev);
132 static void stmmac_exit_fs(struct net_device *dev);
135 #define STMMAC_COAL_TIMER(x) (ns_to_ktime((x) * NSEC_PER_USEC))
137 int stmmac_bus_clks_config(struct stmmac_priv *priv, bool enabled)
142 ret = clk_prepare_enable(priv->plat->stmmac_clk);
145 ret = clk_prepare_enable(priv->plat->pclk);
147 clk_disable_unprepare(priv->plat->stmmac_clk);
150 if (priv->plat->clks_config) {
151 ret = priv->plat->clks_config(priv->plat->bsp_priv, enabled);
153 clk_disable_unprepare(priv->plat->stmmac_clk);
154 clk_disable_unprepare(priv->plat->pclk);
159 clk_disable_unprepare(priv->plat->stmmac_clk);
160 clk_disable_unprepare(priv->plat->pclk);
161 if (priv->plat->clks_config)
162 priv->plat->clks_config(priv->plat->bsp_priv, enabled);
167 EXPORT_SYMBOL_GPL(stmmac_bus_clks_config);
170 * stmmac_verify_args - verify the driver parameters.
171 * Description: it checks the driver parameters and set a default in case of
174 static void stmmac_verify_args(void)
176 if (unlikely(watchdog < 0))
178 if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
179 buf_sz = DEFAULT_BUFSIZE;
180 if (unlikely(flow_ctrl > 1))
181 flow_ctrl = FLOW_AUTO;
182 else if (likely(flow_ctrl < 0))
183 flow_ctrl = FLOW_OFF;
184 if (unlikely((pause < 0) || (pause > 0xffff)))
187 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
190 static void __stmmac_disable_all_queues(struct stmmac_priv *priv)
192 u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
193 u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
194 u32 maxq = max(rx_queues_cnt, tx_queues_cnt);
197 for (queue = 0; queue < maxq; queue++) {
198 struct stmmac_channel *ch = &priv->channel[queue];
200 if (stmmac_xdp_is_enabled(priv) &&
201 test_bit(queue, priv->af_xdp_zc_qps)) {
202 napi_disable(&ch->rxtx_napi);
206 if (queue < rx_queues_cnt)
207 napi_disable(&ch->rx_napi);
208 if (queue < tx_queues_cnt)
209 napi_disable(&ch->tx_napi);
214 * stmmac_disable_all_queues - Disable all queues
215 * @priv: driver private structure
217 static void stmmac_disable_all_queues(struct stmmac_priv *priv)
219 u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
220 struct stmmac_rx_queue *rx_q;
223 /* synchronize_rcu() needed for pending XDP buffers to drain */
224 for (queue = 0; queue < rx_queues_cnt; queue++) {
225 rx_q = &priv->rx_queue[queue];
226 if (rx_q->xsk_pool) {
232 __stmmac_disable_all_queues(priv);
236 * stmmac_enable_all_queues - Enable all queues
237 * @priv: driver private structure
239 static void stmmac_enable_all_queues(struct stmmac_priv *priv)
241 u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
242 u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
243 u32 maxq = max(rx_queues_cnt, tx_queues_cnt);
246 for (queue = 0; queue < maxq; queue++) {
247 struct stmmac_channel *ch = &priv->channel[queue];
249 if (stmmac_xdp_is_enabled(priv) &&
250 test_bit(queue, priv->af_xdp_zc_qps)) {
251 napi_enable(&ch->rxtx_napi);
255 if (queue < rx_queues_cnt)
256 napi_enable(&ch->rx_napi);
257 if (queue < tx_queues_cnt)
258 napi_enable(&ch->tx_napi);
262 static void stmmac_service_event_schedule(struct stmmac_priv *priv)
264 if (!test_bit(STMMAC_DOWN, &priv->state) &&
265 !test_and_set_bit(STMMAC_SERVICE_SCHED, &priv->state))
266 queue_work(priv->wq, &priv->service_task);
269 static void stmmac_global_err(struct stmmac_priv *priv)
271 netif_carrier_off(priv->dev);
272 set_bit(STMMAC_RESET_REQUESTED, &priv->state);
273 stmmac_service_event_schedule(priv);
277 * stmmac_clk_csr_set - dynamically set the MDC clock
278 * @priv: driver private structure
279 * Description: this is to dynamically set the MDC clock according to the csr
282 * If a specific clk_csr value is passed from the platform
283 * this means that the CSR Clock Range selection cannot be
284 * changed at run-time and it is fixed (as reported in the driver
285 * documentation). Viceversa the driver will try to set the MDC
286 * clock dynamically according to the actual clock input.
288 static void stmmac_clk_csr_set(struct stmmac_priv *priv)
292 clk_rate = clk_get_rate(priv->plat->stmmac_clk);
294 /* Platform provided default clk_csr would be assumed valid
295 * for all other cases except for the below mentioned ones.
296 * For values higher than the IEEE 802.3 specified frequency
297 * we can not estimate the proper divider as it is not known
298 * the frequency of clk_csr_i. So we do not change the default
301 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
302 if (clk_rate < CSR_F_35M)
303 priv->clk_csr = STMMAC_CSR_20_35M;
304 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
305 priv->clk_csr = STMMAC_CSR_35_60M;
306 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
307 priv->clk_csr = STMMAC_CSR_60_100M;
308 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
309 priv->clk_csr = STMMAC_CSR_100_150M;
310 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
311 priv->clk_csr = STMMAC_CSR_150_250M;
312 else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
313 priv->clk_csr = STMMAC_CSR_250_300M;
316 if (priv->plat->has_sun8i) {
317 if (clk_rate > 160000000)
318 priv->clk_csr = 0x03;
319 else if (clk_rate > 80000000)
320 priv->clk_csr = 0x02;
321 else if (clk_rate > 40000000)
322 priv->clk_csr = 0x01;
327 if (priv->plat->has_xgmac) {
328 if (clk_rate > 400000000)
330 else if (clk_rate > 350000000)
332 else if (clk_rate > 300000000)
334 else if (clk_rate > 250000000)
336 else if (clk_rate > 150000000)
343 static void print_pkt(unsigned char *buf, int len)
345 pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
346 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
349 static inline u32 stmmac_tx_avail(struct stmmac_priv *priv, u32 queue)
351 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
354 if (tx_q->dirty_tx > tx_q->cur_tx)
355 avail = tx_q->dirty_tx - tx_q->cur_tx - 1;
357 avail = priv->dma_tx_size - tx_q->cur_tx + tx_q->dirty_tx - 1;
363 * stmmac_rx_dirty - Get RX queue dirty
364 * @priv: driver private structure
365 * @queue: RX queue index
367 static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv, u32 queue)
369 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
372 if (rx_q->dirty_rx <= rx_q->cur_rx)
373 dirty = rx_q->cur_rx - rx_q->dirty_rx;
375 dirty = priv->dma_rx_size - rx_q->dirty_rx + rx_q->cur_rx;
380 static void stmmac_lpi_entry_timer_config(struct stmmac_priv *priv, bool en)
384 /* Clear/set the SW EEE timer flag based on LPI ET enablement */
385 priv->eee_sw_timer_en = en ? 0 : 1;
386 tx_lpi_timer = en ? priv->tx_lpi_timer : 0;
387 stmmac_set_eee_lpi_timer(priv, priv->hw, tx_lpi_timer);
391 * stmmac_enable_eee_mode - check and enter in LPI mode
392 * @priv: driver private structure
393 * Description: this function is to verify and enter in LPI mode in case of
396 static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
398 u32 tx_cnt = priv->plat->tx_queues_to_use;
401 /* check if all TX queues have the work finished */
402 for (queue = 0; queue < tx_cnt; queue++) {
403 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
405 if (tx_q->dirty_tx != tx_q->cur_tx)
406 return; /* still unfinished work */
409 /* Check and enter in LPI mode */
410 if (!priv->tx_path_in_lpi_mode)
411 stmmac_set_eee_mode(priv, priv->hw,
412 priv->plat->en_tx_lpi_clockgating);
416 * stmmac_disable_eee_mode - disable and exit from LPI mode
417 * @priv: driver private structure
418 * Description: this function is to exit and disable EEE in case of
419 * LPI state is true. This is called by the xmit.
421 void stmmac_disable_eee_mode(struct stmmac_priv *priv)
423 if (!priv->eee_sw_timer_en) {
424 stmmac_lpi_entry_timer_config(priv, 0);
428 stmmac_reset_eee_mode(priv, priv->hw);
429 del_timer_sync(&priv->eee_ctrl_timer);
430 priv->tx_path_in_lpi_mode = false;
434 * stmmac_eee_ctrl_timer - EEE TX SW timer.
435 * @t: timer_list struct containing private info
437 * if there is no data transfer and if we are not in LPI state,
438 * then MAC Transmitter can be moved to LPI state.
440 static void stmmac_eee_ctrl_timer(struct timer_list *t)
442 struct stmmac_priv *priv = from_timer(priv, t, eee_ctrl_timer);
444 stmmac_enable_eee_mode(priv);
445 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer));
449 * stmmac_eee_init - init EEE
450 * @priv: driver private structure
452 * if the GMAC supports the EEE (from the HW cap reg) and the phy device
453 * can also manage EEE, this function enable the LPI state and start related
456 bool stmmac_eee_init(struct stmmac_priv *priv)
458 int eee_tw_timer = priv->eee_tw_timer;
460 /* Using PCS we cannot dial with the phy registers at this stage
461 * so we do not support extra feature like EEE.
463 if (priv->hw->pcs == STMMAC_PCS_TBI ||
464 priv->hw->pcs == STMMAC_PCS_RTBI)
467 /* Check if MAC core supports the EEE feature. */
468 if (!priv->dma_cap.eee)
471 mutex_lock(&priv->lock);
473 /* Check if it needs to be deactivated */
474 if (!priv->eee_active) {
475 if (priv->eee_enabled) {
476 netdev_dbg(priv->dev, "disable EEE\n");
477 stmmac_lpi_entry_timer_config(priv, 0);
478 del_timer_sync(&priv->eee_ctrl_timer);
479 stmmac_set_eee_timer(priv, priv->hw, 0, eee_tw_timer);
481 mutex_unlock(&priv->lock);
485 if (priv->eee_active && !priv->eee_enabled) {
486 timer_setup(&priv->eee_ctrl_timer, stmmac_eee_ctrl_timer, 0);
487 stmmac_set_eee_timer(priv, priv->hw, STMMAC_DEFAULT_LIT_LS,
491 if (priv->plat->has_gmac4 && priv->tx_lpi_timer <= STMMAC_ET_MAX) {
492 del_timer_sync(&priv->eee_ctrl_timer);
493 priv->tx_path_in_lpi_mode = false;
494 stmmac_lpi_entry_timer_config(priv, 1);
496 stmmac_lpi_entry_timer_config(priv, 0);
497 mod_timer(&priv->eee_ctrl_timer,
498 STMMAC_LPI_T(priv->tx_lpi_timer));
501 mutex_unlock(&priv->lock);
502 netdev_dbg(priv->dev, "Energy-Efficient Ethernet initialized\n");
506 /* stmmac_get_tx_hwtstamp - get HW TX timestamps
507 * @priv: driver private structure
508 * @p : descriptor pointer
509 * @skb : the socket buffer
511 * This function will read timestamp from the descriptor & pass it to stack.
512 * and also perform some sanity checks.
514 static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
515 struct dma_desc *p, struct sk_buff *skb)
517 struct skb_shared_hwtstamps shhwtstamp;
522 if (!priv->hwts_tx_en)
525 /* exit if skb doesn't support hw tstamp */
526 if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
529 /* check tx tstamp status */
530 if (stmmac_get_tx_timestamp_status(priv, p)) {
531 stmmac_get_timestamp(priv, p, priv->adv_ts, &ns);
533 } else if (!stmmac_get_mac_tx_timestamp(priv, priv->hw, &ns)) {
538 /* Correct the clk domain crossing(CDC) error */
539 if (priv->plat->has_gmac4 && priv->plat->clk_ptp_rate) {
540 adjust += -(2 * (NSEC_PER_SEC /
541 priv->plat->clk_ptp_rate));
545 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
546 shhwtstamp.hwtstamp = ns_to_ktime(ns);
548 netdev_dbg(priv->dev, "get valid TX hw timestamp %llu\n", ns);
549 /* pass tstamp to stack */
550 skb_tstamp_tx(skb, &shhwtstamp);
554 /* stmmac_get_rx_hwtstamp - get HW RX timestamps
555 * @priv: driver private structure
556 * @p : descriptor pointer
557 * @np : next descriptor pointer
558 * @skb : the socket buffer
560 * This function will read received packet's timestamp from the descriptor
561 * and pass it to stack. It also perform some sanity checks.
563 static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,
564 struct dma_desc *np, struct sk_buff *skb)
566 struct skb_shared_hwtstamps *shhwtstamp = NULL;
567 struct dma_desc *desc = p;
571 if (!priv->hwts_rx_en)
573 /* For GMAC4, the valid timestamp is from CTX next desc. */
574 if (priv->plat->has_gmac4 || priv->plat->has_xgmac)
577 /* Check if timestamp is available */
578 if (stmmac_get_rx_timestamp_status(priv, p, np, priv->adv_ts)) {
579 stmmac_get_timestamp(priv, desc, priv->adv_ts, &ns);
581 /* Correct the clk domain crossing(CDC) error */
582 if (priv->plat->has_gmac4 && priv->plat->clk_ptp_rate) {
583 adjust += 2 * (NSEC_PER_SEC / priv->plat->clk_ptp_rate);
587 netdev_dbg(priv->dev, "get valid RX hw timestamp %llu\n", ns);
588 shhwtstamp = skb_hwtstamps(skb);
589 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
590 shhwtstamp->hwtstamp = ns_to_ktime(ns);
592 netdev_dbg(priv->dev, "cannot get RX hw timestamp\n");
597 * stmmac_hwtstamp_set - control hardware timestamping.
598 * @dev: device pointer.
599 * @ifr: An IOCTL specific structure, that can contain a pointer to
600 * a proprietary structure used to pass information to the driver.
602 * This function configures the MAC to enable/disable both outgoing(TX)
603 * and incoming(RX) packets time stamping based on user input.
605 * 0 on success and an appropriate -ve integer on failure.
607 static int stmmac_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
609 struct stmmac_priv *priv = netdev_priv(dev);
610 struct hwtstamp_config config;
611 struct timespec64 now;
615 u32 ptp_over_ipv4_udp = 0;
616 u32 ptp_over_ipv6_udp = 0;
617 u32 ptp_over_ethernet = 0;
618 u32 snap_type_sel = 0;
619 u32 ts_master_en = 0;
625 xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
627 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
628 netdev_alert(priv->dev, "No support for HW time stamping\n");
629 priv->hwts_tx_en = 0;
630 priv->hwts_rx_en = 0;
635 if (copy_from_user(&config, ifr->ifr_data,
639 netdev_dbg(priv->dev, "%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
640 __func__, config.flags, config.tx_type, config.rx_filter);
642 /* reserved for future extensions */
646 if (config.tx_type != HWTSTAMP_TX_OFF &&
647 config.tx_type != HWTSTAMP_TX_ON)
651 switch (config.rx_filter) {
652 case HWTSTAMP_FILTER_NONE:
653 /* time stamp no incoming packet at all */
654 config.rx_filter = HWTSTAMP_FILTER_NONE;
657 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
658 /* PTP v1, UDP, any kind of event packet */
659 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
660 /* 'xmac' hardware can support Sync, Pdelay_Req and
661 * Pdelay_resp by setting bit14 and bits17/16 to 01
662 * This leaves Delay_Req timestamps out.
663 * Enable all events *and* general purpose message
666 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
667 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
668 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
671 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
672 /* PTP v1, UDP, Sync packet */
673 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
674 /* take time stamp for SYNC messages only */
675 ts_event_en = PTP_TCR_TSEVNTENA;
677 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
678 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
681 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
682 /* PTP v1, UDP, Delay_req packet */
683 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
684 /* take time stamp for Delay_Req messages only */
685 ts_master_en = PTP_TCR_TSMSTRENA;
686 ts_event_en = PTP_TCR_TSEVNTENA;
688 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
689 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
692 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
693 /* PTP v2, UDP, any kind of event packet */
694 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
695 ptp_v2 = PTP_TCR_TSVER2ENA;
696 /* take time stamp for all event messages */
697 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
699 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
700 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
703 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
704 /* PTP v2, UDP, Sync packet */
705 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
706 ptp_v2 = PTP_TCR_TSVER2ENA;
707 /* take time stamp for SYNC messages only */
708 ts_event_en = PTP_TCR_TSEVNTENA;
710 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
711 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
714 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
715 /* PTP v2, UDP, Delay_req packet */
716 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
717 ptp_v2 = PTP_TCR_TSVER2ENA;
718 /* take time stamp for Delay_Req messages only */
719 ts_master_en = PTP_TCR_TSMSTRENA;
720 ts_event_en = PTP_TCR_TSEVNTENA;
722 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
723 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
726 case HWTSTAMP_FILTER_PTP_V2_EVENT:
727 /* PTP v2/802.AS1 any layer, any kind of event packet */
728 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
729 ptp_v2 = PTP_TCR_TSVER2ENA;
730 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
731 if (priv->synopsys_id != DWMAC_CORE_5_10)
732 ts_event_en = PTP_TCR_TSEVNTENA;
733 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
734 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
735 ptp_over_ethernet = PTP_TCR_TSIPENA;
738 case HWTSTAMP_FILTER_PTP_V2_SYNC:
739 /* PTP v2/802.AS1, any layer, Sync packet */
740 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
741 ptp_v2 = PTP_TCR_TSVER2ENA;
742 /* take time stamp for SYNC messages only */
743 ts_event_en = PTP_TCR_TSEVNTENA;
745 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
746 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
747 ptp_over_ethernet = PTP_TCR_TSIPENA;
750 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
751 /* PTP v2/802.AS1, any layer, Delay_req packet */
752 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
753 ptp_v2 = PTP_TCR_TSVER2ENA;
754 /* take time stamp for Delay_Req messages only */
755 ts_master_en = PTP_TCR_TSMSTRENA;
756 ts_event_en = PTP_TCR_TSEVNTENA;
758 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
759 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
760 ptp_over_ethernet = PTP_TCR_TSIPENA;
763 case HWTSTAMP_FILTER_NTP_ALL:
764 case HWTSTAMP_FILTER_ALL:
765 /* time stamp any incoming packet */
766 config.rx_filter = HWTSTAMP_FILTER_ALL;
767 tstamp_all = PTP_TCR_TSENALL;
774 switch (config.rx_filter) {
775 case HWTSTAMP_FILTER_NONE:
776 config.rx_filter = HWTSTAMP_FILTER_NONE;
779 /* PTP v1, UDP, any kind of event packet */
780 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
784 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
785 priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
787 if (!priv->hwts_tx_en && !priv->hwts_rx_en)
788 stmmac_config_hw_tstamping(priv, priv->ptpaddr, 0);
790 value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
791 tstamp_all | ptp_v2 | ptp_over_ethernet |
792 ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
793 ts_master_en | snap_type_sel);
794 stmmac_config_hw_tstamping(priv, priv->ptpaddr, value);
796 /* program Sub Second Increment reg */
797 stmmac_config_sub_second_increment(priv,
798 priv->ptpaddr, priv->plat->clk_ptp_rate,
800 temp = div_u64(1000000000ULL, sec_inc);
802 /* Store sub second increment and flags for later use */
803 priv->sub_second_inc = sec_inc;
804 priv->systime_flags = value;
806 /* calculate default added value:
808 * addend = (2^32)/freq_div_ratio;
809 * where, freq_div_ratio = 1e9ns/sec_inc
811 temp = (u64)(temp << 32);
812 priv->default_addend = div_u64(temp, priv->plat->clk_ptp_rate);
813 stmmac_config_addend(priv, priv->ptpaddr, priv->default_addend);
815 /* initialize system time */
816 ktime_get_real_ts64(&now);
818 /* lower 32 bits of tv_sec are safe until y2106 */
819 stmmac_init_systime(priv, priv->ptpaddr,
820 (u32)now.tv_sec, now.tv_nsec);
823 memcpy(&priv->tstamp_config, &config, sizeof(config));
825 return copy_to_user(ifr->ifr_data, &config,
826 sizeof(config)) ? -EFAULT : 0;
830 * stmmac_hwtstamp_get - read hardware timestamping.
831 * @dev: device pointer.
832 * @ifr: An IOCTL specific structure, that can contain a pointer to
833 * a proprietary structure used to pass information to the driver.
835 * This function obtain the current hardware timestamping settings
838 static int stmmac_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
840 struct stmmac_priv *priv = netdev_priv(dev);
841 struct hwtstamp_config *config = &priv->tstamp_config;
843 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
846 return copy_to_user(ifr->ifr_data, config,
847 sizeof(*config)) ? -EFAULT : 0;
851 * stmmac_init_ptp - init PTP
852 * @priv: driver private structure
853 * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
854 * This is done by looking at the HW cap. register.
855 * This function also registers the ptp driver.
857 static int stmmac_init_ptp(struct stmmac_priv *priv)
859 bool xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
861 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
865 /* Check if adv_ts can be enabled for dwmac 4.x / xgmac core */
866 if (xmac && priv->dma_cap.atime_stamp)
868 /* Dwmac 3.x core with extend_desc can support adv_ts */
869 else if (priv->extend_desc && priv->dma_cap.atime_stamp)
872 if (priv->dma_cap.time_stamp)
873 netdev_info(priv->dev, "IEEE 1588-2002 Timestamp supported\n");
876 netdev_info(priv->dev,
877 "IEEE 1588-2008 Advanced Timestamp supported\n");
879 priv->hwts_tx_en = 0;
880 priv->hwts_rx_en = 0;
882 stmmac_ptp_register(priv);
887 static void stmmac_release_ptp(struct stmmac_priv *priv)
889 clk_disable_unprepare(priv->plat->clk_ptp_ref);
890 stmmac_ptp_unregister(priv);
894 * stmmac_mac_flow_ctrl - Configure flow control in all queues
895 * @priv: driver private structure
896 * @duplex: duplex passed to the next function
897 * Description: It is used for configuring the flow control in all queues
899 static void stmmac_mac_flow_ctrl(struct stmmac_priv *priv, u32 duplex)
901 u32 tx_cnt = priv->plat->tx_queues_to_use;
903 stmmac_flow_ctrl(priv, priv->hw, duplex, priv->flow_ctrl,
904 priv->pause, tx_cnt);
907 static void stmmac_validate(struct phylink_config *config,
908 unsigned long *supported,
909 struct phylink_link_state *state)
911 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
912 __ETHTOOL_DECLARE_LINK_MODE_MASK(mac_supported) = { 0, };
913 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
914 int tx_cnt = priv->plat->tx_queues_to_use;
915 int max_speed = priv->plat->max_speed;
917 phylink_set(mac_supported, 10baseT_Half);
918 phylink_set(mac_supported, 10baseT_Full);
919 phylink_set(mac_supported, 100baseT_Half);
920 phylink_set(mac_supported, 100baseT_Full);
921 phylink_set(mac_supported, 1000baseT_Half);
922 phylink_set(mac_supported, 1000baseT_Full);
923 phylink_set(mac_supported, 1000baseKX_Full);
925 phylink_set(mac_supported, Autoneg);
926 phylink_set(mac_supported, Pause);
927 phylink_set(mac_supported, Asym_Pause);
928 phylink_set_port_modes(mac_supported);
930 /* Cut down 1G if asked to */
931 if ((max_speed > 0) && (max_speed < 1000)) {
932 phylink_set(mask, 1000baseT_Full);
933 phylink_set(mask, 1000baseX_Full);
934 } else if (priv->plat->has_gmac4) {
935 if (!max_speed || max_speed >= 2500) {
936 phylink_set(mac_supported, 2500baseT_Full);
937 phylink_set(mac_supported, 2500baseX_Full);
939 } else if (priv->plat->has_xgmac) {
940 if (!max_speed || (max_speed >= 2500)) {
941 phylink_set(mac_supported, 2500baseT_Full);
942 phylink_set(mac_supported, 2500baseX_Full);
944 if (!max_speed || (max_speed >= 5000)) {
945 phylink_set(mac_supported, 5000baseT_Full);
947 if (!max_speed || (max_speed >= 10000)) {
948 phylink_set(mac_supported, 10000baseSR_Full);
949 phylink_set(mac_supported, 10000baseLR_Full);
950 phylink_set(mac_supported, 10000baseER_Full);
951 phylink_set(mac_supported, 10000baseLRM_Full);
952 phylink_set(mac_supported, 10000baseT_Full);
953 phylink_set(mac_supported, 10000baseKX4_Full);
954 phylink_set(mac_supported, 10000baseKR_Full);
956 if (!max_speed || (max_speed >= 25000)) {
957 phylink_set(mac_supported, 25000baseCR_Full);
958 phylink_set(mac_supported, 25000baseKR_Full);
959 phylink_set(mac_supported, 25000baseSR_Full);
961 if (!max_speed || (max_speed >= 40000)) {
962 phylink_set(mac_supported, 40000baseKR4_Full);
963 phylink_set(mac_supported, 40000baseCR4_Full);
964 phylink_set(mac_supported, 40000baseSR4_Full);
965 phylink_set(mac_supported, 40000baseLR4_Full);
967 if (!max_speed || (max_speed >= 50000)) {
968 phylink_set(mac_supported, 50000baseCR2_Full);
969 phylink_set(mac_supported, 50000baseKR2_Full);
970 phylink_set(mac_supported, 50000baseSR2_Full);
971 phylink_set(mac_supported, 50000baseKR_Full);
972 phylink_set(mac_supported, 50000baseSR_Full);
973 phylink_set(mac_supported, 50000baseCR_Full);
974 phylink_set(mac_supported, 50000baseLR_ER_FR_Full);
975 phylink_set(mac_supported, 50000baseDR_Full);
977 if (!max_speed || (max_speed >= 100000)) {
978 phylink_set(mac_supported, 100000baseKR4_Full);
979 phylink_set(mac_supported, 100000baseSR4_Full);
980 phylink_set(mac_supported, 100000baseCR4_Full);
981 phylink_set(mac_supported, 100000baseLR4_ER4_Full);
982 phylink_set(mac_supported, 100000baseKR2_Full);
983 phylink_set(mac_supported, 100000baseSR2_Full);
984 phylink_set(mac_supported, 100000baseCR2_Full);
985 phylink_set(mac_supported, 100000baseLR2_ER2_FR2_Full);
986 phylink_set(mac_supported, 100000baseDR2_Full);
990 /* Half-Duplex can only work with single queue */
992 phylink_set(mask, 10baseT_Half);
993 phylink_set(mask, 100baseT_Half);
994 phylink_set(mask, 1000baseT_Half);
997 linkmode_and(supported, supported, mac_supported);
998 linkmode_andnot(supported, supported, mask);
1000 linkmode_and(state->advertising, state->advertising, mac_supported);
1001 linkmode_andnot(state->advertising, state->advertising, mask);
1003 /* If PCS is supported, check which modes it supports. */
1005 xpcs_validate(priv->hw->xpcs, supported, state);
1008 static void stmmac_mac_config(struct phylink_config *config, unsigned int mode,
1009 const struct phylink_link_state *state)
1011 /* Nothing to do, xpcs_config() handles everything */
1014 static void stmmac_fpe_link_state_handle(struct stmmac_priv *priv, bool is_up)
1016 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
1017 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
1018 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
1019 bool *hs_enable = &fpe_cfg->hs_enable;
1021 if (is_up && *hs_enable) {
1022 stmmac_fpe_send_mpacket(priv, priv->ioaddr, MPACKET_VERIFY);
1024 *lo_state = FPE_STATE_OFF;
1025 *lp_state = FPE_STATE_OFF;
1029 static void stmmac_mac_link_down(struct phylink_config *config,
1030 unsigned int mode, phy_interface_t interface)
1032 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
1034 stmmac_mac_set(priv, priv->ioaddr, false);
1035 priv->eee_active = false;
1036 priv->tx_lpi_enabled = false;
1037 stmmac_eee_init(priv);
1038 stmmac_set_eee_pls(priv, priv->hw, false);
1040 if (priv->dma_cap.fpesel)
1041 stmmac_fpe_link_state_handle(priv, false);
1044 static void stmmac_mac_link_up(struct phylink_config *config,
1045 struct phy_device *phy,
1046 unsigned int mode, phy_interface_t interface,
1047 int speed, int duplex,
1048 bool tx_pause, bool rx_pause)
1050 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
1053 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
1054 ctrl &= ~priv->hw->link.speed_mask;
1056 if (interface == PHY_INTERFACE_MODE_USXGMII) {
1059 ctrl |= priv->hw->link.xgmii.speed10000;
1062 ctrl |= priv->hw->link.xgmii.speed5000;
1065 ctrl |= priv->hw->link.xgmii.speed2500;
1070 } else if (interface == PHY_INTERFACE_MODE_XLGMII) {
1073 ctrl |= priv->hw->link.xlgmii.speed100000;
1076 ctrl |= priv->hw->link.xlgmii.speed50000;
1079 ctrl |= priv->hw->link.xlgmii.speed40000;
1082 ctrl |= priv->hw->link.xlgmii.speed25000;
1085 ctrl |= priv->hw->link.xgmii.speed10000;
1088 ctrl |= priv->hw->link.speed2500;
1091 ctrl |= priv->hw->link.speed1000;
1099 ctrl |= priv->hw->link.speed2500;
1102 ctrl |= priv->hw->link.speed1000;
1105 ctrl |= priv->hw->link.speed100;
1108 ctrl |= priv->hw->link.speed10;
1115 priv->speed = speed;
1117 if (priv->plat->fix_mac_speed)
1118 priv->plat->fix_mac_speed(priv->plat->bsp_priv, speed);
1121 ctrl &= ~priv->hw->link.duplex;
1123 ctrl |= priv->hw->link.duplex;
1125 /* Flow Control operation */
1126 if (tx_pause && rx_pause)
1127 stmmac_mac_flow_ctrl(priv, duplex);
1129 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
1131 stmmac_mac_set(priv, priv->ioaddr, true);
1132 if (phy && priv->dma_cap.eee) {
1133 priv->eee_active = phy_init_eee(phy, 1) >= 0;
1134 priv->eee_enabled = stmmac_eee_init(priv);
1135 priv->tx_lpi_enabled = priv->eee_enabled;
1136 stmmac_set_eee_pls(priv, priv->hw, true);
1139 if (priv->dma_cap.fpesel)
1140 stmmac_fpe_link_state_handle(priv, true);
1143 static const struct phylink_mac_ops stmmac_phylink_mac_ops = {
1144 .validate = stmmac_validate,
1145 .mac_config = stmmac_mac_config,
1146 .mac_link_down = stmmac_mac_link_down,
1147 .mac_link_up = stmmac_mac_link_up,
1151 * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
1152 * @priv: driver private structure
1153 * Description: this is to verify if the HW supports the PCS.
1154 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
1155 * configured for the TBI, RTBI, or SGMII PHY interface.
1157 static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
1159 int interface = priv->plat->interface;
1161 if (priv->dma_cap.pcs) {
1162 if ((interface == PHY_INTERFACE_MODE_RGMII) ||
1163 (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
1164 (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
1165 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
1166 netdev_dbg(priv->dev, "PCS RGMII support enabled\n");
1167 priv->hw->pcs = STMMAC_PCS_RGMII;
1168 } else if (interface == PHY_INTERFACE_MODE_SGMII) {
1169 netdev_dbg(priv->dev, "PCS SGMII support enabled\n");
1170 priv->hw->pcs = STMMAC_PCS_SGMII;
1176 * stmmac_init_phy - PHY initialization
1177 * @dev: net device structure
1178 * Description: it initializes the driver's PHY state, and attaches the PHY
1179 * to the mac driver.
1183 static int stmmac_init_phy(struct net_device *dev)
1185 struct stmmac_priv *priv = netdev_priv(dev);
1186 struct device_node *node;
1189 node = priv->plat->phylink_node;
1192 ret = phylink_of_phy_connect(priv->phylink, node, 0);
1194 /* Some DT bindings do not set-up the PHY handle. Let's try to
1198 int addr = priv->plat->phy_addr;
1199 struct phy_device *phydev;
1201 phydev = mdiobus_get_phy(priv->mii, addr);
1203 netdev_err(priv->dev, "no phy at addr %d\n", addr);
1207 ret = phylink_connect_phy(priv->phylink, phydev);
1210 if (!priv->plat->pmt) {
1211 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1213 phylink_ethtool_get_wol(priv->phylink, &wol);
1214 device_set_wakeup_capable(priv->device, !!wol.supported);
1220 static int stmmac_phy_setup(struct stmmac_priv *priv)
1222 struct stmmac_mdio_bus_data *mdio_bus_data = priv->plat->mdio_bus_data;
1223 struct fwnode_handle *fwnode = of_fwnode_handle(priv->plat->phylink_node);
1224 int mode = priv->plat->phy_interface;
1225 struct phylink *phylink;
1227 priv->phylink_config.dev = &priv->dev->dev;
1228 priv->phylink_config.type = PHYLINK_NETDEV;
1229 priv->phylink_config.pcs_poll = true;
1230 if (priv->plat->mdio_bus_data)
1231 priv->phylink_config.ovr_an_inband =
1232 mdio_bus_data->xpcs_an_inband;
1235 fwnode = dev_fwnode(priv->device);
1237 phylink = phylink_create(&priv->phylink_config, fwnode,
1238 mode, &stmmac_phylink_mac_ops);
1239 if (IS_ERR(phylink))
1240 return PTR_ERR(phylink);
1243 phylink_set_pcs(phylink, &priv->hw->xpcs->pcs);
1245 priv->phylink = phylink;
1249 static void stmmac_display_rx_rings(struct stmmac_priv *priv)
1251 u32 rx_cnt = priv->plat->rx_queues_to_use;
1252 unsigned int desc_size;
1256 /* Display RX rings */
1257 for (queue = 0; queue < rx_cnt; queue++) {
1258 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1260 pr_info("\tRX Queue %u rings\n", queue);
1262 if (priv->extend_desc) {
1263 head_rx = (void *)rx_q->dma_erx;
1264 desc_size = sizeof(struct dma_extended_desc);
1266 head_rx = (void *)rx_q->dma_rx;
1267 desc_size = sizeof(struct dma_desc);
1270 /* Display RX ring */
1271 stmmac_display_ring(priv, head_rx, priv->dma_rx_size, true,
1272 rx_q->dma_rx_phy, desc_size);
1276 static void stmmac_display_tx_rings(struct stmmac_priv *priv)
1278 u32 tx_cnt = priv->plat->tx_queues_to_use;
1279 unsigned int desc_size;
1283 /* Display TX rings */
1284 for (queue = 0; queue < tx_cnt; queue++) {
1285 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1287 pr_info("\tTX Queue %d rings\n", queue);
1289 if (priv->extend_desc) {
1290 head_tx = (void *)tx_q->dma_etx;
1291 desc_size = sizeof(struct dma_extended_desc);
1292 } else if (tx_q->tbs & STMMAC_TBS_AVAIL) {
1293 head_tx = (void *)tx_q->dma_entx;
1294 desc_size = sizeof(struct dma_edesc);
1296 head_tx = (void *)tx_q->dma_tx;
1297 desc_size = sizeof(struct dma_desc);
1300 stmmac_display_ring(priv, head_tx, priv->dma_tx_size, false,
1301 tx_q->dma_tx_phy, desc_size);
1305 static void stmmac_display_rings(struct stmmac_priv *priv)
1307 /* Display RX ring */
1308 stmmac_display_rx_rings(priv);
1310 /* Display TX ring */
1311 stmmac_display_tx_rings(priv);
1314 static int stmmac_set_bfsize(int mtu, int bufsize)
1318 if (mtu >= BUF_SIZE_8KiB)
1319 ret = BUF_SIZE_16KiB;
1320 else if (mtu >= BUF_SIZE_4KiB)
1321 ret = BUF_SIZE_8KiB;
1322 else if (mtu >= BUF_SIZE_2KiB)
1323 ret = BUF_SIZE_4KiB;
1324 else if (mtu > DEFAULT_BUFSIZE)
1325 ret = BUF_SIZE_2KiB;
1327 ret = DEFAULT_BUFSIZE;
1333 * stmmac_clear_rx_descriptors - clear RX descriptors
1334 * @priv: driver private structure
1335 * @queue: RX queue index
1336 * Description: this function is called to clear the RX descriptors
1337 * in case of both basic and extended descriptors are used.
1339 static void stmmac_clear_rx_descriptors(struct stmmac_priv *priv, u32 queue)
1341 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1344 /* Clear the RX descriptors */
1345 for (i = 0; i < priv->dma_rx_size; i++)
1346 if (priv->extend_desc)
1347 stmmac_init_rx_desc(priv, &rx_q->dma_erx[i].basic,
1348 priv->use_riwt, priv->mode,
1349 (i == priv->dma_rx_size - 1),
1352 stmmac_init_rx_desc(priv, &rx_q->dma_rx[i],
1353 priv->use_riwt, priv->mode,
1354 (i == priv->dma_rx_size - 1),
1359 * stmmac_clear_tx_descriptors - clear tx descriptors
1360 * @priv: driver private structure
1361 * @queue: TX queue index.
1362 * Description: this function is called to clear the TX descriptors
1363 * in case of both basic and extended descriptors are used.
1365 static void stmmac_clear_tx_descriptors(struct stmmac_priv *priv, u32 queue)
1367 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1370 /* Clear the TX descriptors */
1371 for (i = 0; i < priv->dma_tx_size; i++) {
1372 int last = (i == (priv->dma_tx_size - 1));
1375 if (priv->extend_desc)
1376 p = &tx_q->dma_etx[i].basic;
1377 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1378 p = &tx_q->dma_entx[i].basic;
1380 p = &tx_q->dma_tx[i];
1382 stmmac_init_tx_desc(priv, p, priv->mode, last);
1387 * stmmac_clear_descriptors - clear descriptors
1388 * @priv: driver private structure
1389 * Description: this function is called to clear the TX and RX descriptors
1390 * in case of both basic and extended descriptors are used.
1392 static void stmmac_clear_descriptors(struct stmmac_priv *priv)
1394 u32 rx_queue_cnt = priv->plat->rx_queues_to_use;
1395 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1398 /* Clear the RX descriptors */
1399 for (queue = 0; queue < rx_queue_cnt; queue++)
1400 stmmac_clear_rx_descriptors(priv, queue);
1402 /* Clear the TX descriptors */
1403 for (queue = 0; queue < tx_queue_cnt; queue++)
1404 stmmac_clear_tx_descriptors(priv, queue);
1408 * stmmac_init_rx_buffers - init the RX descriptor buffer.
1409 * @priv: driver private structure
1410 * @p: descriptor pointer
1411 * @i: descriptor index
1413 * @queue: RX queue index
1414 * Description: this function is called to allocate a receive buffer, perform
1415 * the DMA mapping and init the descriptor.
1417 static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
1418 int i, gfp_t flags, u32 queue)
1420 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1421 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1424 buf->page = page_pool_dev_alloc_pages(rx_q->page_pool);
1427 buf->page_offset = stmmac_rx_offset(priv);
1430 if (priv->sph && !buf->sec_page) {
1431 buf->sec_page = page_pool_dev_alloc_pages(rx_q->page_pool);
1435 buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
1436 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true);
1438 buf->sec_page = NULL;
1439 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false);
1442 buf->addr = page_pool_get_dma_addr(buf->page) + buf->page_offset;
1444 stmmac_set_desc_addr(priv, p, buf->addr);
1445 if (priv->dma_buf_sz == BUF_SIZE_16KiB)
1446 stmmac_init_desc3(priv, p);
1452 * stmmac_free_rx_buffer - free RX dma buffers
1453 * @priv: private structure
1454 * @queue: RX queue index
1457 static void stmmac_free_rx_buffer(struct stmmac_priv *priv, u32 queue, int i)
1459 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1460 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1463 page_pool_put_full_page(rx_q->page_pool, buf->page, false);
1467 page_pool_put_full_page(rx_q->page_pool, buf->sec_page, false);
1468 buf->sec_page = NULL;
1472 * stmmac_free_tx_buffer - free RX dma buffers
1473 * @priv: private structure
1474 * @queue: RX queue index
1477 static void stmmac_free_tx_buffer(struct stmmac_priv *priv, u32 queue, int i)
1479 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1481 if (tx_q->tx_skbuff_dma[i].buf &&
1482 tx_q->tx_skbuff_dma[i].buf_type != STMMAC_TXBUF_T_XDP_TX) {
1483 if (tx_q->tx_skbuff_dma[i].map_as_page)
1484 dma_unmap_page(priv->device,
1485 tx_q->tx_skbuff_dma[i].buf,
1486 tx_q->tx_skbuff_dma[i].len,
1489 dma_unmap_single(priv->device,
1490 tx_q->tx_skbuff_dma[i].buf,
1491 tx_q->tx_skbuff_dma[i].len,
1495 if (tx_q->xdpf[i] &&
1496 (tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XDP_TX ||
1497 tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XDP_NDO)) {
1498 xdp_return_frame(tx_q->xdpf[i]);
1499 tx_q->xdpf[i] = NULL;
1502 if (tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XSK_TX)
1503 tx_q->xsk_frames_done++;
1505 if (tx_q->tx_skbuff[i] &&
1506 tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_SKB) {
1507 dev_kfree_skb_any(tx_q->tx_skbuff[i]);
1508 tx_q->tx_skbuff[i] = NULL;
1511 tx_q->tx_skbuff_dma[i].buf = 0;
1512 tx_q->tx_skbuff_dma[i].map_as_page = false;
1516 * dma_free_rx_skbufs - free RX dma buffers
1517 * @priv: private structure
1518 * @queue: RX queue index
1520 static void dma_free_rx_skbufs(struct stmmac_priv *priv, u32 queue)
1524 for (i = 0; i < priv->dma_rx_size; i++)
1525 stmmac_free_rx_buffer(priv, queue, i);
1528 static int stmmac_alloc_rx_buffers(struct stmmac_priv *priv, u32 queue,
1531 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1534 for (i = 0; i < priv->dma_rx_size; i++) {
1538 if (priv->extend_desc)
1539 p = &((rx_q->dma_erx + i)->basic);
1541 p = rx_q->dma_rx + i;
1543 ret = stmmac_init_rx_buffers(priv, p, i, flags,
1548 rx_q->buf_alloc_num++;
1555 * dma_free_rx_xskbufs - free RX dma buffers from XSK pool
1556 * @priv: private structure
1557 * @queue: RX queue index
1559 static void dma_free_rx_xskbufs(struct stmmac_priv *priv, u32 queue)
1561 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1564 for (i = 0; i < priv->dma_rx_size; i++) {
1565 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1570 xsk_buff_free(buf->xdp);
1575 static int stmmac_alloc_rx_buffers_zc(struct stmmac_priv *priv, u32 queue)
1577 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1580 for (i = 0; i < priv->dma_rx_size; i++) {
1581 struct stmmac_rx_buffer *buf;
1582 dma_addr_t dma_addr;
1585 if (priv->extend_desc)
1586 p = (struct dma_desc *)(rx_q->dma_erx + i);
1588 p = rx_q->dma_rx + i;
1590 buf = &rx_q->buf_pool[i];
1592 buf->xdp = xsk_buff_alloc(rx_q->xsk_pool);
1596 dma_addr = xsk_buff_xdp_get_dma(buf->xdp);
1597 stmmac_set_desc_addr(priv, p, dma_addr);
1598 rx_q->buf_alloc_num++;
1604 static struct xsk_buff_pool *stmmac_get_xsk_pool(struct stmmac_priv *priv, u32 queue)
1606 if (!stmmac_xdp_is_enabled(priv) || !test_bit(queue, priv->af_xdp_zc_qps))
1609 return xsk_get_pool_from_qid(priv->dev, queue);
1613 * __init_dma_rx_desc_rings - init the RX descriptor ring (per queue)
1614 * @priv: driver private structure
1615 * @queue: RX queue index
1617 * Description: this function initializes the DMA RX descriptors
1618 * and allocates the socket buffers. It supports the chained and ring
1621 static int __init_dma_rx_desc_rings(struct stmmac_priv *priv, u32 queue, gfp_t flags)
1623 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1626 netif_dbg(priv, probe, priv->dev,
1627 "(%s) dma_rx_phy=0x%08x\n", __func__,
1628 (u32)rx_q->dma_rx_phy);
1630 stmmac_clear_rx_descriptors(priv, queue);
1632 xdp_rxq_info_unreg_mem_model(&rx_q->xdp_rxq);
1634 rx_q->xsk_pool = stmmac_get_xsk_pool(priv, queue);
1636 if (rx_q->xsk_pool) {
1637 WARN_ON(xdp_rxq_info_reg_mem_model(&rx_q->xdp_rxq,
1638 MEM_TYPE_XSK_BUFF_POOL,
1640 netdev_info(priv->dev,
1641 "Register MEM_TYPE_XSK_BUFF_POOL RxQ-%d\n",
1643 xsk_pool_set_rxq_info(rx_q->xsk_pool, &rx_q->xdp_rxq);
1645 WARN_ON(xdp_rxq_info_reg_mem_model(&rx_q->xdp_rxq,
1648 netdev_info(priv->dev,
1649 "Register MEM_TYPE_PAGE_POOL RxQ-%d\n",
1653 if (rx_q->xsk_pool) {
1654 /* RX XDP ZC buffer pool may not be populated, e.g.
1657 stmmac_alloc_rx_buffers_zc(priv, queue);
1659 ret = stmmac_alloc_rx_buffers(priv, queue, flags);
1667 /* Setup the chained descriptor addresses */
1668 if (priv->mode == STMMAC_CHAIN_MODE) {
1669 if (priv->extend_desc)
1670 stmmac_mode_init(priv, rx_q->dma_erx,
1672 priv->dma_rx_size, 1);
1674 stmmac_mode_init(priv, rx_q->dma_rx,
1676 priv->dma_rx_size, 0);
1682 static int init_dma_rx_desc_rings(struct net_device *dev, gfp_t flags)
1684 struct stmmac_priv *priv = netdev_priv(dev);
1685 u32 rx_count = priv->plat->rx_queues_to_use;
1689 /* RX INITIALIZATION */
1690 netif_dbg(priv, probe, priv->dev,
1691 "SKB addresses:\nskb\t\tskb data\tdma data\n");
1693 for (queue = 0; queue < rx_count; queue++) {
1694 ret = __init_dma_rx_desc_rings(priv, queue, flags);
1696 goto err_init_rx_buffers;
1701 err_init_rx_buffers:
1702 while (queue >= 0) {
1703 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1706 dma_free_rx_xskbufs(priv, queue);
1708 dma_free_rx_skbufs(priv, queue);
1710 rx_q->buf_alloc_num = 0;
1711 rx_q->xsk_pool = NULL;
1723 * __init_dma_tx_desc_rings - init the TX descriptor ring (per queue)
1724 * @priv: driver private structure
1725 * @queue : TX queue index
1726 * Description: this function initializes the DMA TX descriptors
1727 * and allocates the socket buffers. It supports the chained and ring
1730 static int __init_dma_tx_desc_rings(struct stmmac_priv *priv, u32 queue)
1732 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1735 netif_dbg(priv, probe, priv->dev,
1736 "(%s) dma_tx_phy=0x%08x\n", __func__,
1737 (u32)tx_q->dma_tx_phy);
1739 /* Setup the chained descriptor addresses */
1740 if (priv->mode == STMMAC_CHAIN_MODE) {
1741 if (priv->extend_desc)
1742 stmmac_mode_init(priv, tx_q->dma_etx,
1744 priv->dma_tx_size, 1);
1745 else if (!(tx_q->tbs & STMMAC_TBS_AVAIL))
1746 stmmac_mode_init(priv, tx_q->dma_tx,
1748 priv->dma_tx_size, 0);
1751 tx_q->xsk_pool = stmmac_get_xsk_pool(priv, queue);
1753 for (i = 0; i < priv->dma_tx_size; i++) {
1756 if (priv->extend_desc)
1757 p = &((tx_q->dma_etx + i)->basic);
1758 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1759 p = &((tx_q->dma_entx + i)->basic);
1761 p = tx_q->dma_tx + i;
1763 stmmac_clear_desc(priv, p);
1765 tx_q->tx_skbuff_dma[i].buf = 0;
1766 tx_q->tx_skbuff_dma[i].map_as_page = false;
1767 tx_q->tx_skbuff_dma[i].len = 0;
1768 tx_q->tx_skbuff_dma[i].last_segment = false;
1769 tx_q->tx_skbuff[i] = NULL;
1776 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
1781 static int init_dma_tx_desc_rings(struct net_device *dev)
1783 struct stmmac_priv *priv = netdev_priv(dev);
1787 tx_queue_cnt = priv->plat->tx_queues_to_use;
1789 for (queue = 0; queue < tx_queue_cnt; queue++)
1790 __init_dma_tx_desc_rings(priv, queue);
1796 * init_dma_desc_rings - init the RX/TX descriptor rings
1797 * @dev: net device structure
1799 * Description: this function initializes the DMA RX/TX descriptors
1800 * and allocates the socket buffers. It supports the chained and ring
1803 static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
1805 struct stmmac_priv *priv = netdev_priv(dev);
1808 ret = init_dma_rx_desc_rings(dev, flags);
1812 ret = init_dma_tx_desc_rings(dev);
1814 stmmac_clear_descriptors(priv);
1816 if (netif_msg_hw(priv))
1817 stmmac_display_rings(priv);
1823 * dma_free_tx_skbufs - free TX dma buffers
1824 * @priv: private structure
1825 * @queue: TX queue index
1827 static void dma_free_tx_skbufs(struct stmmac_priv *priv, u32 queue)
1829 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1832 tx_q->xsk_frames_done = 0;
1834 for (i = 0; i < priv->dma_tx_size; i++)
1835 stmmac_free_tx_buffer(priv, queue, i);
1837 if (tx_q->xsk_pool && tx_q->xsk_frames_done) {
1838 xsk_tx_completed(tx_q->xsk_pool, tx_q->xsk_frames_done);
1839 tx_q->xsk_frames_done = 0;
1840 tx_q->xsk_pool = NULL;
1845 * stmmac_free_tx_skbufs - free TX skb buffers
1846 * @priv: private structure
1848 static void stmmac_free_tx_skbufs(struct stmmac_priv *priv)
1850 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1853 for (queue = 0; queue < tx_queue_cnt; queue++)
1854 dma_free_tx_skbufs(priv, queue);
1858 * __free_dma_rx_desc_resources - free RX dma desc resources (per queue)
1859 * @priv: private structure
1860 * @queue: RX queue index
1862 static void __free_dma_rx_desc_resources(struct stmmac_priv *priv, u32 queue)
1864 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1866 /* Release the DMA RX socket buffers */
1868 dma_free_rx_xskbufs(priv, queue);
1870 dma_free_rx_skbufs(priv, queue);
1872 rx_q->buf_alloc_num = 0;
1873 rx_q->xsk_pool = NULL;
1875 /* Free DMA regions of consistent memory previously allocated */
1876 if (!priv->extend_desc)
1877 dma_free_coherent(priv->device, priv->dma_rx_size *
1878 sizeof(struct dma_desc),
1879 rx_q->dma_rx, rx_q->dma_rx_phy);
1881 dma_free_coherent(priv->device, priv->dma_rx_size *
1882 sizeof(struct dma_extended_desc),
1883 rx_q->dma_erx, rx_q->dma_rx_phy);
1885 if (xdp_rxq_info_is_reg(&rx_q->xdp_rxq))
1886 xdp_rxq_info_unreg(&rx_q->xdp_rxq);
1888 kfree(rx_q->buf_pool);
1889 if (rx_q->page_pool)
1890 page_pool_destroy(rx_q->page_pool);
1893 static void free_dma_rx_desc_resources(struct stmmac_priv *priv)
1895 u32 rx_count = priv->plat->rx_queues_to_use;
1898 /* Free RX queue resources */
1899 for (queue = 0; queue < rx_count; queue++)
1900 __free_dma_rx_desc_resources(priv, queue);
1904 * __free_dma_tx_desc_resources - free TX dma desc resources (per queue)
1905 * @priv: private structure
1906 * @queue: TX queue index
1908 static void __free_dma_tx_desc_resources(struct stmmac_priv *priv, u32 queue)
1910 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1914 /* Release the DMA TX socket buffers */
1915 dma_free_tx_skbufs(priv, queue);
1917 if (priv->extend_desc) {
1918 size = sizeof(struct dma_extended_desc);
1919 addr = tx_q->dma_etx;
1920 } else if (tx_q->tbs & STMMAC_TBS_AVAIL) {
1921 size = sizeof(struct dma_edesc);
1922 addr = tx_q->dma_entx;
1924 size = sizeof(struct dma_desc);
1925 addr = tx_q->dma_tx;
1928 size *= priv->dma_tx_size;
1930 dma_free_coherent(priv->device, size, addr, tx_q->dma_tx_phy);
1932 kfree(tx_q->tx_skbuff_dma);
1933 kfree(tx_q->tx_skbuff);
1936 static void free_dma_tx_desc_resources(struct stmmac_priv *priv)
1938 u32 tx_count = priv->plat->tx_queues_to_use;
1941 /* Free TX queue resources */
1942 for (queue = 0; queue < tx_count; queue++)
1943 __free_dma_tx_desc_resources(priv, queue);
1947 * __alloc_dma_rx_desc_resources - alloc RX resources (per queue).
1948 * @priv: private structure
1949 * @queue: RX queue index
1950 * Description: according to which descriptor can be used (extend or basic)
1951 * this function allocates the resources for TX and RX paths. In case of
1952 * reception, for example, it pre-allocated the RX socket buffer in order to
1953 * allow zero-copy mechanism.
1955 static int __alloc_dma_rx_desc_resources(struct stmmac_priv *priv, u32 queue)
1957 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1958 struct stmmac_channel *ch = &priv->channel[queue];
1959 bool xdp_prog = stmmac_xdp_is_enabled(priv);
1960 struct page_pool_params pp_params = { 0 };
1961 unsigned int num_pages;
1962 unsigned int napi_id;
1965 rx_q->queue_index = queue;
1966 rx_q->priv_data = priv;
1968 pp_params.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
1969 pp_params.pool_size = priv->dma_rx_size;
1970 num_pages = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE);
1971 pp_params.order = ilog2(num_pages);
1972 pp_params.nid = dev_to_node(priv->device);
1973 pp_params.dev = priv->device;
1974 pp_params.dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
1975 pp_params.offset = stmmac_rx_offset(priv);
1976 pp_params.max_len = STMMAC_MAX_RX_BUF_SIZE(num_pages);
1978 rx_q->page_pool = page_pool_create(&pp_params);
1979 if (IS_ERR(rx_q->page_pool)) {
1980 ret = PTR_ERR(rx_q->page_pool);
1981 rx_q->page_pool = NULL;
1985 rx_q->buf_pool = kcalloc(priv->dma_rx_size,
1986 sizeof(*rx_q->buf_pool),
1988 if (!rx_q->buf_pool)
1991 if (priv->extend_desc) {
1992 rx_q->dma_erx = dma_alloc_coherent(priv->device,
1994 sizeof(struct dma_extended_desc),
2001 rx_q->dma_rx = dma_alloc_coherent(priv->device,
2003 sizeof(struct dma_desc),
2010 if (stmmac_xdp_is_enabled(priv) &&
2011 test_bit(queue, priv->af_xdp_zc_qps))
2012 napi_id = ch->rxtx_napi.napi_id;
2014 napi_id = ch->rx_napi.napi_id;
2016 ret = xdp_rxq_info_reg(&rx_q->xdp_rxq, priv->dev,
2020 netdev_err(priv->dev, "Failed to register xdp rxq info\n");
2027 static int alloc_dma_rx_desc_resources(struct stmmac_priv *priv)
2029 u32 rx_count = priv->plat->rx_queues_to_use;
2033 /* RX queues buffers and DMA */
2034 for (queue = 0; queue < rx_count; queue++) {
2035 ret = __alloc_dma_rx_desc_resources(priv, queue);
2043 free_dma_rx_desc_resources(priv);
2049 * __alloc_dma_tx_desc_resources - alloc TX resources (per queue).
2050 * @priv: private structure
2051 * @queue: TX queue index
2052 * Description: according to which descriptor can be used (extend or basic)
2053 * this function allocates the resources for TX and RX paths. In case of
2054 * reception, for example, it pre-allocated the RX socket buffer in order to
2055 * allow zero-copy mechanism.
2057 static int __alloc_dma_tx_desc_resources(struct stmmac_priv *priv, u32 queue)
2059 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2063 tx_q->queue_index = queue;
2064 tx_q->priv_data = priv;
2066 tx_q->tx_skbuff_dma = kcalloc(priv->dma_tx_size,
2067 sizeof(*tx_q->tx_skbuff_dma),
2069 if (!tx_q->tx_skbuff_dma)
2072 tx_q->tx_skbuff = kcalloc(priv->dma_tx_size,
2073 sizeof(struct sk_buff *),
2075 if (!tx_q->tx_skbuff)
2078 if (priv->extend_desc)
2079 size = sizeof(struct dma_extended_desc);
2080 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2081 size = sizeof(struct dma_edesc);
2083 size = sizeof(struct dma_desc);
2085 size *= priv->dma_tx_size;
2087 addr = dma_alloc_coherent(priv->device, size,
2088 &tx_q->dma_tx_phy, GFP_KERNEL);
2092 if (priv->extend_desc)
2093 tx_q->dma_etx = addr;
2094 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2095 tx_q->dma_entx = addr;
2097 tx_q->dma_tx = addr;
2102 static int alloc_dma_tx_desc_resources(struct stmmac_priv *priv)
2104 u32 tx_count = priv->plat->tx_queues_to_use;
2108 /* TX queues buffers and DMA */
2109 for (queue = 0; queue < tx_count; queue++) {
2110 ret = __alloc_dma_tx_desc_resources(priv, queue);
2118 free_dma_tx_desc_resources(priv);
2123 * alloc_dma_desc_resources - alloc TX/RX resources.
2124 * @priv: private structure
2125 * Description: according to which descriptor can be used (extend or basic)
2126 * this function allocates the resources for TX and RX paths. In case of
2127 * reception, for example, it pre-allocated the RX socket buffer in order to
2128 * allow zero-copy mechanism.
2130 static int alloc_dma_desc_resources(struct stmmac_priv *priv)
2133 int ret = alloc_dma_rx_desc_resources(priv);
2138 ret = alloc_dma_tx_desc_resources(priv);
2144 * free_dma_desc_resources - free dma desc resources
2145 * @priv: private structure
2147 static void free_dma_desc_resources(struct stmmac_priv *priv)
2149 /* Release the DMA TX socket buffers */
2150 free_dma_tx_desc_resources(priv);
2152 /* Release the DMA RX socket buffers later
2153 * to ensure all pending XDP_TX buffers are returned.
2155 free_dma_rx_desc_resources(priv);
2159 * stmmac_mac_enable_rx_queues - Enable MAC rx queues
2160 * @priv: driver private structure
2161 * Description: It is used for enabling the rx queues in the MAC
2163 static void stmmac_mac_enable_rx_queues(struct stmmac_priv *priv)
2165 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2169 for (queue = 0; queue < rx_queues_count; queue++) {
2170 mode = priv->plat->rx_queues_cfg[queue].mode_to_use;
2171 stmmac_rx_queue_enable(priv, priv->hw, mode, queue);
2176 * stmmac_start_rx_dma - start RX DMA channel
2177 * @priv: driver private structure
2178 * @chan: RX channel index
2180 * This starts a RX DMA channel
2182 static void stmmac_start_rx_dma(struct stmmac_priv *priv, u32 chan)
2184 netdev_dbg(priv->dev, "DMA RX processes started in channel %d\n", chan);
2185 stmmac_start_rx(priv, priv->ioaddr, chan);
2189 * stmmac_start_tx_dma - start TX DMA channel
2190 * @priv: driver private structure
2191 * @chan: TX channel index
2193 * This starts a TX DMA channel
2195 static void stmmac_start_tx_dma(struct stmmac_priv *priv, u32 chan)
2197 netdev_dbg(priv->dev, "DMA TX processes started in channel %d\n", chan);
2198 stmmac_start_tx(priv, priv->ioaddr, chan);
2202 * stmmac_stop_rx_dma - stop RX DMA channel
2203 * @priv: driver private structure
2204 * @chan: RX channel index
2206 * This stops a RX DMA channel
2208 static void stmmac_stop_rx_dma(struct stmmac_priv *priv, u32 chan)
2210 netdev_dbg(priv->dev, "DMA RX processes stopped in channel %d\n", chan);
2211 stmmac_stop_rx(priv, priv->ioaddr, chan);
2215 * stmmac_stop_tx_dma - stop TX DMA channel
2216 * @priv: driver private structure
2217 * @chan: TX channel index
2219 * This stops a TX DMA channel
2221 static void stmmac_stop_tx_dma(struct stmmac_priv *priv, u32 chan)
2223 netdev_dbg(priv->dev, "DMA TX processes stopped in channel %d\n", chan);
2224 stmmac_stop_tx(priv, priv->ioaddr, chan);
2228 * stmmac_start_all_dma - start all RX and TX DMA channels
2229 * @priv: driver private structure
2231 * This starts all the RX and TX DMA channels
2233 static void stmmac_start_all_dma(struct stmmac_priv *priv)
2235 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2236 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2239 for (chan = 0; chan < rx_channels_count; chan++)
2240 stmmac_start_rx_dma(priv, chan);
2242 for (chan = 0; chan < tx_channels_count; chan++)
2243 stmmac_start_tx_dma(priv, chan);
2247 * stmmac_stop_all_dma - stop all RX and TX DMA channels
2248 * @priv: driver private structure
2250 * This stops the RX and TX DMA channels
2252 static void stmmac_stop_all_dma(struct stmmac_priv *priv)
2254 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2255 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2258 for (chan = 0; chan < rx_channels_count; chan++)
2259 stmmac_stop_rx_dma(priv, chan);
2261 for (chan = 0; chan < tx_channels_count; chan++)
2262 stmmac_stop_tx_dma(priv, chan);
2266 * stmmac_dma_operation_mode - HW DMA operation mode
2267 * @priv: driver private structure
2268 * Description: it is used for configuring the DMA operation mode register in
2269 * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
2271 static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
2273 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2274 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2275 int rxfifosz = priv->plat->rx_fifo_size;
2276 int txfifosz = priv->plat->tx_fifo_size;
2283 rxfifosz = priv->dma_cap.rx_fifo_size;
2285 txfifosz = priv->dma_cap.tx_fifo_size;
2287 /* Adjust for real per queue fifo size */
2288 rxfifosz /= rx_channels_count;
2289 txfifosz /= tx_channels_count;
2291 if (priv->plat->force_thresh_dma_mode) {
2294 } else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
2296 * In case of GMAC, SF mode can be enabled
2297 * to perform the TX COE in HW. This depends on:
2298 * 1) TX COE if actually supported
2299 * 2) There is no bugged Jumbo frame support
2300 * that needs to not insert csum in the TDES.
2302 txmode = SF_DMA_MODE;
2303 rxmode = SF_DMA_MODE;
2304 priv->xstats.threshold = SF_DMA_MODE;
2307 rxmode = SF_DMA_MODE;
2310 /* configure all channels */
2311 for (chan = 0; chan < rx_channels_count; chan++) {
2312 struct stmmac_rx_queue *rx_q = &priv->rx_queue[chan];
2315 qmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
2317 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan,
2320 if (rx_q->xsk_pool) {
2321 buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool);
2322 stmmac_set_dma_bfsize(priv, priv->ioaddr,
2326 stmmac_set_dma_bfsize(priv, priv->ioaddr,
2332 for (chan = 0; chan < tx_channels_count; chan++) {
2333 qmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
2335 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan,
2340 static bool stmmac_xdp_xmit_zc(struct stmmac_priv *priv, u32 queue, u32 budget)
2342 struct netdev_queue *nq = netdev_get_tx_queue(priv->dev, queue);
2343 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2344 struct xsk_buff_pool *pool = tx_q->xsk_pool;
2345 unsigned int entry = tx_q->cur_tx;
2346 struct dma_desc *tx_desc = NULL;
2347 struct xdp_desc xdp_desc;
2348 bool work_done = true;
2350 /* Avoids TX time-out as we are sharing with slow path */
2351 nq->trans_start = jiffies;
2353 budget = min(budget, stmmac_tx_avail(priv, queue));
2355 while (budget-- > 0) {
2356 dma_addr_t dma_addr;
2359 /* We are sharing with slow path and stop XSK TX desc submission when
2360 * available TX ring is less than threshold.
2362 if (unlikely(stmmac_tx_avail(priv, queue) < STMMAC_TX_XSK_AVAIL) ||
2363 !netif_carrier_ok(priv->dev)) {
2368 if (!xsk_tx_peek_desc(pool, &xdp_desc))
2371 if (likely(priv->extend_desc))
2372 tx_desc = (struct dma_desc *)(tx_q->dma_etx + entry);
2373 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2374 tx_desc = &tx_q->dma_entx[entry].basic;
2376 tx_desc = tx_q->dma_tx + entry;
2378 dma_addr = xsk_buff_raw_get_dma(pool, xdp_desc.addr);
2379 xsk_buff_raw_dma_sync_for_device(pool, dma_addr, xdp_desc.len);
2381 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XSK_TX;
2383 /* To return XDP buffer to XSK pool, we simple call
2384 * xsk_tx_completed(), so we don't need to fill up
2387 tx_q->tx_skbuff_dma[entry].buf = 0;
2388 tx_q->xdpf[entry] = NULL;
2390 tx_q->tx_skbuff_dma[entry].map_as_page = false;
2391 tx_q->tx_skbuff_dma[entry].len = xdp_desc.len;
2392 tx_q->tx_skbuff_dma[entry].last_segment = true;
2393 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
2395 stmmac_set_desc_addr(priv, tx_desc, dma_addr);
2397 tx_q->tx_count_frames++;
2399 if (!priv->tx_coal_frames[queue])
2401 else if (tx_q->tx_count_frames % priv->tx_coal_frames[queue] == 0)
2407 tx_q->tx_count_frames = 0;
2408 stmmac_set_tx_ic(priv, tx_desc);
2409 priv->xstats.tx_set_ic_bit++;
2412 stmmac_prepare_tx_desc(priv, tx_desc, 1, xdp_desc.len,
2413 true, priv->mode, true, true,
2416 stmmac_enable_dma_transmission(priv, priv->ioaddr);
2418 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
2419 entry = tx_q->cur_tx;
2423 stmmac_flush_tx_descriptors(priv, queue);
2424 xsk_tx_release(pool);
2427 /* Return true if all of the 3 conditions are met
2428 * a) TX Budget is still available
2429 * b) work_done = true when XSK TX desc peek is empty (no more
2430 * pending XSK TX for transmission)
2432 return !!budget && work_done;
2436 * stmmac_tx_clean - to manage the transmission completion
2437 * @priv: driver private structure
2438 * @budget: napi budget limiting this functions packet handling
2439 * @queue: TX queue index
2440 * Description: it reclaims the transmit resources after transmission completes.
2442 static int stmmac_tx_clean(struct stmmac_priv *priv, int budget, u32 queue)
2444 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2445 unsigned int bytes_compl = 0, pkts_compl = 0;
2446 unsigned int entry, xmits = 0, count = 0;
2448 __netif_tx_lock_bh(netdev_get_tx_queue(priv->dev, queue));
2450 priv->xstats.tx_clean++;
2452 tx_q->xsk_frames_done = 0;
2454 entry = tx_q->dirty_tx;
2456 /* Try to clean all TX complete frame in 1 shot */
2457 while ((entry != tx_q->cur_tx) && count < priv->dma_tx_size) {
2458 struct xdp_frame *xdpf;
2459 struct sk_buff *skb;
2463 if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_TX ||
2464 tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_NDO) {
2465 xdpf = tx_q->xdpf[entry];
2467 } else if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_SKB) {
2469 skb = tx_q->tx_skbuff[entry];
2475 if (priv->extend_desc)
2476 p = (struct dma_desc *)(tx_q->dma_etx + entry);
2477 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2478 p = &tx_q->dma_entx[entry].basic;
2480 p = tx_q->dma_tx + entry;
2482 status = stmmac_tx_status(priv, &priv->dev->stats,
2483 &priv->xstats, p, priv->ioaddr);
2484 /* Check if the descriptor is owned by the DMA */
2485 if (unlikely(status & tx_dma_own))
2490 /* Make sure descriptor fields are read after reading
2495 /* Just consider the last segment and ...*/
2496 if (likely(!(status & tx_not_ls))) {
2497 /* ... verify the status error condition */
2498 if (unlikely(status & tx_err)) {
2499 priv->dev->stats.tx_errors++;
2501 priv->dev->stats.tx_packets++;
2502 priv->xstats.tx_pkt_n++;
2505 stmmac_get_tx_hwtstamp(priv, p, skb);
2508 if (likely(tx_q->tx_skbuff_dma[entry].buf &&
2509 tx_q->tx_skbuff_dma[entry].buf_type != STMMAC_TXBUF_T_XDP_TX)) {
2510 if (tx_q->tx_skbuff_dma[entry].map_as_page)
2511 dma_unmap_page(priv->device,
2512 tx_q->tx_skbuff_dma[entry].buf,
2513 tx_q->tx_skbuff_dma[entry].len,
2516 dma_unmap_single(priv->device,
2517 tx_q->tx_skbuff_dma[entry].buf,
2518 tx_q->tx_skbuff_dma[entry].len,
2520 tx_q->tx_skbuff_dma[entry].buf = 0;
2521 tx_q->tx_skbuff_dma[entry].len = 0;
2522 tx_q->tx_skbuff_dma[entry].map_as_page = false;
2525 stmmac_clean_desc3(priv, tx_q, p);
2527 tx_q->tx_skbuff_dma[entry].last_segment = false;
2528 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
2531 tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_TX) {
2532 xdp_return_frame_rx_napi(xdpf);
2533 tx_q->xdpf[entry] = NULL;
2537 tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_NDO) {
2538 xdp_return_frame(xdpf);
2539 tx_q->xdpf[entry] = NULL;
2542 if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XSK_TX)
2543 tx_q->xsk_frames_done++;
2545 if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_SKB) {
2548 bytes_compl += skb->len;
2549 dev_consume_skb_any(skb);
2550 tx_q->tx_skbuff[entry] = NULL;
2554 stmmac_release_tx_desc(priv, p, priv->mode);
2556 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
2558 tx_q->dirty_tx = entry;
2560 netdev_tx_completed_queue(netdev_get_tx_queue(priv->dev, queue),
2561 pkts_compl, bytes_compl);
2563 if (unlikely(netif_tx_queue_stopped(netdev_get_tx_queue(priv->dev,
2565 stmmac_tx_avail(priv, queue) > STMMAC_TX_THRESH(priv)) {
2567 netif_dbg(priv, tx_done, priv->dev,
2568 "%s: restart transmit\n", __func__);
2569 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, queue));
2572 if (tx_q->xsk_pool) {
2575 if (tx_q->xsk_frames_done)
2576 xsk_tx_completed(tx_q->xsk_pool, tx_q->xsk_frames_done);
2578 if (xsk_uses_need_wakeup(tx_q->xsk_pool))
2579 xsk_set_tx_need_wakeup(tx_q->xsk_pool);
2581 /* For XSK TX, we try to send as many as possible.
2582 * If XSK work done (XSK TX desc empty and budget still
2583 * available), return "budget - 1" to reenable TX IRQ.
2584 * Else, return "budget" to make NAPI continue polling.
2586 work_done = stmmac_xdp_xmit_zc(priv, queue,
2587 STMMAC_XSK_TX_BUDGET_MAX);
2594 if (priv->eee_enabled && !priv->tx_path_in_lpi_mode &&
2595 priv->eee_sw_timer_en) {
2596 stmmac_enable_eee_mode(priv);
2597 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer));
2600 /* We still have pending packets, let's call for a new scheduling */
2601 if (tx_q->dirty_tx != tx_q->cur_tx)
2602 hrtimer_start(&tx_q->txtimer,
2603 STMMAC_COAL_TIMER(priv->tx_coal_timer[queue]),
2606 __netif_tx_unlock_bh(netdev_get_tx_queue(priv->dev, queue));
2608 /* Combine decisions from TX clean and XSK TX */
2609 return max(count, xmits);
2613 * stmmac_tx_err - to manage the tx error
2614 * @priv: driver private structure
2615 * @chan: channel index
2616 * Description: it cleans the descriptors and restarts the transmission
2617 * in case of transmission errors.
2619 static void stmmac_tx_err(struct stmmac_priv *priv, u32 chan)
2621 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2623 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, chan));
2625 stmmac_stop_tx_dma(priv, chan);
2626 dma_free_tx_skbufs(priv, chan);
2627 stmmac_clear_tx_descriptors(priv, chan);
2631 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, chan));
2632 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2633 tx_q->dma_tx_phy, chan);
2634 stmmac_start_tx_dma(priv, chan);
2636 priv->dev->stats.tx_errors++;
2637 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, chan));
2641 * stmmac_set_dma_operation_mode - Set DMA operation mode by channel
2642 * @priv: driver private structure
2643 * @txmode: TX operating mode
2644 * @rxmode: RX operating mode
2645 * @chan: channel index
2646 * Description: it is used for configuring of the DMA operation mode in
2647 * runtime in order to program the tx/rx DMA thresholds or Store-And-Forward
2650 static void stmmac_set_dma_operation_mode(struct stmmac_priv *priv, u32 txmode,
2651 u32 rxmode, u32 chan)
2653 u8 rxqmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
2654 u8 txqmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
2655 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2656 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2657 int rxfifosz = priv->plat->rx_fifo_size;
2658 int txfifosz = priv->plat->tx_fifo_size;
2661 rxfifosz = priv->dma_cap.rx_fifo_size;
2663 txfifosz = priv->dma_cap.tx_fifo_size;
2665 /* Adjust for real per queue fifo size */
2666 rxfifosz /= rx_channels_count;
2667 txfifosz /= tx_channels_count;
2669 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan, rxfifosz, rxqmode);
2670 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan, txfifosz, txqmode);
2673 static bool stmmac_safety_feat_interrupt(struct stmmac_priv *priv)
2677 ret = stmmac_safety_feat_irq_status(priv, priv->dev,
2678 priv->ioaddr, priv->dma_cap.asp, &priv->sstats);
2679 if (ret && (ret != -EINVAL)) {
2680 stmmac_global_err(priv);
2687 static int stmmac_napi_check(struct stmmac_priv *priv, u32 chan, u32 dir)
2689 int status = stmmac_dma_interrupt_status(priv, priv->ioaddr,
2690 &priv->xstats, chan, dir);
2691 struct stmmac_rx_queue *rx_q = &priv->rx_queue[chan];
2692 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2693 struct stmmac_channel *ch = &priv->channel[chan];
2694 struct napi_struct *rx_napi;
2695 struct napi_struct *tx_napi;
2696 unsigned long flags;
2698 rx_napi = rx_q->xsk_pool ? &ch->rxtx_napi : &ch->rx_napi;
2699 tx_napi = tx_q->xsk_pool ? &ch->rxtx_napi : &ch->tx_napi;
2701 if ((status & handle_rx) && (chan < priv->plat->rx_queues_to_use)) {
2702 if (napi_schedule_prep(rx_napi)) {
2703 spin_lock_irqsave(&ch->lock, flags);
2704 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
2705 spin_unlock_irqrestore(&ch->lock, flags);
2706 __napi_schedule(rx_napi);
2710 if ((status & handle_tx) && (chan < priv->plat->tx_queues_to_use)) {
2711 if (napi_schedule_prep(tx_napi)) {
2712 spin_lock_irqsave(&ch->lock, flags);
2713 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
2714 spin_unlock_irqrestore(&ch->lock, flags);
2715 __napi_schedule(tx_napi);
2723 * stmmac_dma_interrupt - DMA ISR
2724 * @priv: driver private structure
2725 * Description: this is the DMA ISR. It is called by the main ISR.
2726 * It calls the dwmac dma routine and schedule poll method in case of some
2729 static void stmmac_dma_interrupt(struct stmmac_priv *priv)
2731 u32 tx_channel_count = priv->plat->tx_queues_to_use;
2732 u32 rx_channel_count = priv->plat->rx_queues_to_use;
2733 u32 channels_to_check = tx_channel_count > rx_channel_count ?
2734 tx_channel_count : rx_channel_count;
2736 int status[max_t(u32, MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES)];
2738 /* Make sure we never check beyond our status buffer. */
2739 if (WARN_ON_ONCE(channels_to_check > ARRAY_SIZE(status)))
2740 channels_to_check = ARRAY_SIZE(status);
2742 for (chan = 0; chan < channels_to_check; chan++)
2743 status[chan] = stmmac_napi_check(priv, chan,
2746 for (chan = 0; chan < tx_channel_count; chan++) {
2747 if (unlikely(status[chan] & tx_hard_error_bump_tc)) {
2748 /* Try to bump up the dma threshold on this failure */
2749 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
2752 if (priv->plat->force_thresh_dma_mode)
2753 stmmac_set_dma_operation_mode(priv,
2758 stmmac_set_dma_operation_mode(priv,
2762 priv->xstats.threshold = tc;
2764 } else if (unlikely(status[chan] == tx_hard_error)) {
2765 stmmac_tx_err(priv, chan);
2771 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
2772 * @priv: driver private structure
2773 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
2775 static void stmmac_mmc_setup(struct stmmac_priv *priv)
2777 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
2778 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
2780 stmmac_mmc_intr_all_mask(priv, priv->mmcaddr);
2782 if (priv->dma_cap.rmon) {
2783 stmmac_mmc_ctrl(priv, priv->mmcaddr, mode);
2784 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
2786 netdev_info(priv->dev, "No MAC Management Counters available\n");
2790 * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
2791 * @priv: driver private structure
2793 * new GMAC chip generations have a new register to indicate the
2794 * presence of the optional feature/functions.
2795 * This can be also used to override the value passed through the
2796 * platform and necessary for old MAC10/100 and GMAC chips.
2798 static int stmmac_get_hw_features(struct stmmac_priv *priv)
2800 return stmmac_get_hw_feature(priv, priv->ioaddr, &priv->dma_cap) == 0;
2804 * stmmac_check_ether_addr - check if the MAC addr is valid
2805 * @priv: driver private structure
2807 * it is to verify if the MAC address is valid, in case of failures it
2808 * generates a random MAC address
2810 static void stmmac_check_ether_addr(struct stmmac_priv *priv)
2812 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
2813 stmmac_get_umac_addr(priv, priv->hw, priv->dev->dev_addr, 0);
2814 if (!is_valid_ether_addr(priv->dev->dev_addr))
2815 eth_hw_addr_random(priv->dev);
2816 dev_info(priv->device, "device MAC address %pM\n",
2817 priv->dev->dev_addr);
2822 * stmmac_init_dma_engine - DMA init.
2823 * @priv: driver private structure
2825 * It inits the DMA invoking the specific MAC/GMAC callback.
2826 * Some DMA parameters can be passed from the platform;
2827 * in case of these are not passed a default is kept for the MAC or GMAC.
2829 static int stmmac_init_dma_engine(struct stmmac_priv *priv)
2831 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2832 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2833 u32 dma_csr_ch = max(rx_channels_count, tx_channels_count);
2834 struct stmmac_rx_queue *rx_q;
2835 struct stmmac_tx_queue *tx_q;
2840 if (!priv->plat->dma_cfg || !priv->plat->dma_cfg->pbl) {
2841 dev_err(priv->device, "Invalid DMA configuration\n");
2845 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
2848 ret = stmmac_reset(priv, priv->ioaddr);
2850 dev_err(priv->device, "Failed to reset the dma\n");
2854 /* DMA Configuration */
2855 stmmac_dma_init(priv, priv->ioaddr, priv->plat->dma_cfg, atds);
2857 if (priv->plat->axi)
2858 stmmac_axi(priv, priv->ioaddr, priv->plat->axi);
2860 /* DMA CSR Channel configuration */
2861 for (chan = 0; chan < dma_csr_ch; chan++)
2862 stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan);
2864 /* DMA RX Channel Configuration */
2865 for (chan = 0; chan < rx_channels_count; chan++) {
2866 rx_q = &priv->rx_queue[chan];
2868 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2869 rx_q->dma_rx_phy, chan);
2871 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
2872 (rx_q->buf_alloc_num *
2873 sizeof(struct dma_desc));
2874 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
2875 rx_q->rx_tail_addr, chan);
2878 /* DMA TX Channel Configuration */
2879 for (chan = 0; chan < tx_channels_count; chan++) {
2880 tx_q = &priv->tx_queue[chan];
2882 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2883 tx_q->dma_tx_phy, chan);
2885 tx_q->tx_tail_addr = tx_q->dma_tx_phy;
2886 stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
2887 tx_q->tx_tail_addr, chan);
2893 static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue)
2895 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2897 hrtimer_start(&tx_q->txtimer,
2898 STMMAC_COAL_TIMER(priv->tx_coal_timer[queue]),
2903 * stmmac_tx_timer - mitigation sw timer for tx.
2906 * This is the timer handler to directly invoke the stmmac_tx_clean.
2908 static enum hrtimer_restart stmmac_tx_timer(struct hrtimer *t)
2910 struct stmmac_tx_queue *tx_q = container_of(t, struct stmmac_tx_queue, txtimer);
2911 struct stmmac_priv *priv = tx_q->priv_data;
2912 struct stmmac_channel *ch;
2913 struct napi_struct *napi;
2915 ch = &priv->channel[tx_q->queue_index];
2916 napi = tx_q->xsk_pool ? &ch->rxtx_napi : &ch->tx_napi;
2918 if (likely(napi_schedule_prep(napi))) {
2919 unsigned long flags;
2921 spin_lock_irqsave(&ch->lock, flags);
2922 stmmac_disable_dma_irq(priv, priv->ioaddr, ch->index, 0, 1);
2923 spin_unlock_irqrestore(&ch->lock, flags);
2924 __napi_schedule(napi);
2927 return HRTIMER_NORESTART;
2931 * stmmac_init_coalesce - init mitigation options.
2932 * @priv: driver private structure
2934 * This inits the coalesce parameters: i.e. timer rate,
2935 * timer handler and default threshold used for enabling the
2936 * interrupt on completion bit.
2938 static void stmmac_init_coalesce(struct stmmac_priv *priv)
2940 u32 tx_channel_count = priv->plat->tx_queues_to_use;
2941 u32 rx_channel_count = priv->plat->rx_queues_to_use;
2944 for (chan = 0; chan < tx_channel_count; chan++) {
2945 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2947 priv->tx_coal_frames[chan] = STMMAC_TX_FRAMES;
2948 priv->tx_coal_timer[chan] = STMMAC_COAL_TX_TIMER;
2950 hrtimer_init(&tx_q->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2951 tx_q->txtimer.function = stmmac_tx_timer;
2954 for (chan = 0; chan < rx_channel_count; chan++)
2955 priv->rx_coal_frames[chan] = STMMAC_RX_FRAMES;
2958 static void stmmac_set_rings_length(struct stmmac_priv *priv)
2960 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2961 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2964 /* set TX ring length */
2965 for (chan = 0; chan < tx_channels_count; chan++)
2966 stmmac_set_tx_ring_len(priv, priv->ioaddr,
2967 (priv->dma_tx_size - 1), chan);
2969 /* set RX ring length */
2970 for (chan = 0; chan < rx_channels_count; chan++)
2971 stmmac_set_rx_ring_len(priv, priv->ioaddr,
2972 (priv->dma_rx_size - 1), chan);
2976 * stmmac_set_tx_queue_weight - Set TX queue weight
2977 * @priv: driver private structure
2978 * Description: It is used for setting TX queues weight
2980 static void stmmac_set_tx_queue_weight(struct stmmac_priv *priv)
2982 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2986 for (queue = 0; queue < tx_queues_count; queue++) {
2987 weight = priv->plat->tx_queues_cfg[queue].weight;
2988 stmmac_set_mtl_tx_queue_weight(priv, priv->hw, weight, queue);
2993 * stmmac_configure_cbs - Configure CBS in TX queue
2994 * @priv: driver private structure
2995 * Description: It is used for configuring CBS in AVB TX queues
2997 static void stmmac_configure_cbs(struct stmmac_priv *priv)
2999 u32 tx_queues_count = priv->plat->tx_queues_to_use;
3003 /* queue 0 is reserved for legacy traffic */
3004 for (queue = 1; queue < tx_queues_count; queue++) {
3005 mode_to_use = priv->plat->tx_queues_cfg[queue].mode_to_use;
3006 if (mode_to_use == MTL_QUEUE_DCB)
3009 stmmac_config_cbs(priv, priv->hw,
3010 priv->plat->tx_queues_cfg[queue].send_slope,
3011 priv->plat->tx_queues_cfg[queue].idle_slope,
3012 priv->plat->tx_queues_cfg[queue].high_credit,
3013 priv->plat->tx_queues_cfg[queue].low_credit,
3019 * stmmac_rx_queue_dma_chan_map - Map RX queue to RX dma channel
3020 * @priv: driver private structure
3021 * Description: It is used for mapping RX queues to RX dma channels
3023 static void stmmac_rx_queue_dma_chan_map(struct stmmac_priv *priv)
3025 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3029 for (queue = 0; queue < rx_queues_count; queue++) {
3030 chan = priv->plat->rx_queues_cfg[queue].chan;
3031 stmmac_map_mtl_to_dma(priv, priv->hw, queue, chan);
3036 * stmmac_mac_config_rx_queues_prio - Configure RX Queue priority
3037 * @priv: driver private structure
3038 * Description: It is used for configuring the RX Queue Priority
3040 static void stmmac_mac_config_rx_queues_prio(struct stmmac_priv *priv)
3042 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3046 for (queue = 0; queue < rx_queues_count; queue++) {
3047 if (!priv->plat->rx_queues_cfg[queue].use_prio)
3050 prio = priv->plat->rx_queues_cfg[queue].prio;
3051 stmmac_rx_queue_prio(priv, priv->hw, prio, queue);
3056 * stmmac_mac_config_tx_queues_prio - Configure TX Queue priority
3057 * @priv: driver private structure
3058 * Description: It is used for configuring the TX Queue Priority
3060 static void stmmac_mac_config_tx_queues_prio(struct stmmac_priv *priv)
3062 u32 tx_queues_count = priv->plat->tx_queues_to_use;
3066 for (queue = 0; queue < tx_queues_count; queue++) {
3067 if (!priv->plat->tx_queues_cfg[queue].use_prio)
3070 prio = priv->plat->tx_queues_cfg[queue].prio;
3071 stmmac_tx_queue_prio(priv, priv->hw, prio, queue);
3076 * stmmac_mac_config_rx_queues_routing - Configure RX Queue Routing
3077 * @priv: driver private structure
3078 * Description: It is used for configuring the RX queue routing
3080 static void stmmac_mac_config_rx_queues_routing(struct stmmac_priv *priv)
3082 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3086 for (queue = 0; queue < rx_queues_count; queue++) {
3087 /* no specific packet type routing specified for the queue */
3088 if (priv->plat->rx_queues_cfg[queue].pkt_route == 0x0)
3091 packet = priv->plat->rx_queues_cfg[queue].pkt_route;
3092 stmmac_rx_queue_routing(priv, priv->hw, packet, queue);
3096 static void stmmac_mac_config_rss(struct stmmac_priv *priv)
3098 if (!priv->dma_cap.rssen || !priv->plat->rss_en) {
3099 priv->rss.enable = false;
3103 if (priv->dev->features & NETIF_F_RXHASH)
3104 priv->rss.enable = true;
3106 priv->rss.enable = false;
3108 stmmac_rss_configure(priv, priv->hw, &priv->rss,
3109 priv->plat->rx_queues_to_use);
3113 * stmmac_mtl_configuration - Configure MTL
3114 * @priv: driver private structure
3115 * Description: It is used for configurring MTL
3117 static void stmmac_mtl_configuration(struct stmmac_priv *priv)
3119 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3120 u32 tx_queues_count = priv->plat->tx_queues_to_use;
3122 if (tx_queues_count > 1)
3123 stmmac_set_tx_queue_weight(priv);
3125 /* Configure MTL RX algorithms */
3126 if (rx_queues_count > 1)
3127 stmmac_prog_mtl_rx_algorithms(priv, priv->hw,
3128 priv->plat->rx_sched_algorithm);
3130 /* Configure MTL TX algorithms */
3131 if (tx_queues_count > 1)
3132 stmmac_prog_mtl_tx_algorithms(priv, priv->hw,
3133 priv->plat->tx_sched_algorithm);
3135 /* Configure CBS in AVB TX queues */
3136 if (tx_queues_count > 1)
3137 stmmac_configure_cbs(priv);
3139 /* Map RX MTL to DMA channels */
3140 stmmac_rx_queue_dma_chan_map(priv);
3142 /* Enable MAC RX Queues */
3143 stmmac_mac_enable_rx_queues(priv);
3145 /* Set RX priorities */
3146 if (rx_queues_count > 1)
3147 stmmac_mac_config_rx_queues_prio(priv);
3149 /* Set TX priorities */
3150 if (tx_queues_count > 1)
3151 stmmac_mac_config_tx_queues_prio(priv);
3153 /* Set RX routing */
3154 if (rx_queues_count > 1)
3155 stmmac_mac_config_rx_queues_routing(priv);
3157 /* Receive Side Scaling */
3158 if (rx_queues_count > 1)
3159 stmmac_mac_config_rss(priv);
3162 static void stmmac_safety_feat_configuration(struct stmmac_priv *priv)
3164 if (priv->dma_cap.asp) {
3165 netdev_info(priv->dev, "Enabling Safety Features\n");
3166 stmmac_safety_feat_config(priv, priv->ioaddr, priv->dma_cap.asp,
3167 priv->plat->safety_feat_cfg);
3169 netdev_info(priv->dev, "No Safety Features support found\n");
3173 static int stmmac_fpe_start_wq(struct stmmac_priv *priv)
3177 clear_bit(__FPE_TASK_SCHED, &priv->fpe_task_state);
3178 clear_bit(__FPE_REMOVING, &priv->fpe_task_state);
3180 name = priv->wq_name;
3181 sprintf(name, "%s-fpe", priv->dev->name);
3183 priv->fpe_wq = create_singlethread_workqueue(name);
3184 if (!priv->fpe_wq) {
3185 netdev_err(priv->dev, "%s: Failed to create workqueue\n", name);
3189 netdev_info(priv->dev, "FPE workqueue start");
3195 * stmmac_hw_setup - setup mac in a usable state.
3196 * @dev : pointer to the device structure.
3197 * @init_ptp: initialize PTP if set
3199 * this is the main function to setup the HW in a usable state because the
3200 * dma engine is reset, the core registers are configured (e.g. AXI,
3201 * Checksum features, timers). The DMA is ready to start receiving and
3204 * 0 on success and an appropriate (-)ve integer as defined in errno.h
3207 static int stmmac_hw_setup(struct net_device *dev, bool init_ptp)
3209 struct stmmac_priv *priv = netdev_priv(dev);
3210 u32 rx_cnt = priv->plat->rx_queues_to_use;
3211 u32 tx_cnt = priv->plat->tx_queues_to_use;
3216 /* DMA initialization and SW reset */
3217 ret = stmmac_init_dma_engine(priv);
3219 netdev_err(priv->dev, "%s: DMA engine initialization failed\n",
3224 /* Copy the MAC addr into the HW */
3225 stmmac_set_umac_addr(priv, priv->hw, dev->dev_addr, 0);
3227 /* PS and related bits will be programmed according to the speed */
3228 if (priv->hw->pcs) {
3229 int speed = priv->plat->mac_port_sel_speed;
3231 if ((speed == SPEED_10) || (speed == SPEED_100) ||
3232 (speed == SPEED_1000)) {
3233 priv->hw->ps = speed;
3235 dev_warn(priv->device, "invalid port speed\n");
3240 /* Initialize the MAC Core */
3241 stmmac_core_init(priv, priv->hw, dev);
3244 stmmac_mtl_configuration(priv);
3246 /* Initialize Safety Features */
3247 stmmac_safety_feat_configuration(priv);
3249 ret = stmmac_rx_ipc(priv, priv->hw);
3251 netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n");
3252 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
3253 priv->hw->rx_csum = 0;
3256 /* Enable the MAC Rx/Tx */
3257 stmmac_mac_set(priv, priv->ioaddr, true);
3259 /* Set the HW DMA mode and the COE */
3260 stmmac_dma_operation_mode(priv);
3262 stmmac_mmc_setup(priv);
3265 ret = clk_prepare_enable(priv->plat->clk_ptp_ref);
3267 netdev_warn(priv->dev, "failed to enable PTP reference clock: %d\n", ret);
3269 ret = stmmac_init_ptp(priv);
3270 if (ret == -EOPNOTSUPP)
3271 netdev_warn(priv->dev, "PTP not supported by HW\n");
3273 netdev_warn(priv->dev, "PTP init failed\n");
3276 priv->eee_tw_timer = STMMAC_DEFAULT_TWT_LS;
3278 /* Convert the timer from msec to usec */
3279 if (!priv->tx_lpi_timer)
3280 priv->tx_lpi_timer = eee_timer * 1000;
3282 if (priv->use_riwt) {
3285 for (queue = 0; queue < rx_cnt; queue++) {
3286 if (!priv->rx_riwt[queue])
3287 priv->rx_riwt[queue] = DEF_DMA_RIWT;
3289 stmmac_rx_watchdog(priv, priv->ioaddr,
3290 priv->rx_riwt[queue], queue);
3295 stmmac_pcs_ctrl_ane(priv, priv->ioaddr, 1, priv->hw->ps, 0);
3297 /* set TX and RX rings length */
3298 stmmac_set_rings_length(priv);
3302 for (chan = 0; chan < tx_cnt; chan++) {
3303 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
3305 /* TSO and TBS cannot co-exist */
3306 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3309 stmmac_enable_tso(priv, priv->ioaddr, 1, chan);
3313 /* Enable Split Header */
3314 sph_en = (priv->hw->rx_csum > 0) && priv->sph;
3315 for (chan = 0; chan < rx_cnt; chan++)
3316 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
3319 /* VLAN Tag Insertion */
3320 if (priv->dma_cap.vlins)
3321 stmmac_enable_vlan(priv, priv->hw, STMMAC_VLAN_INSERT);
3324 for (chan = 0; chan < tx_cnt; chan++) {
3325 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
3326 int enable = tx_q->tbs & STMMAC_TBS_AVAIL;
3328 stmmac_enable_tbs(priv, priv->ioaddr, enable, chan);
3331 /* Configure real RX and TX queues */
3332 netif_set_real_num_rx_queues(dev, priv->plat->rx_queues_to_use);
3333 netif_set_real_num_tx_queues(dev, priv->plat->tx_queues_to_use);
3335 /* Start the ball rolling... */
3336 stmmac_start_all_dma(priv);
3338 if (priv->dma_cap.fpesel) {
3339 stmmac_fpe_start_wq(priv);
3341 if (priv->plat->fpe_cfg->enable)
3342 stmmac_fpe_handshake(priv, true);
3348 static void stmmac_hw_teardown(struct net_device *dev)
3350 struct stmmac_priv *priv = netdev_priv(dev);
3352 clk_disable_unprepare(priv->plat->clk_ptp_ref);
3355 static void stmmac_free_irq(struct net_device *dev,
3356 enum request_irq_err irq_err, int irq_idx)
3358 struct stmmac_priv *priv = netdev_priv(dev);
3362 case REQ_IRQ_ERR_ALL:
3363 irq_idx = priv->plat->tx_queues_to_use;
3365 case REQ_IRQ_ERR_TX:
3366 for (j = irq_idx - 1; j >= 0; j--) {
3367 if (priv->tx_irq[j] > 0) {
3368 irq_set_affinity_hint(priv->tx_irq[j], NULL);
3369 free_irq(priv->tx_irq[j], &priv->tx_queue[j]);
3372 irq_idx = priv->plat->rx_queues_to_use;
3374 case REQ_IRQ_ERR_RX:
3375 for (j = irq_idx - 1; j >= 0; j--) {
3376 if (priv->rx_irq[j] > 0) {
3377 irq_set_affinity_hint(priv->rx_irq[j], NULL);
3378 free_irq(priv->rx_irq[j], &priv->rx_queue[j]);
3382 if (priv->sfty_ue_irq > 0 && priv->sfty_ue_irq != dev->irq)
3383 free_irq(priv->sfty_ue_irq, dev);
3385 case REQ_IRQ_ERR_SFTY_UE:
3386 if (priv->sfty_ce_irq > 0 && priv->sfty_ce_irq != dev->irq)
3387 free_irq(priv->sfty_ce_irq, dev);
3389 case REQ_IRQ_ERR_SFTY_CE:
3390 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq)
3391 free_irq(priv->lpi_irq, dev);
3393 case REQ_IRQ_ERR_LPI:
3394 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq)
3395 free_irq(priv->wol_irq, dev);
3397 case REQ_IRQ_ERR_WOL:
3398 free_irq(dev->irq, dev);
3400 case REQ_IRQ_ERR_MAC:
3401 case REQ_IRQ_ERR_NO:
3402 /* If MAC IRQ request error, no more IRQ to free */
3407 static int stmmac_request_irq_multi_msi(struct net_device *dev)
3409 struct stmmac_priv *priv = netdev_priv(dev);
3410 enum request_irq_err irq_err;
3417 /* For common interrupt */
3418 int_name = priv->int_name_mac;
3419 sprintf(int_name, "%s:%s", dev->name, "mac");
3420 ret = request_irq(dev->irq, stmmac_mac_interrupt,
3422 if (unlikely(ret < 0)) {
3423 netdev_err(priv->dev,
3424 "%s: alloc mac MSI %d (error: %d)\n",
3425 __func__, dev->irq, ret);
3426 irq_err = REQ_IRQ_ERR_MAC;
3430 /* Request the Wake IRQ in case of another line
3433 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq) {
3434 int_name = priv->int_name_wol;
3435 sprintf(int_name, "%s:%s", dev->name, "wol");
3436 ret = request_irq(priv->wol_irq,
3437 stmmac_mac_interrupt,
3439 if (unlikely(ret < 0)) {
3440 netdev_err(priv->dev,
3441 "%s: alloc wol MSI %d (error: %d)\n",
3442 __func__, priv->wol_irq, ret);
3443 irq_err = REQ_IRQ_ERR_WOL;
3448 /* Request the LPI IRQ in case of another line
3451 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq) {
3452 int_name = priv->int_name_lpi;
3453 sprintf(int_name, "%s:%s", dev->name, "lpi");
3454 ret = request_irq(priv->lpi_irq,
3455 stmmac_mac_interrupt,
3457 if (unlikely(ret < 0)) {
3458 netdev_err(priv->dev,
3459 "%s: alloc lpi MSI %d (error: %d)\n",
3460 __func__, priv->lpi_irq, ret);
3461 irq_err = REQ_IRQ_ERR_LPI;
3466 /* Request the Safety Feature Correctible Error line in
3467 * case of another line is used
3469 if (priv->sfty_ce_irq > 0 && priv->sfty_ce_irq != dev->irq) {
3470 int_name = priv->int_name_sfty_ce;
3471 sprintf(int_name, "%s:%s", dev->name, "safety-ce");
3472 ret = request_irq(priv->sfty_ce_irq,
3473 stmmac_safety_interrupt,
3475 if (unlikely(ret < 0)) {
3476 netdev_err(priv->dev,
3477 "%s: alloc sfty ce MSI %d (error: %d)\n",
3478 __func__, priv->sfty_ce_irq, ret);
3479 irq_err = REQ_IRQ_ERR_SFTY_CE;
3484 /* Request the Safety Feature Uncorrectible Error line in
3485 * case of another line is used
3487 if (priv->sfty_ue_irq > 0 && priv->sfty_ue_irq != dev->irq) {
3488 int_name = priv->int_name_sfty_ue;
3489 sprintf(int_name, "%s:%s", dev->name, "safety-ue");
3490 ret = request_irq(priv->sfty_ue_irq,
3491 stmmac_safety_interrupt,
3493 if (unlikely(ret < 0)) {
3494 netdev_err(priv->dev,
3495 "%s: alloc sfty ue MSI %d (error: %d)\n",
3496 __func__, priv->sfty_ue_irq, ret);
3497 irq_err = REQ_IRQ_ERR_SFTY_UE;
3502 /* Request Rx MSI irq */
3503 for (i = 0; i < priv->plat->rx_queues_to_use; i++) {
3504 if (priv->rx_irq[i] == 0)
3507 int_name = priv->int_name_rx_irq[i];
3508 sprintf(int_name, "%s:%s-%d", dev->name, "rx", i);
3509 ret = request_irq(priv->rx_irq[i],
3511 0, int_name, &priv->rx_queue[i]);
3512 if (unlikely(ret < 0)) {
3513 netdev_err(priv->dev,
3514 "%s: alloc rx-%d MSI %d (error: %d)\n",
3515 __func__, i, priv->rx_irq[i], ret);
3516 irq_err = REQ_IRQ_ERR_RX;
3520 cpumask_clear(&cpu_mask);
3521 cpumask_set_cpu(i % num_online_cpus(), &cpu_mask);
3522 irq_set_affinity_hint(priv->rx_irq[i], &cpu_mask);
3525 /* Request Tx MSI irq */
3526 for (i = 0; i < priv->plat->tx_queues_to_use; i++) {
3527 if (priv->tx_irq[i] == 0)
3530 int_name = priv->int_name_tx_irq[i];
3531 sprintf(int_name, "%s:%s-%d", dev->name, "tx", i);
3532 ret = request_irq(priv->tx_irq[i],
3534 0, int_name, &priv->tx_queue[i]);
3535 if (unlikely(ret < 0)) {
3536 netdev_err(priv->dev,
3537 "%s: alloc tx-%d MSI %d (error: %d)\n",
3538 __func__, i, priv->tx_irq[i], ret);
3539 irq_err = REQ_IRQ_ERR_TX;
3543 cpumask_clear(&cpu_mask);
3544 cpumask_set_cpu(i % num_online_cpus(), &cpu_mask);
3545 irq_set_affinity_hint(priv->tx_irq[i], &cpu_mask);
3551 stmmac_free_irq(dev, irq_err, irq_idx);
3555 static int stmmac_request_irq_single(struct net_device *dev)
3557 struct stmmac_priv *priv = netdev_priv(dev);
3558 enum request_irq_err irq_err;
3561 ret = request_irq(dev->irq, stmmac_interrupt,
3562 IRQF_SHARED, dev->name, dev);
3563 if (unlikely(ret < 0)) {
3564 netdev_err(priv->dev,
3565 "%s: ERROR: allocating the IRQ %d (error: %d)\n",
3566 __func__, dev->irq, ret);
3567 irq_err = REQ_IRQ_ERR_MAC;
3571 /* Request the Wake IRQ in case of another line
3574 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq) {
3575 ret = request_irq(priv->wol_irq, stmmac_interrupt,
3576 IRQF_SHARED, dev->name, dev);
3577 if (unlikely(ret < 0)) {
3578 netdev_err(priv->dev,
3579 "%s: ERROR: allocating the WoL IRQ %d (%d)\n",
3580 __func__, priv->wol_irq, ret);
3581 irq_err = REQ_IRQ_ERR_WOL;
3586 /* Request the IRQ lines */
3587 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq) {
3588 ret = request_irq(priv->lpi_irq, stmmac_interrupt,
3589 IRQF_SHARED, dev->name, dev);
3590 if (unlikely(ret < 0)) {
3591 netdev_err(priv->dev,
3592 "%s: ERROR: allocating the LPI IRQ %d (%d)\n",
3593 __func__, priv->lpi_irq, ret);
3594 irq_err = REQ_IRQ_ERR_LPI;
3602 stmmac_free_irq(dev, irq_err, 0);
3606 static int stmmac_request_irq(struct net_device *dev)
3608 struct stmmac_priv *priv = netdev_priv(dev);
3611 /* Request the IRQ lines */
3612 if (priv->plat->multi_msi_en)
3613 ret = stmmac_request_irq_multi_msi(dev);
3615 ret = stmmac_request_irq_single(dev);
3621 * stmmac_open - open entry point of the driver
3622 * @dev : pointer to the device structure.
3624 * This function is the open entry point of the driver.
3626 * 0 on success and an appropriate (-)ve integer as defined in errno.h
3629 int stmmac_open(struct net_device *dev)
3631 struct stmmac_priv *priv = netdev_priv(dev);
3632 int mode = priv->plat->phy_interface;
3637 ret = pm_runtime_get_sync(priv->device);
3639 pm_runtime_put_noidle(priv->device);
3643 if (priv->hw->pcs != STMMAC_PCS_TBI &&
3644 priv->hw->pcs != STMMAC_PCS_RTBI &&
3646 xpcs_get_an_mode(priv->hw->xpcs, mode) != DW_AN_C73)) {
3647 ret = stmmac_init_phy(dev);
3649 netdev_err(priv->dev,
3650 "%s: Cannot attach to PHY (error: %d)\n",
3652 goto init_phy_error;
3656 /* Extra statistics */
3657 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
3658 priv->xstats.threshold = tc;
3660 bfsize = stmmac_set_16kib_bfsize(priv, dev->mtu);
3664 if (bfsize < BUF_SIZE_16KiB)
3665 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
3667 priv->dma_buf_sz = bfsize;
3670 priv->rx_copybreak = STMMAC_RX_COPYBREAK;
3672 if (!priv->dma_tx_size)
3673 priv->dma_tx_size = DMA_DEFAULT_TX_SIZE;
3674 if (!priv->dma_rx_size)
3675 priv->dma_rx_size = DMA_DEFAULT_RX_SIZE;
3677 /* Earlier check for TBS */
3678 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++) {
3679 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
3680 int tbs_en = priv->plat->tx_queues_cfg[chan].tbs_en;
3682 /* Setup per-TXQ tbs flag before TX descriptor alloc */
3683 tx_q->tbs |= tbs_en ? STMMAC_TBS_AVAIL : 0;
3686 ret = alloc_dma_desc_resources(priv);
3688 netdev_err(priv->dev, "%s: DMA descriptors allocation failed\n",
3690 goto dma_desc_error;
3693 ret = init_dma_desc_rings(dev, GFP_KERNEL);
3695 netdev_err(priv->dev, "%s: DMA descriptors initialization failed\n",
3700 ret = stmmac_hw_setup(dev, true);
3702 netdev_err(priv->dev, "%s: Hw setup failed\n", __func__);
3706 stmmac_init_coalesce(priv);
3708 phylink_start(priv->phylink);
3709 /* We may have called phylink_speed_down before */
3710 phylink_speed_up(priv->phylink);
3712 ret = stmmac_request_irq(dev);
3716 stmmac_enable_all_queues(priv);
3717 netif_tx_start_all_queues(priv->dev);
3722 phylink_stop(priv->phylink);
3724 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
3725 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
3727 stmmac_hw_teardown(dev);
3729 free_dma_desc_resources(priv);
3731 phylink_disconnect_phy(priv->phylink);
3733 pm_runtime_put(priv->device);
3737 static void stmmac_fpe_stop_wq(struct stmmac_priv *priv)
3739 set_bit(__FPE_REMOVING, &priv->fpe_task_state);
3742 destroy_workqueue(priv->fpe_wq);
3744 netdev_info(priv->dev, "FPE workqueue stop");
3748 * stmmac_release - close entry point of the driver
3749 * @dev : device pointer.
3751 * This is the stop entry point of the driver.
3753 int stmmac_release(struct net_device *dev)
3755 struct stmmac_priv *priv = netdev_priv(dev);
3758 if (device_may_wakeup(priv->device))
3759 phylink_speed_down(priv->phylink, false);
3760 /* Stop and disconnect the PHY */
3761 phylink_stop(priv->phylink);
3762 phylink_disconnect_phy(priv->phylink);
3764 stmmac_disable_all_queues(priv);
3766 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
3767 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
3769 /* Free the IRQ lines */
3770 stmmac_free_irq(dev, REQ_IRQ_ERR_ALL, 0);
3772 if (priv->eee_enabled) {
3773 priv->tx_path_in_lpi_mode = false;
3774 del_timer_sync(&priv->eee_ctrl_timer);
3777 /* Stop TX/RX DMA and clear the descriptors */
3778 stmmac_stop_all_dma(priv);
3780 /* Release and free the Rx/Tx resources */
3781 free_dma_desc_resources(priv);
3783 /* Disable the MAC Rx/Tx */
3784 stmmac_mac_set(priv, priv->ioaddr, false);
3786 netif_carrier_off(dev);
3788 stmmac_release_ptp(priv);
3790 pm_runtime_put(priv->device);
3792 if (priv->dma_cap.fpesel)
3793 stmmac_fpe_stop_wq(priv);
3798 static bool stmmac_vlan_insert(struct stmmac_priv *priv, struct sk_buff *skb,
3799 struct stmmac_tx_queue *tx_q)
3801 u16 tag = 0x0, inner_tag = 0x0;
3802 u32 inner_type = 0x0;
3805 if (!priv->dma_cap.vlins)
3807 if (!skb_vlan_tag_present(skb))
3809 if (skb->vlan_proto == htons(ETH_P_8021AD)) {
3810 inner_tag = skb_vlan_tag_get(skb);
3811 inner_type = STMMAC_VLAN_INSERT;
3814 tag = skb_vlan_tag_get(skb);
3816 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3817 p = &tx_q->dma_entx[tx_q->cur_tx].basic;
3819 p = &tx_q->dma_tx[tx_q->cur_tx];
3821 if (stmmac_set_desc_vlan_tag(priv, p, tag, inner_tag, inner_type))
3824 stmmac_set_tx_owner(priv, p);
3825 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
3830 * stmmac_tso_allocator - close entry point of the driver
3831 * @priv: driver private structure
3832 * @des: buffer start address
3833 * @total_len: total length to fill in descriptors
3834 * @last_segment: condition for the last descriptor
3835 * @queue: TX queue index
3837 * This function fills descriptor and request new descriptors according to
3838 * buffer length to fill
3840 static void stmmac_tso_allocator(struct stmmac_priv *priv, dma_addr_t des,
3841 int total_len, bool last_segment, u32 queue)
3843 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
3844 struct dma_desc *desc;
3848 tmp_len = total_len;
3850 while (tmp_len > 0) {
3851 dma_addr_t curr_addr;
3853 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx,
3855 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
3857 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3858 desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
3860 desc = &tx_q->dma_tx[tx_q->cur_tx];
3862 curr_addr = des + (total_len - tmp_len);
3863 if (priv->dma_cap.addr64 <= 32)
3864 desc->des0 = cpu_to_le32(curr_addr);
3866 stmmac_set_desc_addr(priv, desc, curr_addr);
3868 buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ?
3869 TSO_MAX_BUFF_SIZE : tmp_len;
3871 stmmac_prepare_tso_tx_desc(priv, desc, 0, buff_size,
3873 (last_segment) && (tmp_len <= TSO_MAX_BUFF_SIZE),
3876 tmp_len -= TSO_MAX_BUFF_SIZE;
3880 static void stmmac_flush_tx_descriptors(struct stmmac_priv *priv, int queue)
3882 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
3885 if (likely(priv->extend_desc))
3886 desc_size = sizeof(struct dma_extended_desc);
3887 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
3888 desc_size = sizeof(struct dma_edesc);
3890 desc_size = sizeof(struct dma_desc);
3892 /* The own bit must be the latest setting done when prepare the
3893 * descriptor and then barrier is needed to make sure that
3894 * all is coherent before granting the DMA engine.
3898 tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * desc_size);
3899 stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue);
3903 * stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO)
3904 * @skb : the socket buffer
3905 * @dev : device pointer
3906 * Description: this is the transmit function that is called on TSO frames
3907 * (support available on GMAC4 and newer chips).
3908 * Diagram below show the ring programming in case of TSO frames:
3912 * | DES0 |---> buffer1 = L2/L3/L4 header
3913 * | DES1 |---> TCP Payload (can continue on next descr...)
3914 * | DES2 |---> buffer 1 and 2 len
3915 * | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0]
3921 * | DES0 | --| Split TCP Payload on Buffers 1 and 2
3923 * | DES2 | --> buffer 1 and 2 len
3927 * mss is fixed when enable tso, so w/o programming the TDES3 ctx field.
3929 static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev)
3931 struct dma_desc *desc, *first, *mss_desc = NULL;
3932 struct stmmac_priv *priv = netdev_priv(dev);
3933 int nfrags = skb_shinfo(skb)->nr_frags;
3934 u32 queue = skb_get_queue_mapping(skb);
3935 unsigned int first_entry, tx_packets;
3936 int tmp_pay_len = 0, first_tx;
3937 struct stmmac_tx_queue *tx_q;
3938 bool has_vlan, set_ic;
3939 u8 proto_hdr_len, hdr;
3944 tx_q = &priv->tx_queue[queue];
3945 first_tx = tx_q->cur_tx;
3947 /* Compute header lengths */
3948 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
3949 proto_hdr_len = skb_transport_offset(skb) + sizeof(struct udphdr);
3950 hdr = sizeof(struct udphdr);
3952 proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
3953 hdr = tcp_hdrlen(skb);
3956 /* Desc availability based on threshold should be enough safe */
3957 if (unlikely(stmmac_tx_avail(priv, queue) <
3958 (((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) {
3959 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
3960 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
3962 /* This is a hard error, log it. */
3963 netdev_err(priv->dev,
3964 "%s: Tx Ring full when queue awake\n",
3967 return NETDEV_TX_BUSY;
3970 pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */
3972 mss = skb_shinfo(skb)->gso_size;
3974 /* set new MSS value if needed */
3975 if (mss != tx_q->mss) {
3976 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3977 mss_desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
3979 mss_desc = &tx_q->dma_tx[tx_q->cur_tx];
3981 stmmac_set_mss(priv, mss_desc, mss);
3983 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx,
3985 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
3988 if (netif_msg_tx_queued(priv)) {
3989 pr_info("%s: hdrlen %d, hdr_len %d, pay_len %d, mss %d\n",
3990 __func__, hdr, proto_hdr_len, pay_len, mss);
3991 pr_info("\tskb->len %d, skb->data_len %d\n", skb->len,
3995 /* Check if VLAN can be inserted by HW */
3996 has_vlan = stmmac_vlan_insert(priv, skb, tx_q);
3998 first_entry = tx_q->cur_tx;
3999 WARN_ON(tx_q->tx_skbuff[first_entry]);
4001 if (tx_q->tbs & STMMAC_TBS_AVAIL)
4002 desc = &tx_q->dma_entx[first_entry].basic;
4004 desc = &tx_q->dma_tx[first_entry];
4008 stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT);
4010 /* first descriptor: fill Headers on Buf1 */
4011 des = dma_map_single(priv->device, skb->data, skb_headlen(skb),
4013 if (dma_mapping_error(priv->device, des))
4016 tx_q->tx_skbuff_dma[first_entry].buf = des;
4017 tx_q->tx_skbuff_dma[first_entry].len = skb_headlen(skb);
4018 tx_q->tx_skbuff_dma[first_entry].map_as_page = false;
4019 tx_q->tx_skbuff_dma[first_entry].buf_type = STMMAC_TXBUF_T_SKB;
4021 if (priv->dma_cap.addr64 <= 32) {
4022 first->des0 = cpu_to_le32(des);
4024 /* Fill start of payload in buff2 of first descriptor */
4026 first->des1 = cpu_to_le32(des + proto_hdr_len);
4028 /* If needed take extra descriptors to fill the remaining payload */
4029 tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE;
4031 stmmac_set_desc_addr(priv, first, des);
4032 tmp_pay_len = pay_len;
4033 des += proto_hdr_len;
4037 stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0), queue);
4039 /* Prepare fragments */
4040 for (i = 0; i < nfrags; i++) {
4041 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4043 des = skb_frag_dma_map(priv->device, frag, 0,
4044 skb_frag_size(frag),
4046 if (dma_mapping_error(priv->device, des))
4049 stmmac_tso_allocator(priv, des, skb_frag_size(frag),
4050 (i == nfrags - 1), queue);
4052 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf = des;
4053 tx_q->tx_skbuff_dma[tx_q->cur_tx].len = skb_frag_size(frag);
4054 tx_q->tx_skbuff_dma[tx_q->cur_tx].map_as_page = true;
4055 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf_type = STMMAC_TXBUF_T_SKB;
4058 tx_q->tx_skbuff_dma[tx_q->cur_tx].last_segment = true;
4060 /* Only the last descriptor gets to point to the skb. */
4061 tx_q->tx_skbuff[tx_q->cur_tx] = skb;
4062 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf_type = STMMAC_TXBUF_T_SKB;
4064 /* Manage tx mitigation */
4065 tx_packets = (tx_q->cur_tx + 1) - first_tx;
4066 tx_q->tx_count_frames += tx_packets;
4068 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en)
4070 else if (!priv->tx_coal_frames[queue])
4072 else if (tx_packets > priv->tx_coal_frames[queue])
4074 else if ((tx_q->tx_count_frames %
4075 priv->tx_coal_frames[queue]) < tx_packets)
4081 if (tx_q->tbs & STMMAC_TBS_AVAIL)
4082 desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
4084 desc = &tx_q->dma_tx[tx_q->cur_tx];
4086 tx_q->tx_count_frames = 0;
4087 stmmac_set_tx_ic(priv, desc);
4088 priv->xstats.tx_set_ic_bit++;
4091 /* We've used all descriptors we need for this skb, however,
4092 * advance cur_tx so that it references a fresh descriptor.
4093 * ndo_start_xmit will fill this descriptor the next time it's
4094 * called and stmmac_tx_clean may clean up to this descriptor.
4096 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
4098 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
4099 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
4101 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
4104 dev->stats.tx_bytes += skb->len;
4105 priv->xstats.tx_tso_frames++;
4106 priv->xstats.tx_tso_nfrags += nfrags;
4108 if (priv->sarc_type)
4109 stmmac_set_desc_sarc(priv, first, priv->sarc_type);
4111 skb_tx_timestamp(skb);
4113 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4114 priv->hwts_tx_en)) {
4115 /* declare that device is doing timestamping */
4116 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4117 stmmac_enable_tx_timestamp(priv, first);
4120 /* Complete the first descriptor before granting the DMA */
4121 stmmac_prepare_tso_tx_desc(priv, first, 1,
4124 1, tx_q->tx_skbuff_dma[first_entry].last_segment,
4125 hdr / 4, (skb->len - proto_hdr_len));
4127 /* If context desc is used to change MSS */
4129 /* Make sure that first descriptor has been completely
4130 * written, including its own bit. This is because MSS is
4131 * actually before first descriptor, so we need to make
4132 * sure that MSS's own bit is the last thing written.
4135 stmmac_set_tx_owner(priv, mss_desc);
4138 if (netif_msg_pktdata(priv)) {
4139 pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n",
4140 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
4141 tx_q->cur_tx, first, nfrags);
4142 pr_info(">>> frame to be transmitted: ");
4143 print_pkt(skb->data, skb_headlen(skb));
4146 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
4148 stmmac_flush_tx_descriptors(priv, queue);
4149 stmmac_tx_timer_arm(priv, queue);
4151 return NETDEV_TX_OK;
4154 dev_err(priv->device, "Tx dma map failed\n");
4156 priv->dev->stats.tx_dropped++;
4157 return NETDEV_TX_OK;
4161 * stmmac_xmit - Tx entry point of the driver
4162 * @skb : the socket buffer
4163 * @dev : device pointer
4164 * Description : this is the tx entry point of the driver.
4165 * It programs the chain or the ring and supports oversized frames
4168 static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
4170 unsigned int first_entry, tx_packets, enh_desc;
4171 struct stmmac_priv *priv = netdev_priv(dev);
4172 unsigned int nopaged_len = skb_headlen(skb);
4173 int i, csum_insertion = 0, is_jumbo = 0;
4174 u32 queue = skb_get_queue_mapping(skb);
4175 int nfrags = skb_shinfo(skb)->nr_frags;
4176 int gso = skb_shinfo(skb)->gso_type;
4177 struct dma_edesc *tbs_desc = NULL;
4178 struct dma_desc *desc, *first;
4179 struct stmmac_tx_queue *tx_q;
4180 bool has_vlan, set_ic;
4181 int entry, first_tx;
4184 tx_q = &priv->tx_queue[queue];
4185 first_tx = tx_q->cur_tx;
4187 if (priv->tx_path_in_lpi_mode && priv->eee_sw_timer_en)
4188 stmmac_disable_eee_mode(priv);
4190 /* Manage oversized TCP frames for GMAC4 device */
4191 if (skb_is_gso(skb) && priv->tso) {
4192 if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
4193 return stmmac_tso_xmit(skb, dev);
4194 if (priv->plat->has_gmac4 && (gso & SKB_GSO_UDP_L4))
4195 return stmmac_tso_xmit(skb, dev);
4198 if (unlikely(stmmac_tx_avail(priv, queue) < nfrags + 1)) {
4199 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
4200 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
4202 /* This is a hard error, log it. */
4203 netdev_err(priv->dev,
4204 "%s: Tx Ring full when queue awake\n",
4207 return NETDEV_TX_BUSY;
4210 /* Check if VLAN can be inserted by HW */
4211 has_vlan = stmmac_vlan_insert(priv, skb, tx_q);
4213 entry = tx_q->cur_tx;
4214 first_entry = entry;
4215 WARN_ON(tx_q->tx_skbuff[first_entry]);
4217 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
4219 if (likely(priv->extend_desc))
4220 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
4221 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4222 desc = &tx_q->dma_entx[entry].basic;
4224 desc = tx_q->dma_tx + entry;
4229 stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT);
4231 enh_desc = priv->plat->enh_desc;
4232 /* To program the descriptors according to the size of the frame */
4234 is_jumbo = stmmac_is_jumbo_frm(priv, skb->len, enh_desc);
4236 if (unlikely(is_jumbo)) {
4237 entry = stmmac_jumbo_frm(priv, tx_q, skb, csum_insertion);
4238 if (unlikely(entry < 0) && (entry != -EINVAL))
4242 for (i = 0; i < nfrags; i++) {
4243 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4244 int len = skb_frag_size(frag);
4245 bool last_segment = (i == (nfrags - 1));
4247 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
4248 WARN_ON(tx_q->tx_skbuff[entry]);
4250 if (likely(priv->extend_desc))
4251 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
4252 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4253 desc = &tx_q->dma_entx[entry].basic;
4255 desc = tx_q->dma_tx + entry;
4257 des = skb_frag_dma_map(priv->device, frag, 0, len,
4259 if (dma_mapping_error(priv->device, des))
4260 goto dma_map_err; /* should reuse desc w/o issues */
4262 tx_q->tx_skbuff_dma[entry].buf = des;
4264 stmmac_set_desc_addr(priv, desc, des);
4266 tx_q->tx_skbuff_dma[entry].map_as_page = true;
4267 tx_q->tx_skbuff_dma[entry].len = len;
4268 tx_q->tx_skbuff_dma[entry].last_segment = last_segment;
4269 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_SKB;
4271 /* Prepare the descriptor and set the own bit too */
4272 stmmac_prepare_tx_desc(priv, desc, 0, len, csum_insertion,
4273 priv->mode, 1, last_segment, skb->len);
4276 /* Only the last descriptor gets to point to the skb. */
4277 tx_q->tx_skbuff[entry] = skb;
4278 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_SKB;
4280 /* According to the coalesce parameter the IC bit for the latest
4281 * segment is reset and the timer re-started to clean the tx status.
4282 * This approach takes care about the fragments: desc is the first
4283 * element in case of no SG.
4285 tx_packets = (entry + 1) - first_tx;
4286 tx_q->tx_count_frames += tx_packets;
4288 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en)
4290 else if (!priv->tx_coal_frames[queue])
4292 else if (tx_packets > priv->tx_coal_frames[queue])
4294 else if ((tx_q->tx_count_frames %
4295 priv->tx_coal_frames[queue]) < tx_packets)
4301 if (likely(priv->extend_desc))
4302 desc = &tx_q->dma_etx[entry].basic;
4303 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4304 desc = &tx_q->dma_entx[entry].basic;
4306 desc = &tx_q->dma_tx[entry];
4308 tx_q->tx_count_frames = 0;
4309 stmmac_set_tx_ic(priv, desc);
4310 priv->xstats.tx_set_ic_bit++;
4313 /* We've used all descriptors we need for this skb, however,
4314 * advance cur_tx so that it references a fresh descriptor.
4315 * ndo_start_xmit will fill this descriptor the next time it's
4316 * called and stmmac_tx_clean may clean up to this descriptor.
4318 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
4319 tx_q->cur_tx = entry;
4321 if (netif_msg_pktdata(priv)) {
4322 netdev_dbg(priv->dev,
4323 "%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
4324 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
4325 entry, first, nfrags);
4327 netdev_dbg(priv->dev, ">>> frame to be transmitted: ");
4328 print_pkt(skb->data, skb->len);
4331 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
4332 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
4334 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
4337 dev->stats.tx_bytes += skb->len;
4339 if (priv->sarc_type)
4340 stmmac_set_desc_sarc(priv, first, priv->sarc_type);
4342 skb_tx_timestamp(skb);
4344 /* Ready to fill the first descriptor and set the OWN bit w/o any
4345 * problems because all the descriptors are actually ready to be
4346 * passed to the DMA engine.
4348 if (likely(!is_jumbo)) {
4349 bool last_segment = (nfrags == 0);
4351 des = dma_map_single(priv->device, skb->data,
4352 nopaged_len, DMA_TO_DEVICE);
4353 if (dma_mapping_error(priv->device, des))
4356 tx_q->tx_skbuff_dma[first_entry].buf = des;
4357 tx_q->tx_skbuff_dma[first_entry].buf_type = STMMAC_TXBUF_T_SKB;
4358 tx_q->tx_skbuff_dma[first_entry].map_as_page = false;
4360 stmmac_set_desc_addr(priv, first, des);
4362 tx_q->tx_skbuff_dma[first_entry].len = nopaged_len;
4363 tx_q->tx_skbuff_dma[first_entry].last_segment = last_segment;
4365 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4366 priv->hwts_tx_en)) {
4367 /* declare that device is doing timestamping */
4368 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4369 stmmac_enable_tx_timestamp(priv, first);
4372 /* Prepare the first descriptor setting the OWN bit too */
4373 stmmac_prepare_tx_desc(priv, first, 1, nopaged_len,
4374 csum_insertion, priv->mode, 0, last_segment,
4378 if (tx_q->tbs & STMMAC_TBS_EN) {
4379 struct timespec64 ts = ns_to_timespec64(skb->tstamp);
4381 tbs_desc = &tx_q->dma_entx[first_entry];
4382 stmmac_set_desc_tbs(priv, tbs_desc, ts.tv_sec, ts.tv_nsec);
4385 stmmac_set_tx_owner(priv, first);
4387 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
4389 stmmac_enable_dma_transmission(priv, priv->ioaddr);
4391 stmmac_flush_tx_descriptors(priv, queue);
4392 stmmac_tx_timer_arm(priv, queue);
4394 return NETDEV_TX_OK;
4397 netdev_err(priv->dev, "Tx DMA map failed\n");
4399 priv->dev->stats.tx_dropped++;
4400 return NETDEV_TX_OK;
4403 static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
4405 struct vlan_ethhdr *veth;
4409 veth = (struct vlan_ethhdr *)skb->data;
4410 vlan_proto = veth->h_vlan_proto;
4412 if ((vlan_proto == htons(ETH_P_8021Q) &&
4413 dev->features & NETIF_F_HW_VLAN_CTAG_RX) ||
4414 (vlan_proto == htons(ETH_P_8021AD) &&
4415 dev->features & NETIF_F_HW_VLAN_STAG_RX)) {
4416 /* pop the vlan tag */
4417 vlanid = ntohs(veth->h_vlan_TCI);
4418 memmove(skb->data + VLAN_HLEN, veth, ETH_ALEN * 2);
4419 skb_pull(skb, VLAN_HLEN);
4420 __vlan_hwaccel_put_tag(skb, vlan_proto, vlanid);
4425 * stmmac_rx_refill - refill used skb preallocated buffers
4426 * @priv: driver private structure
4427 * @queue: RX queue index
4428 * Description : this is to reallocate the skb for the reception process
4429 * that is based on zero-copy.
4431 static inline void stmmac_rx_refill(struct stmmac_priv *priv, u32 queue)
4433 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4434 int dirty = stmmac_rx_dirty(priv, queue);
4435 unsigned int entry = rx_q->dirty_rx;
4437 while (dirty-- > 0) {
4438 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry];
4442 if (priv->extend_desc)
4443 p = (struct dma_desc *)(rx_q->dma_erx + entry);
4445 p = rx_q->dma_rx + entry;
4448 buf->page = page_pool_dev_alloc_pages(rx_q->page_pool);
4453 if (priv->sph && !buf->sec_page) {
4454 buf->sec_page = page_pool_dev_alloc_pages(rx_q->page_pool);
4458 buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
4461 buf->addr = page_pool_get_dma_addr(buf->page) + buf->page_offset;
4463 stmmac_set_desc_addr(priv, p, buf->addr);
4465 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true);
4467 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false);
4468 stmmac_refill_desc3(priv, rx_q, p);
4470 rx_q->rx_count_frames++;
4471 rx_q->rx_count_frames += priv->rx_coal_frames[queue];
4472 if (rx_q->rx_count_frames > priv->rx_coal_frames[queue])
4473 rx_q->rx_count_frames = 0;
4475 use_rx_wd = !priv->rx_coal_frames[queue];
4476 use_rx_wd |= rx_q->rx_count_frames > 0;
4477 if (!priv->use_riwt)
4481 stmmac_set_rx_owner(priv, p, use_rx_wd);
4483 entry = STMMAC_GET_ENTRY(entry, priv->dma_rx_size);
4485 rx_q->dirty_rx = entry;
4486 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
4487 (rx_q->dirty_rx * sizeof(struct dma_desc));
4488 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue);
4491 static unsigned int stmmac_rx_buf1_len(struct stmmac_priv *priv,
4493 int status, unsigned int len)
4495 unsigned int plen = 0, hlen = 0;
4496 int coe = priv->hw->rx_csum;
4498 /* Not first descriptor, buffer is always zero */
4499 if (priv->sph && len)
4502 /* First descriptor, get split header length */
4503 stmmac_get_rx_header_len(priv, p, &hlen);
4504 if (priv->sph && hlen) {
4505 priv->xstats.rx_split_hdr_pkt_n++;
4509 /* First descriptor, not last descriptor and not split header */
4510 if (status & rx_not_ls)
4511 return priv->dma_buf_sz;
4513 plen = stmmac_get_rx_frame_len(priv, p, coe);
4515 /* First descriptor and last descriptor and not split header */
4516 return min_t(unsigned int, priv->dma_buf_sz, plen);
4519 static unsigned int stmmac_rx_buf2_len(struct stmmac_priv *priv,
4521 int status, unsigned int len)
4523 int coe = priv->hw->rx_csum;
4524 unsigned int plen = 0;
4526 /* Not split header, buffer is not available */
4530 /* Not last descriptor */
4531 if (status & rx_not_ls)
4532 return priv->dma_buf_sz;
4534 plen = stmmac_get_rx_frame_len(priv, p, coe);
4536 /* Last descriptor */
4540 static int stmmac_xdp_xmit_xdpf(struct stmmac_priv *priv, int queue,
4541 struct xdp_frame *xdpf, bool dma_map)
4543 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
4544 unsigned int entry = tx_q->cur_tx;
4545 struct dma_desc *tx_desc;
4546 dma_addr_t dma_addr;
4549 if (stmmac_tx_avail(priv, queue) < STMMAC_TX_THRESH(priv))
4550 return STMMAC_XDP_CONSUMED;
4552 if (likely(priv->extend_desc))
4553 tx_desc = (struct dma_desc *)(tx_q->dma_etx + entry);
4554 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4555 tx_desc = &tx_q->dma_entx[entry].basic;
4557 tx_desc = tx_q->dma_tx + entry;
4560 dma_addr = dma_map_single(priv->device, xdpf->data,
4561 xdpf->len, DMA_TO_DEVICE);
4562 if (dma_mapping_error(priv->device, dma_addr))
4563 return STMMAC_XDP_CONSUMED;
4565 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XDP_NDO;
4567 struct page *page = virt_to_page(xdpf->data);
4569 dma_addr = page_pool_get_dma_addr(page) + sizeof(*xdpf) +
4571 dma_sync_single_for_device(priv->device, dma_addr,
4572 xdpf->len, DMA_BIDIRECTIONAL);
4574 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XDP_TX;
4577 tx_q->tx_skbuff_dma[entry].buf = dma_addr;
4578 tx_q->tx_skbuff_dma[entry].map_as_page = false;
4579 tx_q->tx_skbuff_dma[entry].len = xdpf->len;
4580 tx_q->tx_skbuff_dma[entry].last_segment = true;
4581 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
4583 tx_q->xdpf[entry] = xdpf;
4585 stmmac_set_desc_addr(priv, tx_desc, dma_addr);
4587 stmmac_prepare_tx_desc(priv, tx_desc, 1, xdpf->len,
4588 true, priv->mode, true, true,
4591 tx_q->tx_count_frames++;
4593 if (tx_q->tx_count_frames % priv->tx_coal_frames[queue] == 0)
4599 tx_q->tx_count_frames = 0;
4600 stmmac_set_tx_ic(priv, tx_desc);
4601 priv->xstats.tx_set_ic_bit++;
4604 stmmac_enable_dma_transmission(priv, priv->ioaddr);
4606 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
4607 tx_q->cur_tx = entry;
4609 return STMMAC_XDP_TX;
4612 static int stmmac_xdp_get_tx_queue(struct stmmac_priv *priv,
4617 if (unlikely(index < 0))
4620 while (index >= priv->plat->tx_queues_to_use)
4621 index -= priv->plat->tx_queues_to_use;
4626 static int stmmac_xdp_xmit_back(struct stmmac_priv *priv,
4627 struct xdp_buff *xdp)
4629 struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp);
4630 int cpu = smp_processor_id();
4631 struct netdev_queue *nq;
4635 if (unlikely(!xdpf))
4636 return STMMAC_XDP_CONSUMED;
4638 queue = stmmac_xdp_get_tx_queue(priv, cpu);
4639 nq = netdev_get_tx_queue(priv->dev, queue);
4641 __netif_tx_lock(nq, cpu);
4642 /* Avoids TX time-out as we are sharing with slow path */
4643 nq->trans_start = jiffies;
4645 res = stmmac_xdp_xmit_xdpf(priv, queue, xdpf, false);
4646 if (res == STMMAC_XDP_TX)
4647 stmmac_flush_tx_descriptors(priv, queue);
4649 __netif_tx_unlock(nq);
4654 static int __stmmac_xdp_run_prog(struct stmmac_priv *priv,
4655 struct bpf_prog *prog,
4656 struct xdp_buff *xdp)
4661 act = bpf_prog_run_xdp(prog, xdp);
4664 res = STMMAC_XDP_PASS;
4667 res = stmmac_xdp_xmit_back(priv, xdp);
4670 if (xdp_do_redirect(priv->dev, xdp, prog) < 0)
4671 res = STMMAC_XDP_CONSUMED;
4673 res = STMMAC_XDP_REDIRECT;
4676 bpf_warn_invalid_xdp_action(act);
4679 trace_xdp_exception(priv->dev, prog, act);
4682 res = STMMAC_XDP_CONSUMED;
4689 static struct sk_buff *stmmac_xdp_run_prog(struct stmmac_priv *priv,
4690 struct xdp_buff *xdp)
4692 struct bpf_prog *prog;
4695 prog = READ_ONCE(priv->xdp_prog);
4697 res = STMMAC_XDP_PASS;
4701 res = __stmmac_xdp_run_prog(priv, prog, xdp);
4703 return ERR_PTR(-res);
4706 static void stmmac_finalize_xdp_rx(struct stmmac_priv *priv,
4709 int cpu = smp_processor_id();
4712 queue = stmmac_xdp_get_tx_queue(priv, cpu);
4714 if (xdp_status & STMMAC_XDP_TX)
4715 stmmac_tx_timer_arm(priv, queue);
4717 if (xdp_status & STMMAC_XDP_REDIRECT)
4721 static struct sk_buff *stmmac_construct_skb_zc(struct stmmac_channel *ch,
4722 struct xdp_buff *xdp)
4724 unsigned int metasize = xdp->data - xdp->data_meta;
4725 unsigned int datasize = xdp->data_end - xdp->data;
4726 struct sk_buff *skb;
4728 skb = __napi_alloc_skb(&ch->rxtx_napi,
4729 xdp->data_end - xdp->data_hard_start,
4730 GFP_ATOMIC | __GFP_NOWARN);
4734 skb_reserve(skb, xdp->data - xdp->data_hard_start);
4735 memcpy(__skb_put(skb, datasize), xdp->data, datasize);
4737 skb_metadata_set(skb, metasize);
4742 static void stmmac_dispatch_skb_zc(struct stmmac_priv *priv, u32 queue,
4743 struct dma_desc *p, struct dma_desc *np,
4744 struct xdp_buff *xdp)
4746 struct stmmac_channel *ch = &priv->channel[queue];
4747 unsigned int len = xdp->data_end - xdp->data;
4748 enum pkt_hash_types hash_type;
4749 int coe = priv->hw->rx_csum;
4750 struct sk_buff *skb;
4753 skb = stmmac_construct_skb_zc(ch, xdp);
4755 priv->dev->stats.rx_dropped++;
4759 stmmac_get_rx_hwtstamp(priv, p, np, skb);
4760 stmmac_rx_vlan(priv->dev, skb);
4761 skb->protocol = eth_type_trans(skb, priv->dev);
4764 skb_checksum_none_assert(skb);
4766 skb->ip_summed = CHECKSUM_UNNECESSARY;
4768 if (!stmmac_get_rx_hash(priv, p, &hash, &hash_type))
4769 skb_set_hash(skb, hash, hash_type);
4771 skb_record_rx_queue(skb, queue);
4772 napi_gro_receive(&ch->rxtx_napi, skb);
4774 priv->dev->stats.rx_packets++;
4775 priv->dev->stats.rx_bytes += len;
4778 static bool stmmac_rx_refill_zc(struct stmmac_priv *priv, u32 queue, u32 budget)
4780 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4781 unsigned int entry = rx_q->dirty_rx;
4782 struct dma_desc *rx_desc = NULL;
4785 budget = min(budget, stmmac_rx_dirty(priv, queue));
4787 while (budget-- > 0 && entry != rx_q->cur_rx) {
4788 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry];
4789 dma_addr_t dma_addr;
4793 buf->xdp = xsk_buff_alloc(rx_q->xsk_pool);
4800 if (priv->extend_desc)
4801 rx_desc = (struct dma_desc *)(rx_q->dma_erx + entry);
4803 rx_desc = rx_q->dma_rx + entry;
4805 dma_addr = xsk_buff_xdp_get_dma(buf->xdp);
4806 stmmac_set_desc_addr(priv, rx_desc, dma_addr);
4807 stmmac_set_desc_sec_addr(priv, rx_desc, 0, false);
4808 stmmac_refill_desc3(priv, rx_q, rx_desc);
4810 rx_q->rx_count_frames++;
4811 rx_q->rx_count_frames += priv->rx_coal_frames[queue];
4812 if (rx_q->rx_count_frames > priv->rx_coal_frames[queue])
4813 rx_q->rx_count_frames = 0;
4815 use_rx_wd = !priv->rx_coal_frames[queue];
4816 use_rx_wd |= rx_q->rx_count_frames > 0;
4817 if (!priv->use_riwt)
4821 stmmac_set_rx_owner(priv, rx_desc, use_rx_wd);
4823 entry = STMMAC_GET_ENTRY(entry, priv->dma_rx_size);
4827 rx_q->dirty_rx = entry;
4828 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
4829 (rx_q->dirty_rx * sizeof(struct dma_desc));
4830 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue);
4836 static int stmmac_rx_zc(struct stmmac_priv *priv, int limit, u32 queue)
4838 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4839 unsigned int count = 0, error = 0, len = 0;
4840 int dirty = stmmac_rx_dirty(priv, queue);
4841 unsigned int next_entry = rx_q->cur_rx;
4842 unsigned int desc_size;
4843 struct bpf_prog *prog;
4844 bool failure = false;
4848 if (netif_msg_rx_status(priv)) {
4851 netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
4852 if (priv->extend_desc) {
4853 rx_head = (void *)rx_q->dma_erx;
4854 desc_size = sizeof(struct dma_extended_desc);
4856 rx_head = (void *)rx_q->dma_rx;
4857 desc_size = sizeof(struct dma_desc);
4860 stmmac_display_ring(priv, rx_head, priv->dma_rx_size, true,
4861 rx_q->dma_rx_phy, desc_size);
4863 while (count < limit) {
4864 struct stmmac_rx_buffer *buf;
4865 unsigned int buf1_len = 0;
4866 struct dma_desc *np, *p;
4870 if (!count && rx_q->state_saved) {
4871 error = rx_q->state.error;
4872 len = rx_q->state.len;
4874 rx_q->state_saved = false;
4885 buf = &rx_q->buf_pool[entry];
4887 if (dirty >= STMMAC_RX_FILL_BATCH) {
4888 failure = failure ||
4889 !stmmac_rx_refill_zc(priv, queue, dirty);
4893 if (priv->extend_desc)
4894 p = (struct dma_desc *)(rx_q->dma_erx + entry);
4896 p = rx_q->dma_rx + entry;
4898 /* read the status of the incoming frame */
4899 status = stmmac_rx_status(priv, &priv->dev->stats,
4901 /* check if managed by the DMA otherwise go ahead */
4902 if (unlikely(status & dma_own))
4905 /* Prefetch the next RX descriptor */
4906 rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx,
4908 next_entry = rx_q->cur_rx;
4910 if (priv->extend_desc)
4911 np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
4913 np = rx_q->dma_rx + next_entry;
4917 if (priv->extend_desc)
4918 stmmac_rx_extended_status(priv, &priv->dev->stats,
4920 rx_q->dma_erx + entry);
4921 if (unlikely(status == discard_frame)) {
4922 xsk_buff_free(buf->xdp);
4926 if (!priv->hwts_rx_en)
4927 priv->dev->stats.rx_errors++;
4930 if (unlikely(error && (status & rx_not_ls)))
4932 if (unlikely(error)) {
4937 /* Ensure a valid XSK buffer before proceed */
4941 /* XSK pool expects RX frame 1:1 mapped to XSK buffer */
4942 if (likely(status & rx_not_ls)) {
4943 xsk_buff_free(buf->xdp);
4950 /* XDP ZC Frame only support primary buffers for now */
4951 buf1_len = stmmac_rx_buf1_len(priv, p, status, len);
4954 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
4955 * Type frames (LLC/LLC-SNAP)
4957 * llc_snap is never checked in GMAC >= 4, so this ACS
4958 * feature is always disabled and packets need to be
4959 * stripped manually.
4961 if (likely(!(status & rx_not_ls)) &&
4962 (likely(priv->synopsys_id >= DWMAC_CORE_4_00) ||
4963 unlikely(status != llc_snap))) {
4964 buf1_len -= ETH_FCS_LEN;
4968 /* RX buffer is good and fit into a XSK pool buffer */
4969 buf->xdp->data_end = buf->xdp->data + buf1_len;
4970 xsk_buff_dma_sync_for_cpu(buf->xdp, rx_q->xsk_pool);
4972 prog = READ_ONCE(priv->xdp_prog);
4973 res = __stmmac_xdp_run_prog(priv, prog, buf->xdp);
4976 case STMMAC_XDP_PASS:
4977 stmmac_dispatch_skb_zc(priv, queue, p, np, buf->xdp);
4978 xsk_buff_free(buf->xdp);
4980 case STMMAC_XDP_CONSUMED:
4981 xsk_buff_free(buf->xdp);
4982 priv->dev->stats.rx_dropped++;
4985 case STMMAC_XDP_REDIRECT:
4995 if (status & rx_not_ls) {
4996 rx_q->state_saved = true;
4997 rx_q->state.error = error;
4998 rx_q->state.len = len;
5001 stmmac_finalize_xdp_rx(priv, xdp_status);
5003 if (xsk_uses_need_wakeup(rx_q->xsk_pool)) {
5004 if (failure || stmmac_rx_dirty(priv, queue) > 0)
5005 xsk_set_rx_need_wakeup(rx_q->xsk_pool);
5007 xsk_clear_rx_need_wakeup(rx_q->xsk_pool);
5012 return failure ? limit : (int)count;
5016 * stmmac_rx - manage the receive process
5017 * @priv: driver private structure
5018 * @limit: napi bugget
5019 * @queue: RX queue index.
5020 * Description : this the function called by the napi poll method.
5021 * It gets all the frames inside the ring.
5023 static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
5025 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
5026 struct stmmac_channel *ch = &priv->channel[queue];
5027 unsigned int count = 0, error = 0, len = 0;
5028 int status = 0, coe = priv->hw->rx_csum;
5029 unsigned int next_entry = rx_q->cur_rx;
5030 enum dma_data_direction dma_dir;
5031 unsigned int desc_size;
5032 struct sk_buff *skb = NULL;
5033 struct xdp_buff xdp;
5037 dma_dir = page_pool_get_dma_dir(rx_q->page_pool);
5038 buf_sz = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE) * PAGE_SIZE;
5040 if (netif_msg_rx_status(priv)) {
5043 netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
5044 if (priv->extend_desc) {
5045 rx_head = (void *)rx_q->dma_erx;
5046 desc_size = sizeof(struct dma_extended_desc);
5048 rx_head = (void *)rx_q->dma_rx;
5049 desc_size = sizeof(struct dma_desc);
5052 stmmac_display_ring(priv, rx_head, priv->dma_rx_size, true,
5053 rx_q->dma_rx_phy, desc_size);
5055 while (count < limit) {
5056 unsigned int buf1_len = 0, buf2_len = 0;
5057 enum pkt_hash_types hash_type;
5058 struct stmmac_rx_buffer *buf;
5059 struct dma_desc *np, *p;
5063 if (!count && rx_q->state_saved) {
5064 skb = rx_q->state.skb;
5065 error = rx_q->state.error;
5066 len = rx_q->state.len;
5068 rx_q->state_saved = false;
5081 buf = &rx_q->buf_pool[entry];
5083 if (priv->extend_desc)
5084 p = (struct dma_desc *)(rx_q->dma_erx + entry);
5086 p = rx_q->dma_rx + entry;
5088 /* read the status of the incoming frame */
5089 status = stmmac_rx_status(priv, &priv->dev->stats,
5091 /* check if managed by the DMA otherwise go ahead */
5092 if (unlikely(status & dma_own))
5095 rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx,
5097 next_entry = rx_q->cur_rx;
5099 if (priv->extend_desc)
5100 np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
5102 np = rx_q->dma_rx + next_entry;
5106 if (priv->extend_desc)
5107 stmmac_rx_extended_status(priv, &priv->dev->stats,
5108 &priv->xstats, rx_q->dma_erx + entry);
5109 if (unlikely(status == discard_frame)) {
5110 page_pool_recycle_direct(rx_q->page_pool, buf->page);
5113 if (!priv->hwts_rx_en)
5114 priv->dev->stats.rx_errors++;
5117 if (unlikely(error && (status & rx_not_ls)))
5119 if (unlikely(error)) {
5126 /* Buffer is good. Go on. */
5128 prefetch(page_address(buf->page) + buf->page_offset);
5130 prefetch(page_address(buf->sec_page));
5132 buf1_len = stmmac_rx_buf1_len(priv, p, status, len);
5134 buf2_len = stmmac_rx_buf2_len(priv, p, status, len);
5137 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
5138 * Type frames (LLC/LLC-SNAP)
5140 * llc_snap is never checked in GMAC >= 4, so this ACS
5141 * feature is always disabled and packets need to be
5142 * stripped manually.
5144 if (likely(!(status & rx_not_ls)) &&
5145 (likely(priv->synopsys_id >= DWMAC_CORE_4_00) ||
5146 unlikely(status != llc_snap))) {
5148 buf2_len -= ETH_FCS_LEN;
5150 buf1_len -= ETH_FCS_LEN;
5156 unsigned int pre_len, sync_len;
5158 dma_sync_single_for_cpu(priv->device, buf->addr,
5161 xdp_init_buff(&xdp, buf_sz, &rx_q->xdp_rxq);
5162 xdp_prepare_buff(&xdp, page_address(buf->page),
5163 buf->page_offset, buf1_len, false);
5165 pre_len = xdp.data_end - xdp.data_hard_start -
5167 skb = stmmac_xdp_run_prog(priv, &xdp);
5168 /* Due xdp_adjust_tail: DMA sync for_device
5169 * cover max len CPU touch
5171 sync_len = xdp.data_end - xdp.data_hard_start -
5173 sync_len = max(sync_len, pre_len);
5175 /* For Not XDP_PASS verdict */
5177 unsigned int xdp_res = -PTR_ERR(skb);
5179 if (xdp_res & STMMAC_XDP_CONSUMED) {
5180 page_pool_put_page(rx_q->page_pool,
5181 virt_to_head_page(xdp.data),
5184 priv->dev->stats.rx_dropped++;
5186 /* Clear skb as it was set as
5187 * status by XDP program.
5191 if (unlikely((status & rx_not_ls)))
5196 } else if (xdp_res & (STMMAC_XDP_TX |
5197 STMMAC_XDP_REDIRECT)) {
5198 xdp_status |= xdp_res;
5208 /* XDP program may expand or reduce tail */
5209 buf1_len = xdp.data_end - xdp.data;
5211 skb = napi_alloc_skb(&ch->rx_napi, buf1_len);
5213 priv->dev->stats.rx_dropped++;
5218 /* XDP program may adjust header */
5219 skb_copy_to_linear_data(skb, xdp.data, buf1_len);
5220 skb_put(skb, buf1_len);
5222 /* Data payload copied into SKB, page ready for recycle */
5223 page_pool_recycle_direct(rx_q->page_pool, buf->page);
5225 } else if (buf1_len) {
5226 dma_sync_single_for_cpu(priv->device, buf->addr,
5228 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
5229 buf->page, buf->page_offset, buf1_len,
5232 /* Data payload appended into SKB */
5233 page_pool_release_page(rx_q->page_pool, buf->page);
5238 dma_sync_single_for_cpu(priv->device, buf->sec_addr,
5240 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
5241 buf->sec_page, 0, buf2_len,
5244 /* Data payload appended into SKB */
5245 page_pool_release_page(rx_q->page_pool, buf->sec_page);
5246 buf->sec_page = NULL;
5250 if (likely(status & rx_not_ls))
5255 /* Got entire packet into SKB. Finish it. */
5257 stmmac_get_rx_hwtstamp(priv, p, np, skb);
5258 stmmac_rx_vlan(priv->dev, skb);
5259 skb->protocol = eth_type_trans(skb, priv->dev);
5262 skb_checksum_none_assert(skb);
5264 skb->ip_summed = CHECKSUM_UNNECESSARY;
5266 if (!stmmac_get_rx_hash(priv, p, &hash, &hash_type))
5267 skb_set_hash(skb, hash, hash_type);
5269 skb_record_rx_queue(skb, queue);
5270 napi_gro_receive(&ch->rx_napi, skb);
5273 priv->dev->stats.rx_packets++;
5274 priv->dev->stats.rx_bytes += len;
5278 if (status & rx_not_ls || skb) {
5279 rx_q->state_saved = true;
5280 rx_q->state.skb = skb;
5281 rx_q->state.error = error;
5282 rx_q->state.len = len;
5285 stmmac_finalize_xdp_rx(priv, xdp_status);
5287 stmmac_rx_refill(priv, queue);
5289 priv->xstats.rx_pkt_n += count;
5294 static int stmmac_napi_poll_rx(struct napi_struct *napi, int budget)
5296 struct stmmac_channel *ch =
5297 container_of(napi, struct stmmac_channel, rx_napi);
5298 struct stmmac_priv *priv = ch->priv_data;
5299 u32 chan = ch->index;
5302 priv->xstats.napi_poll++;
5304 work_done = stmmac_rx(priv, budget, chan);
5305 if (work_done < budget && napi_complete_done(napi, work_done)) {
5306 unsigned long flags;
5308 spin_lock_irqsave(&ch->lock, flags);
5309 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
5310 spin_unlock_irqrestore(&ch->lock, flags);
5316 static int stmmac_napi_poll_tx(struct napi_struct *napi, int budget)
5318 struct stmmac_channel *ch =
5319 container_of(napi, struct stmmac_channel, tx_napi);
5320 struct stmmac_priv *priv = ch->priv_data;
5321 u32 chan = ch->index;
5324 priv->xstats.napi_poll++;
5326 work_done = stmmac_tx_clean(priv, budget, chan);
5327 work_done = min(work_done, budget);
5329 if (work_done < budget && napi_complete_done(napi, work_done)) {
5330 unsigned long flags;
5332 spin_lock_irqsave(&ch->lock, flags);
5333 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
5334 spin_unlock_irqrestore(&ch->lock, flags);
5340 static int stmmac_napi_poll_rxtx(struct napi_struct *napi, int budget)
5342 struct stmmac_channel *ch =
5343 container_of(napi, struct stmmac_channel, rxtx_napi);
5344 struct stmmac_priv *priv = ch->priv_data;
5345 int rx_done, tx_done;
5346 u32 chan = ch->index;
5348 priv->xstats.napi_poll++;
5350 tx_done = stmmac_tx_clean(priv, budget, chan);
5351 tx_done = min(tx_done, budget);
5353 rx_done = stmmac_rx_zc(priv, budget, chan);
5355 /* If either TX or RX work is not complete, return budget
5358 if (tx_done >= budget || rx_done >= budget)
5361 /* all work done, exit the polling mode */
5362 if (napi_complete_done(napi, rx_done)) {
5363 unsigned long flags;
5365 spin_lock_irqsave(&ch->lock, flags);
5366 /* Both RX and TX work done are compelte,
5367 * so enable both RX & TX IRQs.
5369 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 1);
5370 spin_unlock_irqrestore(&ch->lock, flags);
5373 return min(rx_done, budget - 1);
5378 * @dev : Pointer to net device structure
5379 * @txqueue: the index of the hanging transmit queue
5380 * Description: this function is called when a packet transmission fails to
5381 * complete within a reasonable time. The driver will mark the error in the
5382 * netdev structure and arrange for the device to be reset to a sane state
5383 * in order to transmit a new packet.
5385 static void stmmac_tx_timeout(struct net_device *dev, unsigned int txqueue)
5387 struct stmmac_priv *priv = netdev_priv(dev);
5389 stmmac_global_err(priv);
5393 * stmmac_set_rx_mode - entry point for multicast addressing
5394 * @dev : pointer to the device structure
5396 * This function is a driver entry point which gets called by the kernel
5397 * whenever multicast addresses must be enabled/disabled.
5401 static void stmmac_set_rx_mode(struct net_device *dev)
5403 struct stmmac_priv *priv = netdev_priv(dev);
5405 stmmac_set_filter(priv, priv->hw, dev);
5409 * stmmac_change_mtu - entry point to change MTU size for the device.
5410 * @dev : device pointer.
5411 * @new_mtu : the new MTU size for the device.
5412 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
5413 * to drive packet transmission. Ethernet has an MTU of 1500 octets
5414 * (ETH_DATA_LEN). This value can be changed with ifconfig.
5416 * 0 on success and an appropriate (-)ve integer as defined in errno.h
5419 static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
5421 struct stmmac_priv *priv = netdev_priv(dev);
5422 int txfifosz = priv->plat->tx_fifo_size;
5423 const int mtu = new_mtu;
5426 txfifosz = priv->dma_cap.tx_fifo_size;
5428 txfifosz /= priv->plat->tx_queues_to_use;
5430 if (netif_running(dev)) {
5431 netdev_err(priv->dev, "must be stopped to change its MTU\n");
5435 if (stmmac_xdp_is_enabled(priv) && new_mtu > ETH_DATA_LEN) {
5436 netdev_dbg(priv->dev, "Jumbo frames not supported for XDP\n");
5440 new_mtu = STMMAC_ALIGN(new_mtu);
5442 /* If condition true, FIFO is too small or MTU too large */
5443 if ((txfifosz < new_mtu) || (new_mtu > BUF_SIZE_16KiB))
5448 netdev_update_features(dev);
5453 static netdev_features_t stmmac_fix_features(struct net_device *dev,
5454 netdev_features_t features)
5456 struct stmmac_priv *priv = netdev_priv(dev);
5458 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
5459 features &= ~NETIF_F_RXCSUM;
5461 if (!priv->plat->tx_coe)
5462 features &= ~NETIF_F_CSUM_MASK;
5464 /* Some GMAC devices have a bugged Jumbo frame support that
5465 * needs to have the Tx COE disabled for oversized frames
5466 * (due to limited buffer sizes). In this case we disable
5467 * the TX csum insertion in the TDES and not use SF.
5469 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
5470 features &= ~NETIF_F_CSUM_MASK;
5472 /* Disable tso if asked by ethtool */
5473 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
5474 if (features & NETIF_F_TSO)
5483 static int stmmac_set_features(struct net_device *netdev,
5484 netdev_features_t features)
5486 struct stmmac_priv *priv = netdev_priv(netdev);
5490 /* Keep the COE Type in case of csum is supporting */
5491 if (features & NETIF_F_RXCSUM)
5492 priv->hw->rx_csum = priv->plat->rx_coe;
5494 priv->hw->rx_csum = 0;
5495 /* No check needed because rx_coe has been set before and it will be
5496 * fixed in case of issue.
5498 stmmac_rx_ipc(priv, priv->hw);
5500 sph_en = (priv->hw->rx_csum > 0) && priv->sph;
5502 for (chan = 0; chan < priv->plat->rx_queues_to_use; chan++)
5503 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
5508 static void stmmac_fpe_event_status(struct stmmac_priv *priv, int status)
5510 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
5511 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
5512 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
5513 bool *hs_enable = &fpe_cfg->hs_enable;
5515 if (status == FPE_EVENT_UNKNOWN || !*hs_enable)
5518 /* If LP has sent verify mPacket, LP is FPE capable */
5519 if ((status & FPE_EVENT_RVER) == FPE_EVENT_RVER) {
5520 if (*lp_state < FPE_STATE_CAPABLE)
5521 *lp_state = FPE_STATE_CAPABLE;
5523 /* If user has requested FPE enable, quickly response */
5525 stmmac_fpe_send_mpacket(priv, priv->ioaddr,
5529 /* If Local has sent verify mPacket, Local is FPE capable */
5530 if ((status & FPE_EVENT_TVER) == FPE_EVENT_TVER) {
5531 if (*lo_state < FPE_STATE_CAPABLE)
5532 *lo_state = FPE_STATE_CAPABLE;
5535 /* If LP has sent response mPacket, LP is entering FPE ON */
5536 if ((status & FPE_EVENT_RRSP) == FPE_EVENT_RRSP)
5537 *lp_state = FPE_STATE_ENTERING_ON;
5539 /* If Local has sent response mPacket, Local is entering FPE ON */
5540 if ((status & FPE_EVENT_TRSP) == FPE_EVENT_TRSP)
5541 *lo_state = FPE_STATE_ENTERING_ON;
5543 if (!test_bit(__FPE_REMOVING, &priv->fpe_task_state) &&
5544 !test_and_set_bit(__FPE_TASK_SCHED, &priv->fpe_task_state) &&
5546 queue_work(priv->fpe_wq, &priv->fpe_task);
5550 static void stmmac_common_interrupt(struct stmmac_priv *priv)
5552 u32 rx_cnt = priv->plat->rx_queues_to_use;
5553 u32 tx_cnt = priv->plat->tx_queues_to_use;
5558 xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
5559 queues_count = (rx_cnt > tx_cnt) ? rx_cnt : tx_cnt;
5562 pm_wakeup_event(priv->device, 0);
5564 if (priv->dma_cap.estsel)
5565 stmmac_est_irq_status(priv, priv->ioaddr, priv->dev,
5566 &priv->xstats, tx_cnt);
5568 if (priv->dma_cap.fpesel) {
5569 int status = stmmac_fpe_irq_status(priv, priv->ioaddr,
5572 stmmac_fpe_event_status(priv, status);
5575 /* To handle GMAC own interrupts */
5576 if ((priv->plat->has_gmac) || xmac) {
5577 int status = stmmac_host_irq_status(priv, priv->hw, &priv->xstats);
5579 if (unlikely(status)) {
5580 /* For LPI we need to save the tx status */
5581 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
5582 priv->tx_path_in_lpi_mode = true;
5583 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
5584 priv->tx_path_in_lpi_mode = false;
5587 for (queue = 0; queue < queues_count; queue++) {
5588 status = stmmac_host_mtl_irq_status(priv, priv->hw,
5592 /* PCS link status */
5593 if (priv->hw->pcs) {
5594 if (priv->xstats.pcs_link)
5595 netif_carrier_on(priv->dev);
5597 netif_carrier_off(priv->dev);
5600 stmmac_timestamp_interrupt(priv, priv);
5605 * stmmac_interrupt - main ISR
5606 * @irq: interrupt number.
5607 * @dev_id: to pass the net device pointer.
5608 * Description: this is the main driver interrupt service routine.
5610 * o DMA service routine (to manage incoming frame reception and transmission
5612 * o Core interrupts to manage: remote wake-up, management counter, LPI
5615 static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
5617 struct net_device *dev = (struct net_device *)dev_id;
5618 struct stmmac_priv *priv = netdev_priv(dev);
5620 /* Check if adapter is up */
5621 if (test_bit(STMMAC_DOWN, &priv->state))
5624 /* Check if a fatal error happened */
5625 if (stmmac_safety_feat_interrupt(priv))
5628 /* To handle Common interrupts */
5629 stmmac_common_interrupt(priv);
5631 /* To handle DMA interrupts */
5632 stmmac_dma_interrupt(priv);
5637 static irqreturn_t stmmac_mac_interrupt(int irq, void *dev_id)
5639 struct net_device *dev = (struct net_device *)dev_id;
5640 struct stmmac_priv *priv = netdev_priv(dev);
5642 if (unlikely(!dev)) {
5643 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5647 /* Check if adapter is up */
5648 if (test_bit(STMMAC_DOWN, &priv->state))
5651 /* To handle Common interrupts */
5652 stmmac_common_interrupt(priv);
5657 static irqreturn_t stmmac_safety_interrupt(int irq, void *dev_id)
5659 struct net_device *dev = (struct net_device *)dev_id;
5660 struct stmmac_priv *priv = netdev_priv(dev);
5662 if (unlikely(!dev)) {
5663 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5667 /* Check if adapter is up */
5668 if (test_bit(STMMAC_DOWN, &priv->state))
5671 /* Check if a fatal error happened */
5672 stmmac_safety_feat_interrupt(priv);
5677 static irqreturn_t stmmac_msi_intr_tx(int irq, void *data)
5679 struct stmmac_tx_queue *tx_q = (struct stmmac_tx_queue *)data;
5680 int chan = tx_q->queue_index;
5681 struct stmmac_priv *priv;
5684 priv = container_of(tx_q, struct stmmac_priv, tx_queue[chan]);
5686 if (unlikely(!data)) {
5687 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5691 /* Check if adapter is up */
5692 if (test_bit(STMMAC_DOWN, &priv->state))
5695 status = stmmac_napi_check(priv, chan, DMA_DIR_TX);
5697 if (unlikely(status & tx_hard_error_bump_tc)) {
5698 /* Try to bump up the dma threshold on this failure */
5699 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
5702 if (priv->plat->force_thresh_dma_mode)
5703 stmmac_set_dma_operation_mode(priv,
5708 stmmac_set_dma_operation_mode(priv,
5712 priv->xstats.threshold = tc;
5714 } else if (unlikely(status == tx_hard_error)) {
5715 stmmac_tx_err(priv, chan);
5721 static irqreturn_t stmmac_msi_intr_rx(int irq, void *data)
5723 struct stmmac_rx_queue *rx_q = (struct stmmac_rx_queue *)data;
5724 int chan = rx_q->queue_index;
5725 struct stmmac_priv *priv;
5727 priv = container_of(rx_q, struct stmmac_priv, rx_queue[chan]);
5729 if (unlikely(!data)) {
5730 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5734 /* Check if adapter is up */
5735 if (test_bit(STMMAC_DOWN, &priv->state))
5738 stmmac_napi_check(priv, chan, DMA_DIR_RX);
5743 #ifdef CONFIG_NET_POLL_CONTROLLER
5744 /* Polling receive - used by NETCONSOLE and other diagnostic tools
5745 * to allow network I/O with interrupts disabled.
5747 static void stmmac_poll_controller(struct net_device *dev)
5749 struct stmmac_priv *priv = netdev_priv(dev);
5752 /* If adapter is down, do nothing */
5753 if (test_bit(STMMAC_DOWN, &priv->state))
5756 if (priv->plat->multi_msi_en) {
5757 for (i = 0; i < priv->plat->rx_queues_to_use; i++)
5758 stmmac_msi_intr_rx(0, &priv->rx_queue[i]);
5760 for (i = 0; i < priv->plat->tx_queues_to_use; i++)
5761 stmmac_msi_intr_tx(0, &priv->tx_queue[i]);
5763 disable_irq(dev->irq);
5764 stmmac_interrupt(dev->irq, dev);
5765 enable_irq(dev->irq);
5771 * stmmac_ioctl - Entry point for the Ioctl
5772 * @dev: Device pointer.
5773 * @rq: An IOCTL specefic structure, that can contain a pointer to
5774 * a proprietary structure used to pass information to the driver.
5775 * @cmd: IOCTL command
5777 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
5779 static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
5781 struct stmmac_priv *priv = netdev_priv (dev);
5782 int ret = -EOPNOTSUPP;
5784 if (!netif_running(dev))
5791 ret = phylink_mii_ioctl(priv->phylink, rq, cmd);
5794 ret = stmmac_hwtstamp_set(dev, rq);
5797 ret = stmmac_hwtstamp_get(dev, rq);
5806 static int stmmac_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
5809 struct stmmac_priv *priv = cb_priv;
5810 int ret = -EOPNOTSUPP;
5812 if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
5815 __stmmac_disable_all_queues(priv);
5818 case TC_SETUP_CLSU32:
5819 ret = stmmac_tc_setup_cls_u32(priv, priv, type_data);
5821 case TC_SETUP_CLSFLOWER:
5822 ret = stmmac_tc_setup_cls(priv, priv, type_data);
5828 stmmac_enable_all_queues(priv);
5832 static LIST_HEAD(stmmac_block_cb_list);
5834 static int stmmac_setup_tc(struct net_device *ndev, enum tc_setup_type type,
5837 struct stmmac_priv *priv = netdev_priv(ndev);
5840 case TC_SETUP_BLOCK:
5841 return flow_block_cb_setup_simple(type_data,
5842 &stmmac_block_cb_list,
5843 stmmac_setup_tc_block_cb,
5845 case TC_SETUP_QDISC_CBS:
5846 return stmmac_tc_setup_cbs(priv, priv, type_data);
5847 case TC_SETUP_QDISC_TAPRIO:
5848 return stmmac_tc_setup_taprio(priv, priv, type_data);
5849 case TC_SETUP_QDISC_ETF:
5850 return stmmac_tc_setup_etf(priv, priv, type_data);
5856 static u16 stmmac_select_queue(struct net_device *dev, struct sk_buff *skb,
5857 struct net_device *sb_dev)
5859 int gso = skb_shinfo(skb)->gso_type;
5861 if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6 | SKB_GSO_UDP_L4)) {
5863 * There is no way to determine the number of TSO/USO
5864 * capable Queues. Let's use always the Queue 0
5865 * because if TSO/USO is supported then at least this
5866 * one will be capable.
5871 return netdev_pick_tx(dev, skb, NULL) % dev->real_num_tx_queues;
5874 static int stmmac_set_mac_address(struct net_device *ndev, void *addr)
5876 struct stmmac_priv *priv = netdev_priv(ndev);
5879 ret = pm_runtime_get_sync(priv->device);
5881 pm_runtime_put_noidle(priv->device);
5885 ret = eth_mac_addr(ndev, addr);
5889 stmmac_set_umac_addr(priv, priv->hw, ndev->dev_addr, 0);
5892 pm_runtime_put(priv->device);
5897 #ifdef CONFIG_DEBUG_FS
5898 static struct dentry *stmmac_fs_dir;
5900 static void sysfs_display_ring(void *head, int size, int extend_desc,
5901 struct seq_file *seq, dma_addr_t dma_phy_addr)
5904 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
5905 struct dma_desc *p = (struct dma_desc *)head;
5906 dma_addr_t dma_addr;
5908 for (i = 0; i < size; i++) {
5910 dma_addr = dma_phy_addr + i * sizeof(*ep);
5911 seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n",
5913 le32_to_cpu(ep->basic.des0),
5914 le32_to_cpu(ep->basic.des1),
5915 le32_to_cpu(ep->basic.des2),
5916 le32_to_cpu(ep->basic.des3));
5919 dma_addr = dma_phy_addr + i * sizeof(*p);
5920 seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n",
5922 le32_to_cpu(p->des0), le32_to_cpu(p->des1),
5923 le32_to_cpu(p->des2), le32_to_cpu(p->des3));
5926 seq_printf(seq, "\n");
5930 static int stmmac_rings_status_show(struct seq_file *seq, void *v)
5932 struct net_device *dev = seq->private;
5933 struct stmmac_priv *priv = netdev_priv(dev);
5934 u32 rx_count = priv->plat->rx_queues_to_use;
5935 u32 tx_count = priv->plat->tx_queues_to_use;
5938 if ((dev->flags & IFF_UP) == 0)
5941 for (queue = 0; queue < rx_count; queue++) {
5942 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
5944 seq_printf(seq, "RX Queue %d:\n", queue);
5946 if (priv->extend_desc) {
5947 seq_printf(seq, "Extended descriptor ring:\n");
5948 sysfs_display_ring((void *)rx_q->dma_erx,
5949 priv->dma_rx_size, 1, seq, rx_q->dma_rx_phy);
5951 seq_printf(seq, "Descriptor ring:\n");
5952 sysfs_display_ring((void *)rx_q->dma_rx,
5953 priv->dma_rx_size, 0, seq, rx_q->dma_rx_phy);
5957 for (queue = 0; queue < tx_count; queue++) {
5958 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
5960 seq_printf(seq, "TX Queue %d:\n", queue);
5962 if (priv->extend_desc) {
5963 seq_printf(seq, "Extended descriptor ring:\n");
5964 sysfs_display_ring((void *)tx_q->dma_etx,
5965 priv->dma_tx_size, 1, seq, tx_q->dma_tx_phy);
5966 } else if (!(tx_q->tbs & STMMAC_TBS_AVAIL)) {
5967 seq_printf(seq, "Descriptor ring:\n");
5968 sysfs_display_ring((void *)tx_q->dma_tx,
5969 priv->dma_tx_size, 0, seq, tx_q->dma_tx_phy);
5975 DEFINE_SHOW_ATTRIBUTE(stmmac_rings_status);
5977 static int stmmac_dma_cap_show(struct seq_file *seq, void *v)
5979 struct net_device *dev = seq->private;
5980 struct stmmac_priv *priv = netdev_priv(dev);
5982 if (!priv->hw_cap_support) {
5983 seq_printf(seq, "DMA HW features not supported\n");
5987 seq_printf(seq, "==============================\n");
5988 seq_printf(seq, "\tDMA HW features\n");
5989 seq_printf(seq, "==============================\n");
5991 seq_printf(seq, "\t10/100 Mbps: %s\n",
5992 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
5993 seq_printf(seq, "\t1000 Mbps: %s\n",
5994 (priv->dma_cap.mbps_1000) ? "Y" : "N");
5995 seq_printf(seq, "\tHalf duplex: %s\n",
5996 (priv->dma_cap.half_duplex) ? "Y" : "N");
5997 seq_printf(seq, "\tHash Filter: %s\n",
5998 (priv->dma_cap.hash_filter) ? "Y" : "N");
5999 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
6000 (priv->dma_cap.multi_addr) ? "Y" : "N");
6001 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfaces): %s\n",
6002 (priv->dma_cap.pcs) ? "Y" : "N");
6003 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
6004 (priv->dma_cap.sma_mdio) ? "Y" : "N");
6005 seq_printf(seq, "\tPMT Remote wake up: %s\n",
6006 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
6007 seq_printf(seq, "\tPMT Magic Frame: %s\n",
6008 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
6009 seq_printf(seq, "\tRMON module: %s\n",
6010 (priv->dma_cap.rmon) ? "Y" : "N");
6011 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
6012 (priv->dma_cap.time_stamp) ? "Y" : "N");
6013 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp: %s\n",
6014 (priv->dma_cap.atime_stamp) ? "Y" : "N");
6015 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE): %s\n",
6016 (priv->dma_cap.eee) ? "Y" : "N");
6017 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
6018 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
6019 (priv->dma_cap.tx_coe) ? "Y" : "N");
6020 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
6021 seq_printf(seq, "\tIP Checksum Offload in RX: %s\n",
6022 (priv->dma_cap.rx_coe) ? "Y" : "N");
6024 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
6025 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
6026 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
6027 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
6029 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
6030 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
6031 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
6032 priv->dma_cap.number_rx_channel);
6033 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
6034 priv->dma_cap.number_tx_channel);
6035 seq_printf(seq, "\tNumber of Additional RX queues: %d\n",
6036 priv->dma_cap.number_rx_queues);
6037 seq_printf(seq, "\tNumber of Additional TX queues: %d\n",
6038 priv->dma_cap.number_tx_queues);
6039 seq_printf(seq, "\tEnhanced descriptors: %s\n",
6040 (priv->dma_cap.enh_desc) ? "Y" : "N");
6041 seq_printf(seq, "\tTX Fifo Size: %d\n", priv->dma_cap.tx_fifo_size);
6042 seq_printf(seq, "\tRX Fifo Size: %d\n", priv->dma_cap.rx_fifo_size);
6043 seq_printf(seq, "\tHash Table Size: %d\n", priv->dma_cap.hash_tb_sz);
6044 seq_printf(seq, "\tTSO: %s\n", priv->dma_cap.tsoen ? "Y" : "N");
6045 seq_printf(seq, "\tNumber of PPS Outputs: %d\n",
6046 priv->dma_cap.pps_out_num);
6047 seq_printf(seq, "\tSafety Features: %s\n",
6048 priv->dma_cap.asp ? "Y" : "N");
6049 seq_printf(seq, "\tFlexible RX Parser: %s\n",
6050 priv->dma_cap.frpsel ? "Y" : "N");
6051 seq_printf(seq, "\tEnhanced Addressing: %d\n",
6052 priv->dma_cap.addr64);
6053 seq_printf(seq, "\tReceive Side Scaling: %s\n",
6054 priv->dma_cap.rssen ? "Y" : "N");
6055 seq_printf(seq, "\tVLAN Hash Filtering: %s\n",
6056 priv->dma_cap.vlhash ? "Y" : "N");
6057 seq_printf(seq, "\tSplit Header: %s\n",
6058 priv->dma_cap.sphen ? "Y" : "N");
6059 seq_printf(seq, "\tVLAN TX Insertion: %s\n",
6060 priv->dma_cap.vlins ? "Y" : "N");
6061 seq_printf(seq, "\tDouble VLAN: %s\n",
6062 priv->dma_cap.dvlan ? "Y" : "N");
6063 seq_printf(seq, "\tNumber of L3/L4 Filters: %d\n",
6064 priv->dma_cap.l3l4fnum);
6065 seq_printf(seq, "\tARP Offloading: %s\n",
6066 priv->dma_cap.arpoffsel ? "Y" : "N");
6067 seq_printf(seq, "\tEnhancements to Scheduled Traffic (EST): %s\n",
6068 priv->dma_cap.estsel ? "Y" : "N");
6069 seq_printf(seq, "\tFrame Preemption (FPE): %s\n",
6070 priv->dma_cap.fpesel ? "Y" : "N");
6071 seq_printf(seq, "\tTime-Based Scheduling (TBS): %s\n",
6072 priv->dma_cap.tbssel ? "Y" : "N");
6075 DEFINE_SHOW_ATTRIBUTE(stmmac_dma_cap);
6077 /* Use network device events to rename debugfs file entries.
6079 static int stmmac_device_event(struct notifier_block *unused,
6080 unsigned long event, void *ptr)
6082 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6083 struct stmmac_priv *priv = netdev_priv(dev);
6085 if (dev->netdev_ops != &stmmac_netdev_ops)
6089 case NETDEV_CHANGENAME:
6090 if (priv->dbgfs_dir)
6091 priv->dbgfs_dir = debugfs_rename(stmmac_fs_dir,
6101 static struct notifier_block stmmac_notifier = {
6102 .notifier_call = stmmac_device_event,
6105 static void stmmac_init_fs(struct net_device *dev)
6107 struct stmmac_priv *priv = netdev_priv(dev);
6111 /* Create per netdev entries */
6112 priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
6114 /* Entry to report DMA RX/TX rings */
6115 debugfs_create_file("descriptors_status", 0444, priv->dbgfs_dir, dev,
6116 &stmmac_rings_status_fops);
6118 /* Entry to report the DMA HW features */
6119 debugfs_create_file("dma_cap", 0444, priv->dbgfs_dir, dev,
6120 &stmmac_dma_cap_fops);
6125 static void stmmac_exit_fs(struct net_device *dev)
6127 struct stmmac_priv *priv = netdev_priv(dev);
6129 debugfs_remove_recursive(priv->dbgfs_dir);
6131 #endif /* CONFIG_DEBUG_FS */
6133 static u32 stmmac_vid_crc32_le(__le16 vid_le)
6135 unsigned char *data = (unsigned char *)&vid_le;
6136 unsigned char data_byte = 0;
6141 bits = get_bitmask_order(VLAN_VID_MASK);
6142 for (i = 0; i < bits; i++) {
6144 data_byte = data[i / 8];
6146 temp = ((crc & 1) ^ data_byte) & 1;
6157 static int stmmac_vlan_update(struct stmmac_priv *priv, bool is_double)
6164 for_each_set_bit(vid, priv->active_vlans, VLAN_N_VID) {
6165 __le16 vid_le = cpu_to_le16(vid);
6166 crc = bitrev32(~stmmac_vid_crc32_le(vid_le)) >> 28;
6171 if (!priv->dma_cap.vlhash) {
6172 if (count > 2) /* VID = 0 always passes filter */
6175 pmatch = cpu_to_le16(vid);
6179 return stmmac_update_vlan_hash(priv, priv->hw, hash, pmatch, is_double);
6182 static int stmmac_vlan_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid)
6184 struct stmmac_priv *priv = netdev_priv(ndev);
6185 bool is_double = false;
6188 if (be16_to_cpu(proto) == ETH_P_8021AD)
6191 set_bit(vid, priv->active_vlans);
6192 ret = stmmac_vlan_update(priv, is_double);
6194 clear_bit(vid, priv->active_vlans);
6198 if (priv->hw->num_vlan) {
6199 ret = stmmac_add_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid);
6207 static int stmmac_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid)
6209 struct stmmac_priv *priv = netdev_priv(ndev);
6210 bool is_double = false;
6213 ret = pm_runtime_get_sync(priv->device);
6215 pm_runtime_put_noidle(priv->device);
6219 if (be16_to_cpu(proto) == ETH_P_8021AD)
6222 clear_bit(vid, priv->active_vlans);
6224 if (priv->hw->num_vlan) {
6225 ret = stmmac_del_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid);
6227 goto del_vlan_error;
6230 ret = stmmac_vlan_update(priv, is_double);
6233 pm_runtime_put(priv->device);
6238 static int stmmac_bpf(struct net_device *dev, struct netdev_bpf *bpf)
6240 struct stmmac_priv *priv = netdev_priv(dev);
6242 switch (bpf->command) {
6243 case XDP_SETUP_PROG:
6244 return stmmac_xdp_set_prog(priv, bpf->prog, bpf->extack);
6245 case XDP_SETUP_XSK_POOL:
6246 return stmmac_xdp_setup_pool(priv, bpf->xsk.pool,
6253 static int stmmac_xdp_xmit(struct net_device *dev, int num_frames,
6254 struct xdp_frame **frames, u32 flags)
6256 struct stmmac_priv *priv = netdev_priv(dev);
6257 int cpu = smp_processor_id();
6258 struct netdev_queue *nq;
6262 if (unlikely(test_bit(STMMAC_DOWN, &priv->state)))
6265 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
6268 queue = stmmac_xdp_get_tx_queue(priv, cpu);
6269 nq = netdev_get_tx_queue(priv->dev, queue);
6271 __netif_tx_lock(nq, cpu);
6272 /* Avoids TX time-out as we are sharing with slow path */
6273 nq->trans_start = jiffies;
6275 for (i = 0; i < num_frames; i++) {
6278 res = stmmac_xdp_xmit_xdpf(priv, queue, frames[i], true);
6279 if (res == STMMAC_XDP_CONSUMED)
6285 if (flags & XDP_XMIT_FLUSH) {
6286 stmmac_flush_tx_descriptors(priv, queue);
6287 stmmac_tx_timer_arm(priv, queue);
6290 __netif_tx_unlock(nq);
6295 void stmmac_disable_rx_queue(struct stmmac_priv *priv, u32 queue)
6297 struct stmmac_channel *ch = &priv->channel[queue];
6298 unsigned long flags;
6300 spin_lock_irqsave(&ch->lock, flags);
6301 stmmac_disable_dma_irq(priv, priv->ioaddr, queue, 1, 0);
6302 spin_unlock_irqrestore(&ch->lock, flags);
6304 stmmac_stop_rx_dma(priv, queue);
6305 __free_dma_rx_desc_resources(priv, queue);
6308 void stmmac_enable_rx_queue(struct stmmac_priv *priv, u32 queue)
6310 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
6311 struct stmmac_channel *ch = &priv->channel[queue];
6312 unsigned long flags;
6316 ret = __alloc_dma_rx_desc_resources(priv, queue);
6318 netdev_err(priv->dev, "Failed to alloc RX desc.\n");
6322 ret = __init_dma_rx_desc_rings(priv, queue, GFP_KERNEL);
6324 __free_dma_rx_desc_resources(priv, queue);
6325 netdev_err(priv->dev, "Failed to init RX desc.\n");
6329 stmmac_clear_rx_descriptors(priv, queue);
6331 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6332 rx_q->dma_rx_phy, rx_q->queue_index);
6334 rx_q->rx_tail_addr = rx_q->dma_rx_phy + (rx_q->buf_alloc_num *
6335 sizeof(struct dma_desc));
6336 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
6337 rx_q->rx_tail_addr, rx_q->queue_index);
6339 if (rx_q->xsk_pool && rx_q->buf_alloc_num) {
6340 buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool);
6341 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6345 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6350 stmmac_start_rx_dma(priv, queue);
6352 spin_lock_irqsave(&ch->lock, flags);
6353 stmmac_enable_dma_irq(priv, priv->ioaddr, queue, 1, 0);
6354 spin_unlock_irqrestore(&ch->lock, flags);
6357 void stmmac_disable_tx_queue(struct stmmac_priv *priv, u32 queue)
6359 struct stmmac_channel *ch = &priv->channel[queue];
6360 unsigned long flags;
6362 spin_lock_irqsave(&ch->lock, flags);
6363 stmmac_disable_dma_irq(priv, priv->ioaddr, queue, 0, 1);
6364 spin_unlock_irqrestore(&ch->lock, flags);
6366 stmmac_stop_tx_dma(priv, queue);
6367 __free_dma_tx_desc_resources(priv, queue);
6370 void stmmac_enable_tx_queue(struct stmmac_priv *priv, u32 queue)
6372 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
6373 struct stmmac_channel *ch = &priv->channel[queue];
6374 unsigned long flags;
6377 ret = __alloc_dma_tx_desc_resources(priv, queue);
6379 netdev_err(priv->dev, "Failed to alloc TX desc.\n");
6383 ret = __init_dma_tx_desc_rings(priv, queue);
6385 __free_dma_tx_desc_resources(priv, queue);
6386 netdev_err(priv->dev, "Failed to init TX desc.\n");
6390 stmmac_clear_tx_descriptors(priv, queue);
6392 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6393 tx_q->dma_tx_phy, tx_q->queue_index);
6395 if (tx_q->tbs & STMMAC_TBS_AVAIL)
6396 stmmac_enable_tbs(priv, priv->ioaddr, 1, tx_q->queue_index);
6398 tx_q->tx_tail_addr = tx_q->dma_tx_phy;
6399 stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
6400 tx_q->tx_tail_addr, tx_q->queue_index);
6402 stmmac_start_tx_dma(priv, queue);
6404 spin_lock_irqsave(&ch->lock, flags);
6405 stmmac_enable_dma_irq(priv, priv->ioaddr, queue, 0, 1);
6406 spin_unlock_irqrestore(&ch->lock, flags);
6409 int stmmac_xsk_wakeup(struct net_device *dev, u32 queue, u32 flags)
6411 struct stmmac_priv *priv = netdev_priv(dev);
6412 struct stmmac_rx_queue *rx_q;
6413 struct stmmac_tx_queue *tx_q;
6414 struct stmmac_channel *ch;
6416 if (test_bit(STMMAC_DOWN, &priv->state) ||
6417 !netif_carrier_ok(priv->dev))
6420 if (!stmmac_xdp_is_enabled(priv))
6423 if (queue >= priv->plat->rx_queues_to_use ||
6424 queue >= priv->plat->tx_queues_to_use)
6427 rx_q = &priv->rx_queue[queue];
6428 tx_q = &priv->tx_queue[queue];
6429 ch = &priv->channel[queue];
6431 if (!rx_q->xsk_pool && !tx_q->xsk_pool)
6434 if (!napi_if_scheduled_mark_missed(&ch->rxtx_napi)) {
6435 /* EQoS does not have per-DMA channel SW interrupt,
6436 * so we schedule RX Napi straight-away.
6438 if (likely(napi_schedule_prep(&ch->rxtx_napi)))
6439 __napi_schedule(&ch->rxtx_napi);
6445 static const struct net_device_ops stmmac_netdev_ops = {
6446 .ndo_open = stmmac_open,
6447 .ndo_start_xmit = stmmac_xmit,
6448 .ndo_stop = stmmac_release,
6449 .ndo_change_mtu = stmmac_change_mtu,
6450 .ndo_fix_features = stmmac_fix_features,
6451 .ndo_set_features = stmmac_set_features,
6452 .ndo_set_rx_mode = stmmac_set_rx_mode,
6453 .ndo_tx_timeout = stmmac_tx_timeout,
6454 .ndo_do_ioctl = stmmac_ioctl,
6455 .ndo_setup_tc = stmmac_setup_tc,
6456 .ndo_select_queue = stmmac_select_queue,
6457 #ifdef CONFIG_NET_POLL_CONTROLLER
6458 .ndo_poll_controller = stmmac_poll_controller,
6460 .ndo_set_mac_address = stmmac_set_mac_address,
6461 .ndo_vlan_rx_add_vid = stmmac_vlan_rx_add_vid,
6462 .ndo_vlan_rx_kill_vid = stmmac_vlan_rx_kill_vid,
6463 .ndo_bpf = stmmac_bpf,
6464 .ndo_xdp_xmit = stmmac_xdp_xmit,
6465 .ndo_xsk_wakeup = stmmac_xsk_wakeup,
6468 static void stmmac_reset_subtask(struct stmmac_priv *priv)
6470 if (!test_and_clear_bit(STMMAC_RESET_REQUESTED, &priv->state))
6472 if (test_bit(STMMAC_DOWN, &priv->state))
6475 netdev_err(priv->dev, "Reset adapter.\n");
6478 netif_trans_update(priv->dev);
6479 while (test_and_set_bit(STMMAC_RESETING, &priv->state))
6480 usleep_range(1000, 2000);
6482 set_bit(STMMAC_DOWN, &priv->state);
6483 dev_close(priv->dev);
6484 dev_open(priv->dev, NULL);
6485 clear_bit(STMMAC_DOWN, &priv->state);
6486 clear_bit(STMMAC_RESETING, &priv->state);
6490 static void stmmac_service_task(struct work_struct *work)
6492 struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
6495 stmmac_reset_subtask(priv);
6496 clear_bit(STMMAC_SERVICE_SCHED, &priv->state);
6500 * stmmac_hw_init - Init the MAC device
6501 * @priv: driver private structure
6502 * Description: this function is to configure the MAC device according to
6503 * some platform parameters or the HW capability register. It prepares the
6504 * driver to use either ring or chain modes and to setup either enhanced or
6505 * normal descriptors.
6507 static int stmmac_hw_init(struct stmmac_priv *priv)
6511 /* dwmac-sun8i only work in chain mode */
6512 if (priv->plat->has_sun8i)
6514 priv->chain_mode = chain_mode;
6516 /* Initialize HW Interface */
6517 ret = stmmac_hwif_init(priv);
6521 /* Get the HW capability (new GMAC newer than 3.50a) */
6522 priv->hw_cap_support = stmmac_get_hw_features(priv);
6523 if (priv->hw_cap_support) {
6524 dev_info(priv->device, "DMA HW capability register supported\n");
6526 /* We can override some gmac/dma configuration fields: e.g.
6527 * enh_desc, tx_coe (e.g. that are passed through the
6528 * platform) with the values from the HW capability
6529 * register (if supported).
6531 priv->plat->enh_desc = priv->dma_cap.enh_desc;
6532 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up &&
6533 !priv->plat->use_phy_wol;
6534 priv->hw->pmt = priv->plat->pmt;
6535 if (priv->dma_cap.hash_tb_sz) {
6536 priv->hw->multicast_filter_bins =
6537 (BIT(priv->dma_cap.hash_tb_sz) << 5);
6538 priv->hw->mcast_bits_log2 =
6539 ilog2(priv->hw->multicast_filter_bins);
6542 /* TXCOE doesn't work in thresh DMA mode */
6543 if (priv->plat->force_thresh_dma_mode)
6544 priv->plat->tx_coe = 0;
6546 priv->plat->tx_coe = priv->dma_cap.tx_coe;
6548 /* In case of GMAC4 rx_coe is from HW cap register. */
6549 priv->plat->rx_coe = priv->dma_cap.rx_coe;
6551 if (priv->dma_cap.rx_coe_type2)
6552 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
6553 else if (priv->dma_cap.rx_coe_type1)
6554 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
6557 dev_info(priv->device, "No HW DMA feature register supported\n");
6560 if (priv->plat->rx_coe) {
6561 priv->hw->rx_csum = priv->plat->rx_coe;
6562 dev_info(priv->device, "RX Checksum Offload Engine supported\n");
6563 if (priv->synopsys_id < DWMAC_CORE_4_00)
6564 dev_info(priv->device, "COE Type %d\n", priv->hw->rx_csum);
6566 if (priv->plat->tx_coe)
6567 dev_info(priv->device, "TX Checksum insertion supported\n");
6569 if (priv->plat->pmt) {
6570 dev_info(priv->device, "Wake-Up On Lan supported\n");
6571 device_set_wakeup_capable(priv->device, 1);
6574 if (priv->dma_cap.tsoen)
6575 dev_info(priv->device, "TSO supported\n");
6577 priv->hw->vlan_fail_q_en = priv->plat->vlan_fail_q_en;
6578 priv->hw->vlan_fail_q = priv->plat->vlan_fail_q;
6580 /* Run HW quirks, if any */
6581 if (priv->hwif_quirks) {
6582 ret = priv->hwif_quirks(priv);
6587 /* Rx Watchdog is available in the COREs newer than the 3.40.
6588 * In some case, for example on bugged HW this feature
6589 * has to be disable and this can be done by passing the
6590 * riwt_off field from the platform.
6592 if (((priv->synopsys_id >= DWMAC_CORE_3_50) ||
6593 (priv->plat->has_xgmac)) && (!priv->plat->riwt_off)) {
6595 dev_info(priv->device,
6596 "Enable RX Mitigation via HW Watchdog Timer\n");
6602 static void stmmac_napi_add(struct net_device *dev)
6604 struct stmmac_priv *priv = netdev_priv(dev);
6607 maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
6609 for (queue = 0; queue < maxq; queue++) {
6610 struct stmmac_channel *ch = &priv->channel[queue];
6612 ch->priv_data = priv;
6614 spin_lock_init(&ch->lock);
6616 if (queue < priv->plat->rx_queues_to_use) {
6617 netif_napi_add(dev, &ch->rx_napi, stmmac_napi_poll_rx,
6620 if (queue < priv->plat->tx_queues_to_use) {
6621 netif_tx_napi_add(dev, &ch->tx_napi,
6622 stmmac_napi_poll_tx,
6625 if (queue < priv->plat->rx_queues_to_use &&
6626 queue < priv->plat->tx_queues_to_use) {
6627 netif_napi_add(dev, &ch->rxtx_napi,
6628 stmmac_napi_poll_rxtx,
6634 static void stmmac_napi_del(struct net_device *dev)
6636 struct stmmac_priv *priv = netdev_priv(dev);
6639 maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
6641 for (queue = 0; queue < maxq; queue++) {
6642 struct stmmac_channel *ch = &priv->channel[queue];
6644 if (queue < priv->plat->rx_queues_to_use)
6645 netif_napi_del(&ch->rx_napi);
6646 if (queue < priv->plat->tx_queues_to_use)
6647 netif_napi_del(&ch->tx_napi);
6648 if (queue < priv->plat->rx_queues_to_use &&
6649 queue < priv->plat->tx_queues_to_use) {
6650 netif_napi_del(&ch->rxtx_napi);
6655 int stmmac_reinit_queues(struct net_device *dev, u32 rx_cnt, u32 tx_cnt)
6657 struct stmmac_priv *priv = netdev_priv(dev);
6660 if (netif_running(dev))
6661 stmmac_release(dev);
6663 stmmac_napi_del(dev);
6665 priv->plat->rx_queues_to_use = rx_cnt;
6666 priv->plat->tx_queues_to_use = tx_cnt;
6668 stmmac_napi_add(dev);
6670 if (netif_running(dev))
6671 ret = stmmac_open(dev);
6676 int stmmac_reinit_ringparam(struct net_device *dev, u32 rx_size, u32 tx_size)
6678 struct stmmac_priv *priv = netdev_priv(dev);
6681 if (netif_running(dev))
6682 stmmac_release(dev);
6684 priv->dma_rx_size = rx_size;
6685 priv->dma_tx_size = tx_size;
6687 if (netif_running(dev))
6688 ret = stmmac_open(dev);
6693 #define SEND_VERIFY_MPAKCET_FMT "Send Verify mPacket lo_state=%d lp_state=%d\n"
6694 static void stmmac_fpe_lp_task(struct work_struct *work)
6696 struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
6698 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
6699 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
6700 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
6701 bool *hs_enable = &fpe_cfg->hs_enable;
6702 bool *enable = &fpe_cfg->enable;
6705 while (retries-- > 0) {
6706 /* Bail out immediately if FPE handshake is OFF */
6707 if (*lo_state == FPE_STATE_OFF || !*hs_enable)
6710 if (*lo_state == FPE_STATE_ENTERING_ON &&
6711 *lp_state == FPE_STATE_ENTERING_ON) {
6712 stmmac_fpe_configure(priv, priv->ioaddr,
6713 priv->plat->tx_queues_to_use,
6714 priv->plat->rx_queues_to_use,
6717 netdev_info(priv->dev, "configured FPE\n");
6719 *lo_state = FPE_STATE_ON;
6720 *lp_state = FPE_STATE_ON;
6721 netdev_info(priv->dev, "!!! BOTH FPE stations ON\n");
6725 if ((*lo_state == FPE_STATE_CAPABLE ||
6726 *lo_state == FPE_STATE_ENTERING_ON) &&
6727 *lp_state != FPE_STATE_ON) {
6728 netdev_info(priv->dev, SEND_VERIFY_MPAKCET_FMT,
6729 *lo_state, *lp_state);
6730 stmmac_fpe_send_mpacket(priv, priv->ioaddr,
6733 /* Sleep then retry */
6737 clear_bit(__FPE_TASK_SCHED, &priv->fpe_task_state);
6740 void stmmac_fpe_handshake(struct stmmac_priv *priv, bool enable)
6742 if (priv->plat->fpe_cfg->hs_enable != enable) {
6744 stmmac_fpe_send_mpacket(priv, priv->ioaddr,
6747 priv->plat->fpe_cfg->lo_fpe_state = FPE_STATE_OFF;
6748 priv->plat->fpe_cfg->lp_fpe_state = FPE_STATE_OFF;
6751 priv->plat->fpe_cfg->hs_enable = enable;
6757 * @device: device pointer
6758 * @plat_dat: platform data pointer
6759 * @res: stmmac resource pointer
6760 * Description: this is the main probe function used to
6761 * call the alloc_etherdev, allocate the priv structure.
6763 * returns 0 on success, otherwise errno.
6765 int stmmac_dvr_probe(struct device *device,
6766 struct plat_stmmacenet_data *plat_dat,
6767 struct stmmac_resources *res)
6769 struct net_device *ndev = NULL;
6770 struct stmmac_priv *priv;
6774 ndev = devm_alloc_etherdev_mqs(device, sizeof(struct stmmac_priv),
6775 MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES);
6779 SET_NETDEV_DEV(ndev, device);
6781 priv = netdev_priv(ndev);
6782 priv->device = device;
6785 stmmac_set_ethtool_ops(ndev);
6786 priv->pause = pause;
6787 priv->plat = plat_dat;
6788 priv->ioaddr = res->addr;
6789 priv->dev->base_addr = (unsigned long)res->addr;
6790 priv->plat->dma_cfg->multi_msi_en = priv->plat->multi_msi_en;
6792 priv->dev->irq = res->irq;
6793 priv->wol_irq = res->wol_irq;
6794 priv->lpi_irq = res->lpi_irq;
6795 priv->sfty_ce_irq = res->sfty_ce_irq;
6796 priv->sfty_ue_irq = res->sfty_ue_irq;
6797 for (i = 0; i < MTL_MAX_RX_QUEUES; i++)
6798 priv->rx_irq[i] = res->rx_irq[i];
6799 for (i = 0; i < MTL_MAX_TX_QUEUES; i++)
6800 priv->tx_irq[i] = res->tx_irq[i];
6802 if (!is_zero_ether_addr(res->mac))
6803 memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
6805 dev_set_drvdata(device, priv->dev);
6807 /* Verify driver arguments */
6808 stmmac_verify_args();
6810 priv->af_xdp_zc_qps = bitmap_zalloc(MTL_MAX_TX_QUEUES, GFP_KERNEL);
6811 if (!priv->af_xdp_zc_qps)
6814 /* Allocate workqueue */
6815 priv->wq = create_singlethread_workqueue("stmmac_wq");
6817 dev_err(priv->device, "failed to create workqueue\n");
6821 INIT_WORK(&priv->service_task, stmmac_service_task);
6823 /* Initialize Link Partner FPE workqueue */
6824 INIT_WORK(&priv->fpe_task, stmmac_fpe_lp_task);
6826 /* Override with kernel parameters if supplied XXX CRS XXX
6827 * this needs to have multiple instances
6829 if ((phyaddr >= 0) && (phyaddr <= 31))
6830 priv->plat->phy_addr = phyaddr;
6832 if (priv->plat->stmmac_rst) {
6833 ret = reset_control_assert(priv->plat->stmmac_rst);
6834 reset_control_deassert(priv->plat->stmmac_rst);
6835 /* Some reset controllers have only reset callback instead of
6836 * assert + deassert callbacks pair.
6838 if (ret == -ENOTSUPP)
6839 reset_control_reset(priv->plat->stmmac_rst);
6842 ret = reset_control_deassert(priv->plat->stmmac_ahb_rst);
6843 if (ret == -ENOTSUPP)
6844 dev_err(priv->device, "unable to bring out of ahb reset: %pe\n",
6847 /* Init MAC and get the capabilities */
6848 ret = stmmac_hw_init(priv);
6852 /* Only DWMAC core version 5.20 onwards supports HW descriptor prefetch.
6854 if (priv->synopsys_id < DWMAC_CORE_5_20)
6855 priv->plat->dma_cfg->dche = false;
6857 stmmac_check_ether_addr(priv);
6859 ndev->netdev_ops = &stmmac_netdev_ops;
6861 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
6864 ret = stmmac_tc_init(priv, priv);
6866 ndev->hw_features |= NETIF_F_HW_TC;
6869 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
6870 ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
6871 if (priv->plat->has_gmac4)
6872 ndev->hw_features |= NETIF_F_GSO_UDP_L4;
6874 dev_info(priv->device, "TSO feature enabled\n");
6877 if (priv->dma_cap.sphen) {
6878 ndev->hw_features |= NETIF_F_GRO;
6879 priv->sph_cap = true;
6880 priv->sph = priv->sph_cap;
6881 dev_info(priv->device, "SPH feature enabled\n");
6884 /* The current IP register MAC_HW_Feature1[ADDR64] only define
6885 * 32/40/64 bit width, but some SOC support others like i.MX8MP
6886 * support 34 bits but it map to 40 bits width in MAC_HW_Feature1[ADDR64].
6887 * So overwrite dma_cap.addr64 according to HW real design.
6889 if (priv->plat->addr64)
6890 priv->dma_cap.addr64 = priv->plat->addr64;
6892 if (priv->dma_cap.addr64) {
6893 ret = dma_set_mask_and_coherent(device,
6894 DMA_BIT_MASK(priv->dma_cap.addr64));
6896 dev_info(priv->device, "Using %d bits DMA width\n",
6897 priv->dma_cap.addr64);
6900 * If more than 32 bits can be addressed, make sure to
6901 * enable enhanced addressing mode.
6903 if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
6904 priv->plat->dma_cfg->eame = true;
6906 ret = dma_set_mask_and_coherent(device, DMA_BIT_MASK(32));
6908 dev_err(priv->device, "Failed to set DMA Mask\n");
6912 priv->dma_cap.addr64 = 32;
6916 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
6917 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
6918 #ifdef STMMAC_VLAN_TAG_USED
6919 /* Both mac100 and gmac support receive VLAN tag detection */
6920 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX;
6921 if (priv->dma_cap.vlhash) {
6922 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
6923 ndev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
6925 if (priv->dma_cap.vlins) {
6926 ndev->features |= NETIF_F_HW_VLAN_CTAG_TX;
6927 if (priv->dma_cap.dvlan)
6928 ndev->features |= NETIF_F_HW_VLAN_STAG_TX;
6931 priv->msg_enable = netif_msg_init(debug, default_msg_level);
6933 /* Initialize RSS */
6934 rxq = priv->plat->rx_queues_to_use;
6935 netdev_rss_key_fill(priv->rss.key, sizeof(priv->rss.key));
6936 for (i = 0; i < ARRAY_SIZE(priv->rss.table); i++)
6937 priv->rss.table[i] = ethtool_rxfh_indir_default(i, rxq);
6939 if (priv->dma_cap.rssen && priv->plat->rss_en)
6940 ndev->features |= NETIF_F_RXHASH;
6942 /* MTU range: 46 - hw-specific max */
6943 ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
6944 if (priv->plat->has_xgmac)
6945 ndev->max_mtu = XGMAC_JUMBO_LEN;
6946 else if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
6947 ndev->max_mtu = JUMBO_LEN;
6949 ndev->max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
6950 /* Will not overwrite ndev->max_mtu if plat->maxmtu > ndev->max_mtu
6951 * as well as plat->maxmtu < ndev->min_mtu which is a invalid range.
6953 if ((priv->plat->maxmtu < ndev->max_mtu) &&
6954 (priv->plat->maxmtu >= ndev->min_mtu))
6955 ndev->max_mtu = priv->plat->maxmtu;
6956 else if (priv->plat->maxmtu < ndev->min_mtu)
6957 dev_warn(priv->device,
6958 "%s: warning: maxmtu having invalid value (%d)\n",
6959 __func__, priv->plat->maxmtu);
6962 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
6964 /* Setup channels NAPI */
6965 stmmac_napi_add(ndev);
6967 mutex_init(&priv->lock);
6969 /* If a specific clk_csr value is passed from the platform
6970 * this means that the CSR Clock Range selection cannot be
6971 * changed at run-time and it is fixed. Viceversa the driver'll try to
6972 * set the MDC clock dynamically according to the csr actual
6975 if (priv->plat->clk_csr >= 0)
6976 priv->clk_csr = priv->plat->clk_csr;
6978 stmmac_clk_csr_set(priv);
6980 stmmac_check_pcs_mode(priv);
6982 pm_runtime_get_noresume(device);
6983 pm_runtime_set_active(device);
6984 pm_runtime_enable(device);
6986 if (priv->hw->pcs != STMMAC_PCS_TBI &&
6987 priv->hw->pcs != STMMAC_PCS_RTBI) {
6988 /* MDIO bus Registration */
6989 ret = stmmac_mdio_register(ndev);
6991 dev_err(priv->device,
6992 "%s: MDIO bus (id: %d) registration failed",
6993 __func__, priv->plat->bus_id);
6994 goto error_mdio_register;
6998 if (priv->plat->speed_mode_2500)
6999 priv->plat->speed_mode_2500(ndev, priv->plat->bsp_priv);
7001 if (priv->plat->mdio_bus_data && priv->plat->mdio_bus_data->has_xpcs) {
7002 ret = stmmac_xpcs_setup(priv->mii);
7004 goto error_xpcs_setup;
7007 ret = stmmac_phy_setup(priv);
7009 netdev_err(ndev, "failed to setup phy (%d)\n", ret);
7010 goto error_phy_setup;
7013 ret = register_netdev(ndev);
7015 dev_err(priv->device, "%s: ERROR %i registering the device\n",
7017 goto error_netdev_register;
7020 if (priv->plat->serdes_powerup) {
7021 ret = priv->plat->serdes_powerup(ndev,
7022 priv->plat->bsp_priv);
7025 goto error_serdes_powerup;
7028 #ifdef CONFIG_DEBUG_FS
7029 stmmac_init_fs(ndev);
7032 /* Let pm_runtime_put() disable the clocks.
7033 * If CONFIG_PM is not enabled, the clocks will stay powered.
7035 pm_runtime_put(device);
7039 error_serdes_powerup:
7040 unregister_netdev(ndev);
7041 error_netdev_register:
7042 phylink_destroy(priv->phylink);
7045 if (priv->hw->pcs != STMMAC_PCS_TBI &&
7046 priv->hw->pcs != STMMAC_PCS_RTBI)
7047 stmmac_mdio_unregister(ndev);
7048 error_mdio_register:
7049 stmmac_napi_del(ndev);
7051 destroy_workqueue(priv->wq);
7052 bitmap_free(priv->af_xdp_zc_qps);
7056 EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
7060 * @dev: device pointer
7061 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
7062 * changes the link status, releases the DMA descriptor rings.
7064 int stmmac_dvr_remove(struct device *dev)
7066 struct net_device *ndev = dev_get_drvdata(dev);
7067 struct stmmac_priv *priv = netdev_priv(ndev);
7069 netdev_info(priv->dev, "%s: removing driver", __func__);
7071 stmmac_stop_all_dma(priv);
7072 stmmac_mac_set(priv, priv->ioaddr, false);
7073 netif_carrier_off(ndev);
7074 unregister_netdev(ndev);
7076 /* Serdes power down needs to happen after VLAN filter
7077 * is deleted that is triggered by unregister_netdev().
7079 if (priv->plat->serdes_powerdown)
7080 priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv);
7082 #ifdef CONFIG_DEBUG_FS
7083 stmmac_exit_fs(ndev);
7085 phylink_destroy(priv->phylink);
7086 if (priv->plat->stmmac_rst)
7087 reset_control_assert(priv->plat->stmmac_rst);
7088 reset_control_assert(priv->plat->stmmac_ahb_rst);
7089 pm_runtime_put(dev);
7090 pm_runtime_disable(dev);
7091 if (priv->hw->pcs != STMMAC_PCS_TBI &&
7092 priv->hw->pcs != STMMAC_PCS_RTBI)
7093 stmmac_mdio_unregister(ndev);
7094 destroy_workqueue(priv->wq);
7095 mutex_destroy(&priv->lock);
7096 bitmap_free(priv->af_xdp_zc_qps);
7100 EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
7103 * stmmac_suspend - suspend callback
7104 * @dev: device pointer
7105 * Description: this is the function to suspend the device and it is called
7106 * by the platform driver to stop the network queue, release the resources,
7107 * program the PMT register (for WoL), clean and release driver resources.
7109 int stmmac_suspend(struct device *dev)
7111 struct net_device *ndev = dev_get_drvdata(dev);
7112 struct stmmac_priv *priv = netdev_priv(ndev);
7116 if (!ndev || !netif_running(ndev))
7119 phylink_mac_change(priv->phylink, false);
7121 mutex_lock(&priv->lock);
7123 netif_device_detach(ndev);
7125 stmmac_disable_all_queues(priv);
7127 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
7128 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
7130 if (priv->eee_enabled) {
7131 priv->tx_path_in_lpi_mode = false;
7132 del_timer_sync(&priv->eee_ctrl_timer);
7135 /* Stop TX/RX DMA */
7136 stmmac_stop_all_dma(priv);
7138 if (priv->plat->serdes_powerdown)
7139 priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv);
7141 /* Enable Power down mode by programming the PMT regs */
7142 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7143 stmmac_pmt(priv, priv->hw, priv->wolopts);
7146 mutex_unlock(&priv->lock);
7148 if (device_may_wakeup(priv->device))
7149 phylink_speed_down(priv->phylink, false);
7150 phylink_stop(priv->phylink);
7152 mutex_lock(&priv->lock);
7154 stmmac_mac_set(priv, priv->ioaddr, false);
7155 pinctrl_pm_select_sleep_state(priv->device);
7156 /* Disable clock in case of PWM is off */
7157 clk_disable_unprepare(priv->plat->clk_ptp_ref);
7158 ret = pm_runtime_force_suspend(dev);
7160 mutex_unlock(&priv->lock);
7165 mutex_unlock(&priv->lock);
7167 if (priv->dma_cap.fpesel) {
7169 stmmac_fpe_configure(priv, priv->ioaddr,
7170 priv->plat->tx_queues_to_use,
7171 priv->plat->rx_queues_to_use, false);
7173 stmmac_fpe_handshake(priv, false);
7176 priv->speed = SPEED_UNKNOWN;
7179 EXPORT_SYMBOL_GPL(stmmac_suspend);
7182 * stmmac_reset_queues_param - reset queue parameters
7183 * @priv: device pointer
7185 static void stmmac_reset_queues_param(struct stmmac_priv *priv)
7187 u32 rx_cnt = priv->plat->rx_queues_to_use;
7188 u32 tx_cnt = priv->plat->tx_queues_to_use;
7191 for (queue = 0; queue < rx_cnt; queue++) {
7192 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
7198 for (queue = 0; queue < tx_cnt; queue++) {
7199 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
7205 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
7210 * stmmac_resume - resume callback
7211 * @dev: device pointer
7212 * Description: when resume this function is invoked to setup the DMA and CORE
7213 * in a usable state.
7215 int stmmac_resume(struct device *dev)
7217 struct net_device *ndev = dev_get_drvdata(dev);
7218 struct stmmac_priv *priv = netdev_priv(ndev);
7221 if (!netif_running(ndev))
7224 /* Power Down bit, into the PM register, is cleared
7225 * automatically as soon as a magic packet or a Wake-up frame
7226 * is received. Anyway, it's better to manually clear
7227 * this bit because it can generate problems while resuming
7228 * from another devices (e.g. serial console).
7230 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7231 mutex_lock(&priv->lock);
7232 stmmac_pmt(priv, priv->hw, 0);
7233 mutex_unlock(&priv->lock);
7236 pinctrl_pm_select_default_state(priv->device);
7237 /* enable the clk previously disabled */
7238 ret = pm_runtime_force_resume(dev);
7241 if (priv->plat->clk_ptp_ref)
7242 clk_prepare_enable(priv->plat->clk_ptp_ref);
7243 /* reset the phy so that it's ready */
7245 stmmac_mdio_reset(priv->mii);
7248 if (priv->plat->serdes_powerup) {
7249 ret = priv->plat->serdes_powerup(ndev,
7250 priv->plat->bsp_priv);
7256 if (!device_may_wakeup(priv->device) || !priv->plat->pmt) {
7258 phylink_start(priv->phylink);
7259 /* We may have called phylink_speed_down before */
7260 phylink_speed_up(priv->phylink);
7265 mutex_lock(&priv->lock);
7267 stmmac_reset_queues_param(priv);
7269 stmmac_free_tx_skbufs(priv);
7270 stmmac_clear_descriptors(priv);
7272 stmmac_hw_setup(ndev, false);
7273 stmmac_init_coalesce(priv);
7274 stmmac_set_rx_mode(ndev);
7276 stmmac_restore_hw_vlan_rx_fltr(priv, ndev, priv->hw);
7278 stmmac_enable_all_queues(priv);
7280 mutex_unlock(&priv->lock);
7283 phylink_mac_change(priv->phylink, true);
7285 netif_device_attach(ndev);
7289 EXPORT_SYMBOL_GPL(stmmac_resume);
7292 static int __init stmmac_cmdline_opt(char *str)
7298 while ((opt = strsep(&str, ",")) != NULL) {
7299 if (!strncmp(opt, "debug:", 6)) {
7300 if (kstrtoint(opt + 6, 0, &debug))
7302 } else if (!strncmp(opt, "phyaddr:", 8)) {
7303 if (kstrtoint(opt + 8, 0, &phyaddr))
7305 } else if (!strncmp(opt, "buf_sz:", 7)) {
7306 if (kstrtoint(opt + 7, 0, &buf_sz))
7308 } else if (!strncmp(opt, "tc:", 3)) {
7309 if (kstrtoint(opt + 3, 0, &tc))
7311 } else if (!strncmp(opt, "watchdog:", 9)) {
7312 if (kstrtoint(opt + 9, 0, &watchdog))
7314 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
7315 if (kstrtoint(opt + 10, 0, &flow_ctrl))
7317 } else if (!strncmp(opt, "pause:", 6)) {
7318 if (kstrtoint(opt + 6, 0, &pause))
7320 } else if (!strncmp(opt, "eee_timer:", 10)) {
7321 if (kstrtoint(opt + 10, 0, &eee_timer))
7323 } else if (!strncmp(opt, "chain_mode:", 11)) {
7324 if (kstrtoint(opt + 11, 0, &chain_mode))
7331 pr_err("%s: ERROR broken module parameter conversion", __func__);
7335 __setup("stmmaceth=", stmmac_cmdline_opt);
7338 static int __init stmmac_init(void)
7340 #ifdef CONFIG_DEBUG_FS
7341 /* Create debugfs main directory if it doesn't exist yet */
7343 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
7344 register_netdevice_notifier(&stmmac_notifier);
7350 static void __exit stmmac_exit(void)
7352 #ifdef CONFIG_DEBUG_FS
7353 unregister_netdevice_notifier(&stmmac_notifier);
7354 debugfs_remove_recursive(stmmac_fs_dir);
7358 module_init(stmmac_init)
7359 module_exit(stmmac_exit)
7361 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
7362 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
7363 MODULE_LICENSE("GPL");