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 __ETHTOOL_DECLARE_LINK_MODE_MASK(mac_supported) = { 0, };
945 /* This is very similar to phylink_generic_validate() except that
946 * we always use PHY_INTERFACE_MODE_INTERNAL to get all capabilities.
947 * This is because we don't always have config->supported_interfaces
948 * populated (only when we have the XPCS.)
950 * When we do have an XPCS, we could pass state->interface, as XPCS
951 * limits to a subset of the ethtool link modes allowed here.
953 phylink_set(mac_supported, Autoneg);
954 phylink_set_port_modes(mac_supported);
955 phylink_get_linkmodes(mac_supported, PHY_INTERFACE_MODE_INTERNAL,
956 config->mac_capabilities);
958 linkmode_and(supported, supported, mac_supported);
959 linkmode_and(state->advertising, state->advertising, mac_supported);
962 static void stmmac_mac_config(struct phylink_config *config, unsigned int mode,
963 const struct phylink_link_state *state)
965 /* Nothing to do, xpcs_config() handles everything */
968 static void stmmac_fpe_link_state_handle(struct stmmac_priv *priv, bool is_up)
970 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
971 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
972 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
973 bool *hs_enable = &fpe_cfg->hs_enable;
975 if (is_up && *hs_enable) {
976 stmmac_fpe_send_mpacket(priv, priv->ioaddr, MPACKET_VERIFY);
978 *lo_state = FPE_STATE_OFF;
979 *lp_state = FPE_STATE_OFF;
983 static void stmmac_mac_link_down(struct phylink_config *config,
984 unsigned int mode, phy_interface_t interface)
986 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
988 stmmac_mac_set(priv, priv->ioaddr, false);
989 priv->eee_active = false;
990 priv->tx_lpi_enabled = false;
991 priv->eee_enabled = stmmac_eee_init(priv);
992 stmmac_set_eee_pls(priv, priv->hw, false);
994 if (priv->dma_cap.fpesel)
995 stmmac_fpe_link_state_handle(priv, false);
998 static void stmmac_mac_link_up(struct phylink_config *config,
999 struct phy_device *phy,
1000 unsigned int mode, phy_interface_t interface,
1001 int speed, int duplex,
1002 bool tx_pause, bool rx_pause)
1004 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
1007 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
1008 ctrl &= ~priv->hw->link.speed_mask;
1010 if (interface == PHY_INTERFACE_MODE_USXGMII) {
1013 ctrl |= priv->hw->link.xgmii.speed10000;
1016 ctrl |= priv->hw->link.xgmii.speed5000;
1019 ctrl |= priv->hw->link.xgmii.speed2500;
1024 } else if (interface == PHY_INTERFACE_MODE_XLGMII) {
1027 ctrl |= priv->hw->link.xlgmii.speed100000;
1030 ctrl |= priv->hw->link.xlgmii.speed50000;
1033 ctrl |= priv->hw->link.xlgmii.speed40000;
1036 ctrl |= priv->hw->link.xlgmii.speed25000;
1039 ctrl |= priv->hw->link.xgmii.speed10000;
1042 ctrl |= priv->hw->link.speed2500;
1045 ctrl |= priv->hw->link.speed1000;
1053 ctrl |= priv->hw->link.speed2500;
1056 ctrl |= priv->hw->link.speed1000;
1059 ctrl |= priv->hw->link.speed100;
1062 ctrl |= priv->hw->link.speed10;
1069 priv->speed = speed;
1071 if (priv->plat->fix_mac_speed)
1072 priv->plat->fix_mac_speed(priv->plat->bsp_priv, speed);
1075 ctrl &= ~priv->hw->link.duplex;
1077 ctrl |= priv->hw->link.duplex;
1079 /* Flow Control operation */
1080 if (tx_pause && rx_pause)
1081 stmmac_mac_flow_ctrl(priv, duplex);
1083 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
1085 stmmac_mac_set(priv, priv->ioaddr, true);
1086 if (phy && priv->dma_cap.eee) {
1087 priv->eee_active = phy_init_eee(phy, 1) >= 0;
1088 priv->eee_enabled = stmmac_eee_init(priv);
1089 priv->tx_lpi_enabled = priv->eee_enabled;
1090 stmmac_set_eee_pls(priv, priv->hw, true);
1093 if (priv->dma_cap.fpesel)
1094 stmmac_fpe_link_state_handle(priv, true);
1097 static const struct phylink_mac_ops stmmac_phylink_mac_ops = {
1098 .validate = stmmac_validate,
1099 .mac_config = stmmac_mac_config,
1100 .mac_link_down = stmmac_mac_link_down,
1101 .mac_link_up = stmmac_mac_link_up,
1105 * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
1106 * @priv: driver private structure
1107 * Description: this is to verify if the HW supports the PCS.
1108 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
1109 * configured for the TBI, RTBI, or SGMII PHY interface.
1111 static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
1113 int interface = priv->plat->interface;
1115 if (priv->dma_cap.pcs) {
1116 if ((interface == PHY_INTERFACE_MODE_RGMII) ||
1117 (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
1118 (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
1119 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
1120 netdev_dbg(priv->dev, "PCS RGMII support enabled\n");
1121 priv->hw->pcs = STMMAC_PCS_RGMII;
1122 } else if (interface == PHY_INTERFACE_MODE_SGMII) {
1123 netdev_dbg(priv->dev, "PCS SGMII support enabled\n");
1124 priv->hw->pcs = STMMAC_PCS_SGMII;
1130 * stmmac_init_phy - PHY initialization
1131 * @dev: net device structure
1132 * Description: it initializes the driver's PHY state, and attaches the PHY
1133 * to the mac driver.
1137 static int stmmac_init_phy(struct net_device *dev)
1139 struct stmmac_priv *priv = netdev_priv(dev);
1140 struct device_node *node;
1143 node = priv->plat->phylink_node;
1146 ret = phylink_of_phy_connect(priv->phylink, node, 0);
1148 /* Some DT bindings do not set-up the PHY handle. Let's try to
1152 int addr = priv->plat->phy_addr;
1153 struct phy_device *phydev;
1155 phydev = mdiobus_get_phy(priv->mii, addr);
1157 netdev_err(priv->dev, "no phy at addr %d\n", addr);
1161 ret = phylink_connect_phy(priv->phylink, phydev);
1164 if (!priv->plat->pmt) {
1165 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1167 phylink_ethtool_get_wol(priv->phylink, &wol);
1168 device_set_wakeup_capable(priv->device, !!wol.supported);
1174 static int stmmac_phy_setup(struct stmmac_priv *priv)
1176 struct stmmac_mdio_bus_data *mdio_bus_data = priv->plat->mdio_bus_data;
1177 struct fwnode_handle *fwnode = of_fwnode_handle(priv->plat->phylink_node);
1178 int max_speed = priv->plat->max_speed;
1179 int mode = priv->plat->phy_interface;
1180 struct phylink *phylink;
1182 priv->phylink_config.dev = &priv->dev->dev;
1183 priv->phylink_config.type = PHYLINK_NETDEV;
1184 priv->phylink_config.pcs_poll = true;
1185 if (priv->plat->mdio_bus_data)
1186 priv->phylink_config.ovr_an_inband =
1187 mdio_bus_data->xpcs_an_inband;
1190 fwnode = dev_fwnode(priv->device);
1192 /* Set the platform/firmware specified interface mode */
1193 __set_bit(mode, priv->phylink_config.supported_interfaces);
1195 /* If we have an xpcs, it defines which PHY interfaces are supported. */
1197 xpcs_get_interfaces(priv->hw->xpcs,
1198 priv->phylink_config.supported_interfaces);
1200 priv->phylink_config.mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
1203 if (!max_speed || max_speed >= 1000)
1204 priv->phylink_config.mac_capabilities |= MAC_1000;
1206 if (priv->plat->has_gmac4) {
1207 if (!max_speed || max_speed >= 2500)
1208 priv->phylink_config.mac_capabilities |= MAC_2500FD;
1209 } else if (priv->plat->has_xgmac) {
1210 if (!max_speed || max_speed >= 2500)
1211 priv->phylink_config.mac_capabilities |= MAC_2500FD;
1212 if (!max_speed || max_speed >= 5000)
1213 priv->phylink_config.mac_capabilities |= MAC_5000FD;
1214 if (!max_speed || max_speed >= 10000)
1215 priv->phylink_config.mac_capabilities |= MAC_10000FD;
1216 if (!max_speed || max_speed >= 25000)
1217 priv->phylink_config.mac_capabilities |= MAC_25000FD;
1218 if (!max_speed || max_speed >= 40000)
1219 priv->phylink_config.mac_capabilities |= MAC_40000FD;
1220 if (!max_speed || max_speed >= 50000)
1221 priv->phylink_config.mac_capabilities |= MAC_50000FD;
1222 if (!max_speed || max_speed >= 100000)
1223 priv->phylink_config.mac_capabilities |= MAC_100000FD;
1226 /* Half-Duplex can only work with single queue */
1227 if (priv->plat->tx_queues_to_use > 1)
1228 priv->phylink_config.mac_capabilities &=
1229 ~(MAC_10HD | MAC_100HD | MAC_1000HD);
1231 phylink = phylink_create(&priv->phylink_config, fwnode,
1232 mode, &stmmac_phylink_mac_ops);
1233 if (IS_ERR(phylink))
1234 return PTR_ERR(phylink);
1237 phylink_set_pcs(phylink, &priv->hw->xpcs->pcs);
1239 priv->phylink = phylink;
1243 static void stmmac_display_rx_rings(struct stmmac_priv *priv)
1245 u32 rx_cnt = priv->plat->rx_queues_to_use;
1246 unsigned int desc_size;
1250 /* Display RX rings */
1251 for (queue = 0; queue < rx_cnt; queue++) {
1252 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1254 pr_info("\tRX Queue %u rings\n", queue);
1256 if (priv->extend_desc) {
1257 head_rx = (void *)rx_q->dma_erx;
1258 desc_size = sizeof(struct dma_extended_desc);
1260 head_rx = (void *)rx_q->dma_rx;
1261 desc_size = sizeof(struct dma_desc);
1264 /* Display RX ring */
1265 stmmac_display_ring(priv, head_rx, priv->dma_rx_size, true,
1266 rx_q->dma_rx_phy, desc_size);
1270 static void stmmac_display_tx_rings(struct stmmac_priv *priv)
1272 u32 tx_cnt = priv->plat->tx_queues_to_use;
1273 unsigned int desc_size;
1277 /* Display TX rings */
1278 for (queue = 0; queue < tx_cnt; queue++) {
1279 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1281 pr_info("\tTX Queue %d rings\n", queue);
1283 if (priv->extend_desc) {
1284 head_tx = (void *)tx_q->dma_etx;
1285 desc_size = sizeof(struct dma_extended_desc);
1286 } else if (tx_q->tbs & STMMAC_TBS_AVAIL) {
1287 head_tx = (void *)tx_q->dma_entx;
1288 desc_size = sizeof(struct dma_edesc);
1290 head_tx = (void *)tx_q->dma_tx;
1291 desc_size = sizeof(struct dma_desc);
1294 stmmac_display_ring(priv, head_tx, priv->dma_tx_size, false,
1295 tx_q->dma_tx_phy, desc_size);
1299 static void stmmac_display_rings(struct stmmac_priv *priv)
1301 /* Display RX ring */
1302 stmmac_display_rx_rings(priv);
1304 /* Display TX ring */
1305 stmmac_display_tx_rings(priv);
1308 static int stmmac_set_bfsize(int mtu, int bufsize)
1312 if (mtu >= BUF_SIZE_8KiB)
1313 ret = BUF_SIZE_16KiB;
1314 else if (mtu >= BUF_SIZE_4KiB)
1315 ret = BUF_SIZE_8KiB;
1316 else if (mtu >= BUF_SIZE_2KiB)
1317 ret = BUF_SIZE_4KiB;
1318 else if (mtu > DEFAULT_BUFSIZE)
1319 ret = BUF_SIZE_2KiB;
1321 ret = DEFAULT_BUFSIZE;
1327 * stmmac_clear_rx_descriptors - clear RX descriptors
1328 * @priv: driver private structure
1329 * @queue: RX queue index
1330 * Description: this function is called to clear the RX descriptors
1331 * in case of both basic and extended descriptors are used.
1333 static void stmmac_clear_rx_descriptors(struct stmmac_priv *priv, u32 queue)
1335 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1338 /* Clear the RX descriptors */
1339 for (i = 0; i < priv->dma_rx_size; i++)
1340 if (priv->extend_desc)
1341 stmmac_init_rx_desc(priv, &rx_q->dma_erx[i].basic,
1342 priv->use_riwt, priv->mode,
1343 (i == priv->dma_rx_size - 1),
1346 stmmac_init_rx_desc(priv, &rx_q->dma_rx[i],
1347 priv->use_riwt, priv->mode,
1348 (i == priv->dma_rx_size - 1),
1353 * stmmac_clear_tx_descriptors - clear tx descriptors
1354 * @priv: driver private structure
1355 * @queue: TX queue index.
1356 * Description: this function is called to clear the TX descriptors
1357 * in case of both basic and extended descriptors are used.
1359 static void stmmac_clear_tx_descriptors(struct stmmac_priv *priv, u32 queue)
1361 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1364 /* Clear the TX descriptors */
1365 for (i = 0; i < priv->dma_tx_size; i++) {
1366 int last = (i == (priv->dma_tx_size - 1));
1369 if (priv->extend_desc)
1370 p = &tx_q->dma_etx[i].basic;
1371 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1372 p = &tx_q->dma_entx[i].basic;
1374 p = &tx_q->dma_tx[i];
1376 stmmac_init_tx_desc(priv, p, priv->mode, last);
1381 * stmmac_clear_descriptors - clear descriptors
1382 * @priv: driver private structure
1383 * Description: this function is called to clear the TX and RX descriptors
1384 * in case of both basic and extended descriptors are used.
1386 static void stmmac_clear_descriptors(struct stmmac_priv *priv)
1388 u32 rx_queue_cnt = priv->plat->rx_queues_to_use;
1389 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1392 /* Clear the RX descriptors */
1393 for (queue = 0; queue < rx_queue_cnt; queue++)
1394 stmmac_clear_rx_descriptors(priv, queue);
1396 /* Clear the TX descriptors */
1397 for (queue = 0; queue < tx_queue_cnt; queue++)
1398 stmmac_clear_tx_descriptors(priv, queue);
1402 * stmmac_init_rx_buffers - init the RX descriptor buffer.
1403 * @priv: driver private structure
1404 * @p: descriptor pointer
1405 * @i: descriptor index
1407 * @queue: RX queue index
1408 * Description: this function is called to allocate a receive buffer, perform
1409 * the DMA mapping and init the descriptor.
1411 static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
1412 int i, gfp_t flags, u32 queue)
1414 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1415 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1416 gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
1418 if (priv->dma_cap.addr64 <= 32)
1422 buf->page = page_pool_alloc_pages(rx_q->page_pool, gfp);
1425 buf->page_offset = stmmac_rx_offset(priv);
1428 if (priv->sph && !buf->sec_page) {
1429 buf->sec_page = page_pool_alloc_pages(rx_q->page_pool, gfp);
1433 buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
1434 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true);
1436 buf->sec_page = NULL;
1437 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false);
1440 buf->addr = page_pool_get_dma_addr(buf->page) + buf->page_offset;
1442 stmmac_set_desc_addr(priv, p, buf->addr);
1443 if (priv->dma_buf_sz == BUF_SIZE_16KiB)
1444 stmmac_init_desc3(priv, p);
1450 * stmmac_free_rx_buffer - free RX dma buffers
1451 * @priv: private structure
1452 * @queue: RX queue index
1455 static void stmmac_free_rx_buffer(struct stmmac_priv *priv, u32 queue, int i)
1457 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1458 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1461 page_pool_put_full_page(rx_q->page_pool, buf->page, false);
1465 page_pool_put_full_page(rx_q->page_pool, buf->sec_page, false);
1466 buf->sec_page = NULL;
1470 * stmmac_free_tx_buffer - free RX dma buffers
1471 * @priv: private structure
1472 * @queue: RX queue index
1475 static void stmmac_free_tx_buffer(struct stmmac_priv *priv, u32 queue, int i)
1477 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1479 if (tx_q->tx_skbuff_dma[i].buf &&
1480 tx_q->tx_skbuff_dma[i].buf_type != STMMAC_TXBUF_T_XDP_TX) {
1481 if (tx_q->tx_skbuff_dma[i].map_as_page)
1482 dma_unmap_page(priv->device,
1483 tx_q->tx_skbuff_dma[i].buf,
1484 tx_q->tx_skbuff_dma[i].len,
1487 dma_unmap_single(priv->device,
1488 tx_q->tx_skbuff_dma[i].buf,
1489 tx_q->tx_skbuff_dma[i].len,
1493 if (tx_q->xdpf[i] &&
1494 (tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XDP_TX ||
1495 tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XDP_NDO)) {
1496 xdp_return_frame(tx_q->xdpf[i]);
1497 tx_q->xdpf[i] = NULL;
1500 if (tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XSK_TX)
1501 tx_q->xsk_frames_done++;
1503 if (tx_q->tx_skbuff[i] &&
1504 tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_SKB) {
1505 dev_kfree_skb_any(tx_q->tx_skbuff[i]);
1506 tx_q->tx_skbuff[i] = NULL;
1509 tx_q->tx_skbuff_dma[i].buf = 0;
1510 tx_q->tx_skbuff_dma[i].map_as_page = false;
1514 * dma_free_rx_skbufs - free RX dma buffers
1515 * @priv: private structure
1516 * @queue: RX queue index
1518 static void dma_free_rx_skbufs(struct stmmac_priv *priv, u32 queue)
1522 for (i = 0; i < priv->dma_rx_size; i++)
1523 stmmac_free_rx_buffer(priv, queue, i);
1526 static int stmmac_alloc_rx_buffers(struct stmmac_priv *priv, u32 queue,
1529 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1532 for (i = 0; i < priv->dma_rx_size; i++) {
1536 if (priv->extend_desc)
1537 p = &((rx_q->dma_erx + i)->basic);
1539 p = rx_q->dma_rx + i;
1541 ret = stmmac_init_rx_buffers(priv, p, i, flags,
1546 rx_q->buf_alloc_num++;
1553 * dma_free_rx_xskbufs - free RX dma buffers from XSK pool
1554 * @priv: private structure
1555 * @queue: RX queue index
1557 static void dma_free_rx_xskbufs(struct stmmac_priv *priv, u32 queue)
1559 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1562 for (i = 0; i < priv->dma_rx_size; i++) {
1563 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1568 xsk_buff_free(buf->xdp);
1573 static int stmmac_alloc_rx_buffers_zc(struct stmmac_priv *priv, u32 queue)
1575 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1578 for (i = 0; i < priv->dma_rx_size; i++) {
1579 struct stmmac_rx_buffer *buf;
1580 dma_addr_t dma_addr;
1583 if (priv->extend_desc)
1584 p = (struct dma_desc *)(rx_q->dma_erx + i);
1586 p = rx_q->dma_rx + i;
1588 buf = &rx_q->buf_pool[i];
1590 buf->xdp = xsk_buff_alloc(rx_q->xsk_pool);
1594 dma_addr = xsk_buff_xdp_get_dma(buf->xdp);
1595 stmmac_set_desc_addr(priv, p, dma_addr);
1596 rx_q->buf_alloc_num++;
1602 static struct xsk_buff_pool *stmmac_get_xsk_pool(struct stmmac_priv *priv, u32 queue)
1604 if (!stmmac_xdp_is_enabled(priv) || !test_bit(queue, priv->af_xdp_zc_qps))
1607 return xsk_get_pool_from_qid(priv->dev, queue);
1611 * __init_dma_rx_desc_rings - init the RX descriptor ring (per queue)
1612 * @priv: driver private structure
1613 * @queue: RX queue index
1615 * Description: this function initializes the DMA RX descriptors
1616 * and allocates the socket buffers. It supports the chained and ring
1619 static int __init_dma_rx_desc_rings(struct stmmac_priv *priv, u32 queue, gfp_t flags)
1621 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1624 netif_dbg(priv, probe, priv->dev,
1625 "(%s) dma_rx_phy=0x%08x\n", __func__,
1626 (u32)rx_q->dma_rx_phy);
1628 stmmac_clear_rx_descriptors(priv, queue);
1630 xdp_rxq_info_unreg_mem_model(&rx_q->xdp_rxq);
1632 rx_q->xsk_pool = stmmac_get_xsk_pool(priv, queue);
1634 if (rx_q->xsk_pool) {
1635 WARN_ON(xdp_rxq_info_reg_mem_model(&rx_q->xdp_rxq,
1636 MEM_TYPE_XSK_BUFF_POOL,
1638 netdev_info(priv->dev,
1639 "Register MEM_TYPE_XSK_BUFF_POOL RxQ-%d\n",
1641 xsk_pool_set_rxq_info(rx_q->xsk_pool, &rx_q->xdp_rxq);
1643 WARN_ON(xdp_rxq_info_reg_mem_model(&rx_q->xdp_rxq,
1646 netdev_info(priv->dev,
1647 "Register MEM_TYPE_PAGE_POOL RxQ-%d\n",
1651 if (rx_q->xsk_pool) {
1652 /* RX XDP ZC buffer pool may not be populated, e.g.
1655 stmmac_alloc_rx_buffers_zc(priv, queue);
1657 ret = stmmac_alloc_rx_buffers(priv, queue, flags);
1665 /* Setup the chained descriptor addresses */
1666 if (priv->mode == STMMAC_CHAIN_MODE) {
1667 if (priv->extend_desc)
1668 stmmac_mode_init(priv, rx_q->dma_erx,
1670 priv->dma_rx_size, 1);
1672 stmmac_mode_init(priv, rx_q->dma_rx,
1674 priv->dma_rx_size, 0);
1680 static int init_dma_rx_desc_rings(struct net_device *dev, gfp_t flags)
1682 struct stmmac_priv *priv = netdev_priv(dev);
1683 u32 rx_count = priv->plat->rx_queues_to_use;
1687 /* RX INITIALIZATION */
1688 netif_dbg(priv, probe, priv->dev,
1689 "SKB addresses:\nskb\t\tskb data\tdma data\n");
1691 for (queue = 0; queue < rx_count; queue++) {
1692 ret = __init_dma_rx_desc_rings(priv, queue, flags);
1694 goto err_init_rx_buffers;
1699 err_init_rx_buffers:
1700 while (queue >= 0) {
1701 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1704 dma_free_rx_xskbufs(priv, queue);
1706 dma_free_rx_skbufs(priv, queue);
1708 rx_q->buf_alloc_num = 0;
1709 rx_q->xsk_pool = NULL;
1721 * __init_dma_tx_desc_rings - init the TX descriptor ring (per queue)
1722 * @priv: driver private structure
1723 * @queue : TX queue index
1724 * Description: this function initializes the DMA TX descriptors
1725 * and allocates the socket buffers. It supports the chained and ring
1728 static int __init_dma_tx_desc_rings(struct stmmac_priv *priv, u32 queue)
1730 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1733 netif_dbg(priv, probe, priv->dev,
1734 "(%s) dma_tx_phy=0x%08x\n", __func__,
1735 (u32)tx_q->dma_tx_phy);
1737 /* Setup the chained descriptor addresses */
1738 if (priv->mode == STMMAC_CHAIN_MODE) {
1739 if (priv->extend_desc)
1740 stmmac_mode_init(priv, tx_q->dma_etx,
1742 priv->dma_tx_size, 1);
1743 else if (!(tx_q->tbs & STMMAC_TBS_AVAIL))
1744 stmmac_mode_init(priv, tx_q->dma_tx,
1746 priv->dma_tx_size, 0);
1749 tx_q->xsk_pool = stmmac_get_xsk_pool(priv, queue);
1751 for (i = 0; i < priv->dma_tx_size; i++) {
1754 if (priv->extend_desc)
1755 p = &((tx_q->dma_etx + i)->basic);
1756 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1757 p = &((tx_q->dma_entx + i)->basic);
1759 p = tx_q->dma_tx + i;
1761 stmmac_clear_desc(priv, p);
1763 tx_q->tx_skbuff_dma[i].buf = 0;
1764 tx_q->tx_skbuff_dma[i].map_as_page = false;
1765 tx_q->tx_skbuff_dma[i].len = 0;
1766 tx_q->tx_skbuff_dma[i].last_segment = false;
1767 tx_q->tx_skbuff[i] = NULL;
1774 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
1779 static int init_dma_tx_desc_rings(struct net_device *dev)
1781 struct stmmac_priv *priv = netdev_priv(dev);
1785 tx_queue_cnt = priv->plat->tx_queues_to_use;
1787 for (queue = 0; queue < tx_queue_cnt; queue++)
1788 __init_dma_tx_desc_rings(priv, queue);
1794 * init_dma_desc_rings - init the RX/TX descriptor rings
1795 * @dev: net device structure
1797 * Description: this function initializes the DMA RX/TX descriptors
1798 * and allocates the socket buffers. It supports the chained and ring
1801 static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
1803 struct stmmac_priv *priv = netdev_priv(dev);
1806 ret = init_dma_rx_desc_rings(dev, flags);
1810 ret = init_dma_tx_desc_rings(dev);
1812 stmmac_clear_descriptors(priv);
1814 if (netif_msg_hw(priv))
1815 stmmac_display_rings(priv);
1821 * dma_free_tx_skbufs - free TX dma buffers
1822 * @priv: private structure
1823 * @queue: TX queue index
1825 static void dma_free_tx_skbufs(struct stmmac_priv *priv, u32 queue)
1827 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1830 tx_q->xsk_frames_done = 0;
1832 for (i = 0; i < priv->dma_tx_size; i++)
1833 stmmac_free_tx_buffer(priv, queue, i);
1835 if (tx_q->xsk_pool && tx_q->xsk_frames_done) {
1836 xsk_tx_completed(tx_q->xsk_pool, tx_q->xsk_frames_done);
1837 tx_q->xsk_frames_done = 0;
1838 tx_q->xsk_pool = NULL;
1843 * stmmac_free_tx_skbufs - free TX skb buffers
1844 * @priv: private structure
1846 static void stmmac_free_tx_skbufs(struct stmmac_priv *priv)
1848 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1851 for (queue = 0; queue < tx_queue_cnt; queue++)
1852 dma_free_tx_skbufs(priv, queue);
1856 * __free_dma_rx_desc_resources - free RX dma desc resources (per queue)
1857 * @priv: private structure
1858 * @queue: RX queue index
1860 static void __free_dma_rx_desc_resources(struct stmmac_priv *priv, u32 queue)
1862 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1864 /* Release the DMA RX socket buffers */
1866 dma_free_rx_xskbufs(priv, queue);
1868 dma_free_rx_skbufs(priv, queue);
1870 rx_q->buf_alloc_num = 0;
1871 rx_q->xsk_pool = NULL;
1873 /* Free DMA regions of consistent memory previously allocated */
1874 if (!priv->extend_desc)
1875 dma_free_coherent(priv->device, priv->dma_rx_size *
1876 sizeof(struct dma_desc),
1877 rx_q->dma_rx, rx_q->dma_rx_phy);
1879 dma_free_coherent(priv->device, priv->dma_rx_size *
1880 sizeof(struct dma_extended_desc),
1881 rx_q->dma_erx, rx_q->dma_rx_phy);
1883 if (xdp_rxq_info_is_reg(&rx_q->xdp_rxq))
1884 xdp_rxq_info_unreg(&rx_q->xdp_rxq);
1886 kfree(rx_q->buf_pool);
1887 if (rx_q->page_pool)
1888 page_pool_destroy(rx_q->page_pool);
1891 static void free_dma_rx_desc_resources(struct stmmac_priv *priv)
1893 u32 rx_count = priv->plat->rx_queues_to_use;
1896 /* Free RX queue resources */
1897 for (queue = 0; queue < rx_count; queue++)
1898 __free_dma_rx_desc_resources(priv, queue);
1902 * __free_dma_tx_desc_resources - free TX dma desc resources (per queue)
1903 * @priv: private structure
1904 * @queue: TX queue index
1906 static void __free_dma_tx_desc_resources(struct stmmac_priv *priv, u32 queue)
1908 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1912 /* Release the DMA TX socket buffers */
1913 dma_free_tx_skbufs(priv, queue);
1915 if (priv->extend_desc) {
1916 size = sizeof(struct dma_extended_desc);
1917 addr = tx_q->dma_etx;
1918 } else if (tx_q->tbs & STMMAC_TBS_AVAIL) {
1919 size = sizeof(struct dma_edesc);
1920 addr = tx_q->dma_entx;
1922 size = sizeof(struct dma_desc);
1923 addr = tx_q->dma_tx;
1926 size *= priv->dma_tx_size;
1928 dma_free_coherent(priv->device, size, addr, tx_q->dma_tx_phy);
1930 kfree(tx_q->tx_skbuff_dma);
1931 kfree(tx_q->tx_skbuff);
1934 static void free_dma_tx_desc_resources(struct stmmac_priv *priv)
1936 u32 tx_count = priv->plat->tx_queues_to_use;
1939 /* Free TX queue resources */
1940 for (queue = 0; queue < tx_count; queue++)
1941 __free_dma_tx_desc_resources(priv, queue);
1945 * __alloc_dma_rx_desc_resources - alloc RX resources (per queue).
1946 * @priv: private structure
1947 * @queue: RX queue index
1948 * Description: according to which descriptor can be used (extend or basic)
1949 * this function allocates the resources for TX and RX paths. In case of
1950 * reception, for example, it pre-allocated the RX socket buffer in order to
1951 * allow zero-copy mechanism.
1953 static int __alloc_dma_rx_desc_resources(struct stmmac_priv *priv, u32 queue)
1955 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1956 struct stmmac_channel *ch = &priv->channel[queue];
1957 bool xdp_prog = stmmac_xdp_is_enabled(priv);
1958 struct page_pool_params pp_params = { 0 };
1959 unsigned int num_pages;
1960 unsigned int napi_id;
1963 rx_q->queue_index = queue;
1964 rx_q->priv_data = priv;
1966 pp_params.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
1967 pp_params.pool_size = priv->dma_rx_size;
1968 num_pages = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE);
1969 pp_params.order = ilog2(num_pages);
1970 pp_params.nid = dev_to_node(priv->device);
1971 pp_params.dev = priv->device;
1972 pp_params.dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
1973 pp_params.offset = stmmac_rx_offset(priv);
1974 pp_params.max_len = STMMAC_MAX_RX_BUF_SIZE(num_pages);
1976 rx_q->page_pool = page_pool_create(&pp_params);
1977 if (IS_ERR(rx_q->page_pool)) {
1978 ret = PTR_ERR(rx_q->page_pool);
1979 rx_q->page_pool = NULL;
1983 rx_q->buf_pool = kcalloc(priv->dma_rx_size,
1984 sizeof(*rx_q->buf_pool),
1986 if (!rx_q->buf_pool)
1989 if (priv->extend_desc) {
1990 rx_q->dma_erx = dma_alloc_coherent(priv->device,
1992 sizeof(struct dma_extended_desc),
1999 rx_q->dma_rx = dma_alloc_coherent(priv->device,
2001 sizeof(struct dma_desc),
2008 if (stmmac_xdp_is_enabled(priv) &&
2009 test_bit(queue, priv->af_xdp_zc_qps))
2010 napi_id = ch->rxtx_napi.napi_id;
2012 napi_id = ch->rx_napi.napi_id;
2014 ret = xdp_rxq_info_reg(&rx_q->xdp_rxq, priv->dev,
2018 netdev_err(priv->dev, "Failed to register xdp rxq info\n");
2025 static int alloc_dma_rx_desc_resources(struct stmmac_priv *priv)
2027 u32 rx_count = priv->plat->rx_queues_to_use;
2031 /* RX queues buffers and DMA */
2032 for (queue = 0; queue < rx_count; queue++) {
2033 ret = __alloc_dma_rx_desc_resources(priv, queue);
2041 free_dma_rx_desc_resources(priv);
2047 * __alloc_dma_tx_desc_resources - alloc TX resources (per queue).
2048 * @priv: private structure
2049 * @queue: TX queue index
2050 * Description: according to which descriptor can be used (extend or basic)
2051 * this function allocates the resources for TX and RX paths. In case of
2052 * reception, for example, it pre-allocated the RX socket buffer in order to
2053 * allow zero-copy mechanism.
2055 static int __alloc_dma_tx_desc_resources(struct stmmac_priv *priv, u32 queue)
2057 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2061 tx_q->queue_index = queue;
2062 tx_q->priv_data = priv;
2064 tx_q->tx_skbuff_dma = kcalloc(priv->dma_tx_size,
2065 sizeof(*tx_q->tx_skbuff_dma),
2067 if (!tx_q->tx_skbuff_dma)
2070 tx_q->tx_skbuff = kcalloc(priv->dma_tx_size,
2071 sizeof(struct sk_buff *),
2073 if (!tx_q->tx_skbuff)
2076 if (priv->extend_desc)
2077 size = sizeof(struct dma_extended_desc);
2078 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2079 size = sizeof(struct dma_edesc);
2081 size = sizeof(struct dma_desc);
2083 size *= priv->dma_tx_size;
2085 addr = dma_alloc_coherent(priv->device, size,
2086 &tx_q->dma_tx_phy, GFP_KERNEL);
2090 if (priv->extend_desc)
2091 tx_q->dma_etx = addr;
2092 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2093 tx_q->dma_entx = addr;
2095 tx_q->dma_tx = addr;
2100 static int alloc_dma_tx_desc_resources(struct stmmac_priv *priv)
2102 u32 tx_count = priv->plat->tx_queues_to_use;
2106 /* TX queues buffers and DMA */
2107 for (queue = 0; queue < tx_count; queue++) {
2108 ret = __alloc_dma_tx_desc_resources(priv, queue);
2116 free_dma_tx_desc_resources(priv);
2121 * alloc_dma_desc_resources - alloc TX/RX resources.
2122 * @priv: private structure
2123 * Description: according to which descriptor can be used (extend or basic)
2124 * this function allocates the resources for TX and RX paths. In case of
2125 * reception, for example, it pre-allocated the RX socket buffer in order to
2126 * allow zero-copy mechanism.
2128 static int alloc_dma_desc_resources(struct stmmac_priv *priv)
2131 int ret = alloc_dma_rx_desc_resources(priv);
2136 ret = alloc_dma_tx_desc_resources(priv);
2142 * free_dma_desc_resources - free dma desc resources
2143 * @priv: private structure
2145 static void free_dma_desc_resources(struct stmmac_priv *priv)
2147 /* Release the DMA TX socket buffers */
2148 free_dma_tx_desc_resources(priv);
2150 /* Release the DMA RX socket buffers later
2151 * to ensure all pending XDP_TX buffers are returned.
2153 free_dma_rx_desc_resources(priv);
2157 * stmmac_mac_enable_rx_queues - Enable MAC rx queues
2158 * @priv: driver private structure
2159 * Description: It is used for enabling the rx queues in the MAC
2161 static void stmmac_mac_enable_rx_queues(struct stmmac_priv *priv)
2163 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2167 for (queue = 0; queue < rx_queues_count; queue++) {
2168 mode = priv->plat->rx_queues_cfg[queue].mode_to_use;
2169 stmmac_rx_queue_enable(priv, priv->hw, mode, queue);
2174 * stmmac_start_rx_dma - start RX DMA channel
2175 * @priv: driver private structure
2176 * @chan: RX channel index
2178 * This starts a RX DMA channel
2180 static void stmmac_start_rx_dma(struct stmmac_priv *priv, u32 chan)
2182 netdev_dbg(priv->dev, "DMA RX processes started in channel %d\n", chan);
2183 stmmac_start_rx(priv, priv->ioaddr, chan);
2187 * stmmac_start_tx_dma - start TX DMA channel
2188 * @priv: driver private structure
2189 * @chan: TX channel index
2191 * This starts a TX DMA channel
2193 static void stmmac_start_tx_dma(struct stmmac_priv *priv, u32 chan)
2195 netdev_dbg(priv->dev, "DMA TX processes started in channel %d\n", chan);
2196 stmmac_start_tx(priv, priv->ioaddr, chan);
2200 * stmmac_stop_rx_dma - stop RX DMA channel
2201 * @priv: driver private structure
2202 * @chan: RX channel index
2204 * This stops a RX DMA channel
2206 static void stmmac_stop_rx_dma(struct stmmac_priv *priv, u32 chan)
2208 netdev_dbg(priv->dev, "DMA RX processes stopped in channel %d\n", chan);
2209 stmmac_stop_rx(priv, priv->ioaddr, chan);
2213 * stmmac_stop_tx_dma - stop TX DMA channel
2214 * @priv: driver private structure
2215 * @chan: TX channel index
2217 * This stops a TX DMA channel
2219 static void stmmac_stop_tx_dma(struct stmmac_priv *priv, u32 chan)
2221 netdev_dbg(priv->dev, "DMA TX processes stopped in channel %d\n", chan);
2222 stmmac_stop_tx(priv, priv->ioaddr, chan);
2226 * stmmac_start_all_dma - start all RX and TX DMA channels
2227 * @priv: driver private structure
2229 * This starts all the RX and TX DMA channels
2231 static void stmmac_start_all_dma(struct stmmac_priv *priv)
2233 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2234 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2237 for (chan = 0; chan < rx_channels_count; chan++)
2238 stmmac_start_rx_dma(priv, chan);
2240 for (chan = 0; chan < tx_channels_count; chan++)
2241 stmmac_start_tx_dma(priv, chan);
2245 * stmmac_stop_all_dma - stop all RX and TX DMA channels
2246 * @priv: driver private structure
2248 * This stops the RX and TX DMA channels
2250 static void stmmac_stop_all_dma(struct stmmac_priv *priv)
2252 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2253 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2256 for (chan = 0; chan < rx_channels_count; chan++)
2257 stmmac_stop_rx_dma(priv, chan);
2259 for (chan = 0; chan < tx_channels_count; chan++)
2260 stmmac_stop_tx_dma(priv, chan);
2264 * stmmac_dma_operation_mode - HW DMA operation mode
2265 * @priv: driver private structure
2266 * Description: it is used for configuring the DMA operation mode register in
2267 * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
2269 static void stmmac_dma_operation_mode(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;
2273 int rxfifosz = priv->plat->rx_fifo_size;
2274 int txfifosz = priv->plat->tx_fifo_size;
2281 rxfifosz = priv->dma_cap.rx_fifo_size;
2283 txfifosz = priv->dma_cap.tx_fifo_size;
2285 /* Adjust for real per queue fifo size */
2286 rxfifosz /= rx_channels_count;
2287 txfifosz /= tx_channels_count;
2289 if (priv->plat->force_thresh_dma_mode) {
2292 } else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
2294 * In case of GMAC, SF mode can be enabled
2295 * to perform the TX COE in HW. This depends on:
2296 * 1) TX COE if actually supported
2297 * 2) There is no bugged Jumbo frame support
2298 * that needs to not insert csum in the TDES.
2300 txmode = SF_DMA_MODE;
2301 rxmode = SF_DMA_MODE;
2302 priv->xstats.threshold = SF_DMA_MODE;
2305 rxmode = SF_DMA_MODE;
2308 /* configure all channels */
2309 for (chan = 0; chan < rx_channels_count; chan++) {
2310 struct stmmac_rx_queue *rx_q = &priv->rx_queue[chan];
2313 qmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
2315 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan,
2318 if (rx_q->xsk_pool) {
2319 buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool);
2320 stmmac_set_dma_bfsize(priv, priv->ioaddr,
2324 stmmac_set_dma_bfsize(priv, priv->ioaddr,
2330 for (chan = 0; chan < tx_channels_count; chan++) {
2331 qmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
2333 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan,
2338 static bool stmmac_xdp_xmit_zc(struct stmmac_priv *priv, u32 queue, u32 budget)
2340 struct netdev_queue *nq = netdev_get_tx_queue(priv->dev, queue);
2341 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2342 struct xsk_buff_pool *pool = tx_q->xsk_pool;
2343 unsigned int entry = tx_q->cur_tx;
2344 struct dma_desc *tx_desc = NULL;
2345 struct xdp_desc xdp_desc;
2346 bool work_done = true;
2348 /* Avoids TX time-out as we are sharing with slow path */
2349 txq_trans_cond_update(nq);
2351 budget = min(budget, stmmac_tx_avail(priv, queue));
2353 while (budget-- > 0) {
2354 dma_addr_t dma_addr;
2357 /* We are sharing with slow path and stop XSK TX desc submission when
2358 * available TX ring is less than threshold.
2360 if (unlikely(stmmac_tx_avail(priv, queue) < STMMAC_TX_XSK_AVAIL) ||
2361 !netif_carrier_ok(priv->dev)) {
2366 if (!xsk_tx_peek_desc(pool, &xdp_desc))
2369 if (likely(priv->extend_desc))
2370 tx_desc = (struct dma_desc *)(tx_q->dma_etx + entry);
2371 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2372 tx_desc = &tx_q->dma_entx[entry].basic;
2374 tx_desc = tx_q->dma_tx + entry;
2376 dma_addr = xsk_buff_raw_get_dma(pool, xdp_desc.addr);
2377 xsk_buff_raw_dma_sync_for_device(pool, dma_addr, xdp_desc.len);
2379 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XSK_TX;
2381 /* To return XDP buffer to XSK pool, we simple call
2382 * xsk_tx_completed(), so we don't need to fill up
2385 tx_q->tx_skbuff_dma[entry].buf = 0;
2386 tx_q->xdpf[entry] = NULL;
2388 tx_q->tx_skbuff_dma[entry].map_as_page = false;
2389 tx_q->tx_skbuff_dma[entry].len = xdp_desc.len;
2390 tx_q->tx_skbuff_dma[entry].last_segment = true;
2391 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
2393 stmmac_set_desc_addr(priv, tx_desc, dma_addr);
2395 tx_q->tx_count_frames++;
2397 if (!priv->tx_coal_frames[queue])
2399 else if (tx_q->tx_count_frames % priv->tx_coal_frames[queue] == 0)
2405 tx_q->tx_count_frames = 0;
2406 stmmac_set_tx_ic(priv, tx_desc);
2407 priv->xstats.tx_set_ic_bit++;
2410 stmmac_prepare_tx_desc(priv, tx_desc, 1, xdp_desc.len,
2411 true, priv->mode, true, true,
2414 stmmac_enable_dma_transmission(priv, priv->ioaddr);
2416 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
2417 entry = tx_q->cur_tx;
2421 stmmac_flush_tx_descriptors(priv, queue);
2422 xsk_tx_release(pool);
2425 /* Return true if all of the 3 conditions are met
2426 * a) TX Budget is still available
2427 * b) work_done = true when XSK TX desc peek is empty (no more
2428 * pending XSK TX for transmission)
2430 return !!budget && work_done;
2433 static void stmmac_bump_dma_threshold(struct stmmac_priv *priv, u32 chan)
2435 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) && tc <= 256) {
2438 if (priv->plat->force_thresh_dma_mode)
2439 stmmac_set_dma_operation_mode(priv, tc, tc, chan);
2441 stmmac_set_dma_operation_mode(priv, tc, SF_DMA_MODE,
2444 priv->xstats.threshold = tc;
2449 * stmmac_tx_clean - to manage the transmission completion
2450 * @priv: driver private structure
2451 * @budget: napi budget limiting this functions packet handling
2452 * @queue: TX queue index
2453 * Description: it reclaims the transmit resources after transmission completes.
2455 static int stmmac_tx_clean(struct stmmac_priv *priv, int budget, u32 queue)
2457 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2458 unsigned int bytes_compl = 0, pkts_compl = 0;
2459 unsigned int entry, xmits = 0, count = 0;
2461 __netif_tx_lock_bh(netdev_get_tx_queue(priv->dev, queue));
2463 priv->xstats.tx_clean++;
2465 tx_q->xsk_frames_done = 0;
2467 entry = tx_q->dirty_tx;
2469 /* Try to clean all TX complete frame in 1 shot */
2470 while ((entry != tx_q->cur_tx) && count < priv->dma_tx_size) {
2471 struct xdp_frame *xdpf;
2472 struct sk_buff *skb;
2476 if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_TX ||
2477 tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_NDO) {
2478 xdpf = tx_q->xdpf[entry];
2480 } else if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_SKB) {
2482 skb = tx_q->tx_skbuff[entry];
2488 if (priv->extend_desc)
2489 p = (struct dma_desc *)(tx_q->dma_etx + entry);
2490 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2491 p = &tx_q->dma_entx[entry].basic;
2493 p = tx_q->dma_tx + entry;
2495 status = stmmac_tx_status(priv, &priv->dev->stats,
2496 &priv->xstats, p, priv->ioaddr);
2497 /* Check if the descriptor is owned by the DMA */
2498 if (unlikely(status & tx_dma_own))
2503 /* Make sure descriptor fields are read after reading
2508 /* Just consider the last segment and ...*/
2509 if (likely(!(status & tx_not_ls))) {
2510 /* ... verify the status error condition */
2511 if (unlikely(status & tx_err)) {
2512 priv->dev->stats.tx_errors++;
2513 if (unlikely(status & tx_err_bump_tc))
2514 stmmac_bump_dma_threshold(priv, queue);
2516 priv->dev->stats.tx_packets++;
2517 priv->xstats.tx_pkt_n++;
2518 priv->xstats.txq_stats[queue].tx_pkt_n++;
2521 stmmac_get_tx_hwtstamp(priv, p, skb);
2524 if (likely(tx_q->tx_skbuff_dma[entry].buf &&
2525 tx_q->tx_skbuff_dma[entry].buf_type != STMMAC_TXBUF_T_XDP_TX)) {
2526 if (tx_q->tx_skbuff_dma[entry].map_as_page)
2527 dma_unmap_page(priv->device,
2528 tx_q->tx_skbuff_dma[entry].buf,
2529 tx_q->tx_skbuff_dma[entry].len,
2532 dma_unmap_single(priv->device,
2533 tx_q->tx_skbuff_dma[entry].buf,
2534 tx_q->tx_skbuff_dma[entry].len,
2536 tx_q->tx_skbuff_dma[entry].buf = 0;
2537 tx_q->tx_skbuff_dma[entry].len = 0;
2538 tx_q->tx_skbuff_dma[entry].map_as_page = false;
2541 stmmac_clean_desc3(priv, tx_q, p);
2543 tx_q->tx_skbuff_dma[entry].last_segment = false;
2544 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
2547 tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_TX) {
2548 xdp_return_frame_rx_napi(xdpf);
2549 tx_q->xdpf[entry] = NULL;
2553 tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_NDO) {
2554 xdp_return_frame(xdpf);
2555 tx_q->xdpf[entry] = NULL;
2558 if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XSK_TX)
2559 tx_q->xsk_frames_done++;
2561 if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_SKB) {
2564 bytes_compl += skb->len;
2565 dev_consume_skb_any(skb);
2566 tx_q->tx_skbuff[entry] = NULL;
2570 stmmac_release_tx_desc(priv, p, priv->mode);
2572 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
2574 tx_q->dirty_tx = entry;
2576 netdev_tx_completed_queue(netdev_get_tx_queue(priv->dev, queue),
2577 pkts_compl, bytes_compl);
2579 if (unlikely(netif_tx_queue_stopped(netdev_get_tx_queue(priv->dev,
2581 stmmac_tx_avail(priv, queue) > STMMAC_TX_THRESH(priv)) {
2583 netif_dbg(priv, tx_done, priv->dev,
2584 "%s: restart transmit\n", __func__);
2585 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, queue));
2588 if (tx_q->xsk_pool) {
2591 if (tx_q->xsk_frames_done)
2592 xsk_tx_completed(tx_q->xsk_pool, tx_q->xsk_frames_done);
2594 if (xsk_uses_need_wakeup(tx_q->xsk_pool))
2595 xsk_set_tx_need_wakeup(tx_q->xsk_pool);
2597 /* For XSK TX, we try to send as many as possible.
2598 * If XSK work done (XSK TX desc empty and budget still
2599 * available), return "budget - 1" to reenable TX IRQ.
2600 * Else, return "budget" to make NAPI continue polling.
2602 work_done = stmmac_xdp_xmit_zc(priv, queue,
2603 STMMAC_XSK_TX_BUDGET_MAX);
2610 if (priv->eee_enabled && !priv->tx_path_in_lpi_mode &&
2611 priv->eee_sw_timer_en) {
2612 stmmac_enable_eee_mode(priv);
2613 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer));
2616 /* We still have pending packets, let's call for a new scheduling */
2617 if (tx_q->dirty_tx != tx_q->cur_tx)
2618 hrtimer_start(&tx_q->txtimer,
2619 STMMAC_COAL_TIMER(priv->tx_coal_timer[queue]),
2622 __netif_tx_unlock_bh(netdev_get_tx_queue(priv->dev, queue));
2624 /* Combine decisions from TX clean and XSK TX */
2625 return max(count, xmits);
2629 * stmmac_tx_err - to manage the tx error
2630 * @priv: driver private structure
2631 * @chan: channel index
2632 * Description: it cleans the descriptors and restarts the transmission
2633 * in case of transmission errors.
2635 static void stmmac_tx_err(struct stmmac_priv *priv, u32 chan)
2637 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2639 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, chan));
2641 stmmac_stop_tx_dma(priv, chan);
2642 dma_free_tx_skbufs(priv, chan);
2643 stmmac_clear_tx_descriptors(priv, chan);
2647 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, chan));
2648 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2649 tx_q->dma_tx_phy, chan);
2650 stmmac_start_tx_dma(priv, chan);
2652 priv->dev->stats.tx_errors++;
2653 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, chan));
2657 * stmmac_set_dma_operation_mode - Set DMA operation mode by channel
2658 * @priv: driver private structure
2659 * @txmode: TX operating mode
2660 * @rxmode: RX operating mode
2661 * @chan: channel index
2662 * Description: it is used for configuring of the DMA operation mode in
2663 * runtime in order to program the tx/rx DMA thresholds or Store-And-Forward
2666 static void stmmac_set_dma_operation_mode(struct stmmac_priv *priv, u32 txmode,
2667 u32 rxmode, u32 chan)
2669 u8 rxqmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
2670 u8 txqmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
2671 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2672 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2673 int rxfifosz = priv->plat->rx_fifo_size;
2674 int txfifosz = priv->plat->tx_fifo_size;
2677 rxfifosz = priv->dma_cap.rx_fifo_size;
2679 txfifosz = priv->dma_cap.tx_fifo_size;
2681 /* Adjust for real per queue fifo size */
2682 rxfifosz /= rx_channels_count;
2683 txfifosz /= tx_channels_count;
2685 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan, rxfifosz, rxqmode);
2686 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan, txfifosz, txqmode);
2689 static bool stmmac_safety_feat_interrupt(struct stmmac_priv *priv)
2693 ret = stmmac_safety_feat_irq_status(priv, priv->dev,
2694 priv->ioaddr, priv->dma_cap.asp, &priv->sstats);
2695 if (ret && (ret != -EINVAL)) {
2696 stmmac_global_err(priv);
2703 static int stmmac_napi_check(struct stmmac_priv *priv, u32 chan, u32 dir)
2705 int status = stmmac_dma_interrupt_status(priv, priv->ioaddr,
2706 &priv->xstats, chan, dir);
2707 struct stmmac_rx_queue *rx_q = &priv->rx_queue[chan];
2708 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2709 struct stmmac_channel *ch = &priv->channel[chan];
2710 struct napi_struct *rx_napi;
2711 struct napi_struct *tx_napi;
2712 unsigned long flags;
2714 rx_napi = rx_q->xsk_pool ? &ch->rxtx_napi : &ch->rx_napi;
2715 tx_napi = tx_q->xsk_pool ? &ch->rxtx_napi : &ch->tx_napi;
2717 if ((status & handle_rx) && (chan < priv->plat->rx_queues_to_use)) {
2718 if (napi_schedule_prep(rx_napi)) {
2719 spin_lock_irqsave(&ch->lock, flags);
2720 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
2721 spin_unlock_irqrestore(&ch->lock, flags);
2722 __napi_schedule(rx_napi);
2726 if ((status & handle_tx) && (chan < priv->plat->tx_queues_to_use)) {
2727 if (napi_schedule_prep(tx_napi)) {
2728 spin_lock_irqsave(&ch->lock, flags);
2729 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
2730 spin_unlock_irqrestore(&ch->lock, flags);
2731 __napi_schedule(tx_napi);
2739 * stmmac_dma_interrupt - DMA ISR
2740 * @priv: driver private structure
2741 * Description: this is the DMA ISR. It is called by the main ISR.
2742 * It calls the dwmac dma routine and schedule poll method in case of some
2745 static void stmmac_dma_interrupt(struct stmmac_priv *priv)
2747 u32 tx_channel_count = priv->plat->tx_queues_to_use;
2748 u32 rx_channel_count = priv->plat->rx_queues_to_use;
2749 u32 channels_to_check = tx_channel_count > rx_channel_count ?
2750 tx_channel_count : rx_channel_count;
2752 int status[max_t(u32, MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES)];
2754 /* Make sure we never check beyond our status buffer. */
2755 if (WARN_ON_ONCE(channels_to_check > ARRAY_SIZE(status)))
2756 channels_to_check = ARRAY_SIZE(status);
2758 for (chan = 0; chan < channels_to_check; chan++)
2759 status[chan] = stmmac_napi_check(priv, chan,
2762 for (chan = 0; chan < tx_channel_count; chan++) {
2763 if (unlikely(status[chan] & tx_hard_error_bump_tc)) {
2764 /* Try to bump up the dma threshold on this failure */
2765 stmmac_bump_dma_threshold(priv, chan);
2766 } else if (unlikely(status[chan] == tx_hard_error)) {
2767 stmmac_tx_err(priv, chan);
2773 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
2774 * @priv: driver private structure
2775 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
2777 static void stmmac_mmc_setup(struct stmmac_priv *priv)
2779 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
2780 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
2782 stmmac_mmc_intr_all_mask(priv, priv->mmcaddr);
2784 if (priv->dma_cap.rmon) {
2785 stmmac_mmc_ctrl(priv, priv->mmcaddr, mode);
2786 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
2788 netdev_info(priv->dev, "No MAC Management Counters available\n");
2792 * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
2793 * @priv: driver private structure
2795 * new GMAC chip generations have a new register to indicate the
2796 * presence of the optional feature/functions.
2797 * This can be also used to override the value passed through the
2798 * platform and necessary for old MAC10/100 and GMAC chips.
2800 static int stmmac_get_hw_features(struct stmmac_priv *priv)
2802 return stmmac_get_hw_feature(priv, priv->ioaddr, &priv->dma_cap) == 0;
2806 * stmmac_check_ether_addr - check if the MAC addr is valid
2807 * @priv: driver private structure
2809 * it is to verify if the MAC address is valid, in case of failures it
2810 * generates a random MAC address
2812 static void stmmac_check_ether_addr(struct stmmac_priv *priv)
2816 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
2817 stmmac_get_umac_addr(priv, priv->hw, addr, 0);
2818 if (is_valid_ether_addr(addr))
2819 eth_hw_addr_set(priv->dev, addr);
2821 eth_hw_addr_random(priv->dev);
2822 dev_info(priv->device, "device MAC address %pM\n",
2823 priv->dev->dev_addr);
2828 * stmmac_init_dma_engine - DMA init.
2829 * @priv: driver private structure
2831 * It inits the DMA invoking the specific MAC/GMAC callback.
2832 * Some DMA parameters can be passed from the platform;
2833 * in case of these are not passed a default is kept for the MAC or GMAC.
2835 static int stmmac_init_dma_engine(struct stmmac_priv *priv)
2837 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2838 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2839 u32 dma_csr_ch = max(rx_channels_count, tx_channels_count);
2840 struct stmmac_rx_queue *rx_q;
2841 struct stmmac_tx_queue *tx_q;
2846 if (!priv->plat->dma_cfg || !priv->plat->dma_cfg->pbl) {
2847 dev_err(priv->device, "Invalid DMA configuration\n");
2851 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
2854 ret = stmmac_reset(priv, priv->ioaddr);
2856 dev_err(priv->device, "Failed to reset the dma\n");
2860 /* DMA Configuration */
2861 stmmac_dma_init(priv, priv->ioaddr, priv->plat->dma_cfg, atds);
2863 if (priv->plat->axi)
2864 stmmac_axi(priv, priv->ioaddr, priv->plat->axi);
2866 /* DMA CSR Channel configuration */
2867 for (chan = 0; chan < dma_csr_ch; chan++)
2868 stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan);
2870 /* DMA RX Channel Configuration */
2871 for (chan = 0; chan < rx_channels_count; chan++) {
2872 rx_q = &priv->rx_queue[chan];
2874 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2875 rx_q->dma_rx_phy, chan);
2877 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
2878 (rx_q->buf_alloc_num *
2879 sizeof(struct dma_desc));
2880 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
2881 rx_q->rx_tail_addr, chan);
2884 /* DMA TX Channel Configuration */
2885 for (chan = 0; chan < tx_channels_count; chan++) {
2886 tx_q = &priv->tx_queue[chan];
2888 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2889 tx_q->dma_tx_phy, chan);
2891 tx_q->tx_tail_addr = tx_q->dma_tx_phy;
2892 stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
2893 tx_q->tx_tail_addr, chan);
2899 static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue)
2901 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2903 hrtimer_start(&tx_q->txtimer,
2904 STMMAC_COAL_TIMER(priv->tx_coal_timer[queue]),
2909 * stmmac_tx_timer - mitigation sw timer for tx.
2912 * This is the timer handler to directly invoke the stmmac_tx_clean.
2914 static enum hrtimer_restart stmmac_tx_timer(struct hrtimer *t)
2916 struct stmmac_tx_queue *tx_q = container_of(t, struct stmmac_tx_queue, txtimer);
2917 struct stmmac_priv *priv = tx_q->priv_data;
2918 struct stmmac_channel *ch;
2919 struct napi_struct *napi;
2921 ch = &priv->channel[tx_q->queue_index];
2922 napi = tx_q->xsk_pool ? &ch->rxtx_napi : &ch->tx_napi;
2924 if (likely(napi_schedule_prep(napi))) {
2925 unsigned long flags;
2927 spin_lock_irqsave(&ch->lock, flags);
2928 stmmac_disable_dma_irq(priv, priv->ioaddr, ch->index, 0, 1);
2929 spin_unlock_irqrestore(&ch->lock, flags);
2930 __napi_schedule(napi);
2933 return HRTIMER_NORESTART;
2937 * stmmac_init_coalesce - init mitigation options.
2938 * @priv: driver private structure
2940 * This inits the coalesce parameters: i.e. timer rate,
2941 * timer handler and default threshold used for enabling the
2942 * interrupt on completion bit.
2944 static void stmmac_init_coalesce(struct stmmac_priv *priv)
2946 u32 tx_channel_count = priv->plat->tx_queues_to_use;
2947 u32 rx_channel_count = priv->plat->rx_queues_to_use;
2950 for (chan = 0; chan < tx_channel_count; chan++) {
2951 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2953 priv->tx_coal_frames[chan] = STMMAC_TX_FRAMES;
2954 priv->tx_coal_timer[chan] = STMMAC_COAL_TX_TIMER;
2956 hrtimer_init(&tx_q->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2957 tx_q->txtimer.function = stmmac_tx_timer;
2960 for (chan = 0; chan < rx_channel_count; chan++)
2961 priv->rx_coal_frames[chan] = STMMAC_RX_FRAMES;
2964 static void stmmac_set_rings_length(struct stmmac_priv *priv)
2966 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2967 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2970 /* set TX ring length */
2971 for (chan = 0; chan < tx_channels_count; chan++)
2972 stmmac_set_tx_ring_len(priv, priv->ioaddr,
2973 (priv->dma_tx_size - 1), chan);
2975 /* set RX ring length */
2976 for (chan = 0; chan < rx_channels_count; chan++)
2977 stmmac_set_rx_ring_len(priv, priv->ioaddr,
2978 (priv->dma_rx_size - 1), chan);
2982 * stmmac_set_tx_queue_weight - Set TX queue weight
2983 * @priv: driver private structure
2984 * Description: It is used for setting TX queues weight
2986 static void stmmac_set_tx_queue_weight(struct stmmac_priv *priv)
2988 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2992 for (queue = 0; queue < tx_queues_count; queue++) {
2993 weight = priv->plat->tx_queues_cfg[queue].weight;
2994 stmmac_set_mtl_tx_queue_weight(priv, priv->hw, weight, queue);
2999 * stmmac_configure_cbs - Configure CBS in TX queue
3000 * @priv: driver private structure
3001 * Description: It is used for configuring CBS in AVB TX queues
3003 static void stmmac_configure_cbs(struct stmmac_priv *priv)
3005 u32 tx_queues_count = priv->plat->tx_queues_to_use;
3009 /* queue 0 is reserved for legacy traffic */
3010 for (queue = 1; queue < tx_queues_count; queue++) {
3011 mode_to_use = priv->plat->tx_queues_cfg[queue].mode_to_use;
3012 if (mode_to_use == MTL_QUEUE_DCB)
3015 stmmac_config_cbs(priv, priv->hw,
3016 priv->plat->tx_queues_cfg[queue].send_slope,
3017 priv->plat->tx_queues_cfg[queue].idle_slope,
3018 priv->plat->tx_queues_cfg[queue].high_credit,
3019 priv->plat->tx_queues_cfg[queue].low_credit,
3025 * stmmac_rx_queue_dma_chan_map - Map RX queue to RX dma channel
3026 * @priv: driver private structure
3027 * Description: It is used for mapping RX queues to RX dma channels
3029 static void stmmac_rx_queue_dma_chan_map(struct stmmac_priv *priv)
3031 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3035 for (queue = 0; queue < rx_queues_count; queue++) {
3036 chan = priv->plat->rx_queues_cfg[queue].chan;
3037 stmmac_map_mtl_to_dma(priv, priv->hw, queue, chan);
3042 * stmmac_mac_config_rx_queues_prio - Configure RX Queue priority
3043 * @priv: driver private structure
3044 * Description: It is used for configuring the RX Queue Priority
3046 static void stmmac_mac_config_rx_queues_prio(struct stmmac_priv *priv)
3048 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3052 for (queue = 0; queue < rx_queues_count; queue++) {
3053 if (!priv->plat->rx_queues_cfg[queue].use_prio)
3056 prio = priv->plat->rx_queues_cfg[queue].prio;
3057 stmmac_rx_queue_prio(priv, priv->hw, prio, queue);
3062 * stmmac_mac_config_tx_queues_prio - Configure TX Queue priority
3063 * @priv: driver private structure
3064 * Description: It is used for configuring the TX Queue Priority
3066 static void stmmac_mac_config_tx_queues_prio(struct stmmac_priv *priv)
3068 u32 tx_queues_count = priv->plat->tx_queues_to_use;
3072 for (queue = 0; queue < tx_queues_count; queue++) {
3073 if (!priv->plat->tx_queues_cfg[queue].use_prio)
3076 prio = priv->plat->tx_queues_cfg[queue].prio;
3077 stmmac_tx_queue_prio(priv, priv->hw, prio, queue);
3082 * stmmac_mac_config_rx_queues_routing - Configure RX Queue Routing
3083 * @priv: driver private structure
3084 * Description: It is used for configuring the RX queue routing
3086 static void stmmac_mac_config_rx_queues_routing(struct stmmac_priv *priv)
3088 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3092 for (queue = 0; queue < rx_queues_count; queue++) {
3093 /* no specific packet type routing specified for the queue */
3094 if (priv->plat->rx_queues_cfg[queue].pkt_route == 0x0)
3097 packet = priv->plat->rx_queues_cfg[queue].pkt_route;
3098 stmmac_rx_queue_routing(priv, priv->hw, packet, queue);
3102 static void stmmac_mac_config_rss(struct stmmac_priv *priv)
3104 if (!priv->dma_cap.rssen || !priv->plat->rss_en) {
3105 priv->rss.enable = false;
3109 if (priv->dev->features & NETIF_F_RXHASH)
3110 priv->rss.enable = true;
3112 priv->rss.enable = false;
3114 stmmac_rss_configure(priv, priv->hw, &priv->rss,
3115 priv->plat->rx_queues_to_use);
3119 * stmmac_mtl_configuration - Configure MTL
3120 * @priv: driver private structure
3121 * Description: It is used for configurring MTL
3123 static void stmmac_mtl_configuration(struct stmmac_priv *priv)
3125 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3126 u32 tx_queues_count = priv->plat->tx_queues_to_use;
3128 if (tx_queues_count > 1)
3129 stmmac_set_tx_queue_weight(priv);
3131 /* Configure MTL RX algorithms */
3132 if (rx_queues_count > 1)
3133 stmmac_prog_mtl_rx_algorithms(priv, priv->hw,
3134 priv->plat->rx_sched_algorithm);
3136 /* Configure MTL TX algorithms */
3137 if (tx_queues_count > 1)
3138 stmmac_prog_mtl_tx_algorithms(priv, priv->hw,
3139 priv->plat->tx_sched_algorithm);
3141 /* Configure CBS in AVB TX queues */
3142 if (tx_queues_count > 1)
3143 stmmac_configure_cbs(priv);
3145 /* Map RX MTL to DMA channels */
3146 stmmac_rx_queue_dma_chan_map(priv);
3148 /* Enable MAC RX Queues */
3149 stmmac_mac_enable_rx_queues(priv);
3151 /* Set RX priorities */
3152 if (rx_queues_count > 1)
3153 stmmac_mac_config_rx_queues_prio(priv);
3155 /* Set TX priorities */
3156 if (tx_queues_count > 1)
3157 stmmac_mac_config_tx_queues_prio(priv);
3159 /* Set RX routing */
3160 if (rx_queues_count > 1)
3161 stmmac_mac_config_rx_queues_routing(priv);
3163 /* Receive Side Scaling */
3164 if (rx_queues_count > 1)
3165 stmmac_mac_config_rss(priv);
3168 static void stmmac_safety_feat_configuration(struct stmmac_priv *priv)
3170 if (priv->dma_cap.asp) {
3171 netdev_info(priv->dev, "Enabling Safety Features\n");
3172 stmmac_safety_feat_config(priv, priv->ioaddr, priv->dma_cap.asp,
3173 priv->plat->safety_feat_cfg);
3175 netdev_info(priv->dev, "No Safety Features support found\n");
3179 static int stmmac_fpe_start_wq(struct stmmac_priv *priv)
3183 clear_bit(__FPE_TASK_SCHED, &priv->fpe_task_state);
3184 clear_bit(__FPE_REMOVING, &priv->fpe_task_state);
3186 name = priv->wq_name;
3187 sprintf(name, "%s-fpe", priv->dev->name);
3189 priv->fpe_wq = create_singlethread_workqueue(name);
3190 if (!priv->fpe_wq) {
3191 netdev_err(priv->dev, "%s: Failed to create workqueue\n", name);
3195 netdev_info(priv->dev, "FPE workqueue start");
3201 * stmmac_hw_setup - setup mac in a usable state.
3202 * @dev : pointer to the device structure.
3203 * @init_ptp: initialize PTP if set
3205 * this is the main function to setup the HW in a usable state because the
3206 * dma engine is reset, the core registers are configured (e.g. AXI,
3207 * Checksum features, timers). The DMA is ready to start receiving and
3210 * 0 on success and an appropriate (-)ve integer as defined in errno.h
3213 static int stmmac_hw_setup(struct net_device *dev, bool init_ptp)
3215 struct stmmac_priv *priv = netdev_priv(dev);
3216 u32 rx_cnt = priv->plat->rx_queues_to_use;
3217 u32 tx_cnt = priv->plat->tx_queues_to_use;
3222 /* DMA initialization and SW reset */
3223 ret = stmmac_init_dma_engine(priv);
3225 netdev_err(priv->dev, "%s: DMA engine initialization failed\n",
3230 /* Copy the MAC addr into the HW */
3231 stmmac_set_umac_addr(priv, priv->hw, dev->dev_addr, 0);
3233 /* PS and related bits will be programmed according to the speed */
3234 if (priv->hw->pcs) {
3235 int speed = priv->plat->mac_port_sel_speed;
3237 if ((speed == SPEED_10) || (speed == SPEED_100) ||
3238 (speed == SPEED_1000)) {
3239 priv->hw->ps = speed;
3241 dev_warn(priv->device, "invalid port speed\n");
3246 /* Initialize the MAC Core */
3247 stmmac_core_init(priv, priv->hw, dev);
3250 stmmac_mtl_configuration(priv);
3252 /* Initialize Safety Features */
3253 stmmac_safety_feat_configuration(priv);
3255 ret = stmmac_rx_ipc(priv, priv->hw);
3257 netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n");
3258 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
3259 priv->hw->rx_csum = 0;
3262 /* Enable the MAC Rx/Tx */
3263 stmmac_mac_set(priv, priv->ioaddr, true);
3265 /* Set the HW DMA mode and the COE */
3266 stmmac_dma_operation_mode(priv);
3268 stmmac_mmc_setup(priv);
3271 ret = stmmac_init_ptp(priv);
3272 if (ret == -EOPNOTSUPP)
3273 netdev_warn(priv->dev, "PTP not supported by HW\n");
3275 netdev_warn(priv->dev, "PTP init failed\n");
3278 priv->eee_tw_timer = STMMAC_DEFAULT_TWT_LS;
3280 /* Convert the timer from msec to usec */
3281 if (!priv->tx_lpi_timer)
3282 priv->tx_lpi_timer = eee_timer * 1000;
3284 if (priv->use_riwt) {
3287 for (queue = 0; queue < rx_cnt; queue++) {
3288 if (!priv->rx_riwt[queue])
3289 priv->rx_riwt[queue] = DEF_DMA_RIWT;
3291 stmmac_rx_watchdog(priv, priv->ioaddr,
3292 priv->rx_riwt[queue], queue);
3297 stmmac_pcs_ctrl_ane(priv, priv->ioaddr, 1, priv->hw->ps, 0);
3299 /* set TX and RX rings length */
3300 stmmac_set_rings_length(priv);
3304 for (chan = 0; chan < tx_cnt; chan++) {
3305 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
3307 /* TSO and TBS cannot co-exist */
3308 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3311 stmmac_enable_tso(priv, priv->ioaddr, 1, chan);
3315 /* Enable Split Header */
3316 sph_en = (priv->hw->rx_csum > 0) && priv->sph;
3317 for (chan = 0; chan < rx_cnt; chan++)
3318 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
3321 /* VLAN Tag Insertion */
3322 if (priv->dma_cap.vlins)
3323 stmmac_enable_vlan(priv, priv->hw, STMMAC_VLAN_INSERT);
3326 for (chan = 0; chan < tx_cnt; chan++) {
3327 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
3328 int enable = tx_q->tbs & STMMAC_TBS_AVAIL;
3330 stmmac_enable_tbs(priv, priv->ioaddr, enable, chan);
3333 /* Configure real RX and TX queues */
3334 netif_set_real_num_rx_queues(dev, priv->plat->rx_queues_to_use);
3335 netif_set_real_num_tx_queues(dev, priv->plat->tx_queues_to_use);
3337 /* Start the ball rolling... */
3338 stmmac_start_all_dma(priv);
3340 if (priv->dma_cap.fpesel) {
3341 stmmac_fpe_start_wq(priv);
3343 if (priv->plat->fpe_cfg->enable)
3344 stmmac_fpe_handshake(priv, true);
3350 static void stmmac_hw_teardown(struct net_device *dev)
3352 struct stmmac_priv *priv = netdev_priv(dev);
3354 clk_disable_unprepare(priv->plat->clk_ptp_ref);
3357 static void stmmac_free_irq(struct net_device *dev,
3358 enum request_irq_err irq_err, int irq_idx)
3360 struct stmmac_priv *priv = netdev_priv(dev);
3364 case REQ_IRQ_ERR_ALL:
3365 irq_idx = priv->plat->tx_queues_to_use;
3367 case REQ_IRQ_ERR_TX:
3368 for (j = irq_idx - 1; j >= 0; j--) {
3369 if (priv->tx_irq[j] > 0) {
3370 irq_set_affinity_hint(priv->tx_irq[j], NULL);
3371 free_irq(priv->tx_irq[j], &priv->tx_queue[j]);
3374 irq_idx = priv->plat->rx_queues_to_use;
3376 case REQ_IRQ_ERR_RX:
3377 for (j = irq_idx - 1; j >= 0; j--) {
3378 if (priv->rx_irq[j] > 0) {
3379 irq_set_affinity_hint(priv->rx_irq[j], NULL);
3380 free_irq(priv->rx_irq[j], &priv->rx_queue[j]);
3384 if (priv->sfty_ue_irq > 0 && priv->sfty_ue_irq != dev->irq)
3385 free_irq(priv->sfty_ue_irq, dev);
3387 case REQ_IRQ_ERR_SFTY_UE:
3388 if (priv->sfty_ce_irq > 0 && priv->sfty_ce_irq != dev->irq)
3389 free_irq(priv->sfty_ce_irq, dev);
3391 case REQ_IRQ_ERR_SFTY_CE:
3392 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq)
3393 free_irq(priv->lpi_irq, dev);
3395 case REQ_IRQ_ERR_LPI:
3396 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq)
3397 free_irq(priv->wol_irq, dev);
3399 case REQ_IRQ_ERR_WOL:
3400 free_irq(dev->irq, dev);
3402 case REQ_IRQ_ERR_MAC:
3403 case REQ_IRQ_ERR_NO:
3404 /* If MAC IRQ request error, no more IRQ to free */
3409 static int stmmac_request_irq_multi_msi(struct net_device *dev)
3411 struct stmmac_priv *priv = netdev_priv(dev);
3412 enum request_irq_err irq_err;
3419 /* For common interrupt */
3420 int_name = priv->int_name_mac;
3421 sprintf(int_name, "%s:%s", dev->name, "mac");
3422 ret = request_irq(dev->irq, stmmac_mac_interrupt,
3424 if (unlikely(ret < 0)) {
3425 netdev_err(priv->dev,
3426 "%s: alloc mac MSI %d (error: %d)\n",
3427 __func__, dev->irq, ret);
3428 irq_err = REQ_IRQ_ERR_MAC;
3432 /* Request the Wake IRQ in case of another line
3435 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq) {
3436 int_name = priv->int_name_wol;
3437 sprintf(int_name, "%s:%s", dev->name, "wol");
3438 ret = request_irq(priv->wol_irq,
3439 stmmac_mac_interrupt,
3441 if (unlikely(ret < 0)) {
3442 netdev_err(priv->dev,
3443 "%s: alloc wol MSI %d (error: %d)\n",
3444 __func__, priv->wol_irq, ret);
3445 irq_err = REQ_IRQ_ERR_WOL;
3450 /* Request the LPI IRQ in case of another line
3453 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq) {
3454 int_name = priv->int_name_lpi;
3455 sprintf(int_name, "%s:%s", dev->name, "lpi");
3456 ret = request_irq(priv->lpi_irq,
3457 stmmac_mac_interrupt,
3459 if (unlikely(ret < 0)) {
3460 netdev_err(priv->dev,
3461 "%s: alloc lpi MSI %d (error: %d)\n",
3462 __func__, priv->lpi_irq, ret);
3463 irq_err = REQ_IRQ_ERR_LPI;
3468 /* Request the Safety Feature Correctible Error line in
3469 * case of another line is used
3471 if (priv->sfty_ce_irq > 0 && priv->sfty_ce_irq != dev->irq) {
3472 int_name = priv->int_name_sfty_ce;
3473 sprintf(int_name, "%s:%s", dev->name, "safety-ce");
3474 ret = request_irq(priv->sfty_ce_irq,
3475 stmmac_safety_interrupt,
3477 if (unlikely(ret < 0)) {
3478 netdev_err(priv->dev,
3479 "%s: alloc sfty ce MSI %d (error: %d)\n",
3480 __func__, priv->sfty_ce_irq, ret);
3481 irq_err = REQ_IRQ_ERR_SFTY_CE;
3486 /* Request the Safety Feature Uncorrectible Error line in
3487 * case of another line is used
3489 if (priv->sfty_ue_irq > 0 && priv->sfty_ue_irq != dev->irq) {
3490 int_name = priv->int_name_sfty_ue;
3491 sprintf(int_name, "%s:%s", dev->name, "safety-ue");
3492 ret = request_irq(priv->sfty_ue_irq,
3493 stmmac_safety_interrupt,
3495 if (unlikely(ret < 0)) {
3496 netdev_err(priv->dev,
3497 "%s: alloc sfty ue MSI %d (error: %d)\n",
3498 __func__, priv->sfty_ue_irq, ret);
3499 irq_err = REQ_IRQ_ERR_SFTY_UE;
3504 /* Request Rx MSI irq */
3505 for (i = 0; i < priv->plat->rx_queues_to_use; i++) {
3506 if (i >= MTL_MAX_RX_QUEUES)
3508 if (priv->rx_irq[i] == 0)
3511 int_name = priv->int_name_rx_irq[i];
3512 sprintf(int_name, "%s:%s-%d", dev->name, "rx", i);
3513 ret = request_irq(priv->rx_irq[i],
3515 0, int_name, &priv->rx_queue[i]);
3516 if (unlikely(ret < 0)) {
3517 netdev_err(priv->dev,
3518 "%s: alloc rx-%d MSI %d (error: %d)\n",
3519 __func__, i, priv->rx_irq[i], ret);
3520 irq_err = REQ_IRQ_ERR_RX;
3524 cpumask_clear(&cpu_mask);
3525 cpumask_set_cpu(i % num_online_cpus(), &cpu_mask);
3526 irq_set_affinity_hint(priv->rx_irq[i], &cpu_mask);
3529 /* Request Tx MSI irq */
3530 for (i = 0; i < priv->plat->tx_queues_to_use; i++) {
3531 if (i >= MTL_MAX_TX_QUEUES)
3533 if (priv->tx_irq[i] == 0)
3536 int_name = priv->int_name_tx_irq[i];
3537 sprintf(int_name, "%s:%s-%d", dev->name, "tx", i);
3538 ret = request_irq(priv->tx_irq[i],
3540 0, int_name, &priv->tx_queue[i]);
3541 if (unlikely(ret < 0)) {
3542 netdev_err(priv->dev,
3543 "%s: alloc tx-%d MSI %d (error: %d)\n",
3544 __func__, i, priv->tx_irq[i], ret);
3545 irq_err = REQ_IRQ_ERR_TX;
3549 cpumask_clear(&cpu_mask);
3550 cpumask_set_cpu(i % num_online_cpus(), &cpu_mask);
3551 irq_set_affinity_hint(priv->tx_irq[i], &cpu_mask);
3557 stmmac_free_irq(dev, irq_err, irq_idx);
3561 static int stmmac_request_irq_single(struct net_device *dev)
3563 struct stmmac_priv *priv = netdev_priv(dev);
3564 enum request_irq_err irq_err;
3567 ret = request_irq(dev->irq, stmmac_interrupt,
3568 IRQF_SHARED, dev->name, dev);
3569 if (unlikely(ret < 0)) {
3570 netdev_err(priv->dev,
3571 "%s: ERROR: allocating the IRQ %d (error: %d)\n",
3572 __func__, dev->irq, ret);
3573 irq_err = REQ_IRQ_ERR_MAC;
3577 /* Request the Wake IRQ in case of another line
3580 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq) {
3581 ret = request_irq(priv->wol_irq, stmmac_interrupt,
3582 IRQF_SHARED, dev->name, dev);
3583 if (unlikely(ret < 0)) {
3584 netdev_err(priv->dev,
3585 "%s: ERROR: allocating the WoL IRQ %d (%d)\n",
3586 __func__, priv->wol_irq, ret);
3587 irq_err = REQ_IRQ_ERR_WOL;
3592 /* Request the IRQ lines */
3593 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq) {
3594 ret = request_irq(priv->lpi_irq, stmmac_interrupt,
3595 IRQF_SHARED, dev->name, dev);
3596 if (unlikely(ret < 0)) {
3597 netdev_err(priv->dev,
3598 "%s: ERROR: allocating the LPI IRQ %d (%d)\n",
3599 __func__, priv->lpi_irq, ret);
3600 irq_err = REQ_IRQ_ERR_LPI;
3608 stmmac_free_irq(dev, irq_err, 0);
3612 static int stmmac_request_irq(struct net_device *dev)
3614 struct stmmac_priv *priv = netdev_priv(dev);
3617 /* Request the IRQ lines */
3618 if (priv->plat->multi_msi_en)
3619 ret = stmmac_request_irq_multi_msi(dev);
3621 ret = stmmac_request_irq_single(dev);
3627 * stmmac_open - open entry point of the driver
3628 * @dev : pointer to the device structure.
3630 * This function is the open entry point of the driver.
3632 * 0 on success and an appropriate (-)ve integer as defined in errno.h
3635 static int stmmac_open(struct net_device *dev)
3637 struct stmmac_priv *priv = netdev_priv(dev);
3638 int mode = priv->plat->phy_interface;
3643 ret = pm_runtime_get_sync(priv->device);
3645 pm_runtime_put_noidle(priv->device);
3649 if (priv->hw->pcs != STMMAC_PCS_TBI &&
3650 priv->hw->pcs != STMMAC_PCS_RTBI &&
3652 xpcs_get_an_mode(priv->hw->xpcs, mode) != DW_AN_C73)) {
3653 ret = stmmac_init_phy(dev);
3655 netdev_err(priv->dev,
3656 "%s: Cannot attach to PHY (error: %d)\n",
3658 goto init_phy_error;
3662 /* Extra statistics */
3663 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
3664 priv->xstats.threshold = tc;
3666 bfsize = stmmac_set_16kib_bfsize(priv, dev->mtu);
3670 if (bfsize < BUF_SIZE_16KiB)
3671 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
3673 priv->dma_buf_sz = bfsize;
3676 priv->rx_copybreak = STMMAC_RX_COPYBREAK;
3678 if (!priv->dma_tx_size)
3679 priv->dma_tx_size = DMA_DEFAULT_TX_SIZE;
3680 if (!priv->dma_rx_size)
3681 priv->dma_rx_size = DMA_DEFAULT_RX_SIZE;
3683 /* Earlier check for TBS */
3684 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++) {
3685 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
3686 int tbs_en = priv->plat->tx_queues_cfg[chan].tbs_en;
3688 /* Setup per-TXQ tbs flag before TX descriptor alloc */
3689 tx_q->tbs |= tbs_en ? STMMAC_TBS_AVAIL : 0;
3692 ret = alloc_dma_desc_resources(priv);
3694 netdev_err(priv->dev, "%s: DMA descriptors allocation failed\n",
3696 goto dma_desc_error;
3699 ret = init_dma_desc_rings(dev, GFP_KERNEL);
3701 netdev_err(priv->dev, "%s: DMA descriptors initialization failed\n",
3706 ret = stmmac_hw_setup(dev, true);
3708 netdev_err(priv->dev, "%s: Hw setup failed\n", __func__);
3712 stmmac_init_coalesce(priv);
3714 phylink_start(priv->phylink);
3715 /* We may have called phylink_speed_down before */
3716 phylink_speed_up(priv->phylink);
3718 ret = stmmac_request_irq(dev);
3722 stmmac_enable_all_queues(priv);
3723 netif_tx_start_all_queues(priv->dev);
3728 phylink_stop(priv->phylink);
3730 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
3731 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
3733 stmmac_hw_teardown(dev);
3735 free_dma_desc_resources(priv);
3737 phylink_disconnect_phy(priv->phylink);
3739 pm_runtime_put(priv->device);
3743 static void stmmac_fpe_stop_wq(struct stmmac_priv *priv)
3745 set_bit(__FPE_REMOVING, &priv->fpe_task_state);
3748 destroy_workqueue(priv->fpe_wq);
3750 netdev_info(priv->dev, "FPE workqueue stop");
3754 * stmmac_release - close entry point of the driver
3755 * @dev : device pointer.
3757 * This is the stop entry point of the driver.
3759 static int stmmac_release(struct net_device *dev)
3761 struct stmmac_priv *priv = netdev_priv(dev);
3764 netif_tx_disable(dev);
3766 if (device_may_wakeup(priv->device))
3767 phylink_speed_down(priv->phylink, false);
3768 /* Stop and disconnect the PHY */
3769 phylink_stop(priv->phylink);
3770 phylink_disconnect_phy(priv->phylink);
3772 stmmac_disable_all_queues(priv);
3774 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
3775 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
3777 /* Free the IRQ lines */
3778 stmmac_free_irq(dev, REQ_IRQ_ERR_ALL, 0);
3780 if (priv->eee_enabled) {
3781 priv->tx_path_in_lpi_mode = false;
3782 del_timer_sync(&priv->eee_ctrl_timer);
3785 /* Stop TX/RX DMA and clear the descriptors */
3786 stmmac_stop_all_dma(priv);
3788 /* Release and free the Rx/Tx resources */
3789 free_dma_desc_resources(priv);
3791 /* Disable the MAC Rx/Tx */
3792 stmmac_mac_set(priv, priv->ioaddr, false);
3794 netif_carrier_off(dev);
3796 stmmac_release_ptp(priv);
3798 pm_runtime_put(priv->device);
3800 if (priv->dma_cap.fpesel)
3801 stmmac_fpe_stop_wq(priv);
3806 static bool stmmac_vlan_insert(struct stmmac_priv *priv, struct sk_buff *skb,
3807 struct stmmac_tx_queue *tx_q)
3809 u16 tag = 0x0, inner_tag = 0x0;
3810 u32 inner_type = 0x0;
3813 if (!priv->dma_cap.vlins)
3815 if (!skb_vlan_tag_present(skb))
3817 if (skb->vlan_proto == htons(ETH_P_8021AD)) {
3818 inner_tag = skb_vlan_tag_get(skb);
3819 inner_type = STMMAC_VLAN_INSERT;
3822 tag = skb_vlan_tag_get(skb);
3824 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3825 p = &tx_q->dma_entx[tx_q->cur_tx].basic;
3827 p = &tx_q->dma_tx[tx_q->cur_tx];
3829 if (stmmac_set_desc_vlan_tag(priv, p, tag, inner_tag, inner_type))
3832 stmmac_set_tx_owner(priv, p);
3833 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
3838 * stmmac_tso_allocator - close entry point of the driver
3839 * @priv: driver private structure
3840 * @des: buffer start address
3841 * @total_len: total length to fill in descriptors
3842 * @last_segment: condition for the last descriptor
3843 * @queue: TX queue index
3845 * This function fills descriptor and request new descriptors according to
3846 * buffer length to fill
3848 static void stmmac_tso_allocator(struct stmmac_priv *priv, dma_addr_t des,
3849 int total_len, bool last_segment, u32 queue)
3851 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
3852 struct dma_desc *desc;
3856 tmp_len = total_len;
3858 while (tmp_len > 0) {
3859 dma_addr_t curr_addr;
3861 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx,
3863 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
3865 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3866 desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
3868 desc = &tx_q->dma_tx[tx_q->cur_tx];
3870 curr_addr = des + (total_len - tmp_len);
3871 if (priv->dma_cap.addr64 <= 32)
3872 desc->des0 = cpu_to_le32(curr_addr);
3874 stmmac_set_desc_addr(priv, desc, curr_addr);
3876 buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ?
3877 TSO_MAX_BUFF_SIZE : tmp_len;
3879 stmmac_prepare_tso_tx_desc(priv, desc, 0, buff_size,
3881 (last_segment) && (tmp_len <= TSO_MAX_BUFF_SIZE),
3884 tmp_len -= TSO_MAX_BUFF_SIZE;
3888 static void stmmac_flush_tx_descriptors(struct stmmac_priv *priv, int queue)
3890 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
3893 if (likely(priv->extend_desc))
3894 desc_size = sizeof(struct dma_extended_desc);
3895 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
3896 desc_size = sizeof(struct dma_edesc);
3898 desc_size = sizeof(struct dma_desc);
3900 /* The own bit must be the latest setting done when prepare the
3901 * descriptor and then barrier is needed to make sure that
3902 * all is coherent before granting the DMA engine.
3906 tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * desc_size);
3907 stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue);
3911 * stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO)
3912 * @skb : the socket buffer
3913 * @dev : device pointer
3914 * Description: this is the transmit function that is called on TSO frames
3915 * (support available on GMAC4 and newer chips).
3916 * Diagram below show the ring programming in case of TSO frames:
3920 * | DES0 |---> buffer1 = L2/L3/L4 header
3921 * | DES1 |---> TCP Payload (can continue on next descr...)
3922 * | DES2 |---> buffer 1 and 2 len
3923 * | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0]
3929 * | DES0 | --| Split TCP Payload on Buffers 1 and 2
3931 * | DES2 | --> buffer 1 and 2 len
3935 * mss is fixed when enable tso, so w/o programming the TDES3 ctx field.
3937 static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev)
3939 struct dma_desc *desc, *first, *mss_desc = NULL;
3940 struct stmmac_priv *priv = netdev_priv(dev);
3941 int nfrags = skb_shinfo(skb)->nr_frags;
3942 u32 queue = skb_get_queue_mapping(skb);
3943 unsigned int first_entry, tx_packets;
3944 int tmp_pay_len = 0, first_tx;
3945 struct stmmac_tx_queue *tx_q;
3946 bool has_vlan, set_ic;
3947 u8 proto_hdr_len, hdr;
3952 tx_q = &priv->tx_queue[queue];
3953 first_tx = tx_q->cur_tx;
3955 /* Compute header lengths */
3956 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
3957 proto_hdr_len = skb_transport_offset(skb) + sizeof(struct udphdr);
3958 hdr = sizeof(struct udphdr);
3960 proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
3961 hdr = tcp_hdrlen(skb);
3964 /* Desc availability based on threshold should be enough safe */
3965 if (unlikely(stmmac_tx_avail(priv, queue) <
3966 (((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) {
3967 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
3968 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
3970 /* This is a hard error, log it. */
3971 netdev_err(priv->dev,
3972 "%s: Tx Ring full when queue awake\n",
3975 return NETDEV_TX_BUSY;
3978 pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */
3980 mss = skb_shinfo(skb)->gso_size;
3982 /* set new MSS value if needed */
3983 if (mss != tx_q->mss) {
3984 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3985 mss_desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
3987 mss_desc = &tx_q->dma_tx[tx_q->cur_tx];
3989 stmmac_set_mss(priv, mss_desc, mss);
3991 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx,
3993 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
3996 if (netif_msg_tx_queued(priv)) {
3997 pr_info("%s: hdrlen %d, hdr_len %d, pay_len %d, mss %d\n",
3998 __func__, hdr, proto_hdr_len, pay_len, mss);
3999 pr_info("\tskb->len %d, skb->data_len %d\n", skb->len,
4003 /* Check if VLAN can be inserted by HW */
4004 has_vlan = stmmac_vlan_insert(priv, skb, tx_q);
4006 first_entry = tx_q->cur_tx;
4007 WARN_ON(tx_q->tx_skbuff[first_entry]);
4009 if (tx_q->tbs & STMMAC_TBS_AVAIL)
4010 desc = &tx_q->dma_entx[first_entry].basic;
4012 desc = &tx_q->dma_tx[first_entry];
4016 stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT);
4018 /* first descriptor: fill Headers on Buf1 */
4019 des = dma_map_single(priv->device, skb->data, skb_headlen(skb),
4021 if (dma_mapping_error(priv->device, des))
4024 tx_q->tx_skbuff_dma[first_entry].buf = des;
4025 tx_q->tx_skbuff_dma[first_entry].len = skb_headlen(skb);
4026 tx_q->tx_skbuff_dma[first_entry].map_as_page = false;
4027 tx_q->tx_skbuff_dma[first_entry].buf_type = STMMAC_TXBUF_T_SKB;
4029 if (priv->dma_cap.addr64 <= 32) {
4030 first->des0 = cpu_to_le32(des);
4032 /* Fill start of payload in buff2 of first descriptor */
4034 first->des1 = cpu_to_le32(des + proto_hdr_len);
4036 /* If needed take extra descriptors to fill the remaining payload */
4037 tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE;
4039 stmmac_set_desc_addr(priv, first, des);
4040 tmp_pay_len = pay_len;
4041 des += proto_hdr_len;
4045 stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0), queue);
4047 /* Prepare fragments */
4048 for (i = 0; i < nfrags; i++) {
4049 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4051 des = skb_frag_dma_map(priv->device, frag, 0,
4052 skb_frag_size(frag),
4054 if (dma_mapping_error(priv->device, des))
4057 stmmac_tso_allocator(priv, des, skb_frag_size(frag),
4058 (i == nfrags - 1), queue);
4060 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf = des;
4061 tx_q->tx_skbuff_dma[tx_q->cur_tx].len = skb_frag_size(frag);
4062 tx_q->tx_skbuff_dma[tx_q->cur_tx].map_as_page = true;
4063 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf_type = STMMAC_TXBUF_T_SKB;
4066 tx_q->tx_skbuff_dma[tx_q->cur_tx].last_segment = true;
4068 /* Only the last descriptor gets to point to the skb. */
4069 tx_q->tx_skbuff[tx_q->cur_tx] = skb;
4070 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf_type = STMMAC_TXBUF_T_SKB;
4072 /* Manage tx mitigation */
4073 tx_packets = (tx_q->cur_tx + 1) - first_tx;
4074 tx_q->tx_count_frames += tx_packets;
4076 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en)
4078 else if (!priv->tx_coal_frames[queue])
4080 else if (tx_packets > priv->tx_coal_frames[queue])
4082 else if ((tx_q->tx_count_frames %
4083 priv->tx_coal_frames[queue]) < tx_packets)
4089 if (tx_q->tbs & STMMAC_TBS_AVAIL)
4090 desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
4092 desc = &tx_q->dma_tx[tx_q->cur_tx];
4094 tx_q->tx_count_frames = 0;
4095 stmmac_set_tx_ic(priv, desc);
4096 priv->xstats.tx_set_ic_bit++;
4099 /* We've used all descriptors we need for this skb, however,
4100 * advance cur_tx so that it references a fresh descriptor.
4101 * ndo_start_xmit will fill this descriptor the next time it's
4102 * called and stmmac_tx_clean may clean up to this descriptor.
4104 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
4106 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
4107 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
4109 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
4112 dev->stats.tx_bytes += skb->len;
4113 priv->xstats.tx_tso_frames++;
4114 priv->xstats.tx_tso_nfrags += nfrags;
4116 if (priv->sarc_type)
4117 stmmac_set_desc_sarc(priv, first, priv->sarc_type);
4119 skb_tx_timestamp(skb);
4121 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4122 priv->hwts_tx_en)) {
4123 /* declare that device is doing timestamping */
4124 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4125 stmmac_enable_tx_timestamp(priv, first);
4128 /* Complete the first descriptor before granting the DMA */
4129 stmmac_prepare_tso_tx_desc(priv, first, 1,
4132 1, tx_q->tx_skbuff_dma[first_entry].last_segment,
4133 hdr / 4, (skb->len - proto_hdr_len));
4135 /* If context desc is used to change MSS */
4137 /* Make sure that first descriptor has been completely
4138 * written, including its own bit. This is because MSS is
4139 * actually before first descriptor, so we need to make
4140 * sure that MSS's own bit is the last thing written.
4143 stmmac_set_tx_owner(priv, mss_desc);
4146 if (netif_msg_pktdata(priv)) {
4147 pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n",
4148 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
4149 tx_q->cur_tx, first, nfrags);
4150 pr_info(">>> frame to be transmitted: ");
4151 print_pkt(skb->data, skb_headlen(skb));
4154 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
4156 stmmac_flush_tx_descriptors(priv, queue);
4157 stmmac_tx_timer_arm(priv, queue);
4159 return NETDEV_TX_OK;
4162 dev_err(priv->device, "Tx dma map failed\n");
4164 priv->dev->stats.tx_dropped++;
4165 return NETDEV_TX_OK;
4169 * stmmac_xmit - Tx entry point of the driver
4170 * @skb : the socket buffer
4171 * @dev : device pointer
4172 * Description : this is the tx entry point of the driver.
4173 * It programs the chain or the ring and supports oversized frames
4176 static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
4178 unsigned int first_entry, tx_packets, enh_desc;
4179 struct stmmac_priv *priv = netdev_priv(dev);
4180 unsigned int nopaged_len = skb_headlen(skb);
4181 int i, csum_insertion = 0, is_jumbo = 0;
4182 u32 queue = skb_get_queue_mapping(skb);
4183 int nfrags = skb_shinfo(skb)->nr_frags;
4184 int gso = skb_shinfo(skb)->gso_type;
4185 struct dma_edesc *tbs_desc = NULL;
4186 struct dma_desc *desc, *first;
4187 struct stmmac_tx_queue *tx_q;
4188 bool has_vlan, set_ic;
4189 int entry, first_tx;
4192 tx_q = &priv->tx_queue[queue];
4193 first_tx = tx_q->cur_tx;
4195 if (priv->tx_path_in_lpi_mode && priv->eee_sw_timer_en)
4196 stmmac_disable_eee_mode(priv);
4198 /* Manage oversized TCP frames for GMAC4 device */
4199 if (skb_is_gso(skb) && priv->tso) {
4200 if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
4201 return stmmac_tso_xmit(skb, dev);
4202 if (priv->plat->has_gmac4 && (gso & SKB_GSO_UDP_L4))
4203 return stmmac_tso_xmit(skb, dev);
4206 if (unlikely(stmmac_tx_avail(priv, queue) < nfrags + 1)) {
4207 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
4208 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
4210 /* This is a hard error, log it. */
4211 netdev_err(priv->dev,
4212 "%s: Tx Ring full when queue awake\n",
4215 return NETDEV_TX_BUSY;
4218 /* Check if VLAN can be inserted by HW */
4219 has_vlan = stmmac_vlan_insert(priv, skb, tx_q);
4221 entry = tx_q->cur_tx;
4222 first_entry = entry;
4223 WARN_ON(tx_q->tx_skbuff[first_entry]);
4225 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
4227 if (likely(priv->extend_desc))
4228 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
4229 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4230 desc = &tx_q->dma_entx[entry].basic;
4232 desc = tx_q->dma_tx + entry;
4237 stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT);
4239 enh_desc = priv->plat->enh_desc;
4240 /* To program the descriptors according to the size of the frame */
4242 is_jumbo = stmmac_is_jumbo_frm(priv, skb->len, enh_desc);
4244 if (unlikely(is_jumbo)) {
4245 entry = stmmac_jumbo_frm(priv, tx_q, skb, csum_insertion);
4246 if (unlikely(entry < 0) && (entry != -EINVAL))
4250 for (i = 0; i < nfrags; i++) {
4251 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4252 int len = skb_frag_size(frag);
4253 bool last_segment = (i == (nfrags - 1));
4255 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
4256 WARN_ON(tx_q->tx_skbuff[entry]);
4258 if (likely(priv->extend_desc))
4259 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
4260 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4261 desc = &tx_q->dma_entx[entry].basic;
4263 desc = tx_q->dma_tx + entry;
4265 des = skb_frag_dma_map(priv->device, frag, 0, len,
4267 if (dma_mapping_error(priv->device, des))
4268 goto dma_map_err; /* should reuse desc w/o issues */
4270 tx_q->tx_skbuff_dma[entry].buf = des;
4272 stmmac_set_desc_addr(priv, desc, des);
4274 tx_q->tx_skbuff_dma[entry].map_as_page = true;
4275 tx_q->tx_skbuff_dma[entry].len = len;
4276 tx_q->tx_skbuff_dma[entry].last_segment = last_segment;
4277 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_SKB;
4279 /* Prepare the descriptor and set the own bit too */
4280 stmmac_prepare_tx_desc(priv, desc, 0, len, csum_insertion,
4281 priv->mode, 1, last_segment, skb->len);
4284 /* Only the last descriptor gets to point to the skb. */
4285 tx_q->tx_skbuff[entry] = skb;
4286 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_SKB;
4288 /* According to the coalesce parameter the IC bit for the latest
4289 * segment is reset and the timer re-started to clean the tx status.
4290 * This approach takes care about the fragments: desc is the first
4291 * element in case of no SG.
4293 tx_packets = (entry + 1) - first_tx;
4294 tx_q->tx_count_frames += tx_packets;
4296 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en)
4298 else if (!priv->tx_coal_frames[queue])
4300 else if (tx_packets > priv->tx_coal_frames[queue])
4302 else if ((tx_q->tx_count_frames %
4303 priv->tx_coal_frames[queue]) < tx_packets)
4309 if (likely(priv->extend_desc))
4310 desc = &tx_q->dma_etx[entry].basic;
4311 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4312 desc = &tx_q->dma_entx[entry].basic;
4314 desc = &tx_q->dma_tx[entry];
4316 tx_q->tx_count_frames = 0;
4317 stmmac_set_tx_ic(priv, desc);
4318 priv->xstats.tx_set_ic_bit++;
4321 /* We've used all descriptors we need for this skb, however,
4322 * advance cur_tx so that it references a fresh descriptor.
4323 * ndo_start_xmit will fill this descriptor the next time it's
4324 * called and stmmac_tx_clean may clean up to this descriptor.
4326 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
4327 tx_q->cur_tx = entry;
4329 if (netif_msg_pktdata(priv)) {
4330 netdev_dbg(priv->dev,
4331 "%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
4332 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
4333 entry, first, nfrags);
4335 netdev_dbg(priv->dev, ">>> frame to be transmitted: ");
4336 print_pkt(skb->data, skb->len);
4339 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
4340 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
4342 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
4345 dev->stats.tx_bytes += skb->len;
4347 if (priv->sarc_type)
4348 stmmac_set_desc_sarc(priv, first, priv->sarc_type);
4350 skb_tx_timestamp(skb);
4352 /* Ready to fill the first descriptor and set the OWN bit w/o any
4353 * problems because all the descriptors are actually ready to be
4354 * passed to the DMA engine.
4356 if (likely(!is_jumbo)) {
4357 bool last_segment = (nfrags == 0);
4359 des = dma_map_single(priv->device, skb->data,
4360 nopaged_len, DMA_TO_DEVICE);
4361 if (dma_mapping_error(priv->device, des))
4364 tx_q->tx_skbuff_dma[first_entry].buf = des;
4365 tx_q->tx_skbuff_dma[first_entry].buf_type = STMMAC_TXBUF_T_SKB;
4366 tx_q->tx_skbuff_dma[first_entry].map_as_page = false;
4368 stmmac_set_desc_addr(priv, first, des);
4370 tx_q->tx_skbuff_dma[first_entry].len = nopaged_len;
4371 tx_q->tx_skbuff_dma[first_entry].last_segment = last_segment;
4373 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4374 priv->hwts_tx_en)) {
4375 /* declare that device is doing timestamping */
4376 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4377 stmmac_enable_tx_timestamp(priv, first);
4380 /* Prepare the first descriptor setting the OWN bit too */
4381 stmmac_prepare_tx_desc(priv, first, 1, nopaged_len,
4382 csum_insertion, priv->mode, 0, last_segment,
4386 if (tx_q->tbs & STMMAC_TBS_EN) {
4387 struct timespec64 ts = ns_to_timespec64(skb->tstamp);
4389 tbs_desc = &tx_q->dma_entx[first_entry];
4390 stmmac_set_desc_tbs(priv, tbs_desc, ts.tv_sec, ts.tv_nsec);
4393 stmmac_set_tx_owner(priv, first);
4395 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
4397 stmmac_enable_dma_transmission(priv, priv->ioaddr);
4399 stmmac_flush_tx_descriptors(priv, queue);
4400 stmmac_tx_timer_arm(priv, queue);
4402 return NETDEV_TX_OK;
4405 netdev_err(priv->dev, "Tx DMA map failed\n");
4407 priv->dev->stats.tx_dropped++;
4408 return NETDEV_TX_OK;
4411 static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
4413 struct vlan_ethhdr *veth;
4417 veth = (struct vlan_ethhdr *)skb->data;
4418 vlan_proto = veth->h_vlan_proto;
4420 if ((vlan_proto == htons(ETH_P_8021Q) &&
4421 dev->features & NETIF_F_HW_VLAN_CTAG_RX) ||
4422 (vlan_proto == htons(ETH_P_8021AD) &&
4423 dev->features & NETIF_F_HW_VLAN_STAG_RX)) {
4424 /* pop the vlan tag */
4425 vlanid = ntohs(veth->h_vlan_TCI);
4426 memmove(skb->data + VLAN_HLEN, veth, ETH_ALEN * 2);
4427 skb_pull(skb, VLAN_HLEN);
4428 __vlan_hwaccel_put_tag(skb, vlan_proto, vlanid);
4433 * stmmac_rx_refill - refill used skb preallocated buffers
4434 * @priv: driver private structure
4435 * @queue: RX queue index
4436 * Description : this is to reallocate the skb for the reception process
4437 * that is based on zero-copy.
4439 static inline void stmmac_rx_refill(struct stmmac_priv *priv, u32 queue)
4441 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4442 int dirty = stmmac_rx_dirty(priv, queue);
4443 unsigned int entry = rx_q->dirty_rx;
4444 gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
4446 if (priv->dma_cap.addr64 <= 32)
4449 while (dirty-- > 0) {
4450 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry];
4454 if (priv->extend_desc)
4455 p = (struct dma_desc *)(rx_q->dma_erx + entry);
4457 p = rx_q->dma_rx + entry;
4460 buf->page = page_pool_alloc_pages(rx_q->page_pool, gfp);
4465 if (priv->sph && !buf->sec_page) {
4466 buf->sec_page = page_pool_alloc_pages(rx_q->page_pool, gfp);
4470 buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
4473 buf->addr = page_pool_get_dma_addr(buf->page) + buf->page_offset;
4475 stmmac_set_desc_addr(priv, p, buf->addr);
4477 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true);
4479 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false);
4480 stmmac_refill_desc3(priv, rx_q, p);
4482 rx_q->rx_count_frames++;
4483 rx_q->rx_count_frames += priv->rx_coal_frames[queue];
4484 if (rx_q->rx_count_frames > priv->rx_coal_frames[queue])
4485 rx_q->rx_count_frames = 0;
4487 use_rx_wd = !priv->rx_coal_frames[queue];
4488 use_rx_wd |= rx_q->rx_count_frames > 0;
4489 if (!priv->use_riwt)
4493 stmmac_set_rx_owner(priv, p, use_rx_wd);
4495 entry = STMMAC_GET_ENTRY(entry, priv->dma_rx_size);
4497 rx_q->dirty_rx = entry;
4498 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
4499 (rx_q->dirty_rx * sizeof(struct dma_desc));
4500 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue);
4503 static unsigned int stmmac_rx_buf1_len(struct stmmac_priv *priv,
4505 int status, unsigned int len)
4507 unsigned int plen = 0, hlen = 0;
4508 int coe = priv->hw->rx_csum;
4510 /* Not first descriptor, buffer is always zero */
4511 if (priv->sph && len)
4514 /* First descriptor, get split header length */
4515 stmmac_get_rx_header_len(priv, p, &hlen);
4516 if (priv->sph && hlen) {
4517 priv->xstats.rx_split_hdr_pkt_n++;
4521 /* First descriptor, not last descriptor and not split header */
4522 if (status & rx_not_ls)
4523 return priv->dma_buf_sz;
4525 plen = stmmac_get_rx_frame_len(priv, p, coe);
4527 /* First descriptor and last descriptor and not split header */
4528 return min_t(unsigned int, priv->dma_buf_sz, plen);
4531 static unsigned int stmmac_rx_buf2_len(struct stmmac_priv *priv,
4533 int status, unsigned int len)
4535 int coe = priv->hw->rx_csum;
4536 unsigned int plen = 0;
4538 /* Not split header, buffer is not available */
4542 /* Not last descriptor */
4543 if (status & rx_not_ls)
4544 return priv->dma_buf_sz;
4546 plen = stmmac_get_rx_frame_len(priv, p, coe);
4548 /* Last descriptor */
4552 static int stmmac_xdp_xmit_xdpf(struct stmmac_priv *priv, int queue,
4553 struct xdp_frame *xdpf, bool dma_map)
4555 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
4556 unsigned int entry = tx_q->cur_tx;
4557 struct dma_desc *tx_desc;
4558 dma_addr_t dma_addr;
4561 if (stmmac_tx_avail(priv, queue) < STMMAC_TX_THRESH(priv))
4562 return STMMAC_XDP_CONSUMED;
4564 if (likely(priv->extend_desc))
4565 tx_desc = (struct dma_desc *)(tx_q->dma_etx + entry);
4566 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4567 tx_desc = &tx_q->dma_entx[entry].basic;
4569 tx_desc = tx_q->dma_tx + entry;
4572 dma_addr = dma_map_single(priv->device, xdpf->data,
4573 xdpf->len, DMA_TO_DEVICE);
4574 if (dma_mapping_error(priv->device, dma_addr))
4575 return STMMAC_XDP_CONSUMED;
4577 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XDP_NDO;
4579 struct page *page = virt_to_page(xdpf->data);
4581 dma_addr = page_pool_get_dma_addr(page) + sizeof(*xdpf) +
4583 dma_sync_single_for_device(priv->device, dma_addr,
4584 xdpf->len, DMA_BIDIRECTIONAL);
4586 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XDP_TX;
4589 tx_q->tx_skbuff_dma[entry].buf = dma_addr;
4590 tx_q->tx_skbuff_dma[entry].map_as_page = false;
4591 tx_q->tx_skbuff_dma[entry].len = xdpf->len;
4592 tx_q->tx_skbuff_dma[entry].last_segment = true;
4593 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
4595 tx_q->xdpf[entry] = xdpf;
4597 stmmac_set_desc_addr(priv, tx_desc, dma_addr);
4599 stmmac_prepare_tx_desc(priv, tx_desc, 1, xdpf->len,
4600 true, priv->mode, true, true,
4603 tx_q->tx_count_frames++;
4605 if (tx_q->tx_count_frames % priv->tx_coal_frames[queue] == 0)
4611 tx_q->tx_count_frames = 0;
4612 stmmac_set_tx_ic(priv, tx_desc);
4613 priv->xstats.tx_set_ic_bit++;
4616 stmmac_enable_dma_transmission(priv, priv->ioaddr);
4618 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
4619 tx_q->cur_tx = entry;
4621 return STMMAC_XDP_TX;
4624 static int stmmac_xdp_get_tx_queue(struct stmmac_priv *priv,
4629 if (unlikely(index < 0))
4632 while (index >= priv->plat->tx_queues_to_use)
4633 index -= priv->plat->tx_queues_to_use;
4638 static int stmmac_xdp_xmit_back(struct stmmac_priv *priv,
4639 struct xdp_buff *xdp)
4641 struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp);
4642 int cpu = smp_processor_id();
4643 struct netdev_queue *nq;
4647 if (unlikely(!xdpf))
4648 return STMMAC_XDP_CONSUMED;
4650 queue = stmmac_xdp_get_tx_queue(priv, cpu);
4651 nq = netdev_get_tx_queue(priv->dev, queue);
4653 __netif_tx_lock(nq, cpu);
4654 /* Avoids TX time-out as we are sharing with slow path */
4655 txq_trans_cond_update(nq);
4657 res = stmmac_xdp_xmit_xdpf(priv, queue, xdpf, false);
4658 if (res == STMMAC_XDP_TX)
4659 stmmac_flush_tx_descriptors(priv, queue);
4661 __netif_tx_unlock(nq);
4666 static int __stmmac_xdp_run_prog(struct stmmac_priv *priv,
4667 struct bpf_prog *prog,
4668 struct xdp_buff *xdp)
4673 act = bpf_prog_run_xdp(prog, xdp);
4676 res = STMMAC_XDP_PASS;
4679 res = stmmac_xdp_xmit_back(priv, xdp);
4682 if (xdp_do_redirect(priv->dev, xdp, prog) < 0)
4683 res = STMMAC_XDP_CONSUMED;
4685 res = STMMAC_XDP_REDIRECT;
4688 bpf_warn_invalid_xdp_action(priv->dev, prog, act);
4691 trace_xdp_exception(priv->dev, prog, act);
4694 res = STMMAC_XDP_CONSUMED;
4701 static struct sk_buff *stmmac_xdp_run_prog(struct stmmac_priv *priv,
4702 struct xdp_buff *xdp)
4704 struct bpf_prog *prog;
4707 prog = READ_ONCE(priv->xdp_prog);
4709 res = STMMAC_XDP_PASS;
4713 res = __stmmac_xdp_run_prog(priv, prog, xdp);
4715 return ERR_PTR(-res);
4718 static void stmmac_finalize_xdp_rx(struct stmmac_priv *priv,
4721 int cpu = smp_processor_id();
4724 queue = stmmac_xdp_get_tx_queue(priv, cpu);
4726 if (xdp_status & STMMAC_XDP_TX)
4727 stmmac_tx_timer_arm(priv, queue);
4729 if (xdp_status & STMMAC_XDP_REDIRECT)
4733 static struct sk_buff *stmmac_construct_skb_zc(struct stmmac_channel *ch,
4734 struct xdp_buff *xdp)
4736 unsigned int metasize = xdp->data - xdp->data_meta;
4737 unsigned int datasize = xdp->data_end - xdp->data;
4738 struct sk_buff *skb;
4740 skb = __napi_alloc_skb(&ch->rxtx_napi,
4741 xdp->data_end - xdp->data_hard_start,
4742 GFP_ATOMIC | __GFP_NOWARN);
4746 skb_reserve(skb, xdp->data - xdp->data_hard_start);
4747 memcpy(__skb_put(skb, datasize), xdp->data, datasize);
4749 skb_metadata_set(skb, metasize);
4754 static void stmmac_dispatch_skb_zc(struct stmmac_priv *priv, u32 queue,
4755 struct dma_desc *p, struct dma_desc *np,
4756 struct xdp_buff *xdp)
4758 struct stmmac_channel *ch = &priv->channel[queue];
4759 unsigned int len = xdp->data_end - xdp->data;
4760 enum pkt_hash_types hash_type;
4761 int coe = priv->hw->rx_csum;
4762 struct sk_buff *skb;
4765 skb = stmmac_construct_skb_zc(ch, xdp);
4767 priv->dev->stats.rx_dropped++;
4771 stmmac_get_rx_hwtstamp(priv, p, np, skb);
4772 stmmac_rx_vlan(priv->dev, skb);
4773 skb->protocol = eth_type_trans(skb, priv->dev);
4776 skb_checksum_none_assert(skb);
4778 skb->ip_summed = CHECKSUM_UNNECESSARY;
4780 if (!stmmac_get_rx_hash(priv, p, &hash, &hash_type))
4781 skb_set_hash(skb, hash, hash_type);
4783 skb_record_rx_queue(skb, queue);
4784 napi_gro_receive(&ch->rxtx_napi, skb);
4786 priv->dev->stats.rx_packets++;
4787 priv->dev->stats.rx_bytes += len;
4790 static bool stmmac_rx_refill_zc(struct stmmac_priv *priv, u32 queue, u32 budget)
4792 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4793 unsigned int entry = rx_q->dirty_rx;
4794 struct dma_desc *rx_desc = NULL;
4797 budget = min(budget, stmmac_rx_dirty(priv, queue));
4799 while (budget-- > 0 && entry != rx_q->cur_rx) {
4800 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry];
4801 dma_addr_t dma_addr;
4805 buf->xdp = xsk_buff_alloc(rx_q->xsk_pool);
4812 if (priv->extend_desc)
4813 rx_desc = (struct dma_desc *)(rx_q->dma_erx + entry);
4815 rx_desc = rx_q->dma_rx + entry;
4817 dma_addr = xsk_buff_xdp_get_dma(buf->xdp);
4818 stmmac_set_desc_addr(priv, rx_desc, dma_addr);
4819 stmmac_set_desc_sec_addr(priv, rx_desc, 0, false);
4820 stmmac_refill_desc3(priv, rx_q, rx_desc);
4822 rx_q->rx_count_frames++;
4823 rx_q->rx_count_frames += priv->rx_coal_frames[queue];
4824 if (rx_q->rx_count_frames > priv->rx_coal_frames[queue])
4825 rx_q->rx_count_frames = 0;
4827 use_rx_wd = !priv->rx_coal_frames[queue];
4828 use_rx_wd |= rx_q->rx_count_frames > 0;
4829 if (!priv->use_riwt)
4833 stmmac_set_rx_owner(priv, rx_desc, use_rx_wd);
4835 entry = STMMAC_GET_ENTRY(entry, priv->dma_rx_size);
4839 rx_q->dirty_rx = entry;
4840 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
4841 (rx_q->dirty_rx * sizeof(struct dma_desc));
4842 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue);
4848 static int stmmac_rx_zc(struct stmmac_priv *priv, int limit, u32 queue)
4850 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4851 unsigned int count = 0, error = 0, len = 0;
4852 int dirty = stmmac_rx_dirty(priv, queue);
4853 unsigned int next_entry = rx_q->cur_rx;
4854 unsigned int desc_size;
4855 struct bpf_prog *prog;
4856 bool failure = false;
4860 if (netif_msg_rx_status(priv)) {
4863 netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
4864 if (priv->extend_desc) {
4865 rx_head = (void *)rx_q->dma_erx;
4866 desc_size = sizeof(struct dma_extended_desc);
4868 rx_head = (void *)rx_q->dma_rx;
4869 desc_size = sizeof(struct dma_desc);
4872 stmmac_display_ring(priv, rx_head, priv->dma_rx_size, true,
4873 rx_q->dma_rx_phy, desc_size);
4875 while (count < limit) {
4876 struct stmmac_rx_buffer *buf;
4877 unsigned int buf1_len = 0;
4878 struct dma_desc *np, *p;
4882 if (!count && rx_q->state_saved) {
4883 error = rx_q->state.error;
4884 len = rx_q->state.len;
4886 rx_q->state_saved = false;
4897 buf = &rx_q->buf_pool[entry];
4899 if (dirty >= STMMAC_RX_FILL_BATCH) {
4900 failure = failure ||
4901 !stmmac_rx_refill_zc(priv, queue, dirty);
4905 if (priv->extend_desc)
4906 p = (struct dma_desc *)(rx_q->dma_erx + entry);
4908 p = rx_q->dma_rx + entry;
4910 /* read the status of the incoming frame */
4911 status = stmmac_rx_status(priv, &priv->dev->stats,
4913 /* check if managed by the DMA otherwise go ahead */
4914 if (unlikely(status & dma_own))
4917 /* Prefetch the next RX descriptor */
4918 rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx,
4920 next_entry = rx_q->cur_rx;
4922 if (priv->extend_desc)
4923 np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
4925 np = rx_q->dma_rx + next_entry;
4929 /* Ensure a valid XSK buffer before proceed */
4933 if (priv->extend_desc)
4934 stmmac_rx_extended_status(priv, &priv->dev->stats,
4936 rx_q->dma_erx + entry);
4937 if (unlikely(status == discard_frame)) {
4938 xsk_buff_free(buf->xdp);
4942 if (!priv->hwts_rx_en)
4943 priv->dev->stats.rx_errors++;
4946 if (unlikely(error && (status & rx_not_ls)))
4948 if (unlikely(error)) {
4953 /* XSK pool expects RX frame 1:1 mapped to XSK buffer */
4954 if (likely(status & rx_not_ls)) {
4955 xsk_buff_free(buf->xdp);
4962 /* XDP ZC Frame only support primary buffers for now */
4963 buf1_len = stmmac_rx_buf1_len(priv, p, status, len);
4966 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
4967 * Type frames (LLC/LLC-SNAP)
4969 * llc_snap is never checked in GMAC >= 4, so this ACS
4970 * feature is always disabled and packets need to be
4971 * stripped manually.
4973 if (likely(!(status & rx_not_ls)) &&
4974 (likely(priv->synopsys_id >= DWMAC_CORE_4_00) ||
4975 unlikely(status != llc_snap))) {
4976 buf1_len -= ETH_FCS_LEN;
4980 /* RX buffer is good and fit into a XSK pool buffer */
4981 buf->xdp->data_end = buf->xdp->data + buf1_len;
4982 xsk_buff_dma_sync_for_cpu(buf->xdp, rx_q->xsk_pool);
4984 prog = READ_ONCE(priv->xdp_prog);
4985 res = __stmmac_xdp_run_prog(priv, prog, buf->xdp);
4988 case STMMAC_XDP_PASS:
4989 stmmac_dispatch_skb_zc(priv, queue, p, np, buf->xdp);
4990 xsk_buff_free(buf->xdp);
4992 case STMMAC_XDP_CONSUMED:
4993 xsk_buff_free(buf->xdp);
4994 priv->dev->stats.rx_dropped++;
4997 case STMMAC_XDP_REDIRECT:
5007 if (status & rx_not_ls) {
5008 rx_q->state_saved = true;
5009 rx_q->state.error = error;
5010 rx_q->state.len = len;
5013 stmmac_finalize_xdp_rx(priv, xdp_status);
5015 priv->xstats.rx_pkt_n += count;
5016 priv->xstats.rxq_stats[queue].rx_pkt_n += count;
5018 if (xsk_uses_need_wakeup(rx_q->xsk_pool)) {
5019 if (failure || stmmac_rx_dirty(priv, queue) > 0)
5020 xsk_set_rx_need_wakeup(rx_q->xsk_pool);
5022 xsk_clear_rx_need_wakeup(rx_q->xsk_pool);
5027 return failure ? limit : (int)count;
5031 * stmmac_rx - manage the receive process
5032 * @priv: driver private structure
5033 * @limit: napi bugget
5034 * @queue: RX queue index.
5035 * Description : this the function called by the napi poll method.
5036 * It gets all the frames inside the ring.
5038 static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
5040 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
5041 struct stmmac_channel *ch = &priv->channel[queue];
5042 unsigned int count = 0, error = 0, len = 0;
5043 int status = 0, coe = priv->hw->rx_csum;
5044 unsigned int next_entry = rx_q->cur_rx;
5045 enum dma_data_direction dma_dir;
5046 unsigned int desc_size;
5047 struct sk_buff *skb = NULL;
5048 struct xdp_buff xdp;
5052 dma_dir = page_pool_get_dma_dir(rx_q->page_pool);
5053 buf_sz = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE) * PAGE_SIZE;
5055 if (netif_msg_rx_status(priv)) {
5058 netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
5059 if (priv->extend_desc) {
5060 rx_head = (void *)rx_q->dma_erx;
5061 desc_size = sizeof(struct dma_extended_desc);
5063 rx_head = (void *)rx_q->dma_rx;
5064 desc_size = sizeof(struct dma_desc);
5067 stmmac_display_ring(priv, rx_head, priv->dma_rx_size, true,
5068 rx_q->dma_rx_phy, desc_size);
5070 while (count < limit) {
5071 unsigned int buf1_len = 0, buf2_len = 0;
5072 enum pkt_hash_types hash_type;
5073 struct stmmac_rx_buffer *buf;
5074 struct dma_desc *np, *p;
5078 if (!count && rx_q->state_saved) {
5079 skb = rx_q->state.skb;
5080 error = rx_q->state.error;
5081 len = rx_q->state.len;
5083 rx_q->state_saved = false;
5096 buf = &rx_q->buf_pool[entry];
5098 if (priv->extend_desc)
5099 p = (struct dma_desc *)(rx_q->dma_erx + entry);
5101 p = rx_q->dma_rx + entry;
5103 /* read the status of the incoming frame */
5104 status = stmmac_rx_status(priv, &priv->dev->stats,
5106 /* check if managed by the DMA otherwise go ahead */
5107 if (unlikely(status & dma_own))
5110 rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx,
5112 next_entry = rx_q->cur_rx;
5114 if (priv->extend_desc)
5115 np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
5117 np = rx_q->dma_rx + next_entry;
5121 if (priv->extend_desc)
5122 stmmac_rx_extended_status(priv, &priv->dev->stats,
5123 &priv->xstats, rx_q->dma_erx + entry);
5124 if (unlikely(status == discard_frame)) {
5125 page_pool_recycle_direct(rx_q->page_pool, buf->page);
5128 if (!priv->hwts_rx_en)
5129 priv->dev->stats.rx_errors++;
5132 if (unlikely(error && (status & rx_not_ls)))
5134 if (unlikely(error)) {
5141 /* Buffer is good. Go on. */
5143 prefetch(page_address(buf->page) + buf->page_offset);
5145 prefetch(page_address(buf->sec_page));
5147 buf1_len = stmmac_rx_buf1_len(priv, p, status, len);
5149 buf2_len = stmmac_rx_buf2_len(priv, p, status, len);
5152 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
5153 * Type frames (LLC/LLC-SNAP)
5155 * llc_snap is never checked in GMAC >= 4, so this ACS
5156 * feature is always disabled and packets need to be
5157 * stripped manually.
5159 if (likely(!(status & rx_not_ls)) &&
5160 (likely(priv->synopsys_id >= DWMAC_CORE_4_00) ||
5161 unlikely(status != llc_snap))) {
5163 buf2_len -= ETH_FCS_LEN;
5165 } else if (buf1_len) {
5166 buf1_len -= ETH_FCS_LEN;
5172 unsigned int pre_len, sync_len;
5174 dma_sync_single_for_cpu(priv->device, buf->addr,
5177 xdp_init_buff(&xdp, buf_sz, &rx_q->xdp_rxq);
5178 xdp_prepare_buff(&xdp, page_address(buf->page),
5179 buf->page_offset, buf1_len, false);
5181 pre_len = xdp.data_end - xdp.data_hard_start -
5183 skb = stmmac_xdp_run_prog(priv, &xdp);
5184 /* Due xdp_adjust_tail: DMA sync for_device
5185 * cover max len CPU touch
5187 sync_len = xdp.data_end - xdp.data_hard_start -
5189 sync_len = max(sync_len, pre_len);
5191 /* For Not XDP_PASS verdict */
5193 unsigned int xdp_res = -PTR_ERR(skb);
5195 if (xdp_res & STMMAC_XDP_CONSUMED) {
5196 page_pool_put_page(rx_q->page_pool,
5197 virt_to_head_page(xdp.data),
5200 priv->dev->stats.rx_dropped++;
5202 /* Clear skb as it was set as
5203 * status by XDP program.
5207 if (unlikely((status & rx_not_ls)))
5212 } else if (xdp_res & (STMMAC_XDP_TX |
5213 STMMAC_XDP_REDIRECT)) {
5214 xdp_status |= xdp_res;
5224 /* XDP program may expand or reduce tail */
5225 buf1_len = xdp.data_end - xdp.data;
5227 skb = napi_alloc_skb(&ch->rx_napi, buf1_len);
5229 priv->dev->stats.rx_dropped++;
5234 /* XDP program may adjust header */
5235 skb_copy_to_linear_data(skb, xdp.data, buf1_len);
5236 skb_put(skb, buf1_len);
5238 /* Data payload copied into SKB, page ready for recycle */
5239 page_pool_recycle_direct(rx_q->page_pool, buf->page);
5241 } else if (buf1_len) {
5242 dma_sync_single_for_cpu(priv->device, buf->addr,
5244 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
5245 buf->page, buf->page_offset, buf1_len,
5248 /* Data payload appended into SKB */
5249 page_pool_release_page(rx_q->page_pool, buf->page);
5254 dma_sync_single_for_cpu(priv->device, buf->sec_addr,
5256 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
5257 buf->sec_page, 0, buf2_len,
5260 /* Data payload appended into SKB */
5261 page_pool_release_page(rx_q->page_pool, buf->sec_page);
5262 buf->sec_page = NULL;
5266 if (likely(status & rx_not_ls))
5271 /* Got entire packet into SKB. Finish it. */
5273 stmmac_get_rx_hwtstamp(priv, p, np, skb);
5274 stmmac_rx_vlan(priv->dev, skb);
5275 skb->protocol = eth_type_trans(skb, priv->dev);
5278 skb_checksum_none_assert(skb);
5280 skb->ip_summed = CHECKSUM_UNNECESSARY;
5282 if (!stmmac_get_rx_hash(priv, p, &hash, &hash_type))
5283 skb_set_hash(skb, hash, hash_type);
5285 skb_record_rx_queue(skb, queue);
5286 napi_gro_receive(&ch->rx_napi, skb);
5289 priv->dev->stats.rx_packets++;
5290 priv->dev->stats.rx_bytes += len;
5294 if (status & rx_not_ls || skb) {
5295 rx_q->state_saved = true;
5296 rx_q->state.skb = skb;
5297 rx_q->state.error = error;
5298 rx_q->state.len = len;
5301 stmmac_finalize_xdp_rx(priv, xdp_status);
5303 stmmac_rx_refill(priv, queue);
5305 priv->xstats.rx_pkt_n += count;
5306 priv->xstats.rxq_stats[queue].rx_pkt_n += count;
5311 static int stmmac_napi_poll_rx(struct napi_struct *napi, int budget)
5313 struct stmmac_channel *ch =
5314 container_of(napi, struct stmmac_channel, rx_napi);
5315 struct stmmac_priv *priv = ch->priv_data;
5316 u32 chan = ch->index;
5319 priv->xstats.napi_poll++;
5321 work_done = stmmac_rx(priv, budget, chan);
5322 if (work_done < budget && napi_complete_done(napi, work_done)) {
5323 unsigned long flags;
5325 spin_lock_irqsave(&ch->lock, flags);
5326 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
5327 spin_unlock_irqrestore(&ch->lock, flags);
5333 static int stmmac_napi_poll_tx(struct napi_struct *napi, int budget)
5335 struct stmmac_channel *ch =
5336 container_of(napi, struct stmmac_channel, tx_napi);
5337 struct stmmac_priv *priv = ch->priv_data;
5338 u32 chan = ch->index;
5341 priv->xstats.napi_poll++;
5343 work_done = stmmac_tx_clean(priv, budget, chan);
5344 work_done = min(work_done, budget);
5346 if (work_done < budget && napi_complete_done(napi, work_done)) {
5347 unsigned long flags;
5349 spin_lock_irqsave(&ch->lock, flags);
5350 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
5351 spin_unlock_irqrestore(&ch->lock, flags);
5357 static int stmmac_napi_poll_rxtx(struct napi_struct *napi, int budget)
5359 struct stmmac_channel *ch =
5360 container_of(napi, struct stmmac_channel, rxtx_napi);
5361 struct stmmac_priv *priv = ch->priv_data;
5362 int rx_done, tx_done, rxtx_done;
5363 u32 chan = ch->index;
5365 priv->xstats.napi_poll++;
5367 tx_done = stmmac_tx_clean(priv, budget, chan);
5368 tx_done = min(tx_done, budget);
5370 rx_done = stmmac_rx_zc(priv, budget, chan);
5372 rxtx_done = max(tx_done, rx_done);
5374 /* If either TX or RX work is not complete, return budget
5377 if (rxtx_done >= budget)
5380 /* all work done, exit the polling mode */
5381 if (napi_complete_done(napi, rxtx_done)) {
5382 unsigned long flags;
5384 spin_lock_irqsave(&ch->lock, flags);
5385 /* Both RX and TX work done are compelte,
5386 * so enable both RX & TX IRQs.
5388 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 1);
5389 spin_unlock_irqrestore(&ch->lock, flags);
5392 return min(rxtx_done, budget - 1);
5397 * @dev : Pointer to net device structure
5398 * @txqueue: the index of the hanging transmit queue
5399 * Description: this function is called when a packet transmission fails to
5400 * complete within a reasonable time. The driver will mark the error in the
5401 * netdev structure and arrange for the device to be reset to a sane state
5402 * in order to transmit a new packet.
5404 static void stmmac_tx_timeout(struct net_device *dev, unsigned int txqueue)
5406 struct stmmac_priv *priv = netdev_priv(dev);
5408 stmmac_global_err(priv);
5412 * stmmac_set_rx_mode - entry point for multicast addressing
5413 * @dev : pointer to the device structure
5415 * This function is a driver entry point which gets called by the kernel
5416 * whenever multicast addresses must be enabled/disabled.
5420 static void stmmac_set_rx_mode(struct net_device *dev)
5422 struct stmmac_priv *priv = netdev_priv(dev);
5424 stmmac_set_filter(priv, priv->hw, dev);
5428 * stmmac_change_mtu - entry point to change MTU size for the device.
5429 * @dev : device pointer.
5430 * @new_mtu : the new MTU size for the device.
5431 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
5432 * to drive packet transmission. Ethernet has an MTU of 1500 octets
5433 * (ETH_DATA_LEN). This value can be changed with ifconfig.
5435 * 0 on success and an appropriate (-)ve integer as defined in errno.h
5438 static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
5440 struct stmmac_priv *priv = netdev_priv(dev);
5441 int txfifosz = priv->plat->tx_fifo_size;
5442 const int mtu = new_mtu;
5445 txfifosz = priv->dma_cap.tx_fifo_size;
5447 txfifosz /= priv->plat->tx_queues_to_use;
5449 if (netif_running(dev)) {
5450 netdev_err(priv->dev, "must be stopped to change its MTU\n");
5454 if (stmmac_xdp_is_enabled(priv) && new_mtu > ETH_DATA_LEN) {
5455 netdev_dbg(priv->dev, "Jumbo frames not supported for XDP\n");
5459 new_mtu = STMMAC_ALIGN(new_mtu);
5461 /* If condition true, FIFO is too small or MTU too large */
5462 if ((txfifosz < new_mtu) || (new_mtu > BUF_SIZE_16KiB))
5467 netdev_update_features(dev);
5472 static netdev_features_t stmmac_fix_features(struct net_device *dev,
5473 netdev_features_t features)
5475 struct stmmac_priv *priv = netdev_priv(dev);
5477 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
5478 features &= ~NETIF_F_RXCSUM;
5480 if (!priv->plat->tx_coe)
5481 features &= ~NETIF_F_CSUM_MASK;
5483 /* Some GMAC devices have a bugged Jumbo frame support that
5484 * needs to have the Tx COE disabled for oversized frames
5485 * (due to limited buffer sizes). In this case we disable
5486 * the TX csum insertion in the TDES and not use SF.
5488 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
5489 features &= ~NETIF_F_CSUM_MASK;
5491 /* Disable tso if asked by ethtool */
5492 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
5493 if (features & NETIF_F_TSO)
5502 static int stmmac_set_features(struct net_device *netdev,
5503 netdev_features_t features)
5505 struct stmmac_priv *priv = netdev_priv(netdev);
5507 /* Keep the COE Type in case of csum is supporting */
5508 if (features & NETIF_F_RXCSUM)
5509 priv->hw->rx_csum = priv->plat->rx_coe;
5511 priv->hw->rx_csum = 0;
5512 /* No check needed because rx_coe has been set before and it will be
5513 * fixed in case of issue.
5515 stmmac_rx_ipc(priv, priv->hw);
5517 if (priv->sph_cap) {
5518 bool sph_en = (priv->hw->rx_csum > 0) && priv->sph;
5521 for (chan = 0; chan < priv->plat->rx_queues_to_use; chan++)
5522 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
5528 static void stmmac_fpe_event_status(struct stmmac_priv *priv, int status)
5530 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
5531 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
5532 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
5533 bool *hs_enable = &fpe_cfg->hs_enable;
5535 if (status == FPE_EVENT_UNKNOWN || !*hs_enable)
5538 /* If LP has sent verify mPacket, LP is FPE capable */
5539 if ((status & FPE_EVENT_RVER) == FPE_EVENT_RVER) {
5540 if (*lp_state < FPE_STATE_CAPABLE)
5541 *lp_state = FPE_STATE_CAPABLE;
5543 /* If user has requested FPE enable, quickly response */
5545 stmmac_fpe_send_mpacket(priv, priv->ioaddr,
5549 /* If Local has sent verify mPacket, Local is FPE capable */
5550 if ((status & FPE_EVENT_TVER) == FPE_EVENT_TVER) {
5551 if (*lo_state < FPE_STATE_CAPABLE)
5552 *lo_state = FPE_STATE_CAPABLE;
5555 /* If LP has sent response mPacket, LP is entering FPE ON */
5556 if ((status & FPE_EVENT_RRSP) == FPE_EVENT_RRSP)
5557 *lp_state = FPE_STATE_ENTERING_ON;
5559 /* If Local has sent response mPacket, Local is entering FPE ON */
5560 if ((status & FPE_EVENT_TRSP) == FPE_EVENT_TRSP)
5561 *lo_state = FPE_STATE_ENTERING_ON;
5563 if (!test_bit(__FPE_REMOVING, &priv->fpe_task_state) &&
5564 !test_and_set_bit(__FPE_TASK_SCHED, &priv->fpe_task_state) &&
5566 queue_work(priv->fpe_wq, &priv->fpe_task);
5570 static void stmmac_common_interrupt(struct stmmac_priv *priv)
5572 u32 rx_cnt = priv->plat->rx_queues_to_use;
5573 u32 tx_cnt = priv->plat->tx_queues_to_use;
5578 xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
5579 queues_count = (rx_cnt > tx_cnt) ? rx_cnt : tx_cnt;
5582 pm_wakeup_event(priv->device, 0);
5584 if (priv->dma_cap.estsel)
5585 stmmac_est_irq_status(priv, priv->ioaddr, priv->dev,
5586 &priv->xstats, tx_cnt);
5588 if (priv->dma_cap.fpesel) {
5589 int status = stmmac_fpe_irq_status(priv, priv->ioaddr,
5592 stmmac_fpe_event_status(priv, status);
5595 /* To handle GMAC own interrupts */
5596 if ((priv->plat->has_gmac) || xmac) {
5597 int status = stmmac_host_irq_status(priv, priv->hw, &priv->xstats);
5599 if (unlikely(status)) {
5600 /* For LPI we need to save the tx status */
5601 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
5602 priv->tx_path_in_lpi_mode = true;
5603 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
5604 priv->tx_path_in_lpi_mode = false;
5607 for (queue = 0; queue < queues_count; queue++) {
5608 status = stmmac_host_mtl_irq_status(priv, priv->hw,
5612 /* PCS link status */
5613 if (priv->hw->pcs) {
5614 if (priv->xstats.pcs_link)
5615 netif_carrier_on(priv->dev);
5617 netif_carrier_off(priv->dev);
5620 stmmac_timestamp_interrupt(priv, priv);
5625 * stmmac_interrupt - main ISR
5626 * @irq: interrupt number.
5627 * @dev_id: to pass the net device pointer.
5628 * Description: this is the main driver interrupt service routine.
5630 * o DMA service routine (to manage incoming frame reception and transmission
5632 * o Core interrupts to manage: remote wake-up, management counter, LPI
5635 static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
5637 struct net_device *dev = (struct net_device *)dev_id;
5638 struct stmmac_priv *priv = netdev_priv(dev);
5640 /* Check if adapter is up */
5641 if (test_bit(STMMAC_DOWN, &priv->state))
5644 /* Check if a fatal error happened */
5645 if (stmmac_safety_feat_interrupt(priv))
5648 /* To handle Common interrupts */
5649 stmmac_common_interrupt(priv);
5651 /* To handle DMA interrupts */
5652 stmmac_dma_interrupt(priv);
5657 static irqreturn_t stmmac_mac_interrupt(int irq, void *dev_id)
5659 struct net_device *dev = (struct net_device *)dev_id;
5660 struct stmmac_priv *priv = netdev_priv(dev);
5662 if (unlikely(!dev)) {
5663 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5667 /* Check if adapter is up */
5668 if (test_bit(STMMAC_DOWN, &priv->state))
5671 /* To handle Common interrupts */
5672 stmmac_common_interrupt(priv);
5677 static irqreturn_t stmmac_safety_interrupt(int irq, void *dev_id)
5679 struct net_device *dev = (struct net_device *)dev_id;
5680 struct stmmac_priv *priv = netdev_priv(dev);
5682 if (unlikely(!dev)) {
5683 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5687 /* Check if adapter is up */
5688 if (test_bit(STMMAC_DOWN, &priv->state))
5691 /* Check if a fatal error happened */
5692 stmmac_safety_feat_interrupt(priv);
5697 static irqreturn_t stmmac_msi_intr_tx(int irq, void *data)
5699 struct stmmac_tx_queue *tx_q = (struct stmmac_tx_queue *)data;
5700 int chan = tx_q->queue_index;
5701 struct stmmac_priv *priv;
5704 priv = container_of(tx_q, struct stmmac_priv, tx_queue[chan]);
5706 if (unlikely(!data)) {
5707 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5711 /* Check if adapter is up */
5712 if (test_bit(STMMAC_DOWN, &priv->state))
5715 status = stmmac_napi_check(priv, chan, DMA_DIR_TX);
5717 if (unlikely(status & tx_hard_error_bump_tc)) {
5718 /* Try to bump up the dma threshold on this failure */
5719 stmmac_bump_dma_threshold(priv, chan);
5720 } else if (unlikely(status == tx_hard_error)) {
5721 stmmac_tx_err(priv, chan);
5727 static irqreturn_t stmmac_msi_intr_rx(int irq, void *data)
5729 struct stmmac_rx_queue *rx_q = (struct stmmac_rx_queue *)data;
5730 int chan = rx_q->queue_index;
5731 struct stmmac_priv *priv;
5733 priv = container_of(rx_q, struct stmmac_priv, rx_queue[chan]);
5735 if (unlikely(!data)) {
5736 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5740 /* Check if adapter is up */
5741 if (test_bit(STMMAC_DOWN, &priv->state))
5744 stmmac_napi_check(priv, chan, DMA_DIR_RX);
5749 #ifdef CONFIG_NET_POLL_CONTROLLER
5750 /* Polling receive - used by NETCONSOLE and other diagnostic tools
5751 * to allow network I/O with interrupts disabled.
5753 static void stmmac_poll_controller(struct net_device *dev)
5755 struct stmmac_priv *priv = netdev_priv(dev);
5758 /* If adapter is down, do nothing */
5759 if (test_bit(STMMAC_DOWN, &priv->state))
5762 if (priv->plat->multi_msi_en) {
5763 for (i = 0; i < priv->plat->rx_queues_to_use; i++)
5764 stmmac_msi_intr_rx(0, &priv->rx_queue[i]);
5766 for (i = 0; i < priv->plat->tx_queues_to_use; i++)
5767 stmmac_msi_intr_tx(0, &priv->tx_queue[i]);
5769 disable_irq(dev->irq);
5770 stmmac_interrupt(dev->irq, dev);
5771 enable_irq(dev->irq);
5777 * stmmac_ioctl - Entry point for the Ioctl
5778 * @dev: Device pointer.
5779 * @rq: An IOCTL specefic structure, that can contain a pointer to
5780 * a proprietary structure used to pass information to the driver.
5781 * @cmd: IOCTL command
5783 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
5785 static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
5787 struct stmmac_priv *priv = netdev_priv (dev);
5788 int ret = -EOPNOTSUPP;
5790 if (!netif_running(dev))
5797 ret = phylink_mii_ioctl(priv->phylink, rq, cmd);
5800 ret = stmmac_hwtstamp_set(dev, rq);
5803 ret = stmmac_hwtstamp_get(dev, rq);
5812 static int stmmac_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
5815 struct stmmac_priv *priv = cb_priv;
5816 int ret = -EOPNOTSUPP;
5818 if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
5821 __stmmac_disable_all_queues(priv);
5824 case TC_SETUP_CLSU32:
5825 ret = stmmac_tc_setup_cls_u32(priv, priv, type_data);
5827 case TC_SETUP_CLSFLOWER:
5828 ret = stmmac_tc_setup_cls(priv, priv, type_data);
5834 stmmac_enable_all_queues(priv);
5838 static LIST_HEAD(stmmac_block_cb_list);
5840 static int stmmac_setup_tc(struct net_device *ndev, enum tc_setup_type type,
5843 struct stmmac_priv *priv = netdev_priv(ndev);
5846 case TC_SETUP_BLOCK:
5847 return flow_block_cb_setup_simple(type_data,
5848 &stmmac_block_cb_list,
5849 stmmac_setup_tc_block_cb,
5851 case TC_SETUP_QDISC_CBS:
5852 return stmmac_tc_setup_cbs(priv, priv, type_data);
5853 case TC_SETUP_QDISC_TAPRIO:
5854 return stmmac_tc_setup_taprio(priv, priv, type_data);
5855 case TC_SETUP_QDISC_ETF:
5856 return stmmac_tc_setup_etf(priv, priv, type_data);
5862 static u16 stmmac_select_queue(struct net_device *dev, struct sk_buff *skb,
5863 struct net_device *sb_dev)
5865 int gso = skb_shinfo(skb)->gso_type;
5867 if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6 | SKB_GSO_UDP_L4)) {
5869 * There is no way to determine the number of TSO/USO
5870 * capable Queues. Let's use always the Queue 0
5871 * because if TSO/USO is supported then at least this
5872 * one will be capable.
5877 return netdev_pick_tx(dev, skb, NULL) % dev->real_num_tx_queues;
5880 static int stmmac_set_mac_address(struct net_device *ndev, void *addr)
5882 struct stmmac_priv *priv = netdev_priv(ndev);
5885 ret = pm_runtime_get_sync(priv->device);
5887 pm_runtime_put_noidle(priv->device);
5891 ret = eth_mac_addr(ndev, addr);
5895 stmmac_set_umac_addr(priv, priv->hw, ndev->dev_addr, 0);
5898 pm_runtime_put(priv->device);
5903 #ifdef CONFIG_DEBUG_FS
5904 static struct dentry *stmmac_fs_dir;
5906 static void sysfs_display_ring(void *head, int size, int extend_desc,
5907 struct seq_file *seq, dma_addr_t dma_phy_addr)
5910 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
5911 struct dma_desc *p = (struct dma_desc *)head;
5912 dma_addr_t dma_addr;
5914 for (i = 0; i < size; i++) {
5916 dma_addr = dma_phy_addr + i * sizeof(*ep);
5917 seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n",
5919 le32_to_cpu(ep->basic.des0),
5920 le32_to_cpu(ep->basic.des1),
5921 le32_to_cpu(ep->basic.des2),
5922 le32_to_cpu(ep->basic.des3));
5925 dma_addr = dma_phy_addr + i * sizeof(*p);
5926 seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n",
5928 le32_to_cpu(p->des0), le32_to_cpu(p->des1),
5929 le32_to_cpu(p->des2), le32_to_cpu(p->des3));
5932 seq_printf(seq, "\n");
5936 static int stmmac_rings_status_show(struct seq_file *seq, void *v)
5938 struct net_device *dev = seq->private;
5939 struct stmmac_priv *priv = netdev_priv(dev);
5940 u32 rx_count = priv->plat->rx_queues_to_use;
5941 u32 tx_count = priv->plat->tx_queues_to_use;
5944 if ((dev->flags & IFF_UP) == 0)
5947 for (queue = 0; queue < rx_count; queue++) {
5948 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
5950 seq_printf(seq, "RX Queue %d:\n", queue);
5952 if (priv->extend_desc) {
5953 seq_printf(seq, "Extended descriptor ring:\n");
5954 sysfs_display_ring((void *)rx_q->dma_erx,
5955 priv->dma_rx_size, 1, seq, rx_q->dma_rx_phy);
5957 seq_printf(seq, "Descriptor ring:\n");
5958 sysfs_display_ring((void *)rx_q->dma_rx,
5959 priv->dma_rx_size, 0, seq, rx_q->dma_rx_phy);
5963 for (queue = 0; queue < tx_count; queue++) {
5964 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
5966 seq_printf(seq, "TX Queue %d:\n", queue);
5968 if (priv->extend_desc) {
5969 seq_printf(seq, "Extended descriptor ring:\n");
5970 sysfs_display_ring((void *)tx_q->dma_etx,
5971 priv->dma_tx_size, 1, seq, tx_q->dma_tx_phy);
5972 } else if (!(tx_q->tbs & STMMAC_TBS_AVAIL)) {
5973 seq_printf(seq, "Descriptor ring:\n");
5974 sysfs_display_ring((void *)tx_q->dma_tx,
5975 priv->dma_tx_size, 0, seq, tx_q->dma_tx_phy);
5981 DEFINE_SHOW_ATTRIBUTE(stmmac_rings_status);
5983 static int stmmac_dma_cap_show(struct seq_file *seq, void *v)
5985 struct net_device *dev = seq->private;
5986 struct stmmac_priv *priv = netdev_priv(dev);
5988 if (!priv->hw_cap_support) {
5989 seq_printf(seq, "DMA HW features not supported\n");
5993 seq_printf(seq, "==============================\n");
5994 seq_printf(seq, "\tDMA HW features\n");
5995 seq_printf(seq, "==============================\n");
5997 seq_printf(seq, "\t10/100 Mbps: %s\n",
5998 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
5999 seq_printf(seq, "\t1000 Mbps: %s\n",
6000 (priv->dma_cap.mbps_1000) ? "Y" : "N");
6001 seq_printf(seq, "\tHalf duplex: %s\n",
6002 (priv->dma_cap.half_duplex) ? "Y" : "N");
6003 seq_printf(seq, "\tHash Filter: %s\n",
6004 (priv->dma_cap.hash_filter) ? "Y" : "N");
6005 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
6006 (priv->dma_cap.multi_addr) ? "Y" : "N");
6007 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfaces): %s\n",
6008 (priv->dma_cap.pcs) ? "Y" : "N");
6009 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
6010 (priv->dma_cap.sma_mdio) ? "Y" : "N");
6011 seq_printf(seq, "\tPMT Remote wake up: %s\n",
6012 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
6013 seq_printf(seq, "\tPMT Magic Frame: %s\n",
6014 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
6015 seq_printf(seq, "\tRMON module: %s\n",
6016 (priv->dma_cap.rmon) ? "Y" : "N");
6017 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
6018 (priv->dma_cap.time_stamp) ? "Y" : "N");
6019 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp: %s\n",
6020 (priv->dma_cap.atime_stamp) ? "Y" : "N");
6021 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE): %s\n",
6022 (priv->dma_cap.eee) ? "Y" : "N");
6023 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
6024 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
6025 (priv->dma_cap.tx_coe) ? "Y" : "N");
6026 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
6027 seq_printf(seq, "\tIP Checksum Offload in RX: %s\n",
6028 (priv->dma_cap.rx_coe) ? "Y" : "N");
6030 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
6031 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
6032 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
6033 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
6035 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
6036 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
6037 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
6038 priv->dma_cap.number_rx_channel);
6039 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
6040 priv->dma_cap.number_tx_channel);
6041 seq_printf(seq, "\tNumber of Additional RX queues: %d\n",
6042 priv->dma_cap.number_rx_queues);
6043 seq_printf(seq, "\tNumber of Additional TX queues: %d\n",
6044 priv->dma_cap.number_tx_queues);
6045 seq_printf(seq, "\tEnhanced descriptors: %s\n",
6046 (priv->dma_cap.enh_desc) ? "Y" : "N");
6047 seq_printf(seq, "\tTX Fifo Size: %d\n", priv->dma_cap.tx_fifo_size);
6048 seq_printf(seq, "\tRX Fifo Size: %d\n", priv->dma_cap.rx_fifo_size);
6049 seq_printf(seq, "\tHash Table Size: %d\n", priv->dma_cap.hash_tb_sz);
6050 seq_printf(seq, "\tTSO: %s\n", priv->dma_cap.tsoen ? "Y" : "N");
6051 seq_printf(seq, "\tNumber of PPS Outputs: %d\n",
6052 priv->dma_cap.pps_out_num);
6053 seq_printf(seq, "\tSafety Features: %s\n",
6054 priv->dma_cap.asp ? "Y" : "N");
6055 seq_printf(seq, "\tFlexible RX Parser: %s\n",
6056 priv->dma_cap.frpsel ? "Y" : "N");
6057 seq_printf(seq, "\tEnhanced Addressing: %d\n",
6058 priv->dma_cap.addr64);
6059 seq_printf(seq, "\tReceive Side Scaling: %s\n",
6060 priv->dma_cap.rssen ? "Y" : "N");
6061 seq_printf(seq, "\tVLAN Hash Filtering: %s\n",
6062 priv->dma_cap.vlhash ? "Y" : "N");
6063 seq_printf(seq, "\tSplit Header: %s\n",
6064 priv->dma_cap.sphen ? "Y" : "N");
6065 seq_printf(seq, "\tVLAN TX Insertion: %s\n",
6066 priv->dma_cap.vlins ? "Y" : "N");
6067 seq_printf(seq, "\tDouble VLAN: %s\n",
6068 priv->dma_cap.dvlan ? "Y" : "N");
6069 seq_printf(seq, "\tNumber of L3/L4 Filters: %d\n",
6070 priv->dma_cap.l3l4fnum);
6071 seq_printf(seq, "\tARP Offloading: %s\n",
6072 priv->dma_cap.arpoffsel ? "Y" : "N");
6073 seq_printf(seq, "\tEnhancements to Scheduled Traffic (EST): %s\n",
6074 priv->dma_cap.estsel ? "Y" : "N");
6075 seq_printf(seq, "\tFrame Preemption (FPE): %s\n",
6076 priv->dma_cap.fpesel ? "Y" : "N");
6077 seq_printf(seq, "\tTime-Based Scheduling (TBS): %s\n",
6078 priv->dma_cap.tbssel ? "Y" : "N");
6081 DEFINE_SHOW_ATTRIBUTE(stmmac_dma_cap);
6083 /* Use network device events to rename debugfs file entries.
6085 static int stmmac_device_event(struct notifier_block *unused,
6086 unsigned long event, void *ptr)
6088 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6089 struct stmmac_priv *priv = netdev_priv(dev);
6091 if (dev->netdev_ops != &stmmac_netdev_ops)
6095 case NETDEV_CHANGENAME:
6096 if (priv->dbgfs_dir)
6097 priv->dbgfs_dir = debugfs_rename(stmmac_fs_dir,
6107 static struct notifier_block stmmac_notifier = {
6108 .notifier_call = stmmac_device_event,
6111 static void stmmac_init_fs(struct net_device *dev)
6113 struct stmmac_priv *priv = netdev_priv(dev);
6117 /* Create per netdev entries */
6118 priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
6120 /* Entry to report DMA RX/TX rings */
6121 debugfs_create_file("descriptors_status", 0444, priv->dbgfs_dir, dev,
6122 &stmmac_rings_status_fops);
6124 /* Entry to report the DMA HW features */
6125 debugfs_create_file("dma_cap", 0444, priv->dbgfs_dir, dev,
6126 &stmmac_dma_cap_fops);
6131 static void stmmac_exit_fs(struct net_device *dev)
6133 struct stmmac_priv *priv = netdev_priv(dev);
6135 debugfs_remove_recursive(priv->dbgfs_dir);
6137 #endif /* CONFIG_DEBUG_FS */
6139 static u32 stmmac_vid_crc32_le(__le16 vid_le)
6141 unsigned char *data = (unsigned char *)&vid_le;
6142 unsigned char data_byte = 0;
6147 bits = get_bitmask_order(VLAN_VID_MASK);
6148 for (i = 0; i < bits; i++) {
6150 data_byte = data[i / 8];
6152 temp = ((crc & 1) ^ data_byte) & 1;
6163 static int stmmac_vlan_update(struct stmmac_priv *priv, bool is_double)
6170 for_each_set_bit(vid, priv->active_vlans, VLAN_N_VID) {
6171 __le16 vid_le = cpu_to_le16(vid);
6172 crc = bitrev32(~stmmac_vid_crc32_le(vid_le)) >> 28;
6177 if (!priv->dma_cap.vlhash) {
6178 if (count > 2) /* VID = 0 always passes filter */
6181 pmatch = cpu_to_le16(vid);
6185 return stmmac_update_vlan_hash(priv, priv->hw, hash, pmatch, is_double);
6188 static int stmmac_vlan_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid)
6190 struct stmmac_priv *priv = netdev_priv(ndev);
6191 bool is_double = false;
6194 if (be16_to_cpu(proto) == ETH_P_8021AD)
6197 set_bit(vid, priv->active_vlans);
6198 ret = stmmac_vlan_update(priv, is_double);
6200 clear_bit(vid, priv->active_vlans);
6204 if (priv->hw->num_vlan) {
6205 ret = stmmac_add_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid);
6213 static int stmmac_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid)
6215 struct stmmac_priv *priv = netdev_priv(ndev);
6216 bool is_double = false;
6219 ret = pm_runtime_get_sync(priv->device);
6221 pm_runtime_put_noidle(priv->device);
6225 if (be16_to_cpu(proto) == ETH_P_8021AD)
6228 clear_bit(vid, priv->active_vlans);
6230 if (priv->hw->num_vlan) {
6231 ret = stmmac_del_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid);
6233 goto del_vlan_error;
6236 ret = stmmac_vlan_update(priv, is_double);
6239 pm_runtime_put(priv->device);
6244 static int stmmac_bpf(struct net_device *dev, struct netdev_bpf *bpf)
6246 struct stmmac_priv *priv = netdev_priv(dev);
6248 switch (bpf->command) {
6249 case XDP_SETUP_PROG:
6250 return stmmac_xdp_set_prog(priv, bpf->prog, bpf->extack);
6251 case XDP_SETUP_XSK_POOL:
6252 return stmmac_xdp_setup_pool(priv, bpf->xsk.pool,
6259 static int stmmac_xdp_xmit(struct net_device *dev, int num_frames,
6260 struct xdp_frame **frames, u32 flags)
6262 struct stmmac_priv *priv = netdev_priv(dev);
6263 int cpu = smp_processor_id();
6264 struct netdev_queue *nq;
6268 if (unlikely(test_bit(STMMAC_DOWN, &priv->state)))
6271 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
6274 queue = stmmac_xdp_get_tx_queue(priv, cpu);
6275 nq = netdev_get_tx_queue(priv->dev, queue);
6277 __netif_tx_lock(nq, cpu);
6278 /* Avoids TX time-out as we are sharing with slow path */
6279 txq_trans_cond_update(nq);
6281 for (i = 0; i < num_frames; i++) {
6284 res = stmmac_xdp_xmit_xdpf(priv, queue, frames[i], true);
6285 if (res == STMMAC_XDP_CONSUMED)
6291 if (flags & XDP_XMIT_FLUSH) {
6292 stmmac_flush_tx_descriptors(priv, queue);
6293 stmmac_tx_timer_arm(priv, queue);
6296 __netif_tx_unlock(nq);
6301 void stmmac_disable_rx_queue(struct stmmac_priv *priv, u32 queue)
6303 struct stmmac_channel *ch = &priv->channel[queue];
6304 unsigned long flags;
6306 spin_lock_irqsave(&ch->lock, flags);
6307 stmmac_disable_dma_irq(priv, priv->ioaddr, queue, 1, 0);
6308 spin_unlock_irqrestore(&ch->lock, flags);
6310 stmmac_stop_rx_dma(priv, queue);
6311 __free_dma_rx_desc_resources(priv, queue);
6314 void stmmac_enable_rx_queue(struct stmmac_priv *priv, u32 queue)
6316 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
6317 struct stmmac_channel *ch = &priv->channel[queue];
6318 unsigned long flags;
6322 ret = __alloc_dma_rx_desc_resources(priv, queue);
6324 netdev_err(priv->dev, "Failed to alloc RX desc.\n");
6328 ret = __init_dma_rx_desc_rings(priv, queue, GFP_KERNEL);
6330 __free_dma_rx_desc_resources(priv, queue);
6331 netdev_err(priv->dev, "Failed to init RX desc.\n");
6335 stmmac_clear_rx_descriptors(priv, queue);
6337 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6338 rx_q->dma_rx_phy, rx_q->queue_index);
6340 rx_q->rx_tail_addr = rx_q->dma_rx_phy + (rx_q->buf_alloc_num *
6341 sizeof(struct dma_desc));
6342 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
6343 rx_q->rx_tail_addr, rx_q->queue_index);
6345 if (rx_q->xsk_pool && rx_q->buf_alloc_num) {
6346 buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool);
6347 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6351 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6356 stmmac_start_rx_dma(priv, queue);
6358 spin_lock_irqsave(&ch->lock, flags);
6359 stmmac_enable_dma_irq(priv, priv->ioaddr, queue, 1, 0);
6360 spin_unlock_irqrestore(&ch->lock, flags);
6363 void stmmac_disable_tx_queue(struct stmmac_priv *priv, u32 queue)
6365 struct stmmac_channel *ch = &priv->channel[queue];
6366 unsigned long flags;
6368 spin_lock_irqsave(&ch->lock, flags);
6369 stmmac_disable_dma_irq(priv, priv->ioaddr, queue, 0, 1);
6370 spin_unlock_irqrestore(&ch->lock, flags);
6372 stmmac_stop_tx_dma(priv, queue);
6373 __free_dma_tx_desc_resources(priv, queue);
6376 void stmmac_enable_tx_queue(struct stmmac_priv *priv, u32 queue)
6378 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
6379 struct stmmac_channel *ch = &priv->channel[queue];
6380 unsigned long flags;
6383 ret = __alloc_dma_tx_desc_resources(priv, queue);
6385 netdev_err(priv->dev, "Failed to alloc TX desc.\n");
6389 ret = __init_dma_tx_desc_rings(priv, queue);
6391 __free_dma_tx_desc_resources(priv, queue);
6392 netdev_err(priv->dev, "Failed to init TX desc.\n");
6396 stmmac_clear_tx_descriptors(priv, queue);
6398 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6399 tx_q->dma_tx_phy, tx_q->queue_index);
6401 if (tx_q->tbs & STMMAC_TBS_AVAIL)
6402 stmmac_enable_tbs(priv, priv->ioaddr, 1, tx_q->queue_index);
6404 tx_q->tx_tail_addr = tx_q->dma_tx_phy;
6405 stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
6406 tx_q->tx_tail_addr, tx_q->queue_index);
6408 stmmac_start_tx_dma(priv, queue);
6410 spin_lock_irqsave(&ch->lock, flags);
6411 stmmac_enable_dma_irq(priv, priv->ioaddr, queue, 0, 1);
6412 spin_unlock_irqrestore(&ch->lock, flags);
6415 void stmmac_xdp_release(struct net_device *dev)
6417 struct stmmac_priv *priv = netdev_priv(dev);
6420 /* Disable NAPI process */
6421 stmmac_disable_all_queues(priv);
6423 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
6424 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
6426 /* Free the IRQ lines */
6427 stmmac_free_irq(dev, REQ_IRQ_ERR_ALL, 0);
6429 /* Stop TX/RX DMA channels */
6430 stmmac_stop_all_dma(priv);
6432 /* Release and free the Rx/Tx resources */
6433 free_dma_desc_resources(priv);
6435 /* Disable the MAC Rx/Tx */
6436 stmmac_mac_set(priv, priv->ioaddr, false);
6438 /* set trans_start so we don't get spurious
6439 * watchdogs during reset
6441 netif_trans_update(dev);
6442 netif_carrier_off(dev);
6445 int stmmac_xdp_open(struct net_device *dev)
6447 struct stmmac_priv *priv = netdev_priv(dev);
6448 u32 rx_cnt = priv->plat->rx_queues_to_use;
6449 u32 tx_cnt = priv->plat->tx_queues_to_use;
6450 u32 dma_csr_ch = max(rx_cnt, tx_cnt);
6451 struct stmmac_rx_queue *rx_q;
6452 struct stmmac_tx_queue *tx_q;
6458 ret = alloc_dma_desc_resources(priv);
6460 netdev_err(dev, "%s: DMA descriptors allocation failed\n",
6462 goto dma_desc_error;
6465 ret = init_dma_desc_rings(dev, GFP_KERNEL);
6467 netdev_err(dev, "%s: DMA descriptors initialization failed\n",
6472 /* DMA CSR Channel configuration */
6473 for (chan = 0; chan < dma_csr_ch; chan++)
6474 stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan);
6476 /* Adjust Split header */
6477 sph_en = (priv->hw->rx_csum > 0) && priv->sph;
6479 /* DMA RX Channel Configuration */
6480 for (chan = 0; chan < rx_cnt; chan++) {
6481 rx_q = &priv->rx_queue[chan];
6483 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6484 rx_q->dma_rx_phy, chan);
6486 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
6487 (rx_q->buf_alloc_num *
6488 sizeof(struct dma_desc));
6489 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
6490 rx_q->rx_tail_addr, chan);
6492 if (rx_q->xsk_pool && rx_q->buf_alloc_num) {
6493 buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool);
6494 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6498 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6503 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
6506 /* DMA TX Channel Configuration */
6507 for (chan = 0; chan < tx_cnt; chan++) {
6508 tx_q = &priv->tx_queue[chan];
6510 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6511 tx_q->dma_tx_phy, chan);
6513 tx_q->tx_tail_addr = tx_q->dma_tx_phy;
6514 stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
6515 tx_q->tx_tail_addr, chan);
6517 hrtimer_init(&tx_q->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6518 tx_q->txtimer.function = stmmac_tx_timer;
6521 /* Enable the MAC Rx/Tx */
6522 stmmac_mac_set(priv, priv->ioaddr, true);
6524 /* Start Rx & Tx DMA Channels */
6525 stmmac_start_all_dma(priv);
6527 ret = stmmac_request_irq(dev);
6531 /* Enable NAPI process*/
6532 stmmac_enable_all_queues(priv);
6533 netif_carrier_on(dev);
6534 netif_tx_start_all_queues(dev);
6539 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
6540 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
6542 stmmac_hw_teardown(dev);
6544 free_dma_desc_resources(priv);
6549 int stmmac_xsk_wakeup(struct net_device *dev, u32 queue, u32 flags)
6551 struct stmmac_priv *priv = netdev_priv(dev);
6552 struct stmmac_rx_queue *rx_q;
6553 struct stmmac_tx_queue *tx_q;
6554 struct stmmac_channel *ch;
6556 if (test_bit(STMMAC_DOWN, &priv->state) ||
6557 !netif_carrier_ok(priv->dev))
6560 if (!stmmac_xdp_is_enabled(priv))
6563 if (queue >= priv->plat->rx_queues_to_use ||
6564 queue >= priv->plat->tx_queues_to_use)
6567 rx_q = &priv->rx_queue[queue];
6568 tx_q = &priv->tx_queue[queue];
6569 ch = &priv->channel[queue];
6571 if (!rx_q->xsk_pool && !tx_q->xsk_pool)
6574 if (!napi_if_scheduled_mark_missed(&ch->rxtx_napi)) {
6575 /* EQoS does not have per-DMA channel SW interrupt,
6576 * so we schedule RX Napi straight-away.
6578 if (likely(napi_schedule_prep(&ch->rxtx_napi)))
6579 __napi_schedule(&ch->rxtx_napi);
6585 static const struct net_device_ops stmmac_netdev_ops = {
6586 .ndo_open = stmmac_open,
6587 .ndo_start_xmit = stmmac_xmit,
6588 .ndo_stop = stmmac_release,
6589 .ndo_change_mtu = stmmac_change_mtu,
6590 .ndo_fix_features = stmmac_fix_features,
6591 .ndo_set_features = stmmac_set_features,
6592 .ndo_set_rx_mode = stmmac_set_rx_mode,
6593 .ndo_tx_timeout = stmmac_tx_timeout,
6594 .ndo_eth_ioctl = stmmac_ioctl,
6595 .ndo_setup_tc = stmmac_setup_tc,
6596 .ndo_select_queue = stmmac_select_queue,
6597 #ifdef CONFIG_NET_POLL_CONTROLLER
6598 .ndo_poll_controller = stmmac_poll_controller,
6600 .ndo_set_mac_address = stmmac_set_mac_address,
6601 .ndo_vlan_rx_add_vid = stmmac_vlan_rx_add_vid,
6602 .ndo_vlan_rx_kill_vid = stmmac_vlan_rx_kill_vid,
6603 .ndo_bpf = stmmac_bpf,
6604 .ndo_xdp_xmit = stmmac_xdp_xmit,
6605 .ndo_xsk_wakeup = stmmac_xsk_wakeup,
6608 static void stmmac_reset_subtask(struct stmmac_priv *priv)
6610 if (!test_and_clear_bit(STMMAC_RESET_REQUESTED, &priv->state))
6612 if (test_bit(STMMAC_DOWN, &priv->state))
6615 netdev_err(priv->dev, "Reset adapter.\n");
6618 netif_trans_update(priv->dev);
6619 while (test_and_set_bit(STMMAC_RESETING, &priv->state))
6620 usleep_range(1000, 2000);
6622 set_bit(STMMAC_DOWN, &priv->state);
6623 dev_close(priv->dev);
6624 dev_open(priv->dev, NULL);
6625 clear_bit(STMMAC_DOWN, &priv->state);
6626 clear_bit(STMMAC_RESETING, &priv->state);
6630 static void stmmac_service_task(struct work_struct *work)
6632 struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
6635 stmmac_reset_subtask(priv);
6636 clear_bit(STMMAC_SERVICE_SCHED, &priv->state);
6640 * stmmac_hw_init - Init the MAC device
6641 * @priv: driver private structure
6642 * Description: this function is to configure the MAC device according to
6643 * some platform parameters or the HW capability register. It prepares the
6644 * driver to use either ring or chain modes and to setup either enhanced or
6645 * normal descriptors.
6647 static int stmmac_hw_init(struct stmmac_priv *priv)
6651 /* dwmac-sun8i only work in chain mode */
6652 if (priv->plat->has_sun8i)
6654 priv->chain_mode = chain_mode;
6656 /* Initialize HW Interface */
6657 ret = stmmac_hwif_init(priv);
6661 /* Get the HW capability (new GMAC newer than 3.50a) */
6662 priv->hw_cap_support = stmmac_get_hw_features(priv);
6663 if (priv->hw_cap_support) {
6664 dev_info(priv->device, "DMA HW capability register supported\n");
6666 /* We can override some gmac/dma configuration fields: e.g.
6667 * enh_desc, tx_coe (e.g. that are passed through the
6668 * platform) with the values from the HW capability
6669 * register (if supported).
6671 priv->plat->enh_desc = priv->dma_cap.enh_desc;
6672 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up &&
6673 !priv->plat->use_phy_wol;
6674 priv->hw->pmt = priv->plat->pmt;
6675 if (priv->dma_cap.hash_tb_sz) {
6676 priv->hw->multicast_filter_bins =
6677 (BIT(priv->dma_cap.hash_tb_sz) << 5);
6678 priv->hw->mcast_bits_log2 =
6679 ilog2(priv->hw->multicast_filter_bins);
6682 /* TXCOE doesn't work in thresh DMA mode */
6683 if (priv->plat->force_thresh_dma_mode)
6684 priv->plat->tx_coe = 0;
6686 priv->plat->tx_coe = priv->dma_cap.tx_coe;
6688 /* In case of GMAC4 rx_coe is from HW cap register. */
6689 priv->plat->rx_coe = priv->dma_cap.rx_coe;
6691 if (priv->dma_cap.rx_coe_type2)
6692 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
6693 else if (priv->dma_cap.rx_coe_type1)
6694 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
6697 dev_info(priv->device, "No HW DMA feature register supported\n");
6700 if (priv->plat->rx_coe) {
6701 priv->hw->rx_csum = priv->plat->rx_coe;
6702 dev_info(priv->device, "RX Checksum Offload Engine supported\n");
6703 if (priv->synopsys_id < DWMAC_CORE_4_00)
6704 dev_info(priv->device, "COE Type %d\n", priv->hw->rx_csum);
6706 if (priv->plat->tx_coe)
6707 dev_info(priv->device, "TX Checksum insertion supported\n");
6709 if (priv->plat->pmt) {
6710 dev_info(priv->device, "Wake-Up On Lan supported\n");
6711 device_set_wakeup_capable(priv->device, 1);
6714 if (priv->dma_cap.tsoen)
6715 dev_info(priv->device, "TSO supported\n");
6717 priv->hw->vlan_fail_q_en = priv->plat->vlan_fail_q_en;
6718 priv->hw->vlan_fail_q = priv->plat->vlan_fail_q;
6720 /* Run HW quirks, if any */
6721 if (priv->hwif_quirks) {
6722 ret = priv->hwif_quirks(priv);
6727 /* Rx Watchdog is available in the COREs newer than the 3.40.
6728 * In some case, for example on bugged HW this feature
6729 * has to be disable and this can be done by passing the
6730 * riwt_off field from the platform.
6732 if (((priv->synopsys_id >= DWMAC_CORE_3_50) ||
6733 (priv->plat->has_xgmac)) && (!priv->plat->riwt_off)) {
6735 dev_info(priv->device,
6736 "Enable RX Mitigation via HW Watchdog Timer\n");
6742 static void stmmac_napi_add(struct net_device *dev)
6744 struct stmmac_priv *priv = netdev_priv(dev);
6747 maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
6749 for (queue = 0; queue < maxq; queue++) {
6750 struct stmmac_channel *ch = &priv->channel[queue];
6752 ch->priv_data = priv;
6754 spin_lock_init(&ch->lock);
6756 if (queue < priv->plat->rx_queues_to_use) {
6757 netif_napi_add(dev, &ch->rx_napi, stmmac_napi_poll_rx,
6760 if (queue < priv->plat->tx_queues_to_use) {
6761 netif_tx_napi_add(dev, &ch->tx_napi,
6762 stmmac_napi_poll_tx,
6765 if (queue < priv->plat->rx_queues_to_use &&
6766 queue < priv->plat->tx_queues_to_use) {
6767 netif_napi_add(dev, &ch->rxtx_napi,
6768 stmmac_napi_poll_rxtx,
6774 static void stmmac_napi_del(struct net_device *dev)
6776 struct stmmac_priv *priv = netdev_priv(dev);
6779 maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
6781 for (queue = 0; queue < maxq; queue++) {
6782 struct stmmac_channel *ch = &priv->channel[queue];
6784 if (queue < priv->plat->rx_queues_to_use)
6785 netif_napi_del(&ch->rx_napi);
6786 if (queue < priv->plat->tx_queues_to_use)
6787 netif_napi_del(&ch->tx_napi);
6788 if (queue < priv->plat->rx_queues_to_use &&
6789 queue < priv->plat->tx_queues_to_use) {
6790 netif_napi_del(&ch->rxtx_napi);
6795 int stmmac_reinit_queues(struct net_device *dev, u32 rx_cnt, u32 tx_cnt)
6797 struct stmmac_priv *priv = netdev_priv(dev);
6800 if (netif_running(dev))
6801 stmmac_release(dev);
6803 stmmac_napi_del(dev);
6805 priv->plat->rx_queues_to_use = rx_cnt;
6806 priv->plat->tx_queues_to_use = tx_cnt;
6808 stmmac_napi_add(dev);
6810 if (netif_running(dev))
6811 ret = stmmac_open(dev);
6816 int stmmac_reinit_ringparam(struct net_device *dev, u32 rx_size, u32 tx_size)
6818 struct stmmac_priv *priv = netdev_priv(dev);
6821 if (netif_running(dev))
6822 stmmac_release(dev);
6824 priv->dma_rx_size = rx_size;
6825 priv->dma_tx_size = tx_size;
6827 if (netif_running(dev))
6828 ret = stmmac_open(dev);
6833 #define SEND_VERIFY_MPAKCET_FMT "Send Verify mPacket lo_state=%d lp_state=%d\n"
6834 static void stmmac_fpe_lp_task(struct work_struct *work)
6836 struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
6838 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
6839 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
6840 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
6841 bool *hs_enable = &fpe_cfg->hs_enable;
6842 bool *enable = &fpe_cfg->enable;
6845 while (retries-- > 0) {
6846 /* Bail out immediately if FPE handshake is OFF */
6847 if (*lo_state == FPE_STATE_OFF || !*hs_enable)
6850 if (*lo_state == FPE_STATE_ENTERING_ON &&
6851 *lp_state == FPE_STATE_ENTERING_ON) {
6852 stmmac_fpe_configure(priv, priv->ioaddr,
6853 priv->plat->tx_queues_to_use,
6854 priv->plat->rx_queues_to_use,
6857 netdev_info(priv->dev, "configured FPE\n");
6859 *lo_state = FPE_STATE_ON;
6860 *lp_state = FPE_STATE_ON;
6861 netdev_info(priv->dev, "!!! BOTH FPE stations ON\n");
6865 if ((*lo_state == FPE_STATE_CAPABLE ||
6866 *lo_state == FPE_STATE_ENTERING_ON) &&
6867 *lp_state != FPE_STATE_ON) {
6868 netdev_info(priv->dev, SEND_VERIFY_MPAKCET_FMT,
6869 *lo_state, *lp_state);
6870 stmmac_fpe_send_mpacket(priv, priv->ioaddr,
6873 /* Sleep then retry */
6877 clear_bit(__FPE_TASK_SCHED, &priv->fpe_task_state);
6880 void stmmac_fpe_handshake(struct stmmac_priv *priv, bool enable)
6882 if (priv->plat->fpe_cfg->hs_enable != enable) {
6884 stmmac_fpe_send_mpacket(priv, priv->ioaddr,
6887 priv->plat->fpe_cfg->lo_fpe_state = FPE_STATE_OFF;
6888 priv->plat->fpe_cfg->lp_fpe_state = FPE_STATE_OFF;
6891 priv->plat->fpe_cfg->hs_enable = enable;
6897 * @device: device pointer
6898 * @plat_dat: platform data pointer
6899 * @res: stmmac resource pointer
6900 * Description: this is the main probe function used to
6901 * call the alloc_etherdev, allocate the priv structure.
6903 * returns 0 on success, otherwise errno.
6905 int stmmac_dvr_probe(struct device *device,
6906 struct plat_stmmacenet_data *plat_dat,
6907 struct stmmac_resources *res)
6909 struct net_device *ndev = NULL;
6910 struct stmmac_priv *priv;
6914 ndev = devm_alloc_etherdev_mqs(device, sizeof(struct stmmac_priv),
6915 MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES);
6919 SET_NETDEV_DEV(ndev, device);
6921 priv = netdev_priv(ndev);
6922 priv->device = device;
6925 stmmac_set_ethtool_ops(ndev);
6926 priv->pause = pause;
6927 priv->plat = plat_dat;
6928 priv->ioaddr = res->addr;
6929 priv->dev->base_addr = (unsigned long)res->addr;
6930 priv->plat->dma_cfg->multi_msi_en = priv->plat->multi_msi_en;
6932 priv->dev->irq = res->irq;
6933 priv->wol_irq = res->wol_irq;
6934 priv->lpi_irq = res->lpi_irq;
6935 priv->sfty_ce_irq = res->sfty_ce_irq;
6936 priv->sfty_ue_irq = res->sfty_ue_irq;
6937 for (i = 0; i < MTL_MAX_RX_QUEUES; i++)
6938 priv->rx_irq[i] = res->rx_irq[i];
6939 for (i = 0; i < MTL_MAX_TX_QUEUES; i++)
6940 priv->tx_irq[i] = res->tx_irq[i];
6942 if (!is_zero_ether_addr(res->mac))
6943 eth_hw_addr_set(priv->dev, res->mac);
6945 dev_set_drvdata(device, priv->dev);
6947 /* Verify driver arguments */
6948 stmmac_verify_args();
6950 priv->af_xdp_zc_qps = bitmap_zalloc(MTL_MAX_TX_QUEUES, GFP_KERNEL);
6951 if (!priv->af_xdp_zc_qps)
6954 /* Allocate workqueue */
6955 priv->wq = create_singlethread_workqueue("stmmac_wq");
6957 dev_err(priv->device, "failed to create workqueue\n");
6961 INIT_WORK(&priv->service_task, stmmac_service_task);
6963 /* Initialize Link Partner FPE workqueue */
6964 INIT_WORK(&priv->fpe_task, stmmac_fpe_lp_task);
6966 /* Override with kernel parameters if supplied XXX CRS XXX
6967 * this needs to have multiple instances
6969 if ((phyaddr >= 0) && (phyaddr <= 31))
6970 priv->plat->phy_addr = phyaddr;
6972 if (priv->plat->stmmac_rst) {
6973 ret = reset_control_assert(priv->plat->stmmac_rst);
6974 reset_control_deassert(priv->plat->stmmac_rst);
6975 /* Some reset controllers have only reset callback instead of
6976 * assert + deassert callbacks pair.
6978 if (ret == -ENOTSUPP)
6979 reset_control_reset(priv->plat->stmmac_rst);
6982 ret = reset_control_deassert(priv->plat->stmmac_ahb_rst);
6983 if (ret == -ENOTSUPP)
6984 dev_err(priv->device, "unable to bring out of ahb reset: %pe\n",
6987 /* Init MAC and get the capabilities */
6988 ret = stmmac_hw_init(priv);
6992 /* Only DWMAC core version 5.20 onwards supports HW descriptor prefetch.
6994 if (priv->synopsys_id < DWMAC_CORE_5_20)
6995 priv->plat->dma_cfg->dche = false;
6997 stmmac_check_ether_addr(priv);
6999 ndev->netdev_ops = &stmmac_netdev_ops;
7001 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
7004 ret = stmmac_tc_init(priv, priv);
7006 ndev->hw_features |= NETIF_F_HW_TC;
7009 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
7010 ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
7011 if (priv->plat->has_gmac4)
7012 ndev->hw_features |= NETIF_F_GSO_UDP_L4;
7014 dev_info(priv->device, "TSO feature enabled\n");
7017 if (priv->dma_cap.sphen) {
7018 ndev->hw_features |= NETIF_F_GRO;
7019 priv->sph_cap = true;
7020 priv->sph = priv->sph_cap;
7021 dev_info(priv->device, "SPH feature enabled\n");
7024 /* The current IP register MAC_HW_Feature1[ADDR64] only define
7025 * 32/40/64 bit width, but some SOC support others like i.MX8MP
7026 * support 34 bits but it map to 40 bits width in MAC_HW_Feature1[ADDR64].
7027 * So overwrite dma_cap.addr64 according to HW real design.
7029 if (priv->plat->addr64)
7030 priv->dma_cap.addr64 = priv->plat->addr64;
7032 if (priv->dma_cap.addr64) {
7033 ret = dma_set_mask_and_coherent(device,
7034 DMA_BIT_MASK(priv->dma_cap.addr64));
7036 dev_info(priv->device, "Using %d bits DMA width\n",
7037 priv->dma_cap.addr64);
7040 * If more than 32 bits can be addressed, make sure to
7041 * enable enhanced addressing mode.
7043 if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
7044 priv->plat->dma_cfg->eame = true;
7046 ret = dma_set_mask_and_coherent(device, DMA_BIT_MASK(32));
7048 dev_err(priv->device, "Failed to set DMA Mask\n");
7052 priv->dma_cap.addr64 = 32;
7056 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
7057 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
7058 #ifdef STMMAC_VLAN_TAG_USED
7059 /* Both mac100 and gmac support receive VLAN tag detection */
7060 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX;
7061 if (priv->dma_cap.vlhash) {
7062 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
7063 ndev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
7065 if (priv->dma_cap.vlins) {
7066 ndev->features |= NETIF_F_HW_VLAN_CTAG_TX;
7067 if (priv->dma_cap.dvlan)
7068 ndev->features |= NETIF_F_HW_VLAN_STAG_TX;
7071 priv->msg_enable = netif_msg_init(debug, default_msg_level);
7073 /* Initialize RSS */
7074 rxq = priv->plat->rx_queues_to_use;
7075 netdev_rss_key_fill(priv->rss.key, sizeof(priv->rss.key));
7076 for (i = 0; i < ARRAY_SIZE(priv->rss.table); i++)
7077 priv->rss.table[i] = ethtool_rxfh_indir_default(i, rxq);
7079 if (priv->dma_cap.rssen && priv->plat->rss_en)
7080 ndev->features |= NETIF_F_RXHASH;
7082 /* MTU range: 46 - hw-specific max */
7083 ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
7084 if (priv->plat->has_xgmac)
7085 ndev->max_mtu = XGMAC_JUMBO_LEN;
7086 else if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
7087 ndev->max_mtu = JUMBO_LEN;
7089 ndev->max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
7090 /* Will not overwrite ndev->max_mtu if plat->maxmtu > ndev->max_mtu
7091 * as well as plat->maxmtu < ndev->min_mtu which is a invalid range.
7093 if ((priv->plat->maxmtu < ndev->max_mtu) &&
7094 (priv->plat->maxmtu >= ndev->min_mtu))
7095 ndev->max_mtu = priv->plat->maxmtu;
7096 else if (priv->plat->maxmtu < ndev->min_mtu)
7097 dev_warn(priv->device,
7098 "%s: warning: maxmtu having invalid value (%d)\n",
7099 __func__, priv->plat->maxmtu);
7102 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
7104 /* Setup channels NAPI */
7105 stmmac_napi_add(ndev);
7107 mutex_init(&priv->lock);
7109 /* If a specific clk_csr value is passed from the platform
7110 * this means that the CSR Clock Range selection cannot be
7111 * changed at run-time and it is fixed. Viceversa the driver'll try to
7112 * set the MDC clock dynamically according to the csr actual
7115 if (priv->plat->clk_csr >= 0)
7116 priv->clk_csr = priv->plat->clk_csr;
7118 stmmac_clk_csr_set(priv);
7120 stmmac_check_pcs_mode(priv);
7122 pm_runtime_get_noresume(device);
7123 pm_runtime_set_active(device);
7124 if (!pm_runtime_enabled(device))
7125 pm_runtime_enable(device);
7127 if (priv->hw->pcs != STMMAC_PCS_TBI &&
7128 priv->hw->pcs != STMMAC_PCS_RTBI) {
7129 /* MDIO bus Registration */
7130 ret = stmmac_mdio_register(ndev);
7132 dev_err(priv->device,
7133 "%s: MDIO bus (id: %d) registration failed",
7134 __func__, priv->plat->bus_id);
7135 goto error_mdio_register;
7139 if (priv->plat->speed_mode_2500)
7140 priv->plat->speed_mode_2500(ndev, priv->plat->bsp_priv);
7142 if (priv->plat->mdio_bus_data && priv->plat->mdio_bus_data->has_xpcs) {
7143 ret = stmmac_xpcs_setup(priv->mii);
7145 goto error_xpcs_setup;
7148 ret = stmmac_phy_setup(priv);
7150 netdev_err(ndev, "failed to setup phy (%d)\n", ret);
7151 goto error_phy_setup;
7154 ret = register_netdev(ndev);
7156 dev_err(priv->device, "%s: ERROR %i registering the device\n",
7158 goto error_netdev_register;
7161 if (priv->plat->serdes_powerup) {
7162 ret = priv->plat->serdes_powerup(ndev,
7163 priv->plat->bsp_priv);
7166 goto error_serdes_powerup;
7169 #ifdef CONFIG_DEBUG_FS
7170 stmmac_init_fs(ndev);
7173 if (priv->plat->dump_debug_regs)
7174 priv->plat->dump_debug_regs(priv->plat->bsp_priv);
7176 /* Let pm_runtime_put() disable the clocks.
7177 * If CONFIG_PM is not enabled, the clocks will stay powered.
7179 pm_runtime_put(device);
7183 error_serdes_powerup:
7184 unregister_netdev(ndev);
7185 error_netdev_register:
7186 phylink_destroy(priv->phylink);
7189 if (priv->hw->pcs != STMMAC_PCS_TBI &&
7190 priv->hw->pcs != STMMAC_PCS_RTBI)
7191 stmmac_mdio_unregister(ndev);
7192 error_mdio_register:
7193 stmmac_napi_del(ndev);
7195 destroy_workqueue(priv->wq);
7196 bitmap_free(priv->af_xdp_zc_qps);
7200 EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
7204 * @dev: device pointer
7205 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
7206 * changes the link status, releases the DMA descriptor rings.
7208 int stmmac_dvr_remove(struct device *dev)
7210 struct net_device *ndev = dev_get_drvdata(dev);
7211 struct stmmac_priv *priv = netdev_priv(ndev);
7213 netdev_info(priv->dev, "%s: removing driver", __func__);
7215 stmmac_stop_all_dma(priv);
7216 stmmac_mac_set(priv, priv->ioaddr, false);
7217 netif_carrier_off(ndev);
7218 unregister_netdev(ndev);
7220 /* Serdes power down needs to happen after VLAN filter
7221 * is deleted that is triggered by unregister_netdev().
7223 if (priv->plat->serdes_powerdown)
7224 priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv);
7226 #ifdef CONFIG_DEBUG_FS
7227 stmmac_exit_fs(ndev);
7229 phylink_destroy(priv->phylink);
7230 if (priv->plat->stmmac_rst)
7231 reset_control_assert(priv->plat->stmmac_rst);
7232 reset_control_assert(priv->plat->stmmac_ahb_rst);
7233 pm_runtime_put(dev);
7234 pm_runtime_disable(dev);
7235 if (priv->hw->pcs != STMMAC_PCS_TBI &&
7236 priv->hw->pcs != STMMAC_PCS_RTBI)
7237 stmmac_mdio_unregister(ndev);
7238 destroy_workqueue(priv->wq);
7239 mutex_destroy(&priv->lock);
7240 bitmap_free(priv->af_xdp_zc_qps);
7244 EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
7247 * stmmac_suspend - suspend callback
7248 * @dev: device pointer
7249 * Description: this is the function to suspend the device and it is called
7250 * by the platform driver to stop the network queue, release the resources,
7251 * program the PMT register (for WoL), clean and release driver resources.
7253 int stmmac_suspend(struct device *dev)
7255 struct net_device *ndev = dev_get_drvdata(dev);
7256 struct stmmac_priv *priv = netdev_priv(ndev);
7259 if (!ndev || !netif_running(ndev))
7262 mutex_lock(&priv->lock);
7264 netif_device_detach(ndev);
7266 stmmac_disable_all_queues(priv);
7268 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
7269 hrtimer_cancel(&priv->tx_queue[chan].txtimer);
7271 if (priv->eee_enabled) {
7272 priv->tx_path_in_lpi_mode = false;
7273 del_timer_sync(&priv->eee_ctrl_timer);
7276 /* Stop TX/RX DMA */
7277 stmmac_stop_all_dma(priv);
7279 if (priv->plat->serdes_powerdown)
7280 priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv);
7282 /* Enable Power down mode by programming the PMT regs */
7283 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7284 stmmac_pmt(priv, priv->hw, priv->wolopts);
7287 stmmac_mac_set(priv, priv->ioaddr, false);
7288 pinctrl_pm_select_sleep_state(priv->device);
7291 mutex_unlock(&priv->lock);
7294 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7295 phylink_suspend(priv->phylink, true);
7297 if (device_may_wakeup(priv->device))
7298 phylink_speed_down(priv->phylink, false);
7299 phylink_suspend(priv->phylink, false);
7303 if (priv->dma_cap.fpesel) {
7305 stmmac_fpe_configure(priv, priv->ioaddr,
7306 priv->plat->tx_queues_to_use,
7307 priv->plat->rx_queues_to_use, false);
7309 stmmac_fpe_handshake(priv, false);
7310 stmmac_fpe_stop_wq(priv);
7313 priv->speed = SPEED_UNKNOWN;
7316 EXPORT_SYMBOL_GPL(stmmac_suspend);
7319 * stmmac_reset_queues_param - reset queue parameters
7320 * @priv: device pointer
7322 static void stmmac_reset_queues_param(struct stmmac_priv *priv)
7324 u32 rx_cnt = priv->plat->rx_queues_to_use;
7325 u32 tx_cnt = priv->plat->tx_queues_to_use;
7328 for (queue = 0; queue < rx_cnt; queue++) {
7329 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
7335 for (queue = 0; queue < tx_cnt; queue++) {
7336 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
7342 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
7347 * stmmac_resume - resume callback
7348 * @dev: device pointer
7349 * Description: when resume this function is invoked to setup the DMA and CORE
7350 * in a usable state.
7352 int stmmac_resume(struct device *dev)
7354 struct net_device *ndev = dev_get_drvdata(dev);
7355 struct stmmac_priv *priv = netdev_priv(ndev);
7358 if (!netif_running(ndev))
7361 /* Power Down bit, into the PM register, is cleared
7362 * automatically as soon as a magic packet or a Wake-up frame
7363 * is received. Anyway, it's better to manually clear
7364 * this bit because it can generate problems while resuming
7365 * from another devices (e.g. serial console).
7367 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7368 mutex_lock(&priv->lock);
7369 stmmac_pmt(priv, priv->hw, 0);
7370 mutex_unlock(&priv->lock);
7373 pinctrl_pm_select_default_state(priv->device);
7374 /* reset the phy so that it's ready */
7376 stmmac_mdio_reset(priv->mii);
7379 if (priv->plat->serdes_powerup) {
7380 ret = priv->plat->serdes_powerup(ndev,
7381 priv->plat->bsp_priv);
7388 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7389 phylink_resume(priv->phylink);
7391 phylink_resume(priv->phylink);
7392 if (device_may_wakeup(priv->device))
7393 phylink_speed_up(priv->phylink);
7398 mutex_lock(&priv->lock);
7400 stmmac_reset_queues_param(priv);
7402 stmmac_free_tx_skbufs(priv);
7403 stmmac_clear_descriptors(priv);
7405 stmmac_hw_setup(ndev, false);
7406 stmmac_init_coalesce(priv);
7407 stmmac_set_rx_mode(ndev);
7409 stmmac_restore_hw_vlan_rx_fltr(priv, ndev, priv->hw);
7411 stmmac_enable_all_queues(priv);
7413 mutex_unlock(&priv->lock);
7416 netif_device_attach(ndev);
7420 EXPORT_SYMBOL_GPL(stmmac_resume);
7423 static int __init stmmac_cmdline_opt(char *str)
7429 while ((opt = strsep(&str, ",")) != NULL) {
7430 if (!strncmp(opt, "debug:", 6)) {
7431 if (kstrtoint(opt + 6, 0, &debug))
7433 } else if (!strncmp(opt, "phyaddr:", 8)) {
7434 if (kstrtoint(opt + 8, 0, &phyaddr))
7436 } else if (!strncmp(opt, "buf_sz:", 7)) {
7437 if (kstrtoint(opt + 7, 0, &buf_sz))
7439 } else if (!strncmp(opt, "tc:", 3)) {
7440 if (kstrtoint(opt + 3, 0, &tc))
7442 } else if (!strncmp(opt, "watchdog:", 9)) {
7443 if (kstrtoint(opt + 9, 0, &watchdog))
7445 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
7446 if (kstrtoint(opt + 10, 0, &flow_ctrl))
7448 } else if (!strncmp(opt, "pause:", 6)) {
7449 if (kstrtoint(opt + 6, 0, &pause))
7451 } else if (!strncmp(opt, "eee_timer:", 10)) {
7452 if (kstrtoint(opt + 10, 0, &eee_timer))
7454 } else if (!strncmp(opt, "chain_mode:", 11)) {
7455 if (kstrtoint(opt + 11, 0, &chain_mode))
7462 pr_err("%s: ERROR broken module parameter conversion", __func__);
7466 __setup("stmmaceth=", stmmac_cmdline_opt);
7469 static int __init stmmac_init(void)
7471 #ifdef CONFIG_DEBUG_FS
7472 /* Create debugfs main directory if it doesn't exist yet */
7474 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
7475 register_netdevice_notifier(&stmmac_notifier);
7481 static void __exit stmmac_exit(void)
7483 #ifdef CONFIG_DEBUG_FS
7484 unregister_netdevice_notifier(&stmmac_notifier);
7485 debugfs_remove_recursive(stmmac_fs_dir);
7489 module_init(stmmac_init)
7490 module_exit(stmmac_exit)
7492 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
7493 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
7494 MODULE_LICENSE("GPL");