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 void 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; /* 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);
425 * stmmac_disable_eee_mode - disable and exit from LPI mode
426 * @priv: driver private structure
427 * Description: this function is to exit and disable EEE in case of
428 * LPI state is true. This is called by the xmit.
430 void stmmac_disable_eee_mode(struct stmmac_priv *priv)
432 if (!priv->eee_sw_timer_en) {
433 stmmac_lpi_entry_timer_config(priv, 0);
437 stmmac_reset_eee_mode(priv, priv->hw);
438 del_timer_sync(&priv->eee_ctrl_timer);
439 priv->tx_path_in_lpi_mode = false;
443 * stmmac_eee_ctrl_timer - EEE TX SW timer.
444 * @t: timer_list struct containing private info
446 * if there is no data transfer and if we are not in LPI state,
447 * then MAC Transmitter can be moved to LPI state.
449 static void stmmac_eee_ctrl_timer(struct timer_list *t)
451 struct stmmac_priv *priv = from_timer(priv, t, eee_ctrl_timer);
453 stmmac_enable_eee_mode(priv);
454 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer));
458 * stmmac_eee_init - init EEE
459 * @priv: driver private structure
461 * if the GMAC supports the EEE (from the HW cap reg) and the phy device
462 * can also manage EEE, this function enable the LPI state and start related
465 bool stmmac_eee_init(struct stmmac_priv *priv)
467 int eee_tw_timer = priv->eee_tw_timer;
469 /* Using PCS we cannot dial with the phy registers at this stage
470 * so we do not support extra feature like EEE.
472 if (priv->hw->pcs == STMMAC_PCS_TBI ||
473 priv->hw->pcs == STMMAC_PCS_RTBI)
476 /* Check if MAC core supports the EEE feature. */
477 if (!priv->dma_cap.eee)
480 mutex_lock(&priv->lock);
482 /* Check if it needs to be deactivated */
483 if (!priv->eee_active) {
484 if (priv->eee_enabled) {
485 netdev_dbg(priv->dev, "disable EEE\n");
486 stmmac_lpi_entry_timer_config(priv, 0);
487 del_timer_sync(&priv->eee_ctrl_timer);
488 stmmac_set_eee_timer(priv, priv->hw, 0, eee_tw_timer);
490 xpcs_config_eee(priv->hw->xpcs,
491 priv->plat->mult_fact_100ns,
494 mutex_unlock(&priv->lock);
498 if (priv->eee_active && !priv->eee_enabled) {
499 timer_setup(&priv->eee_ctrl_timer, stmmac_eee_ctrl_timer, 0);
500 stmmac_set_eee_timer(priv, priv->hw, STMMAC_DEFAULT_LIT_LS,
503 xpcs_config_eee(priv->hw->xpcs,
504 priv->plat->mult_fact_100ns,
508 if (priv->plat->has_gmac4 && priv->tx_lpi_timer <= STMMAC_ET_MAX) {
509 del_timer_sync(&priv->eee_ctrl_timer);
510 priv->tx_path_in_lpi_mode = false;
511 stmmac_lpi_entry_timer_config(priv, 1);
513 stmmac_lpi_entry_timer_config(priv, 0);
514 mod_timer(&priv->eee_ctrl_timer,
515 STMMAC_LPI_T(priv->tx_lpi_timer));
518 mutex_unlock(&priv->lock);
519 netdev_dbg(priv->dev, "Energy-Efficient Ethernet initialized\n");
523 /* stmmac_get_tx_hwtstamp - get HW TX timestamps
524 * @priv: driver private structure
525 * @p : descriptor pointer
526 * @skb : the socket buffer
528 * This function will read timestamp from the descriptor & pass it to stack.
529 * and also perform some sanity checks.
531 static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
532 struct dma_desc *p, struct sk_buff *skb)
534 struct skb_shared_hwtstamps shhwtstamp;
538 if (!priv->hwts_tx_en)
541 /* exit if skb doesn't support hw tstamp */
542 if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
545 /* check tx tstamp status */
546 if (stmmac_get_tx_timestamp_status(priv, p)) {
547 stmmac_get_timestamp(priv, p, priv->adv_ts, &ns);
549 } else if (!stmmac_get_mac_tx_timestamp(priv, priv->hw, &ns)) {
554 ns -= priv->plat->cdc_error_adj;
556 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
557 shhwtstamp.hwtstamp = ns_to_ktime(ns);
559 netdev_dbg(priv->dev, "get valid TX hw timestamp %llu\n", ns);
560 /* pass tstamp to stack */
561 skb_tstamp_tx(skb, &shhwtstamp);
565 /* stmmac_get_rx_hwtstamp - get HW RX timestamps
566 * @priv: driver private structure
567 * @p : descriptor pointer
568 * @np : next descriptor pointer
569 * @skb : the socket buffer
571 * This function will read received packet's timestamp from the descriptor
572 * and pass it to stack. It also perform some sanity checks.
574 static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,
575 struct dma_desc *np, struct sk_buff *skb)
577 struct skb_shared_hwtstamps *shhwtstamp = NULL;
578 struct dma_desc *desc = p;
581 if (!priv->hwts_rx_en)
583 /* For GMAC4, the valid timestamp is from CTX next desc. */
584 if (priv->plat->has_gmac4 || priv->plat->has_xgmac)
587 /* Check if timestamp is available */
588 if (stmmac_get_rx_timestamp_status(priv, p, np, priv->adv_ts)) {
589 stmmac_get_timestamp(priv, desc, priv->adv_ts, &ns);
591 ns -= priv->plat->cdc_error_adj;
593 netdev_dbg(priv->dev, "get valid RX hw timestamp %llu\n", ns);
594 shhwtstamp = skb_hwtstamps(skb);
595 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
596 shhwtstamp->hwtstamp = ns_to_ktime(ns);
598 netdev_dbg(priv->dev, "cannot get RX hw timestamp\n");
603 * stmmac_hwtstamp_set - control hardware timestamping.
604 * @dev: device pointer.
605 * @ifr: An IOCTL specific structure, that can contain a pointer to
606 * a proprietary structure used to pass information to the driver.
608 * This function configures the MAC to enable/disable both outgoing(TX)
609 * and incoming(RX) packets time stamping based on user input.
611 * 0 on success and an appropriate -ve integer on failure.
613 static int stmmac_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
615 struct stmmac_priv *priv = netdev_priv(dev);
616 struct hwtstamp_config config;
619 u32 ptp_over_ipv4_udp = 0;
620 u32 ptp_over_ipv6_udp = 0;
621 u32 ptp_over_ethernet = 0;
622 u32 snap_type_sel = 0;
623 u32 ts_master_en = 0;
626 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
627 netdev_alert(priv->dev, "No support for HW time stamping\n");
628 priv->hwts_tx_en = 0;
629 priv->hwts_rx_en = 0;
634 if (copy_from_user(&config, ifr->ifr_data,
638 netdev_dbg(priv->dev, "%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
639 __func__, config.flags, config.tx_type, config.rx_filter);
641 if (config.tx_type != HWTSTAMP_TX_OFF &&
642 config.tx_type != HWTSTAMP_TX_ON)
646 switch (config.rx_filter) {
647 case HWTSTAMP_FILTER_NONE:
648 /* time stamp no incoming packet at all */
649 config.rx_filter = HWTSTAMP_FILTER_NONE;
652 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
653 /* PTP v1, UDP, any kind of event packet */
654 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
655 /* 'xmac' hardware can support Sync, Pdelay_Req and
656 * Pdelay_resp by setting bit14 and bits17/16 to 01
657 * This leaves Delay_Req timestamps out.
658 * Enable all events *and* general purpose message
661 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
662 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
663 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
666 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
667 /* PTP v1, UDP, Sync packet */
668 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
669 /* take time stamp for SYNC messages only */
670 ts_event_en = PTP_TCR_TSEVNTENA;
672 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
673 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
676 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
677 /* PTP v1, UDP, Delay_req packet */
678 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
679 /* take time stamp for Delay_Req messages only */
680 ts_master_en = PTP_TCR_TSMSTRENA;
681 ts_event_en = PTP_TCR_TSEVNTENA;
683 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
684 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
687 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
688 /* PTP v2, UDP, any kind of event packet */
689 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
690 ptp_v2 = PTP_TCR_TSVER2ENA;
691 /* take time stamp for all event messages */
692 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
694 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
695 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
698 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
699 /* PTP v2, UDP, Sync packet */
700 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
701 ptp_v2 = PTP_TCR_TSVER2ENA;
702 /* take time stamp for SYNC messages only */
703 ts_event_en = PTP_TCR_TSEVNTENA;
705 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
706 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
709 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
710 /* PTP v2, UDP, Delay_req packet */
711 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
712 ptp_v2 = PTP_TCR_TSVER2ENA;
713 /* take time stamp for Delay_Req messages only */
714 ts_master_en = PTP_TCR_TSMSTRENA;
715 ts_event_en = PTP_TCR_TSEVNTENA;
717 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
718 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
721 case HWTSTAMP_FILTER_PTP_V2_EVENT:
722 /* PTP v2/802.AS1 any layer, any kind of event packet */
723 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
724 ptp_v2 = PTP_TCR_TSVER2ENA;
725 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
726 if (priv->synopsys_id < DWMAC_CORE_4_10)
727 ts_event_en = PTP_TCR_TSEVNTENA;
728 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
729 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
730 ptp_over_ethernet = PTP_TCR_TSIPENA;
733 case HWTSTAMP_FILTER_PTP_V2_SYNC:
734 /* PTP v2/802.AS1, any layer, Sync packet */
735 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
736 ptp_v2 = PTP_TCR_TSVER2ENA;
737 /* take time stamp for SYNC messages only */
738 ts_event_en = PTP_TCR_TSEVNTENA;
740 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
741 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
742 ptp_over_ethernet = PTP_TCR_TSIPENA;
745 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
746 /* PTP v2/802.AS1, any layer, Delay_req packet */
747 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
748 ptp_v2 = PTP_TCR_TSVER2ENA;
749 /* take time stamp for Delay_Req messages only */
750 ts_master_en = PTP_TCR_TSMSTRENA;
751 ts_event_en = PTP_TCR_TSEVNTENA;
753 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
754 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
755 ptp_over_ethernet = PTP_TCR_TSIPENA;
758 case HWTSTAMP_FILTER_NTP_ALL:
759 case HWTSTAMP_FILTER_ALL:
760 /* time stamp any incoming packet */
761 config.rx_filter = HWTSTAMP_FILTER_ALL;
762 tstamp_all = PTP_TCR_TSENALL;
769 switch (config.rx_filter) {
770 case HWTSTAMP_FILTER_NONE:
771 config.rx_filter = HWTSTAMP_FILTER_NONE;
774 /* PTP v1, UDP, any kind of event packet */
775 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
779 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
780 priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
782 priv->systime_flags = STMMAC_HWTS_ACTIVE;
784 if (priv->hwts_tx_en || priv->hwts_rx_en) {
785 priv->systime_flags |= tstamp_all | ptp_v2 |
786 ptp_over_ethernet | ptp_over_ipv6_udp |
787 ptp_over_ipv4_udp | ts_event_en |
788 ts_master_en | snap_type_sel;
791 stmmac_config_hw_tstamping(priv, priv->ptpaddr, priv->systime_flags);
793 memcpy(&priv->tstamp_config, &config, sizeof(config));
795 return copy_to_user(ifr->ifr_data, &config,
796 sizeof(config)) ? -EFAULT : 0;
800 * stmmac_hwtstamp_get - read hardware timestamping.
801 * @dev: device pointer.
802 * @ifr: An IOCTL specific structure, that can contain a pointer to
803 * a proprietary structure used to pass information to the driver.
805 * This function obtain the current hardware timestamping settings
808 static int stmmac_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
810 struct stmmac_priv *priv = netdev_priv(dev);
811 struct hwtstamp_config *config = &priv->tstamp_config;
813 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
816 return copy_to_user(ifr->ifr_data, config,
817 sizeof(*config)) ? -EFAULT : 0;
821 * stmmac_init_tstamp_counter - init hardware timestamping counter
822 * @priv: driver private structure
823 * @systime_flags: timestamping flags
825 * Initialize hardware counter for packet timestamping.
826 * This is valid as long as the interface is open and not suspended.
827 * Will be rerun after resuming from suspend, case in which the timestamping
828 * flags updated by stmmac_hwtstamp_set() also need to be restored.
830 int stmmac_init_tstamp_counter(struct stmmac_priv *priv, u32 systime_flags)
832 bool xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
833 struct timespec64 now;
838 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
841 ret = clk_prepare_enable(priv->plat->clk_ptp_ref);
843 netdev_warn(priv->dev,
844 "failed to enable PTP reference clock: %pe\n",
849 stmmac_config_hw_tstamping(priv, priv->ptpaddr, systime_flags);
850 priv->systime_flags = systime_flags;
852 /* program Sub Second Increment reg */
853 stmmac_config_sub_second_increment(priv, priv->ptpaddr,
854 priv->plat->clk_ptp_rate,
856 temp = div_u64(1000000000ULL, sec_inc);
858 /* Store sub second increment for later use */
859 priv->sub_second_inc = sec_inc;
861 /* calculate default added value:
863 * addend = (2^32)/freq_div_ratio;
864 * where, freq_div_ratio = 1e9ns/sec_inc
866 temp = (u64)(temp << 32);
867 priv->default_addend = div_u64(temp, priv->plat->clk_ptp_rate);
868 stmmac_config_addend(priv, priv->ptpaddr, priv->default_addend);
870 /* initialize system time */
871 ktime_get_real_ts64(&now);
873 /* lower 32 bits of tv_sec are safe until y2106 */
874 stmmac_init_systime(priv, priv->ptpaddr, (u32)now.tv_sec, now.tv_nsec);
878 EXPORT_SYMBOL_GPL(stmmac_init_tstamp_counter);
881 * stmmac_init_ptp - init PTP
882 * @priv: driver private structure
883 * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
884 * This is done by looking at the HW cap. register.
885 * This function also registers the ptp driver.
887 static int stmmac_init_ptp(struct stmmac_priv *priv)
889 bool xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
892 ret = stmmac_init_tstamp_counter(priv, STMMAC_HWTS_ACTIVE);
897 /* Check if adv_ts can be enabled for dwmac 4.x / xgmac core */
898 if (xmac && priv->dma_cap.atime_stamp)
900 /* Dwmac 3.x core with extend_desc can support adv_ts */
901 else if (priv->extend_desc && priv->dma_cap.atime_stamp)
904 if (priv->dma_cap.time_stamp)
905 netdev_info(priv->dev, "IEEE 1588-2002 Timestamp supported\n");
908 netdev_info(priv->dev,
909 "IEEE 1588-2008 Advanced Timestamp supported\n");
911 priv->hwts_tx_en = 0;
912 priv->hwts_rx_en = 0;
914 stmmac_ptp_register(priv);
919 static void stmmac_release_ptp(struct stmmac_priv *priv)
921 clk_disable_unprepare(priv->plat->clk_ptp_ref);
922 stmmac_ptp_unregister(priv);
926 * stmmac_mac_flow_ctrl - Configure flow control in all queues
927 * @priv: driver private structure
928 * @duplex: duplex passed to the next function
929 * Description: It is used for configuring the flow control in all queues
931 static void stmmac_mac_flow_ctrl(struct stmmac_priv *priv, u32 duplex)
933 u32 tx_cnt = priv->plat->tx_queues_to_use;
935 stmmac_flow_ctrl(priv, priv->hw, duplex, priv->flow_ctrl,
936 priv->pause, tx_cnt);
939 static void stmmac_validate(struct phylink_config *config,
940 unsigned long *supported,
941 struct phylink_link_state *state)
943 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
944 __ETHTOOL_DECLARE_LINK_MODE_MASK(mac_supported) = { 0, };
945 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
946 int tx_cnt = priv->plat->tx_queues_to_use;
947 int max_speed = priv->plat->max_speed;
949 phylink_set(mac_supported, 10baseT_Half);
950 phylink_set(mac_supported, 10baseT_Full);
951 phylink_set(mac_supported, 100baseT_Half);
952 phylink_set(mac_supported, 100baseT_Full);
953 phylink_set(mac_supported, 1000baseT_Half);
954 phylink_set(mac_supported, 1000baseT_Full);
955 phylink_set(mac_supported, 1000baseKX_Full);
957 phylink_set(mac_supported, Autoneg);
958 phylink_set(mac_supported, Pause);
959 phylink_set(mac_supported, Asym_Pause);
960 phylink_set_port_modes(mac_supported);
962 /* Cut down 1G if asked to */
963 if ((max_speed > 0) && (max_speed < 1000)) {
964 phylink_set(mask, 1000baseT_Full);
965 phylink_set(mask, 1000baseX_Full);
966 } else if (priv->plat->has_gmac4) {
967 if (!max_speed || max_speed >= 2500) {
968 phylink_set(mac_supported, 2500baseT_Full);
969 phylink_set(mac_supported, 2500baseX_Full);
971 } else if (priv->plat->has_xgmac) {
972 if (!max_speed || (max_speed >= 2500)) {
973 phylink_set(mac_supported, 2500baseT_Full);
974 phylink_set(mac_supported, 2500baseX_Full);
976 if (!max_speed || (max_speed >= 5000)) {
977 phylink_set(mac_supported, 5000baseT_Full);
979 if (!max_speed || (max_speed >= 10000)) {
980 phylink_set(mac_supported, 10000baseSR_Full);
981 phylink_set(mac_supported, 10000baseLR_Full);
982 phylink_set(mac_supported, 10000baseER_Full);
983 phylink_set(mac_supported, 10000baseLRM_Full);
984 phylink_set(mac_supported, 10000baseT_Full);
985 phylink_set(mac_supported, 10000baseKX4_Full);
986 phylink_set(mac_supported, 10000baseKR_Full);
988 if (!max_speed || (max_speed >= 25000)) {
989 phylink_set(mac_supported, 25000baseCR_Full);
990 phylink_set(mac_supported, 25000baseKR_Full);
991 phylink_set(mac_supported, 25000baseSR_Full);
993 if (!max_speed || (max_speed >= 40000)) {
994 phylink_set(mac_supported, 40000baseKR4_Full);
995 phylink_set(mac_supported, 40000baseCR4_Full);
996 phylink_set(mac_supported, 40000baseSR4_Full);
997 phylink_set(mac_supported, 40000baseLR4_Full);
999 if (!max_speed || (max_speed >= 50000)) {
1000 phylink_set(mac_supported, 50000baseCR2_Full);
1001 phylink_set(mac_supported, 50000baseKR2_Full);
1002 phylink_set(mac_supported, 50000baseSR2_Full);
1003 phylink_set(mac_supported, 50000baseKR_Full);
1004 phylink_set(mac_supported, 50000baseSR_Full);
1005 phylink_set(mac_supported, 50000baseCR_Full);
1006 phylink_set(mac_supported, 50000baseLR_ER_FR_Full);
1007 phylink_set(mac_supported, 50000baseDR_Full);
1009 if (!max_speed || (max_speed >= 100000)) {
1010 phylink_set(mac_supported, 100000baseKR4_Full);
1011 phylink_set(mac_supported, 100000baseSR4_Full);
1012 phylink_set(mac_supported, 100000baseCR4_Full);
1013 phylink_set(mac_supported, 100000baseLR4_ER4_Full);
1014 phylink_set(mac_supported, 100000baseKR2_Full);
1015 phylink_set(mac_supported, 100000baseSR2_Full);
1016 phylink_set(mac_supported, 100000baseCR2_Full);
1017 phylink_set(mac_supported, 100000baseLR2_ER2_FR2_Full);
1018 phylink_set(mac_supported, 100000baseDR2_Full);
1022 /* Half-Duplex can only work with single queue */
1024 phylink_set(mask, 10baseT_Half);
1025 phylink_set(mask, 100baseT_Half);
1026 phylink_set(mask, 1000baseT_Half);
1029 linkmode_and(supported, supported, mac_supported);
1030 linkmode_andnot(supported, supported, mask);
1032 linkmode_and(state->advertising, state->advertising, mac_supported);
1033 linkmode_andnot(state->advertising, state->advertising, mask);
1035 /* If PCS is supported, check which modes it supports. */
1037 xpcs_validate(priv->hw->xpcs, supported, state);
1040 static void stmmac_mac_config(struct phylink_config *config, unsigned int mode,
1041 const struct phylink_link_state *state)
1043 /* Nothing to do, xpcs_config() handles everything */
1046 static void stmmac_fpe_link_state_handle(struct stmmac_priv *priv, bool is_up)
1048 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
1049 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
1050 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
1051 bool *hs_enable = &fpe_cfg->hs_enable;
1053 if (is_up && *hs_enable) {
1054 stmmac_fpe_send_mpacket(priv, priv->ioaddr, MPACKET_VERIFY);
1056 *lo_state = FPE_STATE_OFF;
1057 *lp_state = FPE_STATE_OFF;
1061 static void stmmac_mac_link_down(struct phylink_config *config,
1062 unsigned int mode, phy_interface_t interface)
1064 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
1066 stmmac_mac_set(priv, priv->ioaddr, false);
1067 priv->eee_active = false;
1068 priv->tx_lpi_enabled = false;
1069 priv->eee_enabled = stmmac_eee_init(priv);
1070 stmmac_set_eee_pls(priv, priv->hw, false);
1072 if (priv->dma_cap.fpesel)
1073 stmmac_fpe_link_state_handle(priv, false);
1076 static void stmmac_mac_link_up(struct phylink_config *config,
1077 struct phy_device *phy,
1078 unsigned int mode, phy_interface_t interface,
1079 int speed, int duplex,
1080 bool tx_pause, bool rx_pause)
1082 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
1085 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
1086 ctrl &= ~priv->hw->link.speed_mask;
1088 if (interface == PHY_INTERFACE_MODE_USXGMII) {
1091 ctrl |= priv->hw->link.xgmii.speed10000;
1094 ctrl |= priv->hw->link.xgmii.speed5000;
1097 ctrl |= priv->hw->link.xgmii.speed2500;
1102 } else if (interface == PHY_INTERFACE_MODE_XLGMII) {
1105 ctrl |= priv->hw->link.xlgmii.speed100000;
1108 ctrl |= priv->hw->link.xlgmii.speed50000;
1111 ctrl |= priv->hw->link.xlgmii.speed40000;
1114 ctrl |= priv->hw->link.xlgmii.speed25000;
1117 ctrl |= priv->hw->link.xgmii.speed10000;
1120 ctrl |= priv->hw->link.speed2500;
1123 ctrl |= priv->hw->link.speed1000;
1131 ctrl |= priv->hw->link.speed2500;
1134 ctrl |= priv->hw->link.speed1000;
1137 ctrl |= priv->hw->link.speed100;
1140 ctrl |= priv->hw->link.speed10;
1147 priv->speed = speed;
1149 if (priv->plat->fix_mac_speed)
1150 priv->plat->fix_mac_speed(priv->plat->bsp_priv, speed);
1153 ctrl &= ~priv->hw->link.duplex;
1155 ctrl |= priv->hw->link.duplex;
1157 /* Flow Control operation */
1158 if (tx_pause && rx_pause)
1159 stmmac_mac_flow_ctrl(priv, duplex);
1161 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
1163 stmmac_mac_set(priv, priv->ioaddr, true);
1164 if (phy && priv->dma_cap.eee) {
1165 priv->eee_active = phy_init_eee(phy, 1) >= 0;
1166 priv->eee_enabled = stmmac_eee_init(priv);
1167 priv->tx_lpi_enabled = priv->eee_enabled;
1168 stmmac_set_eee_pls(priv, priv->hw, true);
1171 if (priv->dma_cap.fpesel)
1172 stmmac_fpe_link_state_handle(priv, true);
1175 static const struct phylink_mac_ops stmmac_phylink_mac_ops = {
1176 .validate = stmmac_validate,
1177 .mac_config = stmmac_mac_config,
1178 .mac_link_down = stmmac_mac_link_down,
1179 .mac_link_up = stmmac_mac_link_up,
1183 * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
1184 * @priv: driver private structure
1185 * Description: this is to verify if the HW supports the PCS.
1186 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
1187 * configured for the TBI, RTBI, or SGMII PHY interface.
1189 static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
1191 int interface = priv->plat->interface;
1193 if (priv->dma_cap.pcs) {
1194 if ((interface == PHY_INTERFACE_MODE_RGMII) ||
1195 (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
1196 (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
1197 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
1198 netdev_dbg(priv->dev, "PCS RGMII support enabled\n");
1199 priv->hw->pcs = STMMAC_PCS_RGMII;
1200 } else if (interface == PHY_INTERFACE_MODE_SGMII) {
1201 netdev_dbg(priv->dev, "PCS SGMII support enabled\n");
1202 priv->hw->pcs = STMMAC_PCS_SGMII;
1208 * stmmac_init_phy - PHY initialization
1209 * @dev: net device structure
1210 * Description: it initializes the driver's PHY state, and attaches the PHY
1211 * to the mac driver.
1215 static int stmmac_init_phy(struct net_device *dev)
1217 struct stmmac_priv *priv = netdev_priv(dev);
1218 struct device_node *node;
1221 node = priv->plat->phylink_node;
1224 ret = phylink_of_phy_connect(priv->phylink, node, 0);
1226 /* Some DT bindings do not set-up the PHY handle. Let's try to
1230 int addr = priv->plat->phy_addr;
1231 struct phy_device *phydev;
1233 phydev = mdiobus_get_phy(priv->mii, addr);
1235 netdev_err(priv->dev, "no phy at addr %d\n", addr);
1239 ret = phylink_connect_phy(priv->phylink, phydev);
1242 if (!priv->plat->pmt) {
1243 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1245 phylink_ethtool_get_wol(priv->phylink, &wol);
1246 device_set_wakeup_capable(priv->device, !!wol.supported);
1252 static int stmmac_phy_setup(struct stmmac_priv *priv)
1254 struct stmmac_mdio_bus_data *mdio_bus_data = priv->plat->mdio_bus_data;
1255 struct fwnode_handle *fwnode = of_fwnode_handle(priv->plat->phylink_node);
1256 int mode = priv->plat->phy_interface;
1257 struct phylink *phylink;
1259 priv->phylink_config.dev = &priv->dev->dev;
1260 priv->phylink_config.type = PHYLINK_NETDEV;
1261 priv->phylink_config.pcs_poll = true;
1262 if (priv->plat->mdio_bus_data)
1263 priv->phylink_config.ovr_an_inband =
1264 mdio_bus_data->xpcs_an_inband;
1267 fwnode = dev_fwnode(priv->device);
1269 phylink = phylink_create(&priv->phylink_config, fwnode,
1270 mode, &stmmac_phylink_mac_ops);
1271 if (IS_ERR(phylink))
1272 return PTR_ERR(phylink);
1275 phylink_set_pcs(phylink, &priv->hw->xpcs->pcs);
1277 priv->phylink = phylink;
1281 static void stmmac_display_rx_rings(struct stmmac_priv *priv)
1283 u32 rx_cnt = priv->plat->rx_queues_to_use;
1284 unsigned int desc_size;
1288 /* Display RX rings */
1289 for (queue = 0; queue < rx_cnt; queue++) {
1290 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1292 pr_info("\tRX Queue %u rings\n", queue);
1294 if (priv->extend_desc) {
1295 head_rx = (void *)rx_q->dma_erx;
1296 desc_size = sizeof(struct dma_extended_desc);
1298 head_rx = (void *)rx_q->dma_rx;
1299 desc_size = sizeof(struct dma_desc);
1302 /* Display RX ring */
1303 stmmac_display_ring(priv, head_rx, priv->dma_rx_size, true,
1304 rx_q->dma_rx_phy, desc_size);
1308 static void stmmac_display_tx_rings(struct stmmac_priv *priv)
1310 u32 tx_cnt = priv->plat->tx_queues_to_use;
1311 unsigned int desc_size;
1315 /* Display TX rings */
1316 for (queue = 0; queue < tx_cnt; queue++) {
1317 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1319 pr_info("\tTX Queue %d rings\n", queue);
1321 if (priv->extend_desc) {
1322 head_tx = (void *)tx_q->dma_etx;
1323 desc_size = sizeof(struct dma_extended_desc);
1324 } else if (tx_q->tbs & STMMAC_TBS_AVAIL) {
1325 head_tx = (void *)tx_q->dma_entx;
1326 desc_size = sizeof(struct dma_edesc);
1328 head_tx = (void *)tx_q->dma_tx;
1329 desc_size = sizeof(struct dma_desc);
1332 stmmac_display_ring(priv, head_tx, priv->dma_tx_size, false,
1333 tx_q->dma_tx_phy, desc_size);
1337 static void stmmac_display_rings(struct stmmac_priv *priv)
1339 /* Display RX ring */
1340 stmmac_display_rx_rings(priv);
1342 /* Display TX ring */
1343 stmmac_display_tx_rings(priv);
1346 static int stmmac_set_bfsize(int mtu, int bufsize)
1350 if (mtu >= BUF_SIZE_8KiB)
1351 ret = BUF_SIZE_16KiB;
1352 else if (mtu >= BUF_SIZE_4KiB)
1353 ret = BUF_SIZE_8KiB;
1354 else if (mtu >= BUF_SIZE_2KiB)
1355 ret = BUF_SIZE_4KiB;
1356 else if (mtu > DEFAULT_BUFSIZE)
1357 ret = BUF_SIZE_2KiB;
1359 ret = DEFAULT_BUFSIZE;
1365 * stmmac_clear_rx_descriptors - clear RX descriptors
1366 * @priv: driver private structure
1367 * @queue: RX queue index
1368 * Description: this function is called to clear the RX descriptors
1369 * in case of both basic and extended descriptors are used.
1371 static void stmmac_clear_rx_descriptors(struct stmmac_priv *priv, u32 queue)
1373 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1376 /* Clear the RX descriptors */
1377 for (i = 0; i < priv->dma_rx_size; i++)
1378 if (priv->extend_desc)
1379 stmmac_init_rx_desc(priv, &rx_q->dma_erx[i].basic,
1380 priv->use_riwt, priv->mode,
1381 (i == priv->dma_rx_size - 1),
1384 stmmac_init_rx_desc(priv, &rx_q->dma_rx[i],
1385 priv->use_riwt, priv->mode,
1386 (i == priv->dma_rx_size - 1),
1391 * stmmac_clear_tx_descriptors - clear tx descriptors
1392 * @priv: driver private structure
1393 * @queue: TX queue index.
1394 * Description: this function is called to clear the TX descriptors
1395 * in case of both basic and extended descriptors are used.
1397 static void stmmac_clear_tx_descriptors(struct stmmac_priv *priv, u32 queue)
1399 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1402 /* Clear the TX descriptors */
1403 for (i = 0; i < priv->dma_tx_size; i++) {
1404 int last = (i == (priv->dma_tx_size - 1));
1407 if (priv->extend_desc)
1408 p = &tx_q->dma_etx[i].basic;
1409 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1410 p = &tx_q->dma_entx[i].basic;
1412 p = &tx_q->dma_tx[i];
1414 stmmac_init_tx_desc(priv, p, priv->mode, last);
1419 * stmmac_clear_descriptors - clear descriptors
1420 * @priv: driver private structure
1421 * Description: this function is called to clear the TX and RX descriptors
1422 * in case of both basic and extended descriptors are used.
1424 static void stmmac_clear_descriptors(struct stmmac_priv *priv)
1426 u32 rx_queue_cnt = priv->plat->rx_queues_to_use;
1427 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1430 /* Clear the RX descriptors */
1431 for (queue = 0; queue < rx_queue_cnt; queue++)
1432 stmmac_clear_rx_descriptors(priv, queue);
1434 /* Clear the TX descriptors */
1435 for (queue = 0; queue < tx_queue_cnt; queue++)
1436 stmmac_clear_tx_descriptors(priv, queue);
1440 * stmmac_init_rx_buffers - init the RX descriptor buffer.
1441 * @priv: driver private structure
1442 * @p: descriptor pointer
1443 * @i: descriptor index
1445 * @queue: RX queue index
1446 * Description: this function is called to allocate a receive buffer, perform
1447 * the DMA mapping and init the descriptor.
1449 static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
1450 int i, gfp_t flags, u32 queue)
1452 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1453 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1454 gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
1456 if (priv->dma_cap.addr64 <= 32)
1460 buf->page = page_pool_alloc_pages(rx_q->page_pool, gfp);
1463 buf->page_offset = stmmac_rx_offset(priv);
1466 if (priv->sph && !buf->sec_page) {
1467 buf->sec_page = page_pool_alloc_pages(rx_q->page_pool, gfp);
1471 buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
1472 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true);
1474 buf->sec_page = NULL;
1475 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false);
1478 buf->addr = page_pool_get_dma_addr(buf->page) + buf->page_offset;
1480 stmmac_set_desc_addr(priv, p, buf->addr);
1481 if (priv->dma_buf_sz == BUF_SIZE_16KiB)
1482 stmmac_init_desc3(priv, p);
1488 * stmmac_free_rx_buffer - free RX dma buffers
1489 * @priv: private structure
1490 * @queue: RX queue index
1493 static void stmmac_free_rx_buffer(struct stmmac_priv *priv, u32 queue, int i)
1495 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1496 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1499 page_pool_put_full_page(rx_q->page_pool, buf->page, false);
1503 page_pool_put_full_page(rx_q->page_pool, buf->sec_page, false);
1504 buf->sec_page = NULL;
1508 * stmmac_free_tx_buffer - free RX dma buffers
1509 * @priv: private structure
1510 * @queue: RX queue index
1513 static void stmmac_free_tx_buffer(struct stmmac_priv *priv, u32 queue, int i)
1515 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1517 if (tx_q->tx_skbuff_dma[i].buf &&
1518 tx_q->tx_skbuff_dma[i].buf_type != STMMAC_TXBUF_T_XDP_TX) {
1519 if (tx_q->tx_skbuff_dma[i].map_as_page)
1520 dma_unmap_page(priv->device,
1521 tx_q->tx_skbuff_dma[i].buf,
1522 tx_q->tx_skbuff_dma[i].len,
1525 dma_unmap_single(priv->device,
1526 tx_q->tx_skbuff_dma[i].buf,
1527 tx_q->tx_skbuff_dma[i].len,
1531 if (tx_q->xdpf[i] &&
1532 (tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XDP_TX ||
1533 tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XDP_NDO)) {
1534 xdp_return_frame(tx_q->xdpf[i]);
1535 tx_q->xdpf[i] = NULL;
1538 if (tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XSK_TX)
1539 tx_q->xsk_frames_done++;
1541 if (tx_q->tx_skbuff[i] &&
1542 tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_SKB) {
1543 dev_kfree_skb_any(tx_q->tx_skbuff[i]);
1544 tx_q->tx_skbuff[i] = NULL;
1547 tx_q->tx_skbuff_dma[i].buf = 0;
1548 tx_q->tx_skbuff_dma[i].map_as_page = false;
1552 * dma_free_rx_skbufs - free RX dma buffers
1553 * @priv: private structure
1554 * @queue: RX queue index
1556 static void dma_free_rx_skbufs(struct stmmac_priv *priv, u32 queue)
1560 for (i = 0; i < priv->dma_rx_size; i++)
1561 stmmac_free_rx_buffer(priv, queue, i);
1564 static int stmmac_alloc_rx_buffers(struct stmmac_priv *priv, u32 queue,
1567 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1570 for (i = 0; i < priv->dma_rx_size; i++) {
1574 if (priv->extend_desc)
1575 p = &((rx_q->dma_erx + i)->basic);
1577 p = rx_q->dma_rx + i;
1579 ret = stmmac_init_rx_buffers(priv, p, i, flags,
1584 rx_q->buf_alloc_num++;
1591 * dma_free_rx_xskbufs - free RX dma buffers from XSK pool
1592 * @priv: private structure
1593 * @queue: RX queue index
1595 static void dma_free_rx_xskbufs(struct stmmac_priv *priv, u32 queue)
1597 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1600 for (i = 0; i < priv->dma_rx_size; i++) {
1601 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1606 xsk_buff_free(buf->xdp);
1611 static int stmmac_alloc_rx_buffers_zc(struct stmmac_priv *priv, u32 queue)
1613 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1616 for (i = 0; i < priv->dma_rx_size; i++) {
1617 struct stmmac_rx_buffer *buf;
1618 dma_addr_t dma_addr;
1621 if (priv->extend_desc)
1622 p = (struct dma_desc *)(rx_q->dma_erx + i);
1624 p = rx_q->dma_rx + i;
1626 buf = &rx_q->buf_pool[i];
1628 buf->xdp = xsk_buff_alloc(rx_q->xsk_pool);
1632 dma_addr = xsk_buff_xdp_get_dma(buf->xdp);
1633 stmmac_set_desc_addr(priv, p, dma_addr);
1634 rx_q->buf_alloc_num++;
1640 static struct xsk_buff_pool *stmmac_get_xsk_pool(struct stmmac_priv *priv, u32 queue)
1642 if (!stmmac_xdp_is_enabled(priv) || !test_bit(queue, priv->af_xdp_zc_qps))
1645 return xsk_get_pool_from_qid(priv->dev, queue);
1649 * __init_dma_rx_desc_rings - init the RX descriptor ring (per queue)
1650 * @priv: driver private structure
1651 * @queue: RX queue index
1653 * Description: this function initializes the DMA RX descriptors
1654 * and allocates the socket buffers. It supports the chained and ring
1657 static int __init_dma_rx_desc_rings(struct stmmac_priv *priv, u32 queue, gfp_t flags)
1659 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1662 netif_dbg(priv, probe, priv->dev,
1663 "(%s) dma_rx_phy=0x%08x\n", __func__,
1664 (u32)rx_q->dma_rx_phy);
1666 stmmac_clear_rx_descriptors(priv, queue);
1668 xdp_rxq_info_unreg_mem_model(&rx_q->xdp_rxq);
1670 rx_q->xsk_pool = stmmac_get_xsk_pool(priv, queue);
1672 if (rx_q->xsk_pool) {
1673 WARN_ON(xdp_rxq_info_reg_mem_model(&rx_q->xdp_rxq,
1674 MEM_TYPE_XSK_BUFF_POOL,
1676 netdev_info(priv->dev,
1677 "Register MEM_TYPE_XSK_BUFF_POOL RxQ-%d\n",
1679 xsk_pool_set_rxq_info(rx_q->xsk_pool, &rx_q->xdp_rxq);
1681 WARN_ON(xdp_rxq_info_reg_mem_model(&rx_q->xdp_rxq,
1684 netdev_info(priv->dev,
1685 "Register MEM_TYPE_PAGE_POOL RxQ-%d\n",
1689 if (rx_q->xsk_pool) {
1690 /* RX XDP ZC buffer pool may not be populated, e.g.
1693 stmmac_alloc_rx_buffers_zc(priv, queue);
1695 ret = stmmac_alloc_rx_buffers(priv, queue, flags);
1703 /* Setup the chained descriptor addresses */
1704 if (priv->mode == STMMAC_CHAIN_MODE) {
1705 if (priv->extend_desc)
1706 stmmac_mode_init(priv, rx_q->dma_erx,
1708 priv->dma_rx_size, 1);
1710 stmmac_mode_init(priv, rx_q->dma_rx,
1712 priv->dma_rx_size, 0);
1718 static int init_dma_rx_desc_rings(struct net_device *dev, gfp_t flags)
1720 struct stmmac_priv *priv = netdev_priv(dev);
1721 u32 rx_count = priv->plat->rx_queues_to_use;
1725 /* RX INITIALIZATION */
1726 netif_dbg(priv, probe, priv->dev,
1727 "SKB addresses:\nskb\t\tskb data\tdma data\n");
1729 for (queue = 0; queue < rx_count; queue++) {
1730 ret = __init_dma_rx_desc_rings(priv, queue, flags);
1732 goto err_init_rx_buffers;
1737 err_init_rx_buffers:
1738 while (queue >= 0) {
1739 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1742 dma_free_rx_xskbufs(priv, queue);
1744 dma_free_rx_skbufs(priv, queue);
1746 rx_q->buf_alloc_num = 0;
1747 rx_q->xsk_pool = NULL;
1759 * __init_dma_tx_desc_rings - init the TX descriptor ring (per queue)
1760 * @priv: driver private structure
1761 * @queue : TX queue index
1762 * Description: this function initializes the DMA TX descriptors
1763 * and allocates the socket buffers. It supports the chained and ring
1766 static int __init_dma_tx_desc_rings(struct stmmac_priv *priv, u32 queue)
1768 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1771 netif_dbg(priv, probe, priv->dev,
1772 "(%s) dma_tx_phy=0x%08x\n", __func__,
1773 (u32)tx_q->dma_tx_phy);
1775 /* Setup the chained descriptor addresses */
1776 if (priv->mode == STMMAC_CHAIN_MODE) {
1777 if (priv->extend_desc)
1778 stmmac_mode_init(priv, tx_q->dma_etx,
1780 priv->dma_tx_size, 1);
1781 else if (!(tx_q->tbs & STMMAC_TBS_AVAIL))
1782 stmmac_mode_init(priv, tx_q->dma_tx,
1784 priv->dma_tx_size, 0);
1787 tx_q->xsk_pool = stmmac_get_xsk_pool(priv, queue);
1789 for (i = 0; i < priv->dma_tx_size; i++) {
1792 if (priv->extend_desc)
1793 p = &((tx_q->dma_etx + i)->basic);
1794 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1795 p = &((tx_q->dma_entx + i)->basic);
1797 p = tx_q->dma_tx + i;
1799 stmmac_clear_desc(priv, p);
1801 tx_q->tx_skbuff_dma[i].buf = 0;
1802 tx_q->tx_skbuff_dma[i].map_as_page = false;
1803 tx_q->tx_skbuff_dma[i].len = 0;
1804 tx_q->tx_skbuff_dma[i].last_segment = false;
1805 tx_q->tx_skbuff[i] = NULL;
1812 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
1817 static int init_dma_tx_desc_rings(struct net_device *dev)
1819 struct stmmac_priv *priv = netdev_priv(dev);
1823 tx_queue_cnt = priv->plat->tx_queues_to_use;
1825 for (queue = 0; queue < tx_queue_cnt; queue++)
1826 __init_dma_tx_desc_rings(priv, queue);
1832 * init_dma_desc_rings - init the RX/TX descriptor rings
1833 * @dev: net device structure
1835 * Description: this function initializes the DMA RX/TX descriptors
1836 * and allocates the socket buffers. It supports the chained and ring
1839 static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
1841 struct stmmac_priv *priv = netdev_priv(dev);
1844 ret = init_dma_rx_desc_rings(dev, flags);
1848 ret = init_dma_tx_desc_rings(dev);
1850 stmmac_clear_descriptors(priv);
1852 if (netif_msg_hw(priv))
1853 stmmac_display_rings(priv);
1859 * dma_free_tx_skbufs - free TX dma buffers
1860 * @priv: private structure
1861 * @queue: TX queue index
1863 static void dma_free_tx_skbufs(struct stmmac_priv *priv, u32 queue)
1865 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1868 tx_q->xsk_frames_done = 0;
1870 for (i = 0; i < priv->dma_tx_size; i++)
1871 stmmac_free_tx_buffer(priv, queue, i);
1873 if (tx_q->xsk_pool && tx_q->xsk_frames_done) {
1874 xsk_tx_completed(tx_q->xsk_pool, tx_q->xsk_frames_done);
1875 tx_q->xsk_frames_done = 0;
1876 tx_q->xsk_pool = NULL;
1881 * stmmac_free_tx_skbufs - free TX skb buffers
1882 * @priv: private structure
1884 static void stmmac_free_tx_skbufs(struct stmmac_priv *priv)
1886 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1889 for (queue = 0; queue < tx_queue_cnt; queue++)
1890 dma_free_tx_skbufs(priv, queue);
1894 * __free_dma_rx_desc_resources - free RX dma desc resources (per queue)
1895 * @priv: private structure
1896 * @queue: RX queue index
1898 static void __free_dma_rx_desc_resources(struct stmmac_priv *priv, u32 queue)
1900 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1902 /* Release the DMA RX socket buffers */
1904 dma_free_rx_xskbufs(priv, queue);
1906 dma_free_rx_skbufs(priv, queue);
1908 rx_q->buf_alloc_num = 0;
1909 rx_q->xsk_pool = NULL;
1911 /* Free DMA regions of consistent memory previously allocated */
1912 if (!priv->extend_desc)
1913 dma_free_coherent(priv->device, priv->dma_rx_size *
1914 sizeof(struct dma_desc),
1915 rx_q->dma_rx, rx_q->dma_rx_phy);
1917 dma_free_coherent(priv->device, priv->dma_rx_size *
1918 sizeof(struct dma_extended_desc),
1919 rx_q->dma_erx, rx_q->dma_rx_phy);
1921 if (xdp_rxq_info_is_reg(&rx_q->xdp_rxq))
1922 xdp_rxq_info_unreg(&rx_q->xdp_rxq);
1924 kfree(rx_q->buf_pool);
1925 if (rx_q->page_pool)
1926 page_pool_destroy(rx_q->page_pool);
1929 static void free_dma_rx_desc_resources(struct stmmac_priv *priv)
1931 u32 rx_count = priv->plat->rx_queues_to_use;
1934 /* Free RX queue resources */
1935 for (queue = 0; queue < rx_count; queue++)
1936 __free_dma_rx_desc_resources(priv, queue);
1940 * __free_dma_tx_desc_resources - free TX dma desc resources (per queue)
1941 * @priv: private structure
1942 * @queue: TX queue index
1944 static void __free_dma_tx_desc_resources(struct stmmac_priv *priv, u32 queue)
1946 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1950 /* Release the DMA TX socket buffers */
1951 dma_free_tx_skbufs(priv, queue);
1953 if (priv->extend_desc) {
1954 size = sizeof(struct dma_extended_desc);
1955 addr = tx_q->dma_etx;
1956 } else if (tx_q->tbs & STMMAC_TBS_AVAIL) {
1957 size = sizeof(struct dma_edesc);
1958 addr = tx_q->dma_entx;
1960 size = sizeof(struct dma_desc);
1961 addr = tx_q->dma_tx;
1964 size *= priv->dma_tx_size;
1966 dma_free_coherent(priv->device, size, addr, tx_q->dma_tx_phy);
1968 kfree(tx_q->tx_skbuff_dma);
1969 kfree(tx_q->tx_skbuff);
1972 static void free_dma_tx_desc_resources(struct stmmac_priv *priv)
1974 u32 tx_count = priv->plat->tx_queues_to_use;
1977 /* Free TX queue resources */
1978 for (queue = 0; queue < tx_count; queue++)
1979 __free_dma_tx_desc_resources(priv, queue);
1983 * __alloc_dma_rx_desc_resources - alloc RX resources (per queue).
1984 * @priv: private structure
1985 * @queue: RX queue index
1986 * Description: according to which descriptor can be used (extend or basic)
1987 * this function allocates the resources for TX and RX paths. In case of
1988 * reception, for example, it pre-allocated the RX socket buffer in order to
1989 * allow zero-copy mechanism.
1991 static int __alloc_dma_rx_desc_resources(struct stmmac_priv *priv, u32 queue)
1993 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1994 struct stmmac_channel *ch = &priv->channel[queue];
1995 bool xdp_prog = stmmac_xdp_is_enabled(priv);
1996 struct page_pool_params pp_params = { 0 };
1997 unsigned int num_pages;
1998 unsigned int napi_id;
2001 rx_q->queue_index = queue;
2002 rx_q->priv_data = priv;
2004 pp_params.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
2005 pp_params.pool_size = priv->dma_rx_size;
2006 num_pages = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE);
2007 pp_params.order = ilog2(num_pages);
2008 pp_params.nid = dev_to_node(priv->device);
2009 pp_params.dev = priv->device;
2010 pp_params.dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
2011 pp_params.offset = stmmac_rx_offset(priv);
2012 pp_params.max_len = STMMAC_MAX_RX_BUF_SIZE(num_pages);
2014 rx_q->page_pool = page_pool_create(&pp_params);
2015 if (IS_ERR(rx_q->page_pool)) {
2016 ret = PTR_ERR(rx_q->page_pool);
2017 rx_q->page_pool = NULL;
2021 rx_q->buf_pool = kcalloc(priv->dma_rx_size,
2022 sizeof(*rx_q->buf_pool),
2024 if (!rx_q->buf_pool)
2027 if (priv->extend_desc) {
2028 rx_q->dma_erx = dma_alloc_coherent(priv->device,
2030 sizeof(struct dma_extended_desc),
2037 rx_q->dma_rx = dma_alloc_coherent(priv->device,
2039 sizeof(struct dma_desc),
2046 if (stmmac_xdp_is_enabled(priv) &&
2047 test_bit(queue, priv->af_xdp_zc_qps))
2048 napi_id = ch->rxtx_napi.napi_id;
2050 napi_id = ch->rx_napi.napi_id;
2052 ret = xdp_rxq_info_reg(&rx_q->xdp_rxq, priv->dev,
2056 netdev_err(priv->dev, "Failed to register xdp rxq info\n");
2063 static int alloc_dma_rx_desc_resources(struct stmmac_priv *priv)
2065 u32 rx_count = priv->plat->rx_queues_to_use;
2069 /* RX queues buffers and DMA */
2070 for (queue = 0; queue < rx_count; queue++) {
2071 ret = __alloc_dma_rx_desc_resources(priv, queue);
2079 free_dma_rx_desc_resources(priv);
2085 * __alloc_dma_tx_desc_resources - alloc TX resources (per queue).
2086 * @priv: private structure
2087 * @queue: TX queue index
2088 * Description: according to which descriptor can be used (extend or basic)
2089 * this function allocates the resources for TX and RX paths. In case of
2090 * reception, for example, it pre-allocated the RX socket buffer in order to
2091 * allow zero-copy mechanism.
2093 static int __alloc_dma_tx_desc_resources(struct stmmac_priv *priv, u32 queue)
2095 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2099 tx_q->queue_index = queue;
2100 tx_q->priv_data = priv;
2102 tx_q->tx_skbuff_dma = kcalloc(priv->dma_tx_size,
2103 sizeof(*tx_q->tx_skbuff_dma),
2105 if (!tx_q->tx_skbuff_dma)
2108 tx_q->tx_skbuff = kcalloc(priv->dma_tx_size,
2109 sizeof(struct sk_buff *),
2111 if (!tx_q->tx_skbuff)
2114 if (priv->extend_desc)
2115 size = sizeof(struct dma_extended_desc);
2116 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2117 size = sizeof(struct dma_edesc);
2119 size = sizeof(struct dma_desc);
2121 size *= priv->dma_tx_size;
2123 addr = dma_alloc_coherent(priv->device, size,
2124 &tx_q->dma_tx_phy, GFP_KERNEL);
2128 if (priv->extend_desc)
2129 tx_q->dma_etx = addr;
2130 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2131 tx_q->dma_entx = addr;
2133 tx_q->dma_tx = addr;
2138 static int alloc_dma_tx_desc_resources(struct stmmac_priv *priv)
2140 u32 tx_count = priv->plat->tx_queues_to_use;
2144 /* TX queues buffers and DMA */
2145 for (queue = 0; queue < tx_count; queue++) {
2146 ret = __alloc_dma_tx_desc_resources(priv, queue);
2154 free_dma_tx_desc_resources(priv);
2159 * alloc_dma_desc_resources - alloc TX/RX resources.
2160 * @priv: private structure
2161 * Description: according to which descriptor can be used (extend or basic)
2162 * this function allocates the resources for TX and RX paths. In case of
2163 * reception, for example, it pre-allocated the RX socket buffer in order to
2164 * allow zero-copy mechanism.
2166 static int alloc_dma_desc_resources(struct stmmac_priv *priv)
2169 int ret = alloc_dma_rx_desc_resources(priv);
2174 ret = alloc_dma_tx_desc_resources(priv);
2180 * free_dma_desc_resources - free dma desc resources
2181 * @priv: private structure
2183 static void free_dma_desc_resources(struct stmmac_priv *priv)
2185 /* Release the DMA TX socket buffers */
2186 free_dma_tx_desc_resources(priv);
2188 /* Release the DMA RX socket buffers later
2189 * to ensure all pending XDP_TX buffers are returned.
2191 free_dma_rx_desc_resources(priv);
2195 * stmmac_mac_enable_rx_queues - Enable MAC rx queues
2196 * @priv: driver private structure
2197 * Description: It is used for enabling the rx queues in the MAC
2199 static void stmmac_mac_enable_rx_queues(struct stmmac_priv *priv)
2201 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2205 for (queue = 0; queue < rx_queues_count; queue++) {
2206 mode = priv->plat->rx_queues_cfg[queue].mode_to_use;
2207 stmmac_rx_queue_enable(priv, priv->hw, mode, queue);
2212 * stmmac_start_rx_dma - start RX DMA channel
2213 * @priv: driver private structure
2214 * @chan: RX channel index
2216 * This starts a RX DMA channel
2218 static void stmmac_start_rx_dma(struct stmmac_priv *priv, u32 chan)
2220 netdev_dbg(priv->dev, "DMA RX processes started in channel %d\n", chan);
2221 stmmac_start_rx(priv, priv->ioaddr, chan);
2225 * stmmac_start_tx_dma - start TX DMA channel
2226 * @priv: driver private structure
2227 * @chan: TX channel index
2229 * This starts a TX DMA channel
2231 static void stmmac_start_tx_dma(struct stmmac_priv *priv, u32 chan)
2233 netdev_dbg(priv->dev, "DMA TX processes started in channel %d\n", chan);
2234 stmmac_start_tx(priv, priv->ioaddr, chan);
2238 * stmmac_stop_rx_dma - stop RX DMA channel
2239 * @priv: driver private structure
2240 * @chan: RX channel index
2242 * This stops a RX DMA channel
2244 static void stmmac_stop_rx_dma(struct stmmac_priv *priv, u32 chan)
2246 netdev_dbg(priv->dev, "DMA RX processes stopped in channel %d\n", chan);
2247 stmmac_stop_rx(priv, priv->ioaddr, chan);
2251 * stmmac_stop_tx_dma - stop TX DMA channel
2252 * @priv: driver private structure
2253 * @chan: TX channel index
2255 * This stops a TX DMA channel
2257 static void stmmac_stop_tx_dma(struct stmmac_priv *priv, u32 chan)
2259 netdev_dbg(priv->dev, "DMA TX processes stopped in channel %d\n", chan);
2260 stmmac_stop_tx(priv, priv->ioaddr, chan);
2264 * stmmac_start_all_dma - start all RX and TX DMA channels
2265 * @priv: driver private structure
2267 * This starts all the RX and TX DMA channels
2269 static void stmmac_start_all_dma(struct stmmac_priv *priv)
2271 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2272 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2275 for (chan = 0; chan < rx_channels_count; chan++)
2276 stmmac_start_rx_dma(priv, chan);
2278 for (chan = 0; chan < tx_channels_count; chan++)
2279 stmmac_start_tx_dma(priv, chan);
2283 * stmmac_stop_all_dma - stop all RX and TX DMA channels
2284 * @priv: driver private structure
2286 * This stops the RX and TX DMA channels
2288 static void stmmac_stop_all_dma(struct stmmac_priv *priv)
2290 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2291 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2294 for (chan = 0; chan < rx_channels_count; chan++)
2295 stmmac_stop_rx_dma(priv, chan);
2297 for (chan = 0; chan < tx_channels_count; chan++)
2298 stmmac_stop_tx_dma(priv, chan);
2302 * stmmac_dma_operation_mode - HW DMA operation mode
2303 * @priv: driver private structure
2304 * Description: it is used for configuring the DMA operation mode register in
2305 * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
2307 static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
2309 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2310 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2311 int rxfifosz = priv->plat->rx_fifo_size;
2312 int txfifosz = priv->plat->tx_fifo_size;
2319 rxfifosz = priv->dma_cap.rx_fifo_size;
2321 txfifosz = priv->dma_cap.tx_fifo_size;
2323 /* Adjust for real per queue fifo size */
2324 rxfifosz /= rx_channels_count;
2325 txfifosz /= tx_channels_count;
2327 if (priv->plat->force_thresh_dma_mode) {
2330 } else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
2332 * In case of GMAC, SF mode can be enabled
2333 * to perform the TX COE in HW. This depends on:
2334 * 1) TX COE if actually supported
2335 * 2) There is no bugged Jumbo frame support
2336 * that needs to not insert csum in the TDES.
2338 txmode = SF_DMA_MODE;
2339 rxmode = SF_DMA_MODE;
2340 priv->xstats.threshold = SF_DMA_MODE;
2343 rxmode = SF_DMA_MODE;
2346 /* configure all channels */
2347 for (chan = 0; chan < rx_channels_count; chan++) {
2348 struct stmmac_rx_queue *rx_q = &priv->rx_queue[chan];
2351 qmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
2353 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan,
2356 if (rx_q->xsk_pool) {
2357 buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool);
2358 stmmac_set_dma_bfsize(priv, priv->ioaddr,
2362 stmmac_set_dma_bfsize(priv, priv->ioaddr,
2368 for (chan = 0; chan < tx_channels_count; chan++) {
2369 qmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
2371 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan,
2376 static bool stmmac_xdp_xmit_zc(struct stmmac_priv *priv, u32 queue, u32 budget)
2378 struct netdev_queue *nq = netdev_get_tx_queue(priv->dev, queue);
2379 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2380 struct xsk_buff_pool *pool = tx_q->xsk_pool;
2381 unsigned int entry = tx_q->cur_tx;
2382 struct dma_desc *tx_desc = NULL;
2383 struct xdp_desc xdp_desc;
2384 bool work_done = true;
2386 /* Avoids TX time-out as we are sharing with slow path */
2387 txq_trans_cond_update(nq);
2389 budget = min(budget, stmmac_tx_avail(priv, queue));
2391 while (budget-- > 0) {
2392 dma_addr_t dma_addr;
2395 /* We are sharing with slow path and stop XSK TX desc submission when
2396 * available TX ring is less than threshold.
2398 if (unlikely(stmmac_tx_avail(priv, queue) < STMMAC_TX_XSK_AVAIL) ||
2399 !netif_carrier_ok(priv->dev)) {
2404 if (!xsk_tx_peek_desc(pool, &xdp_desc))
2407 if (likely(priv->extend_desc))
2408 tx_desc = (struct dma_desc *)(tx_q->dma_etx + entry);
2409 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2410 tx_desc = &tx_q->dma_entx[entry].basic;
2412 tx_desc = tx_q->dma_tx + entry;
2414 dma_addr = xsk_buff_raw_get_dma(pool, xdp_desc.addr);
2415 xsk_buff_raw_dma_sync_for_device(pool, dma_addr, xdp_desc.len);
2417 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XSK_TX;
2419 /* To return XDP buffer to XSK pool, we simple call
2420 * xsk_tx_completed(), so we don't need to fill up
2423 tx_q->tx_skbuff_dma[entry].buf = 0;
2424 tx_q->xdpf[entry] = NULL;
2426 tx_q->tx_skbuff_dma[entry].map_as_page = false;
2427 tx_q->tx_skbuff_dma[entry].len = xdp_desc.len;
2428 tx_q->tx_skbuff_dma[entry].last_segment = true;
2429 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
2431 stmmac_set_desc_addr(priv, tx_desc, dma_addr);
2433 tx_q->tx_count_frames++;
2435 if (!priv->tx_coal_frames[queue])
2437 else if (tx_q->tx_count_frames % priv->tx_coal_frames[queue] == 0)
2443 tx_q->tx_count_frames = 0;
2444 stmmac_set_tx_ic(priv, tx_desc);
2445 priv->xstats.tx_set_ic_bit++;
2448 stmmac_prepare_tx_desc(priv, tx_desc, 1, xdp_desc.len,
2449 true, priv->mode, true, true,
2452 stmmac_enable_dma_transmission(priv, priv->ioaddr);
2454 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
2455 entry = tx_q->cur_tx;
2459 stmmac_flush_tx_descriptors(priv, queue);
2460 xsk_tx_release(pool);
2463 /* Return true if all of the 3 conditions are met
2464 * a) TX Budget is still available
2465 * b) work_done = true when XSK TX desc peek is empty (no more
2466 * pending XSK TX for transmission)
2468 return !!budget && work_done;
2471 static void stmmac_bump_dma_threshold(struct stmmac_priv *priv, u32 chan)
2473 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) && tc <= 256) {
2476 if (priv->plat->force_thresh_dma_mode)
2477 stmmac_set_dma_operation_mode(priv, tc, tc, chan);
2479 stmmac_set_dma_operation_mode(priv, tc, SF_DMA_MODE,
2482 priv->xstats.threshold = tc;
2487 * stmmac_tx_clean - to manage the transmission completion
2488 * @priv: driver private structure
2489 * @budget: napi budget limiting this functions packet handling
2490 * @queue: TX queue index
2491 * Description: it reclaims the transmit resources after transmission completes.
2493 static int stmmac_tx_clean(struct stmmac_priv *priv, int budget, u32 queue)
2495 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2496 unsigned int bytes_compl = 0, pkts_compl = 0;
2497 unsigned int entry, xmits = 0, count = 0;
2499 __netif_tx_lock_bh(netdev_get_tx_queue(priv->dev, queue));
2501 priv->xstats.tx_clean++;
2503 tx_q->xsk_frames_done = 0;
2505 entry = tx_q->dirty_tx;
2507 /* Try to clean all TX complete frame in 1 shot */
2508 while ((entry != tx_q->cur_tx) && count < priv->dma_tx_size) {
2509 struct xdp_frame *xdpf;
2510 struct sk_buff *skb;
2514 if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_TX ||
2515 tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_NDO) {
2516 xdpf = tx_q->xdpf[entry];
2518 } else if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_SKB) {
2520 skb = tx_q->tx_skbuff[entry];
2526 if (priv->extend_desc)
2527 p = (struct dma_desc *)(tx_q->dma_etx + entry);
2528 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2529 p = &tx_q->dma_entx[entry].basic;
2531 p = tx_q->dma_tx + entry;
2533 status = stmmac_tx_status(priv, &priv->dev->stats,
2534 &priv->xstats, p, priv->ioaddr);
2535 /* Check if the descriptor is owned by the DMA */
2536 if (unlikely(status & tx_dma_own))
2541 /* Make sure descriptor fields are read after reading
2546 /* Just consider the last segment and ...*/
2547 if (likely(!(status & tx_not_ls))) {
2548 /* ... verify the status error condition */
2549 if (unlikely(status & tx_err)) {
2550 priv->dev->stats.tx_errors++;
2551 if (unlikely(status & tx_err_bump_tc))
2552 stmmac_bump_dma_threshold(priv, queue);
2554 priv->dev->stats.tx_packets++;
2555 priv->xstats.tx_pkt_n++;
2556 priv->xstats.txq_stats[queue].tx_pkt_n++;
2559 stmmac_get_tx_hwtstamp(priv, p, skb);
2562 if (likely(tx_q->tx_skbuff_dma[entry].buf &&
2563 tx_q->tx_skbuff_dma[entry].buf_type != STMMAC_TXBUF_T_XDP_TX)) {
2564 if (tx_q->tx_skbuff_dma[entry].map_as_page)
2565 dma_unmap_page(priv->device,
2566 tx_q->tx_skbuff_dma[entry].buf,
2567 tx_q->tx_skbuff_dma[entry].len,
2570 dma_unmap_single(priv->device,
2571 tx_q->tx_skbuff_dma[entry].buf,
2572 tx_q->tx_skbuff_dma[entry].len,
2574 tx_q->tx_skbuff_dma[entry].buf = 0;
2575 tx_q->tx_skbuff_dma[entry].len = 0;
2576 tx_q->tx_skbuff_dma[entry].map_as_page = false;
2579 stmmac_clean_desc3(priv, tx_q, p);
2581 tx_q->tx_skbuff_dma[entry].last_segment = false;
2582 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
2585 tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_TX) {
2586 xdp_return_frame_rx_napi(xdpf);
2587 tx_q->xdpf[entry] = NULL;
2591 tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_NDO) {
2592 xdp_return_frame(xdpf);
2593 tx_q->xdpf[entry] = NULL;
2596 if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XSK_TX)
2597 tx_q->xsk_frames_done++;
2599 if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_SKB) {
2602 bytes_compl += skb->len;
2603 dev_consume_skb_any(skb);
2604 tx_q->tx_skbuff[entry] = NULL;
2608 stmmac_release_tx_desc(priv, p, priv->mode);
2610 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
2612 tx_q->dirty_tx = entry;
2614 netdev_tx_completed_queue(netdev_get_tx_queue(priv->dev, queue),
2615 pkts_compl, bytes_compl);
2617 if (unlikely(netif_tx_queue_stopped(netdev_get_tx_queue(priv->dev,
2619 stmmac_tx_avail(priv, queue) > STMMAC_TX_THRESH(priv)) {
2621 netif_dbg(priv, tx_done, priv->dev,
2622 "%s: restart transmit\n", __func__);
2623 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, queue));
2626 if (tx_q->xsk_pool) {
2629 if (tx_q->xsk_frames_done)
2630 xsk_tx_completed(tx_q->xsk_pool, tx_q->xsk_frames_done);
2632 if (xsk_uses_need_wakeup(tx_q->xsk_pool))
2633 xsk_set_tx_need_wakeup(tx_q->xsk_pool);
2635 /* For XSK TX, we try to send as many as possible.
2636 * If XSK work done (XSK TX desc empty and budget still
2637 * available), return "budget - 1" to reenable TX IRQ.
2638 * Else, return "budget" to make NAPI continue polling.
2640 work_done = stmmac_xdp_xmit_zc(priv, queue,
2641 STMMAC_XSK_TX_BUDGET_MAX);
2648 if (priv->eee_enabled && !priv->tx_path_in_lpi_mode &&
2649 priv->eee_sw_timer_en) {
2650 stmmac_enable_eee_mode(priv);
2651 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer));
2654 /* We still have pending packets, let's call for a new scheduling */
2655 if (tx_q->dirty_tx != tx_q->cur_tx)
2656 hrtimer_start(&tx_q->txtimer,
2657 STMMAC_COAL_TIMER(priv->tx_coal_timer[queue]),
2660 __netif_tx_unlock_bh(netdev_get_tx_queue(priv->dev, queue));
2662 /* Combine decisions from TX clean and XSK TX */
2663 return max(count, xmits);
2667 * stmmac_tx_err - to manage the tx error
2668 * @priv: driver private structure
2669 * @chan: channel index
2670 * Description: it cleans the descriptors and restarts the transmission
2671 * in case of transmission errors.
2673 static void stmmac_tx_err(struct stmmac_priv *priv, u32 chan)
2675 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2677 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, chan));
2679 stmmac_stop_tx_dma(priv, chan);
2680 dma_free_tx_skbufs(priv, chan);
2681 stmmac_clear_tx_descriptors(priv, chan);
2685 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, chan));
2686 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2687 tx_q->dma_tx_phy, chan);
2688 stmmac_start_tx_dma(priv, chan);
2690 priv->dev->stats.tx_errors++;
2691 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, chan));
2695 * stmmac_set_dma_operation_mode - Set DMA operation mode by channel
2696 * @priv: driver private structure
2697 * @txmode: TX operating mode
2698 * @rxmode: RX operating mode
2699 * @chan: channel index
2700 * Description: it is used for configuring of the DMA operation mode in
2701 * runtime in order to program the tx/rx DMA thresholds or Store-And-Forward
2704 static void stmmac_set_dma_operation_mode(struct stmmac_priv *priv, u32 txmode,
2705 u32 rxmode, u32 chan)
2707 u8 rxqmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
2708 u8 txqmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
2709 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2710 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2711 int rxfifosz = priv->plat->rx_fifo_size;
2712 int txfifosz = priv->plat->tx_fifo_size;
2715 rxfifosz = priv->dma_cap.rx_fifo_size;
2717 txfifosz = priv->dma_cap.tx_fifo_size;
2719 /* Adjust for real per queue fifo size */
2720 rxfifosz /= rx_channels_count;
2721 txfifosz /= tx_channels_count;
2723 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan, rxfifosz, rxqmode);
2724 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan, txfifosz, txqmode);
2727 static bool stmmac_safety_feat_interrupt(struct stmmac_priv *priv)
2731 ret = stmmac_safety_feat_irq_status(priv, priv->dev,
2732 priv->ioaddr, priv->dma_cap.asp, &priv->sstats);
2733 if (ret && (ret != -EINVAL)) {
2734 stmmac_global_err(priv);
2741 static int stmmac_napi_check(struct stmmac_priv *priv, u32 chan, u32 dir)
2743 int status = stmmac_dma_interrupt_status(priv, priv->ioaddr,
2744 &priv->xstats, chan, dir);
2745 struct stmmac_rx_queue *rx_q = &priv->rx_queue[chan];
2746 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2747 struct stmmac_channel *ch = &priv->channel[chan];
2748 struct napi_struct *rx_napi;
2749 struct napi_struct *tx_napi;
2750 unsigned long flags;
2752 rx_napi = rx_q->xsk_pool ? &ch->rxtx_napi : &ch->rx_napi;
2753 tx_napi = tx_q->xsk_pool ? &ch->rxtx_napi : &ch->tx_napi;
2755 if ((status & handle_rx) && (chan < priv->plat->rx_queues_to_use)) {
2756 if (napi_schedule_prep(rx_napi)) {
2757 spin_lock_irqsave(&ch->lock, flags);
2758 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
2759 spin_unlock_irqrestore(&ch->lock, flags);
2760 __napi_schedule(rx_napi);
2764 if ((status & handle_tx) && (chan < priv->plat->tx_queues_to_use)) {
2765 if (napi_schedule_prep(tx_napi)) {
2766 spin_lock_irqsave(&ch->lock, flags);
2767 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
2768 spin_unlock_irqrestore(&ch->lock, flags);
2769 __napi_schedule(tx_napi);
2777 * stmmac_dma_interrupt - DMA ISR
2778 * @priv: driver private structure
2779 * Description: this is the DMA ISR. It is called by the main ISR.
2780 * It calls the dwmac dma routine and schedule poll method in case of some
2783 static void stmmac_dma_interrupt(struct stmmac_priv *priv)
2785 u32 tx_channel_count = priv->plat->tx_queues_to_use;
2786 u32 rx_channel_count = priv->plat->rx_queues_to_use;
2787 u32 channels_to_check = tx_channel_count > rx_channel_count ?
2788 tx_channel_count : rx_channel_count;
2790 int status[max_t(u32, MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES)];
2792 /* Make sure we never check beyond our status buffer. */
2793 if (WARN_ON_ONCE(channels_to_check > ARRAY_SIZE(status)))
2794 channels_to_check = ARRAY_SIZE(status);
2796 for (chan = 0; chan < channels_to_check; chan++)
2797 status[chan] = stmmac_napi_check(priv, chan,
2800 for (chan = 0; chan < tx_channel_count; chan++) {
2801 if (unlikely(status[chan] & tx_hard_error_bump_tc)) {
2802 /* Try to bump up the dma threshold on this failure */
2803 stmmac_bump_dma_threshold(priv, chan);
2804 } else if (unlikely(status[chan] == tx_hard_error)) {
2805 stmmac_tx_err(priv, chan);
2811 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
2812 * @priv: driver private structure
2813 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
2815 static void stmmac_mmc_setup(struct stmmac_priv *priv)
2817 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
2818 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
2820 stmmac_mmc_intr_all_mask(priv, priv->mmcaddr);
2822 if (priv->dma_cap.rmon) {
2823 stmmac_mmc_ctrl(priv, priv->mmcaddr, mode);
2824 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
2826 netdev_info(priv->dev, "No MAC Management Counters available\n");
2830 * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
2831 * @priv: driver private structure
2833 * new GMAC chip generations have a new register to indicate the
2834 * presence of the optional feature/functions.
2835 * This can be also used to override the value passed through the
2836 * platform and necessary for old MAC10/100 and GMAC chips.
2838 static int stmmac_get_hw_features(struct stmmac_priv *priv)
2840 return stmmac_get_hw_feature(priv, priv->ioaddr, &priv->dma_cap) == 0;
2844 * stmmac_check_ether_addr - check if the MAC addr is valid
2845 * @priv: driver private structure
2847 * it is to verify if the MAC address is valid, in case of failures it
2848 * generates a random MAC address
2850 static void stmmac_check_ether_addr(struct stmmac_priv *priv)
2854 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
2855 stmmac_get_umac_addr(priv, priv->hw, addr, 0);
2856 if (is_valid_ether_addr(addr))
2857 eth_hw_addr_set(priv->dev, addr);
2859 eth_hw_addr_random(priv->dev);
2860 dev_info(priv->device, "device MAC address %pM\n",
2861 priv->dev->dev_addr);
2866 * stmmac_init_dma_engine - DMA init.
2867 * @priv: driver private structure
2869 * It inits the DMA invoking the specific MAC/GMAC callback.
2870 * Some DMA parameters can be passed from the platform;
2871 * in case of these are not passed a default is kept for the MAC or GMAC.
2873 static int stmmac_init_dma_engine(struct stmmac_priv *priv)
2875 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2876 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2877 u32 dma_csr_ch = max(rx_channels_count, tx_channels_count);
2878 struct stmmac_rx_queue *rx_q;
2879 struct stmmac_tx_queue *tx_q;
2884 if (!priv->plat->dma_cfg || !priv->plat->dma_cfg->pbl) {
2885 dev_err(priv->device, "Invalid DMA configuration\n");
2889 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
2892 ret = stmmac_reset(priv, priv->ioaddr);
2894 dev_err(priv->device, "Failed to reset the dma\n");
2898 /* DMA Configuration */
2899 stmmac_dma_init(priv, priv->ioaddr, priv->plat->dma_cfg, atds);
2901 if (priv->plat->axi)
2902 stmmac_axi(priv, priv->ioaddr, priv->plat->axi);
2904 /* DMA CSR Channel configuration */
2905 for (chan = 0; chan < dma_csr_ch; chan++)
2906 stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan);
2908 /* DMA RX Channel Configuration */
2909 for (chan = 0; chan < rx_channels_count; chan++) {
2910 rx_q = &priv->rx_queue[chan];
2912 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2913 rx_q->dma_rx_phy, chan);
2915 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
2916 (rx_q->buf_alloc_num *
2917 sizeof(struct dma_desc));
2918 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
2919 rx_q->rx_tail_addr, chan);
2922 /* DMA TX Channel Configuration */
2923 for (chan = 0; chan < tx_channels_count; chan++) {
2924 tx_q = &priv->tx_queue[chan];
2926 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2927 tx_q->dma_tx_phy, chan);
2929 tx_q->tx_tail_addr = tx_q->dma_tx_phy;
2930 stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
2931 tx_q->tx_tail_addr, chan);
2937 static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue)
2939 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2941 hrtimer_start(&tx_q->txtimer,
2942 STMMAC_COAL_TIMER(priv->tx_coal_timer[queue]),
2947 * stmmac_tx_timer - mitigation sw timer for tx.
2950 * This is the timer handler to directly invoke the stmmac_tx_clean.
2952 static enum hrtimer_restart stmmac_tx_timer(struct hrtimer *t)
2954 struct stmmac_tx_queue *tx_q = container_of(t, struct stmmac_tx_queue, txtimer);
2955 struct stmmac_priv *priv = tx_q->priv_data;
2956 struct stmmac_channel *ch;
2957 struct napi_struct *napi;
2959 ch = &priv->channel[tx_q->queue_index];
2960 napi = tx_q->xsk_pool ? &ch->rxtx_napi : &ch->tx_napi;
2962 if (likely(napi_schedule_prep(napi))) {
2963 unsigned long flags;
2965 spin_lock_irqsave(&ch->lock, flags);
2966 stmmac_disable_dma_irq(priv, priv->ioaddr, ch->index, 0, 1);
2967 spin_unlock_irqrestore(&ch->lock, flags);
2968 __napi_schedule(napi);
2971 return HRTIMER_NORESTART;
2975 * stmmac_init_coalesce - init mitigation options.
2976 * @priv: driver private structure
2978 * This inits the coalesce parameters: i.e. timer rate,
2979 * timer handler and default threshold used for enabling the
2980 * interrupt on completion bit.
2982 static void stmmac_init_coalesce(struct stmmac_priv *priv)
2984 u32 tx_channel_count = priv->plat->tx_queues_to_use;
2985 u32 rx_channel_count = priv->plat->rx_queues_to_use;
2988 for (chan = 0; chan < tx_channel_count; chan++) {
2989 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2991 priv->tx_coal_frames[chan] = STMMAC_TX_FRAMES;
2992 priv->tx_coal_timer[chan] = STMMAC_COAL_TX_TIMER;
2994 hrtimer_init(&tx_q->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2995 tx_q->txtimer.function = stmmac_tx_timer;
2998 for (chan = 0; chan < rx_channel_count; chan++)
2999 priv->rx_coal_frames[chan] = STMMAC_RX_FRAMES;
3002 static void stmmac_set_rings_length(struct stmmac_priv *priv)
3004 u32 rx_channels_count = priv->plat->rx_queues_to_use;
3005 u32 tx_channels_count = priv->plat->tx_queues_to_use;
3008 /* set TX ring length */
3009 for (chan = 0; chan < tx_channels_count; chan++)
3010 stmmac_set_tx_ring_len(priv, priv->ioaddr,
3011 (priv->dma_tx_size - 1), chan);
3013 /* set RX ring length */
3014 for (chan = 0; chan < rx_channels_count; chan++)
3015 stmmac_set_rx_ring_len(priv, priv->ioaddr,
3016 (priv->dma_rx_size - 1), chan);
3020 * stmmac_set_tx_queue_weight - Set TX queue weight
3021 * @priv: driver private structure
3022 * Description: It is used for setting TX queues weight
3024 static void stmmac_set_tx_queue_weight(struct stmmac_priv *priv)
3026 u32 tx_queues_count = priv->plat->tx_queues_to_use;
3030 for (queue = 0; queue < tx_queues_count; queue++) {
3031 weight = priv->plat->tx_queues_cfg[queue].weight;
3032 stmmac_set_mtl_tx_queue_weight(priv, priv->hw, weight, queue);
3037 * stmmac_configure_cbs - Configure CBS in TX queue
3038 * @priv: driver private structure
3039 * Description: It is used for configuring CBS in AVB TX queues
3041 static void stmmac_configure_cbs(struct stmmac_priv *priv)
3043 u32 tx_queues_count = priv->plat->tx_queues_to_use;
3047 /* queue 0 is reserved for legacy traffic */
3048 for (queue = 1; queue < tx_queues_count; queue++) {
3049 mode_to_use = priv->plat->tx_queues_cfg[queue].mode_to_use;
3050 if (mode_to_use == MTL_QUEUE_DCB)
3053 stmmac_config_cbs(priv, priv->hw,
3054 priv->plat->tx_queues_cfg[queue].send_slope,
3055 priv->plat->tx_queues_cfg[queue].idle_slope,
3056 priv->plat->tx_queues_cfg[queue].high_credit,
3057 priv->plat->tx_queues_cfg[queue].low_credit,
3063 * stmmac_rx_queue_dma_chan_map - Map RX queue to RX dma channel
3064 * @priv: driver private structure
3065 * Description: It is used for mapping RX queues to RX dma channels
3067 static void stmmac_rx_queue_dma_chan_map(struct stmmac_priv *priv)
3069 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3073 for (queue = 0; queue < rx_queues_count; queue++) {
3074 chan = priv->plat->rx_queues_cfg[queue].chan;
3075 stmmac_map_mtl_to_dma(priv, priv->hw, queue, chan);
3080 * stmmac_mac_config_rx_queues_prio - Configure RX Queue priority
3081 * @priv: driver private structure
3082 * Description: It is used for configuring the RX Queue Priority
3084 static void stmmac_mac_config_rx_queues_prio(struct stmmac_priv *priv)
3086 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3090 for (queue = 0; queue < rx_queues_count; queue++) {
3091 if (!priv->plat->rx_queues_cfg[queue].use_prio)
3094 prio = priv->plat->rx_queues_cfg[queue].prio;
3095 stmmac_rx_queue_prio(priv, priv->hw, prio, queue);
3100 * stmmac_mac_config_tx_queues_prio - Configure TX Queue priority
3101 * @priv: driver private structure
3102 * Description: It is used for configuring the TX Queue Priority
3104 static void stmmac_mac_config_tx_queues_prio(struct stmmac_priv *priv)
3106 u32 tx_queues_count = priv->plat->tx_queues_to_use;
3110 for (queue = 0; queue < tx_queues_count; queue++) {
3111 if (!priv->plat->tx_queues_cfg[queue].use_prio)
3114 prio = priv->plat->tx_queues_cfg[queue].prio;
3115 stmmac_tx_queue_prio(priv, priv->hw, prio, queue);
3120 * stmmac_mac_config_rx_queues_routing - Configure RX Queue Routing
3121 * @priv: driver private structure
3122 * Description: It is used for configuring the RX queue routing
3124 static void stmmac_mac_config_rx_queues_routing(struct stmmac_priv *priv)
3126 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3130 for (queue = 0; queue < rx_queues_count; queue++) {
3131 /* no specific packet type routing specified for the queue */
3132 if (priv->plat->rx_queues_cfg[queue].pkt_route == 0x0)
3135 packet = priv->plat->rx_queues_cfg[queue].pkt_route;
3136 stmmac_rx_queue_routing(priv, priv->hw, packet, queue);
3140 static void stmmac_mac_config_rss(struct stmmac_priv *priv)
3142 if (!priv->dma_cap.rssen || !priv->plat->rss_en) {
3143 priv->rss.enable = false;
3147 if (priv->dev->features & NETIF_F_RXHASH)
3148 priv->rss.enable = true;
3150 priv->rss.enable = false;
3152 stmmac_rss_configure(priv, priv->hw, &priv->rss,
3153 priv->plat->rx_queues_to_use);
3157 * stmmac_mtl_configuration - Configure MTL
3158 * @priv: driver private structure
3159 * Description: It is used for configurring MTL
3161 static void stmmac_mtl_configuration(struct stmmac_priv *priv)
3163 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3164 u32 tx_queues_count = priv->plat->tx_queues_to_use;
3166 if (tx_queues_count > 1)
3167 stmmac_set_tx_queue_weight(priv);
3169 /* Configure MTL RX algorithms */
3170 if (rx_queues_count > 1)
3171 stmmac_prog_mtl_rx_algorithms(priv, priv->hw,
3172 priv->plat->rx_sched_algorithm);
3174 /* Configure MTL TX algorithms */
3175 if (tx_queues_count > 1)
3176 stmmac_prog_mtl_tx_algorithms(priv, priv->hw,
3177 priv->plat->tx_sched_algorithm);
3179 /* Configure CBS in AVB TX queues */
3180 if (tx_queues_count > 1)
3181 stmmac_configure_cbs(priv);
3183 /* Map RX MTL to DMA channels */
3184 stmmac_rx_queue_dma_chan_map(priv);
3186 /* Enable MAC RX Queues */
3187 stmmac_mac_enable_rx_queues(priv);
3189 /* Set RX priorities */
3190 if (rx_queues_count > 1)
3191 stmmac_mac_config_rx_queues_prio(priv);
3193 /* Set TX priorities */
3194 if (tx_queues_count > 1)
3195 stmmac_mac_config_tx_queues_prio(priv);
3197 /* Set RX routing */
3198 if (rx_queues_count > 1)
3199 stmmac_mac_config_rx_queues_routing(priv);
3201 /* Receive Side Scaling */
3202 if (rx_queues_count > 1)
3203 stmmac_mac_config_rss(priv);
3206 static void stmmac_safety_feat_configuration(struct stmmac_priv *priv)
3208 if (priv->dma_cap.asp) {
3209 netdev_info(priv->dev, "Enabling Safety Features\n");
3210 stmmac_safety_feat_config(priv, priv->ioaddr, priv->dma_cap.asp,
3211 priv->plat->safety_feat_cfg);
3213 netdev_info(priv->dev, "No Safety Features support found\n");
3217 static int stmmac_fpe_start_wq(struct stmmac_priv *priv)
3221 clear_bit(__FPE_TASK_SCHED, &priv->fpe_task_state);
3222 clear_bit(__FPE_REMOVING, &priv->fpe_task_state);
3224 name = priv->wq_name;
3225 sprintf(name, "%s-fpe", priv->dev->name);
3227 priv->fpe_wq = create_singlethread_workqueue(name);
3228 if (!priv->fpe_wq) {
3229 netdev_err(priv->dev, "%s: Failed to create workqueue\n", name);
3233 netdev_info(priv->dev, "FPE workqueue start");
3239 * stmmac_hw_setup - setup mac in a usable state.
3240 * @dev : pointer to the device structure.
3241 * @init_ptp: initialize PTP if set
3243 * this is the main function to setup the HW in a usable state because the
3244 * dma engine is reset, the core registers are configured (e.g. AXI,
3245 * Checksum features, timers). The DMA is ready to start receiving and
3248 * 0 on success and an appropriate (-)ve integer as defined in errno.h
3251 static int stmmac_hw_setup(struct net_device *dev, bool init_ptp)
3253 struct stmmac_priv *priv = netdev_priv(dev);
3254 u32 rx_cnt = priv->plat->rx_queues_to_use;
3255 u32 tx_cnt = priv->plat->tx_queues_to_use;
3260 /* DMA initialization and SW reset */
3261 ret = stmmac_init_dma_engine(priv);
3263 netdev_err(priv->dev, "%s: DMA engine initialization failed\n",
3268 /* Copy the MAC addr into the HW */
3269 stmmac_set_umac_addr(priv, priv->hw, dev->dev_addr, 0);
3271 /* PS and related bits will be programmed according to the speed */
3272 if (priv->hw->pcs) {
3273 int speed = priv->plat->mac_port_sel_speed;
3275 if ((speed == SPEED_10) || (speed == SPEED_100) ||
3276 (speed == SPEED_1000)) {
3277 priv->hw->ps = speed;
3279 dev_warn(priv->device, "invalid port speed\n");
3284 /* Initialize the MAC Core */
3285 stmmac_core_init(priv, priv->hw, dev);
3288 stmmac_mtl_configuration(priv);
3290 /* Initialize Safety Features */
3291 stmmac_safety_feat_configuration(priv);
3293 ret = stmmac_rx_ipc(priv, priv->hw);
3295 netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n");
3296 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
3297 priv->hw->rx_csum = 0;
3300 /* Enable the MAC Rx/Tx */
3301 stmmac_mac_set(priv, priv->ioaddr, true);
3303 /* Set the HW DMA mode and the COE */
3304 stmmac_dma_operation_mode(priv);
3306 stmmac_mmc_setup(priv);
3309 ret = stmmac_init_ptp(priv);
3310 if (ret == -EOPNOTSUPP)
3311 netdev_warn(priv->dev, "PTP not supported by HW\n");
3313 netdev_warn(priv->dev, "PTP init failed\n");
3316 priv->eee_tw_timer = STMMAC_DEFAULT_TWT_LS;
3318 /* Convert the timer from msec to usec */
3319 if (!priv->tx_lpi_timer)
3320 priv->tx_lpi_timer = eee_timer * 1000;
3322 if (priv->use_riwt) {
3325 for (queue = 0; queue < rx_cnt; queue++) {
3326 if (!priv->rx_riwt[queue])
3327 priv->rx_riwt[queue] = DEF_DMA_RIWT;
3329 stmmac_rx_watchdog(priv, priv->ioaddr,
3330 priv->rx_riwt[queue], queue);
3335 stmmac_pcs_ctrl_ane(priv, priv->ioaddr, 1, priv->hw->ps, 0);
3337 /* set TX and RX rings length */
3338 stmmac_set_rings_length(priv);
3342 for (chan = 0; chan < tx_cnt; chan++) {
3343 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
3345 /* TSO and TBS cannot co-exist */
3346 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3349 stmmac_enable_tso(priv, priv->ioaddr, 1, chan);
3353 /* Enable Split Header */
3354 sph_en = (priv->hw->rx_csum > 0) && priv->sph;
3355 for (chan = 0; chan < rx_cnt; chan++)
3356 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
3359 /* VLAN Tag Insertion */
3360 if (priv->dma_cap.vlins)
3361 stmmac_enable_vlan(priv, priv->hw, STMMAC_VLAN_INSERT);
3364 for (chan = 0; chan < tx_cnt; chan++) {
3365 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
3366 int enable = tx_q->tbs & STMMAC_TBS_AVAIL;
3368 stmmac_enable_tbs(priv, priv->ioaddr, enable, chan);
3371 /* Configure real RX and TX queues */
3372 netif_set_real_num_rx_queues(dev, priv->plat->rx_queues_to_use);
3373 netif_set_real_num_tx_queues(dev, priv->plat->tx_queues_to_use);
3375 /* Start the ball rolling... */
3376 stmmac_start_all_dma(priv);
3378 if (priv->dma_cap.fpesel) {
3379 stmmac_fpe_start_wq(priv);
3381 if (priv->plat->fpe_cfg->enable)
3382 stmmac_fpe_handshake(priv, true);
3388 static void stmmac_hw_teardown(struct net_device *dev)
3390 struct stmmac_priv *priv = netdev_priv(dev);
3392 clk_disable_unprepare(priv->plat->clk_ptp_ref);
3395 static void stmmac_free_irq(struct net_device *dev,
3396 enum request_irq_err irq_err, int irq_idx)
3398 struct stmmac_priv *priv = netdev_priv(dev);
3402 case REQ_IRQ_ERR_ALL:
3403 irq_idx = priv->plat->tx_queues_to_use;
3405 case REQ_IRQ_ERR_TX:
3406 for (j = irq_idx - 1; j >= 0; j--) {
3407 if (priv->tx_irq[j] > 0) {
3408 irq_set_affinity_hint(priv->tx_irq[j], NULL);
3409 free_irq(priv->tx_irq[j], &priv->tx_queue[j]);
3412 irq_idx = priv->plat->rx_queues_to_use;
3414 case REQ_IRQ_ERR_RX:
3415 for (j = irq_idx - 1; j >= 0; j--) {
3416 if (priv->rx_irq[j] > 0) {
3417 irq_set_affinity_hint(priv->rx_irq[j], NULL);
3418 free_irq(priv->rx_irq[j], &priv->rx_queue[j]);
3422 if (priv->sfty_ue_irq > 0 && priv->sfty_ue_irq != dev->irq)
3423 free_irq(priv->sfty_ue_irq, dev);
3425 case REQ_IRQ_ERR_SFTY_UE:
3426 if (priv->sfty_ce_irq > 0 && priv->sfty_ce_irq != dev->irq)
3427 free_irq(priv->sfty_ce_irq, dev);
3429 case REQ_IRQ_ERR_SFTY_CE:
3430 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq)
3431 free_irq(priv->lpi_irq, dev);
3433 case REQ_IRQ_ERR_LPI:
3434 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq)
3435 free_irq(priv->wol_irq, dev);
3437 case REQ_IRQ_ERR_WOL:
3438 free_irq(dev->irq, dev);
3440 case REQ_IRQ_ERR_MAC:
3441 case REQ_IRQ_ERR_NO:
3442 /* If MAC IRQ request error, no more IRQ to free */
3447 static int stmmac_request_irq_multi_msi(struct net_device *dev)
3449 struct stmmac_priv *priv = netdev_priv(dev);
3450 enum request_irq_err irq_err;
3457 /* For common interrupt */
3458 int_name = priv->int_name_mac;
3459 sprintf(int_name, "%s:%s", dev->name, "mac");
3460 ret = request_irq(dev->irq, stmmac_mac_interrupt,
3462 if (unlikely(ret < 0)) {
3463 netdev_err(priv->dev,
3464 "%s: alloc mac MSI %d (error: %d)\n",
3465 __func__, dev->irq, ret);
3466 irq_err = REQ_IRQ_ERR_MAC;
3470 /* Request the Wake IRQ in case of another line
3473 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq) {
3474 int_name = priv->int_name_wol;
3475 sprintf(int_name, "%s:%s", dev->name, "wol");
3476 ret = request_irq(priv->wol_irq,
3477 stmmac_mac_interrupt,
3479 if (unlikely(ret < 0)) {
3480 netdev_err(priv->dev,
3481 "%s: alloc wol MSI %d (error: %d)\n",
3482 __func__, priv->wol_irq, ret);
3483 irq_err = REQ_IRQ_ERR_WOL;
3488 /* Request the LPI IRQ in case of another line
3491 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq) {
3492 int_name = priv->int_name_lpi;
3493 sprintf(int_name, "%s:%s", dev->name, "lpi");
3494 ret = request_irq(priv->lpi_irq,
3495 stmmac_mac_interrupt,
3497 if (unlikely(ret < 0)) {
3498 netdev_err(priv->dev,
3499 "%s: alloc lpi MSI %d (error: %d)\n",
3500 __func__, priv->lpi_irq, ret);
3501 irq_err = REQ_IRQ_ERR_LPI;
3506 /* Request the Safety Feature Correctible Error line in
3507 * case of another line is used
3509 if (priv->sfty_ce_irq > 0 && priv->sfty_ce_irq != dev->irq) {
3510 int_name = priv->int_name_sfty_ce;
3511 sprintf(int_name, "%s:%s", dev->name, "safety-ce");
3512 ret = request_irq(priv->sfty_ce_irq,
3513 stmmac_safety_interrupt,
3515 if (unlikely(ret < 0)) {
3516 netdev_err(priv->dev,
3517 "%s: alloc sfty ce MSI %d (error: %d)\n",
3518 __func__, priv->sfty_ce_irq, ret);
3519 irq_err = REQ_IRQ_ERR_SFTY_CE;
3524 /* Request the Safety Feature Uncorrectible Error line in
3525 * case of another line is used
3527 if (priv->sfty_ue_irq > 0 && priv->sfty_ue_irq != dev->irq) {
3528 int_name = priv->int_name_sfty_ue;
3529 sprintf(int_name, "%s:%s", dev->name, "safety-ue");
3530 ret = request_irq(priv->sfty_ue_irq,
3531 stmmac_safety_interrupt,
3533 if (unlikely(ret < 0)) {
3534 netdev_err(priv->dev,
3535 "%s: alloc sfty ue MSI %d (error: %d)\n",
3536 __func__, priv->sfty_ue_irq, ret);
3537 irq_err = REQ_IRQ_ERR_SFTY_UE;
3542 /* Request Rx MSI irq */
3543 for (i = 0; i < priv->plat->rx_queues_to_use; i++) {
3544 if (i >= MTL_MAX_RX_QUEUES)
3546 if (priv->rx_irq[i] == 0)
3549 int_name = priv->int_name_rx_irq[i];
3550 sprintf(int_name, "%s:%s-%d", dev->name, "rx", i);
3551 ret = request_irq(priv->rx_irq[i],
3553 0, int_name, &priv->rx_queue[i]);
3554 if (unlikely(ret < 0)) {
3555 netdev_err(priv->dev,
3556 "%s: alloc rx-%d MSI %d (error: %d)\n",
3557 __func__, i, priv->rx_irq[i], ret);
3558 irq_err = REQ_IRQ_ERR_RX;
3562 cpumask_clear(&cpu_mask);
3563 cpumask_set_cpu(i % num_online_cpus(), &cpu_mask);
3564 irq_set_affinity_hint(priv->rx_irq[i], &cpu_mask);
3567 /* Request Tx MSI irq */
3568 for (i = 0; i < priv->plat->tx_queues_to_use; i++) {
3569 if (i >= MTL_MAX_TX_QUEUES)
3571 if (priv->tx_irq[i] == 0)
3574 int_name = priv->int_name_tx_irq[i];
3575 sprintf(int_name, "%s:%s-%d", dev->name, "tx", i);
3576 ret = request_irq(priv->tx_irq[i],
3578 0, int_name, &priv->tx_queue[i]);
3579 if (unlikely(ret < 0)) {
3580 netdev_err(priv->dev,
3581 "%s: alloc tx-%d MSI %d (error: %d)\n",
3582 __func__, i, priv->tx_irq[i], ret);
3583 irq_err = REQ_IRQ_ERR_TX;
3587 cpumask_clear(&cpu_mask);
3588 cpumask_set_cpu(i % num_online_cpus(), &cpu_mask);
3589 irq_set_affinity_hint(priv->tx_irq[i], &cpu_mask);
3595 stmmac_free_irq(dev, irq_err, irq_idx);
3599 static int stmmac_request_irq_single(struct net_device *dev)
3601 struct stmmac_priv *priv = netdev_priv(dev);
3602 enum request_irq_err irq_err;
3605 ret = request_irq(dev->irq, stmmac_interrupt,
3606 IRQF_SHARED, dev->name, dev);
3607 if (unlikely(ret < 0)) {
3608 netdev_err(priv->dev,
3609 "%s: ERROR: allocating the IRQ %d (error: %d)\n",
3610 __func__, dev->irq, ret);
3611 irq_err = REQ_IRQ_ERR_MAC;
3615 /* Request the Wake IRQ in case of another line
3618 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq) {
3619 ret = request_irq(priv->wol_irq, stmmac_interrupt,
3620 IRQF_SHARED, dev->name, dev);
3621 if (unlikely(ret < 0)) {
3622 netdev_err(priv->dev,
3623 "%s: ERROR: allocating the WoL IRQ %d (%d)\n",
3624 __func__, priv->wol_irq, ret);
3625 irq_err = REQ_IRQ_ERR_WOL;
3630 /* Request the IRQ lines */
3631 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq) {
3632 ret = request_irq(priv->lpi_irq, stmmac_interrupt,
3633 IRQF_SHARED, dev->name, dev);
3634 if (unlikely(ret < 0)) {
3635 netdev_err(priv->dev,
3636 "%s: ERROR: allocating the LPI IRQ %d (%d)\n",
3637 __func__, priv->lpi_irq, ret);
3638 irq_err = REQ_IRQ_ERR_LPI;
3646 stmmac_free_irq(dev, irq_err, 0);
3650 static int stmmac_request_irq(struct net_device *dev)
3652 struct stmmac_priv *priv = netdev_priv(dev);
3655 /* Request the IRQ lines */
3656 if (priv->plat->multi_msi_en)
3657 ret = stmmac_request_irq_multi_msi(dev);
3659 ret = stmmac_request_irq_single(dev);
3665 * stmmac_open - open entry point of the driver
3666 * @dev : pointer to the device structure.
3668 * This function is the open entry point of the driver.
3670 * 0 on success and an appropriate (-)ve integer as defined in errno.h
3673 static int stmmac_open(struct net_device *dev)
3675 struct stmmac_priv *priv = netdev_priv(dev);
3676 int mode = priv->plat->phy_interface;
3681 ret = pm_runtime_get_sync(priv->device);
3683 pm_runtime_put_noidle(priv->device);
3687 if (priv->hw->pcs != STMMAC_PCS_TBI &&
3688 priv->hw->pcs != STMMAC_PCS_RTBI &&
3690 xpcs_get_an_mode(priv->hw->xpcs, mode) != DW_AN_C73)) {
3691 ret = stmmac_init_phy(dev);
3693 netdev_err(priv->dev,
3694 "%s: Cannot attach to PHY (error: %d)\n",
3696 goto init_phy_error;
3700 /* Extra statistics */
3701 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
3702 priv->xstats.threshold = tc;
3704 bfsize = stmmac_set_16kib_bfsize(priv, dev->mtu);
3708 if (bfsize < BUF_SIZE_16KiB)
3709 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
3711 priv->dma_buf_sz = bfsize;
3714 priv->rx_copybreak = STMMAC_RX_COPYBREAK;
3716 if (!priv->dma_tx_size)
3717 priv->dma_tx_size = DMA_DEFAULT_TX_SIZE;
3718 if (!priv->dma_rx_size)
3719 priv->dma_rx_size = DMA_DEFAULT_RX_SIZE;
3721 /* Earlier check for TBS */
3722 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++) {
3723 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
3724 int tbs_en = priv->plat->tx_queues_cfg[chan].tbs_en;
3726 /* Setup per-TXQ tbs flag before TX descriptor alloc */
3727 tx_q->tbs |= tbs_en ? STMMAC_TBS_AVAIL : 0;
3730 ret = alloc_dma_desc_resources(priv);
3732 netdev_err(priv->dev, "%s: DMA descriptors allocation failed\n",
3734 goto dma_desc_error;
3737 ret = init_dma_desc_rings(dev, GFP_KERNEL);
3739 netdev_err(priv->dev, "%s: DMA descriptors initialization failed\n",
3744 ret = stmmac_hw_setup(dev, true);
3746 netdev_err(priv->dev, "%s: Hw setup failed\n", __func__);
3750 stmmac_init_coalesce(priv);
3752 phylink_start(priv->phylink);
3753 /* We may have called phylink_speed_down before */
3754 phylink_speed_up(priv->phylink);
3756 ret = stmmac_request_irq(dev);
3760 stmmac_enable_all_queues(priv);
3761 netif_tx_start_all_queues(priv->dev);
3766 phylink_stop(priv->phylink);
3768 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
3769 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
3771 stmmac_hw_teardown(dev);
3773 free_dma_desc_resources(priv);
3775 phylink_disconnect_phy(priv->phylink);
3777 pm_runtime_put(priv->device);
3781 static void stmmac_fpe_stop_wq(struct stmmac_priv *priv)
3783 set_bit(__FPE_REMOVING, &priv->fpe_task_state);
3786 destroy_workqueue(priv->fpe_wq);
3788 netdev_info(priv->dev, "FPE workqueue stop");
3792 * stmmac_release - close entry point of the driver
3793 * @dev : device pointer.
3795 * This is the stop entry point of the driver.
3797 static int stmmac_release(struct net_device *dev)
3799 struct stmmac_priv *priv = netdev_priv(dev);
3802 netif_tx_disable(dev);
3804 if (device_may_wakeup(priv->device))
3805 phylink_speed_down(priv->phylink, false);
3806 /* Stop and disconnect the PHY */
3807 phylink_stop(priv->phylink);
3808 phylink_disconnect_phy(priv->phylink);
3810 stmmac_disable_all_queues(priv);
3812 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
3813 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
3815 /* Free the IRQ lines */
3816 stmmac_free_irq(dev, REQ_IRQ_ERR_ALL, 0);
3818 if (priv->eee_enabled) {
3819 priv->tx_path_in_lpi_mode = false;
3820 del_timer_sync(&priv->eee_ctrl_timer);
3823 /* Stop TX/RX DMA and clear the descriptors */
3824 stmmac_stop_all_dma(priv);
3826 /* Release and free the Rx/Tx resources */
3827 free_dma_desc_resources(priv);
3829 /* Disable the MAC Rx/Tx */
3830 stmmac_mac_set(priv, priv->ioaddr, false);
3832 netif_carrier_off(dev);
3834 stmmac_release_ptp(priv);
3836 pm_runtime_put(priv->device);
3838 if (priv->dma_cap.fpesel)
3839 stmmac_fpe_stop_wq(priv);
3844 static bool stmmac_vlan_insert(struct stmmac_priv *priv, struct sk_buff *skb,
3845 struct stmmac_tx_queue *tx_q)
3847 u16 tag = 0x0, inner_tag = 0x0;
3848 u32 inner_type = 0x0;
3851 if (!priv->dma_cap.vlins)
3853 if (!skb_vlan_tag_present(skb))
3855 if (skb->vlan_proto == htons(ETH_P_8021AD)) {
3856 inner_tag = skb_vlan_tag_get(skb);
3857 inner_type = STMMAC_VLAN_INSERT;
3860 tag = skb_vlan_tag_get(skb);
3862 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3863 p = &tx_q->dma_entx[tx_q->cur_tx].basic;
3865 p = &tx_q->dma_tx[tx_q->cur_tx];
3867 if (stmmac_set_desc_vlan_tag(priv, p, tag, inner_tag, inner_type))
3870 stmmac_set_tx_owner(priv, p);
3871 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
3876 * stmmac_tso_allocator - close entry point of the driver
3877 * @priv: driver private structure
3878 * @des: buffer start address
3879 * @total_len: total length to fill in descriptors
3880 * @last_segment: condition for the last descriptor
3881 * @queue: TX queue index
3883 * This function fills descriptor and request new descriptors according to
3884 * buffer length to fill
3886 static void stmmac_tso_allocator(struct stmmac_priv *priv, dma_addr_t des,
3887 int total_len, bool last_segment, u32 queue)
3889 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
3890 struct dma_desc *desc;
3894 tmp_len = total_len;
3896 while (tmp_len > 0) {
3897 dma_addr_t curr_addr;
3899 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx,
3901 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
3903 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3904 desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
3906 desc = &tx_q->dma_tx[tx_q->cur_tx];
3908 curr_addr = des + (total_len - tmp_len);
3909 if (priv->dma_cap.addr64 <= 32)
3910 desc->des0 = cpu_to_le32(curr_addr);
3912 stmmac_set_desc_addr(priv, desc, curr_addr);
3914 buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ?
3915 TSO_MAX_BUFF_SIZE : tmp_len;
3917 stmmac_prepare_tso_tx_desc(priv, desc, 0, buff_size,
3919 (last_segment) && (tmp_len <= TSO_MAX_BUFF_SIZE),
3922 tmp_len -= TSO_MAX_BUFF_SIZE;
3926 static void stmmac_flush_tx_descriptors(struct stmmac_priv *priv, int queue)
3928 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
3931 if (likely(priv->extend_desc))
3932 desc_size = sizeof(struct dma_extended_desc);
3933 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
3934 desc_size = sizeof(struct dma_edesc);
3936 desc_size = sizeof(struct dma_desc);
3938 /* The own bit must be the latest setting done when prepare the
3939 * descriptor and then barrier is needed to make sure that
3940 * all is coherent before granting the DMA engine.
3944 tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * desc_size);
3945 stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue);
3949 * stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO)
3950 * @skb : the socket buffer
3951 * @dev : device pointer
3952 * Description: this is the transmit function that is called on TSO frames
3953 * (support available on GMAC4 and newer chips).
3954 * Diagram below show the ring programming in case of TSO frames:
3958 * | DES0 |---> buffer1 = L2/L3/L4 header
3959 * | DES1 |---> TCP Payload (can continue on next descr...)
3960 * | DES2 |---> buffer 1 and 2 len
3961 * | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0]
3967 * | DES0 | --| Split TCP Payload on Buffers 1 and 2
3969 * | DES2 | --> buffer 1 and 2 len
3973 * mss is fixed when enable tso, so w/o programming the TDES3 ctx field.
3975 static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev)
3977 struct dma_desc *desc, *first, *mss_desc = NULL;
3978 struct stmmac_priv *priv = netdev_priv(dev);
3979 int nfrags = skb_shinfo(skb)->nr_frags;
3980 u32 queue = skb_get_queue_mapping(skb);
3981 unsigned int first_entry, tx_packets;
3982 int tmp_pay_len = 0, first_tx;
3983 struct stmmac_tx_queue *tx_q;
3984 bool has_vlan, set_ic;
3985 u8 proto_hdr_len, hdr;
3990 tx_q = &priv->tx_queue[queue];
3991 first_tx = tx_q->cur_tx;
3993 /* Compute header lengths */
3994 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
3995 proto_hdr_len = skb_transport_offset(skb) + sizeof(struct udphdr);
3996 hdr = sizeof(struct udphdr);
3998 proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
3999 hdr = tcp_hdrlen(skb);
4002 /* Desc availability based on threshold should be enough safe */
4003 if (unlikely(stmmac_tx_avail(priv, queue) <
4004 (((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) {
4005 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
4006 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
4008 /* This is a hard error, log it. */
4009 netdev_err(priv->dev,
4010 "%s: Tx Ring full when queue awake\n",
4013 return NETDEV_TX_BUSY;
4016 pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */
4018 mss = skb_shinfo(skb)->gso_size;
4020 /* set new MSS value if needed */
4021 if (mss != tx_q->mss) {
4022 if (tx_q->tbs & STMMAC_TBS_AVAIL)
4023 mss_desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
4025 mss_desc = &tx_q->dma_tx[tx_q->cur_tx];
4027 stmmac_set_mss(priv, mss_desc, mss);
4029 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx,
4031 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
4034 if (netif_msg_tx_queued(priv)) {
4035 pr_info("%s: hdrlen %d, hdr_len %d, pay_len %d, mss %d\n",
4036 __func__, hdr, proto_hdr_len, pay_len, mss);
4037 pr_info("\tskb->len %d, skb->data_len %d\n", skb->len,
4041 /* Check if VLAN can be inserted by HW */
4042 has_vlan = stmmac_vlan_insert(priv, skb, tx_q);
4044 first_entry = tx_q->cur_tx;
4045 WARN_ON(tx_q->tx_skbuff[first_entry]);
4047 if (tx_q->tbs & STMMAC_TBS_AVAIL)
4048 desc = &tx_q->dma_entx[first_entry].basic;
4050 desc = &tx_q->dma_tx[first_entry];
4054 stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT);
4056 /* first descriptor: fill Headers on Buf1 */
4057 des = dma_map_single(priv->device, skb->data, skb_headlen(skb),
4059 if (dma_mapping_error(priv->device, des))
4062 tx_q->tx_skbuff_dma[first_entry].buf = des;
4063 tx_q->tx_skbuff_dma[first_entry].len = skb_headlen(skb);
4064 tx_q->tx_skbuff_dma[first_entry].map_as_page = false;
4065 tx_q->tx_skbuff_dma[first_entry].buf_type = STMMAC_TXBUF_T_SKB;
4067 if (priv->dma_cap.addr64 <= 32) {
4068 first->des0 = cpu_to_le32(des);
4070 /* Fill start of payload in buff2 of first descriptor */
4072 first->des1 = cpu_to_le32(des + proto_hdr_len);
4074 /* If needed take extra descriptors to fill the remaining payload */
4075 tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE;
4077 stmmac_set_desc_addr(priv, first, des);
4078 tmp_pay_len = pay_len;
4079 des += proto_hdr_len;
4083 stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0), queue);
4085 /* Prepare fragments */
4086 for (i = 0; i < nfrags; i++) {
4087 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4089 des = skb_frag_dma_map(priv->device, frag, 0,
4090 skb_frag_size(frag),
4092 if (dma_mapping_error(priv->device, des))
4095 stmmac_tso_allocator(priv, des, skb_frag_size(frag),
4096 (i == nfrags - 1), queue);
4098 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf = des;
4099 tx_q->tx_skbuff_dma[tx_q->cur_tx].len = skb_frag_size(frag);
4100 tx_q->tx_skbuff_dma[tx_q->cur_tx].map_as_page = true;
4101 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf_type = STMMAC_TXBUF_T_SKB;
4104 tx_q->tx_skbuff_dma[tx_q->cur_tx].last_segment = true;
4106 /* Only the last descriptor gets to point to the skb. */
4107 tx_q->tx_skbuff[tx_q->cur_tx] = skb;
4108 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf_type = STMMAC_TXBUF_T_SKB;
4110 /* Manage tx mitigation */
4111 tx_packets = (tx_q->cur_tx + 1) - first_tx;
4112 tx_q->tx_count_frames += tx_packets;
4114 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en)
4116 else if (!priv->tx_coal_frames[queue])
4118 else if (tx_packets > priv->tx_coal_frames[queue])
4120 else if ((tx_q->tx_count_frames %
4121 priv->tx_coal_frames[queue]) < tx_packets)
4127 if (tx_q->tbs & STMMAC_TBS_AVAIL)
4128 desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
4130 desc = &tx_q->dma_tx[tx_q->cur_tx];
4132 tx_q->tx_count_frames = 0;
4133 stmmac_set_tx_ic(priv, desc);
4134 priv->xstats.tx_set_ic_bit++;
4137 /* We've used all descriptors we need for this skb, however,
4138 * advance cur_tx so that it references a fresh descriptor.
4139 * ndo_start_xmit will fill this descriptor the next time it's
4140 * called and stmmac_tx_clean may clean up to this descriptor.
4142 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
4144 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
4145 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
4147 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
4150 dev->stats.tx_bytes += skb->len;
4151 priv->xstats.tx_tso_frames++;
4152 priv->xstats.tx_tso_nfrags += nfrags;
4154 if (priv->sarc_type)
4155 stmmac_set_desc_sarc(priv, first, priv->sarc_type);
4157 skb_tx_timestamp(skb);
4159 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4160 priv->hwts_tx_en)) {
4161 /* declare that device is doing timestamping */
4162 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4163 stmmac_enable_tx_timestamp(priv, first);
4166 /* Complete the first descriptor before granting the DMA */
4167 stmmac_prepare_tso_tx_desc(priv, first, 1,
4170 1, tx_q->tx_skbuff_dma[first_entry].last_segment,
4171 hdr / 4, (skb->len - proto_hdr_len));
4173 /* If context desc is used to change MSS */
4175 /* Make sure that first descriptor has been completely
4176 * written, including its own bit. This is because MSS is
4177 * actually before first descriptor, so we need to make
4178 * sure that MSS's own bit is the last thing written.
4181 stmmac_set_tx_owner(priv, mss_desc);
4184 if (netif_msg_pktdata(priv)) {
4185 pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n",
4186 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
4187 tx_q->cur_tx, first, nfrags);
4188 pr_info(">>> frame to be transmitted: ");
4189 print_pkt(skb->data, skb_headlen(skb));
4192 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
4194 stmmac_flush_tx_descriptors(priv, queue);
4195 stmmac_tx_timer_arm(priv, queue);
4197 return NETDEV_TX_OK;
4200 dev_err(priv->device, "Tx dma map failed\n");
4202 priv->dev->stats.tx_dropped++;
4203 return NETDEV_TX_OK;
4207 * stmmac_xmit - Tx entry point of the driver
4208 * @skb : the socket buffer
4209 * @dev : device pointer
4210 * Description : this is the tx entry point of the driver.
4211 * It programs the chain or the ring and supports oversized frames
4214 static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
4216 unsigned int first_entry, tx_packets, enh_desc;
4217 struct stmmac_priv *priv = netdev_priv(dev);
4218 unsigned int nopaged_len = skb_headlen(skb);
4219 int i, csum_insertion = 0, is_jumbo = 0;
4220 u32 queue = skb_get_queue_mapping(skb);
4221 int nfrags = skb_shinfo(skb)->nr_frags;
4222 int gso = skb_shinfo(skb)->gso_type;
4223 struct dma_edesc *tbs_desc = NULL;
4224 struct dma_desc *desc, *first;
4225 struct stmmac_tx_queue *tx_q;
4226 bool has_vlan, set_ic;
4227 int entry, first_tx;
4230 tx_q = &priv->tx_queue[queue];
4231 first_tx = tx_q->cur_tx;
4233 if (priv->tx_path_in_lpi_mode && priv->eee_sw_timer_en)
4234 stmmac_disable_eee_mode(priv);
4236 /* Manage oversized TCP frames for GMAC4 device */
4237 if (skb_is_gso(skb) && priv->tso) {
4238 if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
4239 return stmmac_tso_xmit(skb, dev);
4240 if (priv->plat->has_gmac4 && (gso & SKB_GSO_UDP_L4))
4241 return stmmac_tso_xmit(skb, dev);
4244 if (unlikely(stmmac_tx_avail(priv, queue) < nfrags + 1)) {
4245 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
4246 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
4248 /* This is a hard error, log it. */
4249 netdev_err(priv->dev,
4250 "%s: Tx Ring full when queue awake\n",
4253 return NETDEV_TX_BUSY;
4256 /* Check if VLAN can be inserted by HW */
4257 has_vlan = stmmac_vlan_insert(priv, skb, tx_q);
4259 entry = tx_q->cur_tx;
4260 first_entry = entry;
4261 WARN_ON(tx_q->tx_skbuff[first_entry]);
4263 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
4265 if (likely(priv->extend_desc))
4266 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
4267 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4268 desc = &tx_q->dma_entx[entry].basic;
4270 desc = tx_q->dma_tx + entry;
4275 stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT);
4277 enh_desc = priv->plat->enh_desc;
4278 /* To program the descriptors according to the size of the frame */
4280 is_jumbo = stmmac_is_jumbo_frm(priv, skb->len, enh_desc);
4282 if (unlikely(is_jumbo)) {
4283 entry = stmmac_jumbo_frm(priv, tx_q, skb, csum_insertion);
4284 if (unlikely(entry < 0) && (entry != -EINVAL))
4288 for (i = 0; i < nfrags; i++) {
4289 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4290 int len = skb_frag_size(frag);
4291 bool last_segment = (i == (nfrags - 1));
4293 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
4294 WARN_ON(tx_q->tx_skbuff[entry]);
4296 if (likely(priv->extend_desc))
4297 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
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 des = skb_frag_dma_map(priv->device, frag, 0, len,
4305 if (dma_mapping_error(priv->device, des))
4306 goto dma_map_err; /* should reuse desc w/o issues */
4308 tx_q->tx_skbuff_dma[entry].buf = des;
4310 stmmac_set_desc_addr(priv, desc, des);
4312 tx_q->tx_skbuff_dma[entry].map_as_page = true;
4313 tx_q->tx_skbuff_dma[entry].len = len;
4314 tx_q->tx_skbuff_dma[entry].last_segment = last_segment;
4315 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_SKB;
4317 /* Prepare the descriptor and set the own bit too */
4318 stmmac_prepare_tx_desc(priv, desc, 0, len, csum_insertion,
4319 priv->mode, 1, last_segment, skb->len);
4322 /* Only the last descriptor gets to point to the skb. */
4323 tx_q->tx_skbuff[entry] = skb;
4324 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_SKB;
4326 /* According to the coalesce parameter the IC bit for the latest
4327 * segment is reset and the timer re-started to clean the tx status.
4328 * This approach takes care about the fragments: desc is the first
4329 * element in case of no SG.
4331 tx_packets = (entry + 1) - first_tx;
4332 tx_q->tx_count_frames += tx_packets;
4334 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en)
4336 else if (!priv->tx_coal_frames[queue])
4338 else if (tx_packets > priv->tx_coal_frames[queue])
4340 else if ((tx_q->tx_count_frames %
4341 priv->tx_coal_frames[queue]) < tx_packets)
4347 if (likely(priv->extend_desc))
4348 desc = &tx_q->dma_etx[entry].basic;
4349 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4350 desc = &tx_q->dma_entx[entry].basic;
4352 desc = &tx_q->dma_tx[entry];
4354 tx_q->tx_count_frames = 0;
4355 stmmac_set_tx_ic(priv, desc);
4356 priv->xstats.tx_set_ic_bit++;
4359 /* We've used all descriptors we need for this skb, however,
4360 * advance cur_tx so that it references a fresh descriptor.
4361 * ndo_start_xmit will fill this descriptor the next time it's
4362 * called and stmmac_tx_clean may clean up to this descriptor.
4364 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
4365 tx_q->cur_tx = entry;
4367 if (netif_msg_pktdata(priv)) {
4368 netdev_dbg(priv->dev,
4369 "%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
4370 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
4371 entry, first, nfrags);
4373 netdev_dbg(priv->dev, ">>> frame to be transmitted: ");
4374 print_pkt(skb->data, skb->len);
4377 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
4378 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
4380 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
4383 dev->stats.tx_bytes += skb->len;
4385 if (priv->sarc_type)
4386 stmmac_set_desc_sarc(priv, first, priv->sarc_type);
4388 skb_tx_timestamp(skb);
4390 /* Ready to fill the first descriptor and set the OWN bit w/o any
4391 * problems because all the descriptors are actually ready to be
4392 * passed to the DMA engine.
4394 if (likely(!is_jumbo)) {
4395 bool last_segment = (nfrags == 0);
4397 des = dma_map_single(priv->device, skb->data,
4398 nopaged_len, DMA_TO_DEVICE);
4399 if (dma_mapping_error(priv->device, des))
4402 tx_q->tx_skbuff_dma[first_entry].buf = des;
4403 tx_q->tx_skbuff_dma[first_entry].buf_type = STMMAC_TXBUF_T_SKB;
4404 tx_q->tx_skbuff_dma[first_entry].map_as_page = false;
4406 stmmac_set_desc_addr(priv, first, des);
4408 tx_q->tx_skbuff_dma[first_entry].len = nopaged_len;
4409 tx_q->tx_skbuff_dma[first_entry].last_segment = last_segment;
4411 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4412 priv->hwts_tx_en)) {
4413 /* declare that device is doing timestamping */
4414 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4415 stmmac_enable_tx_timestamp(priv, first);
4418 /* Prepare the first descriptor setting the OWN bit too */
4419 stmmac_prepare_tx_desc(priv, first, 1, nopaged_len,
4420 csum_insertion, priv->mode, 0, last_segment,
4424 if (tx_q->tbs & STMMAC_TBS_EN) {
4425 struct timespec64 ts = ns_to_timespec64(skb->tstamp);
4427 tbs_desc = &tx_q->dma_entx[first_entry];
4428 stmmac_set_desc_tbs(priv, tbs_desc, ts.tv_sec, ts.tv_nsec);
4431 stmmac_set_tx_owner(priv, first);
4433 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
4435 stmmac_enable_dma_transmission(priv, priv->ioaddr);
4437 stmmac_flush_tx_descriptors(priv, queue);
4438 stmmac_tx_timer_arm(priv, queue);
4440 return NETDEV_TX_OK;
4443 netdev_err(priv->dev, "Tx DMA map failed\n");
4445 priv->dev->stats.tx_dropped++;
4446 return NETDEV_TX_OK;
4449 static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
4451 struct vlan_ethhdr *veth;
4455 veth = (struct vlan_ethhdr *)skb->data;
4456 vlan_proto = veth->h_vlan_proto;
4458 if ((vlan_proto == htons(ETH_P_8021Q) &&
4459 dev->features & NETIF_F_HW_VLAN_CTAG_RX) ||
4460 (vlan_proto == htons(ETH_P_8021AD) &&
4461 dev->features & NETIF_F_HW_VLAN_STAG_RX)) {
4462 /* pop the vlan tag */
4463 vlanid = ntohs(veth->h_vlan_TCI);
4464 memmove(skb->data + VLAN_HLEN, veth, ETH_ALEN * 2);
4465 skb_pull(skb, VLAN_HLEN);
4466 __vlan_hwaccel_put_tag(skb, vlan_proto, vlanid);
4471 * stmmac_rx_refill - refill used skb preallocated buffers
4472 * @priv: driver private structure
4473 * @queue: RX queue index
4474 * Description : this is to reallocate the skb for the reception process
4475 * that is based on zero-copy.
4477 static inline void stmmac_rx_refill(struct stmmac_priv *priv, u32 queue)
4479 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4480 int dirty = stmmac_rx_dirty(priv, queue);
4481 unsigned int entry = rx_q->dirty_rx;
4482 gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
4484 if (priv->dma_cap.addr64 <= 32)
4487 while (dirty-- > 0) {
4488 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry];
4492 if (priv->extend_desc)
4493 p = (struct dma_desc *)(rx_q->dma_erx + entry);
4495 p = rx_q->dma_rx + entry;
4498 buf->page = page_pool_alloc_pages(rx_q->page_pool, gfp);
4503 if (priv->sph && !buf->sec_page) {
4504 buf->sec_page = page_pool_alloc_pages(rx_q->page_pool, gfp);
4508 buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
4511 buf->addr = page_pool_get_dma_addr(buf->page) + buf->page_offset;
4513 stmmac_set_desc_addr(priv, p, buf->addr);
4515 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true);
4517 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false);
4518 stmmac_refill_desc3(priv, rx_q, p);
4520 rx_q->rx_count_frames++;
4521 rx_q->rx_count_frames += priv->rx_coal_frames[queue];
4522 if (rx_q->rx_count_frames > priv->rx_coal_frames[queue])
4523 rx_q->rx_count_frames = 0;
4525 use_rx_wd = !priv->rx_coal_frames[queue];
4526 use_rx_wd |= rx_q->rx_count_frames > 0;
4527 if (!priv->use_riwt)
4531 stmmac_set_rx_owner(priv, p, use_rx_wd);
4533 entry = STMMAC_GET_ENTRY(entry, priv->dma_rx_size);
4535 rx_q->dirty_rx = entry;
4536 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
4537 (rx_q->dirty_rx * sizeof(struct dma_desc));
4538 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue);
4541 static unsigned int stmmac_rx_buf1_len(struct stmmac_priv *priv,
4543 int status, unsigned int len)
4545 unsigned int plen = 0, hlen = 0;
4546 int coe = priv->hw->rx_csum;
4548 /* Not first descriptor, buffer is always zero */
4549 if (priv->sph && len)
4552 /* First descriptor, get split header length */
4553 stmmac_get_rx_header_len(priv, p, &hlen);
4554 if (priv->sph && hlen) {
4555 priv->xstats.rx_split_hdr_pkt_n++;
4559 /* First descriptor, not last descriptor and not split header */
4560 if (status & rx_not_ls)
4561 return priv->dma_buf_sz;
4563 plen = stmmac_get_rx_frame_len(priv, p, coe);
4565 /* First descriptor and last descriptor and not split header */
4566 return min_t(unsigned int, priv->dma_buf_sz, plen);
4569 static unsigned int stmmac_rx_buf2_len(struct stmmac_priv *priv,
4571 int status, unsigned int len)
4573 int coe = priv->hw->rx_csum;
4574 unsigned int plen = 0;
4576 /* Not split header, buffer is not available */
4580 /* Not last descriptor */
4581 if (status & rx_not_ls)
4582 return priv->dma_buf_sz;
4584 plen = stmmac_get_rx_frame_len(priv, p, coe);
4586 /* Last descriptor */
4590 static int stmmac_xdp_xmit_xdpf(struct stmmac_priv *priv, int queue,
4591 struct xdp_frame *xdpf, bool dma_map)
4593 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
4594 unsigned int entry = tx_q->cur_tx;
4595 struct dma_desc *tx_desc;
4596 dma_addr_t dma_addr;
4599 if (stmmac_tx_avail(priv, queue) < STMMAC_TX_THRESH(priv))
4600 return STMMAC_XDP_CONSUMED;
4602 if (likely(priv->extend_desc))
4603 tx_desc = (struct dma_desc *)(tx_q->dma_etx + entry);
4604 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4605 tx_desc = &tx_q->dma_entx[entry].basic;
4607 tx_desc = tx_q->dma_tx + entry;
4610 dma_addr = dma_map_single(priv->device, xdpf->data,
4611 xdpf->len, DMA_TO_DEVICE);
4612 if (dma_mapping_error(priv->device, dma_addr))
4613 return STMMAC_XDP_CONSUMED;
4615 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XDP_NDO;
4617 struct page *page = virt_to_page(xdpf->data);
4619 dma_addr = page_pool_get_dma_addr(page) + sizeof(*xdpf) +
4621 dma_sync_single_for_device(priv->device, dma_addr,
4622 xdpf->len, DMA_BIDIRECTIONAL);
4624 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XDP_TX;
4627 tx_q->tx_skbuff_dma[entry].buf = dma_addr;
4628 tx_q->tx_skbuff_dma[entry].map_as_page = false;
4629 tx_q->tx_skbuff_dma[entry].len = xdpf->len;
4630 tx_q->tx_skbuff_dma[entry].last_segment = true;
4631 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
4633 tx_q->xdpf[entry] = xdpf;
4635 stmmac_set_desc_addr(priv, tx_desc, dma_addr);
4637 stmmac_prepare_tx_desc(priv, tx_desc, 1, xdpf->len,
4638 true, priv->mode, true, true,
4641 tx_q->tx_count_frames++;
4643 if (tx_q->tx_count_frames % priv->tx_coal_frames[queue] == 0)
4649 tx_q->tx_count_frames = 0;
4650 stmmac_set_tx_ic(priv, tx_desc);
4651 priv->xstats.tx_set_ic_bit++;
4654 stmmac_enable_dma_transmission(priv, priv->ioaddr);
4656 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
4657 tx_q->cur_tx = entry;
4659 return STMMAC_XDP_TX;
4662 static int stmmac_xdp_get_tx_queue(struct stmmac_priv *priv,
4667 if (unlikely(index < 0))
4670 while (index >= priv->plat->tx_queues_to_use)
4671 index -= priv->plat->tx_queues_to_use;
4676 static int stmmac_xdp_xmit_back(struct stmmac_priv *priv,
4677 struct xdp_buff *xdp)
4679 struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp);
4680 int cpu = smp_processor_id();
4681 struct netdev_queue *nq;
4685 if (unlikely(!xdpf))
4686 return STMMAC_XDP_CONSUMED;
4688 queue = stmmac_xdp_get_tx_queue(priv, cpu);
4689 nq = netdev_get_tx_queue(priv->dev, queue);
4691 __netif_tx_lock(nq, cpu);
4692 /* Avoids TX time-out as we are sharing with slow path */
4693 txq_trans_cond_update(nq);
4695 res = stmmac_xdp_xmit_xdpf(priv, queue, xdpf, false);
4696 if (res == STMMAC_XDP_TX)
4697 stmmac_flush_tx_descriptors(priv, queue);
4699 __netif_tx_unlock(nq);
4704 static int __stmmac_xdp_run_prog(struct stmmac_priv *priv,
4705 struct bpf_prog *prog,
4706 struct xdp_buff *xdp)
4711 act = bpf_prog_run_xdp(prog, xdp);
4714 res = STMMAC_XDP_PASS;
4717 res = stmmac_xdp_xmit_back(priv, xdp);
4720 if (xdp_do_redirect(priv->dev, xdp, prog) < 0)
4721 res = STMMAC_XDP_CONSUMED;
4723 res = STMMAC_XDP_REDIRECT;
4726 bpf_warn_invalid_xdp_action(priv->dev, prog, act);
4729 trace_xdp_exception(priv->dev, prog, act);
4732 res = STMMAC_XDP_CONSUMED;
4739 static struct sk_buff *stmmac_xdp_run_prog(struct stmmac_priv *priv,
4740 struct xdp_buff *xdp)
4742 struct bpf_prog *prog;
4745 prog = READ_ONCE(priv->xdp_prog);
4747 res = STMMAC_XDP_PASS;
4751 res = __stmmac_xdp_run_prog(priv, prog, xdp);
4753 return ERR_PTR(-res);
4756 static void stmmac_finalize_xdp_rx(struct stmmac_priv *priv,
4759 int cpu = smp_processor_id();
4762 queue = stmmac_xdp_get_tx_queue(priv, cpu);
4764 if (xdp_status & STMMAC_XDP_TX)
4765 stmmac_tx_timer_arm(priv, queue);
4767 if (xdp_status & STMMAC_XDP_REDIRECT)
4771 static struct sk_buff *stmmac_construct_skb_zc(struct stmmac_channel *ch,
4772 struct xdp_buff *xdp)
4774 unsigned int metasize = xdp->data - xdp->data_meta;
4775 unsigned int datasize = xdp->data_end - xdp->data;
4776 struct sk_buff *skb;
4778 skb = __napi_alloc_skb(&ch->rxtx_napi,
4779 xdp->data_end - xdp->data_hard_start,
4780 GFP_ATOMIC | __GFP_NOWARN);
4784 skb_reserve(skb, xdp->data - xdp->data_hard_start);
4785 memcpy(__skb_put(skb, datasize), xdp->data, datasize);
4787 skb_metadata_set(skb, metasize);
4792 static void stmmac_dispatch_skb_zc(struct stmmac_priv *priv, u32 queue,
4793 struct dma_desc *p, struct dma_desc *np,
4794 struct xdp_buff *xdp)
4796 struct stmmac_channel *ch = &priv->channel[queue];
4797 unsigned int len = xdp->data_end - xdp->data;
4798 enum pkt_hash_types hash_type;
4799 int coe = priv->hw->rx_csum;
4800 struct sk_buff *skb;
4803 skb = stmmac_construct_skb_zc(ch, xdp);
4805 priv->dev->stats.rx_dropped++;
4809 stmmac_get_rx_hwtstamp(priv, p, np, skb);
4810 stmmac_rx_vlan(priv->dev, skb);
4811 skb->protocol = eth_type_trans(skb, priv->dev);
4814 skb_checksum_none_assert(skb);
4816 skb->ip_summed = CHECKSUM_UNNECESSARY;
4818 if (!stmmac_get_rx_hash(priv, p, &hash, &hash_type))
4819 skb_set_hash(skb, hash, hash_type);
4821 skb_record_rx_queue(skb, queue);
4822 napi_gro_receive(&ch->rxtx_napi, skb);
4824 priv->dev->stats.rx_packets++;
4825 priv->dev->stats.rx_bytes += len;
4828 static bool stmmac_rx_refill_zc(struct stmmac_priv *priv, u32 queue, u32 budget)
4830 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4831 unsigned int entry = rx_q->dirty_rx;
4832 struct dma_desc *rx_desc = NULL;
4835 budget = min(budget, stmmac_rx_dirty(priv, queue));
4837 while (budget-- > 0 && entry != rx_q->cur_rx) {
4838 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry];
4839 dma_addr_t dma_addr;
4843 buf->xdp = xsk_buff_alloc(rx_q->xsk_pool);
4850 if (priv->extend_desc)
4851 rx_desc = (struct dma_desc *)(rx_q->dma_erx + entry);
4853 rx_desc = rx_q->dma_rx + entry;
4855 dma_addr = xsk_buff_xdp_get_dma(buf->xdp);
4856 stmmac_set_desc_addr(priv, rx_desc, dma_addr);
4857 stmmac_set_desc_sec_addr(priv, rx_desc, 0, false);
4858 stmmac_refill_desc3(priv, rx_q, rx_desc);
4860 rx_q->rx_count_frames++;
4861 rx_q->rx_count_frames += priv->rx_coal_frames[queue];
4862 if (rx_q->rx_count_frames > priv->rx_coal_frames[queue])
4863 rx_q->rx_count_frames = 0;
4865 use_rx_wd = !priv->rx_coal_frames[queue];
4866 use_rx_wd |= rx_q->rx_count_frames > 0;
4867 if (!priv->use_riwt)
4871 stmmac_set_rx_owner(priv, rx_desc, use_rx_wd);
4873 entry = STMMAC_GET_ENTRY(entry, priv->dma_rx_size);
4877 rx_q->dirty_rx = entry;
4878 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
4879 (rx_q->dirty_rx * sizeof(struct dma_desc));
4880 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue);
4886 static int stmmac_rx_zc(struct stmmac_priv *priv, int limit, u32 queue)
4888 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4889 unsigned int count = 0, error = 0, len = 0;
4890 int dirty = stmmac_rx_dirty(priv, queue);
4891 unsigned int next_entry = rx_q->cur_rx;
4892 unsigned int desc_size;
4893 struct bpf_prog *prog;
4894 bool failure = false;
4898 if (netif_msg_rx_status(priv)) {
4901 netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
4902 if (priv->extend_desc) {
4903 rx_head = (void *)rx_q->dma_erx;
4904 desc_size = sizeof(struct dma_extended_desc);
4906 rx_head = (void *)rx_q->dma_rx;
4907 desc_size = sizeof(struct dma_desc);
4910 stmmac_display_ring(priv, rx_head, priv->dma_rx_size, true,
4911 rx_q->dma_rx_phy, desc_size);
4913 while (count < limit) {
4914 struct stmmac_rx_buffer *buf;
4915 unsigned int buf1_len = 0;
4916 struct dma_desc *np, *p;
4920 if (!count && rx_q->state_saved) {
4921 error = rx_q->state.error;
4922 len = rx_q->state.len;
4924 rx_q->state_saved = false;
4935 buf = &rx_q->buf_pool[entry];
4937 if (dirty >= STMMAC_RX_FILL_BATCH) {
4938 failure = failure ||
4939 !stmmac_rx_refill_zc(priv, queue, dirty);
4943 if (priv->extend_desc)
4944 p = (struct dma_desc *)(rx_q->dma_erx + entry);
4946 p = rx_q->dma_rx + entry;
4948 /* read the status of the incoming frame */
4949 status = stmmac_rx_status(priv, &priv->dev->stats,
4951 /* check if managed by the DMA otherwise go ahead */
4952 if (unlikely(status & dma_own))
4955 /* Prefetch the next RX descriptor */
4956 rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx,
4958 next_entry = rx_q->cur_rx;
4960 if (priv->extend_desc)
4961 np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
4963 np = rx_q->dma_rx + next_entry;
4967 /* Ensure a valid XSK buffer before proceed */
4971 if (priv->extend_desc)
4972 stmmac_rx_extended_status(priv, &priv->dev->stats,
4974 rx_q->dma_erx + entry);
4975 if (unlikely(status == discard_frame)) {
4976 xsk_buff_free(buf->xdp);
4980 if (!priv->hwts_rx_en)
4981 priv->dev->stats.rx_errors++;
4984 if (unlikely(error && (status & rx_not_ls)))
4986 if (unlikely(error)) {
4991 /* XSK pool expects RX frame 1:1 mapped to XSK buffer */
4992 if (likely(status & rx_not_ls)) {
4993 xsk_buff_free(buf->xdp);
5000 /* XDP ZC Frame only support primary buffers for now */
5001 buf1_len = stmmac_rx_buf1_len(priv, p, status, len);
5004 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
5005 * Type frames (LLC/LLC-SNAP)
5007 * llc_snap is never checked in GMAC >= 4, so this ACS
5008 * feature is always disabled and packets need to be
5009 * stripped manually.
5011 if (likely(!(status & rx_not_ls)) &&
5012 (likely(priv->synopsys_id >= DWMAC_CORE_4_00) ||
5013 unlikely(status != llc_snap))) {
5014 buf1_len -= ETH_FCS_LEN;
5018 /* RX buffer is good and fit into a XSK pool buffer */
5019 buf->xdp->data_end = buf->xdp->data + buf1_len;
5020 xsk_buff_dma_sync_for_cpu(buf->xdp, rx_q->xsk_pool);
5022 prog = READ_ONCE(priv->xdp_prog);
5023 res = __stmmac_xdp_run_prog(priv, prog, buf->xdp);
5026 case STMMAC_XDP_PASS:
5027 stmmac_dispatch_skb_zc(priv, queue, p, np, buf->xdp);
5028 xsk_buff_free(buf->xdp);
5030 case STMMAC_XDP_CONSUMED:
5031 xsk_buff_free(buf->xdp);
5032 priv->dev->stats.rx_dropped++;
5035 case STMMAC_XDP_REDIRECT:
5045 if (status & rx_not_ls) {
5046 rx_q->state_saved = true;
5047 rx_q->state.error = error;
5048 rx_q->state.len = len;
5051 stmmac_finalize_xdp_rx(priv, xdp_status);
5053 priv->xstats.rx_pkt_n += count;
5054 priv->xstats.rxq_stats[queue].rx_pkt_n += count;
5056 if (xsk_uses_need_wakeup(rx_q->xsk_pool)) {
5057 if (failure || stmmac_rx_dirty(priv, queue) > 0)
5058 xsk_set_rx_need_wakeup(rx_q->xsk_pool);
5060 xsk_clear_rx_need_wakeup(rx_q->xsk_pool);
5065 return failure ? limit : (int)count;
5069 * stmmac_rx - manage the receive process
5070 * @priv: driver private structure
5071 * @limit: napi bugget
5072 * @queue: RX queue index.
5073 * Description : this the function called by the napi poll method.
5074 * It gets all the frames inside the ring.
5076 static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
5078 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
5079 struct stmmac_channel *ch = &priv->channel[queue];
5080 unsigned int count = 0, error = 0, len = 0;
5081 int status = 0, coe = priv->hw->rx_csum;
5082 unsigned int next_entry = rx_q->cur_rx;
5083 enum dma_data_direction dma_dir;
5084 unsigned int desc_size;
5085 struct sk_buff *skb = NULL;
5086 struct xdp_buff xdp;
5090 dma_dir = page_pool_get_dma_dir(rx_q->page_pool);
5091 buf_sz = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE) * PAGE_SIZE;
5093 if (netif_msg_rx_status(priv)) {
5096 netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
5097 if (priv->extend_desc) {
5098 rx_head = (void *)rx_q->dma_erx;
5099 desc_size = sizeof(struct dma_extended_desc);
5101 rx_head = (void *)rx_q->dma_rx;
5102 desc_size = sizeof(struct dma_desc);
5105 stmmac_display_ring(priv, rx_head, priv->dma_rx_size, true,
5106 rx_q->dma_rx_phy, desc_size);
5108 while (count < limit) {
5109 unsigned int buf1_len = 0, buf2_len = 0;
5110 enum pkt_hash_types hash_type;
5111 struct stmmac_rx_buffer *buf;
5112 struct dma_desc *np, *p;
5116 if (!count && rx_q->state_saved) {
5117 skb = rx_q->state.skb;
5118 error = rx_q->state.error;
5119 len = rx_q->state.len;
5121 rx_q->state_saved = false;
5134 buf = &rx_q->buf_pool[entry];
5136 if (priv->extend_desc)
5137 p = (struct dma_desc *)(rx_q->dma_erx + entry);
5139 p = rx_q->dma_rx + entry;
5141 /* read the status of the incoming frame */
5142 status = stmmac_rx_status(priv, &priv->dev->stats,
5144 /* check if managed by the DMA otherwise go ahead */
5145 if (unlikely(status & dma_own))
5148 rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx,
5150 next_entry = rx_q->cur_rx;
5152 if (priv->extend_desc)
5153 np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
5155 np = rx_q->dma_rx + next_entry;
5159 if (priv->extend_desc)
5160 stmmac_rx_extended_status(priv, &priv->dev->stats,
5161 &priv->xstats, rx_q->dma_erx + entry);
5162 if (unlikely(status == discard_frame)) {
5163 page_pool_recycle_direct(rx_q->page_pool, buf->page);
5166 if (!priv->hwts_rx_en)
5167 priv->dev->stats.rx_errors++;
5170 if (unlikely(error && (status & rx_not_ls)))
5172 if (unlikely(error)) {
5179 /* Buffer is good. Go on. */
5181 prefetch(page_address(buf->page) + buf->page_offset);
5183 prefetch(page_address(buf->sec_page));
5185 buf1_len = stmmac_rx_buf1_len(priv, p, status, len);
5187 buf2_len = stmmac_rx_buf2_len(priv, p, status, len);
5190 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
5191 * Type frames (LLC/LLC-SNAP)
5193 * llc_snap is never checked in GMAC >= 4, so this ACS
5194 * feature is always disabled and packets need to be
5195 * stripped manually.
5197 if (likely(!(status & rx_not_ls)) &&
5198 (likely(priv->synopsys_id >= DWMAC_CORE_4_00) ||
5199 unlikely(status != llc_snap))) {
5201 buf2_len -= ETH_FCS_LEN;
5203 } else if (buf1_len) {
5204 buf1_len -= ETH_FCS_LEN;
5210 unsigned int pre_len, sync_len;
5212 dma_sync_single_for_cpu(priv->device, buf->addr,
5215 xdp_init_buff(&xdp, buf_sz, &rx_q->xdp_rxq);
5216 xdp_prepare_buff(&xdp, page_address(buf->page),
5217 buf->page_offset, buf1_len, false);
5219 pre_len = xdp.data_end - xdp.data_hard_start -
5221 skb = stmmac_xdp_run_prog(priv, &xdp);
5222 /* Due xdp_adjust_tail: DMA sync for_device
5223 * cover max len CPU touch
5225 sync_len = xdp.data_end - xdp.data_hard_start -
5227 sync_len = max(sync_len, pre_len);
5229 /* For Not XDP_PASS verdict */
5231 unsigned int xdp_res = -PTR_ERR(skb);
5233 if (xdp_res & STMMAC_XDP_CONSUMED) {
5234 page_pool_put_page(rx_q->page_pool,
5235 virt_to_head_page(xdp.data),
5238 priv->dev->stats.rx_dropped++;
5240 /* Clear skb as it was set as
5241 * status by XDP program.
5245 if (unlikely((status & rx_not_ls)))
5250 } else if (xdp_res & (STMMAC_XDP_TX |
5251 STMMAC_XDP_REDIRECT)) {
5252 xdp_status |= xdp_res;
5262 /* XDP program may expand or reduce tail */
5263 buf1_len = xdp.data_end - xdp.data;
5265 skb = napi_alloc_skb(&ch->rx_napi, buf1_len);
5267 priv->dev->stats.rx_dropped++;
5272 /* XDP program may adjust header */
5273 skb_copy_to_linear_data(skb, xdp.data, buf1_len);
5274 skb_put(skb, buf1_len);
5276 /* Data payload copied into SKB, page ready for recycle */
5277 page_pool_recycle_direct(rx_q->page_pool, buf->page);
5279 } else if (buf1_len) {
5280 dma_sync_single_for_cpu(priv->device, buf->addr,
5282 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
5283 buf->page, buf->page_offset, buf1_len,
5286 /* Data payload appended into SKB */
5287 page_pool_release_page(rx_q->page_pool, buf->page);
5292 dma_sync_single_for_cpu(priv->device, buf->sec_addr,
5294 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
5295 buf->sec_page, 0, buf2_len,
5298 /* Data payload appended into SKB */
5299 page_pool_release_page(rx_q->page_pool, buf->sec_page);
5300 buf->sec_page = NULL;
5304 if (likely(status & rx_not_ls))
5309 /* Got entire packet into SKB. Finish it. */
5311 stmmac_get_rx_hwtstamp(priv, p, np, skb);
5312 stmmac_rx_vlan(priv->dev, skb);
5313 skb->protocol = eth_type_trans(skb, priv->dev);
5316 skb_checksum_none_assert(skb);
5318 skb->ip_summed = CHECKSUM_UNNECESSARY;
5320 if (!stmmac_get_rx_hash(priv, p, &hash, &hash_type))
5321 skb_set_hash(skb, hash, hash_type);
5323 skb_record_rx_queue(skb, queue);
5324 napi_gro_receive(&ch->rx_napi, skb);
5327 priv->dev->stats.rx_packets++;
5328 priv->dev->stats.rx_bytes += len;
5332 if (status & rx_not_ls || skb) {
5333 rx_q->state_saved = true;
5334 rx_q->state.skb = skb;
5335 rx_q->state.error = error;
5336 rx_q->state.len = len;
5339 stmmac_finalize_xdp_rx(priv, xdp_status);
5341 stmmac_rx_refill(priv, queue);
5343 priv->xstats.rx_pkt_n += count;
5344 priv->xstats.rxq_stats[queue].rx_pkt_n += count;
5349 static int stmmac_napi_poll_rx(struct napi_struct *napi, int budget)
5351 struct stmmac_channel *ch =
5352 container_of(napi, struct stmmac_channel, rx_napi);
5353 struct stmmac_priv *priv = ch->priv_data;
5354 u32 chan = ch->index;
5357 priv->xstats.napi_poll++;
5359 work_done = stmmac_rx(priv, budget, chan);
5360 if (work_done < budget && napi_complete_done(napi, work_done)) {
5361 unsigned long flags;
5363 spin_lock_irqsave(&ch->lock, flags);
5364 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
5365 spin_unlock_irqrestore(&ch->lock, flags);
5371 static int stmmac_napi_poll_tx(struct napi_struct *napi, int budget)
5373 struct stmmac_channel *ch =
5374 container_of(napi, struct stmmac_channel, tx_napi);
5375 struct stmmac_priv *priv = ch->priv_data;
5376 u32 chan = ch->index;
5379 priv->xstats.napi_poll++;
5381 work_done = stmmac_tx_clean(priv, budget, chan);
5382 work_done = min(work_done, budget);
5384 if (work_done < budget && napi_complete_done(napi, work_done)) {
5385 unsigned long flags;
5387 spin_lock_irqsave(&ch->lock, flags);
5388 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
5389 spin_unlock_irqrestore(&ch->lock, flags);
5395 static int stmmac_napi_poll_rxtx(struct napi_struct *napi, int budget)
5397 struct stmmac_channel *ch =
5398 container_of(napi, struct stmmac_channel, rxtx_napi);
5399 struct stmmac_priv *priv = ch->priv_data;
5400 int rx_done, tx_done, rxtx_done;
5401 u32 chan = ch->index;
5403 priv->xstats.napi_poll++;
5405 tx_done = stmmac_tx_clean(priv, budget, chan);
5406 tx_done = min(tx_done, budget);
5408 rx_done = stmmac_rx_zc(priv, budget, chan);
5410 rxtx_done = max(tx_done, rx_done);
5412 /* If either TX or RX work is not complete, return budget
5415 if (rxtx_done >= budget)
5418 /* all work done, exit the polling mode */
5419 if (napi_complete_done(napi, rxtx_done)) {
5420 unsigned long flags;
5422 spin_lock_irqsave(&ch->lock, flags);
5423 /* Both RX and TX work done are compelte,
5424 * so enable both RX & TX IRQs.
5426 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 1);
5427 spin_unlock_irqrestore(&ch->lock, flags);
5430 return min(rxtx_done, budget - 1);
5435 * @dev : Pointer to net device structure
5436 * @txqueue: the index of the hanging transmit queue
5437 * Description: this function is called when a packet transmission fails to
5438 * complete within a reasonable time. The driver will mark the error in the
5439 * netdev structure and arrange for the device to be reset to a sane state
5440 * in order to transmit a new packet.
5442 static void stmmac_tx_timeout(struct net_device *dev, unsigned int txqueue)
5444 struct stmmac_priv *priv = netdev_priv(dev);
5446 stmmac_global_err(priv);
5450 * stmmac_set_rx_mode - entry point for multicast addressing
5451 * @dev : pointer to the device structure
5453 * This function is a driver entry point which gets called by the kernel
5454 * whenever multicast addresses must be enabled/disabled.
5458 static void stmmac_set_rx_mode(struct net_device *dev)
5460 struct stmmac_priv *priv = netdev_priv(dev);
5462 stmmac_set_filter(priv, priv->hw, dev);
5466 * stmmac_change_mtu - entry point to change MTU size for the device.
5467 * @dev : device pointer.
5468 * @new_mtu : the new MTU size for the device.
5469 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
5470 * to drive packet transmission. Ethernet has an MTU of 1500 octets
5471 * (ETH_DATA_LEN). This value can be changed with ifconfig.
5473 * 0 on success and an appropriate (-)ve integer as defined in errno.h
5476 static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
5478 struct stmmac_priv *priv = netdev_priv(dev);
5479 int txfifosz = priv->plat->tx_fifo_size;
5480 const int mtu = new_mtu;
5483 txfifosz = priv->dma_cap.tx_fifo_size;
5485 txfifosz /= priv->plat->tx_queues_to_use;
5487 if (netif_running(dev)) {
5488 netdev_err(priv->dev, "must be stopped to change its MTU\n");
5492 if (stmmac_xdp_is_enabled(priv) && new_mtu > ETH_DATA_LEN) {
5493 netdev_dbg(priv->dev, "Jumbo frames not supported for XDP\n");
5497 new_mtu = STMMAC_ALIGN(new_mtu);
5499 /* If condition true, FIFO is too small or MTU too large */
5500 if ((txfifosz < new_mtu) || (new_mtu > BUF_SIZE_16KiB))
5505 netdev_update_features(dev);
5510 static netdev_features_t stmmac_fix_features(struct net_device *dev,
5511 netdev_features_t features)
5513 struct stmmac_priv *priv = netdev_priv(dev);
5515 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
5516 features &= ~NETIF_F_RXCSUM;
5518 if (!priv->plat->tx_coe)
5519 features &= ~NETIF_F_CSUM_MASK;
5521 /* Some GMAC devices have a bugged Jumbo frame support that
5522 * needs to have the Tx COE disabled for oversized frames
5523 * (due to limited buffer sizes). In this case we disable
5524 * the TX csum insertion in the TDES and not use SF.
5526 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
5527 features &= ~NETIF_F_CSUM_MASK;
5529 /* Disable tso if asked by ethtool */
5530 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
5531 if (features & NETIF_F_TSO)
5540 static int stmmac_set_features(struct net_device *netdev,
5541 netdev_features_t features)
5543 struct stmmac_priv *priv = netdev_priv(netdev);
5545 /* Keep the COE Type in case of csum is supporting */
5546 if (features & NETIF_F_RXCSUM)
5547 priv->hw->rx_csum = priv->plat->rx_coe;
5549 priv->hw->rx_csum = 0;
5550 /* No check needed because rx_coe has been set before and it will be
5551 * fixed in case of issue.
5553 stmmac_rx_ipc(priv, priv->hw);
5555 if (priv->sph_cap) {
5556 bool sph_en = (priv->hw->rx_csum > 0) && priv->sph;
5559 for (chan = 0; chan < priv->plat->rx_queues_to_use; chan++)
5560 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
5566 static void stmmac_fpe_event_status(struct stmmac_priv *priv, int status)
5568 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
5569 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
5570 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
5571 bool *hs_enable = &fpe_cfg->hs_enable;
5573 if (status == FPE_EVENT_UNKNOWN || !*hs_enable)
5576 /* If LP has sent verify mPacket, LP is FPE capable */
5577 if ((status & FPE_EVENT_RVER) == FPE_EVENT_RVER) {
5578 if (*lp_state < FPE_STATE_CAPABLE)
5579 *lp_state = FPE_STATE_CAPABLE;
5581 /* If user has requested FPE enable, quickly response */
5583 stmmac_fpe_send_mpacket(priv, priv->ioaddr,
5587 /* If Local has sent verify mPacket, Local is FPE capable */
5588 if ((status & FPE_EVENT_TVER) == FPE_EVENT_TVER) {
5589 if (*lo_state < FPE_STATE_CAPABLE)
5590 *lo_state = FPE_STATE_CAPABLE;
5593 /* If LP has sent response mPacket, LP is entering FPE ON */
5594 if ((status & FPE_EVENT_RRSP) == FPE_EVENT_RRSP)
5595 *lp_state = FPE_STATE_ENTERING_ON;
5597 /* If Local has sent response mPacket, Local is entering FPE ON */
5598 if ((status & FPE_EVENT_TRSP) == FPE_EVENT_TRSP)
5599 *lo_state = FPE_STATE_ENTERING_ON;
5601 if (!test_bit(__FPE_REMOVING, &priv->fpe_task_state) &&
5602 !test_and_set_bit(__FPE_TASK_SCHED, &priv->fpe_task_state) &&
5604 queue_work(priv->fpe_wq, &priv->fpe_task);
5608 static void stmmac_common_interrupt(struct stmmac_priv *priv)
5610 u32 rx_cnt = priv->plat->rx_queues_to_use;
5611 u32 tx_cnt = priv->plat->tx_queues_to_use;
5616 xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
5617 queues_count = (rx_cnt > tx_cnt) ? rx_cnt : tx_cnt;
5620 pm_wakeup_event(priv->device, 0);
5622 if (priv->dma_cap.estsel)
5623 stmmac_est_irq_status(priv, priv->ioaddr, priv->dev,
5624 &priv->xstats, tx_cnt);
5626 if (priv->dma_cap.fpesel) {
5627 int status = stmmac_fpe_irq_status(priv, priv->ioaddr,
5630 stmmac_fpe_event_status(priv, status);
5633 /* To handle GMAC own interrupts */
5634 if ((priv->plat->has_gmac) || xmac) {
5635 int status = stmmac_host_irq_status(priv, priv->hw, &priv->xstats);
5637 if (unlikely(status)) {
5638 /* For LPI we need to save the tx status */
5639 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
5640 priv->tx_path_in_lpi_mode = true;
5641 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
5642 priv->tx_path_in_lpi_mode = false;
5645 for (queue = 0; queue < queues_count; queue++) {
5646 status = stmmac_host_mtl_irq_status(priv, priv->hw,
5650 /* PCS link status */
5651 if (priv->hw->pcs) {
5652 if (priv->xstats.pcs_link)
5653 netif_carrier_on(priv->dev);
5655 netif_carrier_off(priv->dev);
5658 stmmac_timestamp_interrupt(priv, priv);
5663 * stmmac_interrupt - main ISR
5664 * @irq: interrupt number.
5665 * @dev_id: to pass the net device pointer.
5666 * Description: this is the main driver interrupt service routine.
5668 * o DMA service routine (to manage incoming frame reception and transmission
5670 * o Core interrupts to manage: remote wake-up, management counter, LPI
5673 static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
5675 struct net_device *dev = (struct net_device *)dev_id;
5676 struct stmmac_priv *priv = netdev_priv(dev);
5678 /* Check if adapter is up */
5679 if (test_bit(STMMAC_DOWN, &priv->state))
5682 /* Check if a fatal error happened */
5683 if (stmmac_safety_feat_interrupt(priv))
5686 /* To handle Common interrupts */
5687 stmmac_common_interrupt(priv);
5689 /* To handle DMA interrupts */
5690 stmmac_dma_interrupt(priv);
5695 static irqreturn_t stmmac_mac_interrupt(int irq, void *dev_id)
5697 struct net_device *dev = (struct net_device *)dev_id;
5698 struct stmmac_priv *priv = netdev_priv(dev);
5700 if (unlikely(!dev)) {
5701 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5705 /* Check if adapter is up */
5706 if (test_bit(STMMAC_DOWN, &priv->state))
5709 /* To handle Common interrupts */
5710 stmmac_common_interrupt(priv);
5715 static irqreturn_t stmmac_safety_interrupt(int irq, void *dev_id)
5717 struct net_device *dev = (struct net_device *)dev_id;
5718 struct stmmac_priv *priv = netdev_priv(dev);
5720 if (unlikely(!dev)) {
5721 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5725 /* Check if adapter is up */
5726 if (test_bit(STMMAC_DOWN, &priv->state))
5729 /* Check if a fatal error happened */
5730 stmmac_safety_feat_interrupt(priv);
5735 static irqreturn_t stmmac_msi_intr_tx(int irq, void *data)
5737 struct stmmac_tx_queue *tx_q = (struct stmmac_tx_queue *)data;
5738 int chan = tx_q->queue_index;
5739 struct stmmac_priv *priv;
5742 priv = container_of(tx_q, struct stmmac_priv, tx_queue[chan]);
5744 if (unlikely(!data)) {
5745 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5749 /* Check if adapter is up */
5750 if (test_bit(STMMAC_DOWN, &priv->state))
5753 status = stmmac_napi_check(priv, chan, DMA_DIR_TX);
5755 if (unlikely(status & tx_hard_error_bump_tc)) {
5756 /* Try to bump up the dma threshold on this failure */
5757 stmmac_bump_dma_threshold(priv, chan);
5758 } else if (unlikely(status == tx_hard_error)) {
5759 stmmac_tx_err(priv, chan);
5765 static irqreturn_t stmmac_msi_intr_rx(int irq, void *data)
5767 struct stmmac_rx_queue *rx_q = (struct stmmac_rx_queue *)data;
5768 int chan = rx_q->queue_index;
5769 struct stmmac_priv *priv;
5771 priv = container_of(rx_q, struct stmmac_priv, rx_queue[chan]);
5773 if (unlikely(!data)) {
5774 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5778 /* Check if adapter is up */
5779 if (test_bit(STMMAC_DOWN, &priv->state))
5782 stmmac_napi_check(priv, chan, DMA_DIR_RX);
5787 #ifdef CONFIG_NET_POLL_CONTROLLER
5788 /* Polling receive - used by NETCONSOLE and other diagnostic tools
5789 * to allow network I/O with interrupts disabled.
5791 static void stmmac_poll_controller(struct net_device *dev)
5793 struct stmmac_priv *priv = netdev_priv(dev);
5796 /* If adapter is down, do nothing */
5797 if (test_bit(STMMAC_DOWN, &priv->state))
5800 if (priv->plat->multi_msi_en) {
5801 for (i = 0; i < priv->plat->rx_queues_to_use; i++)
5802 stmmac_msi_intr_rx(0, &priv->rx_queue[i]);
5804 for (i = 0; i < priv->plat->tx_queues_to_use; i++)
5805 stmmac_msi_intr_tx(0, &priv->tx_queue[i]);
5807 disable_irq(dev->irq);
5808 stmmac_interrupt(dev->irq, dev);
5809 enable_irq(dev->irq);
5815 * stmmac_ioctl - Entry point for the Ioctl
5816 * @dev: Device pointer.
5817 * @rq: An IOCTL specefic structure, that can contain a pointer to
5818 * a proprietary structure used to pass information to the driver.
5819 * @cmd: IOCTL command
5821 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
5823 static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
5825 struct stmmac_priv *priv = netdev_priv (dev);
5826 int ret = -EOPNOTSUPP;
5828 if (!netif_running(dev))
5835 ret = phylink_mii_ioctl(priv->phylink, rq, cmd);
5838 ret = stmmac_hwtstamp_set(dev, rq);
5841 ret = stmmac_hwtstamp_get(dev, rq);
5850 static int stmmac_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
5853 struct stmmac_priv *priv = cb_priv;
5854 int ret = -EOPNOTSUPP;
5856 if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
5859 __stmmac_disable_all_queues(priv);
5862 case TC_SETUP_CLSU32:
5863 ret = stmmac_tc_setup_cls_u32(priv, priv, type_data);
5865 case TC_SETUP_CLSFLOWER:
5866 ret = stmmac_tc_setup_cls(priv, priv, type_data);
5872 stmmac_enable_all_queues(priv);
5876 static LIST_HEAD(stmmac_block_cb_list);
5878 static int stmmac_setup_tc(struct net_device *ndev, enum tc_setup_type type,
5881 struct stmmac_priv *priv = netdev_priv(ndev);
5884 case TC_SETUP_BLOCK:
5885 return flow_block_cb_setup_simple(type_data,
5886 &stmmac_block_cb_list,
5887 stmmac_setup_tc_block_cb,
5889 case TC_SETUP_QDISC_CBS:
5890 return stmmac_tc_setup_cbs(priv, priv, type_data);
5891 case TC_SETUP_QDISC_TAPRIO:
5892 return stmmac_tc_setup_taprio(priv, priv, type_data);
5893 case TC_SETUP_QDISC_ETF:
5894 return stmmac_tc_setup_etf(priv, priv, type_data);
5900 static u16 stmmac_select_queue(struct net_device *dev, struct sk_buff *skb,
5901 struct net_device *sb_dev)
5903 int gso = skb_shinfo(skb)->gso_type;
5905 if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6 | SKB_GSO_UDP_L4)) {
5907 * There is no way to determine the number of TSO/USO
5908 * capable Queues. Let's use always the Queue 0
5909 * because if TSO/USO is supported then at least this
5910 * one will be capable.
5915 return netdev_pick_tx(dev, skb, NULL) % dev->real_num_tx_queues;
5918 static int stmmac_set_mac_address(struct net_device *ndev, void *addr)
5920 struct stmmac_priv *priv = netdev_priv(ndev);
5923 ret = pm_runtime_get_sync(priv->device);
5925 pm_runtime_put_noidle(priv->device);
5929 ret = eth_mac_addr(ndev, addr);
5933 stmmac_set_umac_addr(priv, priv->hw, ndev->dev_addr, 0);
5936 pm_runtime_put(priv->device);
5941 #ifdef CONFIG_DEBUG_FS
5942 static struct dentry *stmmac_fs_dir;
5944 static void sysfs_display_ring(void *head, int size, int extend_desc,
5945 struct seq_file *seq, dma_addr_t dma_phy_addr)
5948 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
5949 struct dma_desc *p = (struct dma_desc *)head;
5950 dma_addr_t dma_addr;
5952 for (i = 0; i < size; i++) {
5954 dma_addr = dma_phy_addr + i * sizeof(*ep);
5955 seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n",
5957 le32_to_cpu(ep->basic.des0),
5958 le32_to_cpu(ep->basic.des1),
5959 le32_to_cpu(ep->basic.des2),
5960 le32_to_cpu(ep->basic.des3));
5963 dma_addr = dma_phy_addr + i * sizeof(*p);
5964 seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n",
5966 le32_to_cpu(p->des0), le32_to_cpu(p->des1),
5967 le32_to_cpu(p->des2), le32_to_cpu(p->des3));
5970 seq_printf(seq, "\n");
5974 static int stmmac_rings_status_show(struct seq_file *seq, void *v)
5976 struct net_device *dev = seq->private;
5977 struct stmmac_priv *priv = netdev_priv(dev);
5978 u32 rx_count = priv->plat->rx_queues_to_use;
5979 u32 tx_count = priv->plat->tx_queues_to_use;
5982 if ((dev->flags & IFF_UP) == 0)
5985 for (queue = 0; queue < rx_count; queue++) {
5986 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
5988 seq_printf(seq, "RX Queue %d:\n", queue);
5990 if (priv->extend_desc) {
5991 seq_printf(seq, "Extended descriptor ring:\n");
5992 sysfs_display_ring((void *)rx_q->dma_erx,
5993 priv->dma_rx_size, 1, seq, rx_q->dma_rx_phy);
5995 seq_printf(seq, "Descriptor ring:\n");
5996 sysfs_display_ring((void *)rx_q->dma_rx,
5997 priv->dma_rx_size, 0, seq, rx_q->dma_rx_phy);
6001 for (queue = 0; queue < tx_count; queue++) {
6002 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
6004 seq_printf(seq, "TX Queue %d:\n", queue);
6006 if (priv->extend_desc) {
6007 seq_printf(seq, "Extended descriptor ring:\n");
6008 sysfs_display_ring((void *)tx_q->dma_etx,
6009 priv->dma_tx_size, 1, seq, tx_q->dma_tx_phy);
6010 } else if (!(tx_q->tbs & STMMAC_TBS_AVAIL)) {
6011 seq_printf(seq, "Descriptor ring:\n");
6012 sysfs_display_ring((void *)tx_q->dma_tx,
6013 priv->dma_tx_size, 0, seq, tx_q->dma_tx_phy);
6019 DEFINE_SHOW_ATTRIBUTE(stmmac_rings_status);
6021 static int stmmac_dma_cap_show(struct seq_file *seq, void *v)
6023 struct net_device *dev = seq->private;
6024 struct stmmac_priv *priv = netdev_priv(dev);
6026 if (!priv->hw_cap_support) {
6027 seq_printf(seq, "DMA HW features not supported\n");
6031 seq_printf(seq, "==============================\n");
6032 seq_printf(seq, "\tDMA HW features\n");
6033 seq_printf(seq, "==============================\n");
6035 seq_printf(seq, "\t10/100 Mbps: %s\n",
6036 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
6037 seq_printf(seq, "\t1000 Mbps: %s\n",
6038 (priv->dma_cap.mbps_1000) ? "Y" : "N");
6039 seq_printf(seq, "\tHalf duplex: %s\n",
6040 (priv->dma_cap.half_duplex) ? "Y" : "N");
6041 seq_printf(seq, "\tHash Filter: %s\n",
6042 (priv->dma_cap.hash_filter) ? "Y" : "N");
6043 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
6044 (priv->dma_cap.multi_addr) ? "Y" : "N");
6045 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfaces): %s\n",
6046 (priv->dma_cap.pcs) ? "Y" : "N");
6047 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
6048 (priv->dma_cap.sma_mdio) ? "Y" : "N");
6049 seq_printf(seq, "\tPMT Remote wake up: %s\n",
6050 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
6051 seq_printf(seq, "\tPMT Magic Frame: %s\n",
6052 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
6053 seq_printf(seq, "\tRMON module: %s\n",
6054 (priv->dma_cap.rmon) ? "Y" : "N");
6055 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
6056 (priv->dma_cap.time_stamp) ? "Y" : "N");
6057 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp: %s\n",
6058 (priv->dma_cap.atime_stamp) ? "Y" : "N");
6059 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE): %s\n",
6060 (priv->dma_cap.eee) ? "Y" : "N");
6061 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
6062 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
6063 (priv->dma_cap.tx_coe) ? "Y" : "N");
6064 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
6065 seq_printf(seq, "\tIP Checksum Offload in RX: %s\n",
6066 (priv->dma_cap.rx_coe) ? "Y" : "N");
6068 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
6069 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
6070 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
6071 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
6073 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
6074 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
6075 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
6076 priv->dma_cap.number_rx_channel);
6077 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
6078 priv->dma_cap.number_tx_channel);
6079 seq_printf(seq, "\tNumber of Additional RX queues: %d\n",
6080 priv->dma_cap.number_rx_queues);
6081 seq_printf(seq, "\tNumber of Additional TX queues: %d\n",
6082 priv->dma_cap.number_tx_queues);
6083 seq_printf(seq, "\tEnhanced descriptors: %s\n",
6084 (priv->dma_cap.enh_desc) ? "Y" : "N");
6085 seq_printf(seq, "\tTX Fifo Size: %d\n", priv->dma_cap.tx_fifo_size);
6086 seq_printf(seq, "\tRX Fifo Size: %d\n", priv->dma_cap.rx_fifo_size);
6087 seq_printf(seq, "\tHash Table Size: %d\n", priv->dma_cap.hash_tb_sz);
6088 seq_printf(seq, "\tTSO: %s\n", priv->dma_cap.tsoen ? "Y" : "N");
6089 seq_printf(seq, "\tNumber of PPS Outputs: %d\n",
6090 priv->dma_cap.pps_out_num);
6091 seq_printf(seq, "\tSafety Features: %s\n",
6092 priv->dma_cap.asp ? "Y" : "N");
6093 seq_printf(seq, "\tFlexible RX Parser: %s\n",
6094 priv->dma_cap.frpsel ? "Y" : "N");
6095 seq_printf(seq, "\tEnhanced Addressing: %d\n",
6096 priv->dma_cap.addr64);
6097 seq_printf(seq, "\tReceive Side Scaling: %s\n",
6098 priv->dma_cap.rssen ? "Y" : "N");
6099 seq_printf(seq, "\tVLAN Hash Filtering: %s\n",
6100 priv->dma_cap.vlhash ? "Y" : "N");
6101 seq_printf(seq, "\tSplit Header: %s\n",
6102 priv->dma_cap.sphen ? "Y" : "N");
6103 seq_printf(seq, "\tVLAN TX Insertion: %s\n",
6104 priv->dma_cap.vlins ? "Y" : "N");
6105 seq_printf(seq, "\tDouble VLAN: %s\n",
6106 priv->dma_cap.dvlan ? "Y" : "N");
6107 seq_printf(seq, "\tNumber of L3/L4 Filters: %d\n",
6108 priv->dma_cap.l3l4fnum);
6109 seq_printf(seq, "\tARP Offloading: %s\n",
6110 priv->dma_cap.arpoffsel ? "Y" : "N");
6111 seq_printf(seq, "\tEnhancements to Scheduled Traffic (EST): %s\n",
6112 priv->dma_cap.estsel ? "Y" : "N");
6113 seq_printf(seq, "\tFrame Preemption (FPE): %s\n",
6114 priv->dma_cap.fpesel ? "Y" : "N");
6115 seq_printf(seq, "\tTime-Based Scheduling (TBS): %s\n",
6116 priv->dma_cap.tbssel ? "Y" : "N");
6119 DEFINE_SHOW_ATTRIBUTE(stmmac_dma_cap);
6121 /* Use network device events to rename debugfs file entries.
6123 static int stmmac_device_event(struct notifier_block *unused,
6124 unsigned long event, void *ptr)
6126 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6127 struct stmmac_priv *priv = netdev_priv(dev);
6129 if (dev->netdev_ops != &stmmac_netdev_ops)
6133 case NETDEV_CHANGENAME:
6134 if (priv->dbgfs_dir)
6135 priv->dbgfs_dir = debugfs_rename(stmmac_fs_dir,
6145 static struct notifier_block stmmac_notifier = {
6146 .notifier_call = stmmac_device_event,
6149 static void stmmac_init_fs(struct net_device *dev)
6151 struct stmmac_priv *priv = netdev_priv(dev);
6155 /* Create per netdev entries */
6156 priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
6158 /* Entry to report DMA RX/TX rings */
6159 debugfs_create_file("descriptors_status", 0444, priv->dbgfs_dir, dev,
6160 &stmmac_rings_status_fops);
6162 /* Entry to report the DMA HW features */
6163 debugfs_create_file("dma_cap", 0444, priv->dbgfs_dir, dev,
6164 &stmmac_dma_cap_fops);
6169 static void stmmac_exit_fs(struct net_device *dev)
6171 struct stmmac_priv *priv = netdev_priv(dev);
6173 debugfs_remove_recursive(priv->dbgfs_dir);
6175 #endif /* CONFIG_DEBUG_FS */
6177 static u32 stmmac_vid_crc32_le(__le16 vid_le)
6179 unsigned char *data = (unsigned char *)&vid_le;
6180 unsigned char data_byte = 0;
6185 bits = get_bitmask_order(VLAN_VID_MASK);
6186 for (i = 0; i < bits; i++) {
6188 data_byte = data[i / 8];
6190 temp = ((crc & 1) ^ data_byte) & 1;
6201 static int stmmac_vlan_update(struct stmmac_priv *priv, bool is_double)
6208 for_each_set_bit(vid, priv->active_vlans, VLAN_N_VID) {
6209 __le16 vid_le = cpu_to_le16(vid);
6210 crc = bitrev32(~stmmac_vid_crc32_le(vid_le)) >> 28;
6215 if (!priv->dma_cap.vlhash) {
6216 if (count > 2) /* VID = 0 always passes filter */
6219 pmatch = cpu_to_le16(vid);
6223 return stmmac_update_vlan_hash(priv, priv->hw, hash, pmatch, is_double);
6226 static int stmmac_vlan_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid)
6228 struct stmmac_priv *priv = netdev_priv(ndev);
6229 bool is_double = false;
6232 if (be16_to_cpu(proto) == ETH_P_8021AD)
6235 set_bit(vid, priv->active_vlans);
6236 ret = stmmac_vlan_update(priv, is_double);
6238 clear_bit(vid, priv->active_vlans);
6242 if (priv->hw->num_vlan) {
6243 ret = stmmac_add_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid);
6251 static int stmmac_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid)
6253 struct stmmac_priv *priv = netdev_priv(ndev);
6254 bool is_double = false;
6257 ret = pm_runtime_get_sync(priv->device);
6259 pm_runtime_put_noidle(priv->device);
6263 if (be16_to_cpu(proto) == ETH_P_8021AD)
6266 clear_bit(vid, priv->active_vlans);
6268 if (priv->hw->num_vlan) {
6269 ret = stmmac_del_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid);
6271 goto del_vlan_error;
6274 ret = stmmac_vlan_update(priv, is_double);
6277 pm_runtime_put(priv->device);
6282 static int stmmac_bpf(struct net_device *dev, struct netdev_bpf *bpf)
6284 struct stmmac_priv *priv = netdev_priv(dev);
6286 switch (bpf->command) {
6287 case XDP_SETUP_PROG:
6288 return stmmac_xdp_set_prog(priv, bpf->prog, bpf->extack);
6289 case XDP_SETUP_XSK_POOL:
6290 return stmmac_xdp_setup_pool(priv, bpf->xsk.pool,
6297 static int stmmac_xdp_xmit(struct net_device *dev, int num_frames,
6298 struct xdp_frame **frames, u32 flags)
6300 struct stmmac_priv *priv = netdev_priv(dev);
6301 int cpu = smp_processor_id();
6302 struct netdev_queue *nq;
6306 if (unlikely(test_bit(STMMAC_DOWN, &priv->state)))
6309 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
6312 queue = stmmac_xdp_get_tx_queue(priv, cpu);
6313 nq = netdev_get_tx_queue(priv->dev, queue);
6315 __netif_tx_lock(nq, cpu);
6316 /* Avoids TX time-out as we are sharing with slow path */
6317 txq_trans_cond_update(nq);
6319 for (i = 0; i < num_frames; i++) {
6322 res = stmmac_xdp_xmit_xdpf(priv, queue, frames[i], true);
6323 if (res == STMMAC_XDP_CONSUMED)
6329 if (flags & XDP_XMIT_FLUSH) {
6330 stmmac_flush_tx_descriptors(priv, queue);
6331 stmmac_tx_timer_arm(priv, queue);
6334 __netif_tx_unlock(nq);
6339 void stmmac_disable_rx_queue(struct stmmac_priv *priv, u32 queue)
6341 struct stmmac_channel *ch = &priv->channel[queue];
6342 unsigned long flags;
6344 spin_lock_irqsave(&ch->lock, flags);
6345 stmmac_disable_dma_irq(priv, priv->ioaddr, queue, 1, 0);
6346 spin_unlock_irqrestore(&ch->lock, flags);
6348 stmmac_stop_rx_dma(priv, queue);
6349 __free_dma_rx_desc_resources(priv, queue);
6352 void stmmac_enable_rx_queue(struct stmmac_priv *priv, u32 queue)
6354 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
6355 struct stmmac_channel *ch = &priv->channel[queue];
6356 unsigned long flags;
6360 ret = __alloc_dma_rx_desc_resources(priv, queue);
6362 netdev_err(priv->dev, "Failed to alloc RX desc.\n");
6366 ret = __init_dma_rx_desc_rings(priv, queue, GFP_KERNEL);
6368 __free_dma_rx_desc_resources(priv, queue);
6369 netdev_err(priv->dev, "Failed to init RX desc.\n");
6373 stmmac_clear_rx_descriptors(priv, queue);
6375 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6376 rx_q->dma_rx_phy, rx_q->queue_index);
6378 rx_q->rx_tail_addr = rx_q->dma_rx_phy + (rx_q->buf_alloc_num *
6379 sizeof(struct dma_desc));
6380 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
6381 rx_q->rx_tail_addr, rx_q->queue_index);
6383 if (rx_q->xsk_pool && rx_q->buf_alloc_num) {
6384 buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool);
6385 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6389 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6394 stmmac_start_rx_dma(priv, queue);
6396 spin_lock_irqsave(&ch->lock, flags);
6397 stmmac_enable_dma_irq(priv, priv->ioaddr, queue, 1, 0);
6398 spin_unlock_irqrestore(&ch->lock, flags);
6401 void stmmac_disable_tx_queue(struct stmmac_priv *priv, u32 queue)
6403 struct stmmac_channel *ch = &priv->channel[queue];
6404 unsigned long flags;
6406 spin_lock_irqsave(&ch->lock, flags);
6407 stmmac_disable_dma_irq(priv, priv->ioaddr, queue, 0, 1);
6408 spin_unlock_irqrestore(&ch->lock, flags);
6410 stmmac_stop_tx_dma(priv, queue);
6411 __free_dma_tx_desc_resources(priv, queue);
6414 void stmmac_enable_tx_queue(struct stmmac_priv *priv, u32 queue)
6416 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
6417 struct stmmac_channel *ch = &priv->channel[queue];
6418 unsigned long flags;
6421 ret = __alloc_dma_tx_desc_resources(priv, queue);
6423 netdev_err(priv->dev, "Failed to alloc TX desc.\n");
6427 ret = __init_dma_tx_desc_rings(priv, queue);
6429 __free_dma_tx_desc_resources(priv, queue);
6430 netdev_err(priv->dev, "Failed to init TX desc.\n");
6434 stmmac_clear_tx_descriptors(priv, queue);
6436 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6437 tx_q->dma_tx_phy, tx_q->queue_index);
6439 if (tx_q->tbs & STMMAC_TBS_AVAIL)
6440 stmmac_enable_tbs(priv, priv->ioaddr, 1, tx_q->queue_index);
6442 tx_q->tx_tail_addr = tx_q->dma_tx_phy;
6443 stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
6444 tx_q->tx_tail_addr, tx_q->queue_index);
6446 stmmac_start_tx_dma(priv, queue);
6448 spin_lock_irqsave(&ch->lock, flags);
6449 stmmac_enable_dma_irq(priv, priv->ioaddr, queue, 0, 1);
6450 spin_unlock_irqrestore(&ch->lock, flags);
6453 void stmmac_xdp_release(struct net_device *dev)
6455 struct stmmac_priv *priv = netdev_priv(dev);
6458 /* Disable NAPI process */
6459 stmmac_disable_all_queues(priv);
6461 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
6462 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
6464 /* Free the IRQ lines */
6465 stmmac_free_irq(dev, REQ_IRQ_ERR_ALL, 0);
6467 /* Stop TX/RX DMA channels */
6468 stmmac_stop_all_dma(priv);
6470 /* Release and free the Rx/Tx resources */
6471 free_dma_desc_resources(priv);
6473 /* Disable the MAC Rx/Tx */
6474 stmmac_mac_set(priv, priv->ioaddr, false);
6476 /* set trans_start so we don't get spurious
6477 * watchdogs during reset
6479 netif_trans_update(dev);
6480 netif_carrier_off(dev);
6483 int stmmac_xdp_open(struct net_device *dev)
6485 struct stmmac_priv *priv = netdev_priv(dev);
6486 u32 rx_cnt = priv->plat->rx_queues_to_use;
6487 u32 tx_cnt = priv->plat->tx_queues_to_use;
6488 u32 dma_csr_ch = max(rx_cnt, tx_cnt);
6489 struct stmmac_rx_queue *rx_q;
6490 struct stmmac_tx_queue *tx_q;
6496 ret = alloc_dma_desc_resources(priv);
6498 netdev_err(dev, "%s: DMA descriptors allocation failed\n",
6500 goto dma_desc_error;
6503 ret = init_dma_desc_rings(dev, GFP_KERNEL);
6505 netdev_err(dev, "%s: DMA descriptors initialization failed\n",
6510 /* DMA CSR Channel configuration */
6511 for (chan = 0; chan < dma_csr_ch; chan++)
6512 stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan);
6514 /* Adjust Split header */
6515 sph_en = (priv->hw->rx_csum > 0) && priv->sph;
6517 /* DMA RX Channel Configuration */
6518 for (chan = 0; chan < rx_cnt; chan++) {
6519 rx_q = &priv->rx_queue[chan];
6521 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6522 rx_q->dma_rx_phy, chan);
6524 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
6525 (rx_q->buf_alloc_num *
6526 sizeof(struct dma_desc));
6527 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
6528 rx_q->rx_tail_addr, chan);
6530 if (rx_q->xsk_pool && rx_q->buf_alloc_num) {
6531 buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool);
6532 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6536 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6541 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
6544 /* DMA TX Channel Configuration */
6545 for (chan = 0; chan < tx_cnt; chan++) {
6546 tx_q = &priv->tx_queue[chan];
6548 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6549 tx_q->dma_tx_phy, chan);
6551 tx_q->tx_tail_addr = tx_q->dma_tx_phy;
6552 stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
6553 tx_q->tx_tail_addr, chan);
6555 hrtimer_init(&tx_q->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6556 tx_q->txtimer.function = stmmac_tx_timer;
6559 /* Enable the MAC Rx/Tx */
6560 stmmac_mac_set(priv, priv->ioaddr, true);
6562 /* Start Rx & Tx DMA Channels */
6563 stmmac_start_all_dma(priv);
6565 ret = stmmac_request_irq(dev);
6569 /* Enable NAPI process*/
6570 stmmac_enable_all_queues(priv);
6571 netif_carrier_on(dev);
6572 netif_tx_start_all_queues(dev);
6577 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
6578 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
6580 stmmac_hw_teardown(dev);
6582 free_dma_desc_resources(priv);
6587 int stmmac_xsk_wakeup(struct net_device *dev, u32 queue, u32 flags)
6589 struct stmmac_priv *priv = netdev_priv(dev);
6590 struct stmmac_rx_queue *rx_q;
6591 struct stmmac_tx_queue *tx_q;
6592 struct stmmac_channel *ch;
6594 if (test_bit(STMMAC_DOWN, &priv->state) ||
6595 !netif_carrier_ok(priv->dev))
6598 if (!stmmac_xdp_is_enabled(priv))
6601 if (queue >= priv->plat->rx_queues_to_use ||
6602 queue >= priv->plat->tx_queues_to_use)
6605 rx_q = &priv->rx_queue[queue];
6606 tx_q = &priv->tx_queue[queue];
6607 ch = &priv->channel[queue];
6609 if (!rx_q->xsk_pool && !tx_q->xsk_pool)
6612 if (!napi_if_scheduled_mark_missed(&ch->rxtx_napi)) {
6613 /* EQoS does not have per-DMA channel SW interrupt,
6614 * so we schedule RX Napi straight-away.
6616 if (likely(napi_schedule_prep(&ch->rxtx_napi)))
6617 __napi_schedule(&ch->rxtx_napi);
6623 static const struct net_device_ops stmmac_netdev_ops = {
6624 .ndo_open = stmmac_open,
6625 .ndo_start_xmit = stmmac_xmit,
6626 .ndo_stop = stmmac_release,
6627 .ndo_change_mtu = stmmac_change_mtu,
6628 .ndo_fix_features = stmmac_fix_features,
6629 .ndo_set_features = stmmac_set_features,
6630 .ndo_set_rx_mode = stmmac_set_rx_mode,
6631 .ndo_tx_timeout = stmmac_tx_timeout,
6632 .ndo_eth_ioctl = stmmac_ioctl,
6633 .ndo_setup_tc = stmmac_setup_tc,
6634 .ndo_select_queue = stmmac_select_queue,
6635 #ifdef CONFIG_NET_POLL_CONTROLLER
6636 .ndo_poll_controller = stmmac_poll_controller,
6638 .ndo_set_mac_address = stmmac_set_mac_address,
6639 .ndo_vlan_rx_add_vid = stmmac_vlan_rx_add_vid,
6640 .ndo_vlan_rx_kill_vid = stmmac_vlan_rx_kill_vid,
6641 .ndo_bpf = stmmac_bpf,
6642 .ndo_xdp_xmit = stmmac_xdp_xmit,
6643 .ndo_xsk_wakeup = stmmac_xsk_wakeup,
6646 static void stmmac_reset_subtask(struct stmmac_priv *priv)
6648 if (!test_and_clear_bit(STMMAC_RESET_REQUESTED, &priv->state))
6650 if (test_bit(STMMAC_DOWN, &priv->state))
6653 netdev_err(priv->dev, "Reset adapter.\n");
6656 netif_trans_update(priv->dev);
6657 while (test_and_set_bit(STMMAC_RESETING, &priv->state))
6658 usleep_range(1000, 2000);
6660 set_bit(STMMAC_DOWN, &priv->state);
6661 dev_close(priv->dev);
6662 dev_open(priv->dev, NULL);
6663 clear_bit(STMMAC_DOWN, &priv->state);
6664 clear_bit(STMMAC_RESETING, &priv->state);
6668 static void stmmac_service_task(struct work_struct *work)
6670 struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
6673 stmmac_reset_subtask(priv);
6674 clear_bit(STMMAC_SERVICE_SCHED, &priv->state);
6678 * stmmac_hw_init - Init the MAC device
6679 * @priv: driver private structure
6680 * Description: this function is to configure the MAC device according to
6681 * some platform parameters or the HW capability register. It prepares the
6682 * driver to use either ring or chain modes and to setup either enhanced or
6683 * normal descriptors.
6685 static int stmmac_hw_init(struct stmmac_priv *priv)
6689 /* dwmac-sun8i only work in chain mode */
6690 if (priv->plat->has_sun8i)
6692 priv->chain_mode = chain_mode;
6694 /* Initialize HW Interface */
6695 ret = stmmac_hwif_init(priv);
6699 /* Get the HW capability (new GMAC newer than 3.50a) */
6700 priv->hw_cap_support = stmmac_get_hw_features(priv);
6701 if (priv->hw_cap_support) {
6702 dev_info(priv->device, "DMA HW capability register supported\n");
6704 /* We can override some gmac/dma configuration fields: e.g.
6705 * enh_desc, tx_coe (e.g. that are passed through the
6706 * platform) with the values from the HW capability
6707 * register (if supported).
6709 priv->plat->enh_desc = priv->dma_cap.enh_desc;
6710 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up &&
6711 !priv->plat->use_phy_wol;
6712 priv->hw->pmt = priv->plat->pmt;
6713 if (priv->dma_cap.hash_tb_sz) {
6714 priv->hw->multicast_filter_bins =
6715 (BIT(priv->dma_cap.hash_tb_sz) << 5);
6716 priv->hw->mcast_bits_log2 =
6717 ilog2(priv->hw->multicast_filter_bins);
6720 /* TXCOE doesn't work in thresh DMA mode */
6721 if (priv->plat->force_thresh_dma_mode)
6722 priv->plat->tx_coe = 0;
6724 priv->plat->tx_coe = priv->dma_cap.tx_coe;
6726 /* In case of GMAC4 rx_coe is from HW cap register. */
6727 priv->plat->rx_coe = priv->dma_cap.rx_coe;
6729 if (priv->dma_cap.rx_coe_type2)
6730 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
6731 else if (priv->dma_cap.rx_coe_type1)
6732 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
6735 dev_info(priv->device, "No HW DMA feature register supported\n");
6738 if (priv->plat->rx_coe) {
6739 priv->hw->rx_csum = priv->plat->rx_coe;
6740 dev_info(priv->device, "RX Checksum Offload Engine supported\n");
6741 if (priv->synopsys_id < DWMAC_CORE_4_00)
6742 dev_info(priv->device, "COE Type %d\n", priv->hw->rx_csum);
6744 if (priv->plat->tx_coe)
6745 dev_info(priv->device, "TX Checksum insertion supported\n");
6747 if (priv->plat->pmt) {
6748 dev_info(priv->device, "Wake-Up On Lan supported\n");
6749 device_set_wakeup_capable(priv->device, 1);
6752 if (priv->dma_cap.tsoen)
6753 dev_info(priv->device, "TSO supported\n");
6755 priv->hw->vlan_fail_q_en = priv->plat->vlan_fail_q_en;
6756 priv->hw->vlan_fail_q = priv->plat->vlan_fail_q;
6758 /* Run HW quirks, if any */
6759 if (priv->hwif_quirks) {
6760 ret = priv->hwif_quirks(priv);
6765 /* Rx Watchdog is available in the COREs newer than the 3.40.
6766 * In some case, for example on bugged HW this feature
6767 * has to be disable and this can be done by passing the
6768 * riwt_off field from the platform.
6770 if (((priv->synopsys_id >= DWMAC_CORE_3_50) ||
6771 (priv->plat->has_xgmac)) && (!priv->plat->riwt_off)) {
6773 dev_info(priv->device,
6774 "Enable RX Mitigation via HW Watchdog Timer\n");
6780 static void stmmac_napi_add(struct net_device *dev)
6782 struct stmmac_priv *priv = netdev_priv(dev);
6785 maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
6787 for (queue = 0; queue < maxq; queue++) {
6788 struct stmmac_channel *ch = &priv->channel[queue];
6790 ch->priv_data = priv;
6792 spin_lock_init(&ch->lock);
6794 if (queue < priv->plat->rx_queues_to_use) {
6795 netif_napi_add(dev, &ch->rx_napi, stmmac_napi_poll_rx,
6798 if (queue < priv->plat->tx_queues_to_use) {
6799 netif_tx_napi_add(dev, &ch->tx_napi,
6800 stmmac_napi_poll_tx,
6803 if (queue < priv->plat->rx_queues_to_use &&
6804 queue < priv->plat->tx_queues_to_use) {
6805 netif_napi_add(dev, &ch->rxtx_napi,
6806 stmmac_napi_poll_rxtx,
6812 static void stmmac_napi_del(struct net_device *dev)
6814 struct stmmac_priv *priv = netdev_priv(dev);
6817 maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
6819 for (queue = 0; queue < maxq; queue++) {
6820 struct stmmac_channel *ch = &priv->channel[queue];
6822 if (queue < priv->plat->rx_queues_to_use)
6823 netif_napi_del(&ch->rx_napi);
6824 if (queue < priv->plat->tx_queues_to_use)
6825 netif_napi_del(&ch->tx_napi);
6826 if (queue < priv->plat->rx_queues_to_use &&
6827 queue < priv->plat->tx_queues_to_use) {
6828 netif_napi_del(&ch->rxtx_napi);
6833 int stmmac_reinit_queues(struct net_device *dev, u32 rx_cnt, u32 tx_cnt)
6835 struct stmmac_priv *priv = netdev_priv(dev);
6838 if (netif_running(dev))
6839 stmmac_release(dev);
6841 stmmac_napi_del(dev);
6843 priv->plat->rx_queues_to_use = rx_cnt;
6844 priv->plat->tx_queues_to_use = tx_cnt;
6846 stmmac_napi_add(dev);
6848 if (netif_running(dev))
6849 ret = stmmac_open(dev);
6854 int stmmac_reinit_ringparam(struct net_device *dev, u32 rx_size, u32 tx_size)
6856 struct stmmac_priv *priv = netdev_priv(dev);
6859 if (netif_running(dev))
6860 stmmac_release(dev);
6862 priv->dma_rx_size = rx_size;
6863 priv->dma_tx_size = tx_size;
6865 if (netif_running(dev))
6866 ret = stmmac_open(dev);
6871 #define SEND_VERIFY_MPAKCET_FMT "Send Verify mPacket lo_state=%d lp_state=%d\n"
6872 static void stmmac_fpe_lp_task(struct work_struct *work)
6874 struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
6876 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
6877 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
6878 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
6879 bool *hs_enable = &fpe_cfg->hs_enable;
6880 bool *enable = &fpe_cfg->enable;
6883 while (retries-- > 0) {
6884 /* Bail out immediately if FPE handshake is OFF */
6885 if (*lo_state == FPE_STATE_OFF || !*hs_enable)
6888 if (*lo_state == FPE_STATE_ENTERING_ON &&
6889 *lp_state == FPE_STATE_ENTERING_ON) {
6890 stmmac_fpe_configure(priv, priv->ioaddr,
6891 priv->plat->tx_queues_to_use,
6892 priv->plat->rx_queues_to_use,
6895 netdev_info(priv->dev, "configured FPE\n");
6897 *lo_state = FPE_STATE_ON;
6898 *lp_state = FPE_STATE_ON;
6899 netdev_info(priv->dev, "!!! BOTH FPE stations ON\n");
6903 if ((*lo_state == FPE_STATE_CAPABLE ||
6904 *lo_state == FPE_STATE_ENTERING_ON) &&
6905 *lp_state != FPE_STATE_ON) {
6906 netdev_info(priv->dev, SEND_VERIFY_MPAKCET_FMT,
6907 *lo_state, *lp_state);
6908 stmmac_fpe_send_mpacket(priv, priv->ioaddr,
6911 /* Sleep then retry */
6915 clear_bit(__FPE_TASK_SCHED, &priv->fpe_task_state);
6918 void stmmac_fpe_handshake(struct stmmac_priv *priv, bool enable)
6920 if (priv->plat->fpe_cfg->hs_enable != enable) {
6922 stmmac_fpe_send_mpacket(priv, priv->ioaddr,
6925 priv->plat->fpe_cfg->lo_fpe_state = FPE_STATE_OFF;
6926 priv->plat->fpe_cfg->lp_fpe_state = FPE_STATE_OFF;
6929 priv->plat->fpe_cfg->hs_enable = enable;
6935 * @device: device pointer
6936 * @plat_dat: platform data pointer
6937 * @res: stmmac resource pointer
6938 * Description: this is the main probe function used to
6939 * call the alloc_etherdev, allocate the priv structure.
6941 * returns 0 on success, otherwise errno.
6943 int stmmac_dvr_probe(struct device *device,
6944 struct plat_stmmacenet_data *plat_dat,
6945 struct stmmac_resources *res)
6947 struct net_device *ndev = NULL;
6948 struct stmmac_priv *priv;
6952 ndev = devm_alloc_etherdev_mqs(device, sizeof(struct stmmac_priv),
6953 MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES);
6957 SET_NETDEV_DEV(ndev, device);
6959 priv = netdev_priv(ndev);
6960 priv->device = device;
6963 stmmac_set_ethtool_ops(ndev);
6964 priv->pause = pause;
6965 priv->plat = plat_dat;
6966 priv->ioaddr = res->addr;
6967 priv->dev->base_addr = (unsigned long)res->addr;
6968 priv->plat->dma_cfg->multi_msi_en = priv->plat->multi_msi_en;
6970 priv->dev->irq = res->irq;
6971 priv->wol_irq = res->wol_irq;
6972 priv->lpi_irq = res->lpi_irq;
6973 priv->sfty_ce_irq = res->sfty_ce_irq;
6974 priv->sfty_ue_irq = res->sfty_ue_irq;
6975 for (i = 0; i < MTL_MAX_RX_QUEUES; i++)
6976 priv->rx_irq[i] = res->rx_irq[i];
6977 for (i = 0; i < MTL_MAX_TX_QUEUES; i++)
6978 priv->tx_irq[i] = res->tx_irq[i];
6980 if (!is_zero_ether_addr(res->mac))
6981 eth_hw_addr_set(priv->dev, res->mac);
6983 dev_set_drvdata(device, priv->dev);
6985 /* Verify driver arguments */
6986 stmmac_verify_args();
6988 priv->af_xdp_zc_qps = bitmap_zalloc(MTL_MAX_TX_QUEUES, GFP_KERNEL);
6989 if (!priv->af_xdp_zc_qps)
6992 /* Allocate workqueue */
6993 priv->wq = create_singlethread_workqueue("stmmac_wq");
6995 dev_err(priv->device, "failed to create workqueue\n");
6999 INIT_WORK(&priv->service_task, stmmac_service_task);
7001 /* Initialize Link Partner FPE workqueue */
7002 INIT_WORK(&priv->fpe_task, stmmac_fpe_lp_task);
7004 /* Override with kernel parameters if supplied XXX CRS XXX
7005 * this needs to have multiple instances
7007 if ((phyaddr >= 0) && (phyaddr <= 31))
7008 priv->plat->phy_addr = phyaddr;
7010 if (priv->plat->stmmac_rst) {
7011 ret = reset_control_assert(priv->plat->stmmac_rst);
7012 reset_control_deassert(priv->plat->stmmac_rst);
7013 /* Some reset controllers have only reset callback instead of
7014 * assert + deassert callbacks pair.
7016 if (ret == -ENOTSUPP)
7017 reset_control_reset(priv->plat->stmmac_rst);
7020 ret = reset_control_deassert(priv->plat->stmmac_ahb_rst);
7021 if (ret == -ENOTSUPP)
7022 dev_err(priv->device, "unable to bring out of ahb reset: %pe\n",
7025 /* Init MAC and get the capabilities */
7026 ret = stmmac_hw_init(priv);
7030 /* Only DWMAC core version 5.20 onwards supports HW descriptor prefetch.
7032 if (priv->synopsys_id < DWMAC_CORE_5_20)
7033 priv->plat->dma_cfg->dche = false;
7035 stmmac_check_ether_addr(priv);
7037 ndev->netdev_ops = &stmmac_netdev_ops;
7039 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
7042 ret = stmmac_tc_init(priv, priv);
7044 ndev->hw_features |= NETIF_F_HW_TC;
7047 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
7048 ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
7049 if (priv->plat->has_gmac4)
7050 ndev->hw_features |= NETIF_F_GSO_UDP_L4;
7052 dev_info(priv->device, "TSO feature enabled\n");
7055 if (priv->dma_cap.sphen) {
7056 ndev->hw_features |= NETIF_F_GRO;
7057 priv->sph_cap = true;
7058 priv->sph = priv->sph_cap;
7059 dev_info(priv->device, "SPH feature enabled\n");
7062 /* The current IP register MAC_HW_Feature1[ADDR64] only define
7063 * 32/40/64 bit width, but some SOC support others like i.MX8MP
7064 * support 34 bits but it map to 40 bits width in MAC_HW_Feature1[ADDR64].
7065 * So overwrite dma_cap.addr64 according to HW real design.
7067 if (priv->plat->addr64)
7068 priv->dma_cap.addr64 = priv->plat->addr64;
7070 if (priv->dma_cap.addr64) {
7071 ret = dma_set_mask_and_coherent(device,
7072 DMA_BIT_MASK(priv->dma_cap.addr64));
7074 dev_info(priv->device, "Using %d bits DMA width\n",
7075 priv->dma_cap.addr64);
7078 * If more than 32 bits can be addressed, make sure to
7079 * enable enhanced addressing mode.
7081 if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
7082 priv->plat->dma_cfg->eame = true;
7084 ret = dma_set_mask_and_coherent(device, DMA_BIT_MASK(32));
7086 dev_err(priv->device, "Failed to set DMA Mask\n");
7090 priv->dma_cap.addr64 = 32;
7094 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
7095 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
7096 #ifdef STMMAC_VLAN_TAG_USED
7097 /* Both mac100 and gmac support receive VLAN tag detection */
7098 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX;
7099 if (priv->dma_cap.vlhash) {
7100 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
7101 ndev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
7103 if (priv->dma_cap.vlins) {
7104 ndev->features |= NETIF_F_HW_VLAN_CTAG_TX;
7105 if (priv->dma_cap.dvlan)
7106 ndev->features |= NETIF_F_HW_VLAN_STAG_TX;
7109 priv->msg_enable = netif_msg_init(debug, default_msg_level);
7111 /* Initialize RSS */
7112 rxq = priv->plat->rx_queues_to_use;
7113 netdev_rss_key_fill(priv->rss.key, sizeof(priv->rss.key));
7114 for (i = 0; i < ARRAY_SIZE(priv->rss.table); i++)
7115 priv->rss.table[i] = ethtool_rxfh_indir_default(i, rxq);
7117 if (priv->dma_cap.rssen && priv->plat->rss_en)
7118 ndev->features |= NETIF_F_RXHASH;
7120 /* MTU range: 46 - hw-specific max */
7121 ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
7122 if (priv->plat->has_xgmac)
7123 ndev->max_mtu = XGMAC_JUMBO_LEN;
7124 else if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
7125 ndev->max_mtu = JUMBO_LEN;
7127 ndev->max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
7128 /* Will not overwrite ndev->max_mtu if plat->maxmtu > ndev->max_mtu
7129 * as well as plat->maxmtu < ndev->min_mtu which is a invalid range.
7131 if ((priv->plat->maxmtu < ndev->max_mtu) &&
7132 (priv->plat->maxmtu >= ndev->min_mtu))
7133 ndev->max_mtu = priv->plat->maxmtu;
7134 else if (priv->plat->maxmtu < ndev->min_mtu)
7135 dev_warn(priv->device,
7136 "%s: warning: maxmtu having invalid value (%d)\n",
7137 __func__, priv->plat->maxmtu);
7140 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
7142 /* Setup channels NAPI */
7143 stmmac_napi_add(ndev);
7145 mutex_init(&priv->lock);
7147 /* If a specific clk_csr value is passed from the platform
7148 * this means that the CSR Clock Range selection cannot be
7149 * changed at run-time and it is fixed. Viceversa the driver'll try to
7150 * set the MDC clock dynamically according to the csr actual
7153 if (priv->plat->clk_csr >= 0)
7154 priv->clk_csr = priv->plat->clk_csr;
7156 stmmac_clk_csr_set(priv);
7158 stmmac_check_pcs_mode(priv);
7160 pm_runtime_get_noresume(device);
7161 pm_runtime_set_active(device);
7162 if (!pm_runtime_enabled(device))
7163 pm_runtime_enable(device);
7165 if (priv->hw->pcs != STMMAC_PCS_TBI &&
7166 priv->hw->pcs != STMMAC_PCS_RTBI) {
7167 /* MDIO bus Registration */
7168 ret = stmmac_mdio_register(ndev);
7170 dev_err(priv->device,
7171 "%s: MDIO bus (id: %d) registration failed",
7172 __func__, priv->plat->bus_id);
7173 goto error_mdio_register;
7177 if (priv->plat->speed_mode_2500)
7178 priv->plat->speed_mode_2500(ndev, priv->plat->bsp_priv);
7180 if (priv->plat->mdio_bus_data && priv->plat->mdio_bus_data->has_xpcs) {
7181 ret = stmmac_xpcs_setup(priv->mii);
7183 goto error_xpcs_setup;
7186 ret = stmmac_phy_setup(priv);
7188 netdev_err(ndev, "failed to setup phy (%d)\n", ret);
7189 goto error_phy_setup;
7192 ret = register_netdev(ndev);
7194 dev_err(priv->device, "%s: ERROR %i registering the device\n",
7196 goto error_netdev_register;
7199 if (priv->plat->serdes_powerup) {
7200 ret = priv->plat->serdes_powerup(ndev,
7201 priv->plat->bsp_priv);
7204 goto error_serdes_powerup;
7207 #ifdef CONFIG_DEBUG_FS
7208 stmmac_init_fs(ndev);
7211 if (priv->plat->dump_debug_regs)
7212 priv->plat->dump_debug_regs(priv->plat->bsp_priv);
7214 /* Let pm_runtime_put() disable the clocks.
7215 * If CONFIG_PM is not enabled, the clocks will stay powered.
7217 pm_runtime_put(device);
7221 error_serdes_powerup:
7222 unregister_netdev(ndev);
7223 error_netdev_register:
7224 phylink_destroy(priv->phylink);
7227 if (priv->hw->pcs != STMMAC_PCS_TBI &&
7228 priv->hw->pcs != STMMAC_PCS_RTBI)
7229 stmmac_mdio_unregister(ndev);
7230 error_mdio_register:
7231 stmmac_napi_del(ndev);
7233 destroy_workqueue(priv->wq);
7234 bitmap_free(priv->af_xdp_zc_qps);
7238 EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
7242 * @dev: device pointer
7243 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
7244 * changes the link status, releases the DMA descriptor rings.
7246 int stmmac_dvr_remove(struct device *dev)
7248 struct net_device *ndev = dev_get_drvdata(dev);
7249 struct stmmac_priv *priv = netdev_priv(ndev);
7251 netdev_info(priv->dev, "%s: removing driver", __func__);
7253 stmmac_stop_all_dma(priv);
7254 stmmac_mac_set(priv, priv->ioaddr, false);
7255 netif_carrier_off(ndev);
7256 unregister_netdev(ndev);
7258 /* Serdes power down needs to happen after VLAN filter
7259 * is deleted that is triggered by unregister_netdev().
7261 if (priv->plat->serdes_powerdown)
7262 priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv);
7264 #ifdef CONFIG_DEBUG_FS
7265 stmmac_exit_fs(ndev);
7267 phylink_destroy(priv->phylink);
7268 if (priv->plat->stmmac_rst)
7269 reset_control_assert(priv->plat->stmmac_rst);
7270 reset_control_assert(priv->plat->stmmac_ahb_rst);
7271 pm_runtime_put(dev);
7272 pm_runtime_disable(dev);
7273 if (priv->hw->pcs != STMMAC_PCS_TBI &&
7274 priv->hw->pcs != STMMAC_PCS_RTBI)
7275 stmmac_mdio_unregister(ndev);
7276 destroy_workqueue(priv->wq);
7277 mutex_destroy(&priv->lock);
7278 bitmap_free(priv->af_xdp_zc_qps);
7282 EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
7285 * stmmac_suspend - suspend callback
7286 * @dev: device pointer
7287 * Description: this is the function to suspend the device and it is called
7288 * by the platform driver to stop the network queue, release the resources,
7289 * program the PMT register (for WoL), clean and release driver resources.
7291 int stmmac_suspend(struct device *dev)
7293 struct net_device *ndev = dev_get_drvdata(dev);
7294 struct stmmac_priv *priv = netdev_priv(ndev);
7297 if (!ndev || !netif_running(ndev))
7300 mutex_lock(&priv->lock);
7302 netif_device_detach(ndev);
7304 stmmac_disable_all_queues(priv);
7306 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
7307 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
7309 if (priv->eee_enabled) {
7310 priv->tx_path_in_lpi_mode = false;
7311 del_timer_sync(&priv->eee_ctrl_timer);
7314 /* Stop TX/RX DMA */
7315 stmmac_stop_all_dma(priv);
7317 if (priv->plat->serdes_powerdown)
7318 priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv);
7320 /* Enable Power down mode by programming the PMT regs */
7321 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7322 stmmac_pmt(priv, priv->hw, priv->wolopts);
7325 stmmac_mac_set(priv, priv->ioaddr, false);
7326 pinctrl_pm_select_sleep_state(priv->device);
7329 mutex_unlock(&priv->lock);
7332 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7333 phylink_suspend(priv->phylink, true);
7335 if (device_may_wakeup(priv->device))
7336 phylink_speed_down(priv->phylink, false);
7337 phylink_suspend(priv->phylink, false);
7341 if (priv->dma_cap.fpesel) {
7343 stmmac_fpe_configure(priv, priv->ioaddr,
7344 priv->plat->tx_queues_to_use,
7345 priv->plat->rx_queues_to_use, false);
7347 stmmac_fpe_handshake(priv, false);
7348 stmmac_fpe_stop_wq(priv);
7351 priv->speed = SPEED_UNKNOWN;
7354 EXPORT_SYMBOL_GPL(stmmac_suspend);
7357 * stmmac_reset_queues_param - reset queue parameters
7358 * @priv: device pointer
7360 static void stmmac_reset_queues_param(struct stmmac_priv *priv)
7362 u32 rx_cnt = priv->plat->rx_queues_to_use;
7363 u32 tx_cnt = priv->plat->tx_queues_to_use;
7366 for (queue = 0; queue < rx_cnt; queue++) {
7367 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
7373 for (queue = 0; queue < tx_cnt; queue++) {
7374 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
7380 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
7385 * stmmac_resume - resume callback
7386 * @dev: device pointer
7387 * Description: when resume this function is invoked to setup the DMA and CORE
7388 * in a usable state.
7390 int stmmac_resume(struct device *dev)
7392 struct net_device *ndev = dev_get_drvdata(dev);
7393 struct stmmac_priv *priv = netdev_priv(ndev);
7396 if (!netif_running(ndev))
7399 /* Power Down bit, into the PM register, is cleared
7400 * automatically as soon as a magic packet or a Wake-up frame
7401 * is received. Anyway, it's better to manually clear
7402 * this bit because it can generate problems while resuming
7403 * from another devices (e.g. serial console).
7405 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7406 mutex_lock(&priv->lock);
7407 stmmac_pmt(priv, priv->hw, 0);
7408 mutex_unlock(&priv->lock);
7411 pinctrl_pm_select_default_state(priv->device);
7412 /* reset the phy so that it's ready */
7414 stmmac_mdio_reset(priv->mii);
7417 if (priv->plat->serdes_powerup) {
7418 ret = priv->plat->serdes_powerup(ndev,
7419 priv->plat->bsp_priv);
7426 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7427 phylink_resume(priv->phylink);
7429 phylink_resume(priv->phylink);
7430 if (device_may_wakeup(priv->device))
7431 phylink_speed_up(priv->phylink);
7436 mutex_lock(&priv->lock);
7438 stmmac_reset_queues_param(priv);
7440 stmmac_free_tx_skbufs(priv);
7441 stmmac_clear_descriptors(priv);
7443 stmmac_hw_setup(ndev, false);
7444 stmmac_init_coalesce(priv);
7445 stmmac_set_rx_mode(ndev);
7447 stmmac_restore_hw_vlan_rx_fltr(priv, ndev, priv->hw);
7449 stmmac_enable_all_queues(priv);
7451 mutex_unlock(&priv->lock);
7454 netif_device_attach(ndev);
7458 EXPORT_SYMBOL_GPL(stmmac_resume);
7461 static int __init stmmac_cmdline_opt(char *str)
7467 while ((opt = strsep(&str, ",")) != NULL) {
7468 if (!strncmp(opt, "debug:", 6)) {
7469 if (kstrtoint(opt + 6, 0, &debug))
7471 } else if (!strncmp(opt, "phyaddr:", 8)) {
7472 if (kstrtoint(opt + 8, 0, &phyaddr))
7474 } else if (!strncmp(opt, "buf_sz:", 7)) {
7475 if (kstrtoint(opt + 7, 0, &buf_sz))
7477 } else if (!strncmp(opt, "tc:", 3)) {
7478 if (kstrtoint(opt + 3, 0, &tc))
7480 } else if (!strncmp(opt, "watchdog:", 9)) {
7481 if (kstrtoint(opt + 9, 0, &watchdog))
7483 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
7484 if (kstrtoint(opt + 10, 0, &flow_ctrl))
7486 } else if (!strncmp(opt, "pause:", 6)) {
7487 if (kstrtoint(opt + 6, 0, &pause))
7489 } else if (!strncmp(opt, "eee_timer:", 10)) {
7490 if (kstrtoint(opt + 10, 0, &eee_timer))
7492 } else if (!strncmp(opt, "chain_mode:", 11)) {
7493 if (kstrtoint(opt + 11, 0, &chain_mode))
7500 pr_err("%s: ERROR broken module parameter conversion", __func__);
7504 __setup("stmmaceth=", stmmac_cmdline_opt);
7507 static int __init stmmac_init(void)
7509 #ifdef CONFIG_DEBUG_FS
7510 /* Create debugfs main directory if it doesn't exist yet */
7512 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
7513 register_netdevice_notifier(&stmmac_notifier);
7519 static void __exit stmmac_exit(void)
7521 #ifdef CONFIG_DEBUG_FS
7522 unregister_netdevice_notifier(&stmmac_notifier);
7523 debugfs_remove_recursive(stmmac_fs_dir);
7527 module_init(stmmac_init)
7528 module_exit(stmmac_exit)
7530 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
7531 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
7532 MODULE_LICENSE("GPL");