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_conf.dma_tx_size / 4)
78 #define STMMAC_RX_THRESH(x) ((x)->dma_conf.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_reset_rx_queue(struct stmmac_priv *priv, u32 queue);
134 static void stmmac_reset_tx_queue(struct stmmac_priv *priv, u32 queue);
135 static void stmmac_reset_queues_param(struct stmmac_priv *priv);
136 static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue);
137 static void stmmac_flush_tx_descriptors(struct stmmac_priv *priv, int queue);
138 static void stmmac_set_dma_operation_mode(struct stmmac_priv *priv, u32 txmode,
139 u32 rxmode, u32 chan);
141 #ifdef CONFIG_DEBUG_FS
142 static const struct net_device_ops stmmac_netdev_ops;
143 static void stmmac_init_fs(struct net_device *dev);
144 static void stmmac_exit_fs(struct net_device *dev);
147 #define STMMAC_COAL_TIMER(x) (ns_to_ktime((x) * NSEC_PER_USEC))
149 int stmmac_bus_clks_config(struct stmmac_priv *priv, bool enabled)
154 ret = clk_prepare_enable(priv->plat->stmmac_clk);
157 ret = clk_prepare_enable(priv->plat->pclk);
159 clk_disable_unprepare(priv->plat->stmmac_clk);
162 if (priv->plat->clks_config) {
163 ret = priv->plat->clks_config(priv->plat->bsp_priv, enabled);
165 clk_disable_unprepare(priv->plat->stmmac_clk);
166 clk_disable_unprepare(priv->plat->pclk);
171 clk_disable_unprepare(priv->plat->stmmac_clk);
172 clk_disable_unprepare(priv->plat->pclk);
173 if (priv->plat->clks_config)
174 priv->plat->clks_config(priv->plat->bsp_priv, enabled);
179 EXPORT_SYMBOL_GPL(stmmac_bus_clks_config);
182 * stmmac_verify_args - verify the driver parameters.
183 * Description: it checks the driver parameters and set a default in case of
186 static void stmmac_verify_args(void)
188 if (unlikely(watchdog < 0))
190 if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
191 buf_sz = DEFAULT_BUFSIZE;
192 if (unlikely(flow_ctrl > 1))
193 flow_ctrl = FLOW_AUTO;
194 else if (likely(flow_ctrl < 0))
195 flow_ctrl = FLOW_OFF;
196 if (unlikely((pause < 0) || (pause > 0xffff)))
199 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
202 static void __stmmac_disable_all_queues(struct stmmac_priv *priv)
204 u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
205 u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
206 u32 maxq = max(rx_queues_cnt, tx_queues_cnt);
209 for (queue = 0; queue < maxq; queue++) {
210 struct stmmac_channel *ch = &priv->channel[queue];
212 if (stmmac_xdp_is_enabled(priv) &&
213 test_bit(queue, priv->af_xdp_zc_qps)) {
214 napi_disable(&ch->rxtx_napi);
218 if (queue < rx_queues_cnt)
219 napi_disable(&ch->rx_napi);
220 if (queue < tx_queues_cnt)
221 napi_disable(&ch->tx_napi);
226 * stmmac_disable_all_queues - Disable all queues
227 * @priv: driver private structure
229 static void stmmac_disable_all_queues(struct stmmac_priv *priv)
231 u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
232 struct stmmac_rx_queue *rx_q;
235 /* synchronize_rcu() needed for pending XDP buffers to drain */
236 for (queue = 0; queue < rx_queues_cnt; queue++) {
237 rx_q = &priv->dma_conf.rx_queue[queue];
238 if (rx_q->xsk_pool) {
244 __stmmac_disable_all_queues(priv);
248 * stmmac_enable_all_queues - Enable all queues
249 * @priv: driver private structure
251 static void stmmac_enable_all_queues(struct stmmac_priv *priv)
253 u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
254 u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
255 u32 maxq = max(rx_queues_cnt, tx_queues_cnt);
258 for (queue = 0; queue < maxq; queue++) {
259 struct stmmac_channel *ch = &priv->channel[queue];
261 if (stmmac_xdp_is_enabled(priv) &&
262 test_bit(queue, priv->af_xdp_zc_qps)) {
263 napi_enable(&ch->rxtx_napi);
267 if (queue < rx_queues_cnt)
268 napi_enable(&ch->rx_napi);
269 if (queue < tx_queues_cnt)
270 napi_enable(&ch->tx_napi);
274 static void stmmac_service_event_schedule(struct stmmac_priv *priv)
276 if (!test_bit(STMMAC_DOWN, &priv->state) &&
277 !test_and_set_bit(STMMAC_SERVICE_SCHED, &priv->state))
278 queue_work(priv->wq, &priv->service_task);
281 static void stmmac_global_err(struct stmmac_priv *priv)
283 netif_carrier_off(priv->dev);
284 set_bit(STMMAC_RESET_REQUESTED, &priv->state);
285 stmmac_service_event_schedule(priv);
289 * stmmac_clk_csr_set - dynamically set the MDC clock
290 * @priv: driver private structure
291 * Description: this is to dynamically set the MDC clock according to the csr
294 * If a specific clk_csr value is passed from the platform
295 * this means that the CSR Clock Range selection cannot be
296 * changed at run-time and it is fixed (as reported in the driver
297 * documentation). Viceversa the driver will try to set the MDC
298 * clock dynamically according to the actual clock input.
300 static void stmmac_clk_csr_set(struct stmmac_priv *priv)
304 clk_rate = clk_get_rate(priv->plat->stmmac_clk);
306 /* Platform provided default clk_csr would be assumed valid
307 * for all other cases except for the below mentioned ones.
308 * For values higher than the IEEE 802.3 specified frequency
309 * we can not estimate the proper divider as it is not known
310 * the frequency of clk_csr_i. So we do not change the default
313 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
314 if (clk_rate < CSR_F_35M)
315 priv->clk_csr = STMMAC_CSR_20_35M;
316 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
317 priv->clk_csr = STMMAC_CSR_35_60M;
318 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
319 priv->clk_csr = STMMAC_CSR_60_100M;
320 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
321 priv->clk_csr = STMMAC_CSR_100_150M;
322 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
323 priv->clk_csr = STMMAC_CSR_150_250M;
324 else if ((clk_rate >= CSR_F_250M) && (clk_rate <= CSR_F_300M))
325 priv->clk_csr = STMMAC_CSR_250_300M;
328 if (priv->plat->has_sun8i) {
329 if (clk_rate > 160000000)
330 priv->clk_csr = 0x03;
331 else if (clk_rate > 80000000)
332 priv->clk_csr = 0x02;
333 else if (clk_rate > 40000000)
334 priv->clk_csr = 0x01;
339 if (priv->plat->has_xgmac) {
340 if (clk_rate > 400000000)
342 else if (clk_rate > 350000000)
344 else if (clk_rate > 300000000)
346 else if (clk_rate > 250000000)
348 else if (clk_rate > 150000000)
355 static void print_pkt(unsigned char *buf, int len)
357 pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
358 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
361 static inline u32 stmmac_tx_avail(struct stmmac_priv *priv, u32 queue)
363 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[queue];
366 if (tx_q->dirty_tx > tx_q->cur_tx)
367 avail = tx_q->dirty_tx - tx_q->cur_tx - 1;
369 avail = priv->dma_conf.dma_tx_size - tx_q->cur_tx + tx_q->dirty_tx - 1;
375 * stmmac_rx_dirty - Get RX queue dirty
376 * @priv: driver private structure
377 * @queue: RX queue index
379 static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv, u32 queue)
381 struct stmmac_rx_queue *rx_q = &priv->dma_conf.rx_queue[queue];
384 if (rx_q->dirty_rx <= rx_q->cur_rx)
385 dirty = rx_q->cur_rx - rx_q->dirty_rx;
387 dirty = priv->dma_conf.dma_rx_size - rx_q->dirty_rx + rx_q->cur_rx;
392 static void stmmac_lpi_entry_timer_config(struct stmmac_priv *priv, bool en)
396 /* Clear/set the SW EEE timer flag based on LPI ET enablement */
397 priv->eee_sw_timer_en = en ? 0 : 1;
398 tx_lpi_timer = en ? priv->tx_lpi_timer : 0;
399 stmmac_set_eee_lpi_timer(priv, priv->hw, tx_lpi_timer);
403 * stmmac_enable_eee_mode - check and enter in LPI mode
404 * @priv: driver private structure
405 * Description: this function is to verify and enter in LPI mode in case of
408 static int stmmac_enable_eee_mode(struct stmmac_priv *priv)
410 u32 tx_cnt = priv->plat->tx_queues_to_use;
413 /* check if all TX queues have the work finished */
414 for (queue = 0; queue < tx_cnt; queue++) {
415 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[queue];
417 if (tx_q->dirty_tx != tx_q->cur_tx)
418 return -EBUSY; /* still unfinished work */
421 /* Check and enter in LPI mode */
422 if (!priv->tx_path_in_lpi_mode)
423 stmmac_set_eee_mode(priv, priv->hw,
424 priv->plat->en_tx_lpi_clockgating);
429 * stmmac_disable_eee_mode - disable and exit from LPI mode
430 * @priv: driver private structure
431 * Description: this function is to exit and disable EEE in case of
432 * LPI state is true. This is called by the xmit.
434 void stmmac_disable_eee_mode(struct stmmac_priv *priv)
436 if (!priv->eee_sw_timer_en) {
437 stmmac_lpi_entry_timer_config(priv, 0);
441 stmmac_reset_eee_mode(priv, priv->hw);
442 del_timer_sync(&priv->eee_ctrl_timer);
443 priv->tx_path_in_lpi_mode = false;
447 * stmmac_eee_ctrl_timer - EEE TX SW timer.
448 * @t: timer_list struct containing private info
450 * if there is no data transfer and if we are not in LPI state,
451 * then MAC Transmitter can be moved to LPI state.
453 static void stmmac_eee_ctrl_timer(struct timer_list *t)
455 struct stmmac_priv *priv = from_timer(priv, t, eee_ctrl_timer);
457 if (stmmac_enable_eee_mode(priv))
458 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer));
462 * stmmac_eee_init - init EEE
463 * @priv: driver private structure
465 * if the GMAC supports the EEE (from the HW cap reg) and the phy device
466 * can also manage EEE, this function enable the LPI state and start related
469 bool stmmac_eee_init(struct stmmac_priv *priv)
471 int eee_tw_timer = priv->eee_tw_timer;
473 /* Using PCS we cannot dial with the phy registers at this stage
474 * so we do not support extra feature like EEE.
476 if (priv->hw->pcs == STMMAC_PCS_TBI ||
477 priv->hw->pcs == STMMAC_PCS_RTBI)
480 /* Check if MAC core supports the EEE feature. */
481 if (!priv->dma_cap.eee)
484 mutex_lock(&priv->lock);
486 /* Check if it needs to be deactivated */
487 if (!priv->eee_active) {
488 if (priv->eee_enabled) {
489 netdev_dbg(priv->dev, "disable EEE\n");
490 stmmac_lpi_entry_timer_config(priv, 0);
491 del_timer_sync(&priv->eee_ctrl_timer);
492 stmmac_set_eee_timer(priv, priv->hw, 0, eee_tw_timer);
494 xpcs_config_eee(priv->hw->xpcs,
495 priv->plat->mult_fact_100ns,
498 mutex_unlock(&priv->lock);
502 if (priv->eee_active && !priv->eee_enabled) {
503 timer_setup(&priv->eee_ctrl_timer, stmmac_eee_ctrl_timer, 0);
504 stmmac_set_eee_timer(priv, priv->hw, STMMAC_DEFAULT_LIT_LS,
507 xpcs_config_eee(priv->hw->xpcs,
508 priv->plat->mult_fact_100ns,
512 if (priv->plat->has_gmac4 && priv->tx_lpi_timer <= STMMAC_ET_MAX) {
513 del_timer_sync(&priv->eee_ctrl_timer);
514 priv->tx_path_in_lpi_mode = false;
515 stmmac_lpi_entry_timer_config(priv, 1);
517 stmmac_lpi_entry_timer_config(priv, 0);
518 mod_timer(&priv->eee_ctrl_timer,
519 STMMAC_LPI_T(priv->tx_lpi_timer));
522 mutex_unlock(&priv->lock);
523 netdev_dbg(priv->dev, "Energy-Efficient Ethernet initialized\n");
527 /* stmmac_get_tx_hwtstamp - get HW TX timestamps
528 * @priv: driver private structure
529 * @p : descriptor pointer
530 * @skb : the socket buffer
532 * This function will read timestamp from the descriptor & pass it to stack.
533 * and also perform some sanity checks.
535 static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
536 struct dma_desc *p, struct sk_buff *skb)
538 struct skb_shared_hwtstamps shhwtstamp;
542 if (!priv->hwts_tx_en)
545 /* exit if skb doesn't support hw tstamp */
546 if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
549 /* check tx tstamp status */
550 if (stmmac_get_tx_timestamp_status(priv, p)) {
551 stmmac_get_timestamp(priv, p, priv->adv_ts, &ns);
553 } else if (!stmmac_get_mac_tx_timestamp(priv, priv->hw, &ns)) {
558 ns -= priv->plat->cdc_error_adj;
560 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
561 shhwtstamp.hwtstamp = ns_to_ktime(ns);
563 netdev_dbg(priv->dev, "get valid TX hw timestamp %llu\n", ns);
564 /* pass tstamp to stack */
565 skb_tstamp_tx(skb, &shhwtstamp);
569 /* stmmac_get_rx_hwtstamp - get HW RX timestamps
570 * @priv: driver private structure
571 * @p : descriptor pointer
572 * @np : next descriptor pointer
573 * @skb : the socket buffer
575 * This function will read received packet's timestamp from the descriptor
576 * and pass it to stack. It also perform some sanity checks.
578 static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,
579 struct dma_desc *np, struct sk_buff *skb)
581 struct skb_shared_hwtstamps *shhwtstamp = NULL;
582 struct dma_desc *desc = p;
585 if (!priv->hwts_rx_en)
587 /* For GMAC4, the valid timestamp is from CTX next desc. */
588 if (priv->plat->has_gmac4 || priv->plat->has_xgmac)
591 /* Check if timestamp is available */
592 if (stmmac_get_rx_timestamp_status(priv, p, np, priv->adv_ts)) {
593 stmmac_get_timestamp(priv, desc, priv->adv_ts, &ns);
595 ns -= priv->plat->cdc_error_adj;
597 netdev_dbg(priv->dev, "get valid RX hw timestamp %llu\n", ns);
598 shhwtstamp = skb_hwtstamps(skb);
599 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
600 shhwtstamp->hwtstamp = ns_to_ktime(ns);
602 netdev_dbg(priv->dev, "cannot get RX hw timestamp\n");
607 * stmmac_hwtstamp_set - control hardware timestamping.
608 * @dev: device pointer.
609 * @ifr: An IOCTL specific structure, that can contain a pointer to
610 * a proprietary structure used to pass information to the driver.
612 * This function configures the MAC to enable/disable both outgoing(TX)
613 * and incoming(RX) packets time stamping based on user input.
615 * 0 on success and an appropriate -ve integer on failure.
617 static int stmmac_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
619 struct stmmac_priv *priv = netdev_priv(dev);
620 struct hwtstamp_config config;
623 u32 ptp_over_ipv4_udp = 0;
624 u32 ptp_over_ipv6_udp = 0;
625 u32 ptp_over_ethernet = 0;
626 u32 snap_type_sel = 0;
627 u32 ts_master_en = 0;
630 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
631 netdev_alert(priv->dev, "No support for HW time stamping\n");
632 priv->hwts_tx_en = 0;
633 priv->hwts_rx_en = 0;
638 if (copy_from_user(&config, ifr->ifr_data,
642 netdev_dbg(priv->dev, "%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
643 __func__, config.flags, config.tx_type, config.rx_filter);
645 if (config.tx_type != HWTSTAMP_TX_OFF &&
646 config.tx_type != HWTSTAMP_TX_ON)
650 switch (config.rx_filter) {
651 case HWTSTAMP_FILTER_NONE:
652 /* time stamp no incoming packet at all */
653 config.rx_filter = HWTSTAMP_FILTER_NONE;
656 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
657 /* PTP v1, UDP, any kind of event packet */
658 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
659 /* 'xmac' hardware can support Sync, Pdelay_Req and
660 * Pdelay_resp by setting bit14 and bits17/16 to 01
661 * This leaves Delay_Req timestamps out.
662 * Enable all events *and* general purpose message
665 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
666 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
667 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
670 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
671 /* PTP v1, UDP, Sync packet */
672 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
673 /* take time stamp for SYNC messages only */
674 ts_event_en = PTP_TCR_TSEVNTENA;
676 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
677 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
680 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
681 /* PTP v1, UDP, Delay_req packet */
682 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
683 /* take time stamp for Delay_Req messages only */
684 ts_master_en = PTP_TCR_TSMSTRENA;
685 ts_event_en = PTP_TCR_TSEVNTENA;
687 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
688 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
691 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
692 /* PTP v2, UDP, any kind of event packet */
693 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
694 ptp_v2 = PTP_TCR_TSVER2ENA;
695 /* take time stamp for all event messages */
696 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
698 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
699 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
702 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
703 /* PTP v2, UDP, Sync packet */
704 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
705 ptp_v2 = PTP_TCR_TSVER2ENA;
706 /* take time stamp for SYNC messages only */
707 ts_event_en = PTP_TCR_TSEVNTENA;
709 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
710 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
713 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
714 /* PTP v2, UDP, Delay_req packet */
715 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
716 ptp_v2 = PTP_TCR_TSVER2ENA;
717 /* take time stamp for Delay_Req messages only */
718 ts_master_en = PTP_TCR_TSMSTRENA;
719 ts_event_en = PTP_TCR_TSEVNTENA;
721 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
722 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
725 case HWTSTAMP_FILTER_PTP_V2_EVENT:
726 /* PTP v2/802.AS1 any layer, any kind of event packet */
727 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
728 ptp_v2 = PTP_TCR_TSVER2ENA;
729 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
730 if (priv->synopsys_id < DWMAC_CORE_4_10)
731 ts_event_en = PTP_TCR_TSEVNTENA;
732 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
733 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
734 ptp_over_ethernet = PTP_TCR_TSIPENA;
737 case HWTSTAMP_FILTER_PTP_V2_SYNC:
738 /* PTP v2/802.AS1, any layer, Sync packet */
739 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
740 ptp_v2 = PTP_TCR_TSVER2ENA;
741 /* take time stamp for SYNC messages only */
742 ts_event_en = PTP_TCR_TSEVNTENA;
744 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
745 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
746 ptp_over_ethernet = PTP_TCR_TSIPENA;
749 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
750 /* PTP v2/802.AS1, any layer, Delay_req packet */
751 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
752 ptp_v2 = PTP_TCR_TSVER2ENA;
753 /* take time stamp for Delay_Req messages only */
754 ts_master_en = PTP_TCR_TSMSTRENA;
755 ts_event_en = PTP_TCR_TSEVNTENA;
757 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
758 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
759 ptp_over_ethernet = PTP_TCR_TSIPENA;
762 case HWTSTAMP_FILTER_NTP_ALL:
763 case HWTSTAMP_FILTER_ALL:
764 /* time stamp any incoming packet */
765 config.rx_filter = HWTSTAMP_FILTER_ALL;
766 tstamp_all = PTP_TCR_TSENALL;
773 switch (config.rx_filter) {
774 case HWTSTAMP_FILTER_NONE:
775 config.rx_filter = HWTSTAMP_FILTER_NONE;
778 /* PTP v1, UDP, any kind of event packet */
779 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
783 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
784 priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
786 priv->systime_flags = STMMAC_HWTS_ACTIVE;
788 if (priv->hwts_tx_en || priv->hwts_rx_en) {
789 priv->systime_flags |= tstamp_all | ptp_v2 |
790 ptp_over_ethernet | ptp_over_ipv6_udp |
791 ptp_over_ipv4_udp | ts_event_en |
792 ts_master_en | snap_type_sel;
795 stmmac_config_hw_tstamping(priv, priv->ptpaddr, priv->systime_flags);
797 memcpy(&priv->tstamp_config, &config, sizeof(config));
799 return copy_to_user(ifr->ifr_data, &config,
800 sizeof(config)) ? -EFAULT : 0;
804 * stmmac_hwtstamp_get - read hardware timestamping.
805 * @dev: device pointer.
806 * @ifr: An IOCTL specific structure, that can contain a pointer to
807 * a proprietary structure used to pass information to the driver.
809 * This function obtain the current hardware timestamping settings
812 static int stmmac_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
814 struct stmmac_priv *priv = netdev_priv(dev);
815 struct hwtstamp_config *config = &priv->tstamp_config;
817 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
820 return copy_to_user(ifr->ifr_data, config,
821 sizeof(*config)) ? -EFAULT : 0;
825 * stmmac_init_tstamp_counter - init hardware timestamping counter
826 * @priv: driver private structure
827 * @systime_flags: timestamping flags
829 * Initialize hardware counter for packet timestamping.
830 * This is valid as long as the interface is open and not suspended.
831 * Will be rerun after resuming from suspend, case in which the timestamping
832 * flags updated by stmmac_hwtstamp_set() also need to be restored.
834 int stmmac_init_tstamp_counter(struct stmmac_priv *priv, u32 systime_flags)
836 bool xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
837 struct timespec64 now;
841 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
844 stmmac_config_hw_tstamping(priv, priv->ptpaddr, systime_flags);
845 priv->systime_flags = systime_flags;
847 /* program Sub Second Increment reg */
848 stmmac_config_sub_second_increment(priv, priv->ptpaddr,
849 priv->plat->clk_ptp_rate,
851 temp = div_u64(1000000000ULL, sec_inc);
853 /* Store sub second increment for later use */
854 priv->sub_second_inc = sec_inc;
856 /* calculate default added value:
858 * addend = (2^32)/freq_div_ratio;
859 * where, freq_div_ratio = 1e9ns/sec_inc
861 temp = (u64)(temp << 32);
862 priv->default_addend = div_u64(temp, priv->plat->clk_ptp_rate);
863 stmmac_config_addend(priv, priv->ptpaddr, priv->default_addend);
865 /* initialize system time */
866 ktime_get_real_ts64(&now);
868 /* lower 32 bits of tv_sec are safe until y2106 */
869 stmmac_init_systime(priv, priv->ptpaddr, (u32)now.tv_sec, now.tv_nsec);
873 EXPORT_SYMBOL_GPL(stmmac_init_tstamp_counter);
876 * stmmac_init_ptp - init PTP
877 * @priv: driver private structure
878 * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
879 * This is done by looking at the HW cap. register.
880 * This function also registers the ptp driver.
882 static int stmmac_init_ptp(struct stmmac_priv *priv)
884 bool xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
887 if (priv->plat->ptp_clk_freq_config)
888 priv->plat->ptp_clk_freq_config(priv);
890 ret = stmmac_init_tstamp_counter(priv, STMMAC_HWTS_ACTIVE);
895 /* Check if adv_ts can be enabled for dwmac 4.x / xgmac core */
896 if (xmac && priv->dma_cap.atime_stamp)
898 /* Dwmac 3.x core with extend_desc can support adv_ts */
899 else if (priv->extend_desc && priv->dma_cap.atime_stamp)
902 if (priv->dma_cap.time_stamp)
903 netdev_info(priv->dev, "IEEE 1588-2002 Timestamp supported\n");
906 netdev_info(priv->dev,
907 "IEEE 1588-2008 Advanced Timestamp supported\n");
909 priv->hwts_tx_en = 0;
910 priv->hwts_rx_en = 0;
915 static void stmmac_release_ptp(struct stmmac_priv *priv)
917 clk_disable_unprepare(priv->plat->clk_ptp_ref);
918 stmmac_ptp_unregister(priv);
922 * stmmac_mac_flow_ctrl - Configure flow control in all queues
923 * @priv: driver private structure
924 * @duplex: duplex passed to the next function
925 * Description: It is used for configuring the flow control in all queues
927 static void stmmac_mac_flow_ctrl(struct stmmac_priv *priv, u32 duplex)
929 u32 tx_cnt = priv->plat->tx_queues_to_use;
931 stmmac_flow_ctrl(priv, priv->hw, duplex, priv->flow_ctrl,
932 priv->pause, tx_cnt);
935 static struct phylink_pcs *stmmac_mac_select_pcs(struct phylink_config *config,
936 phy_interface_t interface)
938 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
943 return &priv->hw->xpcs->pcs;
946 static void stmmac_mac_config(struct phylink_config *config, unsigned int mode,
947 const struct phylink_link_state *state)
949 /* Nothing to do, xpcs_config() handles everything */
952 static void stmmac_fpe_link_state_handle(struct stmmac_priv *priv, bool is_up)
954 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
955 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
956 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
957 bool *hs_enable = &fpe_cfg->hs_enable;
959 if (is_up && *hs_enable) {
960 stmmac_fpe_send_mpacket(priv, priv->ioaddr, MPACKET_VERIFY);
962 *lo_state = FPE_STATE_OFF;
963 *lp_state = FPE_STATE_OFF;
967 static void stmmac_mac_link_down(struct phylink_config *config,
968 unsigned int mode, phy_interface_t interface)
970 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
972 stmmac_mac_set(priv, priv->ioaddr, false);
973 priv->eee_active = false;
974 priv->tx_lpi_enabled = false;
975 priv->eee_enabled = stmmac_eee_init(priv);
976 stmmac_set_eee_pls(priv, priv->hw, false);
978 if (priv->dma_cap.fpesel)
979 stmmac_fpe_link_state_handle(priv, false);
982 static void stmmac_mac_link_up(struct phylink_config *config,
983 struct phy_device *phy,
984 unsigned int mode, phy_interface_t interface,
985 int speed, int duplex,
986 bool tx_pause, bool rx_pause)
988 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
991 if (priv->plat->serdes_up_after_phy_linkup && priv->plat->serdes_powerup)
992 priv->plat->serdes_powerup(priv->dev, priv->plat->bsp_priv);
994 old_ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
995 ctrl = old_ctrl & ~priv->hw->link.speed_mask;
997 if (interface == PHY_INTERFACE_MODE_USXGMII) {
1000 ctrl |= priv->hw->link.xgmii.speed10000;
1003 ctrl |= priv->hw->link.xgmii.speed5000;
1006 ctrl |= priv->hw->link.xgmii.speed2500;
1011 } else if (interface == PHY_INTERFACE_MODE_XLGMII) {
1014 ctrl |= priv->hw->link.xlgmii.speed100000;
1017 ctrl |= priv->hw->link.xlgmii.speed50000;
1020 ctrl |= priv->hw->link.xlgmii.speed40000;
1023 ctrl |= priv->hw->link.xlgmii.speed25000;
1026 ctrl |= priv->hw->link.xgmii.speed10000;
1029 ctrl |= priv->hw->link.speed2500;
1032 ctrl |= priv->hw->link.speed1000;
1040 ctrl |= priv->hw->link.speed2500;
1043 ctrl |= priv->hw->link.speed1000;
1046 ctrl |= priv->hw->link.speed100;
1049 ctrl |= priv->hw->link.speed10;
1056 priv->speed = speed;
1058 if (priv->plat->fix_mac_speed)
1059 priv->plat->fix_mac_speed(priv->plat->bsp_priv, speed);
1062 ctrl &= ~priv->hw->link.duplex;
1064 ctrl |= priv->hw->link.duplex;
1066 /* Flow Control operation */
1067 if (rx_pause && tx_pause)
1068 priv->flow_ctrl = FLOW_AUTO;
1069 else if (rx_pause && !tx_pause)
1070 priv->flow_ctrl = FLOW_RX;
1071 else if (!rx_pause && tx_pause)
1072 priv->flow_ctrl = FLOW_TX;
1074 priv->flow_ctrl = FLOW_OFF;
1076 stmmac_mac_flow_ctrl(priv, duplex);
1078 if (ctrl != old_ctrl)
1079 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
1081 stmmac_mac_set(priv, priv->ioaddr, true);
1082 if (phy && priv->dma_cap.eee) {
1084 phy_init_eee(phy, !priv->plat->rx_clk_runs_in_lpi) >= 0;
1085 priv->eee_enabled = stmmac_eee_init(priv);
1086 priv->tx_lpi_enabled = priv->eee_enabled;
1087 stmmac_set_eee_pls(priv, priv->hw, true);
1090 if (priv->dma_cap.fpesel)
1091 stmmac_fpe_link_state_handle(priv, true);
1094 static const struct phylink_mac_ops stmmac_phylink_mac_ops = {
1095 .mac_select_pcs = stmmac_mac_select_pcs,
1096 .mac_config = stmmac_mac_config,
1097 .mac_link_down = stmmac_mac_link_down,
1098 .mac_link_up = stmmac_mac_link_up,
1102 * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
1103 * @priv: driver private structure
1104 * Description: this is to verify if the HW supports the PCS.
1105 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
1106 * configured for the TBI, RTBI, or SGMII PHY interface.
1108 static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
1110 int interface = priv->plat->interface;
1112 if (priv->dma_cap.pcs) {
1113 if ((interface == PHY_INTERFACE_MODE_RGMII) ||
1114 (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
1115 (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
1116 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
1117 netdev_dbg(priv->dev, "PCS RGMII support enabled\n");
1118 priv->hw->pcs = STMMAC_PCS_RGMII;
1119 } else if (interface == PHY_INTERFACE_MODE_SGMII) {
1120 netdev_dbg(priv->dev, "PCS SGMII support enabled\n");
1121 priv->hw->pcs = STMMAC_PCS_SGMII;
1127 * stmmac_init_phy - PHY initialization
1128 * @dev: net device structure
1129 * Description: it initializes the driver's PHY state, and attaches the PHY
1130 * to the mac driver.
1134 static int stmmac_init_phy(struct net_device *dev)
1136 struct stmmac_priv *priv = netdev_priv(dev);
1137 struct fwnode_handle *fwnode;
1140 fwnode = of_fwnode_handle(priv->plat->phylink_node);
1142 fwnode = dev_fwnode(priv->device);
1145 ret = phylink_fwnode_phy_connect(priv->phylink, fwnode, 0);
1147 /* Some DT bindings do not set-up the PHY handle. Let's try to
1150 if (!fwnode || ret) {
1151 int addr = priv->plat->phy_addr;
1152 struct phy_device *phydev;
1155 netdev_err(priv->dev, "no phy found\n");
1159 phydev = mdiobus_get_phy(priv->mii, addr);
1161 netdev_err(priv->dev, "no phy at addr %d\n", addr);
1165 ret = phylink_connect_phy(priv->phylink, phydev);
1168 if (!priv->plat->pmt) {
1169 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1171 phylink_ethtool_get_wol(priv->phylink, &wol);
1172 device_set_wakeup_capable(priv->device, !!wol.supported);
1178 static int stmmac_phy_setup(struct stmmac_priv *priv)
1180 struct stmmac_mdio_bus_data *mdio_bus_data = priv->plat->mdio_bus_data;
1181 struct fwnode_handle *fwnode = of_fwnode_handle(priv->plat->phylink_node);
1182 int max_speed = priv->plat->max_speed;
1183 int mode = priv->plat->phy_interface;
1184 struct phylink *phylink;
1186 priv->phylink_config.dev = &priv->dev->dev;
1187 priv->phylink_config.type = PHYLINK_NETDEV;
1188 if (priv->plat->mdio_bus_data)
1189 priv->phylink_config.ovr_an_inband =
1190 mdio_bus_data->xpcs_an_inband;
1193 fwnode = dev_fwnode(priv->device);
1195 /* Set the platform/firmware specified interface mode */
1196 __set_bit(mode, priv->phylink_config.supported_interfaces);
1198 /* If we have an xpcs, it defines which PHY interfaces are supported. */
1200 xpcs_get_interfaces(priv->hw->xpcs,
1201 priv->phylink_config.supported_interfaces);
1203 priv->phylink_config.mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
1206 if (!max_speed || max_speed >= 1000)
1207 priv->phylink_config.mac_capabilities |= MAC_1000;
1209 if (priv->plat->has_gmac4) {
1210 if (!max_speed || max_speed >= 2500)
1211 priv->phylink_config.mac_capabilities |= MAC_2500FD;
1212 } else if (priv->plat->has_xgmac) {
1213 if (!max_speed || max_speed >= 2500)
1214 priv->phylink_config.mac_capabilities |= MAC_2500FD;
1215 if (!max_speed || max_speed >= 5000)
1216 priv->phylink_config.mac_capabilities |= MAC_5000FD;
1217 if (!max_speed || max_speed >= 10000)
1218 priv->phylink_config.mac_capabilities |= MAC_10000FD;
1219 if (!max_speed || max_speed >= 25000)
1220 priv->phylink_config.mac_capabilities |= MAC_25000FD;
1221 if (!max_speed || max_speed >= 40000)
1222 priv->phylink_config.mac_capabilities |= MAC_40000FD;
1223 if (!max_speed || max_speed >= 50000)
1224 priv->phylink_config.mac_capabilities |= MAC_50000FD;
1225 if (!max_speed || max_speed >= 100000)
1226 priv->phylink_config.mac_capabilities |= MAC_100000FD;
1229 /* Half-Duplex can only work with single queue */
1230 if (priv->plat->tx_queues_to_use > 1)
1231 priv->phylink_config.mac_capabilities &=
1232 ~(MAC_10HD | MAC_100HD | MAC_1000HD);
1233 priv->phylink_config.mac_managed_pm = true;
1235 phylink = phylink_create(&priv->phylink_config, fwnode,
1236 mode, &stmmac_phylink_mac_ops);
1237 if (IS_ERR(phylink))
1238 return PTR_ERR(phylink);
1240 priv->phylink = phylink;
1244 static void stmmac_display_rx_rings(struct stmmac_priv *priv,
1245 struct stmmac_dma_conf *dma_conf)
1247 u32 rx_cnt = priv->plat->rx_queues_to_use;
1248 unsigned int desc_size;
1252 /* Display RX rings */
1253 for (queue = 0; queue < rx_cnt; queue++) {
1254 struct stmmac_rx_queue *rx_q = &dma_conf->rx_queue[queue];
1256 pr_info("\tRX Queue %u rings\n", queue);
1258 if (priv->extend_desc) {
1259 head_rx = (void *)rx_q->dma_erx;
1260 desc_size = sizeof(struct dma_extended_desc);
1262 head_rx = (void *)rx_q->dma_rx;
1263 desc_size = sizeof(struct dma_desc);
1266 /* Display RX ring */
1267 stmmac_display_ring(priv, head_rx, dma_conf->dma_rx_size, true,
1268 rx_q->dma_rx_phy, desc_size);
1272 static void stmmac_display_tx_rings(struct stmmac_priv *priv,
1273 struct stmmac_dma_conf *dma_conf)
1275 u32 tx_cnt = priv->plat->tx_queues_to_use;
1276 unsigned int desc_size;
1280 /* Display TX rings */
1281 for (queue = 0; queue < tx_cnt; queue++) {
1282 struct stmmac_tx_queue *tx_q = &dma_conf->tx_queue[queue];
1284 pr_info("\tTX Queue %d rings\n", queue);
1286 if (priv->extend_desc) {
1287 head_tx = (void *)tx_q->dma_etx;
1288 desc_size = sizeof(struct dma_extended_desc);
1289 } else if (tx_q->tbs & STMMAC_TBS_AVAIL) {
1290 head_tx = (void *)tx_q->dma_entx;
1291 desc_size = sizeof(struct dma_edesc);
1293 head_tx = (void *)tx_q->dma_tx;
1294 desc_size = sizeof(struct dma_desc);
1297 stmmac_display_ring(priv, head_tx, dma_conf->dma_tx_size, false,
1298 tx_q->dma_tx_phy, desc_size);
1302 static void stmmac_display_rings(struct stmmac_priv *priv,
1303 struct stmmac_dma_conf *dma_conf)
1305 /* Display RX ring */
1306 stmmac_display_rx_rings(priv, dma_conf);
1308 /* Display TX ring */
1309 stmmac_display_tx_rings(priv, dma_conf);
1312 static int stmmac_set_bfsize(int mtu, int bufsize)
1316 if (mtu >= BUF_SIZE_8KiB)
1317 ret = BUF_SIZE_16KiB;
1318 else if (mtu >= BUF_SIZE_4KiB)
1319 ret = BUF_SIZE_8KiB;
1320 else if (mtu >= BUF_SIZE_2KiB)
1321 ret = BUF_SIZE_4KiB;
1322 else if (mtu > DEFAULT_BUFSIZE)
1323 ret = BUF_SIZE_2KiB;
1325 ret = DEFAULT_BUFSIZE;
1331 * stmmac_clear_rx_descriptors - clear RX descriptors
1332 * @priv: driver private structure
1333 * @dma_conf: structure to take the dma data
1334 * @queue: RX queue index
1335 * Description: this function is called to clear the RX descriptors
1336 * in case of both basic and extended descriptors are used.
1338 static void stmmac_clear_rx_descriptors(struct stmmac_priv *priv,
1339 struct stmmac_dma_conf *dma_conf,
1342 struct stmmac_rx_queue *rx_q = &dma_conf->rx_queue[queue];
1345 /* Clear the RX descriptors */
1346 for (i = 0; i < dma_conf->dma_rx_size; i++)
1347 if (priv->extend_desc)
1348 stmmac_init_rx_desc(priv, &rx_q->dma_erx[i].basic,
1349 priv->use_riwt, priv->mode,
1350 (i == dma_conf->dma_rx_size - 1),
1351 dma_conf->dma_buf_sz);
1353 stmmac_init_rx_desc(priv, &rx_q->dma_rx[i],
1354 priv->use_riwt, priv->mode,
1355 (i == dma_conf->dma_rx_size - 1),
1356 dma_conf->dma_buf_sz);
1360 * stmmac_clear_tx_descriptors - clear tx descriptors
1361 * @priv: driver private structure
1362 * @dma_conf: structure to take the dma data
1363 * @queue: TX queue index.
1364 * Description: this function is called to clear the TX descriptors
1365 * in case of both basic and extended descriptors are used.
1367 static void stmmac_clear_tx_descriptors(struct stmmac_priv *priv,
1368 struct stmmac_dma_conf *dma_conf,
1371 struct stmmac_tx_queue *tx_q = &dma_conf->tx_queue[queue];
1374 /* Clear the TX descriptors */
1375 for (i = 0; i < dma_conf->dma_tx_size; i++) {
1376 int last = (i == (dma_conf->dma_tx_size - 1));
1379 if (priv->extend_desc)
1380 p = &tx_q->dma_etx[i].basic;
1381 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1382 p = &tx_q->dma_entx[i].basic;
1384 p = &tx_q->dma_tx[i];
1386 stmmac_init_tx_desc(priv, p, priv->mode, last);
1391 * stmmac_clear_descriptors - clear descriptors
1392 * @priv: driver private structure
1393 * @dma_conf: structure to take the dma data
1394 * Description: this function is called to clear the TX and RX descriptors
1395 * in case of both basic and extended descriptors are used.
1397 static void stmmac_clear_descriptors(struct stmmac_priv *priv,
1398 struct stmmac_dma_conf *dma_conf)
1400 u32 rx_queue_cnt = priv->plat->rx_queues_to_use;
1401 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1404 /* Clear the RX descriptors */
1405 for (queue = 0; queue < rx_queue_cnt; queue++)
1406 stmmac_clear_rx_descriptors(priv, dma_conf, queue);
1408 /* Clear the TX descriptors */
1409 for (queue = 0; queue < tx_queue_cnt; queue++)
1410 stmmac_clear_tx_descriptors(priv, dma_conf, queue);
1414 * stmmac_init_rx_buffers - init the RX descriptor buffer.
1415 * @priv: driver private structure
1416 * @dma_conf: structure to take the dma data
1417 * @p: descriptor pointer
1418 * @i: descriptor index
1420 * @queue: RX queue index
1421 * Description: this function is called to allocate a receive buffer, perform
1422 * the DMA mapping and init the descriptor.
1424 static int stmmac_init_rx_buffers(struct stmmac_priv *priv,
1425 struct stmmac_dma_conf *dma_conf,
1427 int i, gfp_t flags, u32 queue)
1429 struct stmmac_rx_queue *rx_q = &dma_conf->rx_queue[queue];
1430 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1431 gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
1433 if (priv->dma_cap.addr64 <= 32)
1437 buf->page = page_pool_alloc_pages(rx_q->page_pool, gfp);
1440 buf->page_offset = stmmac_rx_offset(priv);
1443 if (priv->sph && !buf->sec_page) {
1444 buf->sec_page = page_pool_alloc_pages(rx_q->page_pool, gfp);
1448 buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
1449 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true);
1451 buf->sec_page = NULL;
1452 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false);
1455 buf->addr = page_pool_get_dma_addr(buf->page) + buf->page_offset;
1457 stmmac_set_desc_addr(priv, p, buf->addr);
1458 if (dma_conf->dma_buf_sz == BUF_SIZE_16KiB)
1459 stmmac_init_desc3(priv, p);
1465 * stmmac_free_rx_buffer - free RX dma buffers
1466 * @priv: private structure
1470 static void stmmac_free_rx_buffer(struct stmmac_priv *priv,
1471 struct stmmac_rx_queue *rx_q,
1474 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1477 page_pool_put_full_page(rx_q->page_pool, buf->page, false);
1481 page_pool_put_full_page(rx_q->page_pool, buf->sec_page, false);
1482 buf->sec_page = NULL;
1486 * stmmac_free_tx_buffer - free RX dma buffers
1487 * @priv: private structure
1488 * @dma_conf: structure to take the dma data
1489 * @queue: RX queue index
1492 static void stmmac_free_tx_buffer(struct stmmac_priv *priv,
1493 struct stmmac_dma_conf *dma_conf,
1496 struct stmmac_tx_queue *tx_q = &dma_conf->tx_queue[queue];
1498 if (tx_q->tx_skbuff_dma[i].buf &&
1499 tx_q->tx_skbuff_dma[i].buf_type != STMMAC_TXBUF_T_XDP_TX) {
1500 if (tx_q->tx_skbuff_dma[i].map_as_page)
1501 dma_unmap_page(priv->device,
1502 tx_q->tx_skbuff_dma[i].buf,
1503 tx_q->tx_skbuff_dma[i].len,
1506 dma_unmap_single(priv->device,
1507 tx_q->tx_skbuff_dma[i].buf,
1508 tx_q->tx_skbuff_dma[i].len,
1512 if (tx_q->xdpf[i] &&
1513 (tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XDP_TX ||
1514 tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XDP_NDO)) {
1515 xdp_return_frame(tx_q->xdpf[i]);
1516 tx_q->xdpf[i] = NULL;
1519 if (tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XSK_TX)
1520 tx_q->xsk_frames_done++;
1522 if (tx_q->tx_skbuff[i] &&
1523 tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_SKB) {
1524 dev_kfree_skb_any(tx_q->tx_skbuff[i]);
1525 tx_q->tx_skbuff[i] = NULL;
1528 tx_q->tx_skbuff_dma[i].buf = 0;
1529 tx_q->tx_skbuff_dma[i].map_as_page = false;
1533 * dma_free_rx_skbufs - free RX dma buffers
1534 * @priv: private structure
1535 * @dma_conf: structure to take the dma data
1536 * @queue: RX queue index
1538 static void dma_free_rx_skbufs(struct stmmac_priv *priv,
1539 struct stmmac_dma_conf *dma_conf,
1542 struct stmmac_rx_queue *rx_q = &dma_conf->rx_queue[queue];
1545 for (i = 0; i < dma_conf->dma_rx_size; i++)
1546 stmmac_free_rx_buffer(priv, rx_q, i);
1549 static int stmmac_alloc_rx_buffers(struct stmmac_priv *priv,
1550 struct stmmac_dma_conf *dma_conf,
1551 u32 queue, gfp_t flags)
1553 struct stmmac_rx_queue *rx_q = &dma_conf->rx_queue[queue];
1556 for (i = 0; i < dma_conf->dma_rx_size; i++) {
1560 if (priv->extend_desc)
1561 p = &((rx_q->dma_erx + i)->basic);
1563 p = rx_q->dma_rx + i;
1565 ret = stmmac_init_rx_buffers(priv, dma_conf, p, i, flags,
1570 rx_q->buf_alloc_num++;
1577 * dma_free_rx_xskbufs - free RX dma buffers from XSK pool
1578 * @priv: private structure
1579 * @dma_conf: structure to take the dma data
1580 * @queue: RX queue index
1582 static void dma_free_rx_xskbufs(struct stmmac_priv *priv,
1583 struct stmmac_dma_conf *dma_conf,
1586 struct stmmac_rx_queue *rx_q = &dma_conf->rx_queue[queue];
1589 for (i = 0; i < dma_conf->dma_rx_size; i++) {
1590 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1595 xsk_buff_free(buf->xdp);
1600 static int stmmac_alloc_rx_buffers_zc(struct stmmac_priv *priv,
1601 struct stmmac_dma_conf *dma_conf,
1604 struct stmmac_rx_queue *rx_q = &dma_conf->rx_queue[queue];
1607 for (i = 0; i < dma_conf->dma_rx_size; i++) {
1608 struct stmmac_rx_buffer *buf;
1609 dma_addr_t dma_addr;
1612 if (priv->extend_desc)
1613 p = (struct dma_desc *)(rx_q->dma_erx + i);
1615 p = rx_q->dma_rx + i;
1617 buf = &rx_q->buf_pool[i];
1619 buf->xdp = xsk_buff_alloc(rx_q->xsk_pool);
1623 dma_addr = xsk_buff_xdp_get_dma(buf->xdp);
1624 stmmac_set_desc_addr(priv, p, dma_addr);
1625 rx_q->buf_alloc_num++;
1631 static struct xsk_buff_pool *stmmac_get_xsk_pool(struct stmmac_priv *priv, u32 queue)
1633 if (!stmmac_xdp_is_enabled(priv) || !test_bit(queue, priv->af_xdp_zc_qps))
1636 return xsk_get_pool_from_qid(priv->dev, queue);
1640 * __init_dma_rx_desc_rings - init the RX descriptor ring (per queue)
1641 * @priv: driver private structure
1642 * @dma_conf: structure to take the dma data
1643 * @queue: RX queue index
1645 * Description: this function initializes the DMA RX descriptors
1646 * and allocates the socket buffers. It supports the chained and ring
1649 static int __init_dma_rx_desc_rings(struct stmmac_priv *priv,
1650 struct stmmac_dma_conf *dma_conf,
1651 u32 queue, gfp_t flags)
1653 struct stmmac_rx_queue *rx_q = &dma_conf->rx_queue[queue];
1656 netif_dbg(priv, probe, priv->dev,
1657 "(%s) dma_rx_phy=0x%08x\n", __func__,
1658 (u32)rx_q->dma_rx_phy);
1660 stmmac_clear_rx_descriptors(priv, dma_conf, queue);
1662 xdp_rxq_info_unreg_mem_model(&rx_q->xdp_rxq);
1664 rx_q->xsk_pool = stmmac_get_xsk_pool(priv, queue);
1666 if (rx_q->xsk_pool) {
1667 WARN_ON(xdp_rxq_info_reg_mem_model(&rx_q->xdp_rxq,
1668 MEM_TYPE_XSK_BUFF_POOL,
1670 netdev_info(priv->dev,
1671 "Register MEM_TYPE_XSK_BUFF_POOL RxQ-%d\n",
1673 xsk_pool_set_rxq_info(rx_q->xsk_pool, &rx_q->xdp_rxq);
1675 WARN_ON(xdp_rxq_info_reg_mem_model(&rx_q->xdp_rxq,
1678 netdev_info(priv->dev,
1679 "Register MEM_TYPE_PAGE_POOL RxQ-%d\n",
1683 if (rx_q->xsk_pool) {
1684 /* RX XDP ZC buffer pool may not be populated, e.g.
1687 stmmac_alloc_rx_buffers_zc(priv, dma_conf, queue);
1689 ret = stmmac_alloc_rx_buffers(priv, dma_conf, queue, flags);
1694 /* Setup the chained descriptor addresses */
1695 if (priv->mode == STMMAC_CHAIN_MODE) {
1696 if (priv->extend_desc)
1697 stmmac_mode_init(priv, rx_q->dma_erx,
1699 dma_conf->dma_rx_size, 1);
1701 stmmac_mode_init(priv, rx_q->dma_rx,
1703 dma_conf->dma_rx_size, 0);
1709 static int init_dma_rx_desc_rings(struct net_device *dev,
1710 struct stmmac_dma_conf *dma_conf,
1713 struct stmmac_priv *priv = netdev_priv(dev);
1714 u32 rx_count = priv->plat->rx_queues_to_use;
1718 /* RX INITIALIZATION */
1719 netif_dbg(priv, probe, priv->dev,
1720 "SKB addresses:\nskb\t\tskb data\tdma data\n");
1722 for (queue = 0; queue < rx_count; queue++) {
1723 ret = __init_dma_rx_desc_rings(priv, dma_conf, queue, flags);
1725 goto err_init_rx_buffers;
1730 err_init_rx_buffers:
1731 while (queue >= 0) {
1732 struct stmmac_rx_queue *rx_q = &dma_conf->rx_queue[queue];
1735 dma_free_rx_xskbufs(priv, dma_conf, queue);
1737 dma_free_rx_skbufs(priv, dma_conf, queue);
1739 rx_q->buf_alloc_num = 0;
1740 rx_q->xsk_pool = NULL;
1749 * __init_dma_tx_desc_rings - init the TX descriptor ring (per queue)
1750 * @priv: driver private structure
1751 * @dma_conf: structure to take the dma data
1752 * @queue: TX queue index
1753 * Description: this function initializes the DMA TX descriptors
1754 * and allocates the socket buffers. It supports the chained and ring
1757 static int __init_dma_tx_desc_rings(struct stmmac_priv *priv,
1758 struct stmmac_dma_conf *dma_conf,
1761 struct stmmac_tx_queue *tx_q = &dma_conf->tx_queue[queue];
1764 netif_dbg(priv, probe, priv->dev,
1765 "(%s) dma_tx_phy=0x%08x\n", __func__,
1766 (u32)tx_q->dma_tx_phy);
1768 /* Setup the chained descriptor addresses */
1769 if (priv->mode == STMMAC_CHAIN_MODE) {
1770 if (priv->extend_desc)
1771 stmmac_mode_init(priv, tx_q->dma_etx,
1773 dma_conf->dma_tx_size, 1);
1774 else if (!(tx_q->tbs & STMMAC_TBS_AVAIL))
1775 stmmac_mode_init(priv, tx_q->dma_tx,
1777 dma_conf->dma_tx_size, 0);
1780 tx_q->xsk_pool = stmmac_get_xsk_pool(priv, queue);
1782 for (i = 0; i < dma_conf->dma_tx_size; i++) {
1785 if (priv->extend_desc)
1786 p = &((tx_q->dma_etx + i)->basic);
1787 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1788 p = &((tx_q->dma_entx + i)->basic);
1790 p = tx_q->dma_tx + i;
1792 stmmac_clear_desc(priv, p);
1794 tx_q->tx_skbuff_dma[i].buf = 0;
1795 tx_q->tx_skbuff_dma[i].map_as_page = false;
1796 tx_q->tx_skbuff_dma[i].len = 0;
1797 tx_q->tx_skbuff_dma[i].last_segment = false;
1798 tx_q->tx_skbuff[i] = NULL;
1804 static int init_dma_tx_desc_rings(struct net_device *dev,
1805 struct stmmac_dma_conf *dma_conf)
1807 struct stmmac_priv *priv = netdev_priv(dev);
1811 tx_queue_cnt = priv->plat->tx_queues_to_use;
1813 for (queue = 0; queue < tx_queue_cnt; queue++)
1814 __init_dma_tx_desc_rings(priv, dma_conf, queue);
1820 * init_dma_desc_rings - init the RX/TX descriptor rings
1821 * @dev: net device structure
1822 * @dma_conf: structure to take the dma data
1824 * Description: this function initializes the DMA RX/TX descriptors
1825 * and allocates the socket buffers. It supports the chained and ring
1828 static int init_dma_desc_rings(struct net_device *dev,
1829 struct stmmac_dma_conf *dma_conf,
1832 struct stmmac_priv *priv = netdev_priv(dev);
1835 ret = init_dma_rx_desc_rings(dev, dma_conf, flags);
1839 ret = init_dma_tx_desc_rings(dev, dma_conf);
1841 stmmac_clear_descriptors(priv, dma_conf);
1843 if (netif_msg_hw(priv))
1844 stmmac_display_rings(priv, dma_conf);
1850 * dma_free_tx_skbufs - free TX dma buffers
1851 * @priv: private structure
1852 * @dma_conf: structure to take the dma data
1853 * @queue: TX queue index
1855 static void dma_free_tx_skbufs(struct stmmac_priv *priv,
1856 struct stmmac_dma_conf *dma_conf,
1859 struct stmmac_tx_queue *tx_q = &dma_conf->tx_queue[queue];
1862 tx_q->xsk_frames_done = 0;
1864 for (i = 0; i < dma_conf->dma_tx_size; i++)
1865 stmmac_free_tx_buffer(priv, dma_conf, queue, i);
1867 if (tx_q->xsk_pool && tx_q->xsk_frames_done) {
1868 xsk_tx_completed(tx_q->xsk_pool, tx_q->xsk_frames_done);
1869 tx_q->xsk_frames_done = 0;
1870 tx_q->xsk_pool = NULL;
1875 * stmmac_free_tx_skbufs - free TX skb buffers
1876 * @priv: private structure
1878 static void stmmac_free_tx_skbufs(struct stmmac_priv *priv)
1880 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1883 for (queue = 0; queue < tx_queue_cnt; queue++)
1884 dma_free_tx_skbufs(priv, &priv->dma_conf, queue);
1888 * __free_dma_rx_desc_resources - free RX dma desc resources (per queue)
1889 * @priv: private structure
1890 * @dma_conf: structure to take the dma data
1891 * @queue: RX queue index
1893 static void __free_dma_rx_desc_resources(struct stmmac_priv *priv,
1894 struct stmmac_dma_conf *dma_conf,
1897 struct stmmac_rx_queue *rx_q = &dma_conf->rx_queue[queue];
1899 /* Release the DMA RX socket buffers */
1901 dma_free_rx_xskbufs(priv, dma_conf, queue);
1903 dma_free_rx_skbufs(priv, dma_conf, queue);
1905 rx_q->buf_alloc_num = 0;
1906 rx_q->xsk_pool = NULL;
1908 /* Free DMA regions of consistent memory previously allocated */
1909 if (!priv->extend_desc)
1910 dma_free_coherent(priv->device, dma_conf->dma_rx_size *
1911 sizeof(struct dma_desc),
1912 rx_q->dma_rx, rx_q->dma_rx_phy);
1914 dma_free_coherent(priv->device, dma_conf->dma_rx_size *
1915 sizeof(struct dma_extended_desc),
1916 rx_q->dma_erx, rx_q->dma_rx_phy);
1918 if (xdp_rxq_info_is_reg(&rx_q->xdp_rxq))
1919 xdp_rxq_info_unreg(&rx_q->xdp_rxq);
1921 kfree(rx_q->buf_pool);
1922 if (rx_q->page_pool)
1923 page_pool_destroy(rx_q->page_pool);
1926 static void free_dma_rx_desc_resources(struct stmmac_priv *priv,
1927 struct stmmac_dma_conf *dma_conf)
1929 u32 rx_count = priv->plat->rx_queues_to_use;
1932 /* Free RX queue resources */
1933 for (queue = 0; queue < rx_count; queue++)
1934 __free_dma_rx_desc_resources(priv, dma_conf, queue);
1938 * __free_dma_tx_desc_resources - free TX dma desc resources (per queue)
1939 * @priv: private structure
1940 * @dma_conf: structure to take the dma data
1941 * @queue: TX queue index
1943 static void __free_dma_tx_desc_resources(struct stmmac_priv *priv,
1944 struct stmmac_dma_conf *dma_conf,
1947 struct stmmac_tx_queue *tx_q = &dma_conf->tx_queue[queue];
1951 /* Release the DMA TX socket buffers */
1952 dma_free_tx_skbufs(priv, dma_conf, queue);
1954 if (priv->extend_desc) {
1955 size = sizeof(struct dma_extended_desc);
1956 addr = tx_q->dma_etx;
1957 } else if (tx_q->tbs & STMMAC_TBS_AVAIL) {
1958 size = sizeof(struct dma_edesc);
1959 addr = tx_q->dma_entx;
1961 size = sizeof(struct dma_desc);
1962 addr = tx_q->dma_tx;
1965 size *= dma_conf->dma_tx_size;
1967 dma_free_coherent(priv->device, size, addr, tx_q->dma_tx_phy);
1969 kfree(tx_q->tx_skbuff_dma);
1970 kfree(tx_q->tx_skbuff);
1973 static void free_dma_tx_desc_resources(struct stmmac_priv *priv,
1974 struct stmmac_dma_conf *dma_conf)
1976 u32 tx_count = priv->plat->tx_queues_to_use;
1979 /* Free TX queue resources */
1980 for (queue = 0; queue < tx_count; queue++)
1981 __free_dma_tx_desc_resources(priv, dma_conf, queue);
1985 * __alloc_dma_rx_desc_resources - alloc RX resources (per queue).
1986 * @priv: private structure
1987 * @dma_conf: structure to take the dma data
1988 * @queue: RX queue index
1989 * Description: according to which descriptor can be used (extend or basic)
1990 * this function allocates the resources for TX and RX paths. In case of
1991 * reception, for example, it pre-allocated the RX socket buffer in order to
1992 * allow zero-copy mechanism.
1994 static int __alloc_dma_rx_desc_resources(struct stmmac_priv *priv,
1995 struct stmmac_dma_conf *dma_conf,
1998 struct stmmac_rx_queue *rx_q = &dma_conf->rx_queue[queue];
1999 struct stmmac_channel *ch = &priv->channel[queue];
2000 bool xdp_prog = stmmac_xdp_is_enabled(priv);
2001 struct page_pool_params pp_params = { 0 };
2002 unsigned int num_pages;
2003 unsigned int napi_id;
2006 rx_q->queue_index = queue;
2007 rx_q->priv_data = priv;
2009 pp_params.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
2010 pp_params.pool_size = dma_conf->dma_rx_size;
2011 num_pages = DIV_ROUND_UP(dma_conf->dma_buf_sz, PAGE_SIZE);
2012 pp_params.order = ilog2(num_pages);
2013 pp_params.nid = dev_to_node(priv->device);
2014 pp_params.dev = priv->device;
2015 pp_params.dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
2016 pp_params.offset = stmmac_rx_offset(priv);
2017 pp_params.max_len = STMMAC_MAX_RX_BUF_SIZE(num_pages);
2019 rx_q->page_pool = page_pool_create(&pp_params);
2020 if (IS_ERR(rx_q->page_pool)) {
2021 ret = PTR_ERR(rx_q->page_pool);
2022 rx_q->page_pool = NULL;
2026 rx_q->buf_pool = kcalloc(dma_conf->dma_rx_size,
2027 sizeof(*rx_q->buf_pool),
2029 if (!rx_q->buf_pool)
2032 if (priv->extend_desc) {
2033 rx_q->dma_erx = dma_alloc_coherent(priv->device,
2034 dma_conf->dma_rx_size *
2035 sizeof(struct dma_extended_desc),
2042 rx_q->dma_rx = dma_alloc_coherent(priv->device,
2043 dma_conf->dma_rx_size *
2044 sizeof(struct dma_desc),
2051 if (stmmac_xdp_is_enabled(priv) &&
2052 test_bit(queue, priv->af_xdp_zc_qps))
2053 napi_id = ch->rxtx_napi.napi_id;
2055 napi_id = ch->rx_napi.napi_id;
2057 ret = xdp_rxq_info_reg(&rx_q->xdp_rxq, priv->dev,
2061 netdev_err(priv->dev, "Failed to register xdp rxq info\n");
2068 static int alloc_dma_rx_desc_resources(struct stmmac_priv *priv,
2069 struct stmmac_dma_conf *dma_conf)
2071 u32 rx_count = priv->plat->rx_queues_to_use;
2075 /* RX queues buffers and DMA */
2076 for (queue = 0; queue < rx_count; queue++) {
2077 ret = __alloc_dma_rx_desc_resources(priv, dma_conf, queue);
2085 free_dma_rx_desc_resources(priv, dma_conf);
2091 * __alloc_dma_tx_desc_resources - alloc TX resources (per queue).
2092 * @priv: private structure
2093 * @dma_conf: structure to take the dma data
2094 * @queue: TX queue index
2095 * Description: according to which descriptor can be used (extend or basic)
2096 * this function allocates the resources for TX and RX paths. In case of
2097 * reception, for example, it pre-allocated the RX socket buffer in order to
2098 * allow zero-copy mechanism.
2100 static int __alloc_dma_tx_desc_resources(struct stmmac_priv *priv,
2101 struct stmmac_dma_conf *dma_conf,
2104 struct stmmac_tx_queue *tx_q = &dma_conf->tx_queue[queue];
2108 tx_q->queue_index = queue;
2109 tx_q->priv_data = priv;
2111 tx_q->tx_skbuff_dma = kcalloc(dma_conf->dma_tx_size,
2112 sizeof(*tx_q->tx_skbuff_dma),
2114 if (!tx_q->tx_skbuff_dma)
2117 tx_q->tx_skbuff = kcalloc(dma_conf->dma_tx_size,
2118 sizeof(struct sk_buff *),
2120 if (!tx_q->tx_skbuff)
2123 if (priv->extend_desc)
2124 size = sizeof(struct dma_extended_desc);
2125 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2126 size = sizeof(struct dma_edesc);
2128 size = sizeof(struct dma_desc);
2130 size *= dma_conf->dma_tx_size;
2132 addr = dma_alloc_coherent(priv->device, size,
2133 &tx_q->dma_tx_phy, GFP_KERNEL);
2137 if (priv->extend_desc)
2138 tx_q->dma_etx = addr;
2139 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2140 tx_q->dma_entx = addr;
2142 tx_q->dma_tx = addr;
2147 static int alloc_dma_tx_desc_resources(struct stmmac_priv *priv,
2148 struct stmmac_dma_conf *dma_conf)
2150 u32 tx_count = priv->plat->tx_queues_to_use;
2154 /* TX queues buffers and DMA */
2155 for (queue = 0; queue < tx_count; queue++) {
2156 ret = __alloc_dma_tx_desc_resources(priv, dma_conf, queue);
2164 free_dma_tx_desc_resources(priv, dma_conf);
2169 * alloc_dma_desc_resources - alloc TX/RX resources.
2170 * @priv: private structure
2171 * @dma_conf: structure to take the dma data
2172 * Description: according to which descriptor can be used (extend or basic)
2173 * this function allocates the resources for TX and RX paths. In case of
2174 * reception, for example, it pre-allocated the RX socket buffer in order to
2175 * allow zero-copy mechanism.
2177 static int alloc_dma_desc_resources(struct stmmac_priv *priv,
2178 struct stmmac_dma_conf *dma_conf)
2181 int ret = alloc_dma_rx_desc_resources(priv, dma_conf);
2186 ret = alloc_dma_tx_desc_resources(priv, dma_conf);
2192 * free_dma_desc_resources - free dma desc resources
2193 * @priv: private structure
2194 * @dma_conf: structure to take the dma data
2196 static void free_dma_desc_resources(struct stmmac_priv *priv,
2197 struct stmmac_dma_conf *dma_conf)
2199 /* Release the DMA TX socket buffers */
2200 free_dma_tx_desc_resources(priv, dma_conf);
2202 /* Release the DMA RX socket buffers later
2203 * to ensure all pending XDP_TX buffers are returned.
2205 free_dma_rx_desc_resources(priv, dma_conf);
2209 * stmmac_mac_enable_rx_queues - Enable MAC rx queues
2210 * @priv: driver private structure
2211 * Description: It is used for enabling the rx queues in the MAC
2213 static void stmmac_mac_enable_rx_queues(struct stmmac_priv *priv)
2215 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2219 for (queue = 0; queue < rx_queues_count; queue++) {
2220 mode = priv->plat->rx_queues_cfg[queue].mode_to_use;
2221 stmmac_rx_queue_enable(priv, priv->hw, mode, queue);
2226 * stmmac_start_rx_dma - start RX DMA channel
2227 * @priv: driver private structure
2228 * @chan: RX channel index
2230 * This starts a RX DMA channel
2232 static void stmmac_start_rx_dma(struct stmmac_priv *priv, u32 chan)
2234 netdev_dbg(priv->dev, "DMA RX processes started in channel %d\n", chan);
2235 stmmac_start_rx(priv, priv->ioaddr, chan);
2239 * stmmac_start_tx_dma - start TX DMA channel
2240 * @priv: driver private structure
2241 * @chan: TX channel index
2243 * This starts a TX DMA channel
2245 static void stmmac_start_tx_dma(struct stmmac_priv *priv, u32 chan)
2247 netdev_dbg(priv->dev, "DMA TX processes started in channel %d\n", chan);
2248 stmmac_start_tx(priv, priv->ioaddr, chan);
2252 * stmmac_stop_rx_dma - stop RX DMA channel
2253 * @priv: driver private structure
2254 * @chan: RX channel index
2256 * This stops a RX DMA channel
2258 static void stmmac_stop_rx_dma(struct stmmac_priv *priv, u32 chan)
2260 netdev_dbg(priv->dev, "DMA RX processes stopped in channel %d\n", chan);
2261 stmmac_stop_rx(priv, priv->ioaddr, chan);
2265 * stmmac_stop_tx_dma - stop TX DMA channel
2266 * @priv: driver private structure
2267 * @chan: TX channel index
2269 * This stops a TX DMA channel
2271 static void stmmac_stop_tx_dma(struct stmmac_priv *priv, u32 chan)
2273 netdev_dbg(priv->dev, "DMA TX processes stopped in channel %d\n", chan);
2274 stmmac_stop_tx(priv, priv->ioaddr, chan);
2277 static void stmmac_enable_all_dma_irq(struct stmmac_priv *priv)
2279 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2280 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2281 u32 dma_csr_ch = max(rx_channels_count, tx_channels_count);
2284 for (chan = 0; chan < dma_csr_ch; chan++) {
2285 struct stmmac_channel *ch = &priv->channel[chan];
2286 unsigned long flags;
2288 spin_lock_irqsave(&ch->lock, flags);
2289 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 1);
2290 spin_unlock_irqrestore(&ch->lock, flags);
2295 * stmmac_start_all_dma - start all RX and TX DMA channels
2296 * @priv: driver private structure
2298 * This starts all the RX and TX DMA channels
2300 static void stmmac_start_all_dma(struct stmmac_priv *priv)
2302 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2303 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2306 for (chan = 0; chan < rx_channels_count; chan++)
2307 stmmac_start_rx_dma(priv, chan);
2309 for (chan = 0; chan < tx_channels_count; chan++)
2310 stmmac_start_tx_dma(priv, chan);
2314 * stmmac_stop_all_dma - stop all RX and TX DMA channels
2315 * @priv: driver private structure
2317 * This stops the RX and TX DMA channels
2319 static void stmmac_stop_all_dma(struct stmmac_priv *priv)
2321 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2322 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2325 for (chan = 0; chan < rx_channels_count; chan++)
2326 stmmac_stop_rx_dma(priv, chan);
2328 for (chan = 0; chan < tx_channels_count; chan++)
2329 stmmac_stop_tx_dma(priv, chan);
2333 * stmmac_dma_operation_mode - HW DMA operation mode
2334 * @priv: driver private structure
2335 * Description: it is used for configuring the DMA operation mode register in
2336 * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
2338 static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
2340 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2341 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2342 int rxfifosz = priv->plat->rx_fifo_size;
2343 int txfifosz = priv->plat->tx_fifo_size;
2350 rxfifosz = priv->dma_cap.rx_fifo_size;
2352 txfifosz = priv->dma_cap.tx_fifo_size;
2354 /* Adjust for real per queue fifo size */
2355 rxfifosz /= rx_channels_count;
2356 txfifosz /= tx_channels_count;
2358 if (priv->plat->force_thresh_dma_mode) {
2361 } else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
2363 * In case of GMAC, SF mode can be enabled
2364 * to perform the TX COE in HW. This depends on:
2365 * 1) TX COE if actually supported
2366 * 2) There is no bugged Jumbo frame support
2367 * that needs to not insert csum in the TDES.
2369 txmode = SF_DMA_MODE;
2370 rxmode = SF_DMA_MODE;
2371 priv->xstats.threshold = SF_DMA_MODE;
2374 rxmode = SF_DMA_MODE;
2377 /* configure all channels */
2378 for (chan = 0; chan < rx_channels_count; chan++) {
2379 struct stmmac_rx_queue *rx_q = &priv->dma_conf.rx_queue[chan];
2382 qmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
2384 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan,
2387 if (rx_q->xsk_pool) {
2388 buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool);
2389 stmmac_set_dma_bfsize(priv, priv->ioaddr,
2393 stmmac_set_dma_bfsize(priv, priv->ioaddr,
2394 priv->dma_conf.dma_buf_sz,
2399 for (chan = 0; chan < tx_channels_count; chan++) {
2400 qmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
2402 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan,
2407 static bool stmmac_xdp_xmit_zc(struct stmmac_priv *priv, u32 queue, u32 budget)
2409 struct netdev_queue *nq = netdev_get_tx_queue(priv->dev, queue);
2410 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[queue];
2411 struct xsk_buff_pool *pool = tx_q->xsk_pool;
2412 unsigned int entry = tx_q->cur_tx;
2413 struct dma_desc *tx_desc = NULL;
2414 struct xdp_desc xdp_desc;
2415 bool work_done = true;
2417 /* Avoids TX time-out as we are sharing with slow path */
2418 txq_trans_cond_update(nq);
2420 budget = min(budget, stmmac_tx_avail(priv, queue));
2422 while (budget-- > 0) {
2423 dma_addr_t dma_addr;
2426 /* We are sharing with slow path and stop XSK TX desc submission when
2427 * available TX ring is less than threshold.
2429 if (unlikely(stmmac_tx_avail(priv, queue) < STMMAC_TX_XSK_AVAIL) ||
2430 !netif_carrier_ok(priv->dev)) {
2435 if (!xsk_tx_peek_desc(pool, &xdp_desc))
2438 if (likely(priv->extend_desc))
2439 tx_desc = (struct dma_desc *)(tx_q->dma_etx + entry);
2440 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2441 tx_desc = &tx_q->dma_entx[entry].basic;
2443 tx_desc = tx_q->dma_tx + entry;
2445 dma_addr = xsk_buff_raw_get_dma(pool, xdp_desc.addr);
2446 xsk_buff_raw_dma_sync_for_device(pool, dma_addr, xdp_desc.len);
2448 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XSK_TX;
2450 /* To return XDP buffer to XSK pool, we simple call
2451 * xsk_tx_completed(), so we don't need to fill up
2454 tx_q->tx_skbuff_dma[entry].buf = 0;
2455 tx_q->xdpf[entry] = NULL;
2457 tx_q->tx_skbuff_dma[entry].map_as_page = false;
2458 tx_q->tx_skbuff_dma[entry].len = xdp_desc.len;
2459 tx_q->tx_skbuff_dma[entry].last_segment = true;
2460 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
2462 stmmac_set_desc_addr(priv, tx_desc, dma_addr);
2464 tx_q->tx_count_frames++;
2466 if (!priv->tx_coal_frames[queue])
2468 else if (tx_q->tx_count_frames % priv->tx_coal_frames[queue] == 0)
2474 tx_q->tx_count_frames = 0;
2475 stmmac_set_tx_ic(priv, tx_desc);
2476 priv->xstats.tx_set_ic_bit++;
2479 stmmac_prepare_tx_desc(priv, tx_desc, 1, xdp_desc.len,
2480 true, priv->mode, true, true,
2483 stmmac_enable_dma_transmission(priv, priv->ioaddr);
2485 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_conf.dma_tx_size);
2486 entry = tx_q->cur_tx;
2490 stmmac_flush_tx_descriptors(priv, queue);
2491 xsk_tx_release(pool);
2494 /* Return true if all of the 3 conditions are met
2495 * a) TX Budget is still available
2496 * b) work_done = true when XSK TX desc peek is empty (no more
2497 * pending XSK TX for transmission)
2499 return !!budget && work_done;
2502 static void stmmac_bump_dma_threshold(struct stmmac_priv *priv, u32 chan)
2504 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) && tc <= 256) {
2507 if (priv->plat->force_thresh_dma_mode)
2508 stmmac_set_dma_operation_mode(priv, tc, tc, chan);
2510 stmmac_set_dma_operation_mode(priv, tc, SF_DMA_MODE,
2513 priv->xstats.threshold = tc;
2518 * stmmac_tx_clean - to manage the transmission completion
2519 * @priv: driver private structure
2520 * @budget: napi budget limiting this functions packet handling
2521 * @queue: TX queue index
2522 * Description: it reclaims the transmit resources after transmission completes.
2524 static int stmmac_tx_clean(struct stmmac_priv *priv, int budget, u32 queue)
2526 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[queue];
2527 unsigned int bytes_compl = 0, pkts_compl = 0;
2528 unsigned int entry, xmits = 0, count = 0;
2530 __netif_tx_lock_bh(netdev_get_tx_queue(priv->dev, queue));
2532 priv->xstats.tx_clean++;
2534 tx_q->xsk_frames_done = 0;
2536 entry = tx_q->dirty_tx;
2538 /* Try to clean all TX complete frame in 1 shot */
2539 while ((entry != tx_q->cur_tx) && count < priv->dma_conf.dma_tx_size) {
2540 struct xdp_frame *xdpf;
2541 struct sk_buff *skb;
2545 if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_TX ||
2546 tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_NDO) {
2547 xdpf = tx_q->xdpf[entry];
2549 } else if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_SKB) {
2551 skb = tx_q->tx_skbuff[entry];
2557 if (priv->extend_desc)
2558 p = (struct dma_desc *)(tx_q->dma_etx + entry);
2559 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2560 p = &tx_q->dma_entx[entry].basic;
2562 p = tx_q->dma_tx + entry;
2564 status = stmmac_tx_status(priv, &priv->dev->stats,
2565 &priv->xstats, p, priv->ioaddr);
2566 /* Check if the descriptor is owned by the DMA */
2567 if (unlikely(status & tx_dma_own))
2572 /* Make sure descriptor fields are read after reading
2577 /* Just consider the last segment and ...*/
2578 if (likely(!(status & tx_not_ls))) {
2579 /* ... verify the status error condition */
2580 if (unlikely(status & tx_err)) {
2581 priv->dev->stats.tx_errors++;
2582 if (unlikely(status & tx_err_bump_tc))
2583 stmmac_bump_dma_threshold(priv, queue);
2585 priv->dev->stats.tx_packets++;
2586 priv->xstats.tx_pkt_n++;
2587 priv->xstats.txq_stats[queue].tx_pkt_n++;
2590 stmmac_get_tx_hwtstamp(priv, p, skb);
2593 if (likely(tx_q->tx_skbuff_dma[entry].buf &&
2594 tx_q->tx_skbuff_dma[entry].buf_type != STMMAC_TXBUF_T_XDP_TX)) {
2595 if (tx_q->tx_skbuff_dma[entry].map_as_page)
2596 dma_unmap_page(priv->device,
2597 tx_q->tx_skbuff_dma[entry].buf,
2598 tx_q->tx_skbuff_dma[entry].len,
2601 dma_unmap_single(priv->device,
2602 tx_q->tx_skbuff_dma[entry].buf,
2603 tx_q->tx_skbuff_dma[entry].len,
2605 tx_q->tx_skbuff_dma[entry].buf = 0;
2606 tx_q->tx_skbuff_dma[entry].len = 0;
2607 tx_q->tx_skbuff_dma[entry].map_as_page = false;
2610 stmmac_clean_desc3(priv, tx_q, p);
2612 tx_q->tx_skbuff_dma[entry].last_segment = false;
2613 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
2616 tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_TX) {
2617 xdp_return_frame_rx_napi(xdpf);
2618 tx_q->xdpf[entry] = NULL;
2622 tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_NDO) {
2623 xdp_return_frame(xdpf);
2624 tx_q->xdpf[entry] = NULL;
2627 if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XSK_TX)
2628 tx_q->xsk_frames_done++;
2630 if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_SKB) {
2633 bytes_compl += skb->len;
2634 dev_consume_skb_any(skb);
2635 tx_q->tx_skbuff[entry] = NULL;
2639 stmmac_release_tx_desc(priv, p, priv->mode);
2641 entry = STMMAC_GET_ENTRY(entry, priv->dma_conf.dma_tx_size);
2643 tx_q->dirty_tx = entry;
2645 netdev_tx_completed_queue(netdev_get_tx_queue(priv->dev, queue),
2646 pkts_compl, bytes_compl);
2648 if (unlikely(netif_tx_queue_stopped(netdev_get_tx_queue(priv->dev,
2650 stmmac_tx_avail(priv, queue) > STMMAC_TX_THRESH(priv)) {
2652 netif_dbg(priv, tx_done, priv->dev,
2653 "%s: restart transmit\n", __func__);
2654 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, queue));
2657 if (tx_q->xsk_pool) {
2660 if (tx_q->xsk_frames_done)
2661 xsk_tx_completed(tx_q->xsk_pool, tx_q->xsk_frames_done);
2663 if (xsk_uses_need_wakeup(tx_q->xsk_pool))
2664 xsk_set_tx_need_wakeup(tx_q->xsk_pool);
2666 /* For XSK TX, we try to send as many as possible.
2667 * If XSK work done (XSK TX desc empty and budget still
2668 * available), return "budget - 1" to reenable TX IRQ.
2669 * Else, return "budget" to make NAPI continue polling.
2671 work_done = stmmac_xdp_xmit_zc(priv, queue,
2672 STMMAC_XSK_TX_BUDGET_MAX);
2679 if (priv->eee_enabled && !priv->tx_path_in_lpi_mode &&
2680 priv->eee_sw_timer_en) {
2681 if (stmmac_enable_eee_mode(priv))
2682 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer));
2685 /* We still have pending packets, let's call for a new scheduling */
2686 if (tx_q->dirty_tx != tx_q->cur_tx)
2687 hrtimer_start(&tx_q->txtimer,
2688 STMMAC_COAL_TIMER(priv->tx_coal_timer[queue]),
2691 __netif_tx_unlock_bh(netdev_get_tx_queue(priv->dev, queue));
2693 /* Combine decisions from TX clean and XSK TX */
2694 return max(count, xmits);
2698 * stmmac_tx_err - to manage the tx error
2699 * @priv: driver private structure
2700 * @chan: channel index
2701 * Description: it cleans the descriptors and restarts the transmission
2702 * in case of transmission errors.
2704 static void stmmac_tx_err(struct stmmac_priv *priv, u32 chan)
2706 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[chan];
2708 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, chan));
2710 stmmac_stop_tx_dma(priv, chan);
2711 dma_free_tx_skbufs(priv, &priv->dma_conf, chan);
2712 stmmac_clear_tx_descriptors(priv, &priv->dma_conf, chan);
2713 stmmac_reset_tx_queue(priv, chan);
2714 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2715 tx_q->dma_tx_phy, chan);
2716 stmmac_start_tx_dma(priv, chan);
2718 priv->dev->stats.tx_errors++;
2719 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, chan));
2723 * stmmac_set_dma_operation_mode - Set DMA operation mode by channel
2724 * @priv: driver private structure
2725 * @txmode: TX operating mode
2726 * @rxmode: RX operating mode
2727 * @chan: channel index
2728 * Description: it is used for configuring of the DMA operation mode in
2729 * runtime in order to program the tx/rx DMA thresholds or Store-And-Forward
2732 static void stmmac_set_dma_operation_mode(struct stmmac_priv *priv, u32 txmode,
2733 u32 rxmode, u32 chan)
2735 u8 rxqmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
2736 u8 txqmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
2737 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2738 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2739 int rxfifosz = priv->plat->rx_fifo_size;
2740 int txfifosz = priv->plat->tx_fifo_size;
2743 rxfifosz = priv->dma_cap.rx_fifo_size;
2745 txfifosz = priv->dma_cap.tx_fifo_size;
2747 /* Adjust for real per queue fifo size */
2748 rxfifosz /= rx_channels_count;
2749 txfifosz /= tx_channels_count;
2751 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan, rxfifosz, rxqmode);
2752 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan, txfifosz, txqmode);
2755 static bool stmmac_safety_feat_interrupt(struct stmmac_priv *priv)
2759 ret = stmmac_safety_feat_irq_status(priv, priv->dev,
2760 priv->ioaddr, priv->dma_cap.asp, &priv->sstats);
2761 if (ret && (ret != -EINVAL)) {
2762 stmmac_global_err(priv);
2769 static int stmmac_napi_check(struct stmmac_priv *priv, u32 chan, u32 dir)
2771 int status = stmmac_dma_interrupt_status(priv, priv->ioaddr,
2772 &priv->xstats, chan, dir);
2773 struct stmmac_rx_queue *rx_q = &priv->dma_conf.rx_queue[chan];
2774 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[chan];
2775 struct stmmac_channel *ch = &priv->channel[chan];
2776 struct napi_struct *rx_napi;
2777 struct napi_struct *tx_napi;
2778 unsigned long flags;
2780 rx_napi = rx_q->xsk_pool ? &ch->rxtx_napi : &ch->rx_napi;
2781 tx_napi = tx_q->xsk_pool ? &ch->rxtx_napi : &ch->tx_napi;
2783 if ((status & handle_rx) && (chan < priv->plat->rx_queues_to_use)) {
2784 if (napi_schedule_prep(rx_napi)) {
2785 spin_lock_irqsave(&ch->lock, flags);
2786 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
2787 spin_unlock_irqrestore(&ch->lock, flags);
2788 __napi_schedule(rx_napi);
2792 if ((status & handle_tx) && (chan < priv->plat->tx_queues_to_use)) {
2793 if (napi_schedule_prep(tx_napi)) {
2794 spin_lock_irqsave(&ch->lock, flags);
2795 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
2796 spin_unlock_irqrestore(&ch->lock, flags);
2797 __napi_schedule(tx_napi);
2805 * stmmac_dma_interrupt - DMA ISR
2806 * @priv: driver private structure
2807 * Description: this is the DMA ISR. It is called by the main ISR.
2808 * It calls the dwmac dma routine and schedule poll method in case of some
2811 static void stmmac_dma_interrupt(struct stmmac_priv *priv)
2813 u32 tx_channel_count = priv->plat->tx_queues_to_use;
2814 u32 rx_channel_count = priv->plat->rx_queues_to_use;
2815 u32 channels_to_check = tx_channel_count > rx_channel_count ?
2816 tx_channel_count : rx_channel_count;
2818 int status[max_t(u32, MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES)];
2820 /* Make sure we never check beyond our status buffer. */
2821 if (WARN_ON_ONCE(channels_to_check > ARRAY_SIZE(status)))
2822 channels_to_check = ARRAY_SIZE(status);
2824 for (chan = 0; chan < channels_to_check; chan++)
2825 status[chan] = stmmac_napi_check(priv, chan,
2828 for (chan = 0; chan < tx_channel_count; chan++) {
2829 if (unlikely(status[chan] & tx_hard_error_bump_tc)) {
2830 /* Try to bump up the dma threshold on this failure */
2831 stmmac_bump_dma_threshold(priv, chan);
2832 } else if (unlikely(status[chan] == tx_hard_error)) {
2833 stmmac_tx_err(priv, chan);
2839 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
2840 * @priv: driver private structure
2841 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
2843 static void stmmac_mmc_setup(struct stmmac_priv *priv)
2845 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
2846 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
2848 stmmac_mmc_intr_all_mask(priv, priv->mmcaddr);
2850 if (priv->dma_cap.rmon) {
2851 stmmac_mmc_ctrl(priv, priv->mmcaddr, mode);
2852 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
2854 netdev_info(priv->dev, "No MAC Management Counters available\n");
2858 * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
2859 * @priv: driver private structure
2861 * new GMAC chip generations have a new register to indicate the
2862 * presence of the optional feature/functions.
2863 * This can be also used to override the value passed through the
2864 * platform and necessary for old MAC10/100 and GMAC chips.
2866 static int stmmac_get_hw_features(struct stmmac_priv *priv)
2868 return stmmac_get_hw_feature(priv, priv->ioaddr, &priv->dma_cap) == 0;
2872 * stmmac_check_ether_addr - check if the MAC addr is valid
2873 * @priv: driver private structure
2875 * it is to verify if the MAC address is valid, in case of failures it
2876 * generates a random MAC address
2878 static void stmmac_check_ether_addr(struct stmmac_priv *priv)
2882 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
2883 stmmac_get_umac_addr(priv, priv->hw, addr, 0);
2884 if (is_valid_ether_addr(addr))
2885 eth_hw_addr_set(priv->dev, addr);
2887 eth_hw_addr_random(priv->dev);
2888 dev_info(priv->device, "device MAC address %pM\n",
2889 priv->dev->dev_addr);
2894 * stmmac_init_dma_engine - DMA init.
2895 * @priv: driver private structure
2897 * It inits the DMA invoking the specific MAC/GMAC callback.
2898 * Some DMA parameters can be passed from the platform;
2899 * in case of these are not passed a default is kept for the MAC or GMAC.
2901 static int stmmac_init_dma_engine(struct stmmac_priv *priv)
2903 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2904 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2905 u32 dma_csr_ch = max(rx_channels_count, tx_channels_count);
2906 struct stmmac_rx_queue *rx_q;
2907 struct stmmac_tx_queue *tx_q;
2912 if (!priv->plat->dma_cfg || !priv->plat->dma_cfg->pbl) {
2913 dev_err(priv->device, "Invalid DMA configuration\n");
2917 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
2920 ret = stmmac_reset(priv, priv->ioaddr);
2922 dev_err(priv->device, "Failed to reset the dma\n");
2926 /* DMA Configuration */
2927 stmmac_dma_init(priv, priv->ioaddr, priv->plat->dma_cfg, atds);
2929 if (priv->plat->axi)
2930 stmmac_axi(priv, priv->ioaddr, priv->plat->axi);
2932 /* DMA CSR Channel configuration */
2933 for (chan = 0; chan < dma_csr_ch; chan++) {
2934 stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan);
2935 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 1, 1);
2938 /* DMA RX Channel Configuration */
2939 for (chan = 0; chan < rx_channels_count; chan++) {
2940 rx_q = &priv->dma_conf.rx_queue[chan];
2942 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2943 rx_q->dma_rx_phy, chan);
2945 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
2946 (rx_q->buf_alloc_num *
2947 sizeof(struct dma_desc));
2948 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
2949 rx_q->rx_tail_addr, chan);
2952 /* DMA TX Channel Configuration */
2953 for (chan = 0; chan < tx_channels_count; chan++) {
2954 tx_q = &priv->dma_conf.tx_queue[chan];
2956 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2957 tx_q->dma_tx_phy, chan);
2959 tx_q->tx_tail_addr = tx_q->dma_tx_phy;
2960 stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
2961 tx_q->tx_tail_addr, chan);
2967 static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue)
2969 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[queue];
2971 hrtimer_start(&tx_q->txtimer,
2972 STMMAC_COAL_TIMER(priv->tx_coal_timer[queue]),
2977 * stmmac_tx_timer - mitigation sw timer for tx.
2980 * This is the timer handler to directly invoke the stmmac_tx_clean.
2982 static enum hrtimer_restart stmmac_tx_timer(struct hrtimer *t)
2984 struct stmmac_tx_queue *tx_q = container_of(t, struct stmmac_tx_queue, txtimer);
2985 struct stmmac_priv *priv = tx_q->priv_data;
2986 struct stmmac_channel *ch;
2987 struct napi_struct *napi;
2989 ch = &priv->channel[tx_q->queue_index];
2990 napi = tx_q->xsk_pool ? &ch->rxtx_napi : &ch->tx_napi;
2992 if (likely(napi_schedule_prep(napi))) {
2993 unsigned long flags;
2995 spin_lock_irqsave(&ch->lock, flags);
2996 stmmac_disable_dma_irq(priv, priv->ioaddr, ch->index, 0, 1);
2997 spin_unlock_irqrestore(&ch->lock, flags);
2998 __napi_schedule(napi);
3001 return HRTIMER_NORESTART;
3005 * stmmac_init_coalesce - init mitigation options.
3006 * @priv: driver private structure
3008 * This inits the coalesce parameters: i.e. timer rate,
3009 * timer handler and default threshold used for enabling the
3010 * interrupt on completion bit.
3012 static void stmmac_init_coalesce(struct stmmac_priv *priv)
3014 u32 tx_channel_count = priv->plat->tx_queues_to_use;
3015 u32 rx_channel_count = priv->plat->rx_queues_to_use;
3018 for (chan = 0; chan < tx_channel_count; chan++) {
3019 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[chan];
3021 priv->tx_coal_frames[chan] = STMMAC_TX_FRAMES;
3022 priv->tx_coal_timer[chan] = STMMAC_COAL_TX_TIMER;
3024 hrtimer_init(&tx_q->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3025 tx_q->txtimer.function = stmmac_tx_timer;
3028 for (chan = 0; chan < rx_channel_count; chan++)
3029 priv->rx_coal_frames[chan] = STMMAC_RX_FRAMES;
3032 static void stmmac_set_rings_length(struct stmmac_priv *priv)
3034 u32 rx_channels_count = priv->plat->rx_queues_to_use;
3035 u32 tx_channels_count = priv->plat->tx_queues_to_use;
3038 /* set TX ring length */
3039 for (chan = 0; chan < tx_channels_count; chan++)
3040 stmmac_set_tx_ring_len(priv, priv->ioaddr,
3041 (priv->dma_conf.dma_tx_size - 1), chan);
3043 /* set RX ring length */
3044 for (chan = 0; chan < rx_channels_count; chan++)
3045 stmmac_set_rx_ring_len(priv, priv->ioaddr,
3046 (priv->dma_conf.dma_rx_size - 1), chan);
3050 * stmmac_set_tx_queue_weight - Set TX queue weight
3051 * @priv: driver private structure
3052 * Description: It is used for setting TX queues weight
3054 static void stmmac_set_tx_queue_weight(struct stmmac_priv *priv)
3056 u32 tx_queues_count = priv->plat->tx_queues_to_use;
3060 for (queue = 0; queue < tx_queues_count; queue++) {
3061 weight = priv->plat->tx_queues_cfg[queue].weight;
3062 stmmac_set_mtl_tx_queue_weight(priv, priv->hw, weight, queue);
3067 * stmmac_configure_cbs - Configure CBS in TX queue
3068 * @priv: driver private structure
3069 * Description: It is used for configuring CBS in AVB TX queues
3071 static void stmmac_configure_cbs(struct stmmac_priv *priv)
3073 u32 tx_queues_count = priv->plat->tx_queues_to_use;
3077 /* queue 0 is reserved for legacy traffic */
3078 for (queue = 1; queue < tx_queues_count; queue++) {
3079 mode_to_use = priv->plat->tx_queues_cfg[queue].mode_to_use;
3080 if (mode_to_use == MTL_QUEUE_DCB)
3083 stmmac_config_cbs(priv, priv->hw,
3084 priv->plat->tx_queues_cfg[queue].send_slope,
3085 priv->plat->tx_queues_cfg[queue].idle_slope,
3086 priv->plat->tx_queues_cfg[queue].high_credit,
3087 priv->plat->tx_queues_cfg[queue].low_credit,
3093 * stmmac_rx_queue_dma_chan_map - Map RX queue to RX dma channel
3094 * @priv: driver private structure
3095 * Description: It is used for mapping RX queues to RX dma channels
3097 static void stmmac_rx_queue_dma_chan_map(struct stmmac_priv *priv)
3099 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3103 for (queue = 0; queue < rx_queues_count; queue++) {
3104 chan = priv->plat->rx_queues_cfg[queue].chan;
3105 stmmac_map_mtl_to_dma(priv, priv->hw, queue, chan);
3110 * stmmac_mac_config_rx_queues_prio - Configure RX Queue priority
3111 * @priv: driver private structure
3112 * Description: It is used for configuring the RX Queue Priority
3114 static void stmmac_mac_config_rx_queues_prio(struct stmmac_priv *priv)
3116 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3120 for (queue = 0; queue < rx_queues_count; queue++) {
3121 if (!priv->plat->rx_queues_cfg[queue].use_prio)
3124 prio = priv->plat->rx_queues_cfg[queue].prio;
3125 stmmac_rx_queue_prio(priv, priv->hw, prio, queue);
3130 * stmmac_mac_config_tx_queues_prio - Configure TX Queue priority
3131 * @priv: driver private structure
3132 * Description: It is used for configuring the TX Queue Priority
3134 static void stmmac_mac_config_tx_queues_prio(struct stmmac_priv *priv)
3136 u32 tx_queues_count = priv->plat->tx_queues_to_use;
3140 for (queue = 0; queue < tx_queues_count; queue++) {
3141 if (!priv->plat->tx_queues_cfg[queue].use_prio)
3144 prio = priv->plat->tx_queues_cfg[queue].prio;
3145 stmmac_tx_queue_prio(priv, priv->hw, prio, queue);
3150 * stmmac_mac_config_rx_queues_routing - Configure RX Queue Routing
3151 * @priv: driver private structure
3152 * Description: It is used for configuring the RX queue routing
3154 static void stmmac_mac_config_rx_queues_routing(struct stmmac_priv *priv)
3156 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3160 for (queue = 0; queue < rx_queues_count; queue++) {
3161 /* no specific packet type routing specified for the queue */
3162 if (priv->plat->rx_queues_cfg[queue].pkt_route == 0x0)
3165 packet = priv->plat->rx_queues_cfg[queue].pkt_route;
3166 stmmac_rx_queue_routing(priv, priv->hw, packet, queue);
3170 static void stmmac_mac_config_rss(struct stmmac_priv *priv)
3172 if (!priv->dma_cap.rssen || !priv->plat->rss_en) {
3173 priv->rss.enable = false;
3177 if (priv->dev->features & NETIF_F_RXHASH)
3178 priv->rss.enable = true;
3180 priv->rss.enable = false;
3182 stmmac_rss_configure(priv, priv->hw, &priv->rss,
3183 priv->plat->rx_queues_to_use);
3187 * stmmac_mtl_configuration - Configure MTL
3188 * @priv: driver private structure
3189 * Description: It is used for configurring MTL
3191 static void stmmac_mtl_configuration(struct stmmac_priv *priv)
3193 u32 rx_queues_count = priv->plat->rx_queues_to_use;
3194 u32 tx_queues_count = priv->plat->tx_queues_to_use;
3196 if (tx_queues_count > 1)
3197 stmmac_set_tx_queue_weight(priv);
3199 /* Configure MTL RX algorithms */
3200 if (rx_queues_count > 1)
3201 stmmac_prog_mtl_rx_algorithms(priv, priv->hw,
3202 priv->plat->rx_sched_algorithm);
3204 /* Configure MTL TX algorithms */
3205 if (tx_queues_count > 1)
3206 stmmac_prog_mtl_tx_algorithms(priv, priv->hw,
3207 priv->plat->tx_sched_algorithm);
3209 /* Configure CBS in AVB TX queues */
3210 if (tx_queues_count > 1)
3211 stmmac_configure_cbs(priv);
3213 /* Map RX MTL to DMA channels */
3214 stmmac_rx_queue_dma_chan_map(priv);
3216 /* Enable MAC RX Queues */
3217 stmmac_mac_enable_rx_queues(priv);
3219 /* Set RX priorities */
3220 if (rx_queues_count > 1)
3221 stmmac_mac_config_rx_queues_prio(priv);
3223 /* Set TX priorities */
3224 if (tx_queues_count > 1)
3225 stmmac_mac_config_tx_queues_prio(priv);
3227 /* Set RX routing */
3228 if (rx_queues_count > 1)
3229 stmmac_mac_config_rx_queues_routing(priv);
3231 /* Receive Side Scaling */
3232 if (rx_queues_count > 1)
3233 stmmac_mac_config_rss(priv);
3236 static void stmmac_safety_feat_configuration(struct stmmac_priv *priv)
3238 if (priv->dma_cap.asp) {
3239 netdev_info(priv->dev, "Enabling Safety Features\n");
3240 stmmac_safety_feat_config(priv, priv->ioaddr, priv->dma_cap.asp,
3241 priv->plat->safety_feat_cfg);
3243 netdev_info(priv->dev, "No Safety Features support found\n");
3247 static int stmmac_fpe_start_wq(struct stmmac_priv *priv)
3251 clear_bit(__FPE_TASK_SCHED, &priv->fpe_task_state);
3252 clear_bit(__FPE_REMOVING, &priv->fpe_task_state);
3254 name = priv->wq_name;
3255 sprintf(name, "%s-fpe", priv->dev->name);
3257 priv->fpe_wq = create_singlethread_workqueue(name);
3258 if (!priv->fpe_wq) {
3259 netdev_err(priv->dev, "%s: Failed to create workqueue\n", name);
3263 netdev_info(priv->dev, "FPE workqueue start");
3269 * stmmac_hw_setup - setup mac in a usable state.
3270 * @dev : pointer to the device structure.
3271 * @ptp_register: register PTP if set
3273 * this is the main function to setup the HW in a usable state because the
3274 * dma engine is reset, the core registers are configured (e.g. AXI,
3275 * Checksum features, timers). The DMA is ready to start receiving and
3278 * 0 on success and an appropriate (-)ve integer as defined in errno.h
3281 static int stmmac_hw_setup(struct net_device *dev, bool ptp_register)
3283 struct stmmac_priv *priv = netdev_priv(dev);
3284 u32 rx_cnt = priv->plat->rx_queues_to_use;
3285 u32 tx_cnt = priv->plat->tx_queues_to_use;
3290 /* DMA initialization and SW reset */
3291 ret = stmmac_init_dma_engine(priv);
3293 netdev_err(priv->dev, "%s: DMA engine initialization failed\n",
3298 /* Copy the MAC addr into the HW */
3299 stmmac_set_umac_addr(priv, priv->hw, dev->dev_addr, 0);
3301 /* PS and related bits will be programmed according to the speed */
3302 if (priv->hw->pcs) {
3303 int speed = priv->plat->mac_port_sel_speed;
3305 if ((speed == SPEED_10) || (speed == SPEED_100) ||
3306 (speed == SPEED_1000)) {
3307 priv->hw->ps = speed;
3309 dev_warn(priv->device, "invalid port speed\n");
3314 /* Initialize the MAC Core */
3315 stmmac_core_init(priv, priv->hw, dev);
3318 stmmac_mtl_configuration(priv);
3320 /* Initialize Safety Features */
3321 stmmac_safety_feat_configuration(priv);
3323 ret = stmmac_rx_ipc(priv, priv->hw);
3325 netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n");
3326 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
3327 priv->hw->rx_csum = 0;
3330 /* Enable the MAC Rx/Tx */
3331 stmmac_mac_set(priv, priv->ioaddr, true);
3333 /* Set the HW DMA mode and the COE */
3334 stmmac_dma_operation_mode(priv);
3336 stmmac_mmc_setup(priv);
3339 ret = clk_prepare_enable(priv->plat->clk_ptp_ref);
3341 netdev_warn(priv->dev,
3342 "failed to enable PTP reference clock: %pe\n",
3346 ret = stmmac_init_ptp(priv);
3347 if (ret == -EOPNOTSUPP)
3348 netdev_info(priv->dev, "PTP not supported by HW\n");
3350 netdev_warn(priv->dev, "PTP init failed\n");
3351 else if (ptp_register)
3352 stmmac_ptp_register(priv);
3354 priv->eee_tw_timer = STMMAC_DEFAULT_TWT_LS;
3356 /* Convert the timer from msec to usec */
3357 if (!priv->tx_lpi_timer)
3358 priv->tx_lpi_timer = eee_timer * 1000;
3360 if (priv->use_riwt) {
3363 for (queue = 0; queue < rx_cnt; queue++) {
3364 if (!priv->rx_riwt[queue])
3365 priv->rx_riwt[queue] = DEF_DMA_RIWT;
3367 stmmac_rx_watchdog(priv, priv->ioaddr,
3368 priv->rx_riwt[queue], queue);
3373 stmmac_pcs_ctrl_ane(priv, priv->ioaddr, 1, priv->hw->ps, 0);
3375 /* set TX and RX rings length */
3376 stmmac_set_rings_length(priv);
3380 for (chan = 0; chan < tx_cnt; chan++) {
3381 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[chan];
3383 /* TSO and TBS cannot co-exist */
3384 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3387 stmmac_enable_tso(priv, priv->ioaddr, 1, chan);
3391 /* Enable Split Header */
3392 sph_en = (priv->hw->rx_csum > 0) && priv->sph;
3393 for (chan = 0; chan < rx_cnt; chan++)
3394 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
3397 /* VLAN Tag Insertion */
3398 if (priv->dma_cap.vlins)
3399 stmmac_enable_vlan(priv, priv->hw, STMMAC_VLAN_INSERT);
3402 for (chan = 0; chan < tx_cnt; chan++) {
3403 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[chan];
3404 int enable = tx_q->tbs & STMMAC_TBS_AVAIL;
3406 stmmac_enable_tbs(priv, priv->ioaddr, enable, chan);
3409 /* Configure real RX and TX queues */
3410 netif_set_real_num_rx_queues(dev, priv->plat->rx_queues_to_use);
3411 netif_set_real_num_tx_queues(dev, priv->plat->tx_queues_to_use);
3413 /* Start the ball rolling... */
3414 stmmac_start_all_dma(priv);
3416 if (priv->dma_cap.fpesel) {
3417 stmmac_fpe_start_wq(priv);
3419 if (priv->plat->fpe_cfg->enable)
3420 stmmac_fpe_handshake(priv, true);
3426 static void stmmac_hw_teardown(struct net_device *dev)
3428 struct stmmac_priv *priv = netdev_priv(dev);
3430 clk_disable_unprepare(priv->plat->clk_ptp_ref);
3433 static void stmmac_free_irq(struct net_device *dev,
3434 enum request_irq_err irq_err, int irq_idx)
3436 struct stmmac_priv *priv = netdev_priv(dev);
3440 case REQ_IRQ_ERR_ALL:
3441 irq_idx = priv->plat->tx_queues_to_use;
3443 case REQ_IRQ_ERR_TX:
3444 for (j = irq_idx - 1; j >= 0; j--) {
3445 if (priv->tx_irq[j] > 0) {
3446 irq_set_affinity_hint(priv->tx_irq[j], NULL);
3447 free_irq(priv->tx_irq[j], &priv->dma_conf.tx_queue[j]);
3450 irq_idx = priv->plat->rx_queues_to_use;
3452 case REQ_IRQ_ERR_RX:
3453 for (j = irq_idx - 1; j >= 0; j--) {
3454 if (priv->rx_irq[j] > 0) {
3455 irq_set_affinity_hint(priv->rx_irq[j], NULL);
3456 free_irq(priv->rx_irq[j], &priv->dma_conf.rx_queue[j]);
3460 if (priv->sfty_ue_irq > 0 && priv->sfty_ue_irq != dev->irq)
3461 free_irq(priv->sfty_ue_irq, dev);
3463 case REQ_IRQ_ERR_SFTY_UE:
3464 if (priv->sfty_ce_irq > 0 && priv->sfty_ce_irq != dev->irq)
3465 free_irq(priv->sfty_ce_irq, dev);
3467 case REQ_IRQ_ERR_SFTY_CE:
3468 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq)
3469 free_irq(priv->lpi_irq, dev);
3471 case REQ_IRQ_ERR_LPI:
3472 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq)
3473 free_irq(priv->wol_irq, dev);
3475 case REQ_IRQ_ERR_WOL:
3476 free_irq(dev->irq, dev);
3478 case REQ_IRQ_ERR_MAC:
3479 case REQ_IRQ_ERR_NO:
3480 /* If MAC IRQ request error, no more IRQ to free */
3485 static int stmmac_request_irq_multi_msi(struct net_device *dev)
3487 struct stmmac_priv *priv = netdev_priv(dev);
3488 enum request_irq_err irq_err;
3495 /* For common interrupt */
3496 int_name = priv->int_name_mac;
3497 sprintf(int_name, "%s:%s", dev->name, "mac");
3498 ret = request_irq(dev->irq, stmmac_mac_interrupt,
3500 if (unlikely(ret < 0)) {
3501 netdev_err(priv->dev,
3502 "%s: alloc mac MSI %d (error: %d)\n",
3503 __func__, dev->irq, ret);
3504 irq_err = REQ_IRQ_ERR_MAC;
3508 /* Request the Wake IRQ in case of another line
3511 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq) {
3512 int_name = priv->int_name_wol;
3513 sprintf(int_name, "%s:%s", dev->name, "wol");
3514 ret = request_irq(priv->wol_irq,
3515 stmmac_mac_interrupt,
3517 if (unlikely(ret < 0)) {
3518 netdev_err(priv->dev,
3519 "%s: alloc wol MSI %d (error: %d)\n",
3520 __func__, priv->wol_irq, ret);
3521 irq_err = REQ_IRQ_ERR_WOL;
3526 /* Request the LPI IRQ in case of another line
3529 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq) {
3530 int_name = priv->int_name_lpi;
3531 sprintf(int_name, "%s:%s", dev->name, "lpi");
3532 ret = request_irq(priv->lpi_irq,
3533 stmmac_mac_interrupt,
3535 if (unlikely(ret < 0)) {
3536 netdev_err(priv->dev,
3537 "%s: alloc lpi MSI %d (error: %d)\n",
3538 __func__, priv->lpi_irq, ret);
3539 irq_err = REQ_IRQ_ERR_LPI;
3544 /* Request the Safety Feature Correctible Error line in
3545 * case of another line is used
3547 if (priv->sfty_ce_irq > 0 && priv->sfty_ce_irq != dev->irq) {
3548 int_name = priv->int_name_sfty_ce;
3549 sprintf(int_name, "%s:%s", dev->name, "safety-ce");
3550 ret = request_irq(priv->sfty_ce_irq,
3551 stmmac_safety_interrupt,
3553 if (unlikely(ret < 0)) {
3554 netdev_err(priv->dev,
3555 "%s: alloc sfty ce MSI %d (error: %d)\n",
3556 __func__, priv->sfty_ce_irq, ret);
3557 irq_err = REQ_IRQ_ERR_SFTY_CE;
3562 /* Request the Safety Feature Uncorrectible Error line in
3563 * case of another line is used
3565 if (priv->sfty_ue_irq > 0 && priv->sfty_ue_irq != dev->irq) {
3566 int_name = priv->int_name_sfty_ue;
3567 sprintf(int_name, "%s:%s", dev->name, "safety-ue");
3568 ret = request_irq(priv->sfty_ue_irq,
3569 stmmac_safety_interrupt,
3571 if (unlikely(ret < 0)) {
3572 netdev_err(priv->dev,
3573 "%s: alloc sfty ue MSI %d (error: %d)\n",
3574 __func__, priv->sfty_ue_irq, ret);
3575 irq_err = REQ_IRQ_ERR_SFTY_UE;
3580 /* Request Rx MSI irq */
3581 for (i = 0; i < priv->plat->rx_queues_to_use; i++) {
3582 if (i >= MTL_MAX_RX_QUEUES)
3584 if (priv->rx_irq[i] == 0)
3587 int_name = priv->int_name_rx_irq[i];
3588 sprintf(int_name, "%s:%s-%d", dev->name, "rx", i);
3589 ret = request_irq(priv->rx_irq[i],
3591 0, int_name, &priv->dma_conf.rx_queue[i]);
3592 if (unlikely(ret < 0)) {
3593 netdev_err(priv->dev,
3594 "%s: alloc rx-%d MSI %d (error: %d)\n",
3595 __func__, i, priv->rx_irq[i], ret);
3596 irq_err = REQ_IRQ_ERR_RX;
3600 cpumask_clear(&cpu_mask);
3601 cpumask_set_cpu(i % num_online_cpus(), &cpu_mask);
3602 irq_set_affinity_hint(priv->rx_irq[i], &cpu_mask);
3605 /* Request Tx MSI irq */
3606 for (i = 0; i < priv->plat->tx_queues_to_use; i++) {
3607 if (i >= MTL_MAX_TX_QUEUES)
3609 if (priv->tx_irq[i] == 0)
3612 int_name = priv->int_name_tx_irq[i];
3613 sprintf(int_name, "%s:%s-%d", dev->name, "tx", i);
3614 ret = request_irq(priv->tx_irq[i],
3616 0, int_name, &priv->dma_conf.tx_queue[i]);
3617 if (unlikely(ret < 0)) {
3618 netdev_err(priv->dev,
3619 "%s: alloc tx-%d MSI %d (error: %d)\n",
3620 __func__, i, priv->tx_irq[i], ret);
3621 irq_err = REQ_IRQ_ERR_TX;
3625 cpumask_clear(&cpu_mask);
3626 cpumask_set_cpu(i % num_online_cpus(), &cpu_mask);
3627 irq_set_affinity_hint(priv->tx_irq[i], &cpu_mask);
3633 stmmac_free_irq(dev, irq_err, irq_idx);
3637 static int stmmac_request_irq_single(struct net_device *dev)
3639 struct stmmac_priv *priv = netdev_priv(dev);
3640 enum request_irq_err irq_err;
3643 ret = request_irq(dev->irq, stmmac_interrupt,
3644 IRQF_SHARED, dev->name, dev);
3645 if (unlikely(ret < 0)) {
3646 netdev_err(priv->dev,
3647 "%s: ERROR: allocating the IRQ %d (error: %d)\n",
3648 __func__, dev->irq, ret);
3649 irq_err = REQ_IRQ_ERR_MAC;
3653 /* Request the Wake IRQ in case of another line
3656 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq) {
3657 ret = request_irq(priv->wol_irq, stmmac_interrupt,
3658 IRQF_SHARED, dev->name, dev);
3659 if (unlikely(ret < 0)) {
3660 netdev_err(priv->dev,
3661 "%s: ERROR: allocating the WoL IRQ %d (%d)\n",
3662 __func__, priv->wol_irq, ret);
3663 irq_err = REQ_IRQ_ERR_WOL;
3668 /* Request the IRQ lines */
3669 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq) {
3670 ret = request_irq(priv->lpi_irq, stmmac_interrupt,
3671 IRQF_SHARED, dev->name, dev);
3672 if (unlikely(ret < 0)) {
3673 netdev_err(priv->dev,
3674 "%s: ERROR: allocating the LPI IRQ %d (%d)\n",
3675 __func__, priv->lpi_irq, ret);
3676 irq_err = REQ_IRQ_ERR_LPI;
3684 stmmac_free_irq(dev, irq_err, 0);
3688 static int stmmac_request_irq(struct net_device *dev)
3690 struct stmmac_priv *priv = netdev_priv(dev);
3693 /* Request the IRQ lines */
3694 if (priv->plat->multi_msi_en)
3695 ret = stmmac_request_irq_multi_msi(dev);
3697 ret = stmmac_request_irq_single(dev);
3703 * stmmac_setup_dma_desc - Generate a dma_conf and allocate DMA queue
3704 * @priv: driver private structure
3705 * @mtu: MTU to setup the dma queue and buf with
3706 * Description: Allocate and generate a dma_conf based on the provided MTU.
3707 * Allocate the Tx/Rx DMA queue and init them.
3709 * the dma_conf allocated struct on success and an appropriate ERR_PTR on failure.
3711 static struct stmmac_dma_conf *
3712 stmmac_setup_dma_desc(struct stmmac_priv *priv, unsigned int mtu)
3714 struct stmmac_dma_conf *dma_conf;
3715 int chan, bfsize, ret;
3717 dma_conf = kzalloc(sizeof(*dma_conf), GFP_KERNEL);
3719 netdev_err(priv->dev, "%s: DMA conf allocation failed\n",
3721 return ERR_PTR(-ENOMEM);
3724 bfsize = stmmac_set_16kib_bfsize(priv, mtu);
3728 if (bfsize < BUF_SIZE_16KiB)
3729 bfsize = stmmac_set_bfsize(mtu, 0);
3731 dma_conf->dma_buf_sz = bfsize;
3732 /* Chose the tx/rx size from the already defined one in the
3733 * priv struct. (if defined)
3735 dma_conf->dma_tx_size = priv->dma_conf.dma_tx_size;
3736 dma_conf->dma_rx_size = priv->dma_conf.dma_rx_size;
3738 if (!dma_conf->dma_tx_size)
3739 dma_conf->dma_tx_size = DMA_DEFAULT_TX_SIZE;
3740 if (!dma_conf->dma_rx_size)
3741 dma_conf->dma_rx_size = DMA_DEFAULT_RX_SIZE;
3743 /* Earlier check for TBS */
3744 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++) {
3745 struct stmmac_tx_queue *tx_q = &dma_conf->tx_queue[chan];
3746 int tbs_en = priv->plat->tx_queues_cfg[chan].tbs_en;
3748 /* Setup per-TXQ tbs flag before TX descriptor alloc */
3749 tx_q->tbs |= tbs_en ? STMMAC_TBS_AVAIL : 0;
3752 ret = alloc_dma_desc_resources(priv, dma_conf);
3754 netdev_err(priv->dev, "%s: DMA descriptors allocation failed\n",
3759 ret = init_dma_desc_rings(priv->dev, dma_conf, GFP_KERNEL);
3761 netdev_err(priv->dev, "%s: DMA descriptors initialization failed\n",
3769 free_dma_desc_resources(priv, dma_conf);
3772 return ERR_PTR(ret);
3776 * __stmmac_open - open entry point of the driver
3777 * @dev : pointer to the device structure.
3778 * @dma_conf : structure to take the dma data
3780 * This function is the open entry point of the driver.
3782 * 0 on success and an appropriate (-)ve integer as defined in errno.h
3785 static int __stmmac_open(struct net_device *dev,
3786 struct stmmac_dma_conf *dma_conf)
3788 struct stmmac_priv *priv = netdev_priv(dev);
3789 int mode = priv->plat->phy_interface;
3793 ret = pm_runtime_resume_and_get(priv->device);
3797 if (priv->hw->pcs != STMMAC_PCS_TBI &&
3798 priv->hw->pcs != STMMAC_PCS_RTBI &&
3800 xpcs_get_an_mode(priv->hw->xpcs, mode) != DW_AN_C73)) {
3801 ret = stmmac_init_phy(dev);
3803 netdev_err(priv->dev,
3804 "%s: Cannot attach to PHY (error: %d)\n",
3806 goto init_phy_error;
3810 /* Extra statistics */
3811 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
3812 priv->xstats.threshold = tc;
3814 priv->rx_copybreak = STMMAC_RX_COPYBREAK;
3816 buf_sz = dma_conf->dma_buf_sz;
3817 memcpy(&priv->dma_conf, dma_conf, sizeof(*dma_conf));
3819 stmmac_reset_queues_param(priv);
3821 if (!priv->plat->serdes_up_after_phy_linkup && priv->plat->serdes_powerup) {
3822 ret = priv->plat->serdes_powerup(dev, priv->plat->bsp_priv);
3824 netdev_err(priv->dev, "%s: Serdes powerup failed\n",
3830 ret = stmmac_hw_setup(dev, true);
3832 netdev_err(priv->dev, "%s: Hw setup failed\n", __func__);
3836 stmmac_init_coalesce(priv);
3838 phylink_start(priv->phylink);
3839 /* We may have called phylink_speed_down before */
3840 phylink_speed_up(priv->phylink);
3842 ret = stmmac_request_irq(dev);
3846 stmmac_enable_all_queues(priv);
3847 netif_tx_start_all_queues(priv->dev);
3848 stmmac_enable_all_dma_irq(priv);
3853 phylink_stop(priv->phylink);
3855 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
3856 hrtimer_cancel(&priv->dma_conf.tx_queue[chan].txtimer);
3858 stmmac_hw_teardown(dev);
3860 free_dma_desc_resources(priv, &priv->dma_conf);
3861 phylink_disconnect_phy(priv->phylink);
3863 pm_runtime_put(priv->device);
3867 static int stmmac_open(struct net_device *dev)
3869 struct stmmac_priv *priv = netdev_priv(dev);
3870 struct stmmac_dma_conf *dma_conf;
3873 dma_conf = stmmac_setup_dma_desc(priv, dev->mtu);
3874 if (IS_ERR(dma_conf))
3875 return PTR_ERR(dma_conf);
3877 ret = __stmmac_open(dev, dma_conf);
3882 static void stmmac_fpe_stop_wq(struct stmmac_priv *priv)
3884 set_bit(__FPE_REMOVING, &priv->fpe_task_state);
3887 destroy_workqueue(priv->fpe_wq);
3889 netdev_info(priv->dev, "FPE workqueue stop");
3893 * stmmac_release - close entry point of the driver
3894 * @dev : device pointer.
3896 * This is the stop entry point of the driver.
3898 static int stmmac_release(struct net_device *dev)
3900 struct stmmac_priv *priv = netdev_priv(dev);
3903 if (device_may_wakeup(priv->device))
3904 phylink_speed_down(priv->phylink, false);
3905 /* Stop and disconnect the PHY */
3906 phylink_stop(priv->phylink);
3907 phylink_disconnect_phy(priv->phylink);
3909 stmmac_disable_all_queues(priv);
3911 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
3912 hrtimer_cancel(&priv->dma_conf.tx_queue[chan].txtimer);
3914 netif_tx_disable(dev);
3916 /* Free the IRQ lines */
3917 stmmac_free_irq(dev, REQ_IRQ_ERR_ALL, 0);
3919 if (priv->eee_enabled) {
3920 priv->tx_path_in_lpi_mode = false;
3921 del_timer_sync(&priv->eee_ctrl_timer);
3924 /* Stop TX/RX DMA and clear the descriptors */
3925 stmmac_stop_all_dma(priv);
3927 /* Release and free the Rx/Tx resources */
3928 free_dma_desc_resources(priv, &priv->dma_conf);
3930 /* Disable the MAC Rx/Tx */
3931 stmmac_mac_set(priv, priv->ioaddr, false);
3933 /* Powerdown Serdes if there is */
3934 if (priv->plat->serdes_powerdown)
3935 priv->plat->serdes_powerdown(dev, priv->plat->bsp_priv);
3937 netif_carrier_off(dev);
3939 stmmac_release_ptp(priv);
3941 pm_runtime_put(priv->device);
3943 if (priv->dma_cap.fpesel)
3944 stmmac_fpe_stop_wq(priv);
3949 static bool stmmac_vlan_insert(struct stmmac_priv *priv, struct sk_buff *skb,
3950 struct stmmac_tx_queue *tx_q)
3952 u16 tag = 0x0, inner_tag = 0x0;
3953 u32 inner_type = 0x0;
3956 if (!priv->dma_cap.vlins)
3958 if (!skb_vlan_tag_present(skb))
3960 if (skb->vlan_proto == htons(ETH_P_8021AD)) {
3961 inner_tag = skb_vlan_tag_get(skb);
3962 inner_type = STMMAC_VLAN_INSERT;
3965 tag = skb_vlan_tag_get(skb);
3967 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3968 p = &tx_q->dma_entx[tx_q->cur_tx].basic;
3970 p = &tx_q->dma_tx[tx_q->cur_tx];
3972 if (stmmac_set_desc_vlan_tag(priv, p, tag, inner_tag, inner_type))
3975 stmmac_set_tx_owner(priv, p);
3976 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_conf.dma_tx_size);
3981 * stmmac_tso_allocator - close entry point of the driver
3982 * @priv: driver private structure
3983 * @des: buffer start address
3984 * @total_len: total length to fill in descriptors
3985 * @last_segment: condition for the last descriptor
3986 * @queue: TX queue index
3988 * This function fills descriptor and request new descriptors according to
3989 * buffer length to fill
3991 static void stmmac_tso_allocator(struct stmmac_priv *priv, dma_addr_t des,
3992 int total_len, bool last_segment, u32 queue)
3994 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[queue];
3995 struct dma_desc *desc;
3999 tmp_len = total_len;
4001 while (tmp_len > 0) {
4002 dma_addr_t curr_addr;
4004 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx,
4005 priv->dma_conf.dma_tx_size);
4006 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
4008 if (tx_q->tbs & STMMAC_TBS_AVAIL)
4009 desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
4011 desc = &tx_q->dma_tx[tx_q->cur_tx];
4013 curr_addr = des + (total_len - tmp_len);
4014 if (priv->dma_cap.addr64 <= 32)
4015 desc->des0 = cpu_to_le32(curr_addr);
4017 stmmac_set_desc_addr(priv, desc, curr_addr);
4019 buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ?
4020 TSO_MAX_BUFF_SIZE : tmp_len;
4022 stmmac_prepare_tso_tx_desc(priv, desc, 0, buff_size,
4024 (last_segment) && (tmp_len <= TSO_MAX_BUFF_SIZE),
4027 tmp_len -= TSO_MAX_BUFF_SIZE;
4031 static void stmmac_flush_tx_descriptors(struct stmmac_priv *priv, int queue)
4033 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[queue];
4036 if (likely(priv->extend_desc))
4037 desc_size = sizeof(struct dma_extended_desc);
4038 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4039 desc_size = sizeof(struct dma_edesc);
4041 desc_size = sizeof(struct dma_desc);
4043 /* The own bit must be the latest setting done when prepare the
4044 * descriptor and then barrier is needed to make sure that
4045 * all is coherent before granting the DMA engine.
4049 tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * desc_size);
4050 stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue);
4054 * stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO)
4055 * @skb : the socket buffer
4056 * @dev : device pointer
4057 * Description: this is the transmit function that is called on TSO frames
4058 * (support available on GMAC4 and newer chips).
4059 * Diagram below show the ring programming in case of TSO frames:
4063 * | DES0 |---> buffer1 = L2/L3/L4 header
4064 * | DES1 |---> TCP Payload (can continue on next descr...)
4065 * | DES2 |---> buffer 1 and 2 len
4066 * | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0]
4072 * | DES0 | --| Split TCP Payload on Buffers 1 and 2
4074 * | DES2 | --> buffer 1 and 2 len
4078 * mss is fixed when enable tso, so w/o programming the TDES3 ctx field.
4080 static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev)
4082 struct dma_desc *desc, *first, *mss_desc = NULL;
4083 struct stmmac_priv *priv = netdev_priv(dev);
4084 int nfrags = skb_shinfo(skb)->nr_frags;
4085 u32 queue = skb_get_queue_mapping(skb);
4086 unsigned int first_entry, tx_packets;
4087 int tmp_pay_len = 0, first_tx;
4088 struct stmmac_tx_queue *tx_q;
4089 bool has_vlan, set_ic;
4090 u8 proto_hdr_len, hdr;
4095 tx_q = &priv->dma_conf.tx_queue[queue];
4096 first_tx = tx_q->cur_tx;
4098 /* Compute header lengths */
4099 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
4100 proto_hdr_len = skb_transport_offset(skb) + sizeof(struct udphdr);
4101 hdr = sizeof(struct udphdr);
4103 proto_hdr_len = skb_tcp_all_headers(skb);
4104 hdr = tcp_hdrlen(skb);
4107 /* Desc availability based on threshold should be enough safe */
4108 if (unlikely(stmmac_tx_avail(priv, queue) <
4109 (((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) {
4110 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
4111 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
4113 /* This is a hard error, log it. */
4114 netdev_err(priv->dev,
4115 "%s: Tx Ring full when queue awake\n",
4118 return NETDEV_TX_BUSY;
4121 pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */
4123 mss = skb_shinfo(skb)->gso_size;
4125 /* set new MSS value if needed */
4126 if (mss != tx_q->mss) {
4127 if (tx_q->tbs & STMMAC_TBS_AVAIL)
4128 mss_desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
4130 mss_desc = &tx_q->dma_tx[tx_q->cur_tx];
4132 stmmac_set_mss(priv, mss_desc, mss);
4134 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx,
4135 priv->dma_conf.dma_tx_size);
4136 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
4139 if (netif_msg_tx_queued(priv)) {
4140 pr_info("%s: hdrlen %d, hdr_len %d, pay_len %d, mss %d\n",
4141 __func__, hdr, proto_hdr_len, pay_len, mss);
4142 pr_info("\tskb->len %d, skb->data_len %d\n", skb->len,
4146 /* Check if VLAN can be inserted by HW */
4147 has_vlan = stmmac_vlan_insert(priv, skb, tx_q);
4149 first_entry = tx_q->cur_tx;
4150 WARN_ON(tx_q->tx_skbuff[first_entry]);
4152 if (tx_q->tbs & STMMAC_TBS_AVAIL)
4153 desc = &tx_q->dma_entx[first_entry].basic;
4155 desc = &tx_q->dma_tx[first_entry];
4159 stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT);
4161 /* first descriptor: fill Headers on Buf1 */
4162 des = dma_map_single(priv->device, skb->data, skb_headlen(skb),
4164 if (dma_mapping_error(priv->device, des))
4167 tx_q->tx_skbuff_dma[first_entry].buf = des;
4168 tx_q->tx_skbuff_dma[first_entry].len = skb_headlen(skb);
4169 tx_q->tx_skbuff_dma[first_entry].map_as_page = false;
4170 tx_q->tx_skbuff_dma[first_entry].buf_type = STMMAC_TXBUF_T_SKB;
4172 if (priv->dma_cap.addr64 <= 32) {
4173 first->des0 = cpu_to_le32(des);
4175 /* Fill start of payload in buff2 of first descriptor */
4177 first->des1 = cpu_to_le32(des + proto_hdr_len);
4179 /* If needed take extra descriptors to fill the remaining payload */
4180 tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE;
4182 stmmac_set_desc_addr(priv, first, des);
4183 tmp_pay_len = pay_len;
4184 des += proto_hdr_len;
4188 stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0), queue);
4190 /* Prepare fragments */
4191 for (i = 0; i < nfrags; i++) {
4192 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4194 des = skb_frag_dma_map(priv->device, frag, 0,
4195 skb_frag_size(frag),
4197 if (dma_mapping_error(priv->device, des))
4200 stmmac_tso_allocator(priv, des, skb_frag_size(frag),
4201 (i == nfrags - 1), queue);
4203 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf = des;
4204 tx_q->tx_skbuff_dma[tx_q->cur_tx].len = skb_frag_size(frag);
4205 tx_q->tx_skbuff_dma[tx_q->cur_tx].map_as_page = true;
4206 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf_type = STMMAC_TXBUF_T_SKB;
4209 tx_q->tx_skbuff_dma[tx_q->cur_tx].last_segment = true;
4211 /* Only the last descriptor gets to point to the skb. */
4212 tx_q->tx_skbuff[tx_q->cur_tx] = skb;
4213 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf_type = STMMAC_TXBUF_T_SKB;
4215 /* Manage tx mitigation */
4216 tx_packets = (tx_q->cur_tx + 1) - first_tx;
4217 tx_q->tx_count_frames += tx_packets;
4219 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en)
4221 else if (!priv->tx_coal_frames[queue])
4223 else if (tx_packets > priv->tx_coal_frames[queue])
4225 else if ((tx_q->tx_count_frames %
4226 priv->tx_coal_frames[queue]) < tx_packets)
4232 if (tx_q->tbs & STMMAC_TBS_AVAIL)
4233 desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
4235 desc = &tx_q->dma_tx[tx_q->cur_tx];
4237 tx_q->tx_count_frames = 0;
4238 stmmac_set_tx_ic(priv, desc);
4239 priv->xstats.tx_set_ic_bit++;
4242 /* We've used all descriptors we need for this skb, however,
4243 * advance cur_tx so that it references a fresh descriptor.
4244 * ndo_start_xmit will fill this descriptor the next time it's
4245 * called and stmmac_tx_clean may clean up to this descriptor.
4247 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_conf.dma_tx_size);
4249 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
4250 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
4252 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
4255 dev->stats.tx_bytes += skb->len;
4256 priv->xstats.tx_tso_frames++;
4257 priv->xstats.tx_tso_nfrags += nfrags;
4259 if (priv->sarc_type)
4260 stmmac_set_desc_sarc(priv, first, priv->sarc_type);
4262 skb_tx_timestamp(skb);
4264 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4265 priv->hwts_tx_en)) {
4266 /* declare that device is doing timestamping */
4267 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4268 stmmac_enable_tx_timestamp(priv, first);
4271 /* Complete the first descriptor before granting the DMA */
4272 stmmac_prepare_tso_tx_desc(priv, first, 1,
4275 1, tx_q->tx_skbuff_dma[first_entry].last_segment,
4276 hdr / 4, (skb->len - proto_hdr_len));
4278 /* If context desc is used to change MSS */
4280 /* Make sure that first descriptor has been completely
4281 * written, including its own bit. This is because MSS is
4282 * actually before first descriptor, so we need to make
4283 * sure that MSS's own bit is the last thing written.
4286 stmmac_set_tx_owner(priv, mss_desc);
4289 if (netif_msg_pktdata(priv)) {
4290 pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n",
4291 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
4292 tx_q->cur_tx, first, nfrags);
4293 pr_info(">>> frame to be transmitted: ");
4294 print_pkt(skb->data, skb_headlen(skb));
4297 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
4299 stmmac_flush_tx_descriptors(priv, queue);
4300 stmmac_tx_timer_arm(priv, queue);
4302 return NETDEV_TX_OK;
4305 dev_err(priv->device, "Tx dma map failed\n");
4307 priv->dev->stats.tx_dropped++;
4308 return NETDEV_TX_OK;
4312 * stmmac_xmit - Tx entry point of the driver
4313 * @skb : the socket buffer
4314 * @dev : device pointer
4315 * Description : this is the tx entry point of the driver.
4316 * It programs the chain or the ring and supports oversized frames
4319 static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
4321 unsigned int first_entry, tx_packets, enh_desc;
4322 struct stmmac_priv *priv = netdev_priv(dev);
4323 unsigned int nopaged_len = skb_headlen(skb);
4324 int i, csum_insertion = 0, is_jumbo = 0;
4325 u32 queue = skb_get_queue_mapping(skb);
4326 int nfrags = skb_shinfo(skb)->nr_frags;
4327 int gso = skb_shinfo(skb)->gso_type;
4328 struct dma_edesc *tbs_desc = NULL;
4329 struct dma_desc *desc, *first;
4330 struct stmmac_tx_queue *tx_q;
4331 bool has_vlan, set_ic;
4332 int entry, first_tx;
4335 tx_q = &priv->dma_conf.tx_queue[queue];
4336 first_tx = tx_q->cur_tx;
4338 if (priv->tx_path_in_lpi_mode && priv->eee_sw_timer_en)
4339 stmmac_disable_eee_mode(priv);
4341 /* Manage oversized TCP frames for GMAC4 device */
4342 if (skb_is_gso(skb) && priv->tso) {
4343 if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
4344 return stmmac_tso_xmit(skb, dev);
4345 if (priv->plat->has_gmac4 && (gso & SKB_GSO_UDP_L4))
4346 return stmmac_tso_xmit(skb, dev);
4349 if (unlikely(stmmac_tx_avail(priv, queue) < nfrags + 1)) {
4350 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
4351 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
4353 /* This is a hard error, log it. */
4354 netdev_err(priv->dev,
4355 "%s: Tx Ring full when queue awake\n",
4358 return NETDEV_TX_BUSY;
4361 /* Check if VLAN can be inserted by HW */
4362 has_vlan = stmmac_vlan_insert(priv, skb, tx_q);
4364 entry = tx_q->cur_tx;
4365 first_entry = entry;
4366 WARN_ON(tx_q->tx_skbuff[first_entry]);
4368 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
4370 if (likely(priv->extend_desc))
4371 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
4372 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4373 desc = &tx_q->dma_entx[entry].basic;
4375 desc = tx_q->dma_tx + entry;
4380 stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT);
4382 enh_desc = priv->plat->enh_desc;
4383 /* To program the descriptors according to the size of the frame */
4385 is_jumbo = stmmac_is_jumbo_frm(priv, skb->len, enh_desc);
4387 if (unlikely(is_jumbo)) {
4388 entry = stmmac_jumbo_frm(priv, tx_q, skb, csum_insertion);
4389 if (unlikely(entry < 0) && (entry != -EINVAL))
4393 for (i = 0; i < nfrags; i++) {
4394 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4395 int len = skb_frag_size(frag);
4396 bool last_segment = (i == (nfrags - 1));
4398 entry = STMMAC_GET_ENTRY(entry, priv->dma_conf.dma_tx_size);
4399 WARN_ON(tx_q->tx_skbuff[entry]);
4401 if (likely(priv->extend_desc))
4402 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
4403 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4404 desc = &tx_q->dma_entx[entry].basic;
4406 desc = tx_q->dma_tx + entry;
4408 des = skb_frag_dma_map(priv->device, frag, 0, len,
4410 if (dma_mapping_error(priv->device, des))
4411 goto dma_map_err; /* should reuse desc w/o issues */
4413 tx_q->tx_skbuff_dma[entry].buf = des;
4415 stmmac_set_desc_addr(priv, desc, des);
4417 tx_q->tx_skbuff_dma[entry].map_as_page = true;
4418 tx_q->tx_skbuff_dma[entry].len = len;
4419 tx_q->tx_skbuff_dma[entry].last_segment = last_segment;
4420 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_SKB;
4422 /* Prepare the descriptor and set the own bit too */
4423 stmmac_prepare_tx_desc(priv, desc, 0, len, csum_insertion,
4424 priv->mode, 1, last_segment, skb->len);
4427 /* Only the last descriptor gets to point to the skb. */
4428 tx_q->tx_skbuff[entry] = skb;
4429 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_SKB;
4431 /* According to the coalesce parameter the IC bit for the latest
4432 * segment is reset and the timer re-started to clean the tx status.
4433 * This approach takes care about the fragments: desc is the first
4434 * element in case of no SG.
4436 tx_packets = (entry + 1) - first_tx;
4437 tx_q->tx_count_frames += tx_packets;
4439 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en)
4441 else if (!priv->tx_coal_frames[queue])
4443 else if (tx_packets > priv->tx_coal_frames[queue])
4445 else if ((tx_q->tx_count_frames %
4446 priv->tx_coal_frames[queue]) < tx_packets)
4452 if (likely(priv->extend_desc))
4453 desc = &tx_q->dma_etx[entry].basic;
4454 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4455 desc = &tx_q->dma_entx[entry].basic;
4457 desc = &tx_q->dma_tx[entry];
4459 tx_q->tx_count_frames = 0;
4460 stmmac_set_tx_ic(priv, desc);
4461 priv->xstats.tx_set_ic_bit++;
4464 /* We've used all descriptors we need for this skb, however,
4465 * advance cur_tx so that it references a fresh descriptor.
4466 * ndo_start_xmit will fill this descriptor the next time it's
4467 * called and stmmac_tx_clean may clean up to this descriptor.
4469 entry = STMMAC_GET_ENTRY(entry, priv->dma_conf.dma_tx_size);
4470 tx_q->cur_tx = entry;
4472 if (netif_msg_pktdata(priv)) {
4473 netdev_dbg(priv->dev,
4474 "%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
4475 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
4476 entry, first, nfrags);
4478 netdev_dbg(priv->dev, ">>> frame to be transmitted: ");
4479 print_pkt(skb->data, skb->len);
4482 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
4483 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
4485 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
4488 dev->stats.tx_bytes += skb->len;
4490 if (priv->sarc_type)
4491 stmmac_set_desc_sarc(priv, first, priv->sarc_type);
4493 skb_tx_timestamp(skb);
4495 /* Ready to fill the first descriptor and set the OWN bit w/o any
4496 * problems because all the descriptors are actually ready to be
4497 * passed to the DMA engine.
4499 if (likely(!is_jumbo)) {
4500 bool last_segment = (nfrags == 0);
4502 des = dma_map_single(priv->device, skb->data,
4503 nopaged_len, DMA_TO_DEVICE);
4504 if (dma_mapping_error(priv->device, des))
4507 tx_q->tx_skbuff_dma[first_entry].buf = des;
4508 tx_q->tx_skbuff_dma[first_entry].buf_type = STMMAC_TXBUF_T_SKB;
4509 tx_q->tx_skbuff_dma[first_entry].map_as_page = false;
4511 stmmac_set_desc_addr(priv, first, des);
4513 tx_q->tx_skbuff_dma[first_entry].len = nopaged_len;
4514 tx_q->tx_skbuff_dma[first_entry].last_segment = last_segment;
4516 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4517 priv->hwts_tx_en)) {
4518 /* declare that device is doing timestamping */
4519 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4520 stmmac_enable_tx_timestamp(priv, first);
4523 /* Prepare the first descriptor setting the OWN bit too */
4524 stmmac_prepare_tx_desc(priv, first, 1, nopaged_len,
4525 csum_insertion, priv->mode, 0, last_segment,
4529 if (tx_q->tbs & STMMAC_TBS_EN) {
4530 struct timespec64 ts = ns_to_timespec64(skb->tstamp);
4532 tbs_desc = &tx_q->dma_entx[first_entry];
4533 stmmac_set_desc_tbs(priv, tbs_desc, ts.tv_sec, ts.tv_nsec);
4536 stmmac_set_tx_owner(priv, first);
4538 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
4540 stmmac_enable_dma_transmission(priv, priv->ioaddr);
4542 stmmac_flush_tx_descriptors(priv, queue);
4543 stmmac_tx_timer_arm(priv, queue);
4545 return NETDEV_TX_OK;
4548 netdev_err(priv->dev, "Tx DMA map failed\n");
4550 priv->dev->stats.tx_dropped++;
4551 return NETDEV_TX_OK;
4554 static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
4556 struct vlan_ethhdr *veth;
4560 veth = (struct vlan_ethhdr *)skb->data;
4561 vlan_proto = veth->h_vlan_proto;
4563 if ((vlan_proto == htons(ETH_P_8021Q) &&
4564 dev->features & NETIF_F_HW_VLAN_CTAG_RX) ||
4565 (vlan_proto == htons(ETH_P_8021AD) &&
4566 dev->features & NETIF_F_HW_VLAN_STAG_RX)) {
4567 /* pop the vlan tag */
4568 vlanid = ntohs(veth->h_vlan_TCI);
4569 memmove(skb->data + VLAN_HLEN, veth, ETH_ALEN * 2);
4570 skb_pull(skb, VLAN_HLEN);
4571 __vlan_hwaccel_put_tag(skb, vlan_proto, vlanid);
4576 * stmmac_rx_refill - refill used skb preallocated buffers
4577 * @priv: driver private structure
4578 * @queue: RX queue index
4579 * Description : this is to reallocate the skb for the reception process
4580 * that is based on zero-copy.
4582 static inline void stmmac_rx_refill(struct stmmac_priv *priv, u32 queue)
4584 struct stmmac_rx_queue *rx_q = &priv->dma_conf.rx_queue[queue];
4585 int dirty = stmmac_rx_dirty(priv, queue);
4586 unsigned int entry = rx_q->dirty_rx;
4587 gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
4589 if (priv->dma_cap.addr64 <= 32)
4592 while (dirty-- > 0) {
4593 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry];
4597 if (priv->extend_desc)
4598 p = (struct dma_desc *)(rx_q->dma_erx + entry);
4600 p = rx_q->dma_rx + entry;
4603 buf->page = page_pool_alloc_pages(rx_q->page_pool, gfp);
4608 if (priv->sph && !buf->sec_page) {
4609 buf->sec_page = page_pool_alloc_pages(rx_q->page_pool, gfp);
4613 buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
4616 buf->addr = page_pool_get_dma_addr(buf->page) + buf->page_offset;
4618 stmmac_set_desc_addr(priv, p, buf->addr);
4620 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true);
4622 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false);
4623 stmmac_refill_desc3(priv, rx_q, p);
4625 rx_q->rx_count_frames++;
4626 rx_q->rx_count_frames += priv->rx_coal_frames[queue];
4627 if (rx_q->rx_count_frames > priv->rx_coal_frames[queue])
4628 rx_q->rx_count_frames = 0;
4630 use_rx_wd = !priv->rx_coal_frames[queue];
4631 use_rx_wd |= rx_q->rx_count_frames > 0;
4632 if (!priv->use_riwt)
4636 stmmac_set_rx_owner(priv, p, use_rx_wd);
4638 entry = STMMAC_GET_ENTRY(entry, priv->dma_conf.dma_rx_size);
4640 rx_q->dirty_rx = entry;
4641 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
4642 (rx_q->dirty_rx * sizeof(struct dma_desc));
4643 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue);
4646 static unsigned int stmmac_rx_buf1_len(struct stmmac_priv *priv,
4648 int status, unsigned int len)
4650 unsigned int plen = 0, hlen = 0;
4651 int coe = priv->hw->rx_csum;
4653 /* Not first descriptor, buffer is always zero */
4654 if (priv->sph && len)
4657 /* First descriptor, get split header length */
4658 stmmac_get_rx_header_len(priv, p, &hlen);
4659 if (priv->sph && hlen) {
4660 priv->xstats.rx_split_hdr_pkt_n++;
4664 /* First descriptor, not last descriptor and not split header */
4665 if (status & rx_not_ls)
4666 return priv->dma_conf.dma_buf_sz;
4668 plen = stmmac_get_rx_frame_len(priv, p, coe);
4670 /* First descriptor and last descriptor and not split header */
4671 return min_t(unsigned int, priv->dma_conf.dma_buf_sz, plen);
4674 static unsigned int stmmac_rx_buf2_len(struct stmmac_priv *priv,
4676 int status, unsigned int len)
4678 int coe = priv->hw->rx_csum;
4679 unsigned int plen = 0;
4681 /* Not split header, buffer is not available */
4685 /* Not last descriptor */
4686 if (status & rx_not_ls)
4687 return priv->dma_conf.dma_buf_sz;
4689 plen = stmmac_get_rx_frame_len(priv, p, coe);
4691 /* Last descriptor */
4695 static int stmmac_xdp_xmit_xdpf(struct stmmac_priv *priv, int queue,
4696 struct xdp_frame *xdpf, bool dma_map)
4698 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[queue];
4699 unsigned int entry = tx_q->cur_tx;
4700 struct dma_desc *tx_desc;
4701 dma_addr_t dma_addr;
4704 if (stmmac_tx_avail(priv, queue) < STMMAC_TX_THRESH(priv))
4705 return STMMAC_XDP_CONSUMED;
4707 if (likely(priv->extend_desc))
4708 tx_desc = (struct dma_desc *)(tx_q->dma_etx + entry);
4709 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4710 tx_desc = &tx_q->dma_entx[entry].basic;
4712 tx_desc = tx_q->dma_tx + entry;
4715 dma_addr = dma_map_single(priv->device, xdpf->data,
4716 xdpf->len, DMA_TO_DEVICE);
4717 if (dma_mapping_error(priv->device, dma_addr))
4718 return STMMAC_XDP_CONSUMED;
4720 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XDP_NDO;
4722 struct page *page = virt_to_page(xdpf->data);
4724 dma_addr = page_pool_get_dma_addr(page) + sizeof(*xdpf) +
4726 dma_sync_single_for_device(priv->device, dma_addr,
4727 xdpf->len, DMA_BIDIRECTIONAL);
4729 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XDP_TX;
4732 tx_q->tx_skbuff_dma[entry].buf = dma_addr;
4733 tx_q->tx_skbuff_dma[entry].map_as_page = false;
4734 tx_q->tx_skbuff_dma[entry].len = xdpf->len;
4735 tx_q->tx_skbuff_dma[entry].last_segment = true;
4736 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
4738 tx_q->xdpf[entry] = xdpf;
4740 stmmac_set_desc_addr(priv, tx_desc, dma_addr);
4742 stmmac_prepare_tx_desc(priv, tx_desc, 1, xdpf->len,
4743 true, priv->mode, true, true,
4746 tx_q->tx_count_frames++;
4748 if (tx_q->tx_count_frames % priv->tx_coal_frames[queue] == 0)
4754 tx_q->tx_count_frames = 0;
4755 stmmac_set_tx_ic(priv, tx_desc);
4756 priv->xstats.tx_set_ic_bit++;
4759 stmmac_enable_dma_transmission(priv, priv->ioaddr);
4761 entry = STMMAC_GET_ENTRY(entry, priv->dma_conf.dma_tx_size);
4762 tx_q->cur_tx = entry;
4764 return STMMAC_XDP_TX;
4767 static int stmmac_xdp_get_tx_queue(struct stmmac_priv *priv,
4772 if (unlikely(index < 0))
4775 while (index >= priv->plat->tx_queues_to_use)
4776 index -= priv->plat->tx_queues_to_use;
4781 static int stmmac_xdp_xmit_back(struct stmmac_priv *priv,
4782 struct xdp_buff *xdp)
4784 struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp);
4785 int cpu = smp_processor_id();
4786 struct netdev_queue *nq;
4790 if (unlikely(!xdpf))
4791 return STMMAC_XDP_CONSUMED;
4793 queue = stmmac_xdp_get_tx_queue(priv, cpu);
4794 nq = netdev_get_tx_queue(priv->dev, queue);
4796 __netif_tx_lock(nq, cpu);
4797 /* Avoids TX time-out as we are sharing with slow path */
4798 txq_trans_cond_update(nq);
4800 res = stmmac_xdp_xmit_xdpf(priv, queue, xdpf, false);
4801 if (res == STMMAC_XDP_TX)
4802 stmmac_flush_tx_descriptors(priv, queue);
4804 __netif_tx_unlock(nq);
4809 static int __stmmac_xdp_run_prog(struct stmmac_priv *priv,
4810 struct bpf_prog *prog,
4811 struct xdp_buff *xdp)
4816 act = bpf_prog_run_xdp(prog, xdp);
4819 res = STMMAC_XDP_PASS;
4822 res = stmmac_xdp_xmit_back(priv, xdp);
4825 if (xdp_do_redirect(priv->dev, xdp, prog) < 0)
4826 res = STMMAC_XDP_CONSUMED;
4828 res = STMMAC_XDP_REDIRECT;
4831 bpf_warn_invalid_xdp_action(priv->dev, prog, act);
4834 trace_xdp_exception(priv->dev, prog, act);
4837 res = STMMAC_XDP_CONSUMED;
4844 static struct sk_buff *stmmac_xdp_run_prog(struct stmmac_priv *priv,
4845 struct xdp_buff *xdp)
4847 struct bpf_prog *prog;
4850 prog = READ_ONCE(priv->xdp_prog);
4852 res = STMMAC_XDP_PASS;
4856 res = __stmmac_xdp_run_prog(priv, prog, xdp);
4858 return ERR_PTR(-res);
4861 static void stmmac_finalize_xdp_rx(struct stmmac_priv *priv,
4864 int cpu = smp_processor_id();
4867 queue = stmmac_xdp_get_tx_queue(priv, cpu);
4869 if (xdp_status & STMMAC_XDP_TX)
4870 stmmac_tx_timer_arm(priv, queue);
4872 if (xdp_status & STMMAC_XDP_REDIRECT)
4876 static struct sk_buff *stmmac_construct_skb_zc(struct stmmac_channel *ch,
4877 struct xdp_buff *xdp)
4879 unsigned int metasize = xdp->data - xdp->data_meta;
4880 unsigned int datasize = xdp->data_end - xdp->data;
4881 struct sk_buff *skb;
4883 skb = __napi_alloc_skb(&ch->rxtx_napi,
4884 xdp->data_end - xdp->data_hard_start,
4885 GFP_ATOMIC | __GFP_NOWARN);
4889 skb_reserve(skb, xdp->data - xdp->data_hard_start);
4890 memcpy(__skb_put(skb, datasize), xdp->data, datasize);
4892 skb_metadata_set(skb, metasize);
4897 static void stmmac_dispatch_skb_zc(struct stmmac_priv *priv, u32 queue,
4898 struct dma_desc *p, struct dma_desc *np,
4899 struct xdp_buff *xdp)
4901 struct stmmac_channel *ch = &priv->channel[queue];
4902 unsigned int len = xdp->data_end - xdp->data;
4903 enum pkt_hash_types hash_type;
4904 int coe = priv->hw->rx_csum;
4905 struct sk_buff *skb;
4908 skb = stmmac_construct_skb_zc(ch, xdp);
4910 priv->dev->stats.rx_dropped++;
4914 stmmac_get_rx_hwtstamp(priv, p, np, skb);
4915 stmmac_rx_vlan(priv->dev, skb);
4916 skb->protocol = eth_type_trans(skb, priv->dev);
4919 skb_checksum_none_assert(skb);
4921 skb->ip_summed = CHECKSUM_UNNECESSARY;
4923 if (!stmmac_get_rx_hash(priv, p, &hash, &hash_type))
4924 skb_set_hash(skb, hash, hash_type);
4926 skb_record_rx_queue(skb, queue);
4927 napi_gro_receive(&ch->rxtx_napi, skb);
4929 priv->dev->stats.rx_packets++;
4930 priv->dev->stats.rx_bytes += len;
4933 static bool stmmac_rx_refill_zc(struct stmmac_priv *priv, u32 queue, u32 budget)
4935 struct stmmac_rx_queue *rx_q = &priv->dma_conf.rx_queue[queue];
4936 unsigned int entry = rx_q->dirty_rx;
4937 struct dma_desc *rx_desc = NULL;
4940 budget = min(budget, stmmac_rx_dirty(priv, queue));
4942 while (budget-- > 0 && entry != rx_q->cur_rx) {
4943 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry];
4944 dma_addr_t dma_addr;
4948 buf->xdp = xsk_buff_alloc(rx_q->xsk_pool);
4955 if (priv->extend_desc)
4956 rx_desc = (struct dma_desc *)(rx_q->dma_erx + entry);
4958 rx_desc = rx_q->dma_rx + entry;
4960 dma_addr = xsk_buff_xdp_get_dma(buf->xdp);
4961 stmmac_set_desc_addr(priv, rx_desc, dma_addr);
4962 stmmac_set_desc_sec_addr(priv, rx_desc, 0, false);
4963 stmmac_refill_desc3(priv, rx_q, rx_desc);
4965 rx_q->rx_count_frames++;
4966 rx_q->rx_count_frames += priv->rx_coal_frames[queue];
4967 if (rx_q->rx_count_frames > priv->rx_coal_frames[queue])
4968 rx_q->rx_count_frames = 0;
4970 use_rx_wd = !priv->rx_coal_frames[queue];
4971 use_rx_wd |= rx_q->rx_count_frames > 0;
4972 if (!priv->use_riwt)
4976 stmmac_set_rx_owner(priv, rx_desc, use_rx_wd);
4978 entry = STMMAC_GET_ENTRY(entry, priv->dma_conf.dma_rx_size);
4982 rx_q->dirty_rx = entry;
4983 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
4984 (rx_q->dirty_rx * sizeof(struct dma_desc));
4985 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue);
4991 static int stmmac_rx_zc(struct stmmac_priv *priv, int limit, u32 queue)
4993 struct stmmac_rx_queue *rx_q = &priv->dma_conf.rx_queue[queue];
4994 unsigned int count = 0, error = 0, len = 0;
4995 int dirty = stmmac_rx_dirty(priv, queue);
4996 unsigned int next_entry = rx_q->cur_rx;
4997 unsigned int desc_size;
4998 struct bpf_prog *prog;
4999 bool failure = false;
5003 if (netif_msg_rx_status(priv)) {
5006 netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
5007 if (priv->extend_desc) {
5008 rx_head = (void *)rx_q->dma_erx;
5009 desc_size = sizeof(struct dma_extended_desc);
5011 rx_head = (void *)rx_q->dma_rx;
5012 desc_size = sizeof(struct dma_desc);
5015 stmmac_display_ring(priv, rx_head, priv->dma_conf.dma_rx_size, true,
5016 rx_q->dma_rx_phy, desc_size);
5018 while (count < limit) {
5019 struct stmmac_rx_buffer *buf;
5020 unsigned int buf1_len = 0;
5021 struct dma_desc *np, *p;
5025 if (!count && rx_q->state_saved) {
5026 error = rx_q->state.error;
5027 len = rx_q->state.len;
5029 rx_q->state_saved = false;
5040 buf = &rx_q->buf_pool[entry];
5042 if (dirty >= STMMAC_RX_FILL_BATCH) {
5043 failure = failure ||
5044 !stmmac_rx_refill_zc(priv, queue, dirty);
5048 if (priv->extend_desc)
5049 p = (struct dma_desc *)(rx_q->dma_erx + entry);
5051 p = rx_q->dma_rx + entry;
5053 /* read the status of the incoming frame */
5054 status = stmmac_rx_status(priv, &priv->dev->stats,
5056 /* check if managed by the DMA otherwise go ahead */
5057 if (unlikely(status & dma_own))
5060 /* Prefetch the next RX descriptor */
5061 rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx,
5062 priv->dma_conf.dma_rx_size);
5063 next_entry = rx_q->cur_rx;
5065 if (priv->extend_desc)
5066 np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
5068 np = rx_q->dma_rx + next_entry;
5072 /* Ensure a valid XSK buffer before proceed */
5076 if (priv->extend_desc)
5077 stmmac_rx_extended_status(priv, &priv->dev->stats,
5079 rx_q->dma_erx + entry);
5080 if (unlikely(status == discard_frame)) {
5081 xsk_buff_free(buf->xdp);
5085 if (!priv->hwts_rx_en)
5086 priv->dev->stats.rx_errors++;
5089 if (unlikely(error && (status & rx_not_ls)))
5091 if (unlikely(error)) {
5096 /* XSK pool expects RX frame 1:1 mapped to XSK buffer */
5097 if (likely(status & rx_not_ls)) {
5098 xsk_buff_free(buf->xdp);
5105 /* XDP ZC Frame only support primary buffers for now */
5106 buf1_len = stmmac_rx_buf1_len(priv, p, status, len);
5109 /* ACS is disabled; strip manually. */
5110 if (likely(!(status & rx_not_ls))) {
5111 buf1_len -= ETH_FCS_LEN;
5115 /* RX buffer is good and fit into a XSK pool buffer */
5116 buf->xdp->data_end = buf->xdp->data + buf1_len;
5117 xsk_buff_dma_sync_for_cpu(buf->xdp, rx_q->xsk_pool);
5119 prog = READ_ONCE(priv->xdp_prog);
5120 res = __stmmac_xdp_run_prog(priv, prog, buf->xdp);
5123 case STMMAC_XDP_PASS:
5124 stmmac_dispatch_skb_zc(priv, queue, p, np, buf->xdp);
5125 xsk_buff_free(buf->xdp);
5127 case STMMAC_XDP_CONSUMED:
5128 xsk_buff_free(buf->xdp);
5129 priv->dev->stats.rx_dropped++;
5132 case STMMAC_XDP_REDIRECT:
5142 if (status & rx_not_ls) {
5143 rx_q->state_saved = true;
5144 rx_q->state.error = error;
5145 rx_q->state.len = len;
5148 stmmac_finalize_xdp_rx(priv, xdp_status);
5150 priv->xstats.rx_pkt_n += count;
5151 priv->xstats.rxq_stats[queue].rx_pkt_n += count;
5153 if (xsk_uses_need_wakeup(rx_q->xsk_pool)) {
5154 if (failure || stmmac_rx_dirty(priv, queue) > 0)
5155 xsk_set_rx_need_wakeup(rx_q->xsk_pool);
5157 xsk_clear_rx_need_wakeup(rx_q->xsk_pool);
5162 return failure ? limit : (int)count;
5166 * stmmac_rx - manage the receive process
5167 * @priv: driver private structure
5168 * @limit: napi bugget
5169 * @queue: RX queue index.
5170 * Description : this the function called by the napi poll method.
5171 * It gets all the frames inside the ring.
5173 static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
5175 struct stmmac_rx_queue *rx_q = &priv->dma_conf.rx_queue[queue];
5176 struct stmmac_channel *ch = &priv->channel[queue];
5177 unsigned int count = 0, error = 0, len = 0;
5178 int status = 0, coe = priv->hw->rx_csum;
5179 unsigned int next_entry = rx_q->cur_rx;
5180 enum dma_data_direction dma_dir;
5181 unsigned int desc_size;
5182 struct sk_buff *skb = NULL;
5183 struct xdp_buff xdp;
5187 dma_dir = page_pool_get_dma_dir(rx_q->page_pool);
5188 buf_sz = DIV_ROUND_UP(priv->dma_conf.dma_buf_sz, PAGE_SIZE) * PAGE_SIZE;
5190 if (netif_msg_rx_status(priv)) {
5193 netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
5194 if (priv->extend_desc) {
5195 rx_head = (void *)rx_q->dma_erx;
5196 desc_size = sizeof(struct dma_extended_desc);
5198 rx_head = (void *)rx_q->dma_rx;
5199 desc_size = sizeof(struct dma_desc);
5202 stmmac_display_ring(priv, rx_head, priv->dma_conf.dma_rx_size, true,
5203 rx_q->dma_rx_phy, desc_size);
5205 while (count < limit) {
5206 unsigned int buf1_len = 0, buf2_len = 0;
5207 enum pkt_hash_types hash_type;
5208 struct stmmac_rx_buffer *buf;
5209 struct dma_desc *np, *p;
5213 if (!count && rx_q->state_saved) {
5214 skb = rx_q->state.skb;
5215 error = rx_q->state.error;
5216 len = rx_q->state.len;
5218 rx_q->state_saved = false;
5231 buf = &rx_q->buf_pool[entry];
5233 if (priv->extend_desc)
5234 p = (struct dma_desc *)(rx_q->dma_erx + entry);
5236 p = rx_q->dma_rx + entry;
5238 /* read the status of the incoming frame */
5239 status = stmmac_rx_status(priv, &priv->dev->stats,
5241 /* check if managed by the DMA otherwise go ahead */
5242 if (unlikely(status & dma_own))
5245 rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx,
5246 priv->dma_conf.dma_rx_size);
5247 next_entry = rx_q->cur_rx;
5249 if (priv->extend_desc)
5250 np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
5252 np = rx_q->dma_rx + next_entry;
5256 if (priv->extend_desc)
5257 stmmac_rx_extended_status(priv, &priv->dev->stats,
5258 &priv->xstats, rx_q->dma_erx + entry);
5259 if (unlikely(status == discard_frame)) {
5260 page_pool_recycle_direct(rx_q->page_pool, buf->page);
5263 if (!priv->hwts_rx_en)
5264 priv->dev->stats.rx_errors++;
5267 if (unlikely(error && (status & rx_not_ls)))
5269 if (unlikely(error)) {
5276 /* Buffer is good. Go on. */
5278 prefetch(page_address(buf->page) + buf->page_offset);
5280 prefetch(page_address(buf->sec_page));
5282 buf1_len = stmmac_rx_buf1_len(priv, p, status, len);
5284 buf2_len = stmmac_rx_buf2_len(priv, p, status, len);
5287 /* ACS is disabled; strip manually. */
5288 if (likely(!(status & rx_not_ls))) {
5290 buf2_len -= ETH_FCS_LEN;
5292 } else if (buf1_len) {
5293 buf1_len -= ETH_FCS_LEN;
5299 unsigned int pre_len, sync_len;
5301 dma_sync_single_for_cpu(priv->device, buf->addr,
5304 xdp_init_buff(&xdp, buf_sz, &rx_q->xdp_rxq);
5305 xdp_prepare_buff(&xdp, page_address(buf->page),
5306 buf->page_offset, buf1_len, false);
5308 pre_len = xdp.data_end - xdp.data_hard_start -
5310 skb = stmmac_xdp_run_prog(priv, &xdp);
5311 /* Due xdp_adjust_tail: DMA sync for_device
5312 * cover max len CPU touch
5314 sync_len = xdp.data_end - xdp.data_hard_start -
5316 sync_len = max(sync_len, pre_len);
5318 /* For Not XDP_PASS verdict */
5320 unsigned int xdp_res = -PTR_ERR(skb);
5322 if (xdp_res & STMMAC_XDP_CONSUMED) {
5323 page_pool_put_page(rx_q->page_pool,
5324 virt_to_head_page(xdp.data),
5327 priv->dev->stats.rx_dropped++;
5329 /* Clear skb as it was set as
5330 * status by XDP program.
5334 if (unlikely((status & rx_not_ls)))
5339 } else if (xdp_res & (STMMAC_XDP_TX |
5340 STMMAC_XDP_REDIRECT)) {
5341 xdp_status |= xdp_res;
5351 /* XDP program may expand or reduce tail */
5352 buf1_len = xdp.data_end - xdp.data;
5354 skb = napi_alloc_skb(&ch->rx_napi, buf1_len);
5356 priv->dev->stats.rx_dropped++;
5361 /* XDP program may adjust header */
5362 skb_copy_to_linear_data(skb, xdp.data, buf1_len);
5363 skb_put(skb, buf1_len);
5365 /* Data payload copied into SKB, page ready for recycle */
5366 page_pool_recycle_direct(rx_q->page_pool, buf->page);
5368 } else if (buf1_len) {
5369 dma_sync_single_for_cpu(priv->device, buf->addr,
5371 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
5372 buf->page, buf->page_offset, buf1_len,
5373 priv->dma_conf.dma_buf_sz);
5375 /* Data payload appended into SKB */
5376 page_pool_release_page(rx_q->page_pool, buf->page);
5381 dma_sync_single_for_cpu(priv->device, buf->sec_addr,
5383 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
5384 buf->sec_page, 0, buf2_len,
5385 priv->dma_conf.dma_buf_sz);
5387 /* Data payload appended into SKB */
5388 page_pool_release_page(rx_q->page_pool, buf->sec_page);
5389 buf->sec_page = NULL;
5393 if (likely(status & rx_not_ls))
5398 /* Got entire packet into SKB. Finish it. */
5400 stmmac_get_rx_hwtstamp(priv, p, np, skb);
5401 stmmac_rx_vlan(priv->dev, skb);
5402 skb->protocol = eth_type_trans(skb, priv->dev);
5405 skb_checksum_none_assert(skb);
5407 skb->ip_summed = CHECKSUM_UNNECESSARY;
5409 if (!stmmac_get_rx_hash(priv, p, &hash, &hash_type))
5410 skb_set_hash(skb, hash, hash_type);
5412 skb_record_rx_queue(skb, queue);
5413 napi_gro_receive(&ch->rx_napi, skb);
5416 priv->dev->stats.rx_packets++;
5417 priv->dev->stats.rx_bytes += len;
5421 if (status & rx_not_ls || skb) {
5422 rx_q->state_saved = true;
5423 rx_q->state.skb = skb;
5424 rx_q->state.error = error;
5425 rx_q->state.len = len;
5428 stmmac_finalize_xdp_rx(priv, xdp_status);
5430 stmmac_rx_refill(priv, queue);
5432 priv->xstats.rx_pkt_n += count;
5433 priv->xstats.rxq_stats[queue].rx_pkt_n += count;
5438 static int stmmac_napi_poll_rx(struct napi_struct *napi, int budget)
5440 struct stmmac_channel *ch =
5441 container_of(napi, struct stmmac_channel, rx_napi);
5442 struct stmmac_priv *priv = ch->priv_data;
5443 u32 chan = ch->index;
5446 priv->xstats.napi_poll++;
5448 work_done = stmmac_rx(priv, budget, chan);
5449 if (work_done < budget && napi_complete_done(napi, work_done)) {
5450 unsigned long flags;
5452 spin_lock_irqsave(&ch->lock, flags);
5453 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
5454 spin_unlock_irqrestore(&ch->lock, flags);
5460 static int stmmac_napi_poll_tx(struct napi_struct *napi, int budget)
5462 struct stmmac_channel *ch =
5463 container_of(napi, struct stmmac_channel, tx_napi);
5464 struct stmmac_priv *priv = ch->priv_data;
5465 u32 chan = ch->index;
5468 priv->xstats.napi_poll++;
5470 work_done = stmmac_tx_clean(priv, budget, chan);
5471 work_done = min(work_done, budget);
5473 if (work_done < budget && napi_complete_done(napi, work_done)) {
5474 unsigned long flags;
5476 spin_lock_irqsave(&ch->lock, flags);
5477 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
5478 spin_unlock_irqrestore(&ch->lock, flags);
5484 static int stmmac_napi_poll_rxtx(struct napi_struct *napi, int budget)
5486 struct stmmac_channel *ch =
5487 container_of(napi, struct stmmac_channel, rxtx_napi);
5488 struct stmmac_priv *priv = ch->priv_data;
5489 int rx_done, tx_done, rxtx_done;
5490 u32 chan = ch->index;
5492 priv->xstats.napi_poll++;
5494 tx_done = stmmac_tx_clean(priv, budget, chan);
5495 tx_done = min(tx_done, budget);
5497 rx_done = stmmac_rx_zc(priv, budget, chan);
5499 rxtx_done = max(tx_done, rx_done);
5501 /* If either TX or RX work is not complete, return budget
5504 if (rxtx_done >= budget)
5507 /* all work done, exit the polling mode */
5508 if (napi_complete_done(napi, rxtx_done)) {
5509 unsigned long flags;
5511 spin_lock_irqsave(&ch->lock, flags);
5512 /* Both RX and TX work done are compelte,
5513 * so enable both RX & TX IRQs.
5515 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 1);
5516 spin_unlock_irqrestore(&ch->lock, flags);
5519 return min(rxtx_done, budget - 1);
5524 * @dev : Pointer to net device structure
5525 * @txqueue: the index of the hanging transmit queue
5526 * Description: this function is called when a packet transmission fails to
5527 * complete within a reasonable time. The driver will mark the error in the
5528 * netdev structure and arrange for the device to be reset to a sane state
5529 * in order to transmit a new packet.
5531 static void stmmac_tx_timeout(struct net_device *dev, unsigned int txqueue)
5533 struct stmmac_priv *priv = netdev_priv(dev);
5535 stmmac_global_err(priv);
5539 * stmmac_set_rx_mode - entry point for multicast addressing
5540 * @dev : pointer to the device structure
5542 * This function is a driver entry point which gets called by the kernel
5543 * whenever multicast addresses must be enabled/disabled.
5547 static void stmmac_set_rx_mode(struct net_device *dev)
5549 struct stmmac_priv *priv = netdev_priv(dev);
5551 stmmac_set_filter(priv, priv->hw, dev);
5555 * stmmac_change_mtu - entry point to change MTU size for the device.
5556 * @dev : device pointer.
5557 * @new_mtu : the new MTU size for the device.
5558 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
5559 * to drive packet transmission. Ethernet has an MTU of 1500 octets
5560 * (ETH_DATA_LEN). This value can be changed with ifconfig.
5562 * 0 on success and an appropriate (-)ve integer as defined in errno.h
5565 static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
5567 struct stmmac_priv *priv = netdev_priv(dev);
5568 int txfifosz = priv->plat->tx_fifo_size;
5569 struct stmmac_dma_conf *dma_conf;
5570 const int mtu = new_mtu;
5574 txfifosz = priv->dma_cap.tx_fifo_size;
5576 txfifosz /= priv->plat->tx_queues_to_use;
5578 if (stmmac_xdp_is_enabled(priv) && new_mtu > ETH_DATA_LEN) {
5579 netdev_dbg(priv->dev, "Jumbo frames not supported for XDP\n");
5583 new_mtu = STMMAC_ALIGN(new_mtu);
5585 /* If condition true, FIFO is too small or MTU too large */
5586 if ((txfifosz < new_mtu) || (new_mtu > BUF_SIZE_16KiB))
5589 if (netif_running(dev)) {
5590 netdev_dbg(priv->dev, "restarting interface to change its MTU\n");
5591 /* Try to allocate the new DMA conf with the new mtu */
5592 dma_conf = stmmac_setup_dma_desc(priv, mtu);
5593 if (IS_ERR(dma_conf)) {
5594 netdev_err(priv->dev, "failed allocating new dma conf for new MTU %d\n",
5596 return PTR_ERR(dma_conf);
5599 stmmac_release(dev);
5601 ret = __stmmac_open(dev, dma_conf);
5604 netdev_err(priv->dev, "failed reopening the interface after MTU change\n");
5608 stmmac_set_rx_mode(dev);
5612 netdev_update_features(dev);
5617 static netdev_features_t stmmac_fix_features(struct net_device *dev,
5618 netdev_features_t features)
5620 struct stmmac_priv *priv = netdev_priv(dev);
5622 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
5623 features &= ~NETIF_F_RXCSUM;
5625 if (!priv->plat->tx_coe)
5626 features &= ~NETIF_F_CSUM_MASK;
5628 /* Some GMAC devices have a bugged Jumbo frame support that
5629 * needs to have the Tx COE disabled for oversized frames
5630 * (due to limited buffer sizes). In this case we disable
5631 * the TX csum insertion in the TDES and not use SF.
5633 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
5634 features &= ~NETIF_F_CSUM_MASK;
5636 /* Disable tso if asked by ethtool */
5637 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
5638 if (features & NETIF_F_TSO)
5647 static int stmmac_set_features(struct net_device *netdev,
5648 netdev_features_t features)
5650 struct stmmac_priv *priv = netdev_priv(netdev);
5652 /* Keep the COE Type in case of csum is supporting */
5653 if (features & NETIF_F_RXCSUM)
5654 priv->hw->rx_csum = priv->plat->rx_coe;
5656 priv->hw->rx_csum = 0;
5657 /* No check needed because rx_coe has been set before and it will be
5658 * fixed in case of issue.
5660 stmmac_rx_ipc(priv, priv->hw);
5662 if (priv->sph_cap) {
5663 bool sph_en = (priv->hw->rx_csum > 0) && priv->sph;
5666 for (chan = 0; chan < priv->plat->rx_queues_to_use; chan++)
5667 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
5673 static void stmmac_fpe_event_status(struct stmmac_priv *priv, int status)
5675 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
5676 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
5677 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
5678 bool *hs_enable = &fpe_cfg->hs_enable;
5680 if (status == FPE_EVENT_UNKNOWN || !*hs_enable)
5683 /* If LP has sent verify mPacket, LP is FPE capable */
5684 if ((status & FPE_EVENT_RVER) == FPE_EVENT_RVER) {
5685 if (*lp_state < FPE_STATE_CAPABLE)
5686 *lp_state = FPE_STATE_CAPABLE;
5688 /* If user has requested FPE enable, quickly response */
5690 stmmac_fpe_send_mpacket(priv, priv->ioaddr,
5694 /* If Local has sent verify mPacket, Local is FPE capable */
5695 if ((status & FPE_EVENT_TVER) == FPE_EVENT_TVER) {
5696 if (*lo_state < FPE_STATE_CAPABLE)
5697 *lo_state = FPE_STATE_CAPABLE;
5700 /* If LP has sent response mPacket, LP is entering FPE ON */
5701 if ((status & FPE_EVENT_RRSP) == FPE_EVENT_RRSP)
5702 *lp_state = FPE_STATE_ENTERING_ON;
5704 /* If Local has sent response mPacket, Local is entering FPE ON */
5705 if ((status & FPE_EVENT_TRSP) == FPE_EVENT_TRSP)
5706 *lo_state = FPE_STATE_ENTERING_ON;
5708 if (!test_bit(__FPE_REMOVING, &priv->fpe_task_state) &&
5709 !test_and_set_bit(__FPE_TASK_SCHED, &priv->fpe_task_state) &&
5711 queue_work(priv->fpe_wq, &priv->fpe_task);
5715 static void stmmac_common_interrupt(struct stmmac_priv *priv)
5717 u32 rx_cnt = priv->plat->rx_queues_to_use;
5718 u32 tx_cnt = priv->plat->tx_queues_to_use;
5723 xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
5724 queues_count = (rx_cnt > tx_cnt) ? rx_cnt : tx_cnt;
5727 pm_wakeup_event(priv->device, 0);
5729 if (priv->dma_cap.estsel)
5730 stmmac_est_irq_status(priv, priv->ioaddr, priv->dev,
5731 &priv->xstats, tx_cnt);
5733 if (priv->dma_cap.fpesel) {
5734 int status = stmmac_fpe_irq_status(priv, priv->ioaddr,
5737 stmmac_fpe_event_status(priv, status);
5740 /* To handle GMAC own interrupts */
5741 if ((priv->plat->has_gmac) || xmac) {
5742 int status = stmmac_host_irq_status(priv, priv->hw, &priv->xstats);
5744 if (unlikely(status)) {
5745 /* For LPI we need to save the tx status */
5746 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
5747 priv->tx_path_in_lpi_mode = true;
5748 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
5749 priv->tx_path_in_lpi_mode = false;
5752 for (queue = 0; queue < queues_count; queue++) {
5753 status = stmmac_host_mtl_irq_status(priv, priv->hw,
5757 /* PCS link status */
5758 if (priv->hw->pcs) {
5759 if (priv->xstats.pcs_link)
5760 netif_carrier_on(priv->dev);
5762 netif_carrier_off(priv->dev);
5765 stmmac_timestamp_interrupt(priv, priv);
5770 * stmmac_interrupt - main ISR
5771 * @irq: interrupt number.
5772 * @dev_id: to pass the net device pointer.
5773 * Description: this is the main driver interrupt service routine.
5775 * o DMA service routine (to manage incoming frame reception and transmission
5777 * o Core interrupts to manage: remote wake-up, management counter, LPI
5780 static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
5782 struct net_device *dev = (struct net_device *)dev_id;
5783 struct stmmac_priv *priv = netdev_priv(dev);
5785 /* Check if adapter is up */
5786 if (test_bit(STMMAC_DOWN, &priv->state))
5789 /* Check if a fatal error happened */
5790 if (stmmac_safety_feat_interrupt(priv))
5793 /* To handle Common interrupts */
5794 stmmac_common_interrupt(priv);
5796 /* To handle DMA interrupts */
5797 stmmac_dma_interrupt(priv);
5802 static irqreturn_t stmmac_mac_interrupt(int irq, void *dev_id)
5804 struct net_device *dev = (struct net_device *)dev_id;
5805 struct stmmac_priv *priv = netdev_priv(dev);
5807 if (unlikely(!dev)) {
5808 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5812 /* Check if adapter is up */
5813 if (test_bit(STMMAC_DOWN, &priv->state))
5816 /* To handle Common interrupts */
5817 stmmac_common_interrupt(priv);
5822 static irqreturn_t stmmac_safety_interrupt(int irq, void *dev_id)
5824 struct net_device *dev = (struct net_device *)dev_id;
5825 struct stmmac_priv *priv = netdev_priv(dev);
5827 if (unlikely(!dev)) {
5828 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5832 /* Check if adapter is up */
5833 if (test_bit(STMMAC_DOWN, &priv->state))
5836 /* Check if a fatal error happened */
5837 stmmac_safety_feat_interrupt(priv);
5842 static irqreturn_t stmmac_msi_intr_tx(int irq, void *data)
5844 struct stmmac_tx_queue *tx_q = (struct stmmac_tx_queue *)data;
5845 struct stmmac_dma_conf *dma_conf;
5846 int chan = tx_q->queue_index;
5847 struct stmmac_priv *priv;
5850 dma_conf = container_of(tx_q, struct stmmac_dma_conf, tx_queue[chan]);
5851 priv = container_of(dma_conf, struct stmmac_priv, dma_conf);
5853 if (unlikely(!data)) {
5854 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5858 /* Check if adapter is up */
5859 if (test_bit(STMMAC_DOWN, &priv->state))
5862 status = stmmac_napi_check(priv, chan, DMA_DIR_TX);
5864 if (unlikely(status & tx_hard_error_bump_tc)) {
5865 /* Try to bump up the dma threshold on this failure */
5866 stmmac_bump_dma_threshold(priv, chan);
5867 } else if (unlikely(status == tx_hard_error)) {
5868 stmmac_tx_err(priv, chan);
5874 static irqreturn_t stmmac_msi_intr_rx(int irq, void *data)
5876 struct stmmac_rx_queue *rx_q = (struct stmmac_rx_queue *)data;
5877 struct stmmac_dma_conf *dma_conf;
5878 int chan = rx_q->queue_index;
5879 struct stmmac_priv *priv;
5881 dma_conf = container_of(rx_q, struct stmmac_dma_conf, rx_queue[chan]);
5882 priv = container_of(dma_conf, struct stmmac_priv, dma_conf);
5884 if (unlikely(!data)) {
5885 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5889 /* Check if adapter is up */
5890 if (test_bit(STMMAC_DOWN, &priv->state))
5893 stmmac_napi_check(priv, chan, DMA_DIR_RX);
5898 #ifdef CONFIG_NET_POLL_CONTROLLER
5899 /* Polling receive - used by NETCONSOLE and other diagnostic tools
5900 * to allow network I/O with interrupts disabled.
5902 static void stmmac_poll_controller(struct net_device *dev)
5904 struct stmmac_priv *priv = netdev_priv(dev);
5907 /* If adapter is down, do nothing */
5908 if (test_bit(STMMAC_DOWN, &priv->state))
5911 if (priv->plat->multi_msi_en) {
5912 for (i = 0; i < priv->plat->rx_queues_to_use; i++)
5913 stmmac_msi_intr_rx(0, &priv->dma_conf.rx_queue[i]);
5915 for (i = 0; i < priv->plat->tx_queues_to_use; i++)
5916 stmmac_msi_intr_tx(0, &priv->dma_conf.tx_queue[i]);
5918 disable_irq(dev->irq);
5919 stmmac_interrupt(dev->irq, dev);
5920 enable_irq(dev->irq);
5926 * stmmac_ioctl - Entry point for the Ioctl
5927 * @dev: Device pointer.
5928 * @rq: An IOCTL specefic structure, that can contain a pointer to
5929 * a proprietary structure used to pass information to the driver.
5930 * @cmd: IOCTL command
5932 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
5934 static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
5936 struct stmmac_priv *priv = netdev_priv (dev);
5937 int ret = -EOPNOTSUPP;
5939 if (!netif_running(dev))
5946 ret = phylink_mii_ioctl(priv->phylink, rq, cmd);
5949 ret = stmmac_hwtstamp_set(dev, rq);
5952 ret = stmmac_hwtstamp_get(dev, rq);
5961 static int stmmac_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
5964 struct stmmac_priv *priv = cb_priv;
5965 int ret = -EOPNOTSUPP;
5967 if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
5970 __stmmac_disable_all_queues(priv);
5973 case TC_SETUP_CLSU32:
5974 ret = stmmac_tc_setup_cls_u32(priv, priv, type_data);
5976 case TC_SETUP_CLSFLOWER:
5977 ret = stmmac_tc_setup_cls(priv, priv, type_data);
5983 stmmac_enable_all_queues(priv);
5987 static LIST_HEAD(stmmac_block_cb_list);
5989 static int stmmac_setup_tc(struct net_device *ndev, enum tc_setup_type type,
5992 struct stmmac_priv *priv = netdev_priv(ndev);
5995 case TC_SETUP_BLOCK:
5996 return flow_block_cb_setup_simple(type_data,
5997 &stmmac_block_cb_list,
5998 stmmac_setup_tc_block_cb,
6000 case TC_SETUP_QDISC_CBS:
6001 return stmmac_tc_setup_cbs(priv, priv, type_data);
6002 case TC_SETUP_QDISC_TAPRIO:
6003 return stmmac_tc_setup_taprio(priv, priv, type_data);
6004 case TC_SETUP_QDISC_ETF:
6005 return stmmac_tc_setup_etf(priv, priv, type_data);
6011 static u16 stmmac_select_queue(struct net_device *dev, struct sk_buff *skb,
6012 struct net_device *sb_dev)
6014 int gso = skb_shinfo(skb)->gso_type;
6016 if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6 | SKB_GSO_UDP_L4)) {
6018 * There is no way to determine the number of TSO/USO
6019 * capable Queues. Let's use always the Queue 0
6020 * because if TSO/USO is supported then at least this
6021 * one will be capable.
6026 return netdev_pick_tx(dev, skb, NULL) % dev->real_num_tx_queues;
6029 static int stmmac_set_mac_address(struct net_device *ndev, void *addr)
6031 struct stmmac_priv *priv = netdev_priv(ndev);
6034 ret = pm_runtime_resume_and_get(priv->device);
6038 ret = eth_mac_addr(ndev, addr);
6042 stmmac_set_umac_addr(priv, priv->hw, ndev->dev_addr, 0);
6045 pm_runtime_put(priv->device);
6050 #ifdef CONFIG_DEBUG_FS
6051 static struct dentry *stmmac_fs_dir;
6053 static void sysfs_display_ring(void *head, int size, int extend_desc,
6054 struct seq_file *seq, dma_addr_t dma_phy_addr)
6057 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
6058 struct dma_desc *p = (struct dma_desc *)head;
6059 dma_addr_t dma_addr;
6061 for (i = 0; i < size; i++) {
6063 dma_addr = dma_phy_addr + i * sizeof(*ep);
6064 seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n",
6066 le32_to_cpu(ep->basic.des0),
6067 le32_to_cpu(ep->basic.des1),
6068 le32_to_cpu(ep->basic.des2),
6069 le32_to_cpu(ep->basic.des3));
6072 dma_addr = dma_phy_addr + i * sizeof(*p);
6073 seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n",
6075 le32_to_cpu(p->des0), le32_to_cpu(p->des1),
6076 le32_to_cpu(p->des2), le32_to_cpu(p->des3));
6079 seq_printf(seq, "\n");
6083 static int stmmac_rings_status_show(struct seq_file *seq, void *v)
6085 struct net_device *dev = seq->private;
6086 struct stmmac_priv *priv = netdev_priv(dev);
6087 u32 rx_count = priv->plat->rx_queues_to_use;
6088 u32 tx_count = priv->plat->tx_queues_to_use;
6091 if ((dev->flags & IFF_UP) == 0)
6094 for (queue = 0; queue < rx_count; queue++) {
6095 struct stmmac_rx_queue *rx_q = &priv->dma_conf.rx_queue[queue];
6097 seq_printf(seq, "RX Queue %d:\n", queue);
6099 if (priv->extend_desc) {
6100 seq_printf(seq, "Extended descriptor ring:\n");
6101 sysfs_display_ring((void *)rx_q->dma_erx,
6102 priv->dma_conf.dma_rx_size, 1, seq, rx_q->dma_rx_phy);
6104 seq_printf(seq, "Descriptor ring:\n");
6105 sysfs_display_ring((void *)rx_q->dma_rx,
6106 priv->dma_conf.dma_rx_size, 0, seq, rx_q->dma_rx_phy);
6110 for (queue = 0; queue < tx_count; queue++) {
6111 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[queue];
6113 seq_printf(seq, "TX Queue %d:\n", queue);
6115 if (priv->extend_desc) {
6116 seq_printf(seq, "Extended descriptor ring:\n");
6117 sysfs_display_ring((void *)tx_q->dma_etx,
6118 priv->dma_conf.dma_tx_size, 1, seq, tx_q->dma_tx_phy);
6119 } else if (!(tx_q->tbs & STMMAC_TBS_AVAIL)) {
6120 seq_printf(seq, "Descriptor ring:\n");
6121 sysfs_display_ring((void *)tx_q->dma_tx,
6122 priv->dma_conf.dma_tx_size, 0, seq, tx_q->dma_tx_phy);
6128 DEFINE_SHOW_ATTRIBUTE(stmmac_rings_status);
6130 static int stmmac_dma_cap_show(struct seq_file *seq, void *v)
6132 struct net_device *dev = seq->private;
6133 struct stmmac_priv *priv = netdev_priv(dev);
6135 if (!priv->hw_cap_support) {
6136 seq_printf(seq, "DMA HW features not supported\n");
6140 seq_printf(seq, "==============================\n");
6141 seq_printf(seq, "\tDMA HW features\n");
6142 seq_printf(seq, "==============================\n");
6144 seq_printf(seq, "\t10/100 Mbps: %s\n",
6145 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
6146 seq_printf(seq, "\t1000 Mbps: %s\n",
6147 (priv->dma_cap.mbps_1000) ? "Y" : "N");
6148 seq_printf(seq, "\tHalf duplex: %s\n",
6149 (priv->dma_cap.half_duplex) ? "Y" : "N");
6150 seq_printf(seq, "\tHash Filter: %s\n",
6151 (priv->dma_cap.hash_filter) ? "Y" : "N");
6152 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
6153 (priv->dma_cap.multi_addr) ? "Y" : "N");
6154 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfaces): %s\n",
6155 (priv->dma_cap.pcs) ? "Y" : "N");
6156 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
6157 (priv->dma_cap.sma_mdio) ? "Y" : "N");
6158 seq_printf(seq, "\tPMT Remote wake up: %s\n",
6159 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
6160 seq_printf(seq, "\tPMT Magic Frame: %s\n",
6161 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
6162 seq_printf(seq, "\tRMON module: %s\n",
6163 (priv->dma_cap.rmon) ? "Y" : "N");
6164 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
6165 (priv->dma_cap.time_stamp) ? "Y" : "N");
6166 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp: %s\n",
6167 (priv->dma_cap.atime_stamp) ? "Y" : "N");
6168 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE): %s\n",
6169 (priv->dma_cap.eee) ? "Y" : "N");
6170 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
6171 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
6172 (priv->dma_cap.tx_coe) ? "Y" : "N");
6173 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
6174 seq_printf(seq, "\tIP Checksum Offload in RX: %s\n",
6175 (priv->dma_cap.rx_coe) ? "Y" : "N");
6177 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
6178 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
6179 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
6180 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
6182 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
6183 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
6184 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
6185 priv->dma_cap.number_rx_channel);
6186 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
6187 priv->dma_cap.number_tx_channel);
6188 seq_printf(seq, "\tNumber of Additional RX queues: %d\n",
6189 priv->dma_cap.number_rx_queues);
6190 seq_printf(seq, "\tNumber of Additional TX queues: %d\n",
6191 priv->dma_cap.number_tx_queues);
6192 seq_printf(seq, "\tEnhanced descriptors: %s\n",
6193 (priv->dma_cap.enh_desc) ? "Y" : "N");
6194 seq_printf(seq, "\tTX Fifo Size: %d\n", priv->dma_cap.tx_fifo_size);
6195 seq_printf(seq, "\tRX Fifo Size: %d\n", priv->dma_cap.rx_fifo_size);
6196 seq_printf(seq, "\tHash Table Size: %d\n", priv->dma_cap.hash_tb_sz);
6197 seq_printf(seq, "\tTSO: %s\n", priv->dma_cap.tsoen ? "Y" : "N");
6198 seq_printf(seq, "\tNumber of PPS Outputs: %d\n",
6199 priv->dma_cap.pps_out_num);
6200 seq_printf(seq, "\tSafety Features: %s\n",
6201 priv->dma_cap.asp ? "Y" : "N");
6202 seq_printf(seq, "\tFlexible RX Parser: %s\n",
6203 priv->dma_cap.frpsel ? "Y" : "N");
6204 seq_printf(seq, "\tEnhanced Addressing: %d\n",
6205 priv->dma_cap.addr64);
6206 seq_printf(seq, "\tReceive Side Scaling: %s\n",
6207 priv->dma_cap.rssen ? "Y" : "N");
6208 seq_printf(seq, "\tVLAN Hash Filtering: %s\n",
6209 priv->dma_cap.vlhash ? "Y" : "N");
6210 seq_printf(seq, "\tSplit Header: %s\n",
6211 priv->dma_cap.sphen ? "Y" : "N");
6212 seq_printf(seq, "\tVLAN TX Insertion: %s\n",
6213 priv->dma_cap.vlins ? "Y" : "N");
6214 seq_printf(seq, "\tDouble VLAN: %s\n",
6215 priv->dma_cap.dvlan ? "Y" : "N");
6216 seq_printf(seq, "\tNumber of L3/L4 Filters: %d\n",
6217 priv->dma_cap.l3l4fnum);
6218 seq_printf(seq, "\tARP Offloading: %s\n",
6219 priv->dma_cap.arpoffsel ? "Y" : "N");
6220 seq_printf(seq, "\tEnhancements to Scheduled Traffic (EST): %s\n",
6221 priv->dma_cap.estsel ? "Y" : "N");
6222 seq_printf(seq, "\tFrame Preemption (FPE): %s\n",
6223 priv->dma_cap.fpesel ? "Y" : "N");
6224 seq_printf(seq, "\tTime-Based Scheduling (TBS): %s\n",
6225 priv->dma_cap.tbssel ? "Y" : "N");
6228 DEFINE_SHOW_ATTRIBUTE(stmmac_dma_cap);
6230 /* Use network device events to rename debugfs file entries.
6232 static int stmmac_device_event(struct notifier_block *unused,
6233 unsigned long event, void *ptr)
6235 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6236 struct stmmac_priv *priv = netdev_priv(dev);
6238 if (dev->netdev_ops != &stmmac_netdev_ops)
6242 case NETDEV_CHANGENAME:
6243 if (priv->dbgfs_dir)
6244 priv->dbgfs_dir = debugfs_rename(stmmac_fs_dir,
6254 static struct notifier_block stmmac_notifier = {
6255 .notifier_call = stmmac_device_event,
6258 static void stmmac_init_fs(struct net_device *dev)
6260 struct stmmac_priv *priv = netdev_priv(dev);
6264 /* Create per netdev entries */
6265 priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
6267 /* Entry to report DMA RX/TX rings */
6268 debugfs_create_file("descriptors_status", 0444, priv->dbgfs_dir, dev,
6269 &stmmac_rings_status_fops);
6271 /* Entry to report the DMA HW features */
6272 debugfs_create_file("dma_cap", 0444, priv->dbgfs_dir, dev,
6273 &stmmac_dma_cap_fops);
6278 static void stmmac_exit_fs(struct net_device *dev)
6280 struct stmmac_priv *priv = netdev_priv(dev);
6282 debugfs_remove_recursive(priv->dbgfs_dir);
6284 #endif /* CONFIG_DEBUG_FS */
6286 static u32 stmmac_vid_crc32_le(__le16 vid_le)
6288 unsigned char *data = (unsigned char *)&vid_le;
6289 unsigned char data_byte = 0;
6294 bits = get_bitmask_order(VLAN_VID_MASK);
6295 for (i = 0; i < bits; i++) {
6297 data_byte = data[i / 8];
6299 temp = ((crc & 1) ^ data_byte) & 1;
6310 static int stmmac_vlan_update(struct stmmac_priv *priv, bool is_double)
6317 for_each_set_bit(vid, priv->active_vlans, VLAN_N_VID) {
6318 __le16 vid_le = cpu_to_le16(vid);
6319 crc = bitrev32(~stmmac_vid_crc32_le(vid_le)) >> 28;
6324 if (!priv->dma_cap.vlhash) {
6325 if (count > 2) /* VID = 0 always passes filter */
6328 pmatch = cpu_to_le16(vid);
6332 return stmmac_update_vlan_hash(priv, priv->hw, hash, pmatch, is_double);
6335 static int stmmac_vlan_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid)
6337 struct stmmac_priv *priv = netdev_priv(ndev);
6338 bool is_double = false;
6341 if (be16_to_cpu(proto) == ETH_P_8021AD)
6344 set_bit(vid, priv->active_vlans);
6345 ret = stmmac_vlan_update(priv, is_double);
6347 clear_bit(vid, priv->active_vlans);
6351 if (priv->hw->num_vlan) {
6352 ret = stmmac_add_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid);
6360 static int stmmac_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid)
6362 struct stmmac_priv *priv = netdev_priv(ndev);
6363 bool is_double = false;
6366 ret = pm_runtime_resume_and_get(priv->device);
6370 if (be16_to_cpu(proto) == ETH_P_8021AD)
6373 clear_bit(vid, priv->active_vlans);
6375 if (priv->hw->num_vlan) {
6376 ret = stmmac_del_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid);
6378 goto del_vlan_error;
6381 ret = stmmac_vlan_update(priv, is_double);
6384 pm_runtime_put(priv->device);
6389 static int stmmac_bpf(struct net_device *dev, struct netdev_bpf *bpf)
6391 struct stmmac_priv *priv = netdev_priv(dev);
6393 switch (bpf->command) {
6394 case XDP_SETUP_PROG:
6395 return stmmac_xdp_set_prog(priv, bpf->prog, bpf->extack);
6396 case XDP_SETUP_XSK_POOL:
6397 return stmmac_xdp_setup_pool(priv, bpf->xsk.pool,
6404 static int stmmac_xdp_xmit(struct net_device *dev, int num_frames,
6405 struct xdp_frame **frames, u32 flags)
6407 struct stmmac_priv *priv = netdev_priv(dev);
6408 int cpu = smp_processor_id();
6409 struct netdev_queue *nq;
6413 if (unlikely(test_bit(STMMAC_DOWN, &priv->state)))
6416 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
6419 queue = stmmac_xdp_get_tx_queue(priv, cpu);
6420 nq = netdev_get_tx_queue(priv->dev, queue);
6422 __netif_tx_lock(nq, cpu);
6423 /* Avoids TX time-out as we are sharing with slow path */
6424 txq_trans_cond_update(nq);
6426 for (i = 0; i < num_frames; i++) {
6429 res = stmmac_xdp_xmit_xdpf(priv, queue, frames[i], true);
6430 if (res == STMMAC_XDP_CONSUMED)
6436 if (flags & XDP_XMIT_FLUSH) {
6437 stmmac_flush_tx_descriptors(priv, queue);
6438 stmmac_tx_timer_arm(priv, queue);
6441 __netif_tx_unlock(nq);
6446 void stmmac_disable_rx_queue(struct stmmac_priv *priv, u32 queue)
6448 struct stmmac_channel *ch = &priv->channel[queue];
6449 unsigned long flags;
6451 spin_lock_irqsave(&ch->lock, flags);
6452 stmmac_disable_dma_irq(priv, priv->ioaddr, queue, 1, 0);
6453 spin_unlock_irqrestore(&ch->lock, flags);
6455 stmmac_stop_rx_dma(priv, queue);
6456 __free_dma_rx_desc_resources(priv, &priv->dma_conf, queue);
6459 void stmmac_enable_rx_queue(struct stmmac_priv *priv, u32 queue)
6461 struct stmmac_rx_queue *rx_q = &priv->dma_conf.rx_queue[queue];
6462 struct stmmac_channel *ch = &priv->channel[queue];
6463 unsigned long flags;
6467 ret = __alloc_dma_rx_desc_resources(priv, &priv->dma_conf, queue);
6469 netdev_err(priv->dev, "Failed to alloc RX desc.\n");
6473 ret = __init_dma_rx_desc_rings(priv, &priv->dma_conf, queue, GFP_KERNEL);
6475 __free_dma_rx_desc_resources(priv, &priv->dma_conf, queue);
6476 netdev_err(priv->dev, "Failed to init RX desc.\n");
6480 stmmac_reset_rx_queue(priv, queue);
6481 stmmac_clear_rx_descriptors(priv, &priv->dma_conf, queue);
6483 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6484 rx_q->dma_rx_phy, rx_q->queue_index);
6486 rx_q->rx_tail_addr = rx_q->dma_rx_phy + (rx_q->buf_alloc_num *
6487 sizeof(struct dma_desc));
6488 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
6489 rx_q->rx_tail_addr, rx_q->queue_index);
6491 if (rx_q->xsk_pool && rx_q->buf_alloc_num) {
6492 buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool);
6493 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6497 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6498 priv->dma_conf.dma_buf_sz,
6502 stmmac_start_rx_dma(priv, queue);
6504 spin_lock_irqsave(&ch->lock, flags);
6505 stmmac_enable_dma_irq(priv, priv->ioaddr, queue, 1, 0);
6506 spin_unlock_irqrestore(&ch->lock, flags);
6509 void stmmac_disable_tx_queue(struct stmmac_priv *priv, u32 queue)
6511 struct stmmac_channel *ch = &priv->channel[queue];
6512 unsigned long flags;
6514 spin_lock_irqsave(&ch->lock, flags);
6515 stmmac_disable_dma_irq(priv, priv->ioaddr, queue, 0, 1);
6516 spin_unlock_irqrestore(&ch->lock, flags);
6518 stmmac_stop_tx_dma(priv, queue);
6519 __free_dma_tx_desc_resources(priv, &priv->dma_conf, queue);
6522 void stmmac_enable_tx_queue(struct stmmac_priv *priv, u32 queue)
6524 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[queue];
6525 struct stmmac_channel *ch = &priv->channel[queue];
6526 unsigned long flags;
6529 ret = __alloc_dma_tx_desc_resources(priv, &priv->dma_conf, queue);
6531 netdev_err(priv->dev, "Failed to alloc TX desc.\n");
6535 ret = __init_dma_tx_desc_rings(priv, &priv->dma_conf, queue);
6537 __free_dma_tx_desc_resources(priv, &priv->dma_conf, queue);
6538 netdev_err(priv->dev, "Failed to init TX desc.\n");
6542 stmmac_reset_tx_queue(priv, queue);
6543 stmmac_clear_tx_descriptors(priv, &priv->dma_conf, queue);
6545 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6546 tx_q->dma_tx_phy, tx_q->queue_index);
6548 if (tx_q->tbs & STMMAC_TBS_AVAIL)
6549 stmmac_enable_tbs(priv, priv->ioaddr, 1, tx_q->queue_index);
6551 tx_q->tx_tail_addr = tx_q->dma_tx_phy;
6552 stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
6553 tx_q->tx_tail_addr, tx_q->queue_index);
6555 stmmac_start_tx_dma(priv, queue);
6557 spin_lock_irqsave(&ch->lock, flags);
6558 stmmac_enable_dma_irq(priv, priv->ioaddr, queue, 0, 1);
6559 spin_unlock_irqrestore(&ch->lock, flags);
6562 void stmmac_xdp_release(struct net_device *dev)
6564 struct stmmac_priv *priv = netdev_priv(dev);
6567 /* Ensure tx function is not running */
6568 netif_tx_disable(dev);
6570 /* Disable NAPI process */
6571 stmmac_disable_all_queues(priv);
6573 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
6574 hrtimer_cancel(&priv->dma_conf.tx_queue[chan].txtimer);
6576 /* Free the IRQ lines */
6577 stmmac_free_irq(dev, REQ_IRQ_ERR_ALL, 0);
6579 /* Stop TX/RX DMA channels */
6580 stmmac_stop_all_dma(priv);
6582 /* Release and free the Rx/Tx resources */
6583 free_dma_desc_resources(priv, &priv->dma_conf);
6585 /* Disable the MAC Rx/Tx */
6586 stmmac_mac_set(priv, priv->ioaddr, false);
6588 /* set trans_start so we don't get spurious
6589 * watchdogs during reset
6591 netif_trans_update(dev);
6592 netif_carrier_off(dev);
6595 int stmmac_xdp_open(struct net_device *dev)
6597 struct stmmac_priv *priv = netdev_priv(dev);
6598 u32 rx_cnt = priv->plat->rx_queues_to_use;
6599 u32 tx_cnt = priv->plat->tx_queues_to_use;
6600 u32 dma_csr_ch = max(rx_cnt, tx_cnt);
6601 struct stmmac_rx_queue *rx_q;
6602 struct stmmac_tx_queue *tx_q;
6608 ret = alloc_dma_desc_resources(priv, &priv->dma_conf);
6610 netdev_err(dev, "%s: DMA descriptors allocation failed\n",
6612 goto dma_desc_error;
6615 ret = init_dma_desc_rings(dev, &priv->dma_conf, GFP_KERNEL);
6617 netdev_err(dev, "%s: DMA descriptors initialization failed\n",
6622 /* DMA CSR Channel configuration */
6623 for (chan = 0; chan < dma_csr_ch; chan++) {
6624 stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan);
6625 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 1, 1);
6628 /* Adjust Split header */
6629 sph_en = (priv->hw->rx_csum > 0) && priv->sph;
6631 /* DMA RX Channel Configuration */
6632 for (chan = 0; chan < rx_cnt; chan++) {
6633 rx_q = &priv->dma_conf.rx_queue[chan];
6635 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6636 rx_q->dma_rx_phy, chan);
6638 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
6639 (rx_q->buf_alloc_num *
6640 sizeof(struct dma_desc));
6641 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
6642 rx_q->rx_tail_addr, chan);
6644 if (rx_q->xsk_pool && rx_q->buf_alloc_num) {
6645 buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool);
6646 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6650 stmmac_set_dma_bfsize(priv, priv->ioaddr,
6651 priv->dma_conf.dma_buf_sz,
6655 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
6658 /* DMA TX Channel Configuration */
6659 for (chan = 0; chan < tx_cnt; chan++) {
6660 tx_q = &priv->dma_conf.tx_queue[chan];
6662 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6663 tx_q->dma_tx_phy, chan);
6665 tx_q->tx_tail_addr = tx_q->dma_tx_phy;
6666 stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
6667 tx_q->tx_tail_addr, chan);
6669 hrtimer_init(&tx_q->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6670 tx_q->txtimer.function = stmmac_tx_timer;
6673 /* Enable the MAC Rx/Tx */
6674 stmmac_mac_set(priv, priv->ioaddr, true);
6676 /* Start Rx & Tx DMA Channels */
6677 stmmac_start_all_dma(priv);
6679 ret = stmmac_request_irq(dev);
6683 /* Enable NAPI process*/
6684 stmmac_enable_all_queues(priv);
6685 netif_carrier_on(dev);
6686 netif_tx_start_all_queues(dev);
6687 stmmac_enable_all_dma_irq(priv);
6692 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
6693 hrtimer_cancel(&priv->dma_conf.tx_queue[chan].txtimer);
6695 stmmac_hw_teardown(dev);
6697 free_dma_desc_resources(priv, &priv->dma_conf);
6702 int stmmac_xsk_wakeup(struct net_device *dev, u32 queue, u32 flags)
6704 struct stmmac_priv *priv = netdev_priv(dev);
6705 struct stmmac_rx_queue *rx_q;
6706 struct stmmac_tx_queue *tx_q;
6707 struct stmmac_channel *ch;
6709 if (test_bit(STMMAC_DOWN, &priv->state) ||
6710 !netif_carrier_ok(priv->dev))
6713 if (!stmmac_xdp_is_enabled(priv))
6716 if (queue >= priv->plat->rx_queues_to_use ||
6717 queue >= priv->plat->tx_queues_to_use)
6720 rx_q = &priv->dma_conf.rx_queue[queue];
6721 tx_q = &priv->dma_conf.tx_queue[queue];
6722 ch = &priv->channel[queue];
6724 if (!rx_q->xsk_pool && !tx_q->xsk_pool)
6727 if (!napi_if_scheduled_mark_missed(&ch->rxtx_napi)) {
6728 /* EQoS does not have per-DMA channel SW interrupt,
6729 * so we schedule RX Napi straight-away.
6731 if (likely(napi_schedule_prep(&ch->rxtx_napi)))
6732 __napi_schedule(&ch->rxtx_napi);
6738 static const struct net_device_ops stmmac_netdev_ops = {
6739 .ndo_open = stmmac_open,
6740 .ndo_start_xmit = stmmac_xmit,
6741 .ndo_stop = stmmac_release,
6742 .ndo_change_mtu = stmmac_change_mtu,
6743 .ndo_fix_features = stmmac_fix_features,
6744 .ndo_set_features = stmmac_set_features,
6745 .ndo_set_rx_mode = stmmac_set_rx_mode,
6746 .ndo_tx_timeout = stmmac_tx_timeout,
6747 .ndo_eth_ioctl = stmmac_ioctl,
6748 .ndo_setup_tc = stmmac_setup_tc,
6749 .ndo_select_queue = stmmac_select_queue,
6750 #ifdef CONFIG_NET_POLL_CONTROLLER
6751 .ndo_poll_controller = stmmac_poll_controller,
6753 .ndo_set_mac_address = stmmac_set_mac_address,
6754 .ndo_vlan_rx_add_vid = stmmac_vlan_rx_add_vid,
6755 .ndo_vlan_rx_kill_vid = stmmac_vlan_rx_kill_vid,
6756 .ndo_bpf = stmmac_bpf,
6757 .ndo_xdp_xmit = stmmac_xdp_xmit,
6758 .ndo_xsk_wakeup = stmmac_xsk_wakeup,
6761 static void stmmac_reset_subtask(struct stmmac_priv *priv)
6763 if (!test_and_clear_bit(STMMAC_RESET_REQUESTED, &priv->state))
6765 if (test_bit(STMMAC_DOWN, &priv->state))
6768 netdev_err(priv->dev, "Reset adapter.\n");
6771 netif_trans_update(priv->dev);
6772 while (test_and_set_bit(STMMAC_RESETING, &priv->state))
6773 usleep_range(1000, 2000);
6775 set_bit(STMMAC_DOWN, &priv->state);
6776 dev_close(priv->dev);
6777 dev_open(priv->dev, NULL);
6778 clear_bit(STMMAC_DOWN, &priv->state);
6779 clear_bit(STMMAC_RESETING, &priv->state);
6783 static void stmmac_service_task(struct work_struct *work)
6785 struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
6788 stmmac_reset_subtask(priv);
6789 clear_bit(STMMAC_SERVICE_SCHED, &priv->state);
6793 * stmmac_hw_init - Init the MAC device
6794 * @priv: driver private structure
6795 * Description: this function is to configure the MAC device according to
6796 * some platform parameters or the HW capability register. It prepares the
6797 * driver to use either ring or chain modes and to setup either enhanced or
6798 * normal descriptors.
6800 static int stmmac_hw_init(struct stmmac_priv *priv)
6804 /* dwmac-sun8i only work in chain mode */
6805 if (priv->plat->has_sun8i)
6807 priv->chain_mode = chain_mode;
6809 /* Initialize HW Interface */
6810 ret = stmmac_hwif_init(priv);
6814 /* Get the HW capability (new GMAC newer than 3.50a) */
6815 priv->hw_cap_support = stmmac_get_hw_features(priv);
6816 if (priv->hw_cap_support) {
6817 dev_info(priv->device, "DMA HW capability register supported\n");
6819 /* We can override some gmac/dma configuration fields: e.g.
6820 * enh_desc, tx_coe (e.g. that are passed through the
6821 * platform) with the values from the HW capability
6822 * register (if supported).
6824 priv->plat->enh_desc = priv->dma_cap.enh_desc;
6825 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up &&
6826 !priv->plat->use_phy_wol;
6827 priv->hw->pmt = priv->plat->pmt;
6828 if (priv->dma_cap.hash_tb_sz) {
6829 priv->hw->multicast_filter_bins =
6830 (BIT(priv->dma_cap.hash_tb_sz) << 5);
6831 priv->hw->mcast_bits_log2 =
6832 ilog2(priv->hw->multicast_filter_bins);
6835 /* TXCOE doesn't work in thresh DMA mode */
6836 if (priv->plat->force_thresh_dma_mode)
6837 priv->plat->tx_coe = 0;
6839 priv->plat->tx_coe = priv->dma_cap.tx_coe;
6841 /* In case of GMAC4 rx_coe is from HW cap register. */
6842 priv->plat->rx_coe = priv->dma_cap.rx_coe;
6844 if (priv->dma_cap.rx_coe_type2)
6845 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
6846 else if (priv->dma_cap.rx_coe_type1)
6847 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
6850 dev_info(priv->device, "No HW DMA feature register supported\n");
6853 if (priv->plat->rx_coe) {
6854 priv->hw->rx_csum = priv->plat->rx_coe;
6855 dev_info(priv->device, "RX Checksum Offload Engine supported\n");
6856 if (priv->synopsys_id < DWMAC_CORE_4_00)
6857 dev_info(priv->device, "COE Type %d\n", priv->hw->rx_csum);
6859 if (priv->plat->tx_coe)
6860 dev_info(priv->device, "TX Checksum insertion supported\n");
6862 if (priv->plat->pmt) {
6863 dev_info(priv->device, "Wake-Up On Lan supported\n");
6864 device_set_wakeup_capable(priv->device, 1);
6867 if (priv->dma_cap.tsoen)
6868 dev_info(priv->device, "TSO supported\n");
6870 priv->hw->vlan_fail_q_en = priv->plat->vlan_fail_q_en;
6871 priv->hw->vlan_fail_q = priv->plat->vlan_fail_q;
6873 /* Run HW quirks, if any */
6874 if (priv->hwif_quirks) {
6875 ret = priv->hwif_quirks(priv);
6880 /* Rx Watchdog is available in the COREs newer than the 3.40.
6881 * In some case, for example on bugged HW this feature
6882 * has to be disable and this can be done by passing the
6883 * riwt_off field from the platform.
6885 if (((priv->synopsys_id >= DWMAC_CORE_3_50) ||
6886 (priv->plat->has_xgmac)) && (!priv->plat->riwt_off)) {
6888 dev_info(priv->device,
6889 "Enable RX Mitigation via HW Watchdog Timer\n");
6895 static void stmmac_napi_add(struct net_device *dev)
6897 struct stmmac_priv *priv = netdev_priv(dev);
6900 maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
6902 for (queue = 0; queue < maxq; queue++) {
6903 struct stmmac_channel *ch = &priv->channel[queue];
6905 ch->priv_data = priv;
6907 spin_lock_init(&ch->lock);
6909 if (queue < priv->plat->rx_queues_to_use) {
6910 netif_napi_add(dev, &ch->rx_napi, stmmac_napi_poll_rx);
6912 if (queue < priv->plat->tx_queues_to_use) {
6913 netif_napi_add_tx(dev, &ch->tx_napi,
6914 stmmac_napi_poll_tx);
6916 if (queue < priv->plat->rx_queues_to_use &&
6917 queue < priv->plat->tx_queues_to_use) {
6918 netif_napi_add(dev, &ch->rxtx_napi,
6919 stmmac_napi_poll_rxtx);
6924 static void stmmac_napi_del(struct net_device *dev)
6926 struct stmmac_priv *priv = netdev_priv(dev);
6929 maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
6931 for (queue = 0; queue < maxq; queue++) {
6932 struct stmmac_channel *ch = &priv->channel[queue];
6934 if (queue < priv->plat->rx_queues_to_use)
6935 netif_napi_del(&ch->rx_napi);
6936 if (queue < priv->plat->tx_queues_to_use)
6937 netif_napi_del(&ch->tx_napi);
6938 if (queue < priv->plat->rx_queues_to_use &&
6939 queue < priv->plat->tx_queues_to_use) {
6940 netif_napi_del(&ch->rxtx_napi);
6945 int stmmac_reinit_queues(struct net_device *dev, u32 rx_cnt, u32 tx_cnt)
6947 struct stmmac_priv *priv = netdev_priv(dev);
6950 if (netif_running(dev))
6951 stmmac_release(dev);
6953 stmmac_napi_del(dev);
6955 priv->plat->rx_queues_to_use = rx_cnt;
6956 priv->plat->tx_queues_to_use = tx_cnt;
6958 stmmac_napi_add(dev);
6960 if (netif_running(dev))
6961 ret = stmmac_open(dev);
6966 int stmmac_reinit_ringparam(struct net_device *dev, u32 rx_size, u32 tx_size)
6968 struct stmmac_priv *priv = netdev_priv(dev);
6971 if (netif_running(dev))
6972 stmmac_release(dev);
6974 priv->dma_conf.dma_rx_size = rx_size;
6975 priv->dma_conf.dma_tx_size = tx_size;
6977 if (netif_running(dev))
6978 ret = stmmac_open(dev);
6983 #define SEND_VERIFY_MPAKCET_FMT "Send Verify mPacket lo_state=%d lp_state=%d\n"
6984 static void stmmac_fpe_lp_task(struct work_struct *work)
6986 struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
6988 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
6989 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
6990 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
6991 bool *hs_enable = &fpe_cfg->hs_enable;
6992 bool *enable = &fpe_cfg->enable;
6995 while (retries-- > 0) {
6996 /* Bail out immediately if FPE handshake is OFF */
6997 if (*lo_state == FPE_STATE_OFF || !*hs_enable)
7000 if (*lo_state == FPE_STATE_ENTERING_ON &&
7001 *lp_state == FPE_STATE_ENTERING_ON) {
7002 stmmac_fpe_configure(priv, priv->ioaddr,
7003 priv->plat->tx_queues_to_use,
7004 priv->plat->rx_queues_to_use,
7007 netdev_info(priv->dev, "configured FPE\n");
7009 *lo_state = FPE_STATE_ON;
7010 *lp_state = FPE_STATE_ON;
7011 netdev_info(priv->dev, "!!! BOTH FPE stations ON\n");
7015 if ((*lo_state == FPE_STATE_CAPABLE ||
7016 *lo_state == FPE_STATE_ENTERING_ON) &&
7017 *lp_state != FPE_STATE_ON) {
7018 netdev_info(priv->dev, SEND_VERIFY_MPAKCET_FMT,
7019 *lo_state, *lp_state);
7020 stmmac_fpe_send_mpacket(priv, priv->ioaddr,
7023 /* Sleep then retry */
7027 clear_bit(__FPE_TASK_SCHED, &priv->fpe_task_state);
7030 void stmmac_fpe_handshake(struct stmmac_priv *priv, bool enable)
7032 if (priv->plat->fpe_cfg->hs_enable != enable) {
7034 stmmac_fpe_send_mpacket(priv, priv->ioaddr,
7037 priv->plat->fpe_cfg->lo_fpe_state = FPE_STATE_OFF;
7038 priv->plat->fpe_cfg->lp_fpe_state = FPE_STATE_OFF;
7041 priv->plat->fpe_cfg->hs_enable = enable;
7047 * @device: device pointer
7048 * @plat_dat: platform data pointer
7049 * @res: stmmac resource pointer
7050 * Description: this is the main probe function used to
7051 * call the alloc_etherdev, allocate the priv structure.
7053 * returns 0 on success, otherwise errno.
7055 int stmmac_dvr_probe(struct device *device,
7056 struct plat_stmmacenet_data *plat_dat,
7057 struct stmmac_resources *res)
7059 struct net_device *ndev = NULL;
7060 struct stmmac_priv *priv;
7064 ndev = devm_alloc_etherdev_mqs(device, sizeof(struct stmmac_priv),
7065 MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES);
7069 SET_NETDEV_DEV(ndev, device);
7071 priv = netdev_priv(ndev);
7072 priv->device = device;
7075 stmmac_set_ethtool_ops(ndev);
7076 priv->pause = pause;
7077 priv->plat = plat_dat;
7078 priv->ioaddr = res->addr;
7079 priv->dev->base_addr = (unsigned long)res->addr;
7080 priv->plat->dma_cfg->multi_msi_en = priv->plat->multi_msi_en;
7082 priv->dev->irq = res->irq;
7083 priv->wol_irq = res->wol_irq;
7084 priv->lpi_irq = res->lpi_irq;
7085 priv->sfty_ce_irq = res->sfty_ce_irq;
7086 priv->sfty_ue_irq = res->sfty_ue_irq;
7087 for (i = 0; i < MTL_MAX_RX_QUEUES; i++)
7088 priv->rx_irq[i] = res->rx_irq[i];
7089 for (i = 0; i < MTL_MAX_TX_QUEUES; i++)
7090 priv->tx_irq[i] = res->tx_irq[i];
7092 if (!is_zero_ether_addr(res->mac))
7093 eth_hw_addr_set(priv->dev, res->mac);
7095 dev_set_drvdata(device, priv->dev);
7097 /* Verify driver arguments */
7098 stmmac_verify_args();
7100 priv->af_xdp_zc_qps = bitmap_zalloc(MTL_MAX_TX_QUEUES, GFP_KERNEL);
7101 if (!priv->af_xdp_zc_qps)
7104 /* Allocate workqueue */
7105 priv->wq = create_singlethread_workqueue("stmmac_wq");
7107 dev_err(priv->device, "failed to create workqueue\n");
7112 INIT_WORK(&priv->service_task, stmmac_service_task);
7114 /* Initialize Link Partner FPE workqueue */
7115 INIT_WORK(&priv->fpe_task, stmmac_fpe_lp_task);
7117 /* Override with kernel parameters if supplied XXX CRS XXX
7118 * this needs to have multiple instances
7120 if ((phyaddr >= 0) && (phyaddr <= 31))
7121 priv->plat->phy_addr = phyaddr;
7123 if (priv->plat->stmmac_rst) {
7124 ret = reset_control_assert(priv->plat->stmmac_rst);
7125 reset_control_deassert(priv->plat->stmmac_rst);
7126 /* Some reset controllers have only reset callback instead of
7127 * assert + deassert callbacks pair.
7129 if (ret == -ENOTSUPP)
7130 reset_control_reset(priv->plat->stmmac_rst);
7133 ret = reset_control_deassert(priv->plat->stmmac_ahb_rst);
7134 if (ret == -ENOTSUPP)
7135 dev_err(priv->device, "unable to bring out of ahb reset: %pe\n",
7138 /* Init MAC and get the capabilities */
7139 ret = stmmac_hw_init(priv);
7143 /* Only DWMAC core version 5.20 onwards supports HW descriptor prefetch.
7145 if (priv->synopsys_id < DWMAC_CORE_5_20)
7146 priv->plat->dma_cfg->dche = false;
7148 stmmac_check_ether_addr(priv);
7150 ndev->netdev_ops = &stmmac_netdev_ops;
7152 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
7155 ret = stmmac_tc_init(priv, priv);
7157 ndev->hw_features |= NETIF_F_HW_TC;
7160 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
7161 ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
7162 if (priv->plat->has_gmac4)
7163 ndev->hw_features |= NETIF_F_GSO_UDP_L4;
7165 dev_info(priv->device, "TSO feature enabled\n");
7168 if (priv->dma_cap.sphen && !priv->plat->sph_disable) {
7169 ndev->hw_features |= NETIF_F_GRO;
7170 priv->sph_cap = true;
7171 priv->sph = priv->sph_cap;
7172 dev_info(priv->device, "SPH feature enabled\n");
7175 /* The current IP register MAC_HW_Feature1[ADDR64] only define
7176 * 32/40/64 bit width, but some SOC support others like i.MX8MP
7177 * support 34 bits but it map to 40 bits width in MAC_HW_Feature1[ADDR64].
7178 * So overwrite dma_cap.addr64 according to HW real design.
7180 if (priv->plat->addr64)
7181 priv->dma_cap.addr64 = priv->plat->addr64;
7183 if (priv->dma_cap.addr64) {
7184 ret = dma_set_mask_and_coherent(device,
7185 DMA_BIT_MASK(priv->dma_cap.addr64));
7187 dev_info(priv->device, "Using %d bits DMA width\n",
7188 priv->dma_cap.addr64);
7191 * If more than 32 bits can be addressed, make sure to
7192 * enable enhanced addressing mode.
7194 if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
7195 priv->plat->dma_cfg->eame = true;
7197 ret = dma_set_mask_and_coherent(device, DMA_BIT_MASK(32));
7199 dev_err(priv->device, "Failed to set DMA Mask\n");
7203 priv->dma_cap.addr64 = 32;
7207 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
7208 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
7209 #ifdef STMMAC_VLAN_TAG_USED
7210 /* Both mac100 and gmac support receive VLAN tag detection */
7211 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX;
7212 if (priv->dma_cap.vlhash) {
7213 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
7214 ndev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
7216 if (priv->dma_cap.vlins) {
7217 ndev->features |= NETIF_F_HW_VLAN_CTAG_TX;
7218 if (priv->dma_cap.dvlan)
7219 ndev->features |= NETIF_F_HW_VLAN_STAG_TX;
7222 priv->msg_enable = netif_msg_init(debug, default_msg_level);
7224 /* Initialize RSS */
7225 rxq = priv->plat->rx_queues_to_use;
7226 netdev_rss_key_fill(priv->rss.key, sizeof(priv->rss.key));
7227 for (i = 0; i < ARRAY_SIZE(priv->rss.table); i++)
7228 priv->rss.table[i] = ethtool_rxfh_indir_default(i, rxq);
7230 if (priv->dma_cap.rssen && priv->plat->rss_en)
7231 ndev->features |= NETIF_F_RXHASH;
7233 /* MTU range: 46 - hw-specific max */
7234 ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
7235 if (priv->plat->has_xgmac)
7236 ndev->max_mtu = XGMAC_JUMBO_LEN;
7237 else if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
7238 ndev->max_mtu = JUMBO_LEN;
7240 ndev->max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
7241 /* Will not overwrite ndev->max_mtu if plat->maxmtu > ndev->max_mtu
7242 * as well as plat->maxmtu < ndev->min_mtu which is a invalid range.
7244 if ((priv->plat->maxmtu < ndev->max_mtu) &&
7245 (priv->plat->maxmtu >= ndev->min_mtu))
7246 ndev->max_mtu = priv->plat->maxmtu;
7247 else if (priv->plat->maxmtu < ndev->min_mtu)
7248 dev_warn(priv->device,
7249 "%s: warning: maxmtu having invalid value (%d)\n",
7250 __func__, priv->plat->maxmtu);
7253 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
7255 /* Setup channels NAPI */
7256 stmmac_napi_add(ndev);
7258 mutex_init(&priv->lock);
7260 /* If a specific clk_csr value is passed from the platform
7261 * this means that the CSR Clock Range selection cannot be
7262 * changed at run-time and it is fixed. Viceversa the driver'll try to
7263 * set the MDC clock dynamically according to the csr actual
7266 if (priv->plat->clk_csr >= 0)
7267 priv->clk_csr = priv->plat->clk_csr;
7269 stmmac_clk_csr_set(priv);
7271 stmmac_check_pcs_mode(priv);
7273 pm_runtime_get_noresume(device);
7274 pm_runtime_set_active(device);
7275 if (!pm_runtime_enabled(device))
7276 pm_runtime_enable(device);
7278 if (priv->hw->pcs != STMMAC_PCS_TBI &&
7279 priv->hw->pcs != STMMAC_PCS_RTBI) {
7280 /* MDIO bus Registration */
7281 ret = stmmac_mdio_register(ndev);
7283 dev_err_probe(priv->device, ret,
7284 "%s: MDIO bus (id: %d) registration failed\n",
7285 __func__, priv->plat->bus_id);
7286 goto error_mdio_register;
7290 if (priv->plat->speed_mode_2500)
7291 priv->plat->speed_mode_2500(ndev, priv->plat->bsp_priv);
7293 if (priv->plat->mdio_bus_data && priv->plat->mdio_bus_data->has_xpcs) {
7294 ret = stmmac_xpcs_setup(priv->mii);
7296 goto error_xpcs_setup;
7299 ret = stmmac_phy_setup(priv);
7301 netdev_err(ndev, "failed to setup phy (%d)\n", ret);
7302 goto error_phy_setup;
7305 ret = register_netdev(ndev);
7307 dev_err(priv->device, "%s: ERROR %i registering the device\n",
7309 goto error_netdev_register;
7312 #ifdef CONFIG_DEBUG_FS
7313 stmmac_init_fs(ndev);
7316 if (priv->plat->dump_debug_regs)
7317 priv->plat->dump_debug_regs(priv->plat->bsp_priv);
7319 /* Let pm_runtime_put() disable the clocks.
7320 * If CONFIG_PM is not enabled, the clocks will stay powered.
7322 pm_runtime_put(device);
7326 error_netdev_register:
7327 phylink_destroy(priv->phylink);
7330 if (priv->hw->pcs != STMMAC_PCS_TBI &&
7331 priv->hw->pcs != STMMAC_PCS_RTBI)
7332 stmmac_mdio_unregister(ndev);
7333 error_mdio_register:
7334 stmmac_napi_del(ndev);
7336 destroy_workqueue(priv->wq);
7338 bitmap_free(priv->af_xdp_zc_qps);
7342 EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
7346 * @dev: device pointer
7347 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
7348 * changes the link status, releases the DMA descriptor rings.
7350 int stmmac_dvr_remove(struct device *dev)
7352 struct net_device *ndev = dev_get_drvdata(dev);
7353 struct stmmac_priv *priv = netdev_priv(ndev);
7355 netdev_info(priv->dev, "%s: removing driver", __func__);
7357 pm_runtime_get_sync(dev);
7359 stmmac_stop_all_dma(priv);
7360 stmmac_mac_set(priv, priv->ioaddr, false);
7361 netif_carrier_off(ndev);
7362 unregister_netdev(ndev);
7364 /* Serdes power down needs to happen after VLAN filter
7365 * is deleted that is triggered by unregister_netdev().
7367 if (priv->plat->serdes_powerdown)
7368 priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv);
7370 #ifdef CONFIG_DEBUG_FS
7371 stmmac_exit_fs(ndev);
7373 phylink_destroy(priv->phylink);
7374 if (priv->plat->stmmac_rst)
7375 reset_control_assert(priv->plat->stmmac_rst);
7376 reset_control_assert(priv->plat->stmmac_ahb_rst);
7377 if (priv->hw->pcs != STMMAC_PCS_TBI &&
7378 priv->hw->pcs != STMMAC_PCS_RTBI)
7379 stmmac_mdio_unregister(ndev);
7380 destroy_workqueue(priv->wq);
7381 mutex_destroy(&priv->lock);
7382 bitmap_free(priv->af_xdp_zc_qps);
7384 pm_runtime_disable(dev);
7385 pm_runtime_put_noidle(dev);
7389 EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
7392 * stmmac_suspend - suspend callback
7393 * @dev: device pointer
7394 * Description: this is the function to suspend the device and it is called
7395 * by the platform driver to stop the network queue, release the resources,
7396 * program the PMT register (for WoL), clean and release driver resources.
7398 int stmmac_suspend(struct device *dev)
7400 struct net_device *ndev = dev_get_drvdata(dev);
7401 struct stmmac_priv *priv = netdev_priv(ndev);
7404 if (!ndev || !netif_running(ndev))
7407 mutex_lock(&priv->lock);
7409 netif_device_detach(ndev);
7411 stmmac_disable_all_queues(priv);
7413 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
7414 hrtimer_cancel(&priv->dma_conf.tx_queue[chan].txtimer);
7416 if (priv->eee_enabled) {
7417 priv->tx_path_in_lpi_mode = false;
7418 del_timer_sync(&priv->eee_ctrl_timer);
7421 /* Stop TX/RX DMA */
7422 stmmac_stop_all_dma(priv);
7424 if (priv->plat->serdes_powerdown)
7425 priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv);
7427 /* Enable Power down mode by programming the PMT regs */
7428 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7429 stmmac_pmt(priv, priv->hw, priv->wolopts);
7432 stmmac_mac_set(priv, priv->ioaddr, false);
7433 pinctrl_pm_select_sleep_state(priv->device);
7436 mutex_unlock(&priv->lock);
7439 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7440 phylink_suspend(priv->phylink, true);
7442 if (device_may_wakeup(priv->device))
7443 phylink_speed_down(priv->phylink, false);
7444 phylink_suspend(priv->phylink, false);
7448 if (priv->dma_cap.fpesel) {
7450 stmmac_fpe_configure(priv, priv->ioaddr,
7451 priv->plat->tx_queues_to_use,
7452 priv->plat->rx_queues_to_use, false);
7454 stmmac_fpe_handshake(priv, false);
7455 stmmac_fpe_stop_wq(priv);
7458 priv->speed = SPEED_UNKNOWN;
7461 EXPORT_SYMBOL_GPL(stmmac_suspend);
7463 static void stmmac_reset_rx_queue(struct stmmac_priv *priv, u32 queue)
7465 struct stmmac_rx_queue *rx_q = &priv->dma_conf.rx_queue[queue];
7471 static void stmmac_reset_tx_queue(struct stmmac_priv *priv, u32 queue)
7473 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[queue];
7479 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
7483 * stmmac_reset_queues_param - reset queue parameters
7484 * @priv: device pointer
7486 static void stmmac_reset_queues_param(struct stmmac_priv *priv)
7488 u32 rx_cnt = priv->plat->rx_queues_to_use;
7489 u32 tx_cnt = priv->plat->tx_queues_to_use;
7492 for (queue = 0; queue < rx_cnt; queue++)
7493 stmmac_reset_rx_queue(priv, queue);
7495 for (queue = 0; queue < tx_cnt; queue++)
7496 stmmac_reset_tx_queue(priv, queue);
7500 * stmmac_resume - resume callback
7501 * @dev: device pointer
7502 * Description: when resume this function is invoked to setup the DMA and CORE
7503 * in a usable state.
7505 int stmmac_resume(struct device *dev)
7507 struct net_device *ndev = dev_get_drvdata(dev);
7508 struct stmmac_priv *priv = netdev_priv(ndev);
7511 if (!netif_running(ndev))
7514 /* Power Down bit, into the PM register, is cleared
7515 * automatically as soon as a magic packet or a Wake-up frame
7516 * is received. Anyway, it's better to manually clear
7517 * this bit because it can generate problems while resuming
7518 * from another devices (e.g. serial console).
7520 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7521 mutex_lock(&priv->lock);
7522 stmmac_pmt(priv, priv->hw, 0);
7523 mutex_unlock(&priv->lock);
7526 pinctrl_pm_select_default_state(priv->device);
7527 /* reset the phy so that it's ready */
7529 stmmac_mdio_reset(priv->mii);
7532 if (!priv->plat->serdes_up_after_phy_linkup && priv->plat->serdes_powerup) {
7533 ret = priv->plat->serdes_powerup(ndev,
7534 priv->plat->bsp_priv);
7541 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7542 phylink_resume(priv->phylink);
7544 phylink_resume(priv->phylink);
7545 if (device_may_wakeup(priv->device))
7546 phylink_speed_up(priv->phylink);
7551 mutex_lock(&priv->lock);
7553 stmmac_reset_queues_param(priv);
7555 stmmac_free_tx_skbufs(priv);
7556 stmmac_clear_descriptors(priv, &priv->dma_conf);
7558 stmmac_hw_setup(ndev, false);
7559 stmmac_init_coalesce(priv);
7560 stmmac_set_rx_mode(ndev);
7562 stmmac_restore_hw_vlan_rx_fltr(priv, ndev, priv->hw);
7564 stmmac_enable_all_queues(priv);
7565 stmmac_enable_all_dma_irq(priv);
7567 mutex_unlock(&priv->lock);
7570 netif_device_attach(ndev);
7574 EXPORT_SYMBOL_GPL(stmmac_resume);
7577 static int __init stmmac_cmdline_opt(char *str)
7583 while ((opt = strsep(&str, ",")) != NULL) {
7584 if (!strncmp(opt, "debug:", 6)) {
7585 if (kstrtoint(opt + 6, 0, &debug))
7587 } else if (!strncmp(opt, "phyaddr:", 8)) {
7588 if (kstrtoint(opt + 8, 0, &phyaddr))
7590 } else if (!strncmp(opt, "buf_sz:", 7)) {
7591 if (kstrtoint(opt + 7, 0, &buf_sz))
7593 } else if (!strncmp(opt, "tc:", 3)) {
7594 if (kstrtoint(opt + 3, 0, &tc))
7596 } else if (!strncmp(opt, "watchdog:", 9)) {
7597 if (kstrtoint(opt + 9, 0, &watchdog))
7599 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
7600 if (kstrtoint(opt + 10, 0, &flow_ctrl))
7602 } else if (!strncmp(opt, "pause:", 6)) {
7603 if (kstrtoint(opt + 6, 0, &pause))
7605 } else if (!strncmp(opt, "eee_timer:", 10)) {
7606 if (kstrtoint(opt + 10, 0, &eee_timer))
7608 } else if (!strncmp(opt, "chain_mode:", 11)) {
7609 if (kstrtoint(opt + 11, 0, &chain_mode))
7616 pr_err("%s: ERROR broken module parameter conversion", __func__);
7620 __setup("stmmaceth=", stmmac_cmdline_opt);
7623 static int __init stmmac_init(void)
7625 #ifdef CONFIG_DEBUG_FS
7626 /* Create debugfs main directory if it doesn't exist yet */
7628 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
7629 register_netdevice_notifier(&stmmac_notifier);
7635 static void __exit stmmac_exit(void)
7637 #ifdef CONFIG_DEBUG_FS
7638 unregister_netdevice_notifier(&stmmac_notifier);
7639 debugfs_remove_recursive(stmmac_fs_dir);
7643 module_init(stmmac_init)
7644 module_exit(stmmac_exit)
7646 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
7647 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
7648 MODULE_LICENSE("GPL");