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 /* As long as the interface is active, we keep the timestamping counter enabled
54 * with fine resolution and binary rollover. This avoid non-monotonic behavior
55 * (clock jumps) when changing timestamping settings at runtime.
57 #define STMMAC_HWTS_ACTIVE (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | \
60 #define STMMAC_ALIGN(x) ALIGN(ALIGN(x, SMP_CACHE_BYTES), 16)
61 #define TSO_MAX_BUFF_SIZE (SZ_16K - 1)
63 /* Module parameters */
65 static int watchdog = TX_TIMEO;
66 module_param(watchdog, int, 0644);
67 MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
69 static int debug = -1;
70 module_param(debug, int, 0644);
71 MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
73 static int phyaddr = -1;
74 module_param(phyaddr, int, 0444);
75 MODULE_PARM_DESC(phyaddr, "Physical device address");
77 #define STMMAC_TX_THRESH(x) ((x)->dma_tx_size / 4)
78 #define STMMAC_RX_THRESH(x) ((x)->dma_rx_size / 4)
80 /* Limit to make sure XDP TX and slow path can coexist */
81 #define STMMAC_XSK_TX_BUDGET_MAX 256
82 #define STMMAC_TX_XSK_AVAIL 16
83 #define STMMAC_RX_FILL_BATCH 16
85 #define STMMAC_XDP_PASS 0
86 #define STMMAC_XDP_CONSUMED BIT(0)
87 #define STMMAC_XDP_TX BIT(1)
88 #define STMMAC_XDP_REDIRECT BIT(2)
90 static int flow_ctrl = FLOW_AUTO;
91 module_param(flow_ctrl, int, 0644);
92 MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
94 static int pause = PAUSE_TIME;
95 module_param(pause, int, 0644);
96 MODULE_PARM_DESC(pause, "Flow Control Pause Time");
99 static int tc = TC_DEFAULT;
100 module_param(tc, int, 0644);
101 MODULE_PARM_DESC(tc, "DMA threshold control value");
103 #define DEFAULT_BUFSIZE 1536
104 static int buf_sz = DEFAULT_BUFSIZE;
105 module_param(buf_sz, int, 0644);
106 MODULE_PARM_DESC(buf_sz, "DMA buffer size");
108 #define STMMAC_RX_COPYBREAK 256
110 static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
111 NETIF_MSG_LINK | NETIF_MSG_IFUP |
112 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
114 #define STMMAC_DEFAULT_LPI_TIMER 1000
115 static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
116 module_param(eee_timer, int, 0644);
117 MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
118 #define STMMAC_LPI_T(x) (jiffies + usecs_to_jiffies(x))
120 /* By default the driver will use the ring mode to manage tx and rx descriptors,
121 * but allow user to force to use the chain instead of the ring
123 static unsigned int chain_mode;
124 module_param(chain_mode, int, 0444);
125 MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
127 static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
128 /* For MSI interrupts handling */
129 static irqreturn_t stmmac_mac_interrupt(int irq, void *dev_id);
130 static irqreturn_t stmmac_safety_interrupt(int irq, void *dev_id);
131 static irqreturn_t stmmac_msi_intr_tx(int irq, void *data);
132 static irqreturn_t stmmac_msi_intr_rx(int irq, void *data);
133 static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue);
134 static void stmmac_flush_tx_descriptors(struct stmmac_priv *priv, int queue);
135 static void stmmac_set_dma_operation_mode(struct stmmac_priv *priv, u32 txmode,
136 u32 rxmode, u32 chan);
138 #ifdef CONFIG_DEBUG_FS
139 static const struct net_device_ops stmmac_netdev_ops;
140 static void stmmac_init_fs(struct net_device *dev);
141 static void stmmac_exit_fs(struct net_device *dev);
144 #define STMMAC_COAL_TIMER(x) (ns_to_ktime((x) * NSEC_PER_USEC))
146 int stmmac_bus_clks_config(struct stmmac_priv *priv, bool enabled)
151 ret = clk_prepare_enable(priv->plat->stmmac_clk);
154 ret = clk_prepare_enable(priv->plat->pclk);
156 clk_disable_unprepare(priv->plat->stmmac_clk);
159 if (priv->plat->clks_config) {
160 ret = priv->plat->clks_config(priv->plat->bsp_priv, enabled);
162 clk_disable_unprepare(priv->plat->stmmac_clk);
163 clk_disable_unprepare(priv->plat->pclk);
168 clk_disable_unprepare(priv->plat->stmmac_clk);
169 clk_disable_unprepare(priv->plat->pclk);
170 if (priv->plat->clks_config)
171 priv->plat->clks_config(priv->plat->bsp_priv, enabled);
176 EXPORT_SYMBOL_GPL(stmmac_bus_clks_config);
179 * stmmac_verify_args - verify the driver parameters.
180 * Description: it checks the driver parameters and set a default in case of
183 static void stmmac_verify_args(void)
185 if (unlikely(watchdog < 0))
187 if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
188 buf_sz = DEFAULT_BUFSIZE;
189 if (unlikely(flow_ctrl > 1))
190 flow_ctrl = FLOW_AUTO;
191 else if (likely(flow_ctrl < 0))
192 flow_ctrl = FLOW_OFF;
193 if (unlikely((pause < 0) || (pause > 0xffff)))
196 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
199 static void __stmmac_disable_all_queues(struct stmmac_priv *priv)
201 u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
202 u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
203 u32 maxq = max(rx_queues_cnt, tx_queues_cnt);
206 for (queue = 0; queue < maxq; queue++) {
207 struct stmmac_channel *ch = &priv->channel[queue];
209 if (stmmac_xdp_is_enabled(priv) &&
210 test_bit(queue, priv->af_xdp_zc_qps)) {
211 napi_disable(&ch->rxtx_napi);
215 if (queue < rx_queues_cnt)
216 napi_disable(&ch->rx_napi);
217 if (queue < tx_queues_cnt)
218 napi_disable(&ch->tx_napi);
223 * stmmac_disable_all_queues - Disable all queues
224 * @priv: driver private structure
226 static void stmmac_disable_all_queues(struct stmmac_priv *priv)
228 u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
229 struct stmmac_rx_queue *rx_q;
232 /* synchronize_rcu() needed for pending XDP buffers to drain */
233 for (queue = 0; queue < rx_queues_cnt; queue++) {
234 rx_q = &priv->rx_queue[queue];
235 if (rx_q->xsk_pool) {
241 __stmmac_disable_all_queues(priv);
245 * stmmac_enable_all_queues - Enable all queues
246 * @priv: driver private structure
248 static void stmmac_enable_all_queues(struct stmmac_priv *priv)
250 u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
251 u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
252 u32 maxq = max(rx_queues_cnt, tx_queues_cnt);
255 for (queue = 0; queue < maxq; queue++) {
256 struct stmmac_channel *ch = &priv->channel[queue];
258 if (stmmac_xdp_is_enabled(priv) &&
259 test_bit(queue, priv->af_xdp_zc_qps)) {
260 napi_enable(&ch->rxtx_napi);
264 if (queue < rx_queues_cnt)
265 napi_enable(&ch->rx_napi);
266 if (queue < tx_queues_cnt)
267 napi_enable(&ch->tx_napi);
271 static void stmmac_service_event_schedule(struct stmmac_priv *priv)
273 if (!test_bit(STMMAC_DOWN, &priv->state) &&
274 !test_and_set_bit(STMMAC_SERVICE_SCHED, &priv->state))
275 queue_work(priv->wq, &priv->service_task);
278 static void stmmac_global_err(struct stmmac_priv *priv)
280 netif_carrier_off(priv->dev);
281 set_bit(STMMAC_RESET_REQUESTED, &priv->state);
282 stmmac_service_event_schedule(priv);
286 * stmmac_clk_csr_set - dynamically set the MDC clock
287 * @priv: driver private structure
288 * Description: this is to dynamically set the MDC clock according to the csr
291 * If a specific clk_csr value is passed from the platform
292 * this means that the CSR Clock Range selection cannot be
293 * changed at run-time and it is fixed (as reported in the driver
294 * documentation). Viceversa the driver will try to set the MDC
295 * clock dynamically according to the actual clock input.
297 static void stmmac_clk_csr_set(struct stmmac_priv *priv)
301 clk_rate = clk_get_rate(priv->plat->stmmac_clk);
303 /* Platform provided default clk_csr would be assumed valid
304 * for all other cases except for the below mentioned ones.
305 * For values higher than the IEEE 802.3 specified frequency
306 * we can not estimate the proper divider as it is not known
307 * the frequency of clk_csr_i. So we do not change the default
310 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
311 if (clk_rate < CSR_F_35M)
312 priv->clk_csr = STMMAC_CSR_20_35M;
313 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
314 priv->clk_csr = STMMAC_CSR_35_60M;
315 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
316 priv->clk_csr = STMMAC_CSR_60_100M;
317 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
318 priv->clk_csr = STMMAC_CSR_100_150M;
319 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
320 priv->clk_csr = STMMAC_CSR_150_250M;
321 else if ((clk_rate >= CSR_F_250M) && (clk_rate <= CSR_F_300M))
322 priv->clk_csr = STMMAC_CSR_250_300M;
325 if (priv->plat->has_sun8i) {
326 if (clk_rate > 160000000)
327 priv->clk_csr = 0x03;
328 else if (clk_rate > 80000000)
329 priv->clk_csr = 0x02;
330 else if (clk_rate > 40000000)
331 priv->clk_csr = 0x01;
336 if (priv->plat->has_xgmac) {
337 if (clk_rate > 400000000)
339 else if (clk_rate > 350000000)
341 else if (clk_rate > 300000000)
343 else if (clk_rate > 250000000)
345 else if (clk_rate > 150000000)
352 static void print_pkt(unsigned char *buf, int len)
354 pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
355 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
358 static inline u32 stmmac_tx_avail(struct stmmac_priv *priv, u32 queue)
360 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
363 if (tx_q->dirty_tx > tx_q->cur_tx)
364 avail = tx_q->dirty_tx - tx_q->cur_tx - 1;
366 avail = priv->dma_tx_size - tx_q->cur_tx + tx_q->dirty_tx - 1;
372 * stmmac_rx_dirty - Get RX queue dirty
373 * @priv: driver private structure
374 * @queue: RX queue index
376 static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv, u32 queue)
378 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
381 if (rx_q->dirty_rx <= rx_q->cur_rx)
382 dirty = rx_q->cur_rx - rx_q->dirty_rx;
384 dirty = priv->dma_rx_size - rx_q->dirty_rx + rx_q->cur_rx;
389 static void stmmac_lpi_entry_timer_config(struct stmmac_priv *priv, bool en)
393 /* Clear/set the SW EEE timer flag based on LPI ET enablement */
394 priv->eee_sw_timer_en = en ? 0 : 1;
395 tx_lpi_timer = en ? priv->tx_lpi_timer : 0;
396 stmmac_set_eee_lpi_timer(priv, priv->hw, tx_lpi_timer);
400 * stmmac_enable_eee_mode - check and enter in LPI mode
401 * @priv: driver private structure
402 * Description: this function is to verify and enter in LPI mode in case of
405 static int stmmac_enable_eee_mode(struct stmmac_priv *priv)
407 u32 tx_cnt = priv->plat->tx_queues_to_use;
410 /* check if all TX queues have the work finished */
411 for (queue = 0; queue < tx_cnt; queue++) {
412 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
414 if (tx_q->dirty_tx != tx_q->cur_tx)
415 return -EBUSY; /* still unfinished work */
418 /* Check and enter in LPI mode */
419 if (!priv->tx_path_in_lpi_mode)
420 stmmac_set_eee_mode(priv, priv->hw,
421 priv->plat->en_tx_lpi_clockgating);
426 * stmmac_disable_eee_mode - disable and exit from LPI mode
427 * @priv: driver private structure
428 * Description: this function is to exit and disable EEE in case of
429 * LPI state is true. This is called by the xmit.
431 void stmmac_disable_eee_mode(struct stmmac_priv *priv)
433 if (!priv->eee_sw_timer_en) {
434 stmmac_lpi_entry_timer_config(priv, 0);
438 stmmac_reset_eee_mode(priv, priv->hw);
439 del_timer_sync(&priv->eee_ctrl_timer);
440 priv->tx_path_in_lpi_mode = false;
444 * stmmac_eee_ctrl_timer - EEE TX SW timer.
445 * @t: timer_list struct containing private info
447 * if there is no data transfer and if we are not in LPI state,
448 * then MAC Transmitter can be moved to LPI state.
450 static void stmmac_eee_ctrl_timer(struct timer_list *t)
452 struct stmmac_priv *priv = from_timer(priv, t, eee_ctrl_timer);
454 if (stmmac_enable_eee_mode(priv))
455 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer));
459 * stmmac_eee_init - init EEE
460 * @priv: driver private structure
462 * if the GMAC supports the EEE (from the HW cap reg) and the phy device
463 * can also manage EEE, this function enable the LPI state and start related
466 bool stmmac_eee_init(struct stmmac_priv *priv)
468 int eee_tw_timer = priv->eee_tw_timer;
470 /* Using PCS we cannot dial with the phy registers at this stage
471 * so we do not support extra feature like EEE.
473 if (priv->hw->pcs == STMMAC_PCS_TBI ||
474 priv->hw->pcs == STMMAC_PCS_RTBI)
477 /* Check if MAC core supports the EEE feature. */
478 if (!priv->dma_cap.eee)
481 mutex_lock(&priv->lock);
483 /* Check if it needs to be deactivated */
484 if (!priv->eee_active) {
485 if (priv->eee_enabled) {
486 netdev_dbg(priv->dev, "disable EEE\n");
487 stmmac_lpi_entry_timer_config(priv, 0);
488 del_timer_sync(&priv->eee_ctrl_timer);
489 stmmac_set_eee_timer(priv, priv->hw, 0, eee_tw_timer);
491 xpcs_config_eee(priv->hw->xpcs,
492 priv->plat->mult_fact_100ns,
495 mutex_unlock(&priv->lock);
499 if (priv->eee_active && !priv->eee_enabled) {
500 timer_setup(&priv->eee_ctrl_timer, stmmac_eee_ctrl_timer, 0);
501 stmmac_set_eee_timer(priv, priv->hw, STMMAC_DEFAULT_LIT_LS,
504 xpcs_config_eee(priv->hw->xpcs,
505 priv->plat->mult_fact_100ns,
509 if (priv->plat->has_gmac4 && priv->tx_lpi_timer <= STMMAC_ET_MAX) {
510 del_timer_sync(&priv->eee_ctrl_timer);
511 priv->tx_path_in_lpi_mode = false;
512 stmmac_lpi_entry_timer_config(priv, 1);
514 stmmac_lpi_entry_timer_config(priv, 0);
515 mod_timer(&priv->eee_ctrl_timer,
516 STMMAC_LPI_T(priv->tx_lpi_timer));
519 mutex_unlock(&priv->lock);
520 netdev_dbg(priv->dev, "Energy-Efficient Ethernet initialized\n");
524 /* stmmac_get_tx_hwtstamp - get HW TX timestamps
525 * @priv: driver private structure
526 * @p : descriptor pointer
527 * @skb : the socket buffer
529 * This function will read timestamp from the descriptor & pass it to stack.
530 * and also perform some sanity checks.
532 static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
533 struct dma_desc *p, struct sk_buff *skb)
535 struct skb_shared_hwtstamps shhwtstamp;
539 if (!priv->hwts_tx_en)
542 /* exit if skb doesn't support hw tstamp */
543 if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
546 /* check tx tstamp status */
547 if (stmmac_get_tx_timestamp_status(priv, p)) {
548 stmmac_get_timestamp(priv, p, priv->adv_ts, &ns);
550 } else if (!stmmac_get_mac_tx_timestamp(priv, priv->hw, &ns)) {
555 ns -= priv->plat->cdc_error_adj;
557 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
558 shhwtstamp.hwtstamp = ns_to_ktime(ns);
560 netdev_dbg(priv->dev, "get valid TX hw timestamp %llu\n", ns);
561 /* pass tstamp to stack */
562 skb_tstamp_tx(skb, &shhwtstamp);
566 /* stmmac_get_rx_hwtstamp - get HW RX timestamps
567 * @priv: driver private structure
568 * @p : descriptor pointer
569 * @np : next descriptor pointer
570 * @skb : the socket buffer
572 * This function will read received packet's timestamp from the descriptor
573 * and pass it to stack. It also perform some sanity checks.
575 static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,
576 struct dma_desc *np, struct sk_buff *skb)
578 struct skb_shared_hwtstamps *shhwtstamp = NULL;
579 struct dma_desc *desc = p;
582 if (!priv->hwts_rx_en)
584 /* For GMAC4, the valid timestamp is from CTX next desc. */
585 if (priv->plat->has_gmac4 || priv->plat->has_xgmac)
588 /* Check if timestamp is available */
589 if (stmmac_get_rx_timestamp_status(priv, p, np, priv->adv_ts)) {
590 stmmac_get_timestamp(priv, desc, priv->adv_ts, &ns);
592 ns -= priv->plat->cdc_error_adj;
594 netdev_dbg(priv->dev, "get valid RX hw timestamp %llu\n", ns);
595 shhwtstamp = skb_hwtstamps(skb);
596 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
597 shhwtstamp->hwtstamp = ns_to_ktime(ns);
599 netdev_dbg(priv->dev, "cannot get RX hw timestamp\n");
604 * stmmac_hwtstamp_set - control hardware timestamping.
605 * @dev: device pointer.
606 * @ifr: An IOCTL specific structure, that can contain a pointer to
607 * a proprietary structure used to pass information to the driver.
609 * This function configures the MAC to enable/disable both outgoing(TX)
610 * and incoming(RX) packets time stamping based on user input.
612 * 0 on success and an appropriate -ve integer on failure.
614 static int stmmac_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
616 struct stmmac_priv *priv = netdev_priv(dev);
617 struct hwtstamp_config config;
620 u32 ptp_over_ipv4_udp = 0;
621 u32 ptp_over_ipv6_udp = 0;
622 u32 ptp_over_ethernet = 0;
623 u32 snap_type_sel = 0;
624 u32 ts_master_en = 0;
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 if (config.tx_type != HWTSTAMP_TX_OFF &&
643 config.tx_type != HWTSTAMP_TX_ON)
647 switch (config.rx_filter) {
648 case HWTSTAMP_FILTER_NONE:
649 /* time stamp no incoming packet at all */
650 config.rx_filter = HWTSTAMP_FILTER_NONE;
653 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
654 /* PTP v1, UDP, any kind of event packet */
655 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
656 /* 'xmac' hardware can support Sync, Pdelay_Req and
657 * Pdelay_resp by setting bit14 and bits17/16 to 01
658 * This leaves Delay_Req timestamps out.
659 * Enable all events *and* general purpose message
662 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
663 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
664 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
667 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
668 /* PTP v1, UDP, Sync packet */
669 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
670 /* take time stamp for SYNC messages only */
671 ts_event_en = PTP_TCR_TSEVNTENA;
673 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
674 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
677 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
678 /* PTP v1, UDP, Delay_req packet */
679 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
680 /* take time stamp for Delay_Req messages only */
681 ts_master_en = PTP_TCR_TSMSTRENA;
682 ts_event_en = PTP_TCR_TSEVNTENA;
684 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
685 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
688 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
689 /* PTP v2, UDP, any kind of event packet */
690 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
691 ptp_v2 = PTP_TCR_TSVER2ENA;
692 /* take time stamp for all event messages */
693 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
695 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
696 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
699 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
700 /* PTP v2, UDP, Sync packet */
701 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
702 ptp_v2 = PTP_TCR_TSVER2ENA;
703 /* take time stamp for SYNC messages only */
704 ts_event_en = PTP_TCR_TSEVNTENA;
706 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
707 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
710 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
711 /* PTP v2, UDP, Delay_req packet */
712 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
713 ptp_v2 = PTP_TCR_TSVER2ENA;
714 /* take time stamp for Delay_Req messages only */
715 ts_master_en = PTP_TCR_TSMSTRENA;
716 ts_event_en = PTP_TCR_TSEVNTENA;
718 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
719 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
722 case HWTSTAMP_FILTER_PTP_V2_EVENT:
723 /* PTP v2/802.AS1 any layer, any kind of event packet */
724 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
725 ptp_v2 = PTP_TCR_TSVER2ENA;
726 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
727 if (priv->synopsys_id < DWMAC_CORE_4_10)
728 ts_event_en = PTP_TCR_TSEVNTENA;
729 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
730 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
731 ptp_over_ethernet = PTP_TCR_TSIPENA;
734 case HWTSTAMP_FILTER_PTP_V2_SYNC:
735 /* PTP v2/802.AS1, any layer, Sync packet */
736 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
737 ptp_v2 = PTP_TCR_TSVER2ENA;
738 /* take time stamp for SYNC messages only */
739 ts_event_en = PTP_TCR_TSEVNTENA;
741 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
742 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
743 ptp_over_ethernet = PTP_TCR_TSIPENA;
746 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
747 /* PTP v2/802.AS1, any layer, Delay_req packet */
748 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
749 ptp_v2 = PTP_TCR_TSVER2ENA;
750 /* take time stamp for Delay_Req messages only */
751 ts_master_en = PTP_TCR_TSMSTRENA;
752 ts_event_en = PTP_TCR_TSEVNTENA;
754 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
755 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
756 ptp_over_ethernet = PTP_TCR_TSIPENA;
759 case HWTSTAMP_FILTER_NTP_ALL:
760 case HWTSTAMP_FILTER_ALL:
761 /* time stamp any incoming packet */
762 config.rx_filter = HWTSTAMP_FILTER_ALL;
763 tstamp_all = PTP_TCR_TSENALL;
770 switch (config.rx_filter) {
771 case HWTSTAMP_FILTER_NONE:
772 config.rx_filter = HWTSTAMP_FILTER_NONE;
775 /* PTP v1, UDP, any kind of event packet */
776 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
780 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
781 priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
783 priv->systime_flags = STMMAC_HWTS_ACTIVE;
785 if (priv->hwts_tx_en || priv->hwts_rx_en) {
786 priv->systime_flags |= tstamp_all | ptp_v2 |
787 ptp_over_ethernet | ptp_over_ipv6_udp |
788 ptp_over_ipv4_udp | ts_event_en |
789 ts_master_en | snap_type_sel;
792 stmmac_config_hw_tstamping(priv, priv->ptpaddr, priv->systime_flags);
794 memcpy(&priv->tstamp_config, &config, sizeof(config));
796 return copy_to_user(ifr->ifr_data, &config,
797 sizeof(config)) ? -EFAULT : 0;
801 * stmmac_hwtstamp_get - read hardware timestamping.
802 * @dev: device pointer.
803 * @ifr: An IOCTL specific structure, that can contain a pointer to
804 * a proprietary structure used to pass information to the driver.
806 * This function obtain the current hardware timestamping settings
809 static int stmmac_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
811 struct stmmac_priv *priv = netdev_priv(dev);
812 struct hwtstamp_config *config = &priv->tstamp_config;
814 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
817 return copy_to_user(ifr->ifr_data, config,
818 sizeof(*config)) ? -EFAULT : 0;
822 * stmmac_init_tstamp_counter - init hardware timestamping counter
823 * @priv: driver private structure
824 * @systime_flags: timestamping flags
826 * Initialize hardware counter for packet timestamping.
827 * This is valid as long as the interface is open and not suspended.
828 * Will be rerun after resuming from suspend, case in which the timestamping
829 * flags updated by stmmac_hwtstamp_set() also need to be restored.
831 int stmmac_init_tstamp_counter(struct stmmac_priv *priv, u32 systime_flags)
833 bool xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
834 struct timespec64 now;
839 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
842 ret = clk_prepare_enable(priv->plat->clk_ptp_ref);
844 netdev_warn(priv->dev,
845 "failed to enable PTP reference clock: %pe\n",
850 stmmac_config_hw_tstamping(priv, priv->ptpaddr, systime_flags);
851 priv->systime_flags = systime_flags;
853 /* program Sub Second Increment reg */
854 stmmac_config_sub_second_increment(priv, priv->ptpaddr,
855 priv->plat->clk_ptp_rate,
857 temp = div_u64(1000000000ULL, sec_inc);
859 /* Store sub second increment for later use */
860 priv->sub_second_inc = sec_inc;
862 /* calculate default added value:
864 * addend = (2^32)/freq_div_ratio;
865 * where, freq_div_ratio = 1e9ns/sec_inc
867 temp = (u64)(temp << 32);
868 priv->default_addend = div_u64(temp, priv->plat->clk_ptp_rate);
869 stmmac_config_addend(priv, priv->ptpaddr, priv->default_addend);
871 /* initialize system time */
872 ktime_get_real_ts64(&now);
874 /* lower 32 bits of tv_sec are safe until y2106 */
875 stmmac_init_systime(priv, priv->ptpaddr, (u32)now.tv_sec, now.tv_nsec);
879 EXPORT_SYMBOL_GPL(stmmac_init_tstamp_counter);
882 * stmmac_init_ptp - init PTP
883 * @priv: driver private structure
884 * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
885 * This is done by looking at the HW cap. register.
886 * This function also registers the ptp driver.
888 static int stmmac_init_ptp(struct stmmac_priv *priv)
890 bool xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
893 if (priv->plat->ptp_clk_freq_config)
894 priv->plat->ptp_clk_freq_config(priv);
896 ret = stmmac_init_tstamp_counter(priv, STMMAC_HWTS_ACTIVE);
901 /* Check if adv_ts can be enabled for dwmac 4.x / xgmac core */
902 if (xmac && priv->dma_cap.atime_stamp)
904 /* Dwmac 3.x core with extend_desc can support adv_ts */
905 else if (priv->extend_desc && priv->dma_cap.atime_stamp)
908 if (priv->dma_cap.time_stamp)
909 netdev_info(priv->dev, "IEEE 1588-2002 Timestamp supported\n");
912 netdev_info(priv->dev,
913 "IEEE 1588-2008 Advanced Timestamp supported\n");
915 priv->hwts_tx_en = 0;
916 priv->hwts_rx_en = 0;
921 static void stmmac_release_ptp(struct stmmac_priv *priv)
923 clk_disable_unprepare(priv->plat->clk_ptp_ref);
924 stmmac_ptp_unregister(priv);
928 * stmmac_mac_flow_ctrl - Configure flow control in all queues
929 * @priv: driver private structure
930 * @duplex: duplex passed to the next function
931 * Description: It is used for configuring the flow control in all queues
933 static void stmmac_mac_flow_ctrl(struct stmmac_priv *priv, u32 duplex)
935 u32 tx_cnt = priv->plat->tx_queues_to_use;
937 stmmac_flow_ctrl(priv, priv->hw, duplex, priv->flow_ctrl,
938 priv->pause, tx_cnt);
941 static struct phylink_pcs *stmmac_mac_select_pcs(struct phylink_config *config,
942 phy_interface_t interface)
944 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
949 return &priv->hw->xpcs->pcs;
952 static void stmmac_mac_config(struct phylink_config *config, unsigned int mode,
953 const struct phylink_link_state *state)
955 /* Nothing to do, xpcs_config() handles everything */
958 static void stmmac_fpe_link_state_handle(struct stmmac_priv *priv, bool is_up)
960 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
961 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
962 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
963 bool *hs_enable = &fpe_cfg->hs_enable;
965 if (is_up && *hs_enable) {
966 stmmac_fpe_send_mpacket(priv, priv->ioaddr, MPACKET_VERIFY);
968 *lo_state = FPE_STATE_OFF;
969 *lp_state = FPE_STATE_OFF;
973 static void stmmac_mac_link_down(struct phylink_config *config,
974 unsigned int mode, phy_interface_t interface)
976 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
978 stmmac_mac_set(priv, priv->ioaddr, false);
979 priv->eee_active = false;
980 priv->tx_lpi_enabled = false;
981 priv->eee_enabled = stmmac_eee_init(priv);
982 stmmac_set_eee_pls(priv, priv->hw, false);
984 if (priv->dma_cap.fpesel)
985 stmmac_fpe_link_state_handle(priv, false);
988 static void stmmac_mac_link_up(struct phylink_config *config,
989 struct phy_device *phy,
990 unsigned int mode, phy_interface_t interface,
991 int speed, int duplex,
992 bool tx_pause, bool rx_pause)
994 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
997 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
998 ctrl &= ~priv->hw->link.speed_mask;
1000 if (interface == PHY_INTERFACE_MODE_USXGMII) {
1003 ctrl |= priv->hw->link.xgmii.speed10000;
1006 ctrl |= priv->hw->link.xgmii.speed5000;
1009 ctrl |= priv->hw->link.xgmii.speed2500;
1014 } else if (interface == PHY_INTERFACE_MODE_XLGMII) {
1017 ctrl |= priv->hw->link.xlgmii.speed100000;
1020 ctrl |= priv->hw->link.xlgmii.speed50000;
1023 ctrl |= priv->hw->link.xlgmii.speed40000;
1026 ctrl |= priv->hw->link.xlgmii.speed25000;
1029 ctrl |= priv->hw->link.xgmii.speed10000;
1032 ctrl |= priv->hw->link.speed2500;
1035 ctrl |= priv->hw->link.speed1000;
1043 ctrl |= priv->hw->link.speed2500;
1046 ctrl |= priv->hw->link.speed1000;
1049 ctrl |= priv->hw->link.speed100;
1052 ctrl |= priv->hw->link.speed10;
1059 priv->speed = speed;
1061 if (priv->plat->fix_mac_speed)
1062 priv->plat->fix_mac_speed(priv->plat->bsp_priv, speed);
1065 ctrl &= ~priv->hw->link.duplex;
1067 ctrl |= priv->hw->link.duplex;
1069 /* Flow Control operation */
1070 if (tx_pause && rx_pause)
1071 stmmac_mac_flow_ctrl(priv, duplex);
1073 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
1075 stmmac_mac_set(priv, priv->ioaddr, true);
1076 if (phy && priv->dma_cap.eee) {
1077 priv->eee_active = phy_init_eee(phy, 1) >= 0;
1078 priv->eee_enabled = stmmac_eee_init(priv);
1079 priv->tx_lpi_enabled = priv->eee_enabled;
1080 stmmac_set_eee_pls(priv, priv->hw, true);
1083 if (priv->dma_cap.fpesel)
1084 stmmac_fpe_link_state_handle(priv, true);
1087 static const struct phylink_mac_ops stmmac_phylink_mac_ops = {
1088 .validate = phylink_generic_validate,
1089 .mac_select_pcs = stmmac_mac_select_pcs,
1090 .mac_config = stmmac_mac_config,
1091 .mac_link_down = stmmac_mac_link_down,
1092 .mac_link_up = stmmac_mac_link_up,
1096 * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
1097 * @priv: driver private structure
1098 * Description: this is to verify if the HW supports the PCS.
1099 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
1100 * configured for the TBI, RTBI, or SGMII PHY interface.
1102 static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
1104 int interface = priv->plat->interface;
1106 if (priv->dma_cap.pcs) {
1107 if ((interface == PHY_INTERFACE_MODE_RGMII) ||
1108 (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
1109 (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
1110 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
1111 netdev_dbg(priv->dev, "PCS RGMII support enabled\n");
1112 priv->hw->pcs = STMMAC_PCS_RGMII;
1113 } else if (interface == PHY_INTERFACE_MODE_SGMII) {
1114 netdev_dbg(priv->dev, "PCS SGMII support enabled\n");
1115 priv->hw->pcs = STMMAC_PCS_SGMII;
1121 * stmmac_init_phy - PHY initialization
1122 * @dev: net device structure
1123 * Description: it initializes the driver's PHY state, and attaches the PHY
1124 * to the mac driver.
1128 static int stmmac_init_phy(struct net_device *dev)
1130 struct stmmac_priv *priv = netdev_priv(dev);
1131 struct device_node *node;
1134 node = priv->plat->phylink_node;
1137 ret = phylink_of_phy_connect(priv->phylink, node, 0);
1139 /* Some DT bindings do not set-up the PHY handle. Let's try to
1143 int addr = priv->plat->phy_addr;
1144 struct phy_device *phydev;
1146 phydev = mdiobus_get_phy(priv->mii, addr);
1148 netdev_err(priv->dev, "no phy at addr %d\n", addr);
1152 ret = phylink_connect_phy(priv->phylink, phydev);
1155 if (!priv->plat->pmt) {
1156 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1158 phylink_ethtool_get_wol(priv->phylink, &wol);
1159 device_set_wakeup_capable(priv->device, !!wol.supported);
1165 static int stmmac_phy_setup(struct stmmac_priv *priv)
1167 struct stmmac_mdio_bus_data *mdio_bus_data = priv->plat->mdio_bus_data;
1168 struct fwnode_handle *fwnode = of_fwnode_handle(priv->plat->phylink_node);
1169 int max_speed = priv->plat->max_speed;
1170 int mode = priv->plat->phy_interface;
1171 struct phylink *phylink;
1173 priv->phylink_config.dev = &priv->dev->dev;
1174 priv->phylink_config.type = PHYLINK_NETDEV;
1175 if (priv->plat->mdio_bus_data)
1176 priv->phylink_config.ovr_an_inband =
1177 mdio_bus_data->xpcs_an_inband;
1180 fwnode = dev_fwnode(priv->device);
1182 /* Set the platform/firmware specified interface mode */
1183 __set_bit(mode, priv->phylink_config.supported_interfaces);
1185 /* If we have an xpcs, it defines which PHY interfaces are supported. */
1187 xpcs_get_interfaces(priv->hw->xpcs,
1188 priv->phylink_config.supported_interfaces);
1190 priv->phylink_config.mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
1193 if (!max_speed || max_speed >= 1000)
1194 priv->phylink_config.mac_capabilities |= MAC_1000;
1196 if (priv->plat->has_gmac4) {
1197 if (!max_speed || max_speed >= 2500)
1198 priv->phylink_config.mac_capabilities |= MAC_2500FD;
1199 } else if (priv->plat->has_xgmac) {
1200 if (!max_speed || max_speed >= 2500)
1201 priv->phylink_config.mac_capabilities |= MAC_2500FD;
1202 if (!max_speed || max_speed >= 5000)
1203 priv->phylink_config.mac_capabilities |= MAC_5000FD;
1204 if (!max_speed || max_speed >= 10000)
1205 priv->phylink_config.mac_capabilities |= MAC_10000FD;
1206 if (!max_speed || max_speed >= 25000)
1207 priv->phylink_config.mac_capabilities |= MAC_25000FD;
1208 if (!max_speed || max_speed >= 40000)
1209 priv->phylink_config.mac_capabilities |= MAC_40000FD;
1210 if (!max_speed || max_speed >= 50000)
1211 priv->phylink_config.mac_capabilities |= MAC_50000FD;
1212 if (!max_speed || max_speed >= 100000)
1213 priv->phylink_config.mac_capabilities |= MAC_100000FD;
1216 /* Half-Duplex can only work with single queue */
1217 if (priv->plat->tx_queues_to_use > 1)
1218 priv->phylink_config.mac_capabilities &=
1219 ~(MAC_10HD | MAC_100HD | MAC_1000HD);
1221 phylink = phylink_create(&priv->phylink_config, fwnode,
1222 mode, &stmmac_phylink_mac_ops);
1223 if (IS_ERR(phylink))
1224 return PTR_ERR(phylink);
1226 priv->phylink = phylink;
1230 static void stmmac_display_rx_rings(struct stmmac_priv *priv)
1232 u32 rx_cnt = priv->plat->rx_queues_to_use;
1233 unsigned int desc_size;
1237 /* Display RX rings */
1238 for (queue = 0; queue < rx_cnt; queue++) {
1239 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1241 pr_info("\tRX Queue %u rings\n", queue);
1243 if (priv->extend_desc) {
1244 head_rx = (void *)rx_q->dma_erx;
1245 desc_size = sizeof(struct dma_extended_desc);
1247 head_rx = (void *)rx_q->dma_rx;
1248 desc_size = sizeof(struct dma_desc);
1251 /* Display RX ring */
1252 stmmac_display_ring(priv, head_rx, priv->dma_rx_size, true,
1253 rx_q->dma_rx_phy, desc_size);
1257 static void stmmac_display_tx_rings(struct stmmac_priv *priv)
1259 u32 tx_cnt = priv->plat->tx_queues_to_use;
1260 unsigned int desc_size;
1264 /* Display TX rings */
1265 for (queue = 0; queue < tx_cnt; queue++) {
1266 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1268 pr_info("\tTX Queue %d rings\n", queue);
1270 if (priv->extend_desc) {
1271 head_tx = (void *)tx_q->dma_etx;
1272 desc_size = sizeof(struct dma_extended_desc);
1273 } else if (tx_q->tbs & STMMAC_TBS_AVAIL) {
1274 head_tx = (void *)tx_q->dma_entx;
1275 desc_size = sizeof(struct dma_edesc);
1277 head_tx = (void *)tx_q->dma_tx;
1278 desc_size = sizeof(struct dma_desc);
1281 stmmac_display_ring(priv, head_tx, priv->dma_tx_size, false,
1282 tx_q->dma_tx_phy, desc_size);
1286 static void stmmac_display_rings(struct stmmac_priv *priv)
1288 /* Display RX ring */
1289 stmmac_display_rx_rings(priv);
1291 /* Display TX ring */
1292 stmmac_display_tx_rings(priv);
1295 static int stmmac_set_bfsize(int mtu, int bufsize)
1299 if (mtu >= BUF_SIZE_8KiB)
1300 ret = BUF_SIZE_16KiB;
1301 else if (mtu >= BUF_SIZE_4KiB)
1302 ret = BUF_SIZE_8KiB;
1303 else if (mtu >= BUF_SIZE_2KiB)
1304 ret = BUF_SIZE_4KiB;
1305 else if (mtu > DEFAULT_BUFSIZE)
1306 ret = BUF_SIZE_2KiB;
1308 ret = DEFAULT_BUFSIZE;
1314 * stmmac_clear_rx_descriptors - clear RX descriptors
1315 * @priv: driver private structure
1316 * @queue: RX queue index
1317 * Description: this function is called to clear the RX descriptors
1318 * in case of both basic and extended descriptors are used.
1320 static void stmmac_clear_rx_descriptors(struct stmmac_priv *priv, u32 queue)
1322 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1325 /* Clear the RX descriptors */
1326 for (i = 0; i < priv->dma_rx_size; i++)
1327 if (priv->extend_desc)
1328 stmmac_init_rx_desc(priv, &rx_q->dma_erx[i].basic,
1329 priv->use_riwt, priv->mode,
1330 (i == priv->dma_rx_size - 1),
1333 stmmac_init_rx_desc(priv, &rx_q->dma_rx[i],
1334 priv->use_riwt, priv->mode,
1335 (i == priv->dma_rx_size - 1),
1340 * stmmac_clear_tx_descriptors - clear tx descriptors
1341 * @priv: driver private structure
1342 * @queue: TX queue index.
1343 * Description: this function is called to clear the TX descriptors
1344 * in case of both basic and extended descriptors are used.
1346 static void stmmac_clear_tx_descriptors(struct stmmac_priv *priv, u32 queue)
1348 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1351 /* Clear the TX descriptors */
1352 for (i = 0; i < priv->dma_tx_size; i++) {
1353 int last = (i == (priv->dma_tx_size - 1));
1356 if (priv->extend_desc)
1357 p = &tx_q->dma_etx[i].basic;
1358 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1359 p = &tx_q->dma_entx[i].basic;
1361 p = &tx_q->dma_tx[i];
1363 stmmac_init_tx_desc(priv, p, priv->mode, last);
1368 * stmmac_clear_descriptors - clear descriptors
1369 * @priv: driver private structure
1370 * Description: this function is called to clear the TX and RX descriptors
1371 * in case of both basic and extended descriptors are used.
1373 static void stmmac_clear_descriptors(struct stmmac_priv *priv)
1375 u32 rx_queue_cnt = priv->plat->rx_queues_to_use;
1376 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1379 /* Clear the RX descriptors */
1380 for (queue = 0; queue < rx_queue_cnt; queue++)
1381 stmmac_clear_rx_descriptors(priv, queue);
1383 /* Clear the TX descriptors */
1384 for (queue = 0; queue < tx_queue_cnt; queue++)
1385 stmmac_clear_tx_descriptors(priv, queue);
1389 * stmmac_init_rx_buffers - init the RX descriptor buffer.
1390 * @priv: driver private structure
1391 * @p: descriptor pointer
1392 * @i: descriptor index
1394 * @queue: RX queue index
1395 * Description: this function is called to allocate a receive buffer, perform
1396 * the DMA mapping and init the descriptor.
1398 static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
1399 int i, gfp_t flags, u32 queue)
1401 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1402 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1403 gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
1405 if (priv->dma_cap.addr64 <= 32)
1409 buf->page = page_pool_alloc_pages(rx_q->page_pool, gfp);
1412 buf->page_offset = stmmac_rx_offset(priv);
1415 if (priv->sph && !buf->sec_page) {
1416 buf->sec_page = page_pool_alloc_pages(rx_q->page_pool, gfp);
1420 buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
1421 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true);
1423 buf->sec_page = NULL;
1424 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false);
1427 buf->addr = page_pool_get_dma_addr(buf->page) + buf->page_offset;
1429 stmmac_set_desc_addr(priv, p, buf->addr);
1430 if (priv->dma_buf_sz == BUF_SIZE_16KiB)
1431 stmmac_init_desc3(priv, p);
1437 * stmmac_free_rx_buffer - free RX dma buffers
1438 * @priv: private structure
1439 * @queue: RX queue index
1442 static void stmmac_free_rx_buffer(struct stmmac_priv *priv, u32 queue, int i)
1444 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1445 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1448 page_pool_put_full_page(rx_q->page_pool, buf->page, false);
1452 page_pool_put_full_page(rx_q->page_pool, buf->sec_page, false);
1453 buf->sec_page = NULL;
1457 * stmmac_free_tx_buffer - free RX dma buffers
1458 * @priv: private structure
1459 * @queue: RX queue index
1462 static void stmmac_free_tx_buffer(struct stmmac_priv *priv, u32 queue, int i)
1464 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1466 if (tx_q->tx_skbuff_dma[i].buf &&
1467 tx_q->tx_skbuff_dma[i].buf_type != STMMAC_TXBUF_T_XDP_TX) {
1468 if (tx_q->tx_skbuff_dma[i].map_as_page)
1469 dma_unmap_page(priv->device,
1470 tx_q->tx_skbuff_dma[i].buf,
1471 tx_q->tx_skbuff_dma[i].len,
1474 dma_unmap_single(priv->device,
1475 tx_q->tx_skbuff_dma[i].buf,
1476 tx_q->tx_skbuff_dma[i].len,
1480 if (tx_q->xdpf[i] &&
1481 (tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XDP_TX ||
1482 tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XDP_NDO)) {
1483 xdp_return_frame(tx_q->xdpf[i]);
1484 tx_q->xdpf[i] = NULL;
1487 if (tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XSK_TX)
1488 tx_q->xsk_frames_done++;
1490 if (tx_q->tx_skbuff[i] &&
1491 tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_SKB) {
1492 dev_kfree_skb_any(tx_q->tx_skbuff[i]);
1493 tx_q->tx_skbuff[i] = NULL;
1496 tx_q->tx_skbuff_dma[i].buf = 0;
1497 tx_q->tx_skbuff_dma[i].map_as_page = false;
1501 * dma_free_rx_skbufs - free RX dma buffers
1502 * @priv: private structure
1503 * @queue: RX queue index
1505 static void dma_free_rx_skbufs(struct stmmac_priv *priv, u32 queue)
1509 for (i = 0; i < priv->dma_rx_size; i++)
1510 stmmac_free_rx_buffer(priv, queue, i);
1513 static int stmmac_alloc_rx_buffers(struct stmmac_priv *priv, u32 queue,
1516 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1519 for (i = 0; i < priv->dma_rx_size; i++) {
1523 if (priv->extend_desc)
1524 p = &((rx_q->dma_erx + i)->basic);
1526 p = rx_q->dma_rx + i;
1528 ret = stmmac_init_rx_buffers(priv, p, i, flags,
1533 rx_q->buf_alloc_num++;
1540 * dma_free_rx_xskbufs - free RX dma buffers from XSK pool
1541 * @priv: private structure
1542 * @queue: RX queue index
1544 static void dma_free_rx_xskbufs(struct stmmac_priv *priv, u32 queue)
1546 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1549 for (i = 0; i < priv->dma_rx_size; i++) {
1550 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1555 xsk_buff_free(buf->xdp);
1560 static int stmmac_alloc_rx_buffers_zc(struct stmmac_priv *priv, u32 queue)
1562 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1565 for (i = 0; i < priv->dma_rx_size; i++) {
1566 struct stmmac_rx_buffer *buf;
1567 dma_addr_t dma_addr;
1570 if (priv->extend_desc)
1571 p = (struct dma_desc *)(rx_q->dma_erx + i);
1573 p = rx_q->dma_rx + i;
1575 buf = &rx_q->buf_pool[i];
1577 buf->xdp = xsk_buff_alloc(rx_q->xsk_pool);
1581 dma_addr = xsk_buff_xdp_get_dma(buf->xdp);
1582 stmmac_set_desc_addr(priv, p, dma_addr);
1583 rx_q->buf_alloc_num++;
1589 static struct xsk_buff_pool *stmmac_get_xsk_pool(struct stmmac_priv *priv, u32 queue)
1591 if (!stmmac_xdp_is_enabled(priv) || !test_bit(queue, priv->af_xdp_zc_qps))
1594 return xsk_get_pool_from_qid(priv->dev, queue);
1598 * __init_dma_rx_desc_rings - init the RX descriptor ring (per queue)
1599 * @priv: driver private structure
1600 * @queue: RX queue index
1602 * Description: this function initializes the DMA RX descriptors
1603 * and allocates the socket buffers. It supports the chained and ring
1606 static int __init_dma_rx_desc_rings(struct stmmac_priv *priv, u32 queue, gfp_t flags)
1608 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1611 netif_dbg(priv, probe, priv->dev,
1612 "(%s) dma_rx_phy=0x%08x\n", __func__,
1613 (u32)rx_q->dma_rx_phy);
1615 stmmac_clear_rx_descriptors(priv, queue);
1617 xdp_rxq_info_unreg_mem_model(&rx_q->xdp_rxq);
1619 rx_q->xsk_pool = stmmac_get_xsk_pool(priv, queue);
1621 if (rx_q->xsk_pool) {
1622 WARN_ON(xdp_rxq_info_reg_mem_model(&rx_q->xdp_rxq,
1623 MEM_TYPE_XSK_BUFF_POOL,
1625 netdev_info(priv->dev,
1626 "Register MEM_TYPE_XSK_BUFF_POOL RxQ-%d\n",
1628 xsk_pool_set_rxq_info(rx_q->xsk_pool, &rx_q->xdp_rxq);
1630 WARN_ON(xdp_rxq_info_reg_mem_model(&rx_q->xdp_rxq,
1633 netdev_info(priv->dev,
1634 "Register MEM_TYPE_PAGE_POOL RxQ-%d\n",
1638 if (rx_q->xsk_pool) {
1639 /* RX XDP ZC buffer pool may not be populated, e.g.
1642 stmmac_alloc_rx_buffers_zc(priv, queue);
1644 ret = stmmac_alloc_rx_buffers(priv, queue, flags);
1652 /* Setup the chained descriptor addresses */
1653 if (priv->mode == STMMAC_CHAIN_MODE) {
1654 if (priv->extend_desc)
1655 stmmac_mode_init(priv, rx_q->dma_erx,
1657 priv->dma_rx_size, 1);
1659 stmmac_mode_init(priv, rx_q->dma_rx,
1661 priv->dma_rx_size, 0);
1667 static int init_dma_rx_desc_rings(struct net_device *dev, gfp_t flags)
1669 struct stmmac_priv *priv = netdev_priv(dev);
1670 u32 rx_count = priv->plat->rx_queues_to_use;
1674 /* RX INITIALIZATION */
1675 netif_dbg(priv, probe, priv->dev,
1676 "SKB addresses:\nskb\t\tskb data\tdma data\n");
1678 for (queue = 0; queue < rx_count; queue++) {
1679 ret = __init_dma_rx_desc_rings(priv, queue, flags);
1681 goto err_init_rx_buffers;
1686 err_init_rx_buffers:
1687 while (queue >= 0) {
1688 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1691 dma_free_rx_xskbufs(priv, queue);
1693 dma_free_rx_skbufs(priv, queue);
1695 rx_q->buf_alloc_num = 0;
1696 rx_q->xsk_pool = NULL;
1708 * __init_dma_tx_desc_rings - init the TX descriptor ring (per queue)
1709 * @priv: driver private structure
1710 * @queue : TX queue index
1711 * Description: this function initializes the DMA TX descriptors
1712 * and allocates the socket buffers. It supports the chained and ring
1715 static int __init_dma_tx_desc_rings(struct stmmac_priv *priv, u32 queue)
1717 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1720 netif_dbg(priv, probe, priv->dev,
1721 "(%s) dma_tx_phy=0x%08x\n", __func__,
1722 (u32)tx_q->dma_tx_phy);
1724 /* Setup the chained descriptor addresses */
1725 if (priv->mode == STMMAC_CHAIN_MODE) {
1726 if (priv->extend_desc)
1727 stmmac_mode_init(priv, tx_q->dma_etx,
1729 priv->dma_tx_size, 1);
1730 else if (!(tx_q->tbs & STMMAC_TBS_AVAIL))
1731 stmmac_mode_init(priv, tx_q->dma_tx,
1733 priv->dma_tx_size, 0);
1736 tx_q->xsk_pool = stmmac_get_xsk_pool(priv, queue);
1738 for (i = 0; i < priv->dma_tx_size; i++) {
1741 if (priv->extend_desc)
1742 p = &((tx_q->dma_etx + i)->basic);
1743 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1744 p = &((tx_q->dma_entx + i)->basic);
1746 p = tx_q->dma_tx + i;
1748 stmmac_clear_desc(priv, p);
1750 tx_q->tx_skbuff_dma[i].buf = 0;
1751 tx_q->tx_skbuff_dma[i].map_as_page = false;
1752 tx_q->tx_skbuff_dma[i].len = 0;
1753 tx_q->tx_skbuff_dma[i].last_segment = false;
1754 tx_q->tx_skbuff[i] = NULL;
1761 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
1766 static int init_dma_tx_desc_rings(struct net_device *dev)
1768 struct stmmac_priv *priv = netdev_priv(dev);
1772 tx_queue_cnt = priv->plat->tx_queues_to_use;
1774 for (queue = 0; queue < tx_queue_cnt; queue++)
1775 __init_dma_tx_desc_rings(priv, queue);
1781 * init_dma_desc_rings - init the RX/TX descriptor rings
1782 * @dev: net device structure
1784 * Description: this function initializes the DMA RX/TX descriptors
1785 * and allocates the socket buffers. It supports the chained and ring
1788 static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
1790 struct stmmac_priv *priv = netdev_priv(dev);
1793 ret = init_dma_rx_desc_rings(dev, flags);
1797 ret = init_dma_tx_desc_rings(dev);
1799 stmmac_clear_descriptors(priv);
1801 if (netif_msg_hw(priv))
1802 stmmac_display_rings(priv);
1808 * dma_free_tx_skbufs - free TX dma buffers
1809 * @priv: private structure
1810 * @queue: TX queue index
1812 static void dma_free_tx_skbufs(struct stmmac_priv *priv, u32 queue)
1814 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1817 tx_q->xsk_frames_done = 0;
1819 for (i = 0; i < priv->dma_tx_size; i++)
1820 stmmac_free_tx_buffer(priv, queue, i);
1822 if (tx_q->xsk_pool && tx_q->xsk_frames_done) {
1823 xsk_tx_completed(tx_q->xsk_pool, tx_q->xsk_frames_done);
1824 tx_q->xsk_frames_done = 0;
1825 tx_q->xsk_pool = NULL;
1830 * stmmac_free_tx_skbufs - free TX skb buffers
1831 * @priv: private structure
1833 static void stmmac_free_tx_skbufs(struct stmmac_priv *priv)
1835 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1838 for (queue = 0; queue < tx_queue_cnt; queue++)
1839 dma_free_tx_skbufs(priv, queue);
1843 * __free_dma_rx_desc_resources - free RX dma desc resources (per queue)
1844 * @priv: private structure
1845 * @queue: RX queue index
1847 static void __free_dma_rx_desc_resources(struct stmmac_priv *priv, u32 queue)
1849 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1851 /* Release the DMA RX socket buffers */
1853 dma_free_rx_xskbufs(priv, queue);
1855 dma_free_rx_skbufs(priv, queue);
1857 rx_q->buf_alloc_num = 0;
1858 rx_q->xsk_pool = NULL;
1860 /* Free DMA regions of consistent memory previously allocated */
1861 if (!priv->extend_desc)
1862 dma_free_coherent(priv->device, priv->dma_rx_size *
1863 sizeof(struct dma_desc),
1864 rx_q->dma_rx, rx_q->dma_rx_phy);
1866 dma_free_coherent(priv->device, priv->dma_rx_size *
1867 sizeof(struct dma_extended_desc),
1868 rx_q->dma_erx, rx_q->dma_rx_phy);
1870 if (xdp_rxq_info_is_reg(&rx_q->xdp_rxq))
1871 xdp_rxq_info_unreg(&rx_q->xdp_rxq);
1873 kfree(rx_q->buf_pool);
1874 if (rx_q->page_pool)
1875 page_pool_destroy(rx_q->page_pool);
1878 static void free_dma_rx_desc_resources(struct stmmac_priv *priv)
1880 u32 rx_count = priv->plat->rx_queues_to_use;
1883 /* Free RX queue resources */
1884 for (queue = 0; queue < rx_count; queue++)
1885 __free_dma_rx_desc_resources(priv, queue);
1889 * __free_dma_tx_desc_resources - free TX dma desc resources (per queue)
1890 * @priv: private structure
1891 * @queue: TX queue index
1893 static void __free_dma_tx_desc_resources(struct stmmac_priv *priv, u32 queue)
1895 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1899 /* Release the DMA TX socket buffers */
1900 dma_free_tx_skbufs(priv, queue);
1902 if (priv->extend_desc) {
1903 size = sizeof(struct dma_extended_desc);
1904 addr = tx_q->dma_etx;
1905 } else if (tx_q->tbs & STMMAC_TBS_AVAIL) {
1906 size = sizeof(struct dma_edesc);
1907 addr = tx_q->dma_entx;
1909 size = sizeof(struct dma_desc);
1910 addr = tx_q->dma_tx;
1913 size *= priv->dma_tx_size;
1915 dma_free_coherent(priv->device, size, addr, tx_q->dma_tx_phy);
1917 kfree(tx_q->tx_skbuff_dma);
1918 kfree(tx_q->tx_skbuff);
1921 static void free_dma_tx_desc_resources(struct stmmac_priv *priv)
1923 u32 tx_count = priv->plat->tx_queues_to_use;
1926 /* Free TX queue resources */
1927 for (queue = 0; queue < tx_count; queue++)
1928 __free_dma_tx_desc_resources(priv, queue);
1932 * __alloc_dma_rx_desc_resources - alloc RX resources (per queue).
1933 * @priv: private structure
1934 * @queue: RX queue index
1935 * Description: according to which descriptor can be used (extend or basic)
1936 * this function allocates the resources for TX and RX paths. In case of
1937 * reception, for example, it pre-allocated the RX socket buffer in order to
1938 * allow zero-copy mechanism.
1940 static int __alloc_dma_rx_desc_resources(struct stmmac_priv *priv, u32 queue)
1942 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1943 struct stmmac_channel *ch = &priv->channel[queue];
1944 bool xdp_prog = stmmac_xdp_is_enabled(priv);
1945 struct page_pool_params pp_params = { 0 };
1946 unsigned int num_pages;
1947 unsigned int napi_id;
1950 rx_q->queue_index = queue;
1951 rx_q->priv_data = priv;
1953 pp_params.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
1954 pp_params.pool_size = priv->dma_rx_size;
1955 num_pages = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE);
1956 pp_params.order = ilog2(num_pages);
1957 pp_params.nid = dev_to_node(priv->device);
1958 pp_params.dev = priv->device;
1959 pp_params.dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
1960 pp_params.offset = stmmac_rx_offset(priv);
1961 pp_params.max_len = STMMAC_MAX_RX_BUF_SIZE(num_pages);
1963 rx_q->page_pool = page_pool_create(&pp_params);
1964 if (IS_ERR(rx_q->page_pool)) {
1965 ret = PTR_ERR(rx_q->page_pool);
1966 rx_q->page_pool = NULL;
1970 rx_q->buf_pool = kcalloc(priv->dma_rx_size,
1971 sizeof(*rx_q->buf_pool),
1973 if (!rx_q->buf_pool)
1976 if (priv->extend_desc) {
1977 rx_q->dma_erx = dma_alloc_coherent(priv->device,
1979 sizeof(struct dma_extended_desc),
1986 rx_q->dma_rx = dma_alloc_coherent(priv->device,
1988 sizeof(struct dma_desc),
1995 if (stmmac_xdp_is_enabled(priv) &&
1996 test_bit(queue, priv->af_xdp_zc_qps))
1997 napi_id = ch->rxtx_napi.napi_id;
1999 napi_id = ch->rx_napi.napi_id;
2001 ret = xdp_rxq_info_reg(&rx_q->xdp_rxq, priv->dev,
2005 netdev_err(priv->dev, "Failed to register xdp rxq info\n");
2012 static int alloc_dma_rx_desc_resources(struct stmmac_priv *priv)
2014 u32 rx_count = priv->plat->rx_queues_to_use;
2018 /* RX queues buffers and DMA */
2019 for (queue = 0; queue < rx_count; queue++) {
2020 ret = __alloc_dma_rx_desc_resources(priv, queue);
2028 free_dma_rx_desc_resources(priv);
2034 * __alloc_dma_tx_desc_resources - alloc TX resources (per queue).
2035 * @priv: private structure
2036 * @queue: TX queue index
2037 * Description: according to which descriptor can be used (extend or basic)
2038 * this function allocates the resources for TX and RX paths. In case of
2039 * reception, for example, it pre-allocated the RX socket buffer in order to
2040 * allow zero-copy mechanism.
2042 static int __alloc_dma_tx_desc_resources(struct stmmac_priv *priv, u32 queue)
2044 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2048 tx_q->queue_index = queue;
2049 tx_q->priv_data = priv;
2051 tx_q->tx_skbuff_dma = kcalloc(priv->dma_tx_size,
2052 sizeof(*tx_q->tx_skbuff_dma),
2054 if (!tx_q->tx_skbuff_dma)
2057 tx_q->tx_skbuff = kcalloc(priv->dma_tx_size,
2058 sizeof(struct sk_buff *),
2060 if (!tx_q->tx_skbuff)
2063 if (priv->extend_desc)
2064 size = sizeof(struct dma_extended_desc);
2065 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2066 size = sizeof(struct dma_edesc);
2068 size = sizeof(struct dma_desc);
2070 size *= priv->dma_tx_size;
2072 addr = dma_alloc_coherent(priv->device, size,
2073 &tx_q->dma_tx_phy, GFP_KERNEL);
2077 if (priv->extend_desc)
2078 tx_q->dma_etx = addr;
2079 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2080 tx_q->dma_entx = addr;
2082 tx_q->dma_tx = addr;
2087 static int alloc_dma_tx_desc_resources(struct stmmac_priv *priv)
2089 u32 tx_count = priv->plat->tx_queues_to_use;
2093 /* TX queues buffers and DMA */
2094 for (queue = 0; queue < tx_count; queue++) {
2095 ret = __alloc_dma_tx_desc_resources(priv, queue);
2103 free_dma_tx_desc_resources(priv);
2108 * alloc_dma_desc_resources - alloc TX/RX resources.
2109 * @priv: private structure
2110 * Description: according to which descriptor can be used (extend or basic)
2111 * this function allocates the resources for TX and RX paths. In case of
2112 * reception, for example, it pre-allocated the RX socket buffer in order to
2113 * allow zero-copy mechanism.
2115 static int alloc_dma_desc_resources(struct stmmac_priv *priv)
2118 int ret = alloc_dma_rx_desc_resources(priv);
2123 ret = alloc_dma_tx_desc_resources(priv);
2129 * free_dma_desc_resources - free dma desc resources
2130 * @priv: private structure
2132 static void free_dma_desc_resources(struct stmmac_priv *priv)
2134 /* Release the DMA TX socket buffers */
2135 free_dma_tx_desc_resources(priv);
2137 /* Release the DMA RX socket buffers later
2138 * to ensure all pending XDP_TX buffers are returned.
2140 free_dma_rx_desc_resources(priv);
2144 * stmmac_mac_enable_rx_queues - Enable MAC rx queues
2145 * @priv: driver private structure
2146 * Description: It is used for enabling the rx queues in the MAC
2148 static void stmmac_mac_enable_rx_queues(struct stmmac_priv *priv)
2150 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2154 for (queue = 0; queue < rx_queues_count; queue++) {
2155 mode = priv->plat->rx_queues_cfg[queue].mode_to_use;
2156 stmmac_rx_queue_enable(priv, priv->hw, mode, queue);
2161 * stmmac_start_rx_dma - start RX DMA channel
2162 * @priv: driver private structure
2163 * @chan: RX channel index
2165 * This starts a RX DMA channel
2167 static void stmmac_start_rx_dma(struct stmmac_priv *priv, u32 chan)
2169 netdev_dbg(priv->dev, "DMA RX processes started in channel %d\n", chan);
2170 stmmac_start_rx(priv, priv->ioaddr, chan);
2174 * stmmac_start_tx_dma - start TX DMA channel
2175 * @priv: driver private structure
2176 * @chan: TX channel index
2178 * This starts a TX DMA channel
2180 static void stmmac_start_tx_dma(struct stmmac_priv *priv, u32 chan)
2182 netdev_dbg(priv->dev, "DMA TX processes started in channel %d\n", chan);
2183 stmmac_start_tx(priv, priv->ioaddr, chan);
2187 * stmmac_stop_rx_dma - stop RX DMA channel
2188 * @priv: driver private structure
2189 * @chan: RX channel index
2191 * This stops a RX DMA channel
2193 static void stmmac_stop_rx_dma(struct stmmac_priv *priv, u32 chan)
2195 netdev_dbg(priv->dev, "DMA RX processes stopped in channel %d\n", chan);
2196 stmmac_stop_rx(priv, priv->ioaddr, chan);
2200 * stmmac_stop_tx_dma - stop TX DMA channel
2201 * @priv: driver private structure
2202 * @chan: TX channel index
2204 * This stops a TX DMA channel
2206 static void stmmac_stop_tx_dma(struct stmmac_priv *priv, u32 chan)
2208 netdev_dbg(priv->dev, "DMA TX processes stopped in channel %d\n", chan);
2209 stmmac_stop_tx(priv, priv->ioaddr, chan);
2213 * stmmac_start_all_dma - start all RX and TX DMA channels
2214 * @priv: driver private structure
2216 * This starts all the RX and TX DMA channels
2218 static void stmmac_start_all_dma(struct stmmac_priv *priv)
2220 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2221 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2224 for (chan = 0; chan < rx_channels_count; chan++)
2225 stmmac_start_rx_dma(priv, chan);
2227 for (chan = 0; chan < tx_channels_count; chan++)
2228 stmmac_start_tx_dma(priv, chan);
2232 * stmmac_stop_all_dma - stop all RX and TX DMA channels
2233 * @priv: driver private structure
2235 * This stops the RX and TX DMA channels
2237 static void stmmac_stop_all_dma(struct stmmac_priv *priv)
2239 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2240 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2243 for (chan = 0; chan < rx_channels_count; chan++)
2244 stmmac_stop_rx_dma(priv, chan);
2246 for (chan = 0; chan < tx_channels_count; chan++)
2247 stmmac_stop_tx_dma(priv, chan);
2251 * stmmac_dma_operation_mode - HW DMA operation mode
2252 * @priv: driver private structure
2253 * Description: it is used for configuring the DMA operation mode register in
2254 * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
2256 static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
2258 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2259 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2260 int rxfifosz = priv->plat->rx_fifo_size;
2261 int txfifosz = priv->plat->tx_fifo_size;
2268 rxfifosz = priv->dma_cap.rx_fifo_size;
2270 txfifosz = priv->dma_cap.tx_fifo_size;
2272 /* Adjust for real per queue fifo size */
2273 rxfifosz /= rx_channels_count;
2274 txfifosz /= tx_channels_count;
2276 if (priv->plat->force_thresh_dma_mode) {
2279 } else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
2281 * In case of GMAC, SF mode can be enabled
2282 * to perform the TX COE in HW. This depends on:
2283 * 1) TX COE if actually supported
2284 * 2) There is no bugged Jumbo frame support
2285 * that needs to not insert csum in the TDES.
2287 txmode = SF_DMA_MODE;
2288 rxmode = SF_DMA_MODE;
2289 priv->xstats.threshold = SF_DMA_MODE;
2292 rxmode = SF_DMA_MODE;
2295 /* configure all channels */
2296 for (chan = 0; chan < rx_channels_count; chan++) {
2297 struct stmmac_rx_queue *rx_q = &priv->rx_queue[chan];
2300 qmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
2302 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan,
2305 if (rx_q->xsk_pool) {
2306 buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool);
2307 stmmac_set_dma_bfsize(priv, priv->ioaddr,
2311 stmmac_set_dma_bfsize(priv, priv->ioaddr,
2317 for (chan = 0; chan < tx_channels_count; chan++) {
2318 qmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
2320 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan,
2325 static bool stmmac_xdp_xmit_zc(struct stmmac_priv *priv, u32 queue, u32 budget)
2327 struct netdev_queue *nq = netdev_get_tx_queue(priv->dev, queue);
2328 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2329 struct xsk_buff_pool *pool = tx_q->xsk_pool;
2330 unsigned int entry = tx_q->cur_tx;
2331 struct dma_desc *tx_desc = NULL;
2332 struct xdp_desc xdp_desc;
2333 bool work_done = true;
2335 /* Avoids TX time-out as we are sharing with slow path */
2336 txq_trans_cond_update(nq);
2338 budget = min(budget, stmmac_tx_avail(priv, queue));
2340 while (budget-- > 0) {
2341 dma_addr_t dma_addr;
2344 /* We are sharing with slow path and stop XSK TX desc submission when
2345 * available TX ring is less than threshold.
2347 if (unlikely(stmmac_tx_avail(priv, queue) < STMMAC_TX_XSK_AVAIL) ||
2348 !netif_carrier_ok(priv->dev)) {
2353 if (!xsk_tx_peek_desc(pool, &xdp_desc))
2356 if (likely(priv->extend_desc))
2357 tx_desc = (struct dma_desc *)(tx_q->dma_etx + entry);
2358 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2359 tx_desc = &tx_q->dma_entx[entry].basic;
2361 tx_desc = tx_q->dma_tx + entry;
2363 dma_addr = xsk_buff_raw_get_dma(pool, xdp_desc.addr);
2364 xsk_buff_raw_dma_sync_for_device(pool, dma_addr, xdp_desc.len);
2366 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XSK_TX;
2368 /* To return XDP buffer to XSK pool, we simple call
2369 * xsk_tx_completed(), so we don't need to fill up
2372 tx_q->tx_skbuff_dma[entry].buf = 0;
2373 tx_q->xdpf[entry] = NULL;
2375 tx_q->tx_skbuff_dma[entry].map_as_page = false;
2376 tx_q->tx_skbuff_dma[entry].len = xdp_desc.len;
2377 tx_q->tx_skbuff_dma[entry].last_segment = true;
2378 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
2380 stmmac_set_desc_addr(priv, tx_desc, dma_addr);
2382 tx_q->tx_count_frames++;
2384 if (!priv->tx_coal_frames[queue])
2386 else if (tx_q->tx_count_frames % priv->tx_coal_frames[queue] == 0)
2392 tx_q->tx_count_frames = 0;
2393 stmmac_set_tx_ic(priv, tx_desc);
2394 priv->xstats.tx_set_ic_bit++;
2397 stmmac_prepare_tx_desc(priv, tx_desc, 1, xdp_desc.len,
2398 true, priv->mode, true, true,
2401 stmmac_enable_dma_transmission(priv, priv->ioaddr);
2403 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
2404 entry = tx_q->cur_tx;
2408 stmmac_flush_tx_descriptors(priv, queue);
2409 xsk_tx_release(pool);
2412 /* Return true if all of the 3 conditions are met
2413 * a) TX Budget is still available
2414 * b) work_done = true when XSK TX desc peek is empty (no more
2415 * pending XSK TX for transmission)
2417 return !!budget && work_done;
2420 static void stmmac_bump_dma_threshold(struct stmmac_priv *priv, u32 chan)
2422 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) && tc <= 256) {
2425 if (priv->plat->force_thresh_dma_mode)
2426 stmmac_set_dma_operation_mode(priv, tc, tc, chan);
2428 stmmac_set_dma_operation_mode(priv, tc, SF_DMA_MODE,
2431 priv->xstats.threshold = tc;
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++;
2500 if (unlikely(status & tx_err_bump_tc))
2501 stmmac_bump_dma_threshold(priv, queue);
2503 priv->dev->stats.tx_packets++;
2504 priv->xstats.tx_pkt_n++;
2505 priv->xstats.txq_stats[queue].tx_pkt_n++;
2508 stmmac_get_tx_hwtstamp(priv, p, skb);
2511 if (likely(tx_q->tx_skbuff_dma[entry].buf &&
2512 tx_q->tx_skbuff_dma[entry].buf_type != STMMAC_TXBUF_T_XDP_TX)) {
2513 if (tx_q->tx_skbuff_dma[entry].map_as_page)
2514 dma_unmap_page(priv->device,
2515 tx_q->tx_skbuff_dma[entry].buf,
2516 tx_q->tx_skbuff_dma[entry].len,
2519 dma_unmap_single(priv->device,
2520 tx_q->tx_skbuff_dma[entry].buf,
2521 tx_q->tx_skbuff_dma[entry].len,
2523 tx_q->tx_skbuff_dma[entry].buf = 0;
2524 tx_q->tx_skbuff_dma[entry].len = 0;
2525 tx_q->tx_skbuff_dma[entry].map_as_page = false;
2528 stmmac_clean_desc3(priv, tx_q, p);
2530 tx_q->tx_skbuff_dma[entry].last_segment = false;
2531 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
2534 tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_TX) {
2535 xdp_return_frame_rx_napi(xdpf);
2536 tx_q->xdpf[entry] = NULL;
2540 tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_NDO) {
2541 xdp_return_frame(xdpf);
2542 tx_q->xdpf[entry] = NULL;
2545 if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XSK_TX)
2546 tx_q->xsk_frames_done++;
2548 if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_SKB) {
2551 bytes_compl += skb->len;
2552 dev_consume_skb_any(skb);
2553 tx_q->tx_skbuff[entry] = NULL;
2557 stmmac_release_tx_desc(priv, p, priv->mode);
2559 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
2561 tx_q->dirty_tx = entry;
2563 netdev_tx_completed_queue(netdev_get_tx_queue(priv->dev, queue),
2564 pkts_compl, bytes_compl);
2566 if (unlikely(netif_tx_queue_stopped(netdev_get_tx_queue(priv->dev,
2568 stmmac_tx_avail(priv, queue) > STMMAC_TX_THRESH(priv)) {
2570 netif_dbg(priv, tx_done, priv->dev,
2571 "%s: restart transmit\n", __func__);
2572 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, queue));
2575 if (tx_q->xsk_pool) {
2578 if (tx_q->xsk_frames_done)
2579 xsk_tx_completed(tx_q->xsk_pool, tx_q->xsk_frames_done);
2581 if (xsk_uses_need_wakeup(tx_q->xsk_pool))
2582 xsk_set_tx_need_wakeup(tx_q->xsk_pool);
2584 /* For XSK TX, we try to send as many as possible.
2585 * If XSK work done (XSK TX desc empty and budget still
2586 * available), return "budget - 1" to reenable TX IRQ.
2587 * Else, return "budget" to make NAPI continue polling.
2589 work_done = stmmac_xdp_xmit_zc(priv, queue,
2590 STMMAC_XSK_TX_BUDGET_MAX);
2597 if (priv->eee_enabled && !priv->tx_path_in_lpi_mode &&
2598 priv->eee_sw_timer_en) {
2599 if (stmmac_enable_eee_mode(priv))
2600 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer));
2603 /* We still have pending packets, let's call for a new scheduling */
2604 if (tx_q->dirty_tx != tx_q->cur_tx)
2605 hrtimer_start(&tx_q->txtimer,
2606 STMMAC_COAL_TIMER(priv->tx_coal_timer[queue]),
2609 __netif_tx_unlock_bh(netdev_get_tx_queue(priv->dev, queue));
2611 /* Combine decisions from TX clean and XSK TX */
2612 return max(count, xmits);
2616 * stmmac_tx_err - to manage the tx error
2617 * @priv: driver private structure
2618 * @chan: channel index
2619 * Description: it cleans the descriptors and restarts the transmission
2620 * in case of transmission errors.
2622 static void stmmac_tx_err(struct stmmac_priv *priv, u32 chan)
2624 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2626 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, chan));
2628 stmmac_stop_tx_dma(priv, chan);
2629 dma_free_tx_skbufs(priv, chan);
2630 stmmac_clear_tx_descriptors(priv, chan);
2634 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, chan));
2635 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2636 tx_q->dma_tx_phy, chan);
2637 stmmac_start_tx_dma(priv, chan);
2639 priv->dev->stats.tx_errors++;
2640 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, chan));
2644 * stmmac_set_dma_operation_mode - Set DMA operation mode by channel
2645 * @priv: driver private structure
2646 * @txmode: TX operating mode
2647 * @rxmode: RX operating mode
2648 * @chan: channel index
2649 * Description: it is used for configuring of the DMA operation mode in
2650 * runtime in order to program the tx/rx DMA thresholds or Store-And-Forward
2653 static void stmmac_set_dma_operation_mode(struct stmmac_priv *priv, u32 txmode,
2654 u32 rxmode, u32 chan)
2656 u8 rxqmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
2657 u8 txqmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
2658 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2659 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2660 int rxfifosz = priv->plat->rx_fifo_size;
2661 int txfifosz = priv->plat->tx_fifo_size;
2664 rxfifosz = priv->dma_cap.rx_fifo_size;
2666 txfifosz = priv->dma_cap.tx_fifo_size;
2668 /* Adjust for real per queue fifo size */
2669 rxfifosz /= rx_channels_count;
2670 txfifosz /= tx_channels_count;
2672 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan, rxfifosz, rxqmode);
2673 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan, txfifosz, txqmode);
2676 static bool stmmac_safety_feat_interrupt(struct stmmac_priv *priv)
2680 ret = stmmac_safety_feat_irq_status(priv, priv->dev,
2681 priv->ioaddr, priv->dma_cap.asp, &priv->sstats);
2682 if (ret && (ret != -EINVAL)) {
2683 stmmac_global_err(priv);
2690 static int stmmac_napi_check(struct stmmac_priv *priv, u32 chan, u32 dir)
2692 int status = stmmac_dma_interrupt_status(priv, priv->ioaddr,
2693 &priv->xstats, chan, dir);
2694 struct stmmac_rx_queue *rx_q = &priv->rx_queue[chan];
2695 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2696 struct stmmac_channel *ch = &priv->channel[chan];
2697 struct napi_struct *rx_napi;
2698 struct napi_struct *tx_napi;
2699 unsigned long flags;
2701 rx_napi = rx_q->xsk_pool ? &ch->rxtx_napi : &ch->rx_napi;
2702 tx_napi = tx_q->xsk_pool ? &ch->rxtx_napi : &ch->tx_napi;
2704 if ((status & handle_rx) && (chan < priv->plat->rx_queues_to_use)) {
2705 if (napi_schedule_prep(rx_napi)) {
2706 spin_lock_irqsave(&ch->lock, flags);
2707 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
2708 spin_unlock_irqrestore(&ch->lock, flags);
2709 __napi_schedule(rx_napi);
2713 if ((status & handle_tx) && (chan < priv->plat->tx_queues_to_use)) {
2714 if (napi_schedule_prep(tx_napi)) {
2715 spin_lock_irqsave(&ch->lock, flags);
2716 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
2717 spin_unlock_irqrestore(&ch->lock, flags);
2718 __napi_schedule(tx_napi);
2726 * stmmac_dma_interrupt - DMA ISR
2727 * @priv: driver private structure
2728 * Description: this is the DMA ISR. It is called by the main ISR.
2729 * It calls the dwmac dma routine and schedule poll method in case of some
2732 static void stmmac_dma_interrupt(struct stmmac_priv *priv)
2734 u32 tx_channel_count = priv->plat->tx_queues_to_use;
2735 u32 rx_channel_count = priv->plat->rx_queues_to_use;
2736 u32 channels_to_check = tx_channel_count > rx_channel_count ?
2737 tx_channel_count : rx_channel_count;
2739 int status[max_t(u32, MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES)];
2741 /* Make sure we never check beyond our status buffer. */
2742 if (WARN_ON_ONCE(channels_to_check > ARRAY_SIZE(status)))
2743 channels_to_check = ARRAY_SIZE(status);
2745 for (chan = 0; chan < channels_to_check; chan++)
2746 status[chan] = stmmac_napi_check(priv, chan,
2749 for (chan = 0; chan < tx_channel_count; chan++) {
2750 if (unlikely(status[chan] & tx_hard_error_bump_tc)) {
2751 /* Try to bump up the dma threshold on this failure */
2752 stmmac_bump_dma_threshold(priv, chan);
2753 } else if (unlikely(status[chan] == tx_hard_error)) {
2754 stmmac_tx_err(priv, chan);
2760 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
2761 * @priv: driver private structure
2762 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
2764 static void stmmac_mmc_setup(struct stmmac_priv *priv)
2766 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
2767 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
2769 stmmac_mmc_intr_all_mask(priv, priv->mmcaddr);
2771 if (priv->dma_cap.rmon) {
2772 stmmac_mmc_ctrl(priv, priv->mmcaddr, mode);
2773 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
2775 netdev_info(priv->dev, "No MAC Management Counters available\n");
2779 * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
2780 * @priv: driver private structure
2782 * new GMAC chip generations have a new register to indicate the
2783 * presence of the optional feature/functions.
2784 * This can be also used to override the value passed through the
2785 * platform and necessary for old MAC10/100 and GMAC chips.
2787 static int stmmac_get_hw_features(struct stmmac_priv *priv)
2789 return stmmac_get_hw_feature(priv, priv->ioaddr, &priv->dma_cap) == 0;
2793 * stmmac_check_ether_addr - check if the MAC addr is valid
2794 * @priv: driver private structure
2796 * it is to verify if the MAC address is valid, in case of failures it
2797 * generates a random MAC address
2799 static void stmmac_check_ether_addr(struct stmmac_priv *priv)
2803 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
2804 stmmac_get_umac_addr(priv, priv->hw, addr, 0);
2805 if (is_valid_ether_addr(addr))
2806 eth_hw_addr_set(priv->dev, addr);
2808 eth_hw_addr_random(priv->dev);
2809 dev_info(priv->device, "device MAC address %pM\n",
2810 priv->dev->dev_addr);
2815 * stmmac_init_dma_engine - DMA init.
2816 * @priv: driver private structure
2818 * It inits the DMA invoking the specific MAC/GMAC callback.
2819 * Some DMA parameters can be passed from the platform;
2820 * in case of these are not passed a default is kept for the MAC or GMAC.
2822 static int stmmac_init_dma_engine(struct stmmac_priv *priv)
2824 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2825 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2826 u32 dma_csr_ch = max(rx_channels_count, tx_channels_count);
2827 struct stmmac_rx_queue *rx_q;
2828 struct stmmac_tx_queue *tx_q;
2833 if (!priv->plat->dma_cfg || !priv->plat->dma_cfg->pbl) {
2834 dev_err(priv->device, "Invalid DMA configuration\n");
2838 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
2841 ret = stmmac_reset(priv, priv->ioaddr);
2843 dev_err(priv->device, "Failed to reset the dma\n");
2847 /* DMA Configuration */
2848 stmmac_dma_init(priv, priv->ioaddr, priv->plat->dma_cfg, atds);
2850 if (priv->plat->axi)
2851 stmmac_axi(priv, priv->ioaddr, priv->plat->axi);
2853 /* DMA CSR Channel configuration */
2854 for (chan = 0; chan < dma_csr_ch; chan++)
2855 stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan);
2857 /* DMA RX Channel Configuration */
2858 for (chan = 0; chan < rx_channels_count; chan++) {
2859 rx_q = &priv->rx_queue[chan];
2861 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2862 rx_q->dma_rx_phy, chan);
2864 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
2865 (rx_q->buf_alloc_num *
2866 sizeof(struct dma_desc));
2867 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
2868 rx_q->rx_tail_addr, chan);
2871 /* DMA TX Channel Configuration */
2872 for (chan = 0; chan < tx_channels_count; chan++) {
2873 tx_q = &priv->tx_queue[chan];
2875 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2876 tx_q->dma_tx_phy, chan);
2878 tx_q->tx_tail_addr = tx_q->dma_tx_phy;
2879 stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
2880 tx_q->tx_tail_addr, chan);
2886 static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue)
2888 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2890 hrtimer_start(&tx_q->txtimer,
2891 STMMAC_COAL_TIMER(priv->tx_coal_timer[queue]),
2896 * stmmac_tx_timer - mitigation sw timer for tx.
2899 * This is the timer handler to directly invoke the stmmac_tx_clean.
2901 static enum hrtimer_restart stmmac_tx_timer(struct hrtimer *t)
2903 struct stmmac_tx_queue *tx_q = container_of(t, struct stmmac_tx_queue, txtimer);
2904 struct stmmac_priv *priv = tx_q->priv_data;
2905 struct stmmac_channel *ch;
2906 struct napi_struct *napi;
2908 ch = &priv->channel[tx_q->queue_index];
2909 napi = tx_q->xsk_pool ? &ch->rxtx_napi : &ch->tx_napi;
2911 if (likely(napi_schedule_prep(napi))) {
2912 unsigned long flags;
2914 spin_lock_irqsave(&ch->lock, flags);
2915 stmmac_disable_dma_irq(priv, priv->ioaddr, ch->index, 0, 1);
2916 spin_unlock_irqrestore(&ch->lock, flags);
2917 __napi_schedule(napi);
2920 return HRTIMER_NORESTART;
2924 * stmmac_init_coalesce - init mitigation options.
2925 * @priv: driver private structure
2927 * This inits the coalesce parameters: i.e. timer rate,
2928 * timer handler and default threshold used for enabling the
2929 * interrupt on completion bit.
2931 static void stmmac_init_coalesce(struct stmmac_priv *priv)
2933 u32 tx_channel_count = priv->plat->tx_queues_to_use;
2934 u32 rx_channel_count = priv->plat->rx_queues_to_use;
2937 for (chan = 0; chan < tx_channel_count; chan++) {
2938 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2940 priv->tx_coal_frames[chan] = STMMAC_TX_FRAMES;
2941 priv->tx_coal_timer[chan] = STMMAC_COAL_TX_TIMER;
2943 hrtimer_init(&tx_q->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2944 tx_q->txtimer.function = stmmac_tx_timer;
2947 for (chan = 0; chan < rx_channel_count; chan++)
2948 priv->rx_coal_frames[chan] = STMMAC_RX_FRAMES;
2951 static void stmmac_set_rings_length(struct stmmac_priv *priv)
2953 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2954 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2957 /* set TX ring length */
2958 for (chan = 0; chan < tx_channels_count; chan++)
2959 stmmac_set_tx_ring_len(priv, priv->ioaddr,
2960 (priv->dma_tx_size - 1), chan);
2962 /* set RX ring length */
2963 for (chan = 0; chan < rx_channels_count; chan++)
2964 stmmac_set_rx_ring_len(priv, priv->ioaddr,
2965 (priv->dma_rx_size - 1), chan);
2969 * stmmac_set_tx_queue_weight - Set TX queue weight
2970 * @priv: driver private structure
2971 * Description: It is used for setting TX queues weight
2973 static void stmmac_set_tx_queue_weight(struct stmmac_priv *priv)
2975 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2979 for (queue = 0; queue < tx_queues_count; queue++) {
2980 weight = priv->plat->tx_queues_cfg[queue].weight;
2981 stmmac_set_mtl_tx_queue_weight(priv, priv->hw, weight, queue);
2986 * stmmac_configure_cbs - Configure CBS in TX queue
2987 * @priv: driver private structure
2988 * Description: It is used for configuring CBS in AVB TX queues
2990 static void stmmac_configure_cbs(struct stmmac_priv *priv)
2992 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2996 /* queue 0 is reserved for legacy traffic */
2997 for (queue = 1; queue < tx_queues_count; queue++) {
2998 mode_to_use = priv->plat->tx_queues_cfg[queue].mode_to_use;
2999 if (mode_to_use == MTL_QUEUE_DCB)
3002 stmmac_config_cbs(priv, priv->hw,
3003 priv->plat->tx_queues_cfg[queue].send_slope,
3004 priv->plat->tx_queues_cfg[queue].idle_slope,
3005 priv->plat->tx_queues_cfg[queue].high_credit,
3006 priv->plat->tx_queues_cfg[queue].low_credit,
3012 * stmmac_rx_queue_dma_chan_map - Map RX queue to RX dma channel
3013 * @priv: driver private structure
3014 * Description: It is used for mapping RX queues to RX dma channels
3016 static void stmmac_rx_queue_dma_chan_map(struct stmmac_priv *priv)
3018 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3022 for (queue = 0; queue < rx_queues_count; queue++) {
3023 chan = priv->plat->rx_queues_cfg[queue].chan;
3024 stmmac_map_mtl_to_dma(priv, priv->hw, queue, chan);
3029 * stmmac_mac_config_rx_queues_prio - Configure RX Queue priority
3030 * @priv: driver private structure
3031 * Description: It is used for configuring the RX Queue Priority
3033 static void stmmac_mac_config_rx_queues_prio(struct stmmac_priv *priv)
3035 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3039 for (queue = 0; queue < rx_queues_count; queue++) {
3040 if (!priv->plat->rx_queues_cfg[queue].use_prio)
3043 prio = priv->plat->rx_queues_cfg[queue].prio;
3044 stmmac_rx_queue_prio(priv, priv->hw, prio, queue);
3049 * stmmac_mac_config_tx_queues_prio - Configure TX Queue priority
3050 * @priv: driver private structure
3051 * Description: It is used for configuring the TX Queue Priority
3053 static void stmmac_mac_config_tx_queues_prio(struct stmmac_priv *priv)
3055 u32 tx_queues_count = priv->plat->tx_queues_to_use;
3059 for (queue = 0; queue < tx_queues_count; queue++) {
3060 if (!priv->plat->tx_queues_cfg[queue].use_prio)
3063 prio = priv->plat->tx_queues_cfg[queue].prio;
3064 stmmac_tx_queue_prio(priv, priv->hw, prio, queue);
3069 * stmmac_mac_config_rx_queues_routing - Configure RX Queue Routing
3070 * @priv: driver private structure
3071 * Description: It is used for configuring the RX queue routing
3073 static void stmmac_mac_config_rx_queues_routing(struct stmmac_priv *priv)
3075 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3079 for (queue = 0; queue < rx_queues_count; queue++) {
3080 /* no specific packet type routing specified for the queue */
3081 if (priv->plat->rx_queues_cfg[queue].pkt_route == 0x0)
3084 packet = priv->plat->rx_queues_cfg[queue].pkt_route;
3085 stmmac_rx_queue_routing(priv, priv->hw, packet, queue);
3089 static void stmmac_mac_config_rss(struct stmmac_priv *priv)
3091 if (!priv->dma_cap.rssen || !priv->plat->rss_en) {
3092 priv->rss.enable = false;
3096 if (priv->dev->features & NETIF_F_RXHASH)
3097 priv->rss.enable = true;
3099 priv->rss.enable = false;
3101 stmmac_rss_configure(priv, priv->hw, &priv->rss,
3102 priv->plat->rx_queues_to_use);
3106 * stmmac_mtl_configuration - Configure MTL
3107 * @priv: driver private structure
3108 * Description: It is used for configurring MTL
3110 static void stmmac_mtl_configuration(struct stmmac_priv *priv)
3112 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3113 u32 tx_queues_count = priv->plat->tx_queues_to_use;
3115 if (tx_queues_count > 1)
3116 stmmac_set_tx_queue_weight(priv);
3118 /* Configure MTL RX algorithms */
3119 if (rx_queues_count > 1)
3120 stmmac_prog_mtl_rx_algorithms(priv, priv->hw,
3121 priv->plat->rx_sched_algorithm);
3123 /* Configure MTL TX algorithms */
3124 if (tx_queues_count > 1)
3125 stmmac_prog_mtl_tx_algorithms(priv, priv->hw,
3126 priv->plat->tx_sched_algorithm);
3128 /* Configure CBS in AVB TX queues */
3129 if (tx_queues_count > 1)
3130 stmmac_configure_cbs(priv);
3132 /* Map RX MTL to DMA channels */
3133 stmmac_rx_queue_dma_chan_map(priv);
3135 /* Enable MAC RX Queues */
3136 stmmac_mac_enable_rx_queues(priv);
3138 /* Set RX priorities */
3139 if (rx_queues_count > 1)
3140 stmmac_mac_config_rx_queues_prio(priv);
3142 /* Set TX priorities */
3143 if (tx_queues_count > 1)
3144 stmmac_mac_config_tx_queues_prio(priv);
3146 /* Set RX routing */
3147 if (rx_queues_count > 1)
3148 stmmac_mac_config_rx_queues_routing(priv);
3150 /* Receive Side Scaling */
3151 if (rx_queues_count > 1)
3152 stmmac_mac_config_rss(priv);
3155 static void stmmac_safety_feat_configuration(struct stmmac_priv *priv)
3157 if (priv->dma_cap.asp) {
3158 netdev_info(priv->dev, "Enabling Safety Features\n");
3159 stmmac_safety_feat_config(priv, priv->ioaddr, priv->dma_cap.asp,
3160 priv->plat->safety_feat_cfg);
3162 netdev_info(priv->dev, "No Safety Features support found\n");
3166 static int stmmac_fpe_start_wq(struct stmmac_priv *priv)
3170 clear_bit(__FPE_TASK_SCHED, &priv->fpe_task_state);
3171 clear_bit(__FPE_REMOVING, &priv->fpe_task_state);
3173 name = priv->wq_name;
3174 sprintf(name, "%s-fpe", priv->dev->name);
3176 priv->fpe_wq = create_singlethread_workqueue(name);
3177 if (!priv->fpe_wq) {
3178 netdev_err(priv->dev, "%s: Failed to create workqueue\n", name);
3182 netdev_info(priv->dev, "FPE workqueue start");
3188 * stmmac_hw_setup - setup mac in a usable state.
3189 * @dev : pointer to the device structure.
3190 * @ptp_register: register PTP if set
3192 * this is the main function to setup the HW in a usable state because the
3193 * dma engine is reset, the core registers are configured (e.g. AXI,
3194 * Checksum features, timers). The DMA is ready to start receiving and
3197 * 0 on success and an appropriate (-)ve integer as defined in errno.h
3200 static int stmmac_hw_setup(struct net_device *dev, bool ptp_register)
3202 struct stmmac_priv *priv = netdev_priv(dev);
3203 u32 rx_cnt = priv->plat->rx_queues_to_use;
3204 u32 tx_cnt = priv->plat->tx_queues_to_use;
3209 /* DMA initialization and SW reset */
3210 ret = stmmac_init_dma_engine(priv);
3212 netdev_err(priv->dev, "%s: DMA engine initialization failed\n",
3217 /* Copy the MAC addr into the HW */
3218 stmmac_set_umac_addr(priv, priv->hw, dev->dev_addr, 0);
3220 /* PS and related bits will be programmed according to the speed */
3221 if (priv->hw->pcs) {
3222 int speed = priv->plat->mac_port_sel_speed;
3224 if ((speed == SPEED_10) || (speed == SPEED_100) ||
3225 (speed == SPEED_1000)) {
3226 priv->hw->ps = speed;
3228 dev_warn(priv->device, "invalid port speed\n");
3233 /* Initialize the MAC Core */
3234 stmmac_core_init(priv, priv->hw, dev);
3237 stmmac_mtl_configuration(priv);
3239 /* Initialize Safety Features */
3240 stmmac_safety_feat_configuration(priv);
3242 ret = stmmac_rx_ipc(priv, priv->hw);
3244 netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n");
3245 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
3246 priv->hw->rx_csum = 0;
3249 /* Enable the MAC Rx/Tx */
3250 stmmac_mac_set(priv, priv->ioaddr, true);
3252 /* Set the HW DMA mode and the COE */
3253 stmmac_dma_operation_mode(priv);
3255 stmmac_mmc_setup(priv);
3257 ret = stmmac_init_ptp(priv);
3258 if (ret == -EOPNOTSUPP)
3259 netdev_warn(priv->dev, "PTP not supported by HW\n");
3261 netdev_warn(priv->dev, "PTP init failed\n");
3262 else if (ptp_register)
3263 stmmac_ptp_register(priv);
3265 priv->eee_tw_timer = STMMAC_DEFAULT_TWT_LS;
3267 /* Convert the timer from msec to usec */
3268 if (!priv->tx_lpi_timer)
3269 priv->tx_lpi_timer = eee_timer * 1000;
3271 if (priv->use_riwt) {
3274 for (queue = 0; queue < rx_cnt; queue++) {
3275 if (!priv->rx_riwt[queue])
3276 priv->rx_riwt[queue] = DEF_DMA_RIWT;
3278 stmmac_rx_watchdog(priv, priv->ioaddr,
3279 priv->rx_riwt[queue], queue);
3284 stmmac_pcs_ctrl_ane(priv, priv->ioaddr, 1, priv->hw->ps, 0);
3286 /* set TX and RX rings length */
3287 stmmac_set_rings_length(priv);
3291 for (chan = 0; chan < tx_cnt; chan++) {
3292 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
3294 /* TSO and TBS cannot co-exist */
3295 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3298 stmmac_enable_tso(priv, priv->ioaddr, 1, chan);
3302 /* Enable Split Header */
3303 sph_en = (priv->hw->rx_csum > 0) && priv->sph;
3304 for (chan = 0; chan < rx_cnt; chan++)
3305 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
3308 /* VLAN Tag Insertion */
3309 if (priv->dma_cap.vlins)
3310 stmmac_enable_vlan(priv, priv->hw, STMMAC_VLAN_INSERT);
3313 for (chan = 0; chan < tx_cnt; chan++) {
3314 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
3315 int enable = tx_q->tbs & STMMAC_TBS_AVAIL;
3317 stmmac_enable_tbs(priv, priv->ioaddr, enable, chan);
3320 /* Configure real RX and TX queues */
3321 netif_set_real_num_rx_queues(dev, priv->plat->rx_queues_to_use);
3322 netif_set_real_num_tx_queues(dev, priv->plat->tx_queues_to_use);
3324 /* Start the ball rolling... */
3325 stmmac_start_all_dma(priv);
3327 if (priv->dma_cap.fpesel) {
3328 stmmac_fpe_start_wq(priv);
3330 if (priv->plat->fpe_cfg->enable)
3331 stmmac_fpe_handshake(priv, true);
3337 static void stmmac_hw_teardown(struct net_device *dev)
3339 struct stmmac_priv *priv = netdev_priv(dev);
3341 clk_disable_unprepare(priv->plat->clk_ptp_ref);
3344 static void stmmac_free_irq(struct net_device *dev,
3345 enum request_irq_err irq_err, int irq_idx)
3347 struct stmmac_priv *priv = netdev_priv(dev);
3351 case REQ_IRQ_ERR_ALL:
3352 irq_idx = priv->plat->tx_queues_to_use;
3354 case REQ_IRQ_ERR_TX:
3355 for (j = irq_idx - 1; j >= 0; j--) {
3356 if (priv->tx_irq[j] > 0) {
3357 irq_set_affinity_hint(priv->tx_irq[j], NULL);
3358 free_irq(priv->tx_irq[j], &priv->tx_queue[j]);
3361 irq_idx = priv->plat->rx_queues_to_use;
3363 case REQ_IRQ_ERR_RX:
3364 for (j = irq_idx - 1; j >= 0; j--) {
3365 if (priv->rx_irq[j] > 0) {
3366 irq_set_affinity_hint(priv->rx_irq[j], NULL);
3367 free_irq(priv->rx_irq[j], &priv->rx_queue[j]);
3371 if (priv->sfty_ue_irq > 0 && priv->sfty_ue_irq != dev->irq)
3372 free_irq(priv->sfty_ue_irq, dev);
3374 case REQ_IRQ_ERR_SFTY_UE:
3375 if (priv->sfty_ce_irq > 0 && priv->sfty_ce_irq != dev->irq)
3376 free_irq(priv->sfty_ce_irq, dev);
3378 case REQ_IRQ_ERR_SFTY_CE:
3379 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq)
3380 free_irq(priv->lpi_irq, dev);
3382 case REQ_IRQ_ERR_LPI:
3383 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq)
3384 free_irq(priv->wol_irq, dev);
3386 case REQ_IRQ_ERR_WOL:
3387 free_irq(dev->irq, dev);
3389 case REQ_IRQ_ERR_MAC:
3390 case REQ_IRQ_ERR_NO:
3391 /* If MAC IRQ request error, no more IRQ to free */
3396 static int stmmac_request_irq_multi_msi(struct net_device *dev)
3398 struct stmmac_priv *priv = netdev_priv(dev);
3399 enum request_irq_err irq_err;
3406 /* For common interrupt */
3407 int_name = priv->int_name_mac;
3408 sprintf(int_name, "%s:%s", dev->name, "mac");
3409 ret = request_irq(dev->irq, stmmac_mac_interrupt,
3411 if (unlikely(ret < 0)) {
3412 netdev_err(priv->dev,
3413 "%s: alloc mac MSI %d (error: %d)\n",
3414 __func__, dev->irq, ret);
3415 irq_err = REQ_IRQ_ERR_MAC;
3419 /* Request the Wake IRQ in case of another line
3422 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq) {
3423 int_name = priv->int_name_wol;
3424 sprintf(int_name, "%s:%s", dev->name, "wol");
3425 ret = request_irq(priv->wol_irq,
3426 stmmac_mac_interrupt,
3428 if (unlikely(ret < 0)) {
3429 netdev_err(priv->dev,
3430 "%s: alloc wol MSI %d (error: %d)\n",
3431 __func__, priv->wol_irq, ret);
3432 irq_err = REQ_IRQ_ERR_WOL;
3437 /* Request the LPI IRQ in case of another line
3440 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq) {
3441 int_name = priv->int_name_lpi;
3442 sprintf(int_name, "%s:%s", dev->name, "lpi");
3443 ret = request_irq(priv->lpi_irq,
3444 stmmac_mac_interrupt,
3446 if (unlikely(ret < 0)) {
3447 netdev_err(priv->dev,
3448 "%s: alloc lpi MSI %d (error: %d)\n",
3449 __func__, priv->lpi_irq, ret);
3450 irq_err = REQ_IRQ_ERR_LPI;
3455 /* Request the Safety Feature Correctible Error line in
3456 * case of another line is used
3458 if (priv->sfty_ce_irq > 0 && priv->sfty_ce_irq != dev->irq) {
3459 int_name = priv->int_name_sfty_ce;
3460 sprintf(int_name, "%s:%s", dev->name, "safety-ce");
3461 ret = request_irq(priv->sfty_ce_irq,
3462 stmmac_safety_interrupt,
3464 if (unlikely(ret < 0)) {
3465 netdev_err(priv->dev,
3466 "%s: alloc sfty ce MSI %d (error: %d)\n",
3467 __func__, priv->sfty_ce_irq, ret);
3468 irq_err = REQ_IRQ_ERR_SFTY_CE;
3473 /* Request the Safety Feature Uncorrectible Error line in
3474 * case of another line is used
3476 if (priv->sfty_ue_irq > 0 && priv->sfty_ue_irq != dev->irq) {
3477 int_name = priv->int_name_sfty_ue;
3478 sprintf(int_name, "%s:%s", dev->name, "safety-ue");
3479 ret = request_irq(priv->sfty_ue_irq,
3480 stmmac_safety_interrupt,
3482 if (unlikely(ret < 0)) {
3483 netdev_err(priv->dev,
3484 "%s: alloc sfty ue MSI %d (error: %d)\n",
3485 __func__, priv->sfty_ue_irq, ret);
3486 irq_err = REQ_IRQ_ERR_SFTY_UE;
3491 /* Request Rx MSI irq */
3492 for (i = 0; i < priv->plat->rx_queues_to_use; i++) {
3493 if (i >= MTL_MAX_RX_QUEUES)
3495 if (priv->rx_irq[i] == 0)
3498 int_name = priv->int_name_rx_irq[i];
3499 sprintf(int_name, "%s:%s-%d", dev->name, "rx", i);
3500 ret = request_irq(priv->rx_irq[i],
3502 0, int_name, &priv->rx_queue[i]);
3503 if (unlikely(ret < 0)) {
3504 netdev_err(priv->dev,
3505 "%s: alloc rx-%d MSI %d (error: %d)\n",
3506 __func__, i, priv->rx_irq[i], ret);
3507 irq_err = REQ_IRQ_ERR_RX;
3511 cpumask_clear(&cpu_mask);
3512 cpumask_set_cpu(i % num_online_cpus(), &cpu_mask);
3513 irq_set_affinity_hint(priv->rx_irq[i], &cpu_mask);
3516 /* Request Tx MSI irq */
3517 for (i = 0; i < priv->plat->tx_queues_to_use; i++) {
3518 if (i >= MTL_MAX_TX_QUEUES)
3520 if (priv->tx_irq[i] == 0)
3523 int_name = priv->int_name_tx_irq[i];
3524 sprintf(int_name, "%s:%s-%d", dev->name, "tx", i);
3525 ret = request_irq(priv->tx_irq[i],
3527 0, int_name, &priv->tx_queue[i]);
3528 if (unlikely(ret < 0)) {
3529 netdev_err(priv->dev,
3530 "%s: alloc tx-%d MSI %d (error: %d)\n",
3531 __func__, i, priv->tx_irq[i], ret);
3532 irq_err = REQ_IRQ_ERR_TX;
3536 cpumask_clear(&cpu_mask);
3537 cpumask_set_cpu(i % num_online_cpus(), &cpu_mask);
3538 irq_set_affinity_hint(priv->tx_irq[i], &cpu_mask);
3544 stmmac_free_irq(dev, irq_err, irq_idx);
3548 static int stmmac_request_irq_single(struct net_device *dev)
3550 struct stmmac_priv *priv = netdev_priv(dev);
3551 enum request_irq_err irq_err;
3554 ret = request_irq(dev->irq, stmmac_interrupt,
3555 IRQF_SHARED, dev->name, dev);
3556 if (unlikely(ret < 0)) {
3557 netdev_err(priv->dev,
3558 "%s: ERROR: allocating the IRQ %d (error: %d)\n",
3559 __func__, dev->irq, ret);
3560 irq_err = REQ_IRQ_ERR_MAC;
3564 /* Request the Wake IRQ in case of another line
3567 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq) {
3568 ret = request_irq(priv->wol_irq, stmmac_interrupt,
3569 IRQF_SHARED, dev->name, dev);
3570 if (unlikely(ret < 0)) {
3571 netdev_err(priv->dev,
3572 "%s: ERROR: allocating the WoL IRQ %d (%d)\n",
3573 __func__, priv->wol_irq, ret);
3574 irq_err = REQ_IRQ_ERR_WOL;
3579 /* Request the IRQ lines */
3580 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq) {
3581 ret = request_irq(priv->lpi_irq, stmmac_interrupt,
3582 IRQF_SHARED, dev->name, dev);
3583 if (unlikely(ret < 0)) {
3584 netdev_err(priv->dev,
3585 "%s: ERROR: allocating the LPI IRQ %d (%d)\n",
3586 __func__, priv->lpi_irq, ret);
3587 irq_err = REQ_IRQ_ERR_LPI;
3595 stmmac_free_irq(dev, irq_err, 0);
3599 static int stmmac_request_irq(struct net_device *dev)
3601 struct stmmac_priv *priv = netdev_priv(dev);
3604 /* Request the IRQ lines */
3605 if (priv->plat->multi_msi_en)
3606 ret = stmmac_request_irq_multi_msi(dev);
3608 ret = stmmac_request_irq_single(dev);
3614 * stmmac_open - open entry point of the driver
3615 * @dev : pointer to the device structure.
3617 * This function is the open entry point of the driver.
3619 * 0 on success and an appropriate (-)ve integer as defined in errno.h
3622 static int stmmac_open(struct net_device *dev)
3624 struct stmmac_priv *priv = netdev_priv(dev);
3625 int mode = priv->plat->phy_interface;
3630 ret = pm_runtime_get_sync(priv->device);
3632 pm_runtime_put_noidle(priv->device);
3636 if (priv->hw->pcs != STMMAC_PCS_TBI &&
3637 priv->hw->pcs != STMMAC_PCS_RTBI &&
3639 xpcs_get_an_mode(priv->hw->xpcs, mode) != DW_AN_C73)) {
3640 ret = stmmac_init_phy(dev);
3642 netdev_err(priv->dev,
3643 "%s: Cannot attach to PHY (error: %d)\n",
3645 goto init_phy_error;
3649 /* Extra statistics */
3650 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
3651 priv->xstats.threshold = tc;
3653 bfsize = stmmac_set_16kib_bfsize(priv, dev->mtu);
3657 if (bfsize < BUF_SIZE_16KiB)
3658 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
3660 priv->dma_buf_sz = bfsize;
3663 priv->rx_copybreak = STMMAC_RX_COPYBREAK;
3665 if (!priv->dma_tx_size)
3666 priv->dma_tx_size = DMA_DEFAULT_TX_SIZE;
3667 if (!priv->dma_rx_size)
3668 priv->dma_rx_size = DMA_DEFAULT_RX_SIZE;
3670 /* Earlier check for TBS */
3671 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++) {
3672 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
3673 int tbs_en = priv->plat->tx_queues_cfg[chan].tbs_en;
3675 /* Setup per-TXQ tbs flag before TX descriptor alloc */
3676 tx_q->tbs |= tbs_en ? STMMAC_TBS_AVAIL : 0;
3679 ret = alloc_dma_desc_resources(priv);
3681 netdev_err(priv->dev, "%s: DMA descriptors allocation failed\n",
3683 goto dma_desc_error;
3686 ret = init_dma_desc_rings(dev, GFP_KERNEL);
3688 netdev_err(priv->dev, "%s: DMA descriptors initialization failed\n",
3693 ret = stmmac_hw_setup(dev, true);
3695 netdev_err(priv->dev, "%s: Hw setup failed\n", __func__);
3699 stmmac_init_coalesce(priv);
3701 phylink_start(priv->phylink);
3702 /* We may have called phylink_speed_down before */
3703 phylink_speed_up(priv->phylink);
3705 ret = stmmac_request_irq(dev);
3709 stmmac_enable_all_queues(priv);
3710 netif_tx_start_all_queues(priv->dev);
3715 phylink_stop(priv->phylink);
3717 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
3718 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
3720 stmmac_hw_teardown(dev);
3722 free_dma_desc_resources(priv);
3724 phylink_disconnect_phy(priv->phylink);
3726 pm_runtime_put(priv->device);
3730 static void stmmac_fpe_stop_wq(struct stmmac_priv *priv)
3732 set_bit(__FPE_REMOVING, &priv->fpe_task_state);
3735 destroy_workqueue(priv->fpe_wq);
3737 netdev_info(priv->dev, "FPE workqueue stop");
3741 * stmmac_release - close entry point of the driver
3742 * @dev : device pointer.
3744 * This is the stop entry point of the driver.
3746 static int stmmac_release(struct net_device *dev)
3748 struct stmmac_priv *priv = netdev_priv(dev);
3751 netif_tx_disable(dev);
3753 if (device_may_wakeup(priv->device))
3754 phylink_speed_down(priv->phylink, false);
3755 /* Stop and disconnect the PHY */
3756 phylink_stop(priv->phylink);
3757 phylink_disconnect_phy(priv->phylink);
3759 stmmac_disable_all_queues(priv);
3761 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
3762 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
3764 /* Free the IRQ lines */
3765 stmmac_free_irq(dev, REQ_IRQ_ERR_ALL, 0);
3767 if (priv->eee_enabled) {
3768 priv->tx_path_in_lpi_mode = false;
3769 del_timer_sync(&priv->eee_ctrl_timer);
3772 /* Stop TX/RX DMA and clear the descriptors */
3773 stmmac_stop_all_dma(priv);
3775 /* Release and free the Rx/Tx resources */
3776 free_dma_desc_resources(priv);
3778 /* Disable the MAC Rx/Tx */
3779 stmmac_mac_set(priv, priv->ioaddr, false);
3781 netif_carrier_off(dev);
3783 stmmac_release_ptp(priv);
3785 pm_runtime_put(priv->device);
3787 if (priv->dma_cap.fpesel)
3788 stmmac_fpe_stop_wq(priv);
3793 static bool stmmac_vlan_insert(struct stmmac_priv *priv, struct sk_buff *skb,
3794 struct stmmac_tx_queue *tx_q)
3796 u16 tag = 0x0, inner_tag = 0x0;
3797 u32 inner_type = 0x0;
3800 if (!priv->dma_cap.vlins)
3802 if (!skb_vlan_tag_present(skb))
3804 if (skb->vlan_proto == htons(ETH_P_8021AD)) {
3805 inner_tag = skb_vlan_tag_get(skb);
3806 inner_type = STMMAC_VLAN_INSERT;
3809 tag = skb_vlan_tag_get(skb);
3811 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3812 p = &tx_q->dma_entx[tx_q->cur_tx].basic;
3814 p = &tx_q->dma_tx[tx_q->cur_tx];
3816 if (stmmac_set_desc_vlan_tag(priv, p, tag, inner_tag, inner_type))
3819 stmmac_set_tx_owner(priv, p);
3820 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
3825 * stmmac_tso_allocator - close entry point of the driver
3826 * @priv: driver private structure
3827 * @des: buffer start address
3828 * @total_len: total length to fill in descriptors
3829 * @last_segment: condition for the last descriptor
3830 * @queue: TX queue index
3832 * This function fills descriptor and request new descriptors according to
3833 * buffer length to fill
3835 static void stmmac_tso_allocator(struct stmmac_priv *priv, dma_addr_t des,
3836 int total_len, bool last_segment, u32 queue)
3838 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
3839 struct dma_desc *desc;
3843 tmp_len = total_len;
3845 while (tmp_len > 0) {
3846 dma_addr_t curr_addr;
3848 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx,
3850 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
3852 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3853 desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
3855 desc = &tx_q->dma_tx[tx_q->cur_tx];
3857 curr_addr = des + (total_len - tmp_len);
3858 if (priv->dma_cap.addr64 <= 32)
3859 desc->des0 = cpu_to_le32(curr_addr);
3861 stmmac_set_desc_addr(priv, desc, curr_addr);
3863 buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ?
3864 TSO_MAX_BUFF_SIZE : tmp_len;
3866 stmmac_prepare_tso_tx_desc(priv, desc, 0, buff_size,
3868 (last_segment) && (tmp_len <= TSO_MAX_BUFF_SIZE),
3871 tmp_len -= TSO_MAX_BUFF_SIZE;
3875 static void stmmac_flush_tx_descriptors(struct stmmac_priv *priv, int queue)
3877 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
3880 if (likely(priv->extend_desc))
3881 desc_size = sizeof(struct dma_extended_desc);
3882 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
3883 desc_size = sizeof(struct dma_edesc);
3885 desc_size = sizeof(struct dma_desc);
3887 /* The own bit must be the latest setting done when prepare the
3888 * descriptor and then barrier is needed to make sure that
3889 * all is coherent before granting the DMA engine.
3893 tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * desc_size);
3894 stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue);
3898 * stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO)
3899 * @skb : the socket buffer
3900 * @dev : device pointer
3901 * Description: this is the transmit function that is called on TSO frames
3902 * (support available on GMAC4 and newer chips).
3903 * Diagram below show the ring programming in case of TSO frames:
3907 * | DES0 |---> buffer1 = L2/L3/L4 header
3908 * | DES1 |---> TCP Payload (can continue on next descr...)
3909 * | DES2 |---> buffer 1 and 2 len
3910 * | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0]
3916 * | DES0 | --| Split TCP Payload on Buffers 1 and 2
3918 * | DES2 | --> buffer 1 and 2 len
3922 * mss is fixed when enable tso, so w/o programming the TDES3 ctx field.
3924 static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev)
3926 struct dma_desc *desc, *first, *mss_desc = NULL;
3927 struct stmmac_priv *priv = netdev_priv(dev);
3928 int nfrags = skb_shinfo(skb)->nr_frags;
3929 u32 queue = skb_get_queue_mapping(skb);
3930 unsigned int first_entry, tx_packets;
3931 int tmp_pay_len = 0, first_tx;
3932 struct stmmac_tx_queue *tx_q;
3933 bool has_vlan, set_ic;
3934 u8 proto_hdr_len, hdr;
3939 tx_q = &priv->tx_queue[queue];
3940 first_tx = tx_q->cur_tx;
3942 /* Compute header lengths */
3943 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
3944 proto_hdr_len = skb_transport_offset(skb) + sizeof(struct udphdr);
3945 hdr = sizeof(struct udphdr);
3947 proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
3948 hdr = tcp_hdrlen(skb);
3951 /* Desc availability based on threshold should be enough safe */
3952 if (unlikely(stmmac_tx_avail(priv, queue) <
3953 (((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) {
3954 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
3955 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
3957 /* This is a hard error, log it. */
3958 netdev_err(priv->dev,
3959 "%s: Tx Ring full when queue awake\n",
3962 return NETDEV_TX_BUSY;
3965 pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */
3967 mss = skb_shinfo(skb)->gso_size;
3969 /* set new MSS value if needed */
3970 if (mss != tx_q->mss) {
3971 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3972 mss_desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
3974 mss_desc = &tx_q->dma_tx[tx_q->cur_tx];
3976 stmmac_set_mss(priv, mss_desc, mss);
3978 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx,
3980 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
3983 if (netif_msg_tx_queued(priv)) {
3984 pr_info("%s: hdrlen %d, hdr_len %d, pay_len %d, mss %d\n",
3985 __func__, hdr, proto_hdr_len, pay_len, mss);
3986 pr_info("\tskb->len %d, skb->data_len %d\n", skb->len,
3990 /* Check if VLAN can be inserted by HW */
3991 has_vlan = stmmac_vlan_insert(priv, skb, tx_q);
3993 first_entry = tx_q->cur_tx;
3994 WARN_ON(tx_q->tx_skbuff[first_entry]);
3996 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3997 desc = &tx_q->dma_entx[first_entry].basic;
3999 desc = &tx_q->dma_tx[first_entry];
4003 stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT);
4005 /* first descriptor: fill Headers on Buf1 */
4006 des = dma_map_single(priv->device, skb->data, skb_headlen(skb),
4008 if (dma_mapping_error(priv->device, des))
4011 tx_q->tx_skbuff_dma[first_entry].buf = des;
4012 tx_q->tx_skbuff_dma[first_entry].len = skb_headlen(skb);
4013 tx_q->tx_skbuff_dma[first_entry].map_as_page = false;
4014 tx_q->tx_skbuff_dma[first_entry].buf_type = STMMAC_TXBUF_T_SKB;
4016 if (priv->dma_cap.addr64 <= 32) {
4017 first->des0 = cpu_to_le32(des);
4019 /* Fill start of payload in buff2 of first descriptor */
4021 first->des1 = cpu_to_le32(des + proto_hdr_len);
4023 /* If needed take extra descriptors to fill the remaining payload */
4024 tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE;
4026 stmmac_set_desc_addr(priv, first, des);
4027 tmp_pay_len = pay_len;
4028 des += proto_hdr_len;
4032 stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0), queue);
4034 /* Prepare fragments */
4035 for (i = 0; i < nfrags; i++) {
4036 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4038 des = skb_frag_dma_map(priv->device, frag, 0,
4039 skb_frag_size(frag),
4041 if (dma_mapping_error(priv->device, des))
4044 stmmac_tso_allocator(priv, des, skb_frag_size(frag),
4045 (i == nfrags - 1), queue);
4047 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf = des;
4048 tx_q->tx_skbuff_dma[tx_q->cur_tx].len = skb_frag_size(frag);
4049 tx_q->tx_skbuff_dma[tx_q->cur_tx].map_as_page = true;
4050 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf_type = STMMAC_TXBUF_T_SKB;
4053 tx_q->tx_skbuff_dma[tx_q->cur_tx].last_segment = true;
4055 /* Only the last descriptor gets to point to the skb. */
4056 tx_q->tx_skbuff[tx_q->cur_tx] = skb;
4057 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf_type = STMMAC_TXBUF_T_SKB;
4059 /* Manage tx mitigation */
4060 tx_packets = (tx_q->cur_tx + 1) - first_tx;
4061 tx_q->tx_count_frames += tx_packets;
4063 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en)
4065 else if (!priv->tx_coal_frames[queue])
4067 else if (tx_packets > priv->tx_coal_frames[queue])
4069 else if ((tx_q->tx_count_frames %
4070 priv->tx_coal_frames[queue]) < tx_packets)
4076 if (tx_q->tbs & STMMAC_TBS_AVAIL)
4077 desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
4079 desc = &tx_q->dma_tx[tx_q->cur_tx];
4081 tx_q->tx_count_frames = 0;
4082 stmmac_set_tx_ic(priv, desc);
4083 priv->xstats.tx_set_ic_bit++;
4086 /* We've used all descriptors we need for this skb, however,
4087 * advance cur_tx so that it references a fresh descriptor.
4088 * ndo_start_xmit will fill this descriptor the next time it's
4089 * called and stmmac_tx_clean may clean up to this descriptor.
4091 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
4093 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
4094 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
4096 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
4099 dev->stats.tx_bytes += skb->len;
4100 priv->xstats.tx_tso_frames++;
4101 priv->xstats.tx_tso_nfrags += nfrags;
4103 if (priv->sarc_type)
4104 stmmac_set_desc_sarc(priv, first, priv->sarc_type);
4106 skb_tx_timestamp(skb);
4108 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4109 priv->hwts_tx_en)) {
4110 /* declare that device is doing timestamping */
4111 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4112 stmmac_enable_tx_timestamp(priv, first);
4115 /* Complete the first descriptor before granting the DMA */
4116 stmmac_prepare_tso_tx_desc(priv, first, 1,
4119 1, tx_q->tx_skbuff_dma[first_entry].last_segment,
4120 hdr / 4, (skb->len - proto_hdr_len));
4122 /* If context desc is used to change MSS */
4124 /* Make sure that first descriptor has been completely
4125 * written, including its own bit. This is because MSS is
4126 * actually before first descriptor, so we need to make
4127 * sure that MSS's own bit is the last thing written.
4130 stmmac_set_tx_owner(priv, mss_desc);
4133 if (netif_msg_pktdata(priv)) {
4134 pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n",
4135 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
4136 tx_q->cur_tx, first, nfrags);
4137 pr_info(">>> frame to be transmitted: ");
4138 print_pkt(skb->data, skb_headlen(skb));
4141 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
4143 stmmac_flush_tx_descriptors(priv, queue);
4144 stmmac_tx_timer_arm(priv, queue);
4146 return NETDEV_TX_OK;
4149 dev_err(priv->device, "Tx dma map failed\n");
4151 priv->dev->stats.tx_dropped++;
4152 return NETDEV_TX_OK;
4156 * stmmac_xmit - Tx entry point of the driver
4157 * @skb : the socket buffer
4158 * @dev : device pointer
4159 * Description : this is the tx entry point of the driver.
4160 * It programs the chain or the ring and supports oversized frames
4163 static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
4165 unsigned int first_entry, tx_packets, enh_desc;
4166 struct stmmac_priv *priv = netdev_priv(dev);
4167 unsigned int nopaged_len = skb_headlen(skb);
4168 int i, csum_insertion = 0, is_jumbo = 0;
4169 u32 queue = skb_get_queue_mapping(skb);
4170 int nfrags = skb_shinfo(skb)->nr_frags;
4171 int gso = skb_shinfo(skb)->gso_type;
4172 struct dma_edesc *tbs_desc = NULL;
4173 struct dma_desc *desc, *first;
4174 struct stmmac_tx_queue *tx_q;
4175 bool has_vlan, set_ic;
4176 int entry, first_tx;
4179 tx_q = &priv->tx_queue[queue];
4180 first_tx = tx_q->cur_tx;
4182 if (priv->tx_path_in_lpi_mode && priv->eee_sw_timer_en)
4183 stmmac_disable_eee_mode(priv);
4185 /* Manage oversized TCP frames for GMAC4 device */
4186 if (skb_is_gso(skb) && priv->tso) {
4187 if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
4188 return stmmac_tso_xmit(skb, dev);
4189 if (priv->plat->has_gmac4 && (gso & SKB_GSO_UDP_L4))
4190 return stmmac_tso_xmit(skb, dev);
4193 if (unlikely(stmmac_tx_avail(priv, queue) < nfrags + 1)) {
4194 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
4195 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
4197 /* This is a hard error, log it. */
4198 netdev_err(priv->dev,
4199 "%s: Tx Ring full when queue awake\n",
4202 return NETDEV_TX_BUSY;
4205 /* Check if VLAN can be inserted by HW */
4206 has_vlan = stmmac_vlan_insert(priv, skb, tx_q);
4208 entry = tx_q->cur_tx;
4209 first_entry = entry;
4210 WARN_ON(tx_q->tx_skbuff[first_entry]);
4212 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
4214 if (likely(priv->extend_desc))
4215 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
4216 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4217 desc = &tx_q->dma_entx[entry].basic;
4219 desc = tx_q->dma_tx + entry;
4224 stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT);
4226 enh_desc = priv->plat->enh_desc;
4227 /* To program the descriptors according to the size of the frame */
4229 is_jumbo = stmmac_is_jumbo_frm(priv, skb->len, enh_desc);
4231 if (unlikely(is_jumbo)) {
4232 entry = stmmac_jumbo_frm(priv, tx_q, skb, csum_insertion);
4233 if (unlikely(entry < 0) && (entry != -EINVAL))
4237 for (i = 0; i < nfrags; i++) {
4238 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4239 int len = skb_frag_size(frag);
4240 bool last_segment = (i == (nfrags - 1));
4242 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
4243 WARN_ON(tx_q->tx_skbuff[entry]);
4245 if (likely(priv->extend_desc))
4246 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
4247 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4248 desc = &tx_q->dma_entx[entry].basic;
4250 desc = tx_q->dma_tx + entry;
4252 des = skb_frag_dma_map(priv->device, frag, 0, len,
4254 if (dma_mapping_error(priv->device, des))
4255 goto dma_map_err; /* should reuse desc w/o issues */
4257 tx_q->tx_skbuff_dma[entry].buf = des;
4259 stmmac_set_desc_addr(priv, desc, des);
4261 tx_q->tx_skbuff_dma[entry].map_as_page = true;
4262 tx_q->tx_skbuff_dma[entry].len = len;
4263 tx_q->tx_skbuff_dma[entry].last_segment = last_segment;
4264 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_SKB;
4266 /* Prepare the descriptor and set the own bit too */
4267 stmmac_prepare_tx_desc(priv, desc, 0, len, csum_insertion,
4268 priv->mode, 1, last_segment, skb->len);
4271 /* Only the last descriptor gets to point to the skb. */
4272 tx_q->tx_skbuff[entry] = skb;
4273 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_SKB;
4275 /* According to the coalesce parameter the IC bit for the latest
4276 * segment is reset and the timer re-started to clean the tx status.
4277 * This approach takes care about the fragments: desc is the first
4278 * element in case of no SG.
4280 tx_packets = (entry + 1) - first_tx;
4281 tx_q->tx_count_frames += tx_packets;
4283 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en)
4285 else if (!priv->tx_coal_frames[queue])
4287 else if (tx_packets > priv->tx_coal_frames[queue])
4289 else if ((tx_q->tx_count_frames %
4290 priv->tx_coal_frames[queue]) < tx_packets)
4296 if (likely(priv->extend_desc))
4297 desc = &tx_q->dma_etx[entry].basic;
4298 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4299 desc = &tx_q->dma_entx[entry].basic;
4301 desc = &tx_q->dma_tx[entry];
4303 tx_q->tx_count_frames = 0;
4304 stmmac_set_tx_ic(priv, desc);
4305 priv->xstats.tx_set_ic_bit++;
4308 /* We've used all descriptors we need for this skb, however,
4309 * advance cur_tx so that it references a fresh descriptor.
4310 * ndo_start_xmit will fill this descriptor the next time it's
4311 * called and stmmac_tx_clean may clean up to this descriptor.
4313 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
4314 tx_q->cur_tx = entry;
4316 if (netif_msg_pktdata(priv)) {
4317 netdev_dbg(priv->dev,
4318 "%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
4319 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
4320 entry, first, nfrags);
4322 netdev_dbg(priv->dev, ">>> frame to be transmitted: ");
4323 print_pkt(skb->data, skb->len);
4326 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
4327 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
4329 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
4332 dev->stats.tx_bytes += skb->len;
4334 if (priv->sarc_type)
4335 stmmac_set_desc_sarc(priv, first, priv->sarc_type);
4337 skb_tx_timestamp(skb);
4339 /* Ready to fill the first descriptor and set the OWN bit w/o any
4340 * problems because all the descriptors are actually ready to be
4341 * passed to the DMA engine.
4343 if (likely(!is_jumbo)) {
4344 bool last_segment = (nfrags == 0);
4346 des = dma_map_single(priv->device, skb->data,
4347 nopaged_len, DMA_TO_DEVICE);
4348 if (dma_mapping_error(priv->device, des))
4351 tx_q->tx_skbuff_dma[first_entry].buf = des;
4352 tx_q->tx_skbuff_dma[first_entry].buf_type = STMMAC_TXBUF_T_SKB;
4353 tx_q->tx_skbuff_dma[first_entry].map_as_page = false;
4355 stmmac_set_desc_addr(priv, first, des);
4357 tx_q->tx_skbuff_dma[first_entry].len = nopaged_len;
4358 tx_q->tx_skbuff_dma[first_entry].last_segment = last_segment;
4360 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4361 priv->hwts_tx_en)) {
4362 /* declare that device is doing timestamping */
4363 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4364 stmmac_enable_tx_timestamp(priv, first);
4367 /* Prepare the first descriptor setting the OWN bit too */
4368 stmmac_prepare_tx_desc(priv, first, 1, nopaged_len,
4369 csum_insertion, priv->mode, 0, last_segment,
4373 if (tx_q->tbs & STMMAC_TBS_EN) {
4374 struct timespec64 ts = ns_to_timespec64(skb->tstamp);
4376 tbs_desc = &tx_q->dma_entx[first_entry];
4377 stmmac_set_desc_tbs(priv, tbs_desc, ts.tv_sec, ts.tv_nsec);
4380 stmmac_set_tx_owner(priv, first);
4382 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
4384 stmmac_enable_dma_transmission(priv, priv->ioaddr);
4386 stmmac_flush_tx_descriptors(priv, queue);
4387 stmmac_tx_timer_arm(priv, queue);
4389 return NETDEV_TX_OK;
4392 netdev_err(priv->dev, "Tx DMA map failed\n");
4394 priv->dev->stats.tx_dropped++;
4395 return NETDEV_TX_OK;
4398 static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
4400 struct vlan_ethhdr *veth;
4404 veth = (struct vlan_ethhdr *)skb->data;
4405 vlan_proto = veth->h_vlan_proto;
4407 if ((vlan_proto == htons(ETH_P_8021Q) &&
4408 dev->features & NETIF_F_HW_VLAN_CTAG_RX) ||
4409 (vlan_proto == htons(ETH_P_8021AD) &&
4410 dev->features & NETIF_F_HW_VLAN_STAG_RX)) {
4411 /* pop the vlan tag */
4412 vlanid = ntohs(veth->h_vlan_TCI);
4413 memmove(skb->data + VLAN_HLEN, veth, ETH_ALEN * 2);
4414 skb_pull(skb, VLAN_HLEN);
4415 __vlan_hwaccel_put_tag(skb, vlan_proto, vlanid);
4420 * stmmac_rx_refill - refill used skb preallocated buffers
4421 * @priv: driver private structure
4422 * @queue: RX queue index
4423 * Description : this is to reallocate the skb for the reception process
4424 * that is based on zero-copy.
4426 static inline void stmmac_rx_refill(struct stmmac_priv *priv, u32 queue)
4428 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4429 int dirty = stmmac_rx_dirty(priv, queue);
4430 unsigned int entry = rx_q->dirty_rx;
4431 gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
4433 if (priv->dma_cap.addr64 <= 32)
4436 while (dirty-- > 0) {
4437 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry];
4441 if (priv->extend_desc)
4442 p = (struct dma_desc *)(rx_q->dma_erx + entry);
4444 p = rx_q->dma_rx + entry;
4447 buf->page = page_pool_alloc_pages(rx_q->page_pool, gfp);
4452 if (priv->sph && !buf->sec_page) {
4453 buf->sec_page = page_pool_alloc_pages(rx_q->page_pool, gfp);
4457 buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
4460 buf->addr = page_pool_get_dma_addr(buf->page) + buf->page_offset;
4462 stmmac_set_desc_addr(priv, p, buf->addr);
4464 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true);
4466 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false);
4467 stmmac_refill_desc3(priv, rx_q, p);
4469 rx_q->rx_count_frames++;
4470 rx_q->rx_count_frames += priv->rx_coal_frames[queue];
4471 if (rx_q->rx_count_frames > priv->rx_coal_frames[queue])
4472 rx_q->rx_count_frames = 0;
4474 use_rx_wd = !priv->rx_coal_frames[queue];
4475 use_rx_wd |= rx_q->rx_count_frames > 0;
4476 if (!priv->use_riwt)
4480 stmmac_set_rx_owner(priv, p, use_rx_wd);
4482 entry = STMMAC_GET_ENTRY(entry, priv->dma_rx_size);
4484 rx_q->dirty_rx = entry;
4485 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
4486 (rx_q->dirty_rx * sizeof(struct dma_desc));
4487 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue);
4490 static unsigned int stmmac_rx_buf1_len(struct stmmac_priv *priv,
4492 int status, unsigned int len)
4494 unsigned int plen = 0, hlen = 0;
4495 int coe = priv->hw->rx_csum;
4497 /* Not first descriptor, buffer is always zero */
4498 if (priv->sph && len)
4501 /* First descriptor, get split header length */
4502 stmmac_get_rx_header_len(priv, p, &hlen);
4503 if (priv->sph && hlen) {
4504 priv->xstats.rx_split_hdr_pkt_n++;
4508 /* First descriptor, not last descriptor and not split header */
4509 if (status & rx_not_ls)
4510 return priv->dma_buf_sz;
4512 plen = stmmac_get_rx_frame_len(priv, p, coe);
4514 /* First descriptor and last descriptor and not split header */
4515 return min_t(unsigned int, priv->dma_buf_sz, plen);
4518 static unsigned int stmmac_rx_buf2_len(struct stmmac_priv *priv,
4520 int status, unsigned int len)
4522 int coe = priv->hw->rx_csum;
4523 unsigned int plen = 0;
4525 /* Not split header, buffer is not available */
4529 /* Not last descriptor */
4530 if (status & rx_not_ls)
4531 return priv->dma_buf_sz;
4533 plen = stmmac_get_rx_frame_len(priv, p, coe);
4535 /* Last descriptor */
4539 static int stmmac_xdp_xmit_xdpf(struct stmmac_priv *priv, int queue,
4540 struct xdp_frame *xdpf, bool dma_map)
4542 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
4543 unsigned int entry = tx_q->cur_tx;
4544 struct dma_desc *tx_desc;
4545 dma_addr_t dma_addr;
4548 if (stmmac_tx_avail(priv, queue) < STMMAC_TX_THRESH(priv))
4549 return STMMAC_XDP_CONSUMED;
4551 if (likely(priv->extend_desc))
4552 tx_desc = (struct dma_desc *)(tx_q->dma_etx + entry);
4553 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4554 tx_desc = &tx_q->dma_entx[entry].basic;
4556 tx_desc = tx_q->dma_tx + entry;
4559 dma_addr = dma_map_single(priv->device, xdpf->data,
4560 xdpf->len, DMA_TO_DEVICE);
4561 if (dma_mapping_error(priv->device, dma_addr))
4562 return STMMAC_XDP_CONSUMED;
4564 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XDP_NDO;
4566 struct page *page = virt_to_page(xdpf->data);
4568 dma_addr = page_pool_get_dma_addr(page) + sizeof(*xdpf) +
4570 dma_sync_single_for_device(priv->device, dma_addr,
4571 xdpf->len, DMA_BIDIRECTIONAL);
4573 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XDP_TX;
4576 tx_q->tx_skbuff_dma[entry].buf = dma_addr;
4577 tx_q->tx_skbuff_dma[entry].map_as_page = false;
4578 tx_q->tx_skbuff_dma[entry].len = xdpf->len;
4579 tx_q->tx_skbuff_dma[entry].last_segment = true;
4580 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
4582 tx_q->xdpf[entry] = xdpf;
4584 stmmac_set_desc_addr(priv, tx_desc, dma_addr);
4586 stmmac_prepare_tx_desc(priv, tx_desc, 1, xdpf->len,
4587 true, priv->mode, true, true,
4590 tx_q->tx_count_frames++;
4592 if (tx_q->tx_count_frames % priv->tx_coal_frames[queue] == 0)
4598 tx_q->tx_count_frames = 0;
4599 stmmac_set_tx_ic(priv, tx_desc);
4600 priv->xstats.tx_set_ic_bit++;
4603 stmmac_enable_dma_transmission(priv, priv->ioaddr);
4605 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
4606 tx_q->cur_tx = entry;
4608 return STMMAC_XDP_TX;
4611 static int stmmac_xdp_get_tx_queue(struct stmmac_priv *priv,
4616 if (unlikely(index < 0))
4619 while (index >= priv->plat->tx_queues_to_use)
4620 index -= priv->plat->tx_queues_to_use;
4625 static int stmmac_xdp_xmit_back(struct stmmac_priv *priv,
4626 struct xdp_buff *xdp)
4628 struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp);
4629 int cpu = smp_processor_id();
4630 struct netdev_queue *nq;
4634 if (unlikely(!xdpf))
4635 return STMMAC_XDP_CONSUMED;
4637 queue = stmmac_xdp_get_tx_queue(priv, cpu);
4638 nq = netdev_get_tx_queue(priv->dev, queue);
4640 __netif_tx_lock(nq, cpu);
4641 /* Avoids TX time-out as we are sharing with slow path */
4642 txq_trans_cond_update(nq);
4644 res = stmmac_xdp_xmit_xdpf(priv, queue, xdpf, false);
4645 if (res == STMMAC_XDP_TX)
4646 stmmac_flush_tx_descriptors(priv, queue);
4648 __netif_tx_unlock(nq);
4653 static int __stmmac_xdp_run_prog(struct stmmac_priv *priv,
4654 struct bpf_prog *prog,
4655 struct xdp_buff *xdp)
4660 act = bpf_prog_run_xdp(prog, xdp);
4663 res = STMMAC_XDP_PASS;
4666 res = stmmac_xdp_xmit_back(priv, xdp);
4669 if (xdp_do_redirect(priv->dev, xdp, prog) < 0)
4670 res = STMMAC_XDP_CONSUMED;
4672 res = STMMAC_XDP_REDIRECT;
4675 bpf_warn_invalid_xdp_action(priv->dev, prog, act);
4678 trace_xdp_exception(priv->dev, prog, act);
4681 res = STMMAC_XDP_CONSUMED;
4688 static struct sk_buff *stmmac_xdp_run_prog(struct stmmac_priv *priv,
4689 struct xdp_buff *xdp)
4691 struct bpf_prog *prog;
4694 prog = READ_ONCE(priv->xdp_prog);
4696 res = STMMAC_XDP_PASS;
4700 res = __stmmac_xdp_run_prog(priv, prog, xdp);
4702 return ERR_PTR(-res);
4705 static void stmmac_finalize_xdp_rx(struct stmmac_priv *priv,
4708 int cpu = smp_processor_id();
4711 queue = stmmac_xdp_get_tx_queue(priv, cpu);
4713 if (xdp_status & STMMAC_XDP_TX)
4714 stmmac_tx_timer_arm(priv, queue);
4716 if (xdp_status & STMMAC_XDP_REDIRECT)
4720 static struct sk_buff *stmmac_construct_skb_zc(struct stmmac_channel *ch,
4721 struct xdp_buff *xdp)
4723 unsigned int metasize = xdp->data - xdp->data_meta;
4724 unsigned int datasize = xdp->data_end - xdp->data;
4725 struct sk_buff *skb;
4727 skb = __napi_alloc_skb(&ch->rxtx_napi,
4728 xdp->data_end - xdp->data_hard_start,
4729 GFP_ATOMIC | __GFP_NOWARN);
4733 skb_reserve(skb, xdp->data - xdp->data_hard_start);
4734 memcpy(__skb_put(skb, datasize), xdp->data, datasize);
4736 skb_metadata_set(skb, metasize);
4741 static void stmmac_dispatch_skb_zc(struct stmmac_priv *priv, u32 queue,
4742 struct dma_desc *p, struct dma_desc *np,
4743 struct xdp_buff *xdp)
4745 struct stmmac_channel *ch = &priv->channel[queue];
4746 unsigned int len = xdp->data_end - xdp->data;
4747 enum pkt_hash_types hash_type;
4748 int coe = priv->hw->rx_csum;
4749 struct sk_buff *skb;
4752 skb = stmmac_construct_skb_zc(ch, xdp);
4754 priv->dev->stats.rx_dropped++;
4758 stmmac_get_rx_hwtstamp(priv, p, np, skb);
4759 stmmac_rx_vlan(priv->dev, skb);
4760 skb->protocol = eth_type_trans(skb, priv->dev);
4763 skb_checksum_none_assert(skb);
4765 skb->ip_summed = CHECKSUM_UNNECESSARY;
4767 if (!stmmac_get_rx_hash(priv, p, &hash, &hash_type))
4768 skb_set_hash(skb, hash, hash_type);
4770 skb_record_rx_queue(skb, queue);
4771 napi_gro_receive(&ch->rxtx_napi, skb);
4773 priv->dev->stats.rx_packets++;
4774 priv->dev->stats.rx_bytes += len;
4777 static bool stmmac_rx_refill_zc(struct stmmac_priv *priv, u32 queue, u32 budget)
4779 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4780 unsigned int entry = rx_q->dirty_rx;
4781 struct dma_desc *rx_desc = NULL;
4784 budget = min(budget, stmmac_rx_dirty(priv, queue));
4786 while (budget-- > 0 && entry != rx_q->cur_rx) {
4787 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry];
4788 dma_addr_t dma_addr;
4792 buf->xdp = xsk_buff_alloc(rx_q->xsk_pool);
4799 if (priv->extend_desc)
4800 rx_desc = (struct dma_desc *)(rx_q->dma_erx + entry);
4802 rx_desc = rx_q->dma_rx + entry;
4804 dma_addr = xsk_buff_xdp_get_dma(buf->xdp);
4805 stmmac_set_desc_addr(priv, rx_desc, dma_addr);
4806 stmmac_set_desc_sec_addr(priv, rx_desc, 0, false);
4807 stmmac_refill_desc3(priv, rx_q, rx_desc);
4809 rx_q->rx_count_frames++;
4810 rx_q->rx_count_frames += priv->rx_coal_frames[queue];
4811 if (rx_q->rx_count_frames > priv->rx_coal_frames[queue])
4812 rx_q->rx_count_frames = 0;
4814 use_rx_wd = !priv->rx_coal_frames[queue];
4815 use_rx_wd |= rx_q->rx_count_frames > 0;
4816 if (!priv->use_riwt)
4820 stmmac_set_rx_owner(priv, rx_desc, use_rx_wd);
4822 entry = STMMAC_GET_ENTRY(entry, priv->dma_rx_size);
4826 rx_q->dirty_rx = entry;
4827 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
4828 (rx_q->dirty_rx * sizeof(struct dma_desc));
4829 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue);
4835 static int stmmac_rx_zc(struct stmmac_priv *priv, int limit, u32 queue)
4837 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4838 unsigned int count = 0, error = 0, len = 0;
4839 int dirty = stmmac_rx_dirty(priv, queue);
4840 unsigned int next_entry = rx_q->cur_rx;
4841 unsigned int desc_size;
4842 struct bpf_prog *prog;
4843 bool failure = false;
4847 if (netif_msg_rx_status(priv)) {
4850 netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
4851 if (priv->extend_desc) {
4852 rx_head = (void *)rx_q->dma_erx;
4853 desc_size = sizeof(struct dma_extended_desc);
4855 rx_head = (void *)rx_q->dma_rx;
4856 desc_size = sizeof(struct dma_desc);
4859 stmmac_display_ring(priv, rx_head, priv->dma_rx_size, true,
4860 rx_q->dma_rx_phy, desc_size);
4862 while (count < limit) {
4863 struct stmmac_rx_buffer *buf;
4864 unsigned int buf1_len = 0;
4865 struct dma_desc *np, *p;
4869 if (!count && rx_q->state_saved) {
4870 error = rx_q->state.error;
4871 len = rx_q->state.len;
4873 rx_q->state_saved = false;
4884 buf = &rx_q->buf_pool[entry];
4886 if (dirty >= STMMAC_RX_FILL_BATCH) {
4887 failure = failure ||
4888 !stmmac_rx_refill_zc(priv, queue, dirty);
4892 if (priv->extend_desc)
4893 p = (struct dma_desc *)(rx_q->dma_erx + entry);
4895 p = rx_q->dma_rx + entry;
4897 /* read the status of the incoming frame */
4898 status = stmmac_rx_status(priv, &priv->dev->stats,
4900 /* check if managed by the DMA otherwise go ahead */
4901 if (unlikely(status & dma_own))
4904 /* Prefetch the next RX descriptor */
4905 rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx,
4907 next_entry = rx_q->cur_rx;
4909 if (priv->extend_desc)
4910 np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
4912 np = rx_q->dma_rx + next_entry;
4916 /* Ensure a valid XSK buffer before proceed */
4920 if (priv->extend_desc)
4921 stmmac_rx_extended_status(priv, &priv->dev->stats,
4923 rx_q->dma_erx + entry);
4924 if (unlikely(status == discard_frame)) {
4925 xsk_buff_free(buf->xdp);
4929 if (!priv->hwts_rx_en)
4930 priv->dev->stats.rx_errors++;
4933 if (unlikely(error && (status & rx_not_ls)))
4935 if (unlikely(error)) {
4940 /* XSK pool expects RX frame 1:1 mapped to XSK buffer */
4941 if (likely(status & rx_not_ls)) {
4942 xsk_buff_free(buf->xdp);
4949 /* XDP ZC Frame only support primary buffers for now */
4950 buf1_len = stmmac_rx_buf1_len(priv, p, status, len);
4953 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
4954 * Type frames (LLC/LLC-SNAP)
4956 * llc_snap is never checked in GMAC >= 4, so this ACS
4957 * feature is always disabled and packets need to be
4958 * stripped manually.
4960 if (likely(!(status & rx_not_ls)) &&
4961 (likely(priv->synopsys_id >= DWMAC_CORE_4_00) ||
4962 unlikely(status != llc_snap))) {
4963 buf1_len -= ETH_FCS_LEN;
4967 /* RX buffer is good and fit into a XSK pool buffer */
4968 buf->xdp->data_end = buf->xdp->data + buf1_len;
4969 xsk_buff_dma_sync_for_cpu(buf->xdp, rx_q->xsk_pool);
4971 prog = READ_ONCE(priv->xdp_prog);
4972 res = __stmmac_xdp_run_prog(priv, prog, buf->xdp);
4975 case STMMAC_XDP_PASS:
4976 stmmac_dispatch_skb_zc(priv, queue, p, np, buf->xdp);
4977 xsk_buff_free(buf->xdp);
4979 case STMMAC_XDP_CONSUMED:
4980 xsk_buff_free(buf->xdp);
4981 priv->dev->stats.rx_dropped++;
4984 case STMMAC_XDP_REDIRECT:
4994 if (status & rx_not_ls) {
4995 rx_q->state_saved = true;
4996 rx_q->state.error = error;
4997 rx_q->state.len = len;
5000 stmmac_finalize_xdp_rx(priv, xdp_status);
5002 priv->xstats.rx_pkt_n += count;
5003 priv->xstats.rxq_stats[queue].rx_pkt_n += count;
5005 if (xsk_uses_need_wakeup(rx_q->xsk_pool)) {
5006 if (failure || stmmac_rx_dirty(priv, queue) > 0)
5007 xsk_set_rx_need_wakeup(rx_q->xsk_pool);
5009 xsk_clear_rx_need_wakeup(rx_q->xsk_pool);
5014 return failure ? limit : (int)count;
5018 * stmmac_rx - manage the receive process
5019 * @priv: driver private structure
5020 * @limit: napi bugget
5021 * @queue: RX queue index.
5022 * Description : this the function called by the napi poll method.
5023 * It gets all the frames inside the ring.
5025 static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
5027 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
5028 struct stmmac_channel *ch = &priv->channel[queue];
5029 unsigned int count = 0, error = 0, len = 0;
5030 int status = 0, coe = priv->hw->rx_csum;
5031 unsigned int next_entry = rx_q->cur_rx;
5032 enum dma_data_direction dma_dir;
5033 unsigned int desc_size;
5034 struct sk_buff *skb = NULL;
5035 struct xdp_buff xdp;
5039 dma_dir = page_pool_get_dma_dir(rx_q->page_pool);
5040 buf_sz = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE) * PAGE_SIZE;
5042 if (netif_msg_rx_status(priv)) {
5045 netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
5046 if (priv->extend_desc) {
5047 rx_head = (void *)rx_q->dma_erx;
5048 desc_size = sizeof(struct dma_extended_desc);
5050 rx_head = (void *)rx_q->dma_rx;
5051 desc_size = sizeof(struct dma_desc);
5054 stmmac_display_ring(priv, rx_head, priv->dma_rx_size, true,
5055 rx_q->dma_rx_phy, desc_size);
5057 while (count < limit) {
5058 unsigned int buf1_len = 0, buf2_len = 0;
5059 enum pkt_hash_types hash_type;
5060 struct stmmac_rx_buffer *buf;
5061 struct dma_desc *np, *p;
5065 if (!count && rx_q->state_saved) {
5066 skb = rx_q->state.skb;
5067 error = rx_q->state.error;
5068 len = rx_q->state.len;
5070 rx_q->state_saved = false;
5083 buf = &rx_q->buf_pool[entry];
5085 if (priv->extend_desc)
5086 p = (struct dma_desc *)(rx_q->dma_erx + entry);
5088 p = rx_q->dma_rx + entry;
5090 /* read the status of the incoming frame */
5091 status = stmmac_rx_status(priv, &priv->dev->stats,
5093 /* check if managed by the DMA otherwise go ahead */
5094 if (unlikely(status & dma_own))
5097 rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx,
5099 next_entry = rx_q->cur_rx;
5101 if (priv->extend_desc)
5102 np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
5104 np = rx_q->dma_rx + next_entry;
5108 if (priv->extend_desc)
5109 stmmac_rx_extended_status(priv, &priv->dev->stats,
5110 &priv->xstats, rx_q->dma_erx + entry);
5111 if (unlikely(status == discard_frame)) {
5112 page_pool_recycle_direct(rx_q->page_pool, buf->page);
5115 if (!priv->hwts_rx_en)
5116 priv->dev->stats.rx_errors++;
5119 if (unlikely(error && (status & rx_not_ls)))
5121 if (unlikely(error)) {
5128 /* Buffer is good. Go on. */
5130 prefetch(page_address(buf->page) + buf->page_offset);
5132 prefetch(page_address(buf->sec_page));
5134 buf1_len = stmmac_rx_buf1_len(priv, p, status, len);
5136 buf2_len = stmmac_rx_buf2_len(priv, p, status, len);
5139 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
5140 * Type frames (LLC/LLC-SNAP)
5142 * llc_snap is never checked in GMAC >= 4, so this ACS
5143 * feature is always disabled and packets need to be
5144 * stripped manually.
5146 if (likely(!(status & rx_not_ls)) &&
5147 (likely(priv->synopsys_id >= DWMAC_CORE_4_00) ||
5148 unlikely(status != llc_snap))) {
5150 buf2_len -= ETH_FCS_LEN;
5152 } else if (buf1_len) {
5153 buf1_len -= ETH_FCS_LEN;
5159 unsigned int pre_len, sync_len;
5161 dma_sync_single_for_cpu(priv->device, buf->addr,
5164 xdp_init_buff(&xdp, buf_sz, &rx_q->xdp_rxq);
5165 xdp_prepare_buff(&xdp, page_address(buf->page),
5166 buf->page_offset, buf1_len, false);
5168 pre_len = xdp.data_end - xdp.data_hard_start -
5170 skb = stmmac_xdp_run_prog(priv, &xdp);
5171 /* Due xdp_adjust_tail: DMA sync for_device
5172 * cover max len CPU touch
5174 sync_len = xdp.data_end - xdp.data_hard_start -
5176 sync_len = max(sync_len, pre_len);
5178 /* For Not XDP_PASS verdict */
5180 unsigned int xdp_res = -PTR_ERR(skb);
5182 if (xdp_res & STMMAC_XDP_CONSUMED) {
5183 page_pool_put_page(rx_q->page_pool,
5184 virt_to_head_page(xdp.data),
5187 priv->dev->stats.rx_dropped++;
5189 /* Clear skb as it was set as
5190 * status by XDP program.
5194 if (unlikely((status & rx_not_ls)))
5199 } else if (xdp_res & (STMMAC_XDP_TX |
5200 STMMAC_XDP_REDIRECT)) {
5201 xdp_status |= xdp_res;
5211 /* XDP program may expand or reduce tail */
5212 buf1_len = xdp.data_end - xdp.data;
5214 skb = napi_alloc_skb(&ch->rx_napi, buf1_len);
5216 priv->dev->stats.rx_dropped++;
5221 /* XDP program may adjust header */
5222 skb_copy_to_linear_data(skb, xdp.data, buf1_len);
5223 skb_put(skb, buf1_len);
5225 /* Data payload copied into SKB, page ready for recycle */
5226 page_pool_recycle_direct(rx_q->page_pool, buf->page);
5228 } else if (buf1_len) {
5229 dma_sync_single_for_cpu(priv->device, buf->addr,
5231 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
5232 buf->page, buf->page_offset, buf1_len,
5235 /* Data payload appended into SKB */
5236 page_pool_release_page(rx_q->page_pool, buf->page);
5241 dma_sync_single_for_cpu(priv->device, buf->sec_addr,
5243 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
5244 buf->sec_page, 0, buf2_len,
5247 /* Data payload appended into SKB */
5248 page_pool_release_page(rx_q->page_pool, buf->sec_page);
5249 buf->sec_page = NULL;
5253 if (likely(status & rx_not_ls))
5258 /* Got entire packet into SKB. Finish it. */
5260 stmmac_get_rx_hwtstamp(priv, p, np, skb);
5261 stmmac_rx_vlan(priv->dev, skb);
5262 skb->protocol = eth_type_trans(skb, priv->dev);
5265 skb_checksum_none_assert(skb);
5267 skb->ip_summed = CHECKSUM_UNNECESSARY;
5269 if (!stmmac_get_rx_hash(priv, p, &hash, &hash_type))
5270 skb_set_hash(skb, hash, hash_type);
5272 skb_record_rx_queue(skb, queue);
5273 napi_gro_receive(&ch->rx_napi, skb);
5276 priv->dev->stats.rx_packets++;
5277 priv->dev->stats.rx_bytes += len;
5281 if (status & rx_not_ls || skb) {
5282 rx_q->state_saved = true;
5283 rx_q->state.skb = skb;
5284 rx_q->state.error = error;
5285 rx_q->state.len = len;
5288 stmmac_finalize_xdp_rx(priv, xdp_status);
5290 stmmac_rx_refill(priv, queue);
5292 priv->xstats.rx_pkt_n += count;
5293 priv->xstats.rxq_stats[queue].rx_pkt_n += count;
5298 static int stmmac_napi_poll_rx(struct napi_struct *napi, int budget)
5300 struct stmmac_channel *ch =
5301 container_of(napi, struct stmmac_channel, rx_napi);
5302 struct stmmac_priv *priv = ch->priv_data;
5303 u32 chan = ch->index;
5306 priv->xstats.napi_poll++;
5308 work_done = stmmac_rx(priv, budget, chan);
5309 if (work_done < budget && napi_complete_done(napi, work_done)) {
5310 unsigned long flags;
5312 spin_lock_irqsave(&ch->lock, flags);
5313 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
5314 spin_unlock_irqrestore(&ch->lock, flags);
5320 static int stmmac_napi_poll_tx(struct napi_struct *napi, int budget)
5322 struct stmmac_channel *ch =
5323 container_of(napi, struct stmmac_channel, tx_napi);
5324 struct stmmac_priv *priv = ch->priv_data;
5325 u32 chan = ch->index;
5328 priv->xstats.napi_poll++;
5330 work_done = stmmac_tx_clean(priv, budget, chan);
5331 work_done = min(work_done, budget);
5333 if (work_done < budget && napi_complete_done(napi, work_done)) {
5334 unsigned long flags;
5336 spin_lock_irqsave(&ch->lock, flags);
5337 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
5338 spin_unlock_irqrestore(&ch->lock, flags);
5344 static int stmmac_napi_poll_rxtx(struct napi_struct *napi, int budget)
5346 struct stmmac_channel *ch =
5347 container_of(napi, struct stmmac_channel, rxtx_napi);
5348 struct stmmac_priv *priv = ch->priv_data;
5349 int rx_done, tx_done, rxtx_done;
5350 u32 chan = ch->index;
5352 priv->xstats.napi_poll++;
5354 tx_done = stmmac_tx_clean(priv, budget, chan);
5355 tx_done = min(tx_done, budget);
5357 rx_done = stmmac_rx_zc(priv, budget, chan);
5359 rxtx_done = max(tx_done, rx_done);
5361 /* If either TX or RX work is not complete, return budget
5364 if (rxtx_done >= budget)
5367 /* all work done, exit the polling mode */
5368 if (napi_complete_done(napi, rxtx_done)) {
5369 unsigned long flags;
5371 spin_lock_irqsave(&ch->lock, flags);
5372 /* Both RX and TX work done are compelte,
5373 * so enable both RX & TX IRQs.
5375 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 1);
5376 spin_unlock_irqrestore(&ch->lock, flags);
5379 return min(rxtx_done, budget - 1);
5384 * @dev : Pointer to net device structure
5385 * @txqueue: the index of the hanging transmit queue
5386 * Description: this function is called when a packet transmission fails to
5387 * complete within a reasonable time. The driver will mark the error in the
5388 * netdev structure and arrange for the device to be reset to a sane state
5389 * in order to transmit a new packet.
5391 static void stmmac_tx_timeout(struct net_device *dev, unsigned int txqueue)
5393 struct stmmac_priv *priv = netdev_priv(dev);
5395 stmmac_global_err(priv);
5399 * stmmac_set_rx_mode - entry point for multicast addressing
5400 * @dev : pointer to the device structure
5402 * This function is a driver entry point which gets called by the kernel
5403 * whenever multicast addresses must be enabled/disabled.
5407 static void stmmac_set_rx_mode(struct net_device *dev)
5409 struct stmmac_priv *priv = netdev_priv(dev);
5411 stmmac_set_filter(priv, priv->hw, dev);
5415 * stmmac_change_mtu - entry point to change MTU size for the device.
5416 * @dev : device pointer.
5417 * @new_mtu : the new MTU size for the device.
5418 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
5419 * to drive packet transmission. Ethernet has an MTU of 1500 octets
5420 * (ETH_DATA_LEN). This value can be changed with ifconfig.
5422 * 0 on success and an appropriate (-)ve integer as defined in errno.h
5425 static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
5427 struct stmmac_priv *priv = netdev_priv(dev);
5428 int txfifosz = priv->plat->tx_fifo_size;
5429 const int mtu = new_mtu;
5432 txfifosz = priv->dma_cap.tx_fifo_size;
5434 txfifosz /= priv->plat->tx_queues_to_use;
5436 if (netif_running(dev)) {
5437 netdev_err(priv->dev, "must be stopped to change its MTU\n");
5441 if (stmmac_xdp_is_enabled(priv) && new_mtu > ETH_DATA_LEN) {
5442 netdev_dbg(priv->dev, "Jumbo frames not supported for XDP\n");
5446 new_mtu = STMMAC_ALIGN(new_mtu);
5448 /* If condition true, FIFO is too small or MTU too large */
5449 if ((txfifosz < new_mtu) || (new_mtu > BUF_SIZE_16KiB))
5454 netdev_update_features(dev);
5459 static netdev_features_t stmmac_fix_features(struct net_device *dev,
5460 netdev_features_t features)
5462 struct stmmac_priv *priv = netdev_priv(dev);
5464 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
5465 features &= ~NETIF_F_RXCSUM;
5467 if (!priv->plat->tx_coe)
5468 features &= ~NETIF_F_CSUM_MASK;
5470 /* Some GMAC devices have a bugged Jumbo frame support that
5471 * needs to have the Tx COE disabled for oversized frames
5472 * (due to limited buffer sizes). In this case we disable
5473 * the TX csum insertion in the TDES and not use SF.
5475 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
5476 features &= ~NETIF_F_CSUM_MASK;
5478 /* Disable tso if asked by ethtool */
5479 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
5480 if (features & NETIF_F_TSO)
5489 static int stmmac_set_features(struct net_device *netdev,
5490 netdev_features_t features)
5492 struct stmmac_priv *priv = netdev_priv(netdev);
5494 /* Keep the COE Type in case of csum is supporting */
5495 if (features & NETIF_F_RXCSUM)
5496 priv->hw->rx_csum = priv->plat->rx_coe;
5498 priv->hw->rx_csum = 0;
5499 /* No check needed because rx_coe has been set before and it will be
5500 * fixed in case of issue.
5502 stmmac_rx_ipc(priv, priv->hw);
5504 if (priv->sph_cap) {
5505 bool sph_en = (priv->hw->rx_csum > 0) && priv->sph;
5508 for (chan = 0; chan < priv->plat->rx_queues_to_use; chan++)
5509 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
5515 static void stmmac_fpe_event_status(struct stmmac_priv *priv, int status)
5517 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
5518 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
5519 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
5520 bool *hs_enable = &fpe_cfg->hs_enable;
5522 if (status == FPE_EVENT_UNKNOWN || !*hs_enable)
5525 /* If LP has sent verify mPacket, LP is FPE capable */
5526 if ((status & FPE_EVENT_RVER) == FPE_EVENT_RVER) {
5527 if (*lp_state < FPE_STATE_CAPABLE)
5528 *lp_state = FPE_STATE_CAPABLE;
5530 /* If user has requested FPE enable, quickly response */
5532 stmmac_fpe_send_mpacket(priv, priv->ioaddr,
5536 /* If Local has sent verify mPacket, Local is FPE capable */
5537 if ((status & FPE_EVENT_TVER) == FPE_EVENT_TVER) {
5538 if (*lo_state < FPE_STATE_CAPABLE)
5539 *lo_state = FPE_STATE_CAPABLE;
5542 /* If LP has sent response mPacket, LP is entering FPE ON */
5543 if ((status & FPE_EVENT_RRSP) == FPE_EVENT_RRSP)
5544 *lp_state = FPE_STATE_ENTERING_ON;
5546 /* If Local has sent response mPacket, Local is entering FPE ON */
5547 if ((status & FPE_EVENT_TRSP) == FPE_EVENT_TRSP)
5548 *lo_state = FPE_STATE_ENTERING_ON;
5550 if (!test_bit(__FPE_REMOVING, &priv->fpe_task_state) &&
5551 !test_and_set_bit(__FPE_TASK_SCHED, &priv->fpe_task_state) &&
5553 queue_work(priv->fpe_wq, &priv->fpe_task);
5557 static void stmmac_common_interrupt(struct stmmac_priv *priv)
5559 u32 rx_cnt = priv->plat->rx_queues_to_use;
5560 u32 tx_cnt = priv->plat->tx_queues_to_use;
5565 xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
5566 queues_count = (rx_cnt > tx_cnt) ? rx_cnt : tx_cnt;
5569 pm_wakeup_event(priv->device, 0);
5571 if (priv->dma_cap.estsel)
5572 stmmac_est_irq_status(priv, priv->ioaddr, priv->dev,
5573 &priv->xstats, tx_cnt);
5575 if (priv->dma_cap.fpesel) {
5576 int status = stmmac_fpe_irq_status(priv, priv->ioaddr,
5579 stmmac_fpe_event_status(priv, status);
5582 /* To handle GMAC own interrupts */
5583 if ((priv->plat->has_gmac) || xmac) {
5584 int status = stmmac_host_irq_status(priv, priv->hw, &priv->xstats);
5586 if (unlikely(status)) {
5587 /* For LPI we need to save the tx status */
5588 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
5589 priv->tx_path_in_lpi_mode = true;
5590 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
5591 priv->tx_path_in_lpi_mode = false;
5594 for (queue = 0; queue < queues_count; queue++) {
5595 status = stmmac_host_mtl_irq_status(priv, priv->hw,
5599 /* PCS link status */
5600 if (priv->hw->pcs) {
5601 if (priv->xstats.pcs_link)
5602 netif_carrier_on(priv->dev);
5604 netif_carrier_off(priv->dev);
5607 stmmac_timestamp_interrupt(priv, priv);
5612 * stmmac_interrupt - main ISR
5613 * @irq: interrupt number.
5614 * @dev_id: to pass the net device pointer.
5615 * Description: this is the main driver interrupt service routine.
5617 * o DMA service routine (to manage incoming frame reception and transmission
5619 * o Core interrupts to manage: remote wake-up, management counter, LPI
5622 static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
5624 struct net_device *dev = (struct net_device *)dev_id;
5625 struct stmmac_priv *priv = netdev_priv(dev);
5627 /* Check if adapter is up */
5628 if (test_bit(STMMAC_DOWN, &priv->state))
5631 /* Check if a fatal error happened */
5632 if (stmmac_safety_feat_interrupt(priv))
5635 /* To handle Common interrupts */
5636 stmmac_common_interrupt(priv);
5638 /* To handle DMA interrupts */
5639 stmmac_dma_interrupt(priv);
5644 static irqreturn_t stmmac_mac_interrupt(int irq, void *dev_id)
5646 struct net_device *dev = (struct net_device *)dev_id;
5647 struct stmmac_priv *priv = netdev_priv(dev);
5649 if (unlikely(!dev)) {
5650 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5654 /* Check if adapter is up */
5655 if (test_bit(STMMAC_DOWN, &priv->state))
5658 /* To handle Common interrupts */
5659 stmmac_common_interrupt(priv);
5664 static irqreturn_t stmmac_safety_interrupt(int irq, void *dev_id)
5666 struct net_device *dev = (struct net_device *)dev_id;
5667 struct stmmac_priv *priv = netdev_priv(dev);
5669 if (unlikely(!dev)) {
5670 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5674 /* Check if adapter is up */
5675 if (test_bit(STMMAC_DOWN, &priv->state))
5678 /* Check if a fatal error happened */
5679 stmmac_safety_feat_interrupt(priv);
5684 static irqreturn_t stmmac_msi_intr_tx(int irq, void *data)
5686 struct stmmac_tx_queue *tx_q = (struct stmmac_tx_queue *)data;
5687 int chan = tx_q->queue_index;
5688 struct stmmac_priv *priv;
5691 priv = container_of(tx_q, struct stmmac_priv, tx_queue[chan]);
5693 if (unlikely(!data)) {
5694 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5698 /* Check if adapter is up */
5699 if (test_bit(STMMAC_DOWN, &priv->state))
5702 status = stmmac_napi_check(priv, chan, DMA_DIR_TX);
5704 if (unlikely(status & tx_hard_error_bump_tc)) {
5705 /* Try to bump up the dma threshold on this failure */
5706 stmmac_bump_dma_threshold(priv, chan);
5707 } else if (unlikely(status == tx_hard_error)) {
5708 stmmac_tx_err(priv, chan);
5714 static irqreturn_t stmmac_msi_intr_rx(int irq, void *data)
5716 struct stmmac_rx_queue *rx_q = (struct stmmac_rx_queue *)data;
5717 int chan = rx_q->queue_index;
5718 struct stmmac_priv *priv;
5720 priv = container_of(rx_q, struct stmmac_priv, rx_queue[chan]);
5722 if (unlikely(!data)) {
5723 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5727 /* Check if adapter is up */
5728 if (test_bit(STMMAC_DOWN, &priv->state))
5731 stmmac_napi_check(priv, chan, DMA_DIR_RX);
5736 #ifdef CONFIG_NET_POLL_CONTROLLER
5737 /* Polling receive - used by NETCONSOLE and other diagnostic tools
5738 * to allow network I/O with interrupts disabled.
5740 static void stmmac_poll_controller(struct net_device *dev)
5742 struct stmmac_priv *priv = netdev_priv(dev);
5745 /* If adapter is down, do nothing */
5746 if (test_bit(STMMAC_DOWN, &priv->state))
5749 if (priv->plat->multi_msi_en) {
5750 for (i = 0; i < priv->plat->rx_queues_to_use; i++)
5751 stmmac_msi_intr_rx(0, &priv->rx_queue[i]);
5753 for (i = 0; i < priv->plat->tx_queues_to_use; i++)
5754 stmmac_msi_intr_tx(0, &priv->tx_queue[i]);
5756 disable_irq(dev->irq);
5757 stmmac_interrupt(dev->irq, dev);
5758 enable_irq(dev->irq);
5764 * stmmac_ioctl - Entry point for the Ioctl
5765 * @dev: Device pointer.
5766 * @rq: An IOCTL specefic structure, that can contain a pointer to
5767 * a proprietary structure used to pass information to the driver.
5768 * @cmd: IOCTL command
5770 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
5772 static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
5774 struct stmmac_priv *priv = netdev_priv (dev);
5775 int ret = -EOPNOTSUPP;
5777 if (!netif_running(dev))
5784 ret = phylink_mii_ioctl(priv->phylink, rq, cmd);
5787 ret = stmmac_hwtstamp_set(dev, rq);
5790 ret = stmmac_hwtstamp_get(dev, rq);
5799 static int stmmac_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
5802 struct stmmac_priv *priv = cb_priv;
5803 int ret = -EOPNOTSUPP;
5805 if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
5808 __stmmac_disable_all_queues(priv);
5811 case TC_SETUP_CLSU32:
5812 ret = stmmac_tc_setup_cls_u32(priv, priv, type_data);
5814 case TC_SETUP_CLSFLOWER:
5815 ret = stmmac_tc_setup_cls(priv, priv, type_data);
5821 stmmac_enable_all_queues(priv);
5825 static LIST_HEAD(stmmac_block_cb_list);
5827 static int stmmac_setup_tc(struct net_device *ndev, enum tc_setup_type type,
5830 struct stmmac_priv *priv = netdev_priv(ndev);
5833 case TC_SETUP_BLOCK:
5834 return flow_block_cb_setup_simple(type_data,
5835 &stmmac_block_cb_list,
5836 stmmac_setup_tc_block_cb,
5838 case TC_SETUP_QDISC_CBS:
5839 return stmmac_tc_setup_cbs(priv, priv, type_data);
5840 case TC_SETUP_QDISC_TAPRIO:
5841 return stmmac_tc_setup_taprio(priv, priv, type_data);
5842 case TC_SETUP_QDISC_ETF:
5843 return stmmac_tc_setup_etf(priv, priv, type_data);
5849 static u16 stmmac_select_queue(struct net_device *dev, struct sk_buff *skb,
5850 struct net_device *sb_dev)
5852 int gso = skb_shinfo(skb)->gso_type;
5854 if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6 | SKB_GSO_UDP_L4)) {
5856 * There is no way to determine the number of TSO/USO
5857 * capable Queues. Let's use always the Queue 0
5858 * because if TSO/USO is supported then at least this
5859 * one will be capable.
5864 return netdev_pick_tx(dev, skb, NULL) % dev->real_num_tx_queues;
5867 static int stmmac_set_mac_address(struct net_device *ndev, void *addr)
5869 struct stmmac_priv *priv = netdev_priv(ndev);
5872 ret = pm_runtime_get_sync(priv->device);
5874 pm_runtime_put_noidle(priv->device);
5878 ret = eth_mac_addr(ndev, addr);
5882 stmmac_set_umac_addr(priv, priv->hw, ndev->dev_addr, 0);
5885 pm_runtime_put(priv->device);
5890 #ifdef CONFIG_DEBUG_FS
5891 static struct dentry *stmmac_fs_dir;
5893 static void sysfs_display_ring(void *head, int size, int extend_desc,
5894 struct seq_file *seq, dma_addr_t dma_phy_addr)
5897 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
5898 struct dma_desc *p = (struct dma_desc *)head;
5899 dma_addr_t dma_addr;
5901 for (i = 0; i < size; i++) {
5903 dma_addr = dma_phy_addr + i * sizeof(*ep);
5904 seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n",
5906 le32_to_cpu(ep->basic.des0),
5907 le32_to_cpu(ep->basic.des1),
5908 le32_to_cpu(ep->basic.des2),
5909 le32_to_cpu(ep->basic.des3));
5912 dma_addr = dma_phy_addr + i * sizeof(*p);
5913 seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n",
5915 le32_to_cpu(p->des0), le32_to_cpu(p->des1),
5916 le32_to_cpu(p->des2), le32_to_cpu(p->des3));
5919 seq_printf(seq, "\n");
5923 static int stmmac_rings_status_show(struct seq_file *seq, void *v)
5925 struct net_device *dev = seq->private;
5926 struct stmmac_priv *priv = netdev_priv(dev);
5927 u32 rx_count = priv->plat->rx_queues_to_use;
5928 u32 tx_count = priv->plat->tx_queues_to_use;
5931 if ((dev->flags & IFF_UP) == 0)
5934 for (queue = 0; queue < rx_count; queue++) {
5935 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
5937 seq_printf(seq, "RX Queue %d:\n", queue);
5939 if (priv->extend_desc) {
5940 seq_printf(seq, "Extended descriptor ring:\n");
5941 sysfs_display_ring((void *)rx_q->dma_erx,
5942 priv->dma_rx_size, 1, seq, rx_q->dma_rx_phy);
5944 seq_printf(seq, "Descriptor ring:\n");
5945 sysfs_display_ring((void *)rx_q->dma_rx,
5946 priv->dma_rx_size, 0, seq, rx_q->dma_rx_phy);
5950 for (queue = 0; queue < tx_count; queue++) {
5951 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
5953 seq_printf(seq, "TX Queue %d:\n", queue);
5955 if (priv->extend_desc) {
5956 seq_printf(seq, "Extended descriptor ring:\n");
5957 sysfs_display_ring((void *)tx_q->dma_etx,
5958 priv->dma_tx_size, 1, seq, tx_q->dma_tx_phy);
5959 } else if (!(tx_q->tbs & STMMAC_TBS_AVAIL)) {
5960 seq_printf(seq, "Descriptor ring:\n");
5961 sysfs_display_ring((void *)tx_q->dma_tx,
5962 priv->dma_tx_size, 0, seq, tx_q->dma_tx_phy);
5968 DEFINE_SHOW_ATTRIBUTE(stmmac_rings_status);
5970 static int stmmac_dma_cap_show(struct seq_file *seq, void *v)
5972 struct net_device *dev = seq->private;
5973 struct stmmac_priv *priv = netdev_priv(dev);
5975 if (!priv->hw_cap_support) {
5976 seq_printf(seq, "DMA HW features not supported\n");
5980 seq_printf(seq, "==============================\n");
5981 seq_printf(seq, "\tDMA HW features\n");
5982 seq_printf(seq, "==============================\n");
5984 seq_printf(seq, "\t10/100 Mbps: %s\n",
5985 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
5986 seq_printf(seq, "\t1000 Mbps: %s\n",
5987 (priv->dma_cap.mbps_1000) ? "Y" : "N");
5988 seq_printf(seq, "\tHalf duplex: %s\n",
5989 (priv->dma_cap.half_duplex) ? "Y" : "N");
5990 seq_printf(seq, "\tHash Filter: %s\n",
5991 (priv->dma_cap.hash_filter) ? "Y" : "N");
5992 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
5993 (priv->dma_cap.multi_addr) ? "Y" : "N");
5994 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfaces): %s\n",
5995 (priv->dma_cap.pcs) ? "Y" : "N");
5996 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
5997 (priv->dma_cap.sma_mdio) ? "Y" : "N");
5998 seq_printf(seq, "\tPMT Remote wake up: %s\n",
5999 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
6000 seq_printf(seq, "\tPMT Magic Frame: %s\n",
6001 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
6002 seq_printf(seq, "\tRMON module: %s\n",
6003 (priv->dma_cap.rmon) ? "Y" : "N");
6004 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
6005 (priv->dma_cap.time_stamp) ? "Y" : "N");
6006 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp: %s\n",
6007 (priv->dma_cap.atime_stamp) ? "Y" : "N");
6008 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE): %s\n",
6009 (priv->dma_cap.eee) ? "Y" : "N");
6010 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
6011 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
6012 (priv->dma_cap.tx_coe) ? "Y" : "N");
6013 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
6014 seq_printf(seq, "\tIP Checksum Offload in RX: %s\n",
6015 (priv->dma_cap.rx_coe) ? "Y" : "N");
6017 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
6018 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
6019 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
6020 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
6022 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
6023 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
6024 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
6025 priv->dma_cap.number_rx_channel);
6026 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
6027 priv->dma_cap.number_tx_channel);
6028 seq_printf(seq, "\tNumber of Additional RX queues: %d\n",
6029 priv->dma_cap.number_rx_queues);
6030 seq_printf(seq, "\tNumber of Additional TX queues: %d\n",
6031 priv->dma_cap.number_tx_queues);
6032 seq_printf(seq, "\tEnhanced descriptors: %s\n",
6033 (priv->dma_cap.enh_desc) ? "Y" : "N");
6034 seq_printf(seq, "\tTX Fifo Size: %d\n", priv->dma_cap.tx_fifo_size);
6035 seq_printf(seq, "\tRX Fifo Size: %d\n", priv->dma_cap.rx_fifo_size);
6036 seq_printf(seq, "\tHash Table Size: %d\n", priv->dma_cap.hash_tb_sz);
6037 seq_printf(seq, "\tTSO: %s\n", priv->dma_cap.tsoen ? "Y" : "N");
6038 seq_printf(seq, "\tNumber of PPS Outputs: %d\n",
6039 priv->dma_cap.pps_out_num);
6040 seq_printf(seq, "\tSafety Features: %s\n",
6041 priv->dma_cap.asp ? "Y" : "N");
6042 seq_printf(seq, "\tFlexible RX Parser: %s\n",
6043 priv->dma_cap.frpsel ? "Y" : "N");
6044 seq_printf(seq, "\tEnhanced Addressing: %d\n",
6045 priv->dma_cap.addr64);
6046 seq_printf(seq, "\tReceive Side Scaling: %s\n",
6047 priv->dma_cap.rssen ? "Y" : "N");
6048 seq_printf(seq, "\tVLAN Hash Filtering: %s\n",
6049 priv->dma_cap.vlhash ? "Y" : "N");
6050 seq_printf(seq, "\tSplit Header: %s\n",
6051 priv->dma_cap.sphen ? "Y" : "N");
6052 seq_printf(seq, "\tVLAN TX Insertion: %s\n",
6053 priv->dma_cap.vlins ? "Y" : "N");
6054 seq_printf(seq, "\tDouble VLAN: %s\n",
6055 priv->dma_cap.dvlan ? "Y" : "N");
6056 seq_printf(seq, "\tNumber of L3/L4 Filters: %d\n",
6057 priv->dma_cap.l3l4fnum);
6058 seq_printf(seq, "\tARP Offloading: %s\n",
6059 priv->dma_cap.arpoffsel ? "Y" : "N");
6060 seq_printf(seq, "\tEnhancements to Scheduled Traffic (EST): %s\n",
6061 priv->dma_cap.estsel ? "Y" : "N");
6062 seq_printf(seq, "\tFrame Preemption (FPE): %s\n",
6063 priv->dma_cap.fpesel ? "Y" : "N");
6064 seq_printf(seq, "\tTime-Based Scheduling (TBS): %s\n",
6065 priv->dma_cap.tbssel ? "Y" : "N");
6068 DEFINE_SHOW_ATTRIBUTE(stmmac_dma_cap);
6070 /* Use network device events to rename debugfs file entries.
6072 static int stmmac_device_event(struct notifier_block *unused,
6073 unsigned long event, void *ptr)
6075 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6076 struct stmmac_priv *priv = netdev_priv(dev);
6078 if (dev->netdev_ops != &stmmac_netdev_ops)
6082 case NETDEV_CHANGENAME:
6083 if (priv->dbgfs_dir)
6084 priv->dbgfs_dir = debugfs_rename(stmmac_fs_dir,
6094 static struct notifier_block stmmac_notifier = {
6095 .notifier_call = stmmac_device_event,
6098 static void stmmac_init_fs(struct net_device *dev)
6100 struct stmmac_priv *priv = netdev_priv(dev);
6104 /* Create per netdev entries */
6105 priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
6107 /* Entry to report DMA RX/TX rings */
6108 debugfs_create_file("descriptors_status", 0444, priv->dbgfs_dir, dev,
6109 &stmmac_rings_status_fops);
6111 /* Entry to report the DMA HW features */
6112 debugfs_create_file("dma_cap", 0444, priv->dbgfs_dir, dev,
6113 &stmmac_dma_cap_fops);
6118 static void stmmac_exit_fs(struct net_device *dev)
6120 struct stmmac_priv *priv = netdev_priv(dev);
6122 debugfs_remove_recursive(priv->dbgfs_dir);
6124 #endif /* CONFIG_DEBUG_FS */
6126 static u32 stmmac_vid_crc32_le(__le16 vid_le)
6128 unsigned char *data = (unsigned char *)&vid_le;
6129 unsigned char data_byte = 0;
6134 bits = get_bitmask_order(VLAN_VID_MASK);
6135 for (i = 0; i < bits; i++) {
6137 data_byte = data[i / 8];
6139 temp = ((crc & 1) ^ data_byte) & 1;
6150 static int stmmac_vlan_update(struct stmmac_priv *priv, bool is_double)
6157 for_each_set_bit(vid, priv->active_vlans, VLAN_N_VID) {
6158 __le16 vid_le = cpu_to_le16(vid);
6159 crc = bitrev32(~stmmac_vid_crc32_le(vid_le)) >> 28;
6164 if (!priv->dma_cap.vlhash) {
6165 if (count > 2) /* VID = 0 always passes filter */
6168 pmatch = cpu_to_le16(vid);
6172 return stmmac_update_vlan_hash(priv, priv->hw, hash, pmatch, is_double);
6175 static int stmmac_vlan_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid)
6177 struct stmmac_priv *priv = netdev_priv(ndev);
6178 bool is_double = false;
6181 if (be16_to_cpu(proto) == ETH_P_8021AD)
6184 set_bit(vid, priv->active_vlans);
6185 ret = stmmac_vlan_update(priv, is_double);
6187 clear_bit(vid, priv->active_vlans);
6191 if (priv->hw->num_vlan) {
6192 ret = stmmac_add_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid);
6200 static int stmmac_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid)
6202 struct stmmac_priv *priv = netdev_priv(ndev);
6203 bool is_double = false;
6206 ret = pm_runtime_get_sync(priv->device);
6208 pm_runtime_put_noidle(priv->device);
6212 if (be16_to_cpu(proto) == ETH_P_8021AD)
6215 clear_bit(vid, priv->active_vlans);
6217 if (priv->hw->num_vlan) {
6218 ret = stmmac_del_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid);
6220 goto del_vlan_error;
6223 ret = stmmac_vlan_update(priv, is_double);
6226 pm_runtime_put(priv->device);
6231 static int stmmac_bpf(struct net_device *dev, struct netdev_bpf *bpf)
6233 struct stmmac_priv *priv = netdev_priv(dev);
6235 switch (bpf->command) {
6236 case XDP_SETUP_PROG:
6237 return stmmac_xdp_set_prog(priv, bpf->prog, bpf->extack);
6238 case XDP_SETUP_XSK_POOL:
6239 return stmmac_xdp_setup_pool(priv, bpf->xsk.pool,
6246 static int stmmac_xdp_xmit(struct net_device *dev, int num_frames,
6247 struct xdp_frame **frames, u32 flags)
6249 struct stmmac_priv *priv = netdev_priv(dev);
6250 int cpu = smp_processor_id();
6251 struct netdev_queue *nq;
6255 if (unlikely(test_bit(STMMAC_DOWN, &priv->state)))
6258 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
6261 queue = stmmac_xdp_get_tx_queue(priv, cpu);
6262 nq = netdev_get_tx_queue(priv->dev, queue);
6264 __netif_tx_lock(nq, cpu);
6265 /* Avoids TX time-out as we are sharing with slow path */
6266 txq_trans_cond_update(nq);
6268 for (i = 0; i < num_frames; i++) {
6271 res = stmmac_xdp_xmit_xdpf(priv, queue, frames[i], true);
6272 if (res == STMMAC_XDP_CONSUMED)
6278 if (flags & XDP_XMIT_FLUSH) {
6279 stmmac_flush_tx_descriptors(priv, queue);
6280 stmmac_tx_timer_arm(priv, queue);
6283 __netif_tx_unlock(nq);
6288 void stmmac_disable_rx_queue(struct stmmac_priv *priv, u32 queue)
6290 struct stmmac_channel *ch = &priv->channel[queue];
6291 unsigned long flags;
6293 spin_lock_irqsave(&ch->lock, flags);
6294 stmmac_disable_dma_irq(priv, priv->ioaddr, queue, 1, 0);
6295 spin_unlock_irqrestore(&ch->lock, flags);
6297 stmmac_stop_rx_dma(priv, queue);
6298 __free_dma_rx_desc_resources(priv, queue);
6301 void stmmac_enable_rx_queue(struct stmmac_priv *priv, u32 queue)
6303 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
6304 struct stmmac_channel *ch = &priv->channel[queue];
6305 unsigned long flags;
6309 ret = __alloc_dma_rx_desc_resources(priv, queue);
6311 netdev_err(priv->dev, "Failed to alloc RX desc.\n");
6315 ret = __init_dma_rx_desc_rings(priv, queue, GFP_KERNEL);
6317 __free_dma_rx_desc_resources(priv, queue);
6318 netdev_err(priv->dev, "Failed to init RX desc.\n");
6322 stmmac_clear_rx_descriptors(priv, queue);
6324 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6325 rx_q->dma_rx_phy, rx_q->queue_index);
6327 rx_q->rx_tail_addr = rx_q->dma_rx_phy + (rx_q->buf_alloc_num *
6328 sizeof(struct dma_desc));
6329 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
6330 rx_q->rx_tail_addr, rx_q->queue_index);
6332 if (rx_q->xsk_pool && rx_q->buf_alloc_num) {
6333 buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool);
6334 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6338 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6343 stmmac_start_rx_dma(priv, queue);
6345 spin_lock_irqsave(&ch->lock, flags);
6346 stmmac_enable_dma_irq(priv, priv->ioaddr, queue, 1, 0);
6347 spin_unlock_irqrestore(&ch->lock, flags);
6350 void stmmac_disable_tx_queue(struct stmmac_priv *priv, u32 queue)
6352 struct stmmac_channel *ch = &priv->channel[queue];
6353 unsigned long flags;
6355 spin_lock_irqsave(&ch->lock, flags);
6356 stmmac_disable_dma_irq(priv, priv->ioaddr, queue, 0, 1);
6357 spin_unlock_irqrestore(&ch->lock, flags);
6359 stmmac_stop_tx_dma(priv, queue);
6360 __free_dma_tx_desc_resources(priv, queue);
6363 void stmmac_enable_tx_queue(struct stmmac_priv *priv, u32 queue)
6365 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
6366 struct stmmac_channel *ch = &priv->channel[queue];
6367 unsigned long flags;
6370 ret = __alloc_dma_tx_desc_resources(priv, queue);
6372 netdev_err(priv->dev, "Failed to alloc TX desc.\n");
6376 ret = __init_dma_tx_desc_rings(priv, queue);
6378 __free_dma_tx_desc_resources(priv, queue);
6379 netdev_err(priv->dev, "Failed to init TX desc.\n");
6383 stmmac_clear_tx_descriptors(priv, queue);
6385 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6386 tx_q->dma_tx_phy, tx_q->queue_index);
6388 if (tx_q->tbs & STMMAC_TBS_AVAIL)
6389 stmmac_enable_tbs(priv, priv->ioaddr, 1, tx_q->queue_index);
6391 tx_q->tx_tail_addr = tx_q->dma_tx_phy;
6392 stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
6393 tx_q->tx_tail_addr, tx_q->queue_index);
6395 stmmac_start_tx_dma(priv, queue);
6397 spin_lock_irqsave(&ch->lock, flags);
6398 stmmac_enable_dma_irq(priv, priv->ioaddr, queue, 0, 1);
6399 spin_unlock_irqrestore(&ch->lock, flags);
6402 void stmmac_xdp_release(struct net_device *dev)
6404 struct stmmac_priv *priv = netdev_priv(dev);
6407 /* Disable NAPI process */
6408 stmmac_disable_all_queues(priv);
6410 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
6411 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
6413 /* Free the IRQ lines */
6414 stmmac_free_irq(dev, REQ_IRQ_ERR_ALL, 0);
6416 /* Stop TX/RX DMA channels */
6417 stmmac_stop_all_dma(priv);
6419 /* Release and free the Rx/Tx resources */
6420 free_dma_desc_resources(priv);
6422 /* Disable the MAC Rx/Tx */
6423 stmmac_mac_set(priv, priv->ioaddr, false);
6425 /* set trans_start so we don't get spurious
6426 * watchdogs during reset
6428 netif_trans_update(dev);
6429 netif_carrier_off(dev);
6432 int stmmac_xdp_open(struct net_device *dev)
6434 struct stmmac_priv *priv = netdev_priv(dev);
6435 u32 rx_cnt = priv->plat->rx_queues_to_use;
6436 u32 tx_cnt = priv->plat->tx_queues_to_use;
6437 u32 dma_csr_ch = max(rx_cnt, tx_cnt);
6438 struct stmmac_rx_queue *rx_q;
6439 struct stmmac_tx_queue *tx_q;
6445 ret = alloc_dma_desc_resources(priv);
6447 netdev_err(dev, "%s: DMA descriptors allocation failed\n",
6449 goto dma_desc_error;
6452 ret = init_dma_desc_rings(dev, GFP_KERNEL);
6454 netdev_err(dev, "%s: DMA descriptors initialization failed\n",
6459 /* DMA CSR Channel configuration */
6460 for (chan = 0; chan < dma_csr_ch; chan++)
6461 stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan);
6463 /* Adjust Split header */
6464 sph_en = (priv->hw->rx_csum > 0) && priv->sph;
6466 /* DMA RX Channel Configuration */
6467 for (chan = 0; chan < rx_cnt; chan++) {
6468 rx_q = &priv->rx_queue[chan];
6470 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6471 rx_q->dma_rx_phy, chan);
6473 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
6474 (rx_q->buf_alloc_num *
6475 sizeof(struct dma_desc));
6476 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
6477 rx_q->rx_tail_addr, chan);
6479 if (rx_q->xsk_pool && rx_q->buf_alloc_num) {
6480 buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool);
6481 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6485 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6490 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
6493 /* DMA TX Channel Configuration */
6494 for (chan = 0; chan < tx_cnt; chan++) {
6495 tx_q = &priv->tx_queue[chan];
6497 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6498 tx_q->dma_tx_phy, chan);
6500 tx_q->tx_tail_addr = tx_q->dma_tx_phy;
6501 stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
6502 tx_q->tx_tail_addr, chan);
6504 hrtimer_init(&tx_q->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6505 tx_q->txtimer.function = stmmac_tx_timer;
6508 /* Enable the MAC Rx/Tx */
6509 stmmac_mac_set(priv, priv->ioaddr, true);
6511 /* Start Rx & Tx DMA Channels */
6512 stmmac_start_all_dma(priv);
6514 ret = stmmac_request_irq(dev);
6518 /* Enable NAPI process*/
6519 stmmac_enable_all_queues(priv);
6520 netif_carrier_on(dev);
6521 netif_tx_start_all_queues(dev);
6526 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
6527 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
6529 stmmac_hw_teardown(dev);
6531 free_dma_desc_resources(priv);
6536 int stmmac_xsk_wakeup(struct net_device *dev, u32 queue, u32 flags)
6538 struct stmmac_priv *priv = netdev_priv(dev);
6539 struct stmmac_rx_queue *rx_q;
6540 struct stmmac_tx_queue *tx_q;
6541 struct stmmac_channel *ch;
6543 if (test_bit(STMMAC_DOWN, &priv->state) ||
6544 !netif_carrier_ok(priv->dev))
6547 if (!stmmac_xdp_is_enabled(priv))
6550 if (queue >= priv->plat->rx_queues_to_use ||
6551 queue >= priv->plat->tx_queues_to_use)
6554 rx_q = &priv->rx_queue[queue];
6555 tx_q = &priv->tx_queue[queue];
6556 ch = &priv->channel[queue];
6558 if (!rx_q->xsk_pool && !tx_q->xsk_pool)
6561 if (!napi_if_scheduled_mark_missed(&ch->rxtx_napi)) {
6562 /* EQoS does not have per-DMA channel SW interrupt,
6563 * so we schedule RX Napi straight-away.
6565 if (likely(napi_schedule_prep(&ch->rxtx_napi)))
6566 __napi_schedule(&ch->rxtx_napi);
6572 static const struct net_device_ops stmmac_netdev_ops = {
6573 .ndo_open = stmmac_open,
6574 .ndo_start_xmit = stmmac_xmit,
6575 .ndo_stop = stmmac_release,
6576 .ndo_change_mtu = stmmac_change_mtu,
6577 .ndo_fix_features = stmmac_fix_features,
6578 .ndo_set_features = stmmac_set_features,
6579 .ndo_set_rx_mode = stmmac_set_rx_mode,
6580 .ndo_tx_timeout = stmmac_tx_timeout,
6581 .ndo_eth_ioctl = stmmac_ioctl,
6582 .ndo_setup_tc = stmmac_setup_tc,
6583 .ndo_select_queue = stmmac_select_queue,
6584 #ifdef CONFIG_NET_POLL_CONTROLLER
6585 .ndo_poll_controller = stmmac_poll_controller,
6587 .ndo_set_mac_address = stmmac_set_mac_address,
6588 .ndo_vlan_rx_add_vid = stmmac_vlan_rx_add_vid,
6589 .ndo_vlan_rx_kill_vid = stmmac_vlan_rx_kill_vid,
6590 .ndo_bpf = stmmac_bpf,
6591 .ndo_xdp_xmit = stmmac_xdp_xmit,
6592 .ndo_xsk_wakeup = stmmac_xsk_wakeup,
6595 static void stmmac_reset_subtask(struct stmmac_priv *priv)
6597 if (!test_and_clear_bit(STMMAC_RESET_REQUESTED, &priv->state))
6599 if (test_bit(STMMAC_DOWN, &priv->state))
6602 netdev_err(priv->dev, "Reset adapter.\n");
6605 netif_trans_update(priv->dev);
6606 while (test_and_set_bit(STMMAC_RESETING, &priv->state))
6607 usleep_range(1000, 2000);
6609 set_bit(STMMAC_DOWN, &priv->state);
6610 dev_close(priv->dev);
6611 dev_open(priv->dev, NULL);
6612 clear_bit(STMMAC_DOWN, &priv->state);
6613 clear_bit(STMMAC_RESETING, &priv->state);
6617 static void stmmac_service_task(struct work_struct *work)
6619 struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
6622 stmmac_reset_subtask(priv);
6623 clear_bit(STMMAC_SERVICE_SCHED, &priv->state);
6627 * stmmac_hw_init - Init the MAC device
6628 * @priv: driver private structure
6629 * Description: this function is to configure the MAC device according to
6630 * some platform parameters or the HW capability register. It prepares the
6631 * driver to use either ring or chain modes and to setup either enhanced or
6632 * normal descriptors.
6634 static int stmmac_hw_init(struct stmmac_priv *priv)
6638 /* dwmac-sun8i only work in chain mode */
6639 if (priv->plat->has_sun8i)
6641 priv->chain_mode = chain_mode;
6643 /* Initialize HW Interface */
6644 ret = stmmac_hwif_init(priv);
6648 /* Get the HW capability (new GMAC newer than 3.50a) */
6649 priv->hw_cap_support = stmmac_get_hw_features(priv);
6650 if (priv->hw_cap_support) {
6651 dev_info(priv->device, "DMA HW capability register supported\n");
6653 /* We can override some gmac/dma configuration fields: e.g.
6654 * enh_desc, tx_coe (e.g. that are passed through the
6655 * platform) with the values from the HW capability
6656 * register (if supported).
6658 priv->plat->enh_desc = priv->dma_cap.enh_desc;
6659 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up &&
6660 !priv->plat->use_phy_wol;
6661 priv->hw->pmt = priv->plat->pmt;
6662 if (priv->dma_cap.hash_tb_sz) {
6663 priv->hw->multicast_filter_bins =
6664 (BIT(priv->dma_cap.hash_tb_sz) << 5);
6665 priv->hw->mcast_bits_log2 =
6666 ilog2(priv->hw->multicast_filter_bins);
6669 /* TXCOE doesn't work in thresh DMA mode */
6670 if (priv->plat->force_thresh_dma_mode)
6671 priv->plat->tx_coe = 0;
6673 priv->plat->tx_coe = priv->dma_cap.tx_coe;
6675 /* In case of GMAC4 rx_coe is from HW cap register. */
6676 priv->plat->rx_coe = priv->dma_cap.rx_coe;
6678 if (priv->dma_cap.rx_coe_type2)
6679 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
6680 else if (priv->dma_cap.rx_coe_type1)
6681 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
6684 dev_info(priv->device, "No HW DMA feature register supported\n");
6687 if (priv->plat->rx_coe) {
6688 priv->hw->rx_csum = priv->plat->rx_coe;
6689 dev_info(priv->device, "RX Checksum Offload Engine supported\n");
6690 if (priv->synopsys_id < DWMAC_CORE_4_00)
6691 dev_info(priv->device, "COE Type %d\n", priv->hw->rx_csum);
6693 if (priv->plat->tx_coe)
6694 dev_info(priv->device, "TX Checksum insertion supported\n");
6696 if (priv->plat->pmt) {
6697 dev_info(priv->device, "Wake-Up On Lan supported\n");
6698 device_set_wakeup_capable(priv->device, 1);
6701 if (priv->dma_cap.tsoen)
6702 dev_info(priv->device, "TSO supported\n");
6704 priv->hw->vlan_fail_q_en = priv->plat->vlan_fail_q_en;
6705 priv->hw->vlan_fail_q = priv->plat->vlan_fail_q;
6707 /* Run HW quirks, if any */
6708 if (priv->hwif_quirks) {
6709 ret = priv->hwif_quirks(priv);
6714 /* Rx Watchdog is available in the COREs newer than the 3.40.
6715 * In some case, for example on bugged HW this feature
6716 * has to be disable and this can be done by passing the
6717 * riwt_off field from the platform.
6719 if (((priv->synopsys_id >= DWMAC_CORE_3_50) ||
6720 (priv->plat->has_xgmac)) && (!priv->plat->riwt_off)) {
6722 dev_info(priv->device,
6723 "Enable RX Mitigation via HW Watchdog Timer\n");
6729 static void stmmac_napi_add(struct net_device *dev)
6731 struct stmmac_priv *priv = netdev_priv(dev);
6734 maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
6736 for (queue = 0; queue < maxq; queue++) {
6737 struct stmmac_channel *ch = &priv->channel[queue];
6739 ch->priv_data = priv;
6741 spin_lock_init(&ch->lock);
6743 if (queue < priv->plat->rx_queues_to_use) {
6744 netif_napi_add(dev, &ch->rx_napi, stmmac_napi_poll_rx,
6747 if (queue < priv->plat->tx_queues_to_use) {
6748 netif_tx_napi_add(dev, &ch->tx_napi,
6749 stmmac_napi_poll_tx,
6752 if (queue < priv->plat->rx_queues_to_use &&
6753 queue < priv->plat->tx_queues_to_use) {
6754 netif_napi_add(dev, &ch->rxtx_napi,
6755 stmmac_napi_poll_rxtx,
6761 static void stmmac_napi_del(struct net_device *dev)
6763 struct stmmac_priv *priv = netdev_priv(dev);
6766 maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
6768 for (queue = 0; queue < maxq; queue++) {
6769 struct stmmac_channel *ch = &priv->channel[queue];
6771 if (queue < priv->plat->rx_queues_to_use)
6772 netif_napi_del(&ch->rx_napi);
6773 if (queue < priv->plat->tx_queues_to_use)
6774 netif_napi_del(&ch->tx_napi);
6775 if (queue < priv->plat->rx_queues_to_use &&
6776 queue < priv->plat->tx_queues_to_use) {
6777 netif_napi_del(&ch->rxtx_napi);
6782 int stmmac_reinit_queues(struct net_device *dev, u32 rx_cnt, u32 tx_cnt)
6784 struct stmmac_priv *priv = netdev_priv(dev);
6787 if (netif_running(dev))
6788 stmmac_release(dev);
6790 stmmac_napi_del(dev);
6792 priv->plat->rx_queues_to_use = rx_cnt;
6793 priv->plat->tx_queues_to_use = tx_cnt;
6795 stmmac_napi_add(dev);
6797 if (netif_running(dev))
6798 ret = stmmac_open(dev);
6803 int stmmac_reinit_ringparam(struct net_device *dev, u32 rx_size, u32 tx_size)
6805 struct stmmac_priv *priv = netdev_priv(dev);
6808 if (netif_running(dev))
6809 stmmac_release(dev);
6811 priv->dma_rx_size = rx_size;
6812 priv->dma_tx_size = tx_size;
6814 if (netif_running(dev))
6815 ret = stmmac_open(dev);
6820 #define SEND_VERIFY_MPAKCET_FMT "Send Verify mPacket lo_state=%d lp_state=%d\n"
6821 static void stmmac_fpe_lp_task(struct work_struct *work)
6823 struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
6825 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
6826 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
6827 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
6828 bool *hs_enable = &fpe_cfg->hs_enable;
6829 bool *enable = &fpe_cfg->enable;
6832 while (retries-- > 0) {
6833 /* Bail out immediately if FPE handshake is OFF */
6834 if (*lo_state == FPE_STATE_OFF || !*hs_enable)
6837 if (*lo_state == FPE_STATE_ENTERING_ON &&
6838 *lp_state == FPE_STATE_ENTERING_ON) {
6839 stmmac_fpe_configure(priv, priv->ioaddr,
6840 priv->plat->tx_queues_to_use,
6841 priv->plat->rx_queues_to_use,
6844 netdev_info(priv->dev, "configured FPE\n");
6846 *lo_state = FPE_STATE_ON;
6847 *lp_state = FPE_STATE_ON;
6848 netdev_info(priv->dev, "!!! BOTH FPE stations ON\n");
6852 if ((*lo_state == FPE_STATE_CAPABLE ||
6853 *lo_state == FPE_STATE_ENTERING_ON) &&
6854 *lp_state != FPE_STATE_ON) {
6855 netdev_info(priv->dev, SEND_VERIFY_MPAKCET_FMT,
6856 *lo_state, *lp_state);
6857 stmmac_fpe_send_mpacket(priv, priv->ioaddr,
6860 /* Sleep then retry */
6864 clear_bit(__FPE_TASK_SCHED, &priv->fpe_task_state);
6867 void stmmac_fpe_handshake(struct stmmac_priv *priv, bool enable)
6869 if (priv->plat->fpe_cfg->hs_enable != enable) {
6871 stmmac_fpe_send_mpacket(priv, priv->ioaddr,
6874 priv->plat->fpe_cfg->lo_fpe_state = FPE_STATE_OFF;
6875 priv->plat->fpe_cfg->lp_fpe_state = FPE_STATE_OFF;
6878 priv->plat->fpe_cfg->hs_enable = enable;
6884 * @device: device pointer
6885 * @plat_dat: platform data pointer
6886 * @res: stmmac resource pointer
6887 * Description: this is the main probe function used to
6888 * call the alloc_etherdev, allocate the priv structure.
6890 * returns 0 on success, otherwise errno.
6892 int stmmac_dvr_probe(struct device *device,
6893 struct plat_stmmacenet_data *plat_dat,
6894 struct stmmac_resources *res)
6896 struct net_device *ndev = NULL;
6897 struct stmmac_priv *priv;
6901 ndev = devm_alloc_etherdev_mqs(device, sizeof(struct stmmac_priv),
6902 MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES);
6906 SET_NETDEV_DEV(ndev, device);
6908 priv = netdev_priv(ndev);
6909 priv->device = device;
6912 stmmac_set_ethtool_ops(ndev);
6913 priv->pause = pause;
6914 priv->plat = plat_dat;
6915 priv->ioaddr = res->addr;
6916 priv->dev->base_addr = (unsigned long)res->addr;
6917 priv->plat->dma_cfg->multi_msi_en = priv->plat->multi_msi_en;
6919 priv->dev->irq = res->irq;
6920 priv->wol_irq = res->wol_irq;
6921 priv->lpi_irq = res->lpi_irq;
6922 priv->sfty_ce_irq = res->sfty_ce_irq;
6923 priv->sfty_ue_irq = res->sfty_ue_irq;
6924 for (i = 0; i < MTL_MAX_RX_QUEUES; i++)
6925 priv->rx_irq[i] = res->rx_irq[i];
6926 for (i = 0; i < MTL_MAX_TX_QUEUES; i++)
6927 priv->tx_irq[i] = res->tx_irq[i];
6929 if (!is_zero_ether_addr(res->mac))
6930 eth_hw_addr_set(priv->dev, res->mac);
6932 dev_set_drvdata(device, priv->dev);
6934 /* Verify driver arguments */
6935 stmmac_verify_args();
6937 priv->af_xdp_zc_qps = bitmap_zalloc(MTL_MAX_TX_QUEUES, GFP_KERNEL);
6938 if (!priv->af_xdp_zc_qps)
6941 /* Allocate workqueue */
6942 priv->wq = create_singlethread_workqueue("stmmac_wq");
6944 dev_err(priv->device, "failed to create workqueue\n");
6948 INIT_WORK(&priv->service_task, stmmac_service_task);
6950 /* Initialize Link Partner FPE workqueue */
6951 INIT_WORK(&priv->fpe_task, stmmac_fpe_lp_task);
6953 /* Override with kernel parameters if supplied XXX CRS XXX
6954 * this needs to have multiple instances
6956 if ((phyaddr >= 0) && (phyaddr <= 31))
6957 priv->plat->phy_addr = phyaddr;
6959 if (priv->plat->stmmac_rst) {
6960 ret = reset_control_assert(priv->plat->stmmac_rst);
6961 reset_control_deassert(priv->plat->stmmac_rst);
6962 /* Some reset controllers have only reset callback instead of
6963 * assert + deassert callbacks pair.
6965 if (ret == -ENOTSUPP)
6966 reset_control_reset(priv->plat->stmmac_rst);
6969 ret = reset_control_deassert(priv->plat->stmmac_ahb_rst);
6970 if (ret == -ENOTSUPP)
6971 dev_err(priv->device, "unable to bring out of ahb reset: %pe\n",
6974 /* Init MAC and get the capabilities */
6975 ret = stmmac_hw_init(priv);
6979 /* Only DWMAC core version 5.20 onwards supports HW descriptor prefetch.
6981 if (priv->synopsys_id < DWMAC_CORE_5_20)
6982 priv->plat->dma_cfg->dche = false;
6984 stmmac_check_ether_addr(priv);
6986 ndev->netdev_ops = &stmmac_netdev_ops;
6988 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
6991 ret = stmmac_tc_init(priv, priv);
6993 ndev->hw_features |= NETIF_F_HW_TC;
6996 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
6997 ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
6998 if (priv->plat->has_gmac4)
6999 ndev->hw_features |= NETIF_F_GSO_UDP_L4;
7001 dev_info(priv->device, "TSO feature enabled\n");
7004 if (priv->dma_cap.sphen) {
7005 ndev->hw_features |= NETIF_F_GRO;
7006 priv->sph_cap = true;
7007 priv->sph = priv->sph_cap;
7008 dev_info(priv->device, "SPH feature enabled\n");
7011 /* The current IP register MAC_HW_Feature1[ADDR64] only define
7012 * 32/40/64 bit width, but some SOC support others like i.MX8MP
7013 * support 34 bits but it map to 40 bits width in MAC_HW_Feature1[ADDR64].
7014 * So overwrite dma_cap.addr64 according to HW real design.
7016 if (priv->plat->addr64)
7017 priv->dma_cap.addr64 = priv->plat->addr64;
7019 if (priv->dma_cap.addr64) {
7020 ret = dma_set_mask_and_coherent(device,
7021 DMA_BIT_MASK(priv->dma_cap.addr64));
7023 dev_info(priv->device, "Using %d bits DMA width\n",
7024 priv->dma_cap.addr64);
7027 * If more than 32 bits can be addressed, make sure to
7028 * enable enhanced addressing mode.
7030 if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
7031 priv->plat->dma_cfg->eame = true;
7033 ret = dma_set_mask_and_coherent(device, DMA_BIT_MASK(32));
7035 dev_err(priv->device, "Failed to set DMA Mask\n");
7039 priv->dma_cap.addr64 = 32;
7043 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
7044 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
7045 #ifdef STMMAC_VLAN_TAG_USED
7046 /* Both mac100 and gmac support receive VLAN tag detection */
7047 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX;
7048 if (priv->dma_cap.vlhash) {
7049 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
7050 ndev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
7052 if (priv->dma_cap.vlins) {
7053 ndev->features |= NETIF_F_HW_VLAN_CTAG_TX;
7054 if (priv->dma_cap.dvlan)
7055 ndev->features |= NETIF_F_HW_VLAN_STAG_TX;
7058 priv->msg_enable = netif_msg_init(debug, default_msg_level);
7060 /* Initialize RSS */
7061 rxq = priv->plat->rx_queues_to_use;
7062 netdev_rss_key_fill(priv->rss.key, sizeof(priv->rss.key));
7063 for (i = 0; i < ARRAY_SIZE(priv->rss.table); i++)
7064 priv->rss.table[i] = ethtool_rxfh_indir_default(i, rxq);
7066 if (priv->dma_cap.rssen && priv->plat->rss_en)
7067 ndev->features |= NETIF_F_RXHASH;
7069 /* MTU range: 46 - hw-specific max */
7070 ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
7071 if (priv->plat->has_xgmac)
7072 ndev->max_mtu = XGMAC_JUMBO_LEN;
7073 else if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
7074 ndev->max_mtu = JUMBO_LEN;
7076 ndev->max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
7077 /* Will not overwrite ndev->max_mtu if plat->maxmtu > ndev->max_mtu
7078 * as well as plat->maxmtu < ndev->min_mtu which is a invalid range.
7080 if ((priv->plat->maxmtu < ndev->max_mtu) &&
7081 (priv->plat->maxmtu >= ndev->min_mtu))
7082 ndev->max_mtu = priv->plat->maxmtu;
7083 else if (priv->plat->maxmtu < ndev->min_mtu)
7084 dev_warn(priv->device,
7085 "%s: warning: maxmtu having invalid value (%d)\n",
7086 __func__, priv->plat->maxmtu);
7089 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
7091 /* Setup channels NAPI */
7092 stmmac_napi_add(ndev);
7094 mutex_init(&priv->lock);
7096 /* If a specific clk_csr value is passed from the platform
7097 * this means that the CSR Clock Range selection cannot be
7098 * changed at run-time and it is fixed. Viceversa the driver'll try to
7099 * set the MDC clock dynamically according to the csr actual
7102 if (priv->plat->clk_csr >= 0)
7103 priv->clk_csr = priv->plat->clk_csr;
7105 stmmac_clk_csr_set(priv);
7107 stmmac_check_pcs_mode(priv);
7109 pm_runtime_get_noresume(device);
7110 pm_runtime_set_active(device);
7111 if (!pm_runtime_enabled(device))
7112 pm_runtime_enable(device);
7114 if (priv->hw->pcs != STMMAC_PCS_TBI &&
7115 priv->hw->pcs != STMMAC_PCS_RTBI) {
7116 /* MDIO bus Registration */
7117 ret = stmmac_mdio_register(ndev);
7119 dev_err(priv->device,
7120 "%s: MDIO bus (id: %d) registration failed",
7121 __func__, priv->plat->bus_id);
7122 goto error_mdio_register;
7126 if (priv->plat->speed_mode_2500)
7127 priv->plat->speed_mode_2500(ndev, priv->plat->bsp_priv);
7129 if (priv->plat->mdio_bus_data && priv->plat->mdio_bus_data->has_xpcs) {
7130 ret = stmmac_xpcs_setup(priv->mii);
7132 goto error_xpcs_setup;
7135 ret = stmmac_phy_setup(priv);
7137 netdev_err(ndev, "failed to setup phy (%d)\n", ret);
7138 goto error_phy_setup;
7141 ret = register_netdev(ndev);
7143 dev_err(priv->device, "%s: ERROR %i registering the device\n",
7145 goto error_netdev_register;
7148 if (priv->plat->serdes_powerup) {
7149 ret = priv->plat->serdes_powerup(ndev,
7150 priv->plat->bsp_priv);
7153 goto error_serdes_powerup;
7156 #ifdef CONFIG_DEBUG_FS
7157 stmmac_init_fs(ndev);
7160 if (priv->plat->dump_debug_regs)
7161 priv->plat->dump_debug_regs(priv->plat->bsp_priv);
7163 /* Let pm_runtime_put() disable the clocks.
7164 * If CONFIG_PM is not enabled, the clocks will stay powered.
7166 pm_runtime_put(device);
7170 error_serdes_powerup:
7171 unregister_netdev(ndev);
7172 error_netdev_register:
7173 phylink_destroy(priv->phylink);
7176 if (priv->hw->pcs != STMMAC_PCS_TBI &&
7177 priv->hw->pcs != STMMAC_PCS_RTBI)
7178 stmmac_mdio_unregister(ndev);
7179 error_mdio_register:
7180 stmmac_napi_del(ndev);
7182 destroy_workqueue(priv->wq);
7183 bitmap_free(priv->af_xdp_zc_qps);
7187 EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
7191 * @dev: device pointer
7192 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
7193 * changes the link status, releases the DMA descriptor rings.
7195 int stmmac_dvr_remove(struct device *dev)
7197 struct net_device *ndev = dev_get_drvdata(dev);
7198 struct stmmac_priv *priv = netdev_priv(ndev);
7200 netdev_info(priv->dev, "%s: removing driver", __func__);
7202 stmmac_stop_all_dma(priv);
7203 stmmac_mac_set(priv, priv->ioaddr, false);
7204 netif_carrier_off(ndev);
7205 unregister_netdev(ndev);
7207 /* Serdes power down needs to happen after VLAN filter
7208 * is deleted that is triggered by unregister_netdev().
7210 if (priv->plat->serdes_powerdown)
7211 priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv);
7213 #ifdef CONFIG_DEBUG_FS
7214 stmmac_exit_fs(ndev);
7216 phylink_destroy(priv->phylink);
7217 if (priv->plat->stmmac_rst)
7218 reset_control_assert(priv->plat->stmmac_rst);
7219 reset_control_assert(priv->plat->stmmac_ahb_rst);
7220 pm_runtime_put(dev);
7221 pm_runtime_disable(dev);
7222 if (priv->hw->pcs != STMMAC_PCS_TBI &&
7223 priv->hw->pcs != STMMAC_PCS_RTBI)
7224 stmmac_mdio_unregister(ndev);
7225 destroy_workqueue(priv->wq);
7226 mutex_destroy(&priv->lock);
7227 bitmap_free(priv->af_xdp_zc_qps);
7231 EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
7234 * stmmac_suspend - suspend callback
7235 * @dev: device pointer
7236 * Description: this is the function to suspend the device and it is called
7237 * by the platform driver to stop the network queue, release the resources,
7238 * program the PMT register (for WoL), clean and release driver resources.
7240 int stmmac_suspend(struct device *dev)
7242 struct net_device *ndev = dev_get_drvdata(dev);
7243 struct stmmac_priv *priv = netdev_priv(ndev);
7246 if (!ndev || !netif_running(ndev))
7249 mutex_lock(&priv->lock);
7251 netif_device_detach(ndev);
7253 stmmac_disable_all_queues(priv);
7255 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
7256 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
7258 if (priv->eee_enabled) {
7259 priv->tx_path_in_lpi_mode = false;
7260 del_timer_sync(&priv->eee_ctrl_timer);
7263 /* Stop TX/RX DMA */
7264 stmmac_stop_all_dma(priv);
7266 if (priv->plat->serdes_powerdown)
7267 priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv);
7269 /* Enable Power down mode by programming the PMT regs */
7270 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7271 stmmac_pmt(priv, priv->hw, priv->wolopts);
7274 stmmac_mac_set(priv, priv->ioaddr, false);
7275 pinctrl_pm_select_sleep_state(priv->device);
7278 mutex_unlock(&priv->lock);
7281 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7282 phylink_suspend(priv->phylink, true);
7284 if (device_may_wakeup(priv->device))
7285 phylink_speed_down(priv->phylink, false);
7286 phylink_suspend(priv->phylink, false);
7290 if (priv->dma_cap.fpesel) {
7292 stmmac_fpe_configure(priv, priv->ioaddr,
7293 priv->plat->tx_queues_to_use,
7294 priv->plat->rx_queues_to_use, false);
7296 stmmac_fpe_handshake(priv, false);
7297 stmmac_fpe_stop_wq(priv);
7300 priv->speed = SPEED_UNKNOWN;
7303 EXPORT_SYMBOL_GPL(stmmac_suspend);
7306 * stmmac_reset_queues_param - reset queue parameters
7307 * @priv: device pointer
7309 static void stmmac_reset_queues_param(struct stmmac_priv *priv)
7311 u32 rx_cnt = priv->plat->rx_queues_to_use;
7312 u32 tx_cnt = priv->plat->tx_queues_to_use;
7315 for (queue = 0; queue < rx_cnt; queue++) {
7316 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
7322 for (queue = 0; queue < tx_cnt; queue++) {
7323 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
7329 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
7334 * stmmac_resume - resume callback
7335 * @dev: device pointer
7336 * Description: when resume this function is invoked to setup the DMA and CORE
7337 * in a usable state.
7339 int stmmac_resume(struct device *dev)
7341 struct net_device *ndev = dev_get_drvdata(dev);
7342 struct stmmac_priv *priv = netdev_priv(ndev);
7345 if (!netif_running(ndev))
7348 /* Power Down bit, into the PM register, is cleared
7349 * automatically as soon as a magic packet or a Wake-up frame
7350 * is received. Anyway, it's better to manually clear
7351 * this bit because it can generate problems while resuming
7352 * from another devices (e.g. serial console).
7354 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7355 mutex_lock(&priv->lock);
7356 stmmac_pmt(priv, priv->hw, 0);
7357 mutex_unlock(&priv->lock);
7360 pinctrl_pm_select_default_state(priv->device);
7361 /* reset the phy so that it's ready */
7363 stmmac_mdio_reset(priv->mii);
7366 if (priv->plat->serdes_powerup) {
7367 ret = priv->plat->serdes_powerup(ndev,
7368 priv->plat->bsp_priv);
7375 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7376 phylink_resume(priv->phylink);
7378 phylink_resume(priv->phylink);
7379 if (device_may_wakeup(priv->device))
7380 phylink_speed_up(priv->phylink);
7385 mutex_lock(&priv->lock);
7387 stmmac_reset_queues_param(priv);
7389 stmmac_free_tx_skbufs(priv);
7390 stmmac_clear_descriptors(priv);
7392 stmmac_hw_setup(ndev, false);
7393 stmmac_init_coalesce(priv);
7394 stmmac_set_rx_mode(ndev);
7396 stmmac_restore_hw_vlan_rx_fltr(priv, ndev, priv->hw);
7398 stmmac_enable_all_queues(priv);
7400 mutex_unlock(&priv->lock);
7403 netif_device_attach(ndev);
7407 EXPORT_SYMBOL_GPL(stmmac_resume);
7410 static int __init stmmac_cmdline_opt(char *str)
7416 while ((opt = strsep(&str, ",")) != NULL) {
7417 if (!strncmp(opt, "debug:", 6)) {
7418 if (kstrtoint(opt + 6, 0, &debug))
7420 } else if (!strncmp(opt, "phyaddr:", 8)) {
7421 if (kstrtoint(opt + 8, 0, &phyaddr))
7423 } else if (!strncmp(opt, "buf_sz:", 7)) {
7424 if (kstrtoint(opt + 7, 0, &buf_sz))
7426 } else if (!strncmp(opt, "tc:", 3)) {
7427 if (kstrtoint(opt + 3, 0, &tc))
7429 } else if (!strncmp(opt, "watchdog:", 9)) {
7430 if (kstrtoint(opt + 9, 0, &watchdog))
7432 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
7433 if (kstrtoint(opt + 10, 0, &flow_ctrl))
7435 } else if (!strncmp(opt, "pause:", 6)) {
7436 if (kstrtoint(opt + 6, 0, &pause))
7438 } else if (!strncmp(opt, "eee_timer:", 10)) {
7439 if (kstrtoint(opt + 10, 0, &eee_timer))
7441 } else if (!strncmp(opt, "chain_mode:", 11)) {
7442 if (kstrtoint(opt + 11, 0, &chain_mode))
7449 pr_err("%s: ERROR broken module parameter conversion", __func__);
7453 __setup("stmmaceth=", stmmac_cmdline_opt);
7456 static int __init stmmac_init(void)
7458 #ifdef CONFIG_DEBUG_FS
7459 /* Create debugfs main directory if it doesn't exist yet */
7461 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
7462 register_netdevice_notifier(&stmmac_notifier);
7468 static void __exit stmmac_exit(void)
7470 #ifdef CONFIG_DEBUG_FS
7471 unregister_netdevice_notifier(&stmmac_notifier);
7472 debugfs_remove_recursive(stmmac_fs_dir);
7476 module_init(stmmac_init)
7477 module_exit(stmmac_exit)
7479 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
7480 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
7481 MODULE_LICENSE("GPL");