xref: /linux/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c (revision 7a1d55b987dfcbddecdb67eecc76fe555d4348ba)
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.
5 
6 	Copyright(C) 2007-2011 STMicroelectronics Ltd
7 
8 
9   Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
10 
11   Documentation available at:
12 	http://www.stlinux.com
13   Support available at:
14 	https://bugzilla.stlinux.com/
15 *******************************************************************************/
16 
17 #include <linux/clk.h>
18 #include <linux/kernel.h>
19 #include <linux/interrupt.h>
20 #include <linux/ip.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>
27 #include <linux/if.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"
45 #include "stmmac.h"
46 #include "stmmac_xdp.h"
47 #include <linux/reset.h>
48 #include <linux/of_mdio.h>
49 #include "dwmac1000.h"
50 #include "dwxgmac2.h"
51 #include "hwif.h"
52 
53 #define	STMMAC_ALIGN(x)		ALIGN(ALIGN(x, SMP_CACHE_BYTES), 16)
54 #define	TSO_MAX_BUFF_SIZE	(SZ_16K - 1)
55 
56 /* Module parameters */
57 #define TX_TIMEO	5000
58 static int watchdog = TX_TIMEO;
59 module_param(watchdog, int, 0644);
60 MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
61 
62 static int debug = -1;
63 module_param(debug, int, 0644);
64 MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
65 
66 static int phyaddr = -1;
67 module_param(phyaddr, int, 0444);
68 MODULE_PARM_DESC(phyaddr, "Physical device address");
69 
70 #define STMMAC_TX_THRESH(x)	((x)->dma_tx_size / 4)
71 #define STMMAC_RX_THRESH(x)	((x)->dma_rx_size / 4)
72 
73 /* Limit to make sure XDP TX and slow path can coexist */
74 #define STMMAC_XSK_TX_BUDGET_MAX	256
75 #define STMMAC_TX_XSK_AVAIL		16
76 #define STMMAC_RX_FILL_BATCH		16
77 
78 #define STMMAC_XDP_PASS		0
79 #define STMMAC_XDP_CONSUMED	BIT(0)
80 #define STMMAC_XDP_TX		BIT(1)
81 #define STMMAC_XDP_REDIRECT	BIT(2)
82 
83 static int flow_ctrl = FLOW_AUTO;
84 module_param(flow_ctrl, int, 0644);
85 MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
86 
87 static int pause = PAUSE_TIME;
88 module_param(pause, int, 0644);
89 MODULE_PARM_DESC(pause, "Flow Control Pause Time");
90 
91 #define TC_DEFAULT 64
92 static int tc = TC_DEFAULT;
93 module_param(tc, int, 0644);
94 MODULE_PARM_DESC(tc, "DMA threshold control value");
95 
96 #define	DEFAULT_BUFSIZE	1536
97 static int buf_sz = DEFAULT_BUFSIZE;
98 module_param(buf_sz, int, 0644);
99 MODULE_PARM_DESC(buf_sz, "DMA buffer size");
100 
101 #define	STMMAC_RX_COPYBREAK	256
102 
103 static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
104 				      NETIF_MSG_LINK | NETIF_MSG_IFUP |
105 				      NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
106 
107 #define STMMAC_DEFAULT_LPI_TIMER	1000
108 static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
109 module_param(eee_timer, int, 0644);
110 MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
111 #define STMMAC_LPI_T(x) (jiffies + usecs_to_jiffies(x))
112 
113 /* By default the driver will use the ring mode to manage tx and rx descriptors,
114  * but allow user to force to use the chain instead of the ring
115  */
116 static unsigned int chain_mode;
117 module_param(chain_mode, int, 0444);
118 MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
119 
120 static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
121 /* For MSI interrupts handling */
122 static irqreturn_t stmmac_mac_interrupt(int irq, void *dev_id);
123 static irqreturn_t stmmac_safety_interrupt(int irq, void *dev_id);
124 static irqreturn_t stmmac_msi_intr_tx(int irq, void *data);
125 static irqreturn_t stmmac_msi_intr_rx(int irq, void *data);
126 static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue);
127 static void stmmac_flush_tx_descriptors(struct stmmac_priv *priv, int queue);
128 
129 #ifdef CONFIG_DEBUG_FS
130 static const struct net_device_ops stmmac_netdev_ops;
131 static void stmmac_init_fs(struct net_device *dev);
132 static void stmmac_exit_fs(struct net_device *dev);
133 #endif
134 
135 #define STMMAC_COAL_TIMER(x) (ns_to_ktime((x) * NSEC_PER_USEC))
136 
137 int stmmac_bus_clks_config(struct stmmac_priv *priv, bool enabled)
138 {
139 	int ret = 0;
140 
141 	if (enabled) {
142 		ret = clk_prepare_enable(priv->plat->stmmac_clk);
143 		if (ret)
144 			return ret;
145 		ret = clk_prepare_enable(priv->plat->pclk);
146 		if (ret) {
147 			clk_disable_unprepare(priv->plat->stmmac_clk);
148 			return ret;
149 		}
150 		if (priv->plat->clks_config) {
151 			ret = priv->plat->clks_config(priv->plat->bsp_priv, enabled);
152 			if (ret) {
153 				clk_disable_unprepare(priv->plat->stmmac_clk);
154 				clk_disable_unprepare(priv->plat->pclk);
155 				return ret;
156 			}
157 		}
158 	} else {
159 		clk_disable_unprepare(priv->plat->stmmac_clk);
160 		clk_disable_unprepare(priv->plat->pclk);
161 		if (priv->plat->clks_config)
162 			priv->plat->clks_config(priv->plat->bsp_priv, enabled);
163 	}
164 
165 	return ret;
166 }
167 EXPORT_SYMBOL_GPL(stmmac_bus_clks_config);
168 
169 /**
170  * stmmac_verify_args - verify the driver parameters.
171  * Description: it checks the driver parameters and set a default in case of
172  * errors.
173  */
174 static void stmmac_verify_args(void)
175 {
176 	if (unlikely(watchdog < 0))
177 		watchdog = TX_TIMEO;
178 	if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
179 		buf_sz = DEFAULT_BUFSIZE;
180 	if (unlikely(flow_ctrl > 1))
181 		flow_ctrl = FLOW_AUTO;
182 	else if (likely(flow_ctrl < 0))
183 		flow_ctrl = FLOW_OFF;
184 	if (unlikely((pause < 0) || (pause > 0xffff)))
185 		pause = PAUSE_TIME;
186 	if (eee_timer < 0)
187 		eee_timer = STMMAC_DEFAULT_LPI_TIMER;
188 }
189 
190 static void __stmmac_disable_all_queues(struct stmmac_priv *priv)
191 {
192 	u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
193 	u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
194 	u32 maxq = max(rx_queues_cnt, tx_queues_cnt);
195 	u32 queue;
196 
197 	for (queue = 0; queue < maxq; queue++) {
198 		struct stmmac_channel *ch = &priv->channel[queue];
199 
200 		if (stmmac_xdp_is_enabled(priv) &&
201 		    test_bit(queue, priv->af_xdp_zc_qps)) {
202 			napi_disable(&ch->rxtx_napi);
203 			continue;
204 		}
205 
206 		if (queue < rx_queues_cnt)
207 			napi_disable(&ch->rx_napi);
208 		if (queue < tx_queues_cnt)
209 			napi_disable(&ch->tx_napi);
210 	}
211 }
212 
213 /**
214  * stmmac_disable_all_queues - Disable all queues
215  * @priv: driver private structure
216  */
217 static void stmmac_disable_all_queues(struct stmmac_priv *priv)
218 {
219 	u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
220 	struct stmmac_rx_queue *rx_q;
221 	u32 queue;
222 
223 	/* synchronize_rcu() needed for pending XDP buffers to drain */
224 	for (queue = 0; queue < rx_queues_cnt; queue++) {
225 		rx_q = &priv->rx_queue[queue];
226 		if (rx_q->xsk_pool) {
227 			synchronize_rcu();
228 			break;
229 		}
230 	}
231 
232 	__stmmac_disable_all_queues(priv);
233 }
234 
235 /**
236  * stmmac_enable_all_queues - Enable all queues
237  * @priv: driver private structure
238  */
239 static void stmmac_enable_all_queues(struct stmmac_priv *priv)
240 {
241 	u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
242 	u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
243 	u32 maxq = max(rx_queues_cnt, tx_queues_cnt);
244 	u32 queue;
245 
246 	for (queue = 0; queue < maxq; queue++) {
247 		struct stmmac_channel *ch = &priv->channel[queue];
248 
249 		if (stmmac_xdp_is_enabled(priv) &&
250 		    test_bit(queue, priv->af_xdp_zc_qps)) {
251 			napi_enable(&ch->rxtx_napi);
252 			continue;
253 		}
254 
255 		if (queue < rx_queues_cnt)
256 			napi_enable(&ch->rx_napi);
257 		if (queue < tx_queues_cnt)
258 			napi_enable(&ch->tx_napi);
259 	}
260 }
261 
262 static void stmmac_service_event_schedule(struct stmmac_priv *priv)
263 {
264 	if (!test_bit(STMMAC_DOWN, &priv->state) &&
265 	    !test_and_set_bit(STMMAC_SERVICE_SCHED, &priv->state))
266 		queue_work(priv->wq, &priv->service_task);
267 }
268 
269 static void stmmac_global_err(struct stmmac_priv *priv)
270 {
271 	netif_carrier_off(priv->dev);
272 	set_bit(STMMAC_RESET_REQUESTED, &priv->state);
273 	stmmac_service_event_schedule(priv);
274 }
275 
276 /**
277  * stmmac_clk_csr_set - dynamically set the MDC clock
278  * @priv: driver private structure
279  * Description: this is to dynamically set the MDC clock according to the csr
280  * clock input.
281  * Note:
282  *	If a specific clk_csr value is passed from the platform
283  *	this means that the CSR Clock Range selection cannot be
284  *	changed at run-time and it is fixed (as reported in the driver
285  *	documentation). Viceversa the driver will try to set the MDC
286  *	clock dynamically according to the actual clock input.
287  */
288 static void stmmac_clk_csr_set(struct stmmac_priv *priv)
289 {
290 	u32 clk_rate;
291 
292 	clk_rate = clk_get_rate(priv->plat->stmmac_clk);
293 
294 	/* Platform provided default clk_csr would be assumed valid
295 	 * for all other cases except for the below mentioned ones.
296 	 * For values higher than the IEEE 802.3 specified frequency
297 	 * we can not estimate the proper divider as it is not known
298 	 * the frequency of clk_csr_i. So we do not change the default
299 	 * divider.
300 	 */
301 	if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
302 		if (clk_rate < CSR_F_35M)
303 			priv->clk_csr = STMMAC_CSR_20_35M;
304 		else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
305 			priv->clk_csr = STMMAC_CSR_35_60M;
306 		else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
307 			priv->clk_csr = STMMAC_CSR_60_100M;
308 		else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
309 			priv->clk_csr = STMMAC_CSR_100_150M;
310 		else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
311 			priv->clk_csr = STMMAC_CSR_150_250M;
312 		else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
313 			priv->clk_csr = STMMAC_CSR_250_300M;
314 	}
315 
316 	if (priv->plat->has_sun8i) {
317 		if (clk_rate > 160000000)
318 			priv->clk_csr = 0x03;
319 		else if (clk_rate > 80000000)
320 			priv->clk_csr = 0x02;
321 		else if (clk_rate > 40000000)
322 			priv->clk_csr = 0x01;
323 		else
324 			priv->clk_csr = 0;
325 	}
326 
327 	if (priv->plat->has_xgmac) {
328 		if (clk_rate > 400000000)
329 			priv->clk_csr = 0x5;
330 		else if (clk_rate > 350000000)
331 			priv->clk_csr = 0x4;
332 		else if (clk_rate > 300000000)
333 			priv->clk_csr = 0x3;
334 		else if (clk_rate > 250000000)
335 			priv->clk_csr = 0x2;
336 		else if (clk_rate > 150000000)
337 			priv->clk_csr = 0x1;
338 		else
339 			priv->clk_csr = 0x0;
340 	}
341 }
342 
343 static void print_pkt(unsigned char *buf, int len)
344 {
345 	pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
346 	print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
347 }
348 
349 static inline u32 stmmac_tx_avail(struct stmmac_priv *priv, u32 queue)
350 {
351 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
352 	u32 avail;
353 
354 	if (tx_q->dirty_tx > tx_q->cur_tx)
355 		avail = tx_q->dirty_tx - tx_q->cur_tx - 1;
356 	else
357 		avail = priv->dma_tx_size - tx_q->cur_tx + tx_q->dirty_tx - 1;
358 
359 	return avail;
360 }
361 
362 /**
363  * stmmac_rx_dirty - Get RX queue dirty
364  * @priv: driver private structure
365  * @queue: RX queue index
366  */
367 static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv, u32 queue)
368 {
369 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
370 	u32 dirty;
371 
372 	if (rx_q->dirty_rx <= rx_q->cur_rx)
373 		dirty = rx_q->cur_rx - rx_q->dirty_rx;
374 	else
375 		dirty = priv->dma_rx_size - rx_q->dirty_rx + rx_q->cur_rx;
376 
377 	return dirty;
378 }
379 
380 static void stmmac_lpi_entry_timer_config(struct stmmac_priv *priv, bool en)
381 {
382 	int tx_lpi_timer;
383 
384 	/* Clear/set the SW EEE timer flag based on LPI ET enablement */
385 	priv->eee_sw_timer_en = en ? 0 : 1;
386 	tx_lpi_timer  = en ? priv->tx_lpi_timer : 0;
387 	stmmac_set_eee_lpi_timer(priv, priv->hw, tx_lpi_timer);
388 }
389 
390 /**
391  * stmmac_enable_eee_mode - check and enter in LPI mode
392  * @priv: driver private structure
393  * Description: this function is to verify and enter in LPI mode in case of
394  * EEE.
395  */
396 static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
397 {
398 	u32 tx_cnt = priv->plat->tx_queues_to_use;
399 	u32 queue;
400 
401 	/* check if all TX queues have the work finished */
402 	for (queue = 0; queue < tx_cnt; queue++) {
403 		struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
404 
405 		if (tx_q->dirty_tx != tx_q->cur_tx)
406 			return; /* still unfinished work */
407 	}
408 
409 	/* Check and enter in LPI mode */
410 	if (!priv->tx_path_in_lpi_mode)
411 		stmmac_set_eee_mode(priv, priv->hw,
412 				priv->plat->en_tx_lpi_clockgating);
413 }
414 
415 /**
416  * stmmac_disable_eee_mode - disable and exit from LPI mode
417  * @priv: driver private structure
418  * Description: this function is to exit and disable EEE in case of
419  * LPI state is true. This is called by the xmit.
420  */
421 void stmmac_disable_eee_mode(struct stmmac_priv *priv)
422 {
423 	if (!priv->eee_sw_timer_en) {
424 		stmmac_lpi_entry_timer_config(priv, 0);
425 		return;
426 	}
427 
428 	stmmac_reset_eee_mode(priv, priv->hw);
429 	del_timer_sync(&priv->eee_ctrl_timer);
430 	priv->tx_path_in_lpi_mode = false;
431 }
432 
433 /**
434  * stmmac_eee_ctrl_timer - EEE TX SW timer.
435  * @t:  timer_list struct containing private info
436  * Description:
437  *  if there is no data transfer and if we are not in LPI state,
438  *  then MAC Transmitter can be moved to LPI state.
439  */
440 static void stmmac_eee_ctrl_timer(struct timer_list *t)
441 {
442 	struct stmmac_priv *priv = from_timer(priv, t, eee_ctrl_timer);
443 
444 	stmmac_enable_eee_mode(priv);
445 	mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer));
446 }
447 
448 /**
449  * stmmac_eee_init - init EEE
450  * @priv: driver private structure
451  * Description:
452  *  if the GMAC supports the EEE (from the HW cap reg) and the phy device
453  *  can also manage EEE, this function enable the LPI state and start related
454  *  timer.
455  */
456 bool stmmac_eee_init(struct stmmac_priv *priv)
457 {
458 	int eee_tw_timer = priv->eee_tw_timer;
459 
460 	/* Using PCS we cannot dial with the phy registers at this stage
461 	 * so we do not support extra feature like EEE.
462 	 */
463 	if (priv->hw->pcs == STMMAC_PCS_TBI ||
464 	    priv->hw->pcs == STMMAC_PCS_RTBI)
465 		return false;
466 
467 	/* Check if MAC core supports the EEE feature. */
468 	if (!priv->dma_cap.eee)
469 		return false;
470 
471 	mutex_lock(&priv->lock);
472 
473 	/* Check if it needs to be deactivated */
474 	if (!priv->eee_active) {
475 		if (priv->eee_enabled) {
476 			netdev_dbg(priv->dev, "disable EEE\n");
477 			stmmac_lpi_entry_timer_config(priv, 0);
478 			del_timer_sync(&priv->eee_ctrl_timer);
479 			stmmac_set_eee_timer(priv, priv->hw, 0, eee_tw_timer);
480 		}
481 		mutex_unlock(&priv->lock);
482 		return false;
483 	}
484 
485 	if (priv->eee_active && !priv->eee_enabled) {
486 		timer_setup(&priv->eee_ctrl_timer, stmmac_eee_ctrl_timer, 0);
487 		stmmac_set_eee_timer(priv, priv->hw, STMMAC_DEFAULT_LIT_LS,
488 				     eee_tw_timer);
489 	}
490 
491 	if (priv->plat->has_gmac4 && priv->tx_lpi_timer <= STMMAC_ET_MAX) {
492 		del_timer_sync(&priv->eee_ctrl_timer);
493 		priv->tx_path_in_lpi_mode = false;
494 		stmmac_lpi_entry_timer_config(priv, 1);
495 	} else {
496 		stmmac_lpi_entry_timer_config(priv, 0);
497 		mod_timer(&priv->eee_ctrl_timer,
498 			  STMMAC_LPI_T(priv->tx_lpi_timer));
499 	}
500 
501 	mutex_unlock(&priv->lock);
502 	netdev_dbg(priv->dev, "Energy-Efficient Ethernet initialized\n");
503 	return true;
504 }
505 
506 /* stmmac_get_tx_hwtstamp - get HW TX timestamps
507  * @priv: driver private structure
508  * @p : descriptor pointer
509  * @skb : the socket buffer
510  * Description :
511  * This function will read timestamp from the descriptor & pass it to stack.
512  * and also perform some sanity checks.
513  */
514 static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
515 				   struct dma_desc *p, struct sk_buff *skb)
516 {
517 	struct skb_shared_hwtstamps shhwtstamp;
518 	bool found = false;
519 	s64 adjust = 0;
520 	u64 ns = 0;
521 
522 	if (!priv->hwts_tx_en)
523 		return;
524 
525 	/* exit if skb doesn't support hw tstamp */
526 	if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
527 		return;
528 
529 	/* check tx tstamp status */
530 	if (stmmac_get_tx_timestamp_status(priv, p)) {
531 		stmmac_get_timestamp(priv, p, priv->adv_ts, &ns);
532 		found = true;
533 	} else if (!stmmac_get_mac_tx_timestamp(priv, priv->hw, &ns)) {
534 		found = true;
535 	}
536 
537 	if (found) {
538 		/* Correct the clk domain crossing(CDC) error */
539 		if (priv->plat->has_gmac4 && priv->plat->clk_ptp_rate) {
540 			adjust += -(2 * (NSEC_PER_SEC /
541 					 priv->plat->clk_ptp_rate));
542 			ns += adjust;
543 		}
544 
545 		memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
546 		shhwtstamp.hwtstamp = ns_to_ktime(ns);
547 
548 		netdev_dbg(priv->dev, "get valid TX hw timestamp %llu\n", ns);
549 		/* pass tstamp to stack */
550 		skb_tstamp_tx(skb, &shhwtstamp);
551 	}
552 }
553 
554 /* stmmac_get_rx_hwtstamp - get HW RX timestamps
555  * @priv: driver private structure
556  * @p : descriptor pointer
557  * @np : next descriptor pointer
558  * @skb : the socket buffer
559  * Description :
560  * This function will read received packet's timestamp from the descriptor
561  * and pass it to stack. It also perform some sanity checks.
562  */
563 static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,
564 				   struct dma_desc *np, struct sk_buff *skb)
565 {
566 	struct skb_shared_hwtstamps *shhwtstamp = NULL;
567 	struct dma_desc *desc = p;
568 	u64 adjust = 0;
569 	u64 ns = 0;
570 
571 	if (!priv->hwts_rx_en)
572 		return;
573 	/* For GMAC4, the valid timestamp is from CTX next desc. */
574 	if (priv->plat->has_gmac4 || priv->plat->has_xgmac)
575 		desc = np;
576 
577 	/* Check if timestamp is available */
578 	if (stmmac_get_rx_timestamp_status(priv, p, np, priv->adv_ts)) {
579 		stmmac_get_timestamp(priv, desc, priv->adv_ts, &ns);
580 
581 		/* Correct the clk domain crossing(CDC) error */
582 		if (priv->plat->has_gmac4 && priv->plat->clk_ptp_rate) {
583 			adjust += 2 * (NSEC_PER_SEC / priv->plat->clk_ptp_rate);
584 			ns -= adjust;
585 		}
586 
587 		netdev_dbg(priv->dev, "get valid RX hw timestamp %llu\n", ns);
588 		shhwtstamp = skb_hwtstamps(skb);
589 		memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
590 		shhwtstamp->hwtstamp = ns_to_ktime(ns);
591 	} else  {
592 		netdev_dbg(priv->dev, "cannot get RX hw timestamp\n");
593 	}
594 }
595 
596 /**
597  *  stmmac_hwtstamp_set - control hardware timestamping.
598  *  @dev: device pointer.
599  *  @ifr: An IOCTL specific structure, that can contain a pointer to
600  *  a proprietary structure used to pass information to the driver.
601  *  Description:
602  *  This function configures the MAC to enable/disable both outgoing(TX)
603  *  and incoming(RX) packets time stamping based on user input.
604  *  Return Value:
605  *  0 on success and an appropriate -ve integer on failure.
606  */
607 static int stmmac_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
608 {
609 	struct stmmac_priv *priv = netdev_priv(dev);
610 	struct hwtstamp_config config;
611 	struct timespec64 now;
612 	u64 temp = 0;
613 	u32 ptp_v2 = 0;
614 	u32 tstamp_all = 0;
615 	u32 ptp_over_ipv4_udp = 0;
616 	u32 ptp_over_ipv6_udp = 0;
617 	u32 ptp_over_ethernet = 0;
618 	u32 snap_type_sel = 0;
619 	u32 ts_master_en = 0;
620 	u32 ts_event_en = 0;
621 	u32 sec_inc = 0;
622 	u32 value = 0;
623 	bool xmac;
624 
625 	xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
626 
627 	if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
628 		netdev_alert(priv->dev, "No support for HW time stamping\n");
629 		priv->hwts_tx_en = 0;
630 		priv->hwts_rx_en = 0;
631 
632 		return -EOPNOTSUPP;
633 	}
634 
635 	if (copy_from_user(&config, ifr->ifr_data,
636 			   sizeof(config)))
637 		return -EFAULT;
638 
639 	netdev_dbg(priv->dev, "%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
640 		   __func__, config.flags, config.tx_type, config.rx_filter);
641 
642 	/* reserved for future extensions */
643 	if (config.flags)
644 		return -EINVAL;
645 
646 	if (config.tx_type != HWTSTAMP_TX_OFF &&
647 	    config.tx_type != HWTSTAMP_TX_ON)
648 		return -ERANGE;
649 
650 	if (priv->adv_ts) {
651 		switch (config.rx_filter) {
652 		case HWTSTAMP_FILTER_NONE:
653 			/* time stamp no incoming packet at all */
654 			config.rx_filter = HWTSTAMP_FILTER_NONE;
655 			break;
656 
657 		case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
658 			/* PTP v1, UDP, any kind of event packet */
659 			config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
660 			/* 'xmac' hardware can support Sync, Pdelay_Req and
661 			 * Pdelay_resp by setting bit14 and bits17/16 to 01
662 			 * This leaves Delay_Req timestamps out.
663 			 * Enable all events *and* general purpose message
664 			 * timestamping
665 			 */
666 			snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
667 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
668 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
669 			break;
670 
671 		case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
672 			/* PTP v1, UDP, Sync packet */
673 			config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
674 			/* take time stamp for SYNC messages only */
675 			ts_event_en = PTP_TCR_TSEVNTENA;
676 
677 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
678 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
679 			break;
680 
681 		case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
682 			/* PTP v1, UDP, Delay_req packet */
683 			config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
684 			/* take time stamp for Delay_Req messages only */
685 			ts_master_en = PTP_TCR_TSMSTRENA;
686 			ts_event_en = PTP_TCR_TSEVNTENA;
687 
688 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
689 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
690 			break;
691 
692 		case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
693 			/* PTP v2, UDP, any kind of event packet */
694 			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
695 			ptp_v2 = PTP_TCR_TSVER2ENA;
696 			/* take time stamp for all event messages */
697 			snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
698 
699 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
700 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
701 			break;
702 
703 		case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
704 			/* PTP v2, UDP, Sync packet */
705 			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
706 			ptp_v2 = PTP_TCR_TSVER2ENA;
707 			/* take time stamp for SYNC messages only */
708 			ts_event_en = PTP_TCR_TSEVNTENA;
709 
710 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
711 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
712 			break;
713 
714 		case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
715 			/* PTP v2, UDP, Delay_req packet */
716 			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
717 			ptp_v2 = PTP_TCR_TSVER2ENA;
718 			/* take time stamp for Delay_Req messages only */
719 			ts_master_en = PTP_TCR_TSMSTRENA;
720 			ts_event_en = PTP_TCR_TSEVNTENA;
721 
722 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
723 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
724 			break;
725 
726 		case HWTSTAMP_FILTER_PTP_V2_EVENT:
727 			/* PTP v2/802.AS1 any layer, any kind of event packet */
728 			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
729 			ptp_v2 = PTP_TCR_TSVER2ENA;
730 			snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
731 			if (priv->synopsys_id != DWMAC_CORE_5_10)
732 				ts_event_en = PTP_TCR_TSEVNTENA;
733 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
734 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
735 			ptp_over_ethernet = PTP_TCR_TSIPENA;
736 			break;
737 
738 		case HWTSTAMP_FILTER_PTP_V2_SYNC:
739 			/* PTP v2/802.AS1, any layer, Sync packet */
740 			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
741 			ptp_v2 = PTP_TCR_TSVER2ENA;
742 			/* take time stamp for SYNC messages only */
743 			ts_event_en = PTP_TCR_TSEVNTENA;
744 
745 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
746 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
747 			ptp_over_ethernet = PTP_TCR_TSIPENA;
748 			break;
749 
750 		case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
751 			/* PTP v2/802.AS1, any layer, Delay_req packet */
752 			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
753 			ptp_v2 = PTP_TCR_TSVER2ENA;
754 			/* take time stamp for Delay_Req messages only */
755 			ts_master_en = PTP_TCR_TSMSTRENA;
756 			ts_event_en = PTP_TCR_TSEVNTENA;
757 
758 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
759 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
760 			ptp_over_ethernet = PTP_TCR_TSIPENA;
761 			break;
762 
763 		case HWTSTAMP_FILTER_NTP_ALL:
764 		case HWTSTAMP_FILTER_ALL:
765 			/* time stamp any incoming packet */
766 			config.rx_filter = HWTSTAMP_FILTER_ALL;
767 			tstamp_all = PTP_TCR_TSENALL;
768 			break;
769 
770 		default:
771 			return -ERANGE;
772 		}
773 	} else {
774 		switch (config.rx_filter) {
775 		case HWTSTAMP_FILTER_NONE:
776 			config.rx_filter = HWTSTAMP_FILTER_NONE;
777 			break;
778 		default:
779 			/* PTP v1, UDP, any kind of event packet */
780 			config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
781 			break;
782 		}
783 	}
784 	priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
785 	priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
786 
787 	if (!priv->hwts_tx_en && !priv->hwts_rx_en)
788 		stmmac_config_hw_tstamping(priv, priv->ptpaddr, 0);
789 	else {
790 		value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
791 			 tstamp_all | ptp_v2 | ptp_over_ethernet |
792 			 ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
793 			 ts_master_en | snap_type_sel);
794 		stmmac_config_hw_tstamping(priv, priv->ptpaddr, value);
795 
796 		/* program Sub Second Increment reg */
797 		stmmac_config_sub_second_increment(priv,
798 				priv->ptpaddr, priv->plat->clk_ptp_rate,
799 				xmac, &sec_inc);
800 		temp = div_u64(1000000000ULL, sec_inc);
801 
802 		/* Store sub second increment and flags for later use */
803 		priv->sub_second_inc = sec_inc;
804 		priv->systime_flags = value;
805 
806 		/* calculate default added value:
807 		 * formula is :
808 		 * addend = (2^32)/freq_div_ratio;
809 		 * where, freq_div_ratio = 1e9ns/sec_inc
810 		 */
811 		temp = (u64)(temp << 32);
812 		priv->default_addend = div_u64(temp, priv->plat->clk_ptp_rate);
813 		stmmac_config_addend(priv, priv->ptpaddr, priv->default_addend);
814 
815 		/* initialize system time */
816 		ktime_get_real_ts64(&now);
817 
818 		/* lower 32 bits of tv_sec are safe until y2106 */
819 		stmmac_init_systime(priv, priv->ptpaddr,
820 				(u32)now.tv_sec, now.tv_nsec);
821 	}
822 
823 	memcpy(&priv->tstamp_config, &config, sizeof(config));
824 
825 	return copy_to_user(ifr->ifr_data, &config,
826 			    sizeof(config)) ? -EFAULT : 0;
827 }
828 
829 /**
830  *  stmmac_hwtstamp_get - read hardware timestamping.
831  *  @dev: device pointer.
832  *  @ifr: An IOCTL specific structure, that can contain a pointer to
833  *  a proprietary structure used to pass information to the driver.
834  *  Description:
835  *  This function obtain the current hardware timestamping settings
836  *  as requested.
837  */
838 static int stmmac_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
839 {
840 	struct stmmac_priv *priv = netdev_priv(dev);
841 	struct hwtstamp_config *config = &priv->tstamp_config;
842 
843 	if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
844 		return -EOPNOTSUPP;
845 
846 	return copy_to_user(ifr->ifr_data, config,
847 			    sizeof(*config)) ? -EFAULT : 0;
848 }
849 
850 /**
851  * stmmac_init_ptp - init PTP
852  * @priv: driver private structure
853  * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
854  * This is done by looking at the HW cap. register.
855  * This function also registers the ptp driver.
856  */
857 static int stmmac_init_ptp(struct stmmac_priv *priv)
858 {
859 	bool xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
860 
861 	if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
862 		return -EOPNOTSUPP;
863 
864 	priv->adv_ts = 0;
865 	/* Check if adv_ts can be enabled for dwmac 4.x / xgmac core */
866 	if (xmac && priv->dma_cap.atime_stamp)
867 		priv->adv_ts = 1;
868 	/* Dwmac 3.x core with extend_desc can support adv_ts */
869 	else if (priv->extend_desc && priv->dma_cap.atime_stamp)
870 		priv->adv_ts = 1;
871 
872 	if (priv->dma_cap.time_stamp)
873 		netdev_info(priv->dev, "IEEE 1588-2002 Timestamp supported\n");
874 
875 	if (priv->adv_ts)
876 		netdev_info(priv->dev,
877 			    "IEEE 1588-2008 Advanced Timestamp supported\n");
878 
879 	priv->hwts_tx_en = 0;
880 	priv->hwts_rx_en = 0;
881 
882 	stmmac_ptp_register(priv);
883 
884 	return 0;
885 }
886 
887 static void stmmac_release_ptp(struct stmmac_priv *priv)
888 {
889 	clk_disable_unprepare(priv->plat->clk_ptp_ref);
890 	stmmac_ptp_unregister(priv);
891 }
892 
893 /**
894  *  stmmac_mac_flow_ctrl - Configure flow control in all queues
895  *  @priv: driver private structure
896  *  @duplex: duplex passed to the next function
897  *  Description: It is used for configuring the flow control in all queues
898  */
899 static void stmmac_mac_flow_ctrl(struct stmmac_priv *priv, u32 duplex)
900 {
901 	u32 tx_cnt = priv->plat->tx_queues_to_use;
902 
903 	stmmac_flow_ctrl(priv, priv->hw, duplex, priv->flow_ctrl,
904 			priv->pause, tx_cnt);
905 }
906 
907 static void stmmac_validate(struct phylink_config *config,
908 			    unsigned long *supported,
909 			    struct phylink_link_state *state)
910 {
911 	struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
912 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mac_supported) = { 0, };
913 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
914 	int tx_cnt = priv->plat->tx_queues_to_use;
915 	int max_speed = priv->plat->max_speed;
916 
917 	phylink_set(mac_supported, 10baseT_Half);
918 	phylink_set(mac_supported, 10baseT_Full);
919 	phylink_set(mac_supported, 100baseT_Half);
920 	phylink_set(mac_supported, 100baseT_Full);
921 	phylink_set(mac_supported, 1000baseT_Half);
922 	phylink_set(mac_supported, 1000baseT_Full);
923 	phylink_set(mac_supported, 1000baseKX_Full);
924 
925 	phylink_set(mac_supported, Autoneg);
926 	phylink_set(mac_supported, Pause);
927 	phylink_set(mac_supported, Asym_Pause);
928 	phylink_set_port_modes(mac_supported);
929 
930 	/* Cut down 1G if asked to */
931 	if ((max_speed > 0) && (max_speed < 1000)) {
932 		phylink_set(mask, 1000baseT_Full);
933 		phylink_set(mask, 1000baseX_Full);
934 	} else if (priv->plat->has_xgmac) {
935 		if (!max_speed || (max_speed >= 2500)) {
936 			phylink_set(mac_supported, 2500baseT_Full);
937 			phylink_set(mac_supported, 2500baseX_Full);
938 		}
939 		if (!max_speed || (max_speed >= 5000)) {
940 			phylink_set(mac_supported, 5000baseT_Full);
941 		}
942 		if (!max_speed || (max_speed >= 10000)) {
943 			phylink_set(mac_supported, 10000baseSR_Full);
944 			phylink_set(mac_supported, 10000baseLR_Full);
945 			phylink_set(mac_supported, 10000baseER_Full);
946 			phylink_set(mac_supported, 10000baseLRM_Full);
947 			phylink_set(mac_supported, 10000baseT_Full);
948 			phylink_set(mac_supported, 10000baseKX4_Full);
949 			phylink_set(mac_supported, 10000baseKR_Full);
950 		}
951 		if (!max_speed || (max_speed >= 25000)) {
952 			phylink_set(mac_supported, 25000baseCR_Full);
953 			phylink_set(mac_supported, 25000baseKR_Full);
954 			phylink_set(mac_supported, 25000baseSR_Full);
955 		}
956 		if (!max_speed || (max_speed >= 40000)) {
957 			phylink_set(mac_supported, 40000baseKR4_Full);
958 			phylink_set(mac_supported, 40000baseCR4_Full);
959 			phylink_set(mac_supported, 40000baseSR4_Full);
960 			phylink_set(mac_supported, 40000baseLR4_Full);
961 		}
962 		if (!max_speed || (max_speed >= 50000)) {
963 			phylink_set(mac_supported, 50000baseCR2_Full);
964 			phylink_set(mac_supported, 50000baseKR2_Full);
965 			phylink_set(mac_supported, 50000baseSR2_Full);
966 			phylink_set(mac_supported, 50000baseKR_Full);
967 			phylink_set(mac_supported, 50000baseSR_Full);
968 			phylink_set(mac_supported, 50000baseCR_Full);
969 			phylink_set(mac_supported, 50000baseLR_ER_FR_Full);
970 			phylink_set(mac_supported, 50000baseDR_Full);
971 		}
972 		if (!max_speed || (max_speed >= 100000)) {
973 			phylink_set(mac_supported, 100000baseKR4_Full);
974 			phylink_set(mac_supported, 100000baseSR4_Full);
975 			phylink_set(mac_supported, 100000baseCR4_Full);
976 			phylink_set(mac_supported, 100000baseLR4_ER4_Full);
977 			phylink_set(mac_supported, 100000baseKR2_Full);
978 			phylink_set(mac_supported, 100000baseSR2_Full);
979 			phylink_set(mac_supported, 100000baseCR2_Full);
980 			phylink_set(mac_supported, 100000baseLR2_ER2_FR2_Full);
981 			phylink_set(mac_supported, 100000baseDR2_Full);
982 		}
983 	}
984 
985 	/* Half-Duplex can only work with single queue */
986 	if (tx_cnt > 1) {
987 		phylink_set(mask, 10baseT_Half);
988 		phylink_set(mask, 100baseT_Half);
989 		phylink_set(mask, 1000baseT_Half);
990 	}
991 
992 	linkmode_and(supported, supported, mac_supported);
993 	linkmode_andnot(supported, supported, mask);
994 
995 	linkmode_and(state->advertising, state->advertising, mac_supported);
996 	linkmode_andnot(state->advertising, state->advertising, mask);
997 
998 	/* If PCS is supported, check which modes it supports. */
999 	stmmac_xpcs_validate(priv, &priv->hw->xpcs_args, supported, state);
1000 }
1001 
1002 static void stmmac_mac_pcs_get_state(struct phylink_config *config,
1003 				     struct phylink_link_state *state)
1004 {
1005 	struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
1006 
1007 	state->link = 0;
1008 	stmmac_xpcs_get_state(priv, &priv->hw->xpcs_args, state);
1009 }
1010 
1011 static void stmmac_mac_config(struct phylink_config *config, unsigned int mode,
1012 			      const struct phylink_link_state *state)
1013 {
1014 	struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
1015 
1016 	stmmac_xpcs_config(priv, &priv->hw->xpcs_args, state);
1017 }
1018 
1019 static void stmmac_mac_an_restart(struct phylink_config *config)
1020 {
1021 	/* Not Supported */
1022 }
1023 
1024 static void stmmac_fpe_link_state_handle(struct stmmac_priv *priv, bool is_up)
1025 {
1026 	struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
1027 	enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
1028 	enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
1029 	bool *hs_enable = &fpe_cfg->hs_enable;
1030 
1031 	if (is_up && *hs_enable) {
1032 		stmmac_fpe_send_mpacket(priv, priv->ioaddr, MPACKET_VERIFY);
1033 	} else {
1034 		*lo_state = FPE_EVENT_UNKNOWN;
1035 		*lp_state = FPE_EVENT_UNKNOWN;
1036 	}
1037 }
1038 
1039 static void stmmac_mac_link_down(struct phylink_config *config,
1040 				 unsigned int mode, phy_interface_t interface)
1041 {
1042 	struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
1043 
1044 	stmmac_mac_set(priv, priv->ioaddr, false);
1045 	priv->eee_active = false;
1046 	priv->tx_lpi_enabled = false;
1047 	stmmac_eee_init(priv);
1048 	stmmac_set_eee_pls(priv, priv->hw, false);
1049 
1050 	if (priv->dma_cap.fpesel)
1051 		stmmac_fpe_link_state_handle(priv, false);
1052 }
1053 
1054 static void stmmac_mac_link_up(struct phylink_config *config,
1055 			       struct phy_device *phy,
1056 			       unsigned int mode, phy_interface_t interface,
1057 			       int speed, int duplex,
1058 			       bool tx_pause, bool rx_pause)
1059 {
1060 	struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
1061 	u32 ctrl;
1062 
1063 	stmmac_xpcs_link_up(priv, &priv->hw->xpcs_args, speed, interface);
1064 
1065 	ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
1066 	ctrl &= ~priv->hw->link.speed_mask;
1067 
1068 	if (interface == PHY_INTERFACE_MODE_USXGMII) {
1069 		switch (speed) {
1070 		case SPEED_10000:
1071 			ctrl |= priv->hw->link.xgmii.speed10000;
1072 			break;
1073 		case SPEED_5000:
1074 			ctrl |= priv->hw->link.xgmii.speed5000;
1075 			break;
1076 		case SPEED_2500:
1077 			ctrl |= priv->hw->link.xgmii.speed2500;
1078 			break;
1079 		default:
1080 			return;
1081 		}
1082 	} else if (interface == PHY_INTERFACE_MODE_XLGMII) {
1083 		switch (speed) {
1084 		case SPEED_100000:
1085 			ctrl |= priv->hw->link.xlgmii.speed100000;
1086 			break;
1087 		case SPEED_50000:
1088 			ctrl |= priv->hw->link.xlgmii.speed50000;
1089 			break;
1090 		case SPEED_40000:
1091 			ctrl |= priv->hw->link.xlgmii.speed40000;
1092 			break;
1093 		case SPEED_25000:
1094 			ctrl |= priv->hw->link.xlgmii.speed25000;
1095 			break;
1096 		case SPEED_10000:
1097 			ctrl |= priv->hw->link.xgmii.speed10000;
1098 			break;
1099 		case SPEED_2500:
1100 			ctrl |= priv->hw->link.speed2500;
1101 			break;
1102 		case SPEED_1000:
1103 			ctrl |= priv->hw->link.speed1000;
1104 			break;
1105 		default:
1106 			return;
1107 		}
1108 	} else {
1109 		switch (speed) {
1110 		case SPEED_2500:
1111 			ctrl |= priv->hw->link.speed2500;
1112 			break;
1113 		case SPEED_1000:
1114 			ctrl |= priv->hw->link.speed1000;
1115 			break;
1116 		case SPEED_100:
1117 			ctrl |= priv->hw->link.speed100;
1118 			break;
1119 		case SPEED_10:
1120 			ctrl |= priv->hw->link.speed10;
1121 			break;
1122 		default:
1123 			return;
1124 		}
1125 	}
1126 
1127 	priv->speed = speed;
1128 
1129 	if (priv->plat->fix_mac_speed)
1130 		priv->plat->fix_mac_speed(priv->plat->bsp_priv, speed);
1131 
1132 	if (!duplex)
1133 		ctrl &= ~priv->hw->link.duplex;
1134 	else
1135 		ctrl |= priv->hw->link.duplex;
1136 
1137 	/* Flow Control operation */
1138 	if (tx_pause && rx_pause)
1139 		stmmac_mac_flow_ctrl(priv, duplex);
1140 
1141 	writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
1142 
1143 	stmmac_mac_set(priv, priv->ioaddr, true);
1144 	if (phy && priv->dma_cap.eee) {
1145 		priv->eee_active = phy_init_eee(phy, 1) >= 0;
1146 		priv->eee_enabled = stmmac_eee_init(priv);
1147 		priv->tx_lpi_enabled = priv->eee_enabled;
1148 		stmmac_set_eee_pls(priv, priv->hw, true);
1149 	}
1150 
1151 	if (priv->dma_cap.fpesel)
1152 		stmmac_fpe_link_state_handle(priv, true);
1153 }
1154 
1155 static const struct phylink_mac_ops stmmac_phylink_mac_ops = {
1156 	.validate = stmmac_validate,
1157 	.mac_pcs_get_state = stmmac_mac_pcs_get_state,
1158 	.mac_config = stmmac_mac_config,
1159 	.mac_an_restart = stmmac_mac_an_restart,
1160 	.mac_link_down = stmmac_mac_link_down,
1161 	.mac_link_up = stmmac_mac_link_up,
1162 };
1163 
1164 /**
1165  * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
1166  * @priv: driver private structure
1167  * Description: this is to verify if the HW supports the PCS.
1168  * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
1169  * configured for the TBI, RTBI, or SGMII PHY interface.
1170  */
1171 static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
1172 {
1173 	int interface = priv->plat->interface;
1174 
1175 	if (priv->dma_cap.pcs) {
1176 		if ((interface == PHY_INTERFACE_MODE_RGMII) ||
1177 		    (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
1178 		    (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
1179 		    (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
1180 			netdev_dbg(priv->dev, "PCS RGMII support enabled\n");
1181 			priv->hw->pcs = STMMAC_PCS_RGMII;
1182 		} else if (interface == PHY_INTERFACE_MODE_SGMII) {
1183 			netdev_dbg(priv->dev, "PCS SGMII support enabled\n");
1184 			priv->hw->pcs = STMMAC_PCS_SGMII;
1185 		}
1186 	}
1187 }
1188 
1189 /**
1190  * stmmac_init_phy - PHY initialization
1191  * @dev: net device structure
1192  * Description: it initializes the driver's PHY state, and attaches the PHY
1193  * to the mac driver.
1194  *  Return value:
1195  *  0 on success
1196  */
1197 static int stmmac_init_phy(struct net_device *dev)
1198 {
1199 	struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1200 	struct stmmac_priv *priv = netdev_priv(dev);
1201 	struct device_node *node;
1202 	int ret;
1203 
1204 	node = priv->plat->phylink_node;
1205 
1206 	if (node)
1207 		ret = phylink_of_phy_connect(priv->phylink, node, 0);
1208 
1209 	/* Some DT bindings do not set-up the PHY handle. Let's try to
1210 	 * manually parse it
1211 	 */
1212 	if (!node || ret) {
1213 		int addr = priv->plat->phy_addr;
1214 		struct phy_device *phydev;
1215 
1216 		phydev = mdiobus_get_phy(priv->mii, addr);
1217 		if (!phydev) {
1218 			netdev_err(priv->dev, "no phy at addr %d\n", addr);
1219 			return -ENODEV;
1220 		}
1221 
1222 		ret = phylink_connect_phy(priv->phylink, phydev);
1223 	}
1224 
1225 	phylink_ethtool_get_wol(priv->phylink, &wol);
1226 	device_set_wakeup_capable(priv->device, !!wol.supported);
1227 
1228 	return ret;
1229 }
1230 
1231 static int stmmac_phy_setup(struct stmmac_priv *priv)
1232 {
1233 	struct fwnode_handle *fwnode = of_fwnode_handle(priv->plat->phylink_node);
1234 	int mode = priv->plat->phy_interface;
1235 	struct phylink *phylink;
1236 
1237 	priv->phylink_config.dev = &priv->dev->dev;
1238 	priv->phylink_config.type = PHYLINK_NETDEV;
1239 	priv->phylink_config.pcs_poll = true;
1240 	priv->phylink_config.ovr_an_inband =
1241 		priv->plat->mdio_bus_data->xpcs_an_inband;
1242 
1243 	if (!fwnode)
1244 		fwnode = dev_fwnode(priv->device);
1245 
1246 	phylink = phylink_create(&priv->phylink_config, fwnode,
1247 				 mode, &stmmac_phylink_mac_ops);
1248 	if (IS_ERR(phylink))
1249 		return PTR_ERR(phylink);
1250 
1251 	priv->phylink = phylink;
1252 	return 0;
1253 }
1254 
1255 static void stmmac_display_rx_rings(struct stmmac_priv *priv)
1256 {
1257 	u32 rx_cnt = priv->plat->rx_queues_to_use;
1258 	unsigned int desc_size;
1259 	void *head_rx;
1260 	u32 queue;
1261 
1262 	/* Display RX rings */
1263 	for (queue = 0; queue < rx_cnt; queue++) {
1264 		struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1265 
1266 		pr_info("\tRX Queue %u rings\n", queue);
1267 
1268 		if (priv->extend_desc) {
1269 			head_rx = (void *)rx_q->dma_erx;
1270 			desc_size = sizeof(struct dma_extended_desc);
1271 		} else {
1272 			head_rx = (void *)rx_q->dma_rx;
1273 			desc_size = sizeof(struct dma_desc);
1274 		}
1275 
1276 		/* Display RX ring */
1277 		stmmac_display_ring(priv, head_rx, priv->dma_rx_size, true,
1278 				    rx_q->dma_rx_phy, desc_size);
1279 	}
1280 }
1281 
1282 static void stmmac_display_tx_rings(struct stmmac_priv *priv)
1283 {
1284 	u32 tx_cnt = priv->plat->tx_queues_to_use;
1285 	unsigned int desc_size;
1286 	void *head_tx;
1287 	u32 queue;
1288 
1289 	/* Display TX rings */
1290 	for (queue = 0; queue < tx_cnt; queue++) {
1291 		struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1292 
1293 		pr_info("\tTX Queue %d rings\n", queue);
1294 
1295 		if (priv->extend_desc) {
1296 			head_tx = (void *)tx_q->dma_etx;
1297 			desc_size = sizeof(struct dma_extended_desc);
1298 		} else if (tx_q->tbs & STMMAC_TBS_AVAIL) {
1299 			head_tx = (void *)tx_q->dma_entx;
1300 			desc_size = sizeof(struct dma_edesc);
1301 		} else {
1302 			head_tx = (void *)tx_q->dma_tx;
1303 			desc_size = sizeof(struct dma_desc);
1304 		}
1305 
1306 		stmmac_display_ring(priv, head_tx, priv->dma_tx_size, false,
1307 				    tx_q->dma_tx_phy, desc_size);
1308 	}
1309 }
1310 
1311 static void stmmac_display_rings(struct stmmac_priv *priv)
1312 {
1313 	/* Display RX ring */
1314 	stmmac_display_rx_rings(priv);
1315 
1316 	/* Display TX ring */
1317 	stmmac_display_tx_rings(priv);
1318 }
1319 
1320 static int stmmac_set_bfsize(int mtu, int bufsize)
1321 {
1322 	int ret = bufsize;
1323 
1324 	if (mtu >= BUF_SIZE_8KiB)
1325 		ret = BUF_SIZE_16KiB;
1326 	else if (mtu >= BUF_SIZE_4KiB)
1327 		ret = BUF_SIZE_8KiB;
1328 	else if (mtu >= BUF_SIZE_2KiB)
1329 		ret = BUF_SIZE_4KiB;
1330 	else if (mtu > DEFAULT_BUFSIZE)
1331 		ret = BUF_SIZE_2KiB;
1332 	else
1333 		ret = DEFAULT_BUFSIZE;
1334 
1335 	return ret;
1336 }
1337 
1338 /**
1339  * stmmac_clear_rx_descriptors - clear RX descriptors
1340  * @priv: driver private structure
1341  * @queue: RX queue index
1342  * Description: this function is called to clear the RX descriptors
1343  * in case of both basic and extended descriptors are used.
1344  */
1345 static void stmmac_clear_rx_descriptors(struct stmmac_priv *priv, u32 queue)
1346 {
1347 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1348 	int i;
1349 
1350 	/* Clear the RX descriptors */
1351 	for (i = 0; i < priv->dma_rx_size; i++)
1352 		if (priv->extend_desc)
1353 			stmmac_init_rx_desc(priv, &rx_q->dma_erx[i].basic,
1354 					priv->use_riwt, priv->mode,
1355 					(i == priv->dma_rx_size - 1),
1356 					priv->dma_buf_sz);
1357 		else
1358 			stmmac_init_rx_desc(priv, &rx_q->dma_rx[i],
1359 					priv->use_riwt, priv->mode,
1360 					(i == priv->dma_rx_size - 1),
1361 					priv->dma_buf_sz);
1362 }
1363 
1364 /**
1365  * stmmac_clear_tx_descriptors - clear tx descriptors
1366  * @priv: driver private structure
1367  * @queue: TX queue index.
1368  * Description: this function is called to clear the TX descriptors
1369  * in case of both basic and extended descriptors are used.
1370  */
1371 static void stmmac_clear_tx_descriptors(struct stmmac_priv *priv, u32 queue)
1372 {
1373 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1374 	int i;
1375 
1376 	/* Clear the TX descriptors */
1377 	for (i = 0; i < priv->dma_tx_size; i++) {
1378 		int last = (i == (priv->dma_tx_size - 1));
1379 		struct dma_desc *p;
1380 
1381 		if (priv->extend_desc)
1382 			p = &tx_q->dma_etx[i].basic;
1383 		else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1384 			p = &tx_q->dma_entx[i].basic;
1385 		else
1386 			p = &tx_q->dma_tx[i];
1387 
1388 		stmmac_init_tx_desc(priv, p, priv->mode, last);
1389 	}
1390 }
1391 
1392 /**
1393  * stmmac_clear_descriptors - clear descriptors
1394  * @priv: driver private structure
1395  * Description: this function is called to clear the TX and RX descriptors
1396  * in case of both basic and extended descriptors are used.
1397  */
1398 static void stmmac_clear_descriptors(struct stmmac_priv *priv)
1399 {
1400 	u32 rx_queue_cnt = priv->plat->rx_queues_to_use;
1401 	u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1402 	u32 queue;
1403 
1404 	/* Clear the RX descriptors */
1405 	for (queue = 0; queue < rx_queue_cnt; queue++)
1406 		stmmac_clear_rx_descriptors(priv, queue);
1407 
1408 	/* Clear the TX descriptors */
1409 	for (queue = 0; queue < tx_queue_cnt; queue++)
1410 		stmmac_clear_tx_descriptors(priv, queue);
1411 }
1412 
1413 /**
1414  * stmmac_init_rx_buffers - init the RX descriptor buffer.
1415  * @priv: driver private structure
1416  * @p: descriptor pointer
1417  * @i: descriptor index
1418  * @flags: gfp flag
1419  * @queue: RX queue index
1420  * Description: this function is called to allocate a receive buffer, perform
1421  * the DMA mapping and init the descriptor.
1422  */
1423 static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
1424 				  int i, gfp_t flags, u32 queue)
1425 {
1426 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1427 	struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1428 
1429 	if (!buf->page) {
1430 		buf->page = page_pool_dev_alloc_pages(rx_q->page_pool);
1431 		if (!buf->page)
1432 			return -ENOMEM;
1433 		buf->page_offset = stmmac_rx_offset(priv);
1434 	}
1435 
1436 	if (priv->sph && !buf->sec_page) {
1437 		buf->sec_page = page_pool_dev_alloc_pages(rx_q->page_pool);
1438 		if (!buf->sec_page)
1439 			return -ENOMEM;
1440 
1441 		buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
1442 		stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true);
1443 	} else {
1444 		buf->sec_page = NULL;
1445 		stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false);
1446 	}
1447 
1448 	buf->addr = page_pool_get_dma_addr(buf->page) + buf->page_offset;
1449 
1450 	stmmac_set_desc_addr(priv, p, buf->addr);
1451 	if (priv->dma_buf_sz == BUF_SIZE_16KiB)
1452 		stmmac_init_desc3(priv, p);
1453 
1454 	return 0;
1455 }
1456 
1457 /**
1458  * stmmac_free_rx_buffer - free RX dma buffers
1459  * @priv: private structure
1460  * @queue: RX queue index
1461  * @i: buffer index.
1462  */
1463 static void stmmac_free_rx_buffer(struct stmmac_priv *priv, u32 queue, int i)
1464 {
1465 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1466 	struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1467 
1468 	if (buf->page)
1469 		page_pool_put_full_page(rx_q->page_pool, buf->page, false);
1470 	buf->page = NULL;
1471 
1472 	if (buf->sec_page)
1473 		page_pool_put_full_page(rx_q->page_pool, buf->sec_page, false);
1474 	buf->sec_page = NULL;
1475 }
1476 
1477 /**
1478  * stmmac_free_tx_buffer - free RX dma buffers
1479  * @priv: private structure
1480  * @queue: RX queue index
1481  * @i: buffer index.
1482  */
1483 static void stmmac_free_tx_buffer(struct stmmac_priv *priv, u32 queue, int i)
1484 {
1485 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1486 
1487 	if (tx_q->tx_skbuff_dma[i].buf &&
1488 	    tx_q->tx_skbuff_dma[i].buf_type != STMMAC_TXBUF_T_XDP_TX) {
1489 		if (tx_q->tx_skbuff_dma[i].map_as_page)
1490 			dma_unmap_page(priv->device,
1491 				       tx_q->tx_skbuff_dma[i].buf,
1492 				       tx_q->tx_skbuff_dma[i].len,
1493 				       DMA_TO_DEVICE);
1494 		else
1495 			dma_unmap_single(priv->device,
1496 					 tx_q->tx_skbuff_dma[i].buf,
1497 					 tx_q->tx_skbuff_dma[i].len,
1498 					 DMA_TO_DEVICE);
1499 	}
1500 
1501 	if (tx_q->xdpf[i] &&
1502 	    (tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XDP_TX ||
1503 	     tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XDP_NDO)) {
1504 		xdp_return_frame(tx_q->xdpf[i]);
1505 		tx_q->xdpf[i] = NULL;
1506 	}
1507 
1508 	if (tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XSK_TX)
1509 		tx_q->xsk_frames_done++;
1510 
1511 	if (tx_q->tx_skbuff[i] &&
1512 	    tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_SKB) {
1513 		dev_kfree_skb_any(tx_q->tx_skbuff[i]);
1514 		tx_q->tx_skbuff[i] = NULL;
1515 	}
1516 
1517 	tx_q->tx_skbuff_dma[i].buf = 0;
1518 	tx_q->tx_skbuff_dma[i].map_as_page = false;
1519 }
1520 
1521 /**
1522  * dma_free_rx_skbufs - free RX dma buffers
1523  * @priv: private structure
1524  * @queue: RX queue index
1525  */
1526 static void dma_free_rx_skbufs(struct stmmac_priv *priv, u32 queue)
1527 {
1528 	int i;
1529 
1530 	for (i = 0; i < priv->dma_rx_size; i++)
1531 		stmmac_free_rx_buffer(priv, queue, i);
1532 }
1533 
1534 static int stmmac_alloc_rx_buffers(struct stmmac_priv *priv, u32 queue,
1535 				   gfp_t flags)
1536 {
1537 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1538 	int i;
1539 
1540 	for (i = 0; i < priv->dma_rx_size; i++) {
1541 		struct dma_desc *p;
1542 		int ret;
1543 
1544 		if (priv->extend_desc)
1545 			p = &((rx_q->dma_erx + i)->basic);
1546 		else
1547 			p = rx_q->dma_rx + i;
1548 
1549 		ret = stmmac_init_rx_buffers(priv, p, i, flags,
1550 					     queue);
1551 		if (ret)
1552 			return ret;
1553 
1554 		rx_q->buf_alloc_num++;
1555 	}
1556 
1557 	return 0;
1558 }
1559 
1560 /**
1561  * dma_free_rx_xskbufs - free RX dma buffers from XSK pool
1562  * @priv: private structure
1563  * @queue: RX queue index
1564  */
1565 static void dma_free_rx_xskbufs(struct stmmac_priv *priv, u32 queue)
1566 {
1567 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1568 	int i;
1569 
1570 	for (i = 0; i < priv->dma_rx_size; i++) {
1571 		struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1572 
1573 		if (!buf->xdp)
1574 			continue;
1575 
1576 		xsk_buff_free(buf->xdp);
1577 		buf->xdp = NULL;
1578 	}
1579 }
1580 
1581 static int stmmac_alloc_rx_buffers_zc(struct stmmac_priv *priv, u32 queue)
1582 {
1583 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1584 	int i;
1585 
1586 	for (i = 0; i < priv->dma_rx_size; i++) {
1587 		struct stmmac_rx_buffer *buf;
1588 		dma_addr_t dma_addr;
1589 		struct dma_desc *p;
1590 
1591 		if (priv->extend_desc)
1592 			p = (struct dma_desc *)(rx_q->dma_erx + i);
1593 		else
1594 			p = rx_q->dma_rx + i;
1595 
1596 		buf = &rx_q->buf_pool[i];
1597 
1598 		buf->xdp = xsk_buff_alloc(rx_q->xsk_pool);
1599 		if (!buf->xdp)
1600 			return -ENOMEM;
1601 
1602 		dma_addr = xsk_buff_xdp_get_dma(buf->xdp);
1603 		stmmac_set_desc_addr(priv, p, dma_addr);
1604 		rx_q->buf_alloc_num++;
1605 	}
1606 
1607 	return 0;
1608 }
1609 
1610 static struct xsk_buff_pool *stmmac_get_xsk_pool(struct stmmac_priv *priv, u32 queue)
1611 {
1612 	if (!stmmac_xdp_is_enabled(priv) || !test_bit(queue, priv->af_xdp_zc_qps))
1613 		return NULL;
1614 
1615 	return xsk_get_pool_from_qid(priv->dev, queue);
1616 }
1617 
1618 /**
1619  * __init_dma_rx_desc_rings - init the RX descriptor ring (per queue)
1620  * @priv: driver private structure
1621  * @queue: RX queue index
1622  * @flags: gfp flag.
1623  * Description: this function initializes the DMA RX descriptors
1624  * and allocates the socket buffers. It supports the chained and ring
1625  * modes.
1626  */
1627 static int __init_dma_rx_desc_rings(struct stmmac_priv *priv, u32 queue, gfp_t flags)
1628 {
1629 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1630 	int ret;
1631 
1632 	netif_dbg(priv, probe, priv->dev,
1633 		  "(%s) dma_rx_phy=0x%08x\n", __func__,
1634 		  (u32)rx_q->dma_rx_phy);
1635 
1636 	stmmac_clear_rx_descriptors(priv, queue);
1637 
1638 	xdp_rxq_info_unreg_mem_model(&rx_q->xdp_rxq);
1639 
1640 	rx_q->xsk_pool = stmmac_get_xsk_pool(priv, queue);
1641 
1642 	if (rx_q->xsk_pool) {
1643 		WARN_ON(xdp_rxq_info_reg_mem_model(&rx_q->xdp_rxq,
1644 						   MEM_TYPE_XSK_BUFF_POOL,
1645 						   NULL));
1646 		netdev_info(priv->dev,
1647 			    "Register MEM_TYPE_XSK_BUFF_POOL RxQ-%d\n",
1648 			    rx_q->queue_index);
1649 		xsk_pool_set_rxq_info(rx_q->xsk_pool, &rx_q->xdp_rxq);
1650 	} else {
1651 		WARN_ON(xdp_rxq_info_reg_mem_model(&rx_q->xdp_rxq,
1652 						   MEM_TYPE_PAGE_POOL,
1653 						   rx_q->page_pool));
1654 		netdev_info(priv->dev,
1655 			    "Register MEM_TYPE_PAGE_POOL RxQ-%d\n",
1656 			    rx_q->queue_index);
1657 	}
1658 
1659 	if (rx_q->xsk_pool) {
1660 		/* RX XDP ZC buffer pool may not be populated, e.g.
1661 		 * xdpsock TX-only.
1662 		 */
1663 		stmmac_alloc_rx_buffers_zc(priv, queue);
1664 	} else {
1665 		ret = stmmac_alloc_rx_buffers(priv, queue, flags);
1666 		if (ret < 0)
1667 			return -ENOMEM;
1668 	}
1669 
1670 	rx_q->cur_rx = 0;
1671 	rx_q->dirty_rx = 0;
1672 
1673 	/* Setup the chained descriptor addresses */
1674 	if (priv->mode == STMMAC_CHAIN_MODE) {
1675 		if (priv->extend_desc)
1676 			stmmac_mode_init(priv, rx_q->dma_erx,
1677 					 rx_q->dma_rx_phy,
1678 					 priv->dma_rx_size, 1);
1679 		else
1680 			stmmac_mode_init(priv, rx_q->dma_rx,
1681 					 rx_q->dma_rx_phy,
1682 					 priv->dma_rx_size, 0);
1683 	}
1684 
1685 	return 0;
1686 }
1687 
1688 static int init_dma_rx_desc_rings(struct net_device *dev, gfp_t flags)
1689 {
1690 	struct stmmac_priv *priv = netdev_priv(dev);
1691 	u32 rx_count = priv->plat->rx_queues_to_use;
1692 	u32 queue;
1693 	int ret;
1694 
1695 	/* RX INITIALIZATION */
1696 	netif_dbg(priv, probe, priv->dev,
1697 		  "SKB addresses:\nskb\t\tskb data\tdma data\n");
1698 
1699 	for (queue = 0; queue < rx_count; queue++) {
1700 		ret = __init_dma_rx_desc_rings(priv, queue, flags);
1701 		if (ret)
1702 			goto err_init_rx_buffers;
1703 	}
1704 
1705 	return 0;
1706 
1707 err_init_rx_buffers:
1708 	while (queue >= 0) {
1709 		struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1710 
1711 		if (rx_q->xsk_pool)
1712 			dma_free_rx_xskbufs(priv, queue);
1713 		else
1714 			dma_free_rx_skbufs(priv, queue);
1715 
1716 		rx_q->buf_alloc_num = 0;
1717 		rx_q->xsk_pool = NULL;
1718 
1719 		if (queue == 0)
1720 			break;
1721 
1722 		queue--;
1723 	}
1724 
1725 	return ret;
1726 }
1727 
1728 /**
1729  * __init_dma_tx_desc_rings - init the TX descriptor ring (per queue)
1730  * @priv: driver private structure
1731  * @queue : TX queue index
1732  * Description: this function initializes the DMA TX descriptors
1733  * and allocates the socket buffers. It supports the chained and ring
1734  * modes.
1735  */
1736 static int __init_dma_tx_desc_rings(struct stmmac_priv *priv, u32 queue)
1737 {
1738 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1739 	int i;
1740 
1741 	netif_dbg(priv, probe, priv->dev,
1742 		  "(%s) dma_tx_phy=0x%08x\n", __func__,
1743 		  (u32)tx_q->dma_tx_phy);
1744 
1745 	/* Setup the chained descriptor addresses */
1746 	if (priv->mode == STMMAC_CHAIN_MODE) {
1747 		if (priv->extend_desc)
1748 			stmmac_mode_init(priv, tx_q->dma_etx,
1749 					 tx_q->dma_tx_phy,
1750 					 priv->dma_tx_size, 1);
1751 		else if (!(tx_q->tbs & STMMAC_TBS_AVAIL))
1752 			stmmac_mode_init(priv, tx_q->dma_tx,
1753 					 tx_q->dma_tx_phy,
1754 					 priv->dma_tx_size, 0);
1755 	}
1756 
1757 	tx_q->xsk_pool = stmmac_get_xsk_pool(priv, queue);
1758 
1759 	for (i = 0; i < priv->dma_tx_size; i++) {
1760 		struct dma_desc *p;
1761 
1762 		if (priv->extend_desc)
1763 			p = &((tx_q->dma_etx + i)->basic);
1764 		else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1765 			p = &((tx_q->dma_entx + i)->basic);
1766 		else
1767 			p = tx_q->dma_tx + i;
1768 
1769 		stmmac_clear_desc(priv, p);
1770 
1771 		tx_q->tx_skbuff_dma[i].buf = 0;
1772 		tx_q->tx_skbuff_dma[i].map_as_page = false;
1773 		tx_q->tx_skbuff_dma[i].len = 0;
1774 		tx_q->tx_skbuff_dma[i].last_segment = false;
1775 		tx_q->tx_skbuff[i] = NULL;
1776 	}
1777 
1778 	tx_q->dirty_tx = 0;
1779 	tx_q->cur_tx = 0;
1780 	tx_q->mss = 0;
1781 
1782 	netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
1783 
1784 	return 0;
1785 }
1786 
1787 static int init_dma_tx_desc_rings(struct net_device *dev)
1788 {
1789 	struct stmmac_priv *priv = netdev_priv(dev);
1790 	u32 tx_queue_cnt;
1791 	u32 queue;
1792 
1793 	tx_queue_cnt = priv->plat->tx_queues_to_use;
1794 
1795 	for (queue = 0; queue < tx_queue_cnt; queue++)
1796 		__init_dma_tx_desc_rings(priv, queue);
1797 
1798 	return 0;
1799 }
1800 
1801 /**
1802  * init_dma_desc_rings - init the RX/TX descriptor rings
1803  * @dev: net device structure
1804  * @flags: gfp flag.
1805  * Description: this function initializes the DMA RX/TX descriptors
1806  * and allocates the socket buffers. It supports the chained and ring
1807  * modes.
1808  */
1809 static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
1810 {
1811 	struct stmmac_priv *priv = netdev_priv(dev);
1812 	int ret;
1813 
1814 	ret = init_dma_rx_desc_rings(dev, flags);
1815 	if (ret)
1816 		return ret;
1817 
1818 	ret = init_dma_tx_desc_rings(dev);
1819 
1820 	stmmac_clear_descriptors(priv);
1821 
1822 	if (netif_msg_hw(priv))
1823 		stmmac_display_rings(priv);
1824 
1825 	return ret;
1826 }
1827 
1828 /**
1829  * dma_free_tx_skbufs - free TX dma buffers
1830  * @priv: private structure
1831  * @queue: TX queue index
1832  */
1833 static void dma_free_tx_skbufs(struct stmmac_priv *priv, u32 queue)
1834 {
1835 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1836 	int i;
1837 
1838 	tx_q->xsk_frames_done = 0;
1839 
1840 	for (i = 0; i < priv->dma_tx_size; i++)
1841 		stmmac_free_tx_buffer(priv, queue, i);
1842 
1843 	if (tx_q->xsk_pool && tx_q->xsk_frames_done) {
1844 		xsk_tx_completed(tx_q->xsk_pool, tx_q->xsk_frames_done);
1845 		tx_q->xsk_frames_done = 0;
1846 		tx_q->xsk_pool = NULL;
1847 	}
1848 }
1849 
1850 /**
1851  * stmmac_free_tx_skbufs - free TX skb buffers
1852  * @priv: private structure
1853  */
1854 static void stmmac_free_tx_skbufs(struct stmmac_priv *priv)
1855 {
1856 	u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1857 	u32 queue;
1858 
1859 	for (queue = 0; queue < tx_queue_cnt; queue++)
1860 		dma_free_tx_skbufs(priv, queue);
1861 }
1862 
1863 /**
1864  * __free_dma_rx_desc_resources - free RX dma desc resources (per queue)
1865  * @priv: private structure
1866  * @queue: RX queue index
1867  */
1868 static void __free_dma_rx_desc_resources(struct stmmac_priv *priv, u32 queue)
1869 {
1870 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1871 
1872 	/* Release the DMA RX socket buffers */
1873 	if (rx_q->xsk_pool)
1874 		dma_free_rx_xskbufs(priv, queue);
1875 	else
1876 		dma_free_rx_skbufs(priv, queue);
1877 
1878 	rx_q->buf_alloc_num = 0;
1879 	rx_q->xsk_pool = NULL;
1880 
1881 	/* Free DMA regions of consistent memory previously allocated */
1882 	if (!priv->extend_desc)
1883 		dma_free_coherent(priv->device, priv->dma_rx_size *
1884 				  sizeof(struct dma_desc),
1885 				  rx_q->dma_rx, rx_q->dma_rx_phy);
1886 	else
1887 		dma_free_coherent(priv->device, priv->dma_rx_size *
1888 				  sizeof(struct dma_extended_desc),
1889 				  rx_q->dma_erx, rx_q->dma_rx_phy);
1890 
1891 	if (xdp_rxq_info_is_reg(&rx_q->xdp_rxq))
1892 		xdp_rxq_info_unreg(&rx_q->xdp_rxq);
1893 
1894 	kfree(rx_q->buf_pool);
1895 	if (rx_q->page_pool)
1896 		page_pool_destroy(rx_q->page_pool);
1897 }
1898 
1899 static void free_dma_rx_desc_resources(struct stmmac_priv *priv)
1900 {
1901 	u32 rx_count = priv->plat->rx_queues_to_use;
1902 	u32 queue;
1903 
1904 	/* Free RX queue resources */
1905 	for (queue = 0; queue < rx_count; queue++)
1906 		__free_dma_rx_desc_resources(priv, queue);
1907 }
1908 
1909 /**
1910  * __free_dma_tx_desc_resources - free TX dma desc resources (per queue)
1911  * @priv: private structure
1912  * @queue: TX queue index
1913  */
1914 static void __free_dma_tx_desc_resources(struct stmmac_priv *priv, u32 queue)
1915 {
1916 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1917 	size_t size;
1918 	void *addr;
1919 
1920 	/* Release the DMA TX socket buffers */
1921 	dma_free_tx_skbufs(priv, queue);
1922 
1923 	if (priv->extend_desc) {
1924 		size = sizeof(struct dma_extended_desc);
1925 		addr = tx_q->dma_etx;
1926 	} else if (tx_q->tbs & STMMAC_TBS_AVAIL) {
1927 		size = sizeof(struct dma_edesc);
1928 		addr = tx_q->dma_entx;
1929 	} else {
1930 		size = sizeof(struct dma_desc);
1931 		addr = tx_q->dma_tx;
1932 	}
1933 
1934 	size *= priv->dma_tx_size;
1935 
1936 	dma_free_coherent(priv->device, size, addr, tx_q->dma_tx_phy);
1937 
1938 	kfree(tx_q->tx_skbuff_dma);
1939 	kfree(tx_q->tx_skbuff);
1940 }
1941 
1942 static void free_dma_tx_desc_resources(struct stmmac_priv *priv)
1943 {
1944 	u32 tx_count = priv->plat->tx_queues_to_use;
1945 	u32 queue;
1946 
1947 	/* Free TX queue resources */
1948 	for (queue = 0; queue < tx_count; queue++)
1949 		__free_dma_tx_desc_resources(priv, queue);
1950 }
1951 
1952 /**
1953  * __alloc_dma_rx_desc_resources - alloc RX resources (per queue).
1954  * @priv: private structure
1955  * @queue: RX queue index
1956  * Description: according to which descriptor can be used (extend or basic)
1957  * this function allocates the resources for TX and RX paths. In case of
1958  * reception, for example, it pre-allocated the RX socket buffer in order to
1959  * allow zero-copy mechanism.
1960  */
1961 static int __alloc_dma_rx_desc_resources(struct stmmac_priv *priv, u32 queue)
1962 {
1963 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1964 	struct stmmac_channel *ch = &priv->channel[queue];
1965 	bool xdp_prog = stmmac_xdp_is_enabled(priv);
1966 	struct page_pool_params pp_params = { 0 };
1967 	unsigned int num_pages;
1968 	unsigned int napi_id;
1969 	int ret;
1970 
1971 	rx_q->queue_index = queue;
1972 	rx_q->priv_data = priv;
1973 
1974 	pp_params.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
1975 	pp_params.pool_size = priv->dma_rx_size;
1976 	num_pages = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE);
1977 	pp_params.order = ilog2(num_pages);
1978 	pp_params.nid = dev_to_node(priv->device);
1979 	pp_params.dev = priv->device;
1980 	pp_params.dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
1981 	pp_params.offset = stmmac_rx_offset(priv);
1982 	pp_params.max_len = STMMAC_MAX_RX_BUF_SIZE(num_pages);
1983 
1984 	rx_q->page_pool = page_pool_create(&pp_params);
1985 	if (IS_ERR(rx_q->page_pool)) {
1986 		ret = PTR_ERR(rx_q->page_pool);
1987 		rx_q->page_pool = NULL;
1988 		return ret;
1989 	}
1990 
1991 	rx_q->buf_pool = kcalloc(priv->dma_rx_size,
1992 				 sizeof(*rx_q->buf_pool),
1993 				 GFP_KERNEL);
1994 	if (!rx_q->buf_pool)
1995 		return -ENOMEM;
1996 
1997 	if (priv->extend_desc) {
1998 		rx_q->dma_erx = dma_alloc_coherent(priv->device,
1999 						   priv->dma_rx_size *
2000 						   sizeof(struct dma_extended_desc),
2001 						   &rx_q->dma_rx_phy,
2002 						   GFP_KERNEL);
2003 		if (!rx_q->dma_erx)
2004 			return -ENOMEM;
2005 
2006 	} else {
2007 		rx_q->dma_rx = dma_alloc_coherent(priv->device,
2008 						  priv->dma_rx_size *
2009 						  sizeof(struct dma_desc),
2010 						  &rx_q->dma_rx_phy,
2011 						  GFP_KERNEL);
2012 		if (!rx_q->dma_rx)
2013 			return -ENOMEM;
2014 	}
2015 
2016 	if (stmmac_xdp_is_enabled(priv) &&
2017 	    test_bit(queue, priv->af_xdp_zc_qps))
2018 		napi_id = ch->rxtx_napi.napi_id;
2019 	else
2020 		napi_id = ch->rx_napi.napi_id;
2021 
2022 	ret = xdp_rxq_info_reg(&rx_q->xdp_rxq, priv->dev,
2023 			       rx_q->queue_index,
2024 			       napi_id);
2025 	if (ret) {
2026 		netdev_err(priv->dev, "Failed to register xdp rxq info\n");
2027 		return -EINVAL;
2028 	}
2029 
2030 	return 0;
2031 }
2032 
2033 static int alloc_dma_rx_desc_resources(struct stmmac_priv *priv)
2034 {
2035 	u32 rx_count = priv->plat->rx_queues_to_use;
2036 	u32 queue;
2037 	int ret;
2038 
2039 	/* RX queues buffers and DMA */
2040 	for (queue = 0; queue < rx_count; queue++) {
2041 		ret = __alloc_dma_rx_desc_resources(priv, queue);
2042 		if (ret)
2043 			goto err_dma;
2044 	}
2045 
2046 	return 0;
2047 
2048 err_dma:
2049 	free_dma_rx_desc_resources(priv);
2050 
2051 	return ret;
2052 }
2053 
2054 /**
2055  * __alloc_dma_tx_desc_resources - alloc TX resources (per queue).
2056  * @priv: private structure
2057  * @queue: TX queue index
2058  * Description: according to which descriptor can be used (extend or basic)
2059  * this function allocates the resources for TX and RX paths. In case of
2060  * reception, for example, it pre-allocated the RX socket buffer in order to
2061  * allow zero-copy mechanism.
2062  */
2063 static int __alloc_dma_tx_desc_resources(struct stmmac_priv *priv, u32 queue)
2064 {
2065 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2066 	size_t size;
2067 	void *addr;
2068 
2069 	tx_q->queue_index = queue;
2070 	tx_q->priv_data = priv;
2071 
2072 	tx_q->tx_skbuff_dma = kcalloc(priv->dma_tx_size,
2073 				      sizeof(*tx_q->tx_skbuff_dma),
2074 				      GFP_KERNEL);
2075 	if (!tx_q->tx_skbuff_dma)
2076 		return -ENOMEM;
2077 
2078 	tx_q->tx_skbuff = kcalloc(priv->dma_tx_size,
2079 				  sizeof(struct sk_buff *),
2080 				  GFP_KERNEL);
2081 	if (!tx_q->tx_skbuff)
2082 		return -ENOMEM;
2083 
2084 	if (priv->extend_desc)
2085 		size = sizeof(struct dma_extended_desc);
2086 	else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2087 		size = sizeof(struct dma_edesc);
2088 	else
2089 		size = sizeof(struct dma_desc);
2090 
2091 	size *= priv->dma_tx_size;
2092 
2093 	addr = dma_alloc_coherent(priv->device, size,
2094 				  &tx_q->dma_tx_phy, GFP_KERNEL);
2095 	if (!addr)
2096 		return -ENOMEM;
2097 
2098 	if (priv->extend_desc)
2099 		tx_q->dma_etx = addr;
2100 	else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2101 		tx_q->dma_entx = addr;
2102 	else
2103 		tx_q->dma_tx = addr;
2104 
2105 	return 0;
2106 }
2107 
2108 static int alloc_dma_tx_desc_resources(struct stmmac_priv *priv)
2109 {
2110 	u32 tx_count = priv->plat->tx_queues_to_use;
2111 	u32 queue;
2112 	int ret;
2113 
2114 	/* TX queues buffers and DMA */
2115 	for (queue = 0; queue < tx_count; queue++) {
2116 		ret = __alloc_dma_tx_desc_resources(priv, queue);
2117 		if (ret)
2118 			goto err_dma;
2119 	}
2120 
2121 	return 0;
2122 
2123 err_dma:
2124 	free_dma_tx_desc_resources(priv);
2125 	return ret;
2126 }
2127 
2128 /**
2129  * alloc_dma_desc_resources - alloc TX/RX resources.
2130  * @priv: private structure
2131  * Description: according to which descriptor can be used (extend or basic)
2132  * this function allocates the resources for TX and RX paths. In case of
2133  * reception, for example, it pre-allocated the RX socket buffer in order to
2134  * allow zero-copy mechanism.
2135  */
2136 static int alloc_dma_desc_resources(struct stmmac_priv *priv)
2137 {
2138 	/* RX Allocation */
2139 	int ret = alloc_dma_rx_desc_resources(priv);
2140 
2141 	if (ret)
2142 		return ret;
2143 
2144 	ret = alloc_dma_tx_desc_resources(priv);
2145 
2146 	return ret;
2147 }
2148 
2149 /**
2150  * free_dma_desc_resources - free dma desc resources
2151  * @priv: private structure
2152  */
2153 static void free_dma_desc_resources(struct stmmac_priv *priv)
2154 {
2155 	/* Release the DMA TX socket buffers */
2156 	free_dma_tx_desc_resources(priv);
2157 
2158 	/* Release the DMA RX socket buffers later
2159 	 * to ensure all pending XDP_TX buffers are returned.
2160 	 */
2161 	free_dma_rx_desc_resources(priv);
2162 }
2163 
2164 /**
2165  *  stmmac_mac_enable_rx_queues - Enable MAC rx queues
2166  *  @priv: driver private structure
2167  *  Description: It is used for enabling the rx queues in the MAC
2168  */
2169 static void stmmac_mac_enable_rx_queues(struct stmmac_priv *priv)
2170 {
2171 	u32 rx_queues_count = priv->plat->rx_queues_to_use;
2172 	int queue;
2173 	u8 mode;
2174 
2175 	for (queue = 0; queue < rx_queues_count; queue++) {
2176 		mode = priv->plat->rx_queues_cfg[queue].mode_to_use;
2177 		stmmac_rx_queue_enable(priv, priv->hw, mode, queue);
2178 	}
2179 }
2180 
2181 /**
2182  * stmmac_start_rx_dma - start RX DMA channel
2183  * @priv: driver private structure
2184  * @chan: RX channel index
2185  * Description:
2186  * This starts a RX DMA channel
2187  */
2188 static void stmmac_start_rx_dma(struct stmmac_priv *priv, u32 chan)
2189 {
2190 	netdev_dbg(priv->dev, "DMA RX processes started in channel %d\n", chan);
2191 	stmmac_start_rx(priv, priv->ioaddr, chan);
2192 }
2193 
2194 /**
2195  * stmmac_start_tx_dma - start TX DMA channel
2196  * @priv: driver private structure
2197  * @chan: TX channel index
2198  * Description:
2199  * This starts a TX DMA channel
2200  */
2201 static void stmmac_start_tx_dma(struct stmmac_priv *priv, u32 chan)
2202 {
2203 	netdev_dbg(priv->dev, "DMA TX processes started in channel %d\n", chan);
2204 	stmmac_start_tx(priv, priv->ioaddr, chan);
2205 }
2206 
2207 /**
2208  * stmmac_stop_rx_dma - stop RX DMA channel
2209  * @priv: driver private structure
2210  * @chan: RX channel index
2211  * Description:
2212  * This stops a RX DMA channel
2213  */
2214 static void stmmac_stop_rx_dma(struct stmmac_priv *priv, u32 chan)
2215 {
2216 	netdev_dbg(priv->dev, "DMA RX processes stopped in channel %d\n", chan);
2217 	stmmac_stop_rx(priv, priv->ioaddr, chan);
2218 }
2219 
2220 /**
2221  * stmmac_stop_tx_dma - stop TX DMA channel
2222  * @priv: driver private structure
2223  * @chan: TX channel index
2224  * Description:
2225  * This stops a TX DMA channel
2226  */
2227 static void stmmac_stop_tx_dma(struct stmmac_priv *priv, u32 chan)
2228 {
2229 	netdev_dbg(priv->dev, "DMA TX processes stopped in channel %d\n", chan);
2230 	stmmac_stop_tx(priv, priv->ioaddr, chan);
2231 }
2232 
2233 /**
2234  * stmmac_start_all_dma - start all RX and TX DMA channels
2235  * @priv: driver private structure
2236  * Description:
2237  * This starts all the RX and TX DMA channels
2238  */
2239 static void stmmac_start_all_dma(struct stmmac_priv *priv)
2240 {
2241 	u32 rx_channels_count = priv->plat->rx_queues_to_use;
2242 	u32 tx_channels_count = priv->plat->tx_queues_to_use;
2243 	u32 chan = 0;
2244 
2245 	for (chan = 0; chan < rx_channels_count; chan++)
2246 		stmmac_start_rx_dma(priv, chan);
2247 
2248 	for (chan = 0; chan < tx_channels_count; chan++)
2249 		stmmac_start_tx_dma(priv, chan);
2250 }
2251 
2252 /**
2253  * stmmac_stop_all_dma - stop all RX and TX DMA channels
2254  * @priv: driver private structure
2255  * Description:
2256  * This stops the RX and TX DMA channels
2257  */
2258 static void stmmac_stop_all_dma(struct stmmac_priv *priv)
2259 {
2260 	u32 rx_channels_count = priv->plat->rx_queues_to_use;
2261 	u32 tx_channels_count = priv->plat->tx_queues_to_use;
2262 	u32 chan = 0;
2263 
2264 	for (chan = 0; chan < rx_channels_count; chan++)
2265 		stmmac_stop_rx_dma(priv, chan);
2266 
2267 	for (chan = 0; chan < tx_channels_count; chan++)
2268 		stmmac_stop_tx_dma(priv, chan);
2269 }
2270 
2271 /**
2272  *  stmmac_dma_operation_mode - HW DMA operation mode
2273  *  @priv: driver private structure
2274  *  Description: it is used for configuring the DMA operation mode register in
2275  *  order to program the tx/rx DMA thresholds or Store-And-Forward mode.
2276  */
2277 static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
2278 {
2279 	u32 rx_channels_count = priv->plat->rx_queues_to_use;
2280 	u32 tx_channels_count = priv->plat->tx_queues_to_use;
2281 	int rxfifosz = priv->plat->rx_fifo_size;
2282 	int txfifosz = priv->plat->tx_fifo_size;
2283 	u32 txmode = 0;
2284 	u32 rxmode = 0;
2285 	u32 chan = 0;
2286 	u8 qmode = 0;
2287 
2288 	if (rxfifosz == 0)
2289 		rxfifosz = priv->dma_cap.rx_fifo_size;
2290 	if (txfifosz == 0)
2291 		txfifosz = priv->dma_cap.tx_fifo_size;
2292 
2293 	/* Adjust for real per queue fifo size */
2294 	rxfifosz /= rx_channels_count;
2295 	txfifosz /= tx_channels_count;
2296 
2297 	if (priv->plat->force_thresh_dma_mode) {
2298 		txmode = tc;
2299 		rxmode = tc;
2300 	} else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
2301 		/*
2302 		 * In case of GMAC, SF mode can be enabled
2303 		 * to perform the TX COE in HW. This depends on:
2304 		 * 1) TX COE if actually supported
2305 		 * 2) There is no bugged Jumbo frame support
2306 		 *    that needs to not insert csum in the TDES.
2307 		 */
2308 		txmode = SF_DMA_MODE;
2309 		rxmode = SF_DMA_MODE;
2310 		priv->xstats.threshold = SF_DMA_MODE;
2311 	} else {
2312 		txmode = tc;
2313 		rxmode = SF_DMA_MODE;
2314 	}
2315 
2316 	/* configure all channels */
2317 	for (chan = 0; chan < rx_channels_count; chan++) {
2318 		struct stmmac_rx_queue *rx_q = &priv->rx_queue[chan];
2319 		u32 buf_size;
2320 
2321 		qmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
2322 
2323 		stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan,
2324 				rxfifosz, qmode);
2325 
2326 		if (rx_q->xsk_pool) {
2327 			buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool);
2328 			stmmac_set_dma_bfsize(priv, priv->ioaddr,
2329 					      buf_size,
2330 					      chan);
2331 		} else {
2332 			stmmac_set_dma_bfsize(priv, priv->ioaddr,
2333 					      priv->dma_buf_sz,
2334 					      chan);
2335 		}
2336 	}
2337 
2338 	for (chan = 0; chan < tx_channels_count; chan++) {
2339 		qmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
2340 
2341 		stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan,
2342 				txfifosz, qmode);
2343 	}
2344 }
2345 
2346 static bool stmmac_xdp_xmit_zc(struct stmmac_priv *priv, u32 queue, u32 budget)
2347 {
2348 	struct netdev_queue *nq = netdev_get_tx_queue(priv->dev, queue);
2349 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2350 	struct xsk_buff_pool *pool = tx_q->xsk_pool;
2351 	unsigned int entry = tx_q->cur_tx;
2352 	struct dma_desc *tx_desc = NULL;
2353 	struct xdp_desc xdp_desc;
2354 	bool work_done = true;
2355 
2356 	/* Avoids TX time-out as we are sharing with slow path */
2357 	nq->trans_start = jiffies;
2358 
2359 	budget = min(budget, stmmac_tx_avail(priv, queue));
2360 
2361 	while (budget-- > 0) {
2362 		dma_addr_t dma_addr;
2363 		bool set_ic;
2364 
2365 		/* We are sharing with slow path and stop XSK TX desc submission when
2366 		 * available TX ring is less than threshold.
2367 		 */
2368 		if (unlikely(stmmac_tx_avail(priv, queue) < STMMAC_TX_XSK_AVAIL) ||
2369 		    !netif_carrier_ok(priv->dev)) {
2370 			work_done = false;
2371 			break;
2372 		}
2373 
2374 		if (!xsk_tx_peek_desc(pool, &xdp_desc))
2375 			break;
2376 
2377 		if (likely(priv->extend_desc))
2378 			tx_desc = (struct dma_desc *)(tx_q->dma_etx + entry);
2379 		else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2380 			tx_desc = &tx_q->dma_entx[entry].basic;
2381 		else
2382 			tx_desc = tx_q->dma_tx + entry;
2383 
2384 		dma_addr = xsk_buff_raw_get_dma(pool, xdp_desc.addr);
2385 		xsk_buff_raw_dma_sync_for_device(pool, dma_addr, xdp_desc.len);
2386 
2387 		tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XSK_TX;
2388 
2389 		/* To return XDP buffer to XSK pool, we simple call
2390 		 * xsk_tx_completed(), so we don't need to fill up
2391 		 * 'buf' and 'xdpf'.
2392 		 */
2393 		tx_q->tx_skbuff_dma[entry].buf = 0;
2394 		tx_q->xdpf[entry] = NULL;
2395 
2396 		tx_q->tx_skbuff_dma[entry].map_as_page = false;
2397 		tx_q->tx_skbuff_dma[entry].len = xdp_desc.len;
2398 		tx_q->tx_skbuff_dma[entry].last_segment = true;
2399 		tx_q->tx_skbuff_dma[entry].is_jumbo = false;
2400 
2401 		stmmac_set_desc_addr(priv, tx_desc, dma_addr);
2402 
2403 		tx_q->tx_count_frames++;
2404 
2405 		if (!priv->tx_coal_frames[queue])
2406 			set_ic = false;
2407 		else if (tx_q->tx_count_frames % priv->tx_coal_frames[queue] == 0)
2408 			set_ic = true;
2409 		else
2410 			set_ic = false;
2411 
2412 		if (set_ic) {
2413 			tx_q->tx_count_frames = 0;
2414 			stmmac_set_tx_ic(priv, tx_desc);
2415 			priv->xstats.tx_set_ic_bit++;
2416 		}
2417 
2418 		stmmac_prepare_tx_desc(priv, tx_desc, 1, xdp_desc.len,
2419 				       true, priv->mode, true, true,
2420 				       xdp_desc.len);
2421 
2422 		stmmac_enable_dma_transmission(priv, priv->ioaddr);
2423 
2424 		tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
2425 		entry = tx_q->cur_tx;
2426 	}
2427 
2428 	if (tx_desc) {
2429 		stmmac_flush_tx_descriptors(priv, queue);
2430 		xsk_tx_release(pool);
2431 	}
2432 
2433 	/* Return true if all of the 3 conditions are met
2434 	 *  a) TX Budget is still available
2435 	 *  b) work_done = true when XSK TX desc peek is empty (no more
2436 	 *     pending XSK TX for transmission)
2437 	 */
2438 	return !!budget && work_done;
2439 }
2440 
2441 /**
2442  * stmmac_tx_clean - to manage the transmission completion
2443  * @priv: driver private structure
2444  * @budget: napi budget limiting this functions packet handling
2445  * @queue: TX queue index
2446  * Description: it reclaims the transmit resources after transmission completes.
2447  */
2448 static int stmmac_tx_clean(struct stmmac_priv *priv, int budget, u32 queue)
2449 {
2450 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2451 	unsigned int bytes_compl = 0, pkts_compl = 0;
2452 	unsigned int entry, xmits = 0, count = 0;
2453 
2454 	__netif_tx_lock_bh(netdev_get_tx_queue(priv->dev, queue));
2455 
2456 	priv->xstats.tx_clean++;
2457 
2458 	tx_q->xsk_frames_done = 0;
2459 
2460 	entry = tx_q->dirty_tx;
2461 
2462 	/* Try to clean all TX complete frame in 1 shot */
2463 	while ((entry != tx_q->cur_tx) && count < priv->dma_tx_size) {
2464 		struct xdp_frame *xdpf;
2465 		struct sk_buff *skb;
2466 		struct dma_desc *p;
2467 		int status;
2468 
2469 		if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_TX ||
2470 		    tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_NDO) {
2471 			xdpf = tx_q->xdpf[entry];
2472 			skb = NULL;
2473 		} else if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_SKB) {
2474 			xdpf = NULL;
2475 			skb = tx_q->tx_skbuff[entry];
2476 		} else {
2477 			xdpf = NULL;
2478 			skb = NULL;
2479 		}
2480 
2481 		if (priv->extend_desc)
2482 			p = (struct dma_desc *)(tx_q->dma_etx + entry);
2483 		else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2484 			p = &tx_q->dma_entx[entry].basic;
2485 		else
2486 			p = tx_q->dma_tx + entry;
2487 
2488 		status = stmmac_tx_status(priv, &priv->dev->stats,
2489 				&priv->xstats, p, priv->ioaddr);
2490 		/* Check if the descriptor is owned by the DMA */
2491 		if (unlikely(status & tx_dma_own))
2492 			break;
2493 
2494 		count++;
2495 
2496 		/* Make sure descriptor fields are read after reading
2497 		 * the own bit.
2498 		 */
2499 		dma_rmb();
2500 
2501 		/* Just consider the last segment and ...*/
2502 		if (likely(!(status & tx_not_ls))) {
2503 			/* ... verify the status error condition */
2504 			if (unlikely(status & tx_err)) {
2505 				priv->dev->stats.tx_errors++;
2506 			} else {
2507 				priv->dev->stats.tx_packets++;
2508 				priv->xstats.tx_pkt_n++;
2509 			}
2510 			if (skb)
2511 				stmmac_get_tx_hwtstamp(priv, p, skb);
2512 		}
2513 
2514 		if (likely(tx_q->tx_skbuff_dma[entry].buf &&
2515 			   tx_q->tx_skbuff_dma[entry].buf_type != STMMAC_TXBUF_T_XDP_TX)) {
2516 			if (tx_q->tx_skbuff_dma[entry].map_as_page)
2517 				dma_unmap_page(priv->device,
2518 					       tx_q->tx_skbuff_dma[entry].buf,
2519 					       tx_q->tx_skbuff_dma[entry].len,
2520 					       DMA_TO_DEVICE);
2521 			else
2522 				dma_unmap_single(priv->device,
2523 						 tx_q->tx_skbuff_dma[entry].buf,
2524 						 tx_q->tx_skbuff_dma[entry].len,
2525 						 DMA_TO_DEVICE);
2526 			tx_q->tx_skbuff_dma[entry].buf = 0;
2527 			tx_q->tx_skbuff_dma[entry].len = 0;
2528 			tx_q->tx_skbuff_dma[entry].map_as_page = false;
2529 		}
2530 
2531 		stmmac_clean_desc3(priv, tx_q, p);
2532 
2533 		tx_q->tx_skbuff_dma[entry].last_segment = false;
2534 		tx_q->tx_skbuff_dma[entry].is_jumbo = false;
2535 
2536 		if (xdpf &&
2537 		    tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_TX) {
2538 			xdp_return_frame_rx_napi(xdpf);
2539 			tx_q->xdpf[entry] = NULL;
2540 		}
2541 
2542 		if (xdpf &&
2543 		    tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_NDO) {
2544 			xdp_return_frame(xdpf);
2545 			tx_q->xdpf[entry] = NULL;
2546 		}
2547 
2548 		if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XSK_TX)
2549 			tx_q->xsk_frames_done++;
2550 
2551 		if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_SKB) {
2552 			if (likely(skb)) {
2553 				pkts_compl++;
2554 				bytes_compl += skb->len;
2555 				dev_consume_skb_any(skb);
2556 				tx_q->tx_skbuff[entry] = NULL;
2557 			}
2558 		}
2559 
2560 		stmmac_release_tx_desc(priv, p, priv->mode);
2561 
2562 		entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
2563 	}
2564 	tx_q->dirty_tx = entry;
2565 
2566 	netdev_tx_completed_queue(netdev_get_tx_queue(priv->dev, queue),
2567 				  pkts_compl, bytes_compl);
2568 
2569 	if (unlikely(netif_tx_queue_stopped(netdev_get_tx_queue(priv->dev,
2570 								queue))) &&
2571 	    stmmac_tx_avail(priv, queue) > STMMAC_TX_THRESH(priv)) {
2572 
2573 		netif_dbg(priv, tx_done, priv->dev,
2574 			  "%s: restart transmit\n", __func__);
2575 		netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, queue));
2576 	}
2577 
2578 	if (tx_q->xsk_pool) {
2579 		bool work_done;
2580 
2581 		if (tx_q->xsk_frames_done)
2582 			xsk_tx_completed(tx_q->xsk_pool, tx_q->xsk_frames_done);
2583 
2584 		if (xsk_uses_need_wakeup(tx_q->xsk_pool))
2585 			xsk_set_tx_need_wakeup(tx_q->xsk_pool);
2586 
2587 		/* For XSK TX, we try to send as many as possible.
2588 		 * If XSK work done (XSK TX desc empty and budget still
2589 		 * available), return "budget - 1" to reenable TX IRQ.
2590 		 * Else, return "budget" to make NAPI continue polling.
2591 		 */
2592 		work_done = stmmac_xdp_xmit_zc(priv, queue,
2593 					       STMMAC_XSK_TX_BUDGET_MAX);
2594 		if (work_done)
2595 			xmits = budget - 1;
2596 		else
2597 			xmits = budget;
2598 	}
2599 
2600 	if (priv->eee_enabled && !priv->tx_path_in_lpi_mode &&
2601 	    priv->eee_sw_timer_en) {
2602 		stmmac_enable_eee_mode(priv);
2603 		mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer));
2604 	}
2605 
2606 	/* We still have pending packets, let's call for a new scheduling */
2607 	if (tx_q->dirty_tx != tx_q->cur_tx)
2608 		hrtimer_start(&tx_q->txtimer,
2609 			      STMMAC_COAL_TIMER(priv->tx_coal_timer[queue]),
2610 			      HRTIMER_MODE_REL);
2611 
2612 	__netif_tx_unlock_bh(netdev_get_tx_queue(priv->dev, queue));
2613 
2614 	/* Combine decisions from TX clean and XSK TX */
2615 	return max(count, xmits);
2616 }
2617 
2618 /**
2619  * stmmac_tx_err - to manage the tx error
2620  * @priv: driver private structure
2621  * @chan: channel index
2622  * Description: it cleans the descriptors and restarts the transmission
2623  * in case of transmission errors.
2624  */
2625 static void stmmac_tx_err(struct stmmac_priv *priv, u32 chan)
2626 {
2627 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2628 
2629 	netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, chan));
2630 
2631 	stmmac_stop_tx_dma(priv, chan);
2632 	dma_free_tx_skbufs(priv, chan);
2633 	stmmac_clear_tx_descriptors(priv, chan);
2634 	tx_q->dirty_tx = 0;
2635 	tx_q->cur_tx = 0;
2636 	tx_q->mss = 0;
2637 	netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, chan));
2638 	stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2639 			    tx_q->dma_tx_phy, chan);
2640 	stmmac_start_tx_dma(priv, chan);
2641 
2642 	priv->dev->stats.tx_errors++;
2643 	netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, chan));
2644 }
2645 
2646 /**
2647  *  stmmac_set_dma_operation_mode - Set DMA operation mode by channel
2648  *  @priv: driver private structure
2649  *  @txmode: TX operating mode
2650  *  @rxmode: RX operating mode
2651  *  @chan: channel index
2652  *  Description: it is used for configuring of the DMA operation mode in
2653  *  runtime in order to program the tx/rx DMA thresholds or Store-And-Forward
2654  *  mode.
2655  */
2656 static void stmmac_set_dma_operation_mode(struct stmmac_priv *priv, u32 txmode,
2657 					  u32 rxmode, u32 chan)
2658 {
2659 	u8 rxqmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
2660 	u8 txqmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
2661 	u32 rx_channels_count = priv->plat->rx_queues_to_use;
2662 	u32 tx_channels_count = priv->plat->tx_queues_to_use;
2663 	int rxfifosz = priv->plat->rx_fifo_size;
2664 	int txfifosz = priv->plat->tx_fifo_size;
2665 
2666 	if (rxfifosz == 0)
2667 		rxfifosz = priv->dma_cap.rx_fifo_size;
2668 	if (txfifosz == 0)
2669 		txfifosz = priv->dma_cap.tx_fifo_size;
2670 
2671 	/* Adjust for real per queue fifo size */
2672 	rxfifosz /= rx_channels_count;
2673 	txfifosz /= tx_channels_count;
2674 
2675 	stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan, rxfifosz, rxqmode);
2676 	stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan, txfifosz, txqmode);
2677 }
2678 
2679 static bool stmmac_safety_feat_interrupt(struct stmmac_priv *priv)
2680 {
2681 	int ret;
2682 
2683 	ret = stmmac_safety_feat_irq_status(priv, priv->dev,
2684 			priv->ioaddr, priv->dma_cap.asp, &priv->sstats);
2685 	if (ret && (ret != -EINVAL)) {
2686 		stmmac_global_err(priv);
2687 		return true;
2688 	}
2689 
2690 	return false;
2691 }
2692 
2693 static int stmmac_napi_check(struct stmmac_priv *priv, u32 chan, u32 dir)
2694 {
2695 	int status = stmmac_dma_interrupt_status(priv, priv->ioaddr,
2696 						 &priv->xstats, chan, dir);
2697 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[chan];
2698 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2699 	struct stmmac_channel *ch = &priv->channel[chan];
2700 	struct napi_struct *rx_napi;
2701 	struct napi_struct *tx_napi;
2702 	unsigned long flags;
2703 
2704 	rx_napi = rx_q->xsk_pool ? &ch->rxtx_napi : &ch->rx_napi;
2705 	tx_napi = tx_q->xsk_pool ? &ch->rxtx_napi : &ch->tx_napi;
2706 
2707 	if ((status & handle_rx) && (chan < priv->plat->rx_queues_to_use)) {
2708 		if (napi_schedule_prep(rx_napi)) {
2709 			spin_lock_irqsave(&ch->lock, flags);
2710 			stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
2711 			spin_unlock_irqrestore(&ch->lock, flags);
2712 			__napi_schedule(rx_napi);
2713 		}
2714 	}
2715 
2716 	if ((status & handle_tx) && (chan < priv->plat->tx_queues_to_use)) {
2717 		if (napi_schedule_prep(tx_napi)) {
2718 			spin_lock_irqsave(&ch->lock, flags);
2719 			stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
2720 			spin_unlock_irqrestore(&ch->lock, flags);
2721 			__napi_schedule(tx_napi);
2722 		}
2723 	}
2724 
2725 	return status;
2726 }
2727 
2728 /**
2729  * stmmac_dma_interrupt - DMA ISR
2730  * @priv: driver private structure
2731  * Description: this is the DMA ISR. It is called by the main ISR.
2732  * It calls the dwmac dma routine and schedule poll method in case of some
2733  * work can be done.
2734  */
2735 static void stmmac_dma_interrupt(struct stmmac_priv *priv)
2736 {
2737 	u32 tx_channel_count = priv->plat->tx_queues_to_use;
2738 	u32 rx_channel_count = priv->plat->rx_queues_to_use;
2739 	u32 channels_to_check = tx_channel_count > rx_channel_count ?
2740 				tx_channel_count : rx_channel_count;
2741 	u32 chan;
2742 	int status[max_t(u32, MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES)];
2743 
2744 	/* Make sure we never check beyond our status buffer. */
2745 	if (WARN_ON_ONCE(channels_to_check > ARRAY_SIZE(status)))
2746 		channels_to_check = ARRAY_SIZE(status);
2747 
2748 	for (chan = 0; chan < channels_to_check; chan++)
2749 		status[chan] = stmmac_napi_check(priv, chan,
2750 						 DMA_DIR_RXTX);
2751 
2752 	for (chan = 0; chan < tx_channel_count; chan++) {
2753 		if (unlikely(status[chan] & tx_hard_error_bump_tc)) {
2754 			/* Try to bump up the dma threshold on this failure */
2755 			if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
2756 			    (tc <= 256)) {
2757 				tc += 64;
2758 				if (priv->plat->force_thresh_dma_mode)
2759 					stmmac_set_dma_operation_mode(priv,
2760 								      tc,
2761 								      tc,
2762 								      chan);
2763 				else
2764 					stmmac_set_dma_operation_mode(priv,
2765 								    tc,
2766 								    SF_DMA_MODE,
2767 								    chan);
2768 				priv->xstats.threshold = tc;
2769 			}
2770 		} else if (unlikely(status[chan] == tx_hard_error)) {
2771 			stmmac_tx_err(priv, chan);
2772 		}
2773 	}
2774 }
2775 
2776 /**
2777  * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
2778  * @priv: driver private structure
2779  * Description: this masks the MMC irq, in fact, the counters are managed in SW.
2780  */
2781 static void stmmac_mmc_setup(struct stmmac_priv *priv)
2782 {
2783 	unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
2784 			    MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
2785 
2786 	stmmac_mmc_intr_all_mask(priv, priv->mmcaddr);
2787 
2788 	if (priv->dma_cap.rmon) {
2789 		stmmac_mmc_ctrl(priv, priv->mmcaddr, mode);
2790 		memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
2791 	} else
2792 		netdev_info(priv->dev, "No MAC Management Counters available\n");
2793 }
2794 
2795 /**
2796  * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
2797  * @priv: driver private structure
2798  * Description:
2799  *  new GMAC chip generations have a new register to indicate the
2800  *  presence of the optional feature/functions.
2801  *  This can be also used to override the value passed through the
2802  *  platform and necessary for old MAC10/100 and GMAC chips.
2803  */
2804 static int stmmac_get_hw_features(struct stmmac_priv *priv)
2805 {
2806 	return stmmac_get_hw_feature(priv, priv->ioaddr, &priv->dma_cap) == 0;
2807 }
2808 
2809 /**
2810  * stmmac_check_ether_addr - check if the MAC addr is valid
2811  * @priv: driver private structure
2812  * Description:
2813  * it is to verify if the MAC address is valid, in case of failures it
2814  * generates a random MAC address
2815  */
2816 static void stmmac_check_ether_addr(struct stmmac_priv *priv)
2817 {
2818 	if (!is_valid_ether_addr(priv->dev->dev_addr)) {
2819 		stmmac_get_umac_addr(priv, priv->hw, priv->dev->dev_addr, 0);
2820 		if (!is_valid_ether_addr(priv->dev->dev_addr))
2821 			eth_hw_addr_random(priv->dev);
2822 		dev_info(priv->device, "device MAC address %pM\n",
2823 			 priv->dev->dev_addr);
2824 	}
2825 }
2826 
2827 /**
2828  * stmmac_init_dma_engine - DMA init.
2829  * @priv: driver private structure
2830  * Description:
2831  * It inits the DMA invoking the specific MAC/GMAC callback.
2832  * Some DMA parameters can be passed from the platform;
2833  * in case of these are not passed a default is kept for the MAC or GMAC.
2834  */
2835 static int stmmac_init_dma_engine(struct stmmac_priv *priv)
2836 {
2837 	u32 rx_channels_count = priv->plat->rx_queues_to_use;
2838 	u32 tx_channels_count = priv->plat->tx_queues_to_use;
2839 	u32 dma_csr_ch = max(rx_channels_count, tx_channels_count);
2840 	struct stmmac_rx_queue *rx_q;
2841 	struct stmmac_tx_queue *tx_q;
2842 	u32 chan = 0;
2843 	int atds = 0;
2844 	int ret = 0;
2845 
2846 	if (!priv->plat->dma_cfg || !priv->plat->dma_cfg->pbl) {
2847 		dev_err(priv->device, "Invalid DMA configuration\n");
2848 		return -EINVAL;
2849 	}
2850 
2851 	if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
2852 		atds = 1;
2853 
2854 	ret = stmmac_reset(priv, priv->ioaddr);
2855 	if (ret) {
2856 		dev_err(priv->device, "Failed to reset the dma\n");
2857 		return ret;
2858 	}
2859 
2860 	/* DMA Configuration */
2861 	stmmac_dma_init(priv, priv->ioaddr, priv->plat->dma_cfg, atds);
2862 
2863 	if (priv->plat->axi)
2864 		stmmac_axi(priv, priv->ioaddr, priv->plat->axi);
2865 
2866 	/* DMA CSR Channel configuration */
2867 	for (chan = 0; chan < dma_csr_ch; chan++)
2868 		stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan);
2869 
2870 	/* DMA RX Channel Configuration */
2871 	for (chan = 0; chan < rx_channels_count; chan++) {
2872 		rx_q = &priv->rx_queue[chan];
2873 
2874 		stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2875 				    rx_q->dma_rx_phy, chan);
2876 
2877 		rx_q->rx_tail_addr = rx_q->dma_rx_phy +
2878 				     (rx_q->buf_alloc_num *
2879 				      sizeof(struct dma_desc));
2880 		stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
2881 				       rx_q->rx_tail_addr, chan);
2882 	}
2883 
2884 	/* DMA TX Channel Configuration */
2885 	for (chan = 0; chan < tx_channels_count; chan++) {
2886 		tx_q = &priv->tx_queue[chan];
2887 
2888 		stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2889 				    tx_q->dma_tx_phy, chan);
2890 
2891 		tx_q->tx_tail_addr = tx_q->dma_tx_phy;
2892 		stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
2893 				       tx_q->tx_tail_addr, chan);
2894 	}
2895 
2896 	return ret;
2897 }
2898 
2899 static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue)
2900 {
2901 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2902 
2903 	hrtimer_start(&tx_q->txtimer,
2904 		      STMMAC_COAL_TIMER(priv->tx_coal_timer[queue]),
2905 		      HRTIMER_MODE_REL);
2906 }
2907 
2908 /**
2909  * stmmac_tx_timer - mitigation sw timer for tx.
2910  * @t: data pointer
2911  * Description:
2912  * This is the timer handler to directly invoke the stmmac_tx_clean.
2913  */
2914 static enum hrtimer_restart stmmac_tx_timer(struct hrtimer *t)
2915 {
2916 	struct stmmac_tx_queue *tx_q = container_of(t, struct stmmac_tx_queue, txtimer);
2917 	struct stmmac_priv *priv = tx_q->priv_data;
2918 	struct stmmac_channel *ch;
2919 	struct napi_struct *napi;
2920 
2921 	ch = &priv->channel[tx_q->queue_index];
2922 	napi = tx_q->xsk_pool ? &ch->rxtx_napi : &ch->tx_napi;
2923 
2924 	if (likely(napi_schedule_prep(napi))) {
2925 		unsigned long flags;
2926 
2927 		spin_lock_irqsave(&ch->lock, flags);
2928 		stmmac_disable_dma_irq(priv, priv->ioaddr, ch->index, 0, 1);
2929 		spin_unlock_irqrestore(&ch->lock, flags);
2930 		__napi_schedule(napi);
2931 	}
2932 
2933 	return HRTIMER_NORESTART;
2934 }
2935 
2936 /**
2937  * stmmac_init_coalesce - init mitigation options.
2938  * @priv: driver private structure
2939  * Description:
2940  * This inits the coalesce parameters: i.e. timer rate,
2941  * timer handler and default threshold used for enabling the
2942  * interrupt on completion bit.
2943  */
2944 static void stmmac_init_coalesce(struct stmmac_priv *priv)
2945 {
2946 	u32 tx_channel_count = priv->plat->tx_queues_to_use;
2947 	u32 rx_channel_count = priv->plat->rx_queues_to_use;
2948 	u32 chan;
2949 
2950 	for (chan = 0; chan < tx_channel_count; chan++) {
2951 		struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2952 
2953 		priv->tx_coal_frames[chan] = STMMAC_TX_FRAMES;
2954 		priv->tx_coal_timer[chan] = STMMAC_COAL_TX_TIMER;
2955 
2956 		hrtimer_init(&tx_q->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2957 		tx_q->txtimer.function = stmmac_tx_timer;
2958 	}
2959 
2960 	for (chan = 0; chan < rx_channel_count; chan++)
2961 		priv->rx_coal_frames[chan] = STMMAC_RX_FRAMES;
2962 }
2963 
2964 static void stmmac_set_rings_length(struct stmmac_priv *priv)
2965 {
2966 	u32 rx_channels_count = priv->plat->rx_queues_to_use;
2967 	u32 tx_channels_count = priv->plat->tx_queues_to_use;
2968 	u32 chan;
2969 
2970 	/* set TX ring length */
2971 	for (chan = 0; chan < tx_channels_count; chan++)
2972 		stmmac_set_tx_ring_len(priv, priv->ioaddr,
2973 				       (priv->dma_tx_size - 1), chan);
2974 
2975 	/* set RX ring length */
2976 	for (chan = 0; chan < rx_channels_count; chan++)
2977 		stmmac_set_rx_ring_len(priv, priv->ioaddr,
2978 				       (priv->dma_rx_size - 1), chan);
2979 }
2980 
2981 /**
2982  *  stmmac_set_tx_queue_weight - Set TX queue weight
2983  *  @priv: driver private structure
2984  *  Description: It is used for setting TX queues weight
2985  */
2986 static void stmmac_set_tx_queue_weight(struct stmmac_priv *priv)
2987 {
2988 	u32 tx_queues_count = priv->plat->tx_queues_to_use;
2989 	u32 weight;
2990 	u32 queue;
2991 
2992 	for (queue = 0; queue < tx_queues_count; queue++) {
2993 		weight = priv->plat->tx_queues_cfg[queue].weight;
2994 		stmmac_set_mtl_tx_queue_weight(priv, priv->hw, weight, queue);
2995 	}
2996 }
2997 
2998 /**
2999  *  stmmac_configure_cbs - Configure CBS in TX queue
3000  *  @priv: driver private structure
3001  *  Description: It is used for configuring CBS in AVB TX queues
3002  */
3003 static void stmmac_configure_cbs(struct stmmac_priv *priv)
3004 {
3005 	u32 tx_queues_count = priv->plat->tx_queues_to_use;
3006 	u32 mode_to_use;
3007 	u32 queue;
3008 
3009 	/* queue 0 is reserved for legacy traffic */
3010 	for (queue = 1; queue < tx_queues_count; queue++) {
3011 		mode_to_use = priv->plat->tx_queues_cfg[queue].mode_to_use;
3012 		if (mode_to_use == MTL_QUEUE_DCB)
3013 			continue;
3014 
3015 		stmmac_config_cbs(priv, priv->hw,
3016 				priv->plat->tx_queues_cfg[queue].send_slope,
3017 				priv->plat->tx_queues_cfg[queue].idle_slope,
3018 				priv->plat->tx_queues_cfg[queue].high_credit,
3019 				priv->plat->tx_queues_cfg[queue].low_credit,
3020 				queue);
3021 	}
3022 }
3023 
3024 /**
3025  *  stmmac_rx_queue_dma_chan_map - Map RX queue to RX dma channel
3026  *  @priv: driver private structure
3027  *  Description: It is used for mapping RX queues to RX dma channels
3028  */
3029 static void stmmac_rx_queue_dma_chan_map(struct stmmac_priv *priv)
3030 {
3031 	u32 rx_queues_count = priv->plat->rx_queues_to_use;
3032 	u32 queue;
3033 	u32 chan;
3034 
3035 	for (queue = 0; queue < rx_queues_count; queue++) {
3036 		chan = priv->plat->rx_queues_cfg[queue].chan;
3037 		stmmac_map_mtl_to_dma(priv, priv->hw, queue, chan);
3038 	}
3039 }
3040 
3041 /**
3042  *  stmmac_mac_config_rx_queues_prio - Configure RX Queue priority
3043  *  @priv: driver private structure
3044  *  Description: It is used for configuring the RX Queue Priority
3045  */
3046 static void stmmac_mac_config_rx_queues_prio(struct stmmac_priv *priv)
3047 {
3048 	u32 rx_queues_count = priv->plat->rx_queues_to_use;
3049 	u32 queue;
3050 	u32 prio;
3051 
3052 	for (queue = 0; queue < rx_queues_count; queue++) {
3053 		if (!priv->plat->rx_queues_cfg[queue].use_prio)
3054 			continue;
3055 
3056 		prio = priv->plat->rx_queues_cfg[queue].prio;
3057 		stmmac_rx_queue_prio(priv, priv->hw, prio, queue);
3058 	}
3059 }
3060 
3061 /**
3062  *  stmmac_mac_config_tx_queues_prio - Configure TX Queue priority
3063  *  @priv: driver private structure
3064  *  Description: It is used for configuring the TX Queue Priority
3065  */
3066 static void stmmac_mac_config_tx_queues_prio(struct stmmac_priv *priv)
3067 {
3068 	u32 tx_queues_count = priv->plat->tx_queues_to_use;
3069 	u32 queue;
3070 	u32 prio;
3071 
3072 	for (queue = 0; queue < tx_queues_count; queue++) {
3073 		if (!priv->plat->tx_queues_cfg[queue].use_prio)
3074 			continue;
3075 
3076 		prio = priv->plat->tx_queues_cfg[queue].prio;
3077 		stmmac_tx_queue_prio(priv, priv->hw, prio, queue);
3078 	}
3079 }
3080 
3081 /**
3082  *  stmmac_mac_config_rx_queues_routing - Configure RX Queue Routing
3083  *  @priv: driver private structure
3084  *  Description: It is used for configuring the RX queue routing
3085  */
3086 static void stmmac_mac_config_rx_queues_routing(struct stmmac_priv *priv)
3087 {
3088 	u32 rx_queues_count = priv->plat->rx_queues_to_use;
3089 	u32 queue;
3090 	u8 packet;
3091 
3092 	for (queue = 0; queue < rx_queues_count; queue++) {
3093 		/* no specific packet type routing specified for the queue */
3094 		if (priv->plat->rx_queues_cfg[queue].pkt_route == 0x0)
3095 			continue;
3096 
3097 		packet = priv->plat->rx_queues_cfg[queue].pkt_route;
3098 		stmmac_rx_queue_routing(priv, priv->hw, packet, queue);
3099 	}
3100 }
3101 
3102 static void stmmac_mac_config_rss(struct stmmac_priv *priv)
3103 {
3104 	if (!priv->dma_cap.rssen || !priv->plat->rss_en) {
3105 		priv->rss.enable = false;
3106 		return;
3107 	}
3108 
3109 	if (priv->dev->features & NETIF_F_RXHASH)
3110 		priv->rss.enable = true;
3111 	else
3112 		priv->rss.enable = false;
3113 
3114 	stmmac_rss_configure(priv, priv->hw, &priv->rss,
3115 			     priv->plat->rx_queues_to_use);
3116 }
3117 
3118 /**
3119  *  stmmac_mtl_configuration - Configure MTL
3120  *  @priv: driver private structure
3121  *  Description: It is used for configurring MTL
3122  */
3123 static void stmmac_mtl_configuration(struct stmmac_priv *priv)
3124 {
3125 	u32 rx_queues_count = priv->plat->rx_queues_to_use;
3126 	u32 tx_queues_count = priv->plat->tx_queues_to_use;
3127 
3128 	if (tx_queues_count > 1)
3129 		stmmac_set_tx_queue_weight(priv);
3130 
3131 	/* Configure MTL RX algorithms */
3132 	if (rx_queues_count > 1)
3133 		stmmac_prog_mtl_rx_algorithms(priv, priv->hw,
3134 				priv->plat->rx_sched_algorithm);
3135 
3136 	/* Configure MTL TX algorithms */
3137 	if (tx_queues_count > 1)
3138 		stmmac_prog_mtl_tx_algorithms(priv, priv->hw,
3139 				priv->plat->tx_sched_algorithm);
3140 
3141 	/* Configure CBS in AVB TX queues */
3142 	if (tx_queues_count > 1)
3143 		stmmac_configure_cbs(priv);
3144 
3145 	/* Map RX MTL to DMA channels */
3146 	stmmac_rx_queue_dma_chan_map(priv);
3147 
3148 	/* Enable MAC RX Queues */
3149 	stmmac_mac_enable_rx_queues(priv);
3150 
3151 	/* Set RX priorities */
3152 	if (rx_queues_count > 1)
3153 		stmmac_mac_config_rx_queues_prio(priv);
3154 
3155 	/* Set TX priorities */
3156 	if (tx_queues_count > 1)
3157 		stmmac_mac_config_tx_queues_prio(priv);
3158 
3159 	/* Set RX routing */
3160 	if (rx_queues_count > 1)
3161 		stmmac_mac_config_rx_queues_routing(priv);
3162 
3163 	/* Receive Side Scaling */
3164 	if (rx_queues_count > 1)
3165 		stmmac_mac_config_rss(priv);
3166 }
3167 
3168 static void stmmac_safety_feat_configuration(struct stmmac_priv *priv)
3169 {
3170 	if (priv->dma_cap.asp) {
3171 		netdev_info(priv->dev, "Enabling Safety Features\n");
3172 		stmmac_safety_feat_config(priv, priv->ioaddr, priv->dma_cap.asp);
3173 	} else {
3174 		netdev_info(priv->dev, "No Safety Features support found\n");
3175 	}
3176 }
3177 
3178 static int stmmac_fpe_start_wq(struct stmmac_priv *priv)
3179 {
3180 	char *name;
3181 
3182 	clear_bit(__FPE_TASK_SCHED, &priv->fpe_task_state);
3183 
3184 	name = priv->wq_name;
3185 	sprintf(name, "%s-fpe", priv->dev->name);
3186 
3187 	priv->fpe_wq = create_singlethread_workqueue(name);
3188 	if (!priv->fpe_wq) {
3189 		netdev_err(priv->dev, "%s: Failed to create workqueue\n", name);
3190 
3191 		return -ENOMEM;
3192 	}
3193 	netdev_info(priv->dev, "FPE workqueue start");
3194 
3195 	return 0;
3196 }
3197 
3198 /**
3199  * stmmac_hw_setup - setup mac in a usable state.
3200  *  @dev : pointer to the device structure.
3201  *  @init_ptp: initialize PTP if set
3202  *  Description:
3203  *  this is the main function to setup the HW in a usable state because the
3204  *  dma engine is reset, the core registers are configured (e.g. AXI,
3205  *  Checksum features, timers). The DMA is ready to start receiving and
3206  *  transmitting.
3207  *  Return value:
3208  *  0 on success and an appropriate (-)ve integer as defined in errno.h
3209  *  file on failure.
3210  */
3211 static int stmmac_hw_setup(struct net_device *dev, bool init_ptp)
3212 {
3213 	struct stmmac_priv *priv = netdev_priv(dev);
3214 	u32 rx_cnt = priv->plat->rx_queues_to_use;
3215 	u32 tx_cnt = priv->plat->tx_queues_to_use;
3216 	bool sph_en;
3217 	u32 chan;
3218 	int ret;
3219 
3220 	/* DMA initialization and SW reset */
3221 	ret = stmmac_init_dma_engine(priv);
3222 	if (ret < 0) {
3223 		netdev_err(priv->dev, "%s: DMA engine initialization failed\n",
3224 			   __func__);
3225 		return ret;
3226 	}
3227 
3228 	/* Copy the MAC addr into the HW  */
3229 	stmmac_set_umac_addr(priv, priv->hw, dev->dev_addr, 0);
3230 
3231 	/* PS and related bits will be programmed according to the speed */
3232 	if (priv->hw->pcs) {
3233 		int speed = priv->plat->mac_port_sel_speed;
3234 
3235 		if ((speed == SPEED_10) || (speed == SPEED_100) ||
3236 		    (speed == SPEED_1000)) {
3237 			priv->hw->ps = speed;
3238 		} else {
3239 			dev_warn(priv->device, "invalid port speed\n");
3240 			priv->hw->ps = 0;
3241 		}
3242 	}
3243 
3244 	/* Initialize the MAC Core */
3245 	stmmac_core_init(priv, priv->hw, dev);
3246 
3247 	/* Initialize MTL*/
3248 	stmmac_mtl_configuration(priv);
3249 
3250 	/* Initialize Safety Features */
3251 	stmmac_safety_feat_configuration(priv);
3252 
3253 	ret = stmmac_rx_ipc(priv, priv->hw);
3254 	if (!ret) {
3255 		netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n");
3256 		priv->plat->rx_coe = STMMAC_RX_COE_NONE;
3257 		priv->hw->rx_csum = 0;
3258 	}
3259 
3260 	/* Enable the MAC Rx/Tx */
3261 	stmmac_mac_set(priv, priv->ioaddr, true);
3262 
3263 	/* Set the HW DMA mode and the COE */
3264 	stmmac_dma_operation_mode(priv);
3265 
3266 	stmmac_mmc_setup(priv);
3267 
3268 	if (init_ptp) {
3269 		ret = clk_prepare_enable(priv->plat->clk_ptp_ref);
3270 		if (ret < 0)
3271 			netdev_warn(priv->dev, "failed to enable PTP reference clock: %d\n", ret);
3272 
3273 		ret = stmmac_init_ptp(priv);
3274 		if (ret == -EOPNOTSUPP)
3275 			netdev_warn(priv->dev, "PTP not supported by HW\n");
3276 		else if (ret)
3277 			netdev_warn(priv->dev, "PTP init failed\n");
3278 	}
3279 
3280 	priv->eee_tw_timer = STMMAC_DEFAULT_TWT_LS;
3281 
3282 	/* Convert the timer from msec to usec */
3283 	if (!priv->tx_lpi_timer)
3284 		priv->tx_lpi_timer = eee_timer * 1000;
3285 
3286 	if (priv->use_riwt) {
3287 		u32 queue;
3288 
3289 		for (queue = 0; queue < rx_cnt; queue++) {
3290 			if (!priv->rx_riwt[queue])
3291 				priv->rx_riwt[queue] = DEF_DMA_RIWT;
3292 
3293 			stmmac_rx_watchdog(priv, priv->ioaddr,
3294 					   priv->rx_riwt[queue], queue);
3295 		}
3296 	}
3297 
3298 	if (priv->hw->pcs)
3299 		stmmac_pcs_ctrl_ane(priv, priv->ioaddr, 1, priv->hw->ps, 0);
3300 
3301 	/* set TX and RX rings length */
3302 	stmmac_set_rings_length(priv);
3303 
3304 	/* Enable TSO */
3305 	if (priv->tso) {
3306 		for (chan = 0; chan < tx_cnt; chan++) {
3307 			struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
3308 
3309 			/* TSO and TBS cannot co-exist */
3310 			if (tx_q->tbs & STMMAC_TBS_AVAIL)
3311 				continue;
3312 
3313 			stmmac_enable_tso(priv, priv->ioaddr, 1, chan);
3314 		}
3315 	}
3316 
3317 	/* Enable Split Header */
3318 	sph_en = (priv->hw->rx_csum > 0) && priv->sph;
3319 	for (chan = 0; chan < rx_cnt; chan++)
3320 		stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
3321 
3322 
3323 	/* VLAN Tag Insertion */
3324 	if (priv->dma_cap.vlins)
3325 		stmmac_enable_vlan(priv, priv->hw, STMMAC_VLAN_INSERT);
3326 
3327 	/* TBS */
3328 	for (chan = 0; chan < tx_cnt; chan++) {
3329 		struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
3330 		int enable = tx_q->tbs & STMMAC_TBS_AVAIL;
3331 
3332 		stmmac_enable_tbs(priv, priv->ioaddr, enable, chan);
3333 	}
3334 
3335 	/* Configure real RX and TX queues */
3336 	netif_set_real_num_rx_queues(dev, priv->plat->rx_queues_to_use);
3337 	netif_set_real_num_tx_queues(dev, priv->plat->tx_queues_to_use);
3338 
3339 	/* Start the ball rolling... */
3340 	stmmac_start_all_dma(priv);
3341 
3342 	if (priv->dma_cap.fpesel) {
3343 		stmmac_fpe_start_wq(priv);
3344 
3345 		if (priv->plat->fpe_cfg->enable)
3346 			stmmac_fpe_handshake(priv, true);
3347 	}
3348 
3349 	return 0;
3350 }
3351 
3352 static void stmmac_hw_teardown(struct net_device *dev)
3353 {
3354 	struct stmmac_priv *priv = netdev_priv(dev);
3355 
3356 	clk_disable_unprepare(priv->plat->clk_ptp_ref);
3357 }
3358 
3359 static void stmmac_free_irq(struct net_device *dev,
3360 			    enum request_irq_err irq_err, int irq_idx)
3361 {
3362 	struct stmmac_priv *priv = netdev_priv(dev);
3363 	int j;
3364 
3365 	switch (irq_err) {
3366 	case REQ_IRQ_ERR_ALL:
3367 		irq_idx = priv->plat->tx_queues_to_use;
3368 		fallthrough;
3369 	case REQ_IRQ_ERR_TX:
3370 		for (j = irq_idx - 1; j >= 0; j--) {
3371 			if (priv->tx_irq[j] > 0) {
3372 				irq_set_affinity_hint(priv->tx_irq[j], NULL);
3373 				free_irq(priv->tx_irq[j], &priv->tx_queue[j]);
3374 			}
3375 		}
3376 		irq_idx = priv->plat->rx_queues_to_use;
3377 		fallthrough;
3378 	case REQ_IRQ_ERR_RX:
3379 		for (j = irq_idx - 1; j >= 0; j--) {
3380 			if (priv->rx_irq[j] > 0) {
3381 				irq_set_affinity_hint(priv->rx_irq[j], NULL);
3382 				free_irq(priv->rx_irq[j], &priv->rx_queue[j]);
3383 			}
3384 		}
3385 
3386 		if (priv->sfty_ue_irq > 0 && priv->sfty_ue_irq != dev->irq)
3387 			free_irq(priv->sfty_ue_irq, dev);
3388 		fallthrough;
3389 	case REQ_IRQ_ERR_SFTY_UE:
3390 		if (priv->sfty_ce_irq > 0 && priv->sfty_ce_irq != dev->irq)
3391 			free_irq(priv->sfty_ce_irq, dev);
3392 		fallthrough;
3393 	case REQ_IRQ_ERR_SFTY_CE:
3394 		if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq)
3395 			free_irq(priv->lpi_irq, dev);
3396 		fallthrough;
3397 	case REQ_IRQ_ERR_LPI:
3398 		if (priv->wol_irq > 0 && priv->wol_irq != dev->irq)
3399 			free_irq(priv->wol_irq, dev);
3400 		fallthrough;
3401 	case REQ_IRQ_ERR_WOL:
3402 		free_irq(dev->irq, dev);
3403 		fallthrough;
3404 	case REQ_IRQ_ERR_MAC:
3405 	case REQ_IRQ_ERR_NO:
3406 		/* If MAC IRQ request error, no more IRQ to free */
3407 		break;
3408 	}
3409 }
3410 
3411 static int stmmac_request_irq_multi_msi(struct net_device *dev)
3412 {
3413 	enum request_irq_err irq_err = REQ_IRQ_ERR_NO;
3414 	struct stmmac_priv *priv = netdev_priv(dev);
3415 	cpumask_t cpu_mask;
3416 	int irq_idx = 0;
3417 	char *int_name;
3418 	int ret;
3419 	int i;
3420 
3421 	/* For common interrupt */
3422 	int_name = priv->int_name_mac;
3423 	sprintf(int_name, "%s:%s", dev->name, "mac");
3424 	ret = request_irq(dev->irq, stmmac_mac_interrupt,
3425 			  0, int_name, dev);
3426 	if (unlikely(ret < 0)) {
3427 		netdev_err(priv->dev,
3428 			   "%s: alloc mac MSI %d (error: %d)\n",
3429 			   __func__, dev->irq, ret);
3430 		irq_err = REQ_IRQ_ERR_MAC;
3431 		goto irq_error;
3432 	}
3433 
3434 	/* Request the Wake IRQ in case of another line
3435 	 * is used for WoL
3436 	 */
3437 	if (priv->wol_irq > 0 && priv->wol_irq != dev->irq) {
3438 		int_name = priv->int_name_wol;
3439 		sprintf(int_name, "%s:%s", dev->name, "wol");
3440 		ret = request_irq(priv->wol_irq,
3441 				  stmmac_mac_interrupt,
3442 				  0, int_name, dev);
3443 		if (unlikely(ret < 0)) {
3444 			netdev_err(priv->dev,
3445 				   "%s: alloc wol MSI %d (error: %d)\n",
3446 				   __func__, priv->wol_irq, ret);
3447 			irq_err = REQ_IRQ_ERR_WOL;
3448 			goto irq_error;
3449 		}
3450 	}
3451 
3452 	/* Request the LPI IRQ in case of another line
3453 	 * is used for LPI
3454 	 */
3455 	if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq) {
3456 		int_name = priv->int_name_lpi;
3457 		sprintf(int_name, "%s:%s", dev->name, "lpi");
3458 		ret = request_irq(priv->lpi_irq,
3459 				  stmmac_mac_interrupt,
3460 				  0, int_name, dev);
3461 		if (unlikely(ret < 0)) {
3462 			netdev_err(priv->dev,
3463 				   "%s: alloc lpi MSI %d (error: %d)\n",
3464 				   __func__, priv->lpi_irq, ret);
3465 			irq_err = REQ_IRQ_ERR_LPI;
3466 			goto irq_error;
3467 		}
3468 	}
3469 
3470 	/* Request the Safety Feature Correctible Error line in
3471 	 * case of another line is used
3472 	 */
3473 	if (priv->sfty_ce_irq > 0 && priv->sfty_ce_irq != dev->irq) {
3474 		int_name = priv->int_name_sfty_ce;
3475 		sprintf(int_name, "%s:%s", dev->name, "safety-ce");
3476 		ret = request_irq(priv->sfty_ce_irq,
3477 				  stmmac_safety_interrupt,
3478 				  0, int_name, dev);
3479 		if (unlikely(ret < 0)) {
3480 			netdev_err(priv->dev,
3481 				   "%s: alloc sfty ce MSI %d (error: %d)\n",
3482 				   __func__, priv->sfty_ce_irq, ret);
3483 			irq_err = REQ_IRQ_ERR_SFTY_CE;
3484 			goto irq_error;
3485 		}
3486 	}
3487 
3488 	/* Request the Safety Feature Uncorrectible Error line in
3489 	 * case of another line is used
3490 	 */
3491 	if (priv->sfty_ue_irq > 0 && priv->sfty_ue_irq != dev->irq) {
3492 		int_name = priv->int_name_sfty_ue;
3493 		sprintf(int_name, "%s:%s", dev->name, "safety-ue");
3494 		ret = request_irq(priv->sfty_ue_irq,
3495 				  stmmac_safety_interrupt,
3496 				  0, int_name, dev);
3497 		if (unlikely(ret < 0)) {
3498 			netdev_err(priv->dev,
3499 				   "%s: alloc sfty ue MSI %d (error: %d)\n",
3500 				   __func__, priv->sfty_ue_irq, ret);
3501 			irq_err = REQ_IRQ_ERR_SFTY_UE;
3502 			goto irq_error;
3503 		}
3504 	}
3505 
3506 	/* Request Rx MSI irq */
3507 	for (i = 0; i < priv->plat->rx_queues_to_use; i++) {
3508 		if (priv->rx_irq[i] == 0)
3509 			continue;
3510 
3511 		int_name = priv->int_name_rx_irq[i];
3512 		sprintf(int_name, "%s:%s-%d", dev->name, "rx", i);
3513 		ret = request_irq(priv->rx_irq[i],
3514 				  stmmac_msi_intr_rx,
3515 				  0, int_name, &priv->rx_queue[i]);
3516 		if (unlikely(ret < 0)) {
3517 			netdev_err(priv->dev,
3518 				   "%s: alloc rx-%d  MSI %d (error: %d)\n",
3519 				   __func__, i, priv->rx_irq[i], ret);
3520 			irq_err = REQ_IRQ_ERR_RX;
3521 			irq_idx = i;
3522 			goto irq_error;
3523 		}
3524 		cpumask_clear(&cpu_mask);
3525 		cpumask_set_cpu(i % num_online_cpus(), &cpu_mask);
3526 		irq_set_affinity_hint(priv->rx_irq[i], &cpu_mask);
3527 	}
3528 
3529 	/* Request Tx MSI irq */
3530 	for (i = 0; i < priv->plat->tx_queues_to_use; i++) {
3531 		if (priv->tx_irq[i] == 0)
3532 			continue;
3533 
3534 		int_name = priv->int_name_tx_irq[i];
3535 		sprintf(int_name, "%s:%s-%d", dev->name, "tx", i);
3536 		ret = request_irq(priv->tx_irq[i],
3537 				  stmmac_msi_intr_tx,
3538 				  0, int_name, &priv->tx_queue[i]);
3539 		if (unlikely(ret < 0)) {
3540 			netdev_err(priv->dev,
3541 				   "%s: alloc tx-%d  MSI %d (error: %d)\n",
3542 				   __func__, i, priv->tx_irq[i], ret);
3543 			irq_err = REQ_IRQ_ERR_TX;
3544 			irq_idx = i;
3545 			goto irq_error;
3546 		}
3547 		cpumask_clear(&cpu_mask);
3548 		cpumask_set_cpu(i % num_online_cpus(), &cpu_mask);
3549 		irq_set_affinity_hint(priv->tx_irq[i], &cpu_mask);
3550 	}
3551 
3552 	return 0;
3553 
3554 irq_error:
3555 	stmmac_free_irq(dev, irq_err, irq_idx);
3556 	return ret;
3557 }
3558 
3559 static int stmmac_request_irq_single(struct net_device *dev)
3560 {
3561 	enum request_irq_err irq_err = REQ_IRQ_ERR_NO;
3562 	struct stmmac_priv *priv = netdev_priv(dev);
3563 	int ret;
3564 
3565 	ret = request_irq(dev->irq, stmmac_interrupt,
3566 			  IRQF_SHARED, dev->name, dev);
3567 	if (unlikely(ret < 0)) {
3568 		netdev_err(priv->dev,
3569 			   "%s: ERROR: allocating the IRQ %d (error: %d)\n",
3570 			   __func__, dev->irq, ret);
3571 		irq_err = REQ_IRQ_ERR_MAC;
3572 		return ret;
3573 	}
3574 
3575 	/* Request the Wake IRQ in case of another line
3576 	 * is used for WoL
3577 	 */
3578 	if (priv->wol_irq > 0 && priv->wol_irq != dev->irq) {
3579 		ret = request_irq(priv->wol_irq, stmmac_interrupt,
3580 				  IRQF_SHARED, dev->name, dev);
3581 		if (unlikely(ret < 0)) {
3582 			netdev_err(priv->dev,
3583 				   "%s: ERROR: allocating the WoL IRQ %d (%d)\n",
3584 				   __func__, priv->wol_irq, ret);
3585 			irq_err = REQ_IRQ_ERR_WOL;
3586 			return ret;
3587 		}
3588 	}
3589 
3590 	/* Request the IRQ lines */
3591 	if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq) {
3592 		ret = request_irq(priv->lpi_irq, stmmac_interrupt,
3593 				  IRQF_SHARED, dev->name, dev);
3594 		if (unlikely(ret < 0)) {
3595 			netdev_err(priv->dev,
3596 				   "%s: ERROR: allocating the LPI IRQ %d (%d)\n",
3597 				   __func__, priv->lpi_irq, ret);
3598 			irq_err = REQ_IRQ_ERR_LPI;
3599 			goto irq_error;
3600 		}
3601 	}
3602 
3603 	return 0;
3604 
3605 irq_error:
3606 	stmmac_free_irq(dev, irq_err, 0);
3607 	return ret;
3608 }
3609 
3610 static int stmmac_request_irq(struct net_device *dev)
3611 {
3612 	struct stmmac_priv *priv = netdev_priv(dev);
3613 	int ret;
3614 
3615 	/* Request the IRQ lines */
3616 	if (priv->plat->multi_msi_en)
3617 		ret = stmmac_request_irq_multi_msi(dev);
3618 	else
3619 		ret = stmmac_request_irq_single(dev);
3620 
3621 	return ret;
3622 }
3623 
3624 /**
3625  *  stmmac_open - open entry point of the driver
3626  *  @dev : pointer to the device structure.
3627  *  Description:
3628  *  This function is the open entry point of the driver.
3629  *  Return value:
3630  *  0 on success and an appropriate (-)ve integer as defined in errno.h
3631  *  file on failure.
3632  */
3633 int stmmac_open(struct net_device *dev)
3634 {
3635 	struct stmmac_priv *priv = netdev_priv(dev);
3636 	int bfsize = 0;
3637 	u32 chan;
3638 	int ret;
3639 
3640 	ret = pm_runtime_get_sync(priv->device);
3641 	if (ret < 0) {
3642 		pm_runtime_put_noidle(priv->device);
3643 		return ret;
3644 	}
3645 
3646 	if (priv->hw->pcs != STMMAC_PCS_TBI &&
3647 	    priv->hw->pcs != STMMAC_PCS_RTBI &&
3648 	    priv->hw->xpcs_args.an_mode != DW_AN_C73) {
3649 		ret = stmmac_init_phy(dev);
3650 		if (ret) {
3651 			netdev_err(priv->dev,
3652 				   "%s: Cannot attach to PHY (error: %d)\n",
3653 				   __func__, ret);
3654 			goto init_phy_error;
3655 		}
3656 	}
3657 
3658 	/* Extra statistics */
3659 	memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
3660 	priv->xstats.threshold = tc;
3661 
3662 	bfsize = stmmac_set_16kib_bfsize(priv, dev->mtu);
3663 	if (bfsize < 0)
3664 		bfsize = 0;
3665 
3666 	if (bfsize < BUF_SIZE_16KiB)
3667 		bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
3668 
3669 	priv->dma_buf_sz = bfsize;
3670 	buf_sz = bfsize;
3671 
3672 	priv->rx_copybreak = STMMAC_RX_COPYBREAK;
3673 
3674 	if (!priv->dma_tx_size)
3675 		priv->dma_tx_size = DMA_DEFAULT_TX_SIZE;
3676 	if (!priv->dma_rx_size)
3677 		priv->dma_rx_size = DMA_DEFAULT_RX_SIZE;
3678 
3679 	/* Earlier check for TBS */
3680 	for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++) {
3681 		struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
3682 		int tbs_en = priv->plat->tx_queues_cfg[chan].tbs_en;
3683 
3684 		/* Setup per-TXQ tbs flag before TX descriptor alloc */
3685 		tx_q->tbs |= tbs_en ? STMMAC_TBS_AVAIL : 0;
3686 	}
3687 
3688 	ret = alloc_dma_desc_resources(priv);
3689 	if (ret < 0) {
3690 		netdev_err(priv->dev, "%s: DMA descriptors allocation failed\n",
3691 			   __func__);
3692 		goto dma_desc_error;
3693 	}
3694 
3695 	ret = init_dma_desc_rings(dev, GFP_KERNEL);
3696 	if (ret < 0) {
3697 		netdev_err(priv->dev, "%s: DMA descriptors initialization failed\n",
3698 			   __func__);
3699 		goto init_error;
3700 	}
3701 
3702 	ret = stmmac_hw_setup(dev, true);
3703 	if (ret < 0) {
3704 		netdev_err(priv->dev, "%s: Hw setup failed\n", __func__);
3705 		goto init_error;
3706 	}
3707 
3708 	stmmac_init_coalesce(priv);
3709 
3710 	phylink_start(priv->phylink);
3711 	/* We may have called phylink_speed_down before */
3712 	phylink_speed_up(priv->phylink);
3713 
3714 	ret = stmmac_request_irq(dev);
3715 	if (ret)
3716 		goto irq_error;
3717 
3718 	stmmac_enable_all_queues(priv);
3719 	netif_tx_start_all_queues(priv->dev);
3720 
3721 	return 0;
3722 
3723 irq_error:
3724 	phylink_stop(priv->phylink);
3725 
3726 	for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
3727 		hrtimer_cancel(&priv->tx_queue[chan].txtimer);
3728 
3729 	stmmac_hw_teardown(dev);
3730 init_error:
3731 	free_dma_desc_resources(priv);
3732 dma_desc_error:
3733 	phylink_disconnect_phy(priv->phylink);
3734 init_phy_error:
3735 	pm_runtime_put(priv->device);
3736 	return ret;
3737 }
3738 
3739 static void stmmac_fpe_stop_wq(struct stmmac_priv *priv)
3740 {
3741 	set_bit(__FPE_REMOVING, &priv->fpe_task_state);
3742 
3743 	if (priv->fpe_wq)
3744 		destroy_workqueue(priv->fpe_wq);
3745 
3746 	netdev_info(priv->dev, "FPE workqueue stop");
3747 }
3748 
3749 /**
3750  *  stmmac_release - close entry point of the driver
3751  *  @dev : device pointer.
3752  *  Description:
3753  *  This is the stop entry point of the driver.
3754  */
3755 int stmmac_release(struct net_device *dev)
3756 {
3757 	struct stmmac_priv *priv = netdev_priv(dev);
3758 	u32 chan;
3759 
3760 	if (device_may_wakeup(priv->device))
3761 		phylink_speed_down(priv->phylink, false);
3762 	/* Stop and disconnect the PHY */
3763 	phylink_stop(priv->phylink);
3764 	phylink_disconnect_phy(priv->phylink);
3765 
3766 	stmmac_disable_all_queues(priv);
3767 
3768 	for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
3769 		hrtimer_cancel(&priv->tx_queue[chan].txtimer);
3770 
3771 	/* Free the IRQ lines */
3772 	stmmac_free_irq(dev, REQ_IRQ_ERR_ALL, 0);
3773 
3774 	if (priv->eee_enabled) {
3775 		priv->tx_path_in_lpi_mode = false;
3776 		del_timer_sync(&priv->eee_ctrl_timer);
3777 	}
3778 
3779 	/* Stop TX/RX DMA and clear the descriptors */
3780 	stmmac_stop_all_dma(priv);
3781 
3782 	/* Release and free the Rx/Tx resources */
3783 	free_dma_desc_resources(priv);
3784 
3785 	/* Disable the MAC Rx/Tx */
3786 	stmmac_mac_set(priv, priv->ioaddr, false);
3787 
3788 	netif_carrier_off(dev);
3789 
3790 	stmmac_release_ptp(priv);
3791 
3792 	pm_runtime_put(priv->device);
3793 
3794 	if (priv->dma_cap.fpesel)
3795 		stmmac_fpe_stop_wq(priv);
3796 
3797 	return 0;
3798 }
3799 
3800 static bool stmmac_vlan_insert(struct stmmac_priv *priv, struct sk_buff *skb,
3801 			       struct stmmac_tx_queue *tx_q)
3802 {
3803 	u16 tag = 0x0, inner_tag = 0x0;
3804 	u32 inner_type = 0x0;
3805 	struct dma_desc *p;
3806 
3807 	if (!priv->dma_cap.vlins)
3808 		return false;
3809 	if (!skb_vlan_tag_present(skb))
3810 		return false;
3811 	if (skb->vlan_proto == htons(ETH_P_8021AD)) {
3812 		inner_tag = skb_vlan_tag_get(skb);
3813 		inner_type = STMMAC_VLAN_INSERT;
3814 	}
3815 
3816 	tag = skb_vlan_tag_get(skb);
3817 
3818 	if (tx_q->tbs & STMMAC_TBS_AVAIL)
3819 		p = &tx_q->dma_entx[tx_q->cur_tx].basic;
3820 	else
3821 		p = &tx_q->dma_tx[tx_q->cur_tx];
3822 
3823 	if (stmmac_set_desc_vlan_tag(priv, p, tag, inner_tag, inner_type))
3824 		return false;
3825 
3826 	stmmac_set_tx_owner(priv, p);
3827 	tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
3828 	return true;
3829 }
3830 
3831 /**
3832  *  stmmac_tso_allocator - close entry point of the driver
3833  *  @priv: driver private structure
3834  *  @des: buffer start address
3835  *  @total_len: total length to fill in descriptors
3836  *  @last_segment: condition for the last descriptor
3837  *  @queue: TX queue index
3838  *  Description:
3839  *  This function fills descriptor and request new descriptors according to
3840  *  buffer length to fill
3841  */
3842 static void stmmac_tso_allocator(struct stmmac_priv *priv, dma_addr_t des,
3843 				 int total_len, bool last_segment, u32 queue)
3844 {
3845 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
3846 	struct dma_desc *desc;
3847 	u32 buff_size;
3848 	int tmp_len;
3849 
3850 	tmp_len = total_len;
3851 
3852 	while (tmp_len > 0) {
3853 		dma_addr_t curr_addr;
3854 
3855 		tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx,
3856 						priv->dma_tx_size);
3857 		WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
3858 
3859 		if (tx_q->tbs & STMMAC_TBS_AVAIL)
3860 			desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
3861 		else
3862 			desc = &tx_q->dma_tx[tx_q->cur_tx];
3863 
3864 		curr_addr = des + (total_len - tmp_len);
3865 		if (priv->dma_cap.addr64 <= 32)
3866 			desc->des0 = cpu_to_le32(curr_addr);
3867 		else
3868 			stmmac_set_desc_addr(priv, desc, curr_addr);
3869 
3870 		buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ?
3871 			    TSO_MAX_BUFF_SIZE : tmp_len;
3872 
3873 		stmmac_prepare_tso_tx_desc(priv, desc, 0, buff_size,
3874 				0, 1,
3875 				(last_segment) && (tmp_len <= TSO_MAX_BUFF_SIZE),
3876 				0, 0);
3877 
3878 		tmp_len -= TSO_MAX_BUFF_SIZE;
3879 	}
3880 }
3881 
3882 static void stmmac_flush_tx_descriptors(struct stmmac_priv *priv, int queue)
3883 {
3884 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
3885 	int desc_size;
3886 
3887 	if (likely(priv->extend_desc))
3888 		desc_size = sizeof(struct dma_extended_desc);
3889 	else if (tx_q->tbs & STMMAC_TBS_AVAIL)
3890 		desc_size = sizeof(struct dma_edesc);
3891 	else
3892 		desc_size = sizeof(struct dma_desc);
3893 
3894 	/* The own bit must be the latest setting done when prepare the
3895 	 * descriptor and then barrier is needed to make sure that
3896 	 * all is coherent before granting the DMA engine.
3897 	 */
3898 	wmb();
3899 
3900 	tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * desc_size);
3901 	stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue);
3902 }
3903 
3904 /**
3905  *  stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO)
3906  *  @skb : the socket buffer
3907  *  @dev : device pointer
3908  *  Description: this is the transmit function that is called on TSO frames
3909  *  (support available on GMAC4 and newer chips).
3910  *  Diagram below show the ring programming in case of TSO frames:
3911  *
3912  *  First Descriptor
3913  *   --------
3914  *   | DES0 |---> buffer1 = L2/L3/L4 header
3915  *   | DES1 |---> TCP Payload (can continue on next descr...)
3916  *   | DES2 |---> buffer 1 and 2 len
3917  *   | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0]
3918  *   --------
3919  *	|
3920  *     ...
3921  *	|
3922  *   --------
3923  *   | DES0 | --| Split TCP Payload on Buffers 1 and 2
3924  *   | DES1 | --|
3925  *   | DES2 | --> buffer 1 and 2 len
3926  *   | DES3 |
3927  *   --------
3928  *
3929  * mss is fixed when enable tso, so w/o programming the TDES3 ctx field.
3930  */
3931 static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev)
3932 {
3933 	struct dma_desc *desc, *first, *mss_desc = NULL;
3934 	struct stmmac_priv *priv = netdev_priv(dev);
3935 	int nfrags = skb_shinfo(skb)->nr_frags;
3936 	u32 queue = skb_get_queue_mapping(skb);
3937 	unsigned int first_entry, tx_packets;
3938 	int tmp_pay_len = 0, first_tx;
3939 	struct stmmac_tx_queue *tx_q;
3940 	bool has_vlan, set_ic;
3941 	u8 proto_hdr_len, hdr;
3942 	u32 pay_len, mss;
3943 	dma_addr_t des;
3944 	int i;
3945 
3946 	tx_q = &priv->tx_queue[queue];
3947 	first_tx = tx_q->cur_tx;
3948 
3949 	/* Compute header lengths */
3950 	if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
3951 		proto_hdr_len = skb_transport_offset(skb) + sizeof(struct udphdr);
3952 		hdr = sizeof(struct udphdr);
3953 	} else {
3954 		proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
3955 		hdr = tcp_hdrlen(skb);
3956 	}
3957 
3958 	/* Desc availability based on threshold should be enough safe */
3959 	if (unlikely(stmmac_tx_avail(priv, queue) <
3960 		(((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) {
3961 		if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
3962 			netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
3963 								queue));
3964 			/* This is a hard error, log it. */
3965 			netdev_err(priv->dev,
3966 				   "%s: Tx Ring full when queue awake\n",
3967 				   __func__);
3968 		}
3969 		return NETDEV_TX_BUSY;
3970 	}
3971 
3972 	pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */
3973 
3974 	mss = skb_shinfo(skb)->gso_size;
3975 
3976 	/* set new MSS value if needed */
3977 	if (mss != tx_q->mss) {
3978 		if (tx_q->tbs & STMMAC_TBS_AVAIL)
3979 			mss_desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
3980 		else
3981 			mss_desc = &tx_q->dma_tx[tx_q->cur_tx];
3982 
3983 		stmmac_set_mss(priv, mss_desc, mss);
3984 		tx_q->mss = mss;
3985 		tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx,
3986 						priv->dma_tx_size);
3987 		WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
3988 	}
3989 
3990 	if (netif_msg_tx_queued(priv)) {
3991 		pr_info("%s: hdrlen %d, hdr_len %d, pay_len %d, mss %d\n",
3992 			__func__, hdr, proto_hdr_len, pay_len, mss);
3993 		pr_info("\tskb->len %d, skb->data_len %d\n", skb->len,
3994 			skb->data_len);
3995 	}
3996 
3997 	/* Check if VLAN can be inserted by HW */
3998 	has_vlan = stmmac_vlan_insert(priv, skb, tx_q);
3999 
4000 	first_entry = tx_q->cur_tx;
4001 	WARN_ON(tx_q->tx_skbuff[first_entry]);
4002 
4003 	if (tx_q->tbs & STMMAC_TBS_AVAIL)
4004 		desc = &tx_q->dma_entx[first_entry].basic;
4005 	else
4006 		desc = &tx_q->dma_tx[first_entry];
4007 	first = desc;
4008 
4009 	if (has_vlan)
4010 		stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT);
4011 
4012 	/* first descriptor: fill Headers on Buf1 */
4013 	des = dma_map_single(priv->device, skb->data, skb_headlen(skb),
4014 			     DMA_TO_DEVICE);
4015 	if (dma_mapping_error(priv->device, des))
4016 		goto dma_map_err;
4017 
4018 	tx_q->tx_skbuff_dma[first_entry].buf = des;
4019 	tx_q->tx_skbuff_dma[first_entry].len = skb_headlen(skb);
4020 	tx_q->tx_skbuff_dma[first_entry].map_as_page = false;
4021 	tx_q->tx_skbuff_dma[first_entry].buf_type = STMMAC_TXBUF_T_SKB;
4022 
4023 	if (priv->dma_cap.addr64 <= 32) {
4024 		first->des0 = cpu_to_le32(des);
4025 
4026 		/* Fill start of payload in buff2 of first descriptor */
4027 		if (pay_len)
4028 			first->des1 = cpu_to_le32(des + proto_hdr_len);
4029 
4030 		/* If needed take extra descriptors to fill the remaining payload */
4031 		tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE;
4032 	} else {
4033 		stmmac_set_desc_addr(priv, first, des);
4034 		tmp_pay_len = pay_len;
4035 		des += proto_hdr_len;
4036 		pay_len = 0;
4037 	}
4038 
4039 	stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0), queue);
4040 
4041 	/* Prepare fragments */
4042 	for (i = 0; i < nfrags; i++) {
4043 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4044 
4045 		des = skb_frag_dma_map(priv->device, frag, 0,
4046 				       skb_frag_size(frag),
4047 				       DMA_TO_DEVICE);
4048 		if (dma_mapping_error(priv->device, des))
4049 			goto dma_map_err;
4050 
4051 		stmmac_tso_allocator(priv, des, skb_frag_size(frag),
4052 				     (i == nfrags - 1), queue);
4053 
4054 		tx_q->tx_skbuff_dma[tx_q->cur_tx].buf = des;
4055 		tx_q->tx_skbuff_dma[tx_q->cur_tx].len = skb_frag_size(frag);
4056 		tx_q->tx_skbuff_dma[tx_q->cur_tx].map_as_page = true;
4057 		tx_q->tx_skbuff_dma[tx_q->cur_tx].buf_type = STMMAC_TXBUF_T_SKB;
4058 	}
4059 
4060 	tx_q->tx_skbuff_dma[tx_q->cur_tx].last_segment = true;
4061 
4062 	/* Only the last descriptor gets to point to the skb. */
4063 	tx_q->tx_skbuff[tx_q->cur_tx] = skb;
4064 	tx_q->tx_skbuff_dma[tx_q->cur_tx].buf_type = STMMAC_TXBUF_T_SKB;
4065 
4066 	/* Manage tx mitigation */
4067 	tx_packets = (tx_q->cur_tx + 1) - first_tx;
4068 	tx_q->tx_count_frames += tx_packets;
4069 
4070 	if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en)
4071 		set_ic = true;
4072 	else if (!priv->tx_coal_frames[queue])
4073 		set_ic = false;
4074 	else if (tx_packets > priv->tx_coal_frames[queue])
4075 		set_ic = true;
4076 	else if ((tx_q->tx_count_frames %
4077 		  priv->tx_coal_frames[queue]) < tx_packets)
4078 		set_ic = true;
4079 	else
4080 		set_ic = false;
4081 
4082 	if (set_ic) {
4083 		if (tx_q->tbs & STMMAC_TBS_AVAIL)
4084 			desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
4085 		else
4086 			desc = &tx_q->dma_tx[tx_q->cur_tx];
4087 
4088 		tx_q->tx_count_frames = 0;
4089 		stmmac_set_tx_ic(priv, desc);
4090 		priv->xstats.tx_set_ic_bit++;
4091 	}
4092 
4093 	/* We've used all descriptors we need for this skb, however,
4094 	 * advance cur_tx so that it references a fresh descriptor.
4095 	 * ndo_start_xmit will fill this descriptor the next time it's
4096 	 * called and stmmac_tx_clean may clean up to this descriptor.
4097 	 */
4098 	tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
4099 
4100 	if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
4101 		netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
4102 			  __func__);
4103 		netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
4104 	}
4105 
4106 	dev->stats.tx_bytes += skb->len;
4107 	priv->xstats.tx_tso_frames++;
4108 	priv->xstats.tx_tso_nfrags += nfrags;
4109 
4110 	if (priv->sarc_type)
4111 		stmmac_set_desc_sarc(priv, first, priv->sarc_type);
4112 
4113 	skb_tx_timestamp(skb);
4114 
4115 	if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4116 		     priv->hwts_tx_en)) {
4117 		/* declare that device is doing timestamping */
4118 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4119 		stmmac_enable_tx_timestamp(priv, first);
4120 	}
4121 
4122 	/* Complete the first descriptor before granting the DMA */
4123 	stmmac_prepare_tso_tx_desc(priv, first, 1,
4124 			proto_hdr_len,
4125 			pay_len,
4126 			1, tx_q->tx_skbuff_dma[first_entry].last_segment,
4127 			hdr / 4, (skb->len - proto_hdr_len));
4128 
4129 	/* If context desc is used to change MSS */
4130 	if (mss_desc) {
4131 		/* Make sure that first descriptor has been completely
4132 		 * written, including its own bit. This is because MSS is
4133 		 * actually before first descriptor, so we need to make
4134 		 * sure that MSS's own bit is the last thing written.
4135 		 */
4136 		dma_wmb();
4137 		stmmac_set_tx_owner(priv, mss_desc);
4138 	}
4139 
4140 	if (netif_msg_pktdata(priv)) {
4141 		pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n",
4142 			__func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
4143 			tx_q->cur_tx, first, nfrags);
4144 		pr_info(">>> frame to be transmitted: ");
4145 		print_pkt(skb->data, skb_headlen(skb));
4146 	}
4147 
4148 	netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
4149 
4150 	stmmac_flush_tx_descriptors(priv, queue);
4151 	stmmac_tx_timer_arm(priv, queue);
4152 
4153 	return NETDEV_TX_OK;
4154 
4155 dma_map_err:
4156 	dev_err(priv->device, "Tx dma map failed\n");
4157 	dev_kfree_skb(skb);
4158 	priv->dev->stats.tx_dropped++;
4159 	return NETDEV_TX_OK;
4160 }
4161 
4162 /**
4163  *  stmmac_xmit - Tx entry point of the driver
4164  *  @skb : the socket buffer
4165  *  @dev : device pointer
4166  *  Description : this is the tx entry point of the driver.
4167  *  It programs the chain or the ring and supports oversized frames
4168  *  and SG feature.
4169  */
4170 static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
4171 {
4172 	unsigned int first_entry, tx_packets, enh_desc;
4173 	struct stmmac_priv *priv = netdev_priv(dev);
4174 	unsigned int nopaged_len = skb_headlen(skb);
4175 	int i, csum_insertion = 0, is_jumbo = 0;
4176 	u32 queue = skb_get_queue_mapping(skb);
4177 	int nfrags = skb_shinfo(skb)->nr_frags;
4178 	int gso = skb_shinfo(skb)->gso_type;
4179 	struct dma_edesc *tbs_desc = NULL;
4180 	struct dma_desc *desc, *first;
4181 	struct stmmac_tx_queue *tx_q;
4182 	bool has_vlan, set_ic;
4183 	int entry, first_tx;
4184 	dma_addr_t des;
4185 
4186 	tx_q = &priv->tx_queue[queue];
4187 	first_tx = tx_q->cur_tx;
4188 
4189 	if (priv->tx_path_in_lpi_mode && priv->eee_sw_timer_en)
4190 		stmmac_disable_eee_mode(priv);
4191 
4192 	/* Manage oversized TCP frames for GMAC4 device */
4193 	if (skb_is_gso(skb) && priv->tso) {
4194 		if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
4195 			return stmmac_tso_xmit(skb, dev);
4196 		if (priv->plat->has_gmac4 && (gso & SKB_GSO_UDP_L4))
4197 			return stmmac_tso_xmit(skb, dev);
4198 	}
4199 
4200 	if (unlikely(stmmac_tx_avail(priv, queue) < nfrags + 1)) {
4201 		if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
4202 			netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
4203 								queue));
4204 			/* This is a hard error, log it. */
4205 			netdev_err(priv->dev,
4206 				   "%s: Tx Ring full when queue awake\n",
4207 				   __func__);
4208 		}
4209 		return NETDEV_TX_BUSY;
4210 	}
4211 
4212 	/* Check if VLAN can be inserted by HW */
4213 	has_vlan = stmmac_vlan_insert(priv, skb, tx_q);
4214 
4215 	entry = tx_q->cur_tx;
4216 	first_entry = entry;
4217 	WARN_ON(tx_q->tx_skbuff[first_entry]);
4218 
4219 	csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
4220 
4221 	if (likely(priv->extend_desc))
4222 		desc = (struct dma_desc *)(tx_q->dma_etx + entry);
4223 	else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4224 		desc = &tx_q->dma_entx[entry].basic;
4225 	else
4226 		desc = tx_q->dma_tx + entry;
4227 
4228 	first = desc;
4229 
4230 	if (has_vlan)
4231 		stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT);
4232 
4233 	enh_desc = priv->plat->enh_desc;
4234 	/* To program the descriptors according to the size of the frame */
4235 	if (enh_desc)
4236 		is_jumbo = stmmac_is_jumbo_frm(priv, skb->len, enh_desc);
4237 
4238 	if (unlikely(is_jumbo)) {
4239 		entry = stmmac_jumbo_frm(priv, tx_q, skb, csum_insertion);
4240 		if (unlikely(entry < 0) && (entry != -EINVAL))
4241 			goto dma_map_err;
4242 	}
4243 
4244 	for (i = 0; i < nfrags; i++) {
4245 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4246 		int len = skb_frag_size(frag);
4247 		bool last_segment = (i == (nfrags - 1));
4248 
4249 		entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
4250 		WARN_ON(tx_q->tx_skbuff[entry]);
4251 
4252 		if (likely(priv->extend_desc))
4253 			desc = (struct dma_desc *)(tx_q->dma_etx + entry);
4254 		else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4255 			desc = &tx_q->dma_entx[entry].basic;
4256 		else
4257 			desc = tx_q->dma_tx + entry;
4258 
4259 		des = skb_frag_dma_map(priv->device, frag, 0, len,
4260 				       DMA_TO_DEVICE);
4261 		if (dma_mapping_error(priv->device, des))
4262 			goto dma_map_err; /* should reuse desc w/o issues */
4263 
4264 		tx_q->tx_skbuff_dma[entry].buf = des;
4265 
4266 		stmmac_set_desc_addr(priv, desc, des);
4267 
4268 		tx_q->tx_skbuff_dma[entry].map_as_page = true;
4269 		tx_q->tx_skbuff_dma[entry].len = len;
4270 		tx_q->tx_skbuff_dma[entry].last_segment = last_segment;
4271 		tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_SKB;
4272 
4273 		/* Prepare the descriptor and set the own bit too */
4274 		stmmac_prepare_tx_desc(priv, desc, 0, len, csum_insertion,
4275 				priv->mode, 1, last_segment, skb->len);
4276 	}
4277 
4278 	/* Only the last descriptor gets to point to the skb. */
4279 	tx_q->tx_skbuff[entry] = skb;
4280 	tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_SKB;
4281 
4282 	/* According to the coalesce parameter the IC bit for the latest
4283 	 * segment is reset and the timer re-started to clean the tx status.
4284 	 * This approach takes care about the fragments: desc is the first
4285 	 * element in case of no SG.
4286 	 */
4287 	tx_packets = (entry + 1) - first_tx;
4288 	tx_q->tx_count_frames += tx_packets;
4289 
4290 	if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en)
4291 		set_ic = true;
4292 	else if (!priv->tx_coal_frames[queue])
4293 		set_ic = false;
4294 	else if (tx_packets > priv->tx_coal_frames[queue])
4295 		set_ic = true;
4296 	else if ((tx_q->tx_count_frames %
4297 		  priv->tx_coal_frames[queue]) < tx_packets)
4298 		set_ic = true;
4299 	else
4300 		set_ic = false;
4301 
4302 	if (set_ic) {
4303 		if (likely(priv->extend_desc))
4304 			desc = &tx_q->dma_etx[entry].basic;
4305 		else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4306 			desc = &tx_q->dma_entx[entry].basic;
4307 		else
4308 			desc = &tx_q->dma_tx[entry];
4309 
4310 		tx_q->tx_count_frames = 0;
4311 		stmmac_set_tx_ic(priv, desc);
4312 		priv->xstats.tx_set_ic_bit++;
4313 	}
4314 
4315 	/* We've used all descriptors we need for this skb, however,
4316 	 * advance cur_tx so that it references a fresh descriptor.
4317 	 * ndo_start_xmit will fill this descriptor the next time it's
4318 	 * called and stmmac_tx_clean may clean up to this descriptor.
4319 	 */
4320 	entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
4321 	tx_q->cur_tx = entry;
4322 
4323 	if (netif_msg_pktdata(priv)) {
4324 		netdev_dbg(priv->dev,
4325 			   "%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
4326 			   __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
4327 			   entry, first, nfrags);
4328 
4329 		netdev_dbg(priv->dev, ">>> frame to be transmitted: ");
4330 		print_pkt(skb->data, skb->len);
4331 	}
4332 
4333 	if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
4334 		netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
4335 			  __func__);
4336 		netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
4337 	}
4338 
4339 	dev->stats.tx_bytes += skb->len;
4340 
4341 	if (priv->sarc_type)
4342 		stmmac_set_desc_sarc(priv, first, priv->sarc_type);
4343 
4344 	skb_tx_timestamp(skb);
4345 
4346 	/* Ready to fill the first descriptor and set the OWN bit w/o any
4347 	 * problems because all the descriptors are actually ready to be
4348 	 * passed to the DMA engine.
4349 	 */
4350 	if (likely(!is_jumbo)) {
4351 		bool last_segment = (nfrags == 0);
4352 
4353 		des = dma_map_single(priv->device, skb->data,
4354 				     nopaged_len, DMA_TO_DEVICE);
4355 		if (dma_mapping_error(priv->device, des))
4356 			goto dma_map_err;
4357 
4358 		tx_q->tx_skbuff_dma[first_entry].buf = des;
4359 		tx_q->tx_skbuff_dma[first_entry].buf_type = STMMAC_TXBUF_T_SKB;
4360 		tx_q->tx_skbuff_dma[first_entry].map_as_page = false;
4361 
4362 		stmmac_set_desc_addr(priv, first, des);
4363 
4364 		tx_q->tx_skbuff_dma[first_entry].len = nopaged_len;
4365 		tx_q->tx_skbuff_dma[first_entry].last_segment = last_segment;
4366 
4367 		if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4368 			     priv->hwts_tx_en)) {
4369 			/* declare that device is doing timestamping */
4370 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4371 			stmmac_enable_tx_timestamp(priv, first);
4372 		}
4373 
4374 		/* Prepare the first descriptor setting the OWN bit too */
4375 		stmmac_prepare_tx_desc(priv, first, 1, nopaged_len,
4376 				csum_insertion, priv->mode, 0, last_segment,
4377 				skb->len);
4378 	}
4379 
4380 	if (tx_q->tbs & STMMAC_TBS_EN) {
4381 		struct timespec64 ts = ns_to_timespec64(skb->tstamp);
4382 
4383 		tbs_desc = &tx_q->dma_entx[first_entry];
4384 		stmmac_set_desc_tbs(priv, tbs_desc, ts.tv_sec, ts.tv_nsec);
4385 	}
4386 
4387 	stmmac_set_tx_owner(priv, first);
4388 
4389 	netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
4390 
4391 	stmmac_enable_dma_transmission(priv, priv->ioaddr);
4392 
4393 	stmmac_flush_tx_descriptors(priv, queue);
4394 	stmmac_tx_timer_arm(priv, queue);
4395 
4396 	return NETDEV_TX_OK;
4397 
4398 dma_map_err:
4399 	netdev_err(priv->dev, "Tx DMA map failed\n");
4400 	dev_kfree_skb(skb);
4401 	priv->dev->stats.tx_dropped++;
4402 	return NETDEV_TX_OK;
4403 }
4404 
4405 static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
4406 {
4407 	struct vlan_ethhdr *veth;
4408 	__be16 vlan_proto;
4409 	u16 vlanid;
4410 
4411 	veth = (struct vlan_ethhdr *)skb->data;
4412 	vlan_proto = veth->h_vlan_proto;
4413 
4414 	if ((vlan_proto == htons(ETH_P_8021Q) &&
4415 	     dev->features & NETIF_F_HW_VLAN_CTAG_RX) ||
4416 	    (vlan_proto == htons(ETH_P_8021AD) &&
4417 	     dev->features & NETIF_F_HW_VLAN_STAG_RX)) {
4418 		/* pop the vlan tag */
4419 		vlanid = ntohs(veth->h_vlan_TCI);
4420 		memmove(skb->data + VLAN_HLEN, veth, ETH_ALEN * 2);
4421 		skb_pull(skb, VLAN_HLEN);
4422 		__vlan_hwaccel_put_tag(skb, vlan_proto, vlanid);
4423 	}
4424 }
4425 
4426 /**
4427  * stmmac_rx_refill - refill used skb preallocated buffers
4428  * @priv: driver private structure
4429  * @queue: RX queue index
4430  * Description : this is to reallocate the skb for the reception process
4431  * that is based on zero-copy.
4432  */
4433 static inline void stmmac_rx_refill(struct stmmac_priv *priv, u32 queue)
4434 {
4435 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4436 	int dirty = stmmac_rx_dirty(priv, queue);
4437 	unsigned int entry = rx_q->dirty_rx;
4438 
4439 	while (dirty-- > 0) {
4440 		struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry];
4441 		struct dma_desc *p;
4442 		bool use_rx_wd;
4443 
4444 		if (priv->extend_desc)
4445 			p = (struct dma_desc *)(rx_q->dma_erx + entry);
4446 		else
4447 			p = rx_q->dma_rx + entry;
4448 
4449 		if (!buf->page) {
4450 			buf->page = page_pool_dev_alloc_pages(rx_q->page_pool);
4451 			if (!buf->page)
4452 				break;
4453 		}
4454 
4455 		if (priv->sph && !buf->sec_page) {
4456 			buf->sec_page = page_pool_dev_alloc_pages(rx_q->page_pool);
4457 			if (!buf->sec_page)
4458 				break;
4459 
4460 			buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
4461 		}
4462 
4463 		buf->addr = page_pool_get_dma_addr(buf->page) + buf->page_offset;
4464 
4465 		stmmac_set_desc_addr(priv, p, buf->addr);
4466 		if (priv->sph)
4467 			stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true);
4468 		else
4469 			stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false);
4470 		stmmac_refill_desc3(priv, rx_q, p);
4471 
4472 		rx_q->rx_count_frames++;
4473 		rx_q->rx_count_frames += priv->rx_coal_frames[queue];
4474 		if (rx_q->rx_count_frames > priv->rx_coal_frames[queue])
4475 			rx_q->rx_count_frames = 0;
4476 
4477 		use_rx_wd = !priv->rx_coal_frames[queue];
4478 		use_rx_wd |= rx_q->rx_count_frames > 0;
4479 		if (!priv->use_riwt)
4480 			use_rx_wd = false;
4481 
4482 		dma_wmb();
4483 		stmmac_set_rx_owner(priv, p, use_rx_wd);
4484 
4485 		entry = STMMAC_GET_ENTRY(entry, priv->dma_rx_size);
4486 	}
4487 	rx_q->dirty_rx = entry;
4488 	rx_q->rx_tail_addr = rx_q->dma_rx_phy +
4489 			    (rx_q->dirty_rx * sizeof(struct dma_desc));
4490 	stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue);
4491 }
4492 
4493 static unsigned int stmmac_rx_buf1_len(struct stmmac_priv *priv,
4494 				       struct dma_desc *p,
4495 				       int status, unsigned int len)
4496 {
4497 	unsigned int plen = 0, hlen = 0;
4498 	int coe = priv->hw->rx_csum;
4499 
4500 	/* Not first descriptor, buffer is always zero */
4501 	if (priv->sph && len)
4502 		return 0;
4503 
4504 	/* First descriptor, get split header length */
4505 	stmmac_get_rx_header_len(priv, p, &hlen);
4506 	if (priv->sph && hlen) {
4507 		priv->xstats.rx_split_hdr_pkt_n++;
4508 		return hlen;
4509 	}
4510 
4511 	/* First descriptor, not last descriptor and not split header */
4512 	if (status & rx_not_ls)
4513 		return priv->dma_buf_sz;
4514 
4515 	plen = stmmac_get_rx_frame_len(priv, p, coe);
4516 
4517 	/* First descriptor and last descriptor and not split header */
4518 	return min_t(unsigned int, priv->dma_buf_sz, plen);
4519 }
4520 
4521 static unsigned int stmmac_rx_buf2_len(struct stmmac_priv *priv,
4522 				       struct dma_desc *p,
4523 				       int status, unsigned int len)
4524 {
4525 	int coe = priv->hw->rx_csum;
4526 	unsigned int plen = 0;
4527 
4528 	/* Not split header, buffer is not available */
4529 	if (!priv->sph)
4530 		return 0;
4531 
4532 	/* Not last descriptor */
4533 	if (status & rx_not_ls)
4534 		return priv->dma_buf_sz;
4535 
4536 	plen = stmmac_get_rx_frame_len(priv, p, coe);
4537 
4538 	/* Last descriptor */
4539 	return plen - len;
4540 }
4541 
4542 static int stmmac_xdp_xmit_xdpf(struct stmmac_priv *priv, int queue,
4543 				struct xdp_frame *xdpf, bool dma_map)
4544 {
4545 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
4546 	unsigned int entry = tx_q->cur_tx;
4547 	struct dma_desc *tx_desc;
4548 	dma_addr_t dma_addr;
4549 	bool set_ic;
4550 
4551 	if (stmmac_tx_avail(priv, queue) < STMMAC_TX_THRESH(priv))
4552 		return STMMAC_XDP_CONSUMED;
4553 
4554 	if (likely(priv->extend_desc))
4555 		tx_desc = (struct dma_desc *)(tx_q->dma_etx + entry);
4556 	else if (tx_q->tbs & STMMAC_TBS_AVAIL)
4557 		tx_desc = &tx_q->dma_entx[entry].basic;
4558 	else
4559 		tx_desc = tx_q->dma_tx + entry;
4560 
4561 	if (dma_map) {
4562 		dma_addr = dma_map_single(priv->device, xdpf->data,
4563 					  xdpf->len, DMA_TO_DEVICE);
4564 		if (dma_mapping_error(priv->device, dma_addr))
4565 			return STMMAC_XDP_CONSUMED;
4566 
4567 		tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XDP_NDO;
4568 	} else {
4569 		struct page *page = virt_to_page(xdpf->data);
4570 
4571 		dma_addr = page_pool_get_dma_addr(page) + sizeof(*xdpf) +
4572 			   xdpf->headroom;
4573 		dma_sync_single_for_device(priv->device, dma_addr,
4574 					   xdpf->len, DMA_BIDIRECTIONAL);
4575 
4576 		tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XDP_TX;
4577 	}
4578 
4579 	tx_q->tx_skbuff_dma[entry].buf = dma_addr;
4580 	tx_q->tx_skbuff_dma[entry].map_as_page = false;
4581 	tx_q->tx_skbuff_dma[entry].len = xdpf->len;
4582 	tx_q->tx_skbuff_dma[entry].last_segment = true;
4583 	tx_q->tx_skbuff_dma[entry].is_jumbo = false;
4584 
4585 	tx_q->xdpf[entry] = xdpf;
4586 
4587 	stmmac_set_desc_addr(priv, tx_desc, dma_addr);
4588 
4589 	stmmac_prepare_tx_desc(priv, tx_desc, 1, xdpf->len,
4590 			       true, priv->mode, true, true,
4591 			       xdpf->len);
4592 
4593 	tx_q->tx_count_frames++;
4594 
4595 	if (tx_q->tx_count_frames % priv->tx_coal_frames[queue] == 0)
4596 		set_ic = true;
4597 	else
4598 		set_ic = false;
4599 
4600 	if (set_ic) {
4601 		tx_q->tx_count_frames = 0;
4602 		stmmac_set_tx_ic(priv, tx_desc);
4603 		priv->xstats.tx_set_ic_bit++;
4604 	}
4605 
4606 	stmmac_enable_dma_transmission(priv, priv->ioaddr);
4607 
4608 	entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
4609 	tx_q->cur_tx = entry;
4610 
4611 	return STMMAC_XDP_TX;
4612 }
4613 
4614 static int stmmac_xdp_get_tx_queue(struct stmmac_priv *priv,
4615 				   int cpu)
4616 {
4617 	int index = cpu;
4618 
4619 	if (unlikely(index < 0))
4620 		index = 0;
4621 
4622 	while (index >= priv->plat->tx_queues_to_use)
4623 		index -= priv->plat->tx_queues_to_use;
4624 
4625 	return index;
4626 }
4627 
4628 static int stmmac_xdp_xmit_back(struct stmmac_priv *priv,
4629 				struct xdp_buff *xdp)
4630 {
4631 	struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp);
4632 	int cpu = smp_processor_id();
4633 	struct netdev_queue *nq;
4634 	int queue;
4635 	int res;
4636 
4637 	if (unlikely(!xdpf))
4638 		return STMMAC_XDP_CONSUMED;
4639 
4640 	queue = stmmac_xdp_get_tx_queue(priv, cpu);
4641 	nq = netdev_get_tx_queue(priv->dev, queue);
4642 
4643 	__netif_tx_lock(nq, cpu);
4644 	/* Avoids TX time-out as we are sharing with slow path */
4645 	nq->trans_start = jiffies;
4646 
4647 	res = stmmac_xdp_xmit_xdpf(priv, queue, xdpf, false);
4648 	if (res == STMMAC_XDP_TX)
4649 		stmmac_flush_tx_descriptors(priv, queue);
4650 
4651 	__netif_tx_unlock(nq);
4652 
4653 	return res;
4654 }
4655 
4656 /* This function assumes rcu_read_lock() is held by the caller. */
4657 static int __stmmac_xdp_run_prog(struct stmmac_priv *priv,
4658 				 struct bpf_prog *prog,
4659 				 struct xdp_buff *xdp)
4660 {
4661 	u32 act;
4662 	int res;
4663 
4664 	act = bpf_prog_run_xdp(prog, xdp);
4665 	switch (act) {
4666 	case XDP_PASS:
4667 		res = STMMAC_XDP_PASS;
4668 		break;
4669 	case XDP_TX:
4670 		res = stmmac_xdp_xmit_back(priv, xdp);
4671 		break;
4672 	case XDP_REDIRECT:
4673 		if (xdp_do_redirect(priv->dev, xdp, prog) < 0)
4674 			res = STMMAC_XDP_CONSUMED;
4675 		else
4676 			res = STMMAC_XDP_REDIRECT;
4677 		break;
4678 	default:
4679 		bpf_warn_invalid_xdp_action(act);
4680 		fallthrough;
4681 	case XDP_ABORTED:
4682 		trace_xdp_exception(priv->dev, prog, act);
4683 		fallthrough;
4684 	case XDP_DROP:
4685 		res = STMMAC_XDP_CONSUMED;
4686 		break;
4687 	}
4688 
4689 	return res;
4690 }
4691 
4692 static struct sk_buff *stmmac_xdp_run_prog(struct stmmac_priv *priv,
4693 					   struct xdp_buff *xdp)
4694 {
4695 	struct bpf_prog *prog;
4696 	int res;
4697 
4698 	rcu_read_lock();
4699 
4700 	prog = READ_ONCE(priv->xdp_prog);
4701 	if (!prog) {
4702 		res = STMMAC_XDP_PASS;
4703 		goto unlock;
4704 	}
4705 
4706 	res = __stmmac_xdp_run_prog(priv, prog, xdp);
4707 unlock:
4708 	rcu_read_unlock();
4709 	return ERR_PTR(-res);
4710 }
4711 
4712 static void stmmac_finalize_xdp_rx(struct stmmac_priv *priv,
4713 				   int xdp_status)
4714 {
4715 	int cpu = smp_processor_id();
4716 	int queue;
4717 
4718 	queue = stmmac_xdp_get_tx_queue(priv, cpu);
4719 
4720 	if (xdp_status & STMMAC_XDP_TX)
4721 		stmmac_tx_timer_arm(priv, queue);
4722 
4723 	if (xdp_status & STMMAC_XDP_REDIRECT)
4724 		xdp_do_flush();
4725 }
4726 
4727 static struct sk_buff *stmmac_construct_skb_zc(struct stmmac_channel *ch,
4728 					       struct xdp_buff *xdp)
4729 {
4730 	unsigned int metasize = xdp->data - xdp->data_meta;
4731 	unsigned int datasize = xdp->data_end - xdp->data;
4732 	struct sk_buff *skb;
4733 
4734 	skb = __napi_alloc_skb(&ch->rxtx_napi,
4735 			       xdp->data_end - xdp->data_hard_start,
4736 			       GFP_ATOMIC | __GFP_NOWARN);
4737 	if (unlikely(!skb))
4738 		return NULL;
4739 
4740 	skb_reserve(skb, xdp->data - xdp->data_hard_start);
4741 	memcpy(__skb_put(skb, datasize), xdp->data, datasize);
4742 	if (metasize)
4743 		skb_metadata_set(skb, metasize);
4744 
4745 	return skb;
4746 }
4747 
4748 static void stmmac_dispatch_skb_zc(struct stmmac_priv *priv, u32 queue,
4749 				   struct dma_desc *p, struct dma_desc *np,
4750 				   struct xdp_buff *xdp)
4751 {
4752 	struct stmmac_channel *ch = &priv->channel[queue];
4753 	unsigned int len = xdp->data_end - xdp->data;
4754 	enum pkt_hash_types hash_type;
4755 	int coe = priv->hw->rx_csum;
4756 	struct sk_buff *skb;
4757 	u32 hash;
4758 
4759 	skb = stmmac_construct_skb_zc(ch, xdp);
4760 	if (!skb) {
4761 		priv->dev->stats.rx_dropped++;
4762 		return;
4763 	}
4764 
4765 	stmmac_get_rx_hwtstamp(priv, p, np, skb);
4766 	stmmac_rx_vlan(priv->dev, skb);
4767 	skb->protocol = eth_type_trans(skb, priv->dev);
4768 
4769 	if (unlikely(!coe))
4770 		skb_checksum_none_assert(skb);
4771 	else
4772 		skb->ip_summed = CHECKSUM_UNNECESSARY;
4773 
4774 	if (!stmmac_get_rx_hash(priv, p, &hash, &hash_type))
4775 		skb_set_hash(skb, hash, hash_type);
4776 
4777 	skb_record_rx_queue(skb, queue);
4778 	napi_gro_receive(&ch->rxtx_napi, skb);
4779 
4780 	priv->dev->stats.rx_packets++;
4781 	priv->dev->stats.rx_bytes += len;
4782 }
4783 
4784 static bool stmmac_rx_refill_zc(struct stmmac_priv *priv, u32 queue, u32 budget)
4785 {
4786 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4787 	unsigned int entry = rx_q->dirty_rx;
4788 	struct dma_desc *rx_desc = NULL;
4789 	bool ret = true;
4790 
4791 	budget = min(budget, stmmac_rx_dirty(priv, queue));
4792 
4793 	while (budget-- > 0 && entry != rx_q->cur_rx) {
4794 		struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry];
4795 		dma_addr_t dma_addr;
4796 		bool use_rx_wd;
4797 
4798 		if (!buf->xdp) {
4799 			buf->xdp = xsk_buff_alloc(rx_q->xsk_pool);
4800 			if (!buf->xdp) {
4801 				ret = false;
4802 				break;
4803 			}
4804 		}
4805 
4806 		if (priv->extend_desc)
4807 			rx_desc = (struct dma_desc *)(rx_q->dma_erx + entry);
4808 		else
4809 			rx_desc = rx_q->dma_rx + entry;
4810 
4811 		dma_addr = xsk_buff_xdp_get_dma(buf->xdp);
4812 		stmmac_set_desc_addr(priv, rx_desc, dma_addr);
4813 		stmmac_set_desc_sec_addr(priv, rx_desc, 0, false);
4814 		stmmac_refill_desc3(priv, rx_q, rx_desc);
4815 
4816 		rx_q->rx_count_frames++;
4817 		rx_q->rx_count_frames += priv->rx_coal_frames[queue];
4818 		if (rx_q->rx_count_frames > priv->rx_coal_frames[queue])
4819 			rx_q->rx_count_frames = 0;
4820 
4821 		use_rx_wd = !priv->rx_coal_frames[queue];
4822 		use_rx_wd |= rx_q->rx_count_frames > 0;
4823 		if (!priv->use_riwt)
4824 			use_rx_wd = false;
4825 
4826 		dma_wmb();
4827 		stmmac_set_rx_owner(priv, rx_desc, use_rx_wd);
4828 
4829 		entry = STMMAC_GET_ENTRY(entry, priv->dma_rx_size);
4830 	}
4831 
4832 	if (rx_desc) {
4833 		rx_q->dirty_rx = entry;
4834 		rx_q->rx_tail_addr = rx_q->dma_rx_phy +
4835 				     (rx_q->dirty_rx * sizeof(struct dma_desc));
4836 		stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue);
4837 	}
4838 
4839 	return ret;
4840 }
4841 
4842 static int stmmac_rx_zc(struct stmmac_priv *priv, int limit, u32 queue)
4843 {
4844 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4845 	unsigned int count = 0, error = 0, len = 0;
4846 	int dirty = stmmac_rx_dirty(priv, queue);
4847 	unsigned int next_entry = rx_q->cur_rx;
4848 	unsigned int desc_size;
4849 	struct bpf_prog *prog;
4850 	bool failure = false;
4851 	int xdp_status = 0;
4852 	int status = 0;
4853 
4854 	if (netif_msg_rx_status(priv)) {
4855 		void *rx_head;
4856 
4857 		netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
4858 		if (priv->extend_desc) {
4859 			rx_head = (void *)rx_q->dma_erx;
4860 			desc_size = sizeof(struct dma_extended_desc);
4861 		} else {
4862 			rx_head = (void *)rx_q->dma_rx;
4863 			desc_size = sizeof(struct dma_desc);
4864 		}
4865 
4866 		stmmac_display_ring(priv, rx_head, priv->dma_rx_size, true,
4867 				    rx_q->dma_rx_phy, desc_size);
4868 	}
4869 	while (count < limit) {
4870 		struct stmmac_rx_buffer *buf;
4871 		unsigned int buf1_len = 0;
4872 		struct dma_desc *np, *p;
4873 		int entry;
4874 		int res;
4875 
4876 		if (!count && rx_q->state_saved) {
4877 			error = rx_q->state.error;
4878 			len = rx_q->state.len;
4879 		} else {
4880 			rx_q->state_saved = false;
4881 			error = 0;
4882 			len = 0;
4883 		}
4884 
4885 		if (count >= limit)
4886 			break;
4887 
4888 read_again:
4889 		buf1_len = 0;
4890 		entry = next_entry;
4891 		buf = &rx_q->buf_pool[entry];
4892 
4893 		if (dirty >= STMMAC_RX_FILL_BATCH) {
4894 			failure = failure ||
4895 				  !stmmac_rx_refill_zc(priv, queue, dirty);
4896 			dirty = 0;
4897 		}
4898 
4899 		if (priv->extend_desc)
4900 			p = (struct dma_desc *)(rx_q->dma_erx + entry);
4901 		else
4902 			p = rx_q->dma_rx + entry;
4903 
4904 		/* read the status of the incoming frame */
4905 		status = stmmac_rx_status(priv, &priv->dev->stats,
4906 					  &priv->xstats, p);
4907 		/* check if managed by the DMA otherwise go ahead */
4908 		if (unlikely(status & dma_own))
4909 			break;
4910 
4911 		/* Prefetch the next RX descriptor */
4912 		rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx,
4913 						priv->dma_rx_size);
4914 		next_entry = rx_q->cur_rx;
4915 
4916 		if (priv->extend_desc)
4917 			np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
4918 		else
4919 			np = rx_q->dma_rx + next_entry;
4920 
4921 		prefetch(np);
4922 
4923 		if (priv->extend_desc)
4924 			stmmac_rx_extended_status(priv, &priv->dev->stats,
4925 						  &priv->xstats,
4926 						  rx_q->dma_erx + entry);
4927 		if (unlikely(status == discard_frame)) {
4928 			xsk_buff_free(buf->xdp);
4929 			buf->xdp = NULL;
4930 			dirty++;
4931 			error = 1;
4932 			if (!priv->hwts_rx_en)
4933 				priv->dev->stats.rx_errors++;
4934 		}
4935 
4936 		if (unlikely(error && (status & rx_not_ls)))
4937 			goto read_again;
4938 		if (unlikely(error)) {
4939 			count++;
4940 			continue;
4941 		}
4942 
4943 		/* Ensure a valid XSK buffer before proceed */
4944 		if (!buf->xdp)
4945 			break;
4946 
4947 		/* XSK pool expects RX frame 1:1 mapped to XSK buffer */
4948 		if (likely(status & rx_not_ls)) {
4949 			xsk_buff_free(buf->xdp);
4950 			buf->xdp = NULL;
4951 			dirty++;
4952 			count++;
4953 			goto read_again;
4954 		}
4955 
4956 		/* XDP ZC Frame only support primary buffers for now */
4957 		buf1_len = stmmac_rx_buf1_len(priv, p, status, len);
4958 		len += buf1_len;
4959 
4960 		/* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
4961 		 * Type frames (LLC/LLC-SNAP)
4962 		 *
4963 		 * llc_snap is never checked in GMAC >= 4, so this ACS
4964 		 * feature is always disabled and packets need to be
4965 		 * stripped manually.
4966 		 */
4967 		if (likely(!(status & rx_not_ls)) &&
4968 		    (likely(priv->synopsys_id >= DWMAC_CORE_4_00) ||
4969 		     unlikely(status != llc_snap))) {
4970 			buf1_len -= ETH_FCS_LEN;
4971 			len -= ETH_FCS_LEN;
4972 		}
4973 
4974 		/* RX buffer is good and fit into a XSK pool buffer */
4975 		buf->xdp->data_end = buf->xdp->data + buf1_len;
4976 		xsk_buff_dma_sync_for_cpu(buf->xdp, rx_q->xsk_pool);
4977 
4978 		rcu_read_lock();
4979 		prog = READ_ONCE(priv->xdp_prog);
4980 		res = __stmmac_xdp_run_prog(priv, prog, buf->xdp);
4981 		rcu_read_unlock();
4982 
4983 		switch (res) {
4984 		case STMMAC_XDP_PASS:
4985 			stmmac_dispatch_skb_zc(priv, queue, p, np, buf->xdp);
4986 			xsk_buff_free(buf->xdp);
4987 			break;
4988 		case STMMAC_XDP_CONSUMED:
4989 			xsk_buff_free(buf->xdp);
4990 			priv->dev->stats.rx_dropped++;
4991 			break;
4992 		case STMMAC_XDP_TX:
4993 		case STMMAC_XDP_REDIRECT:
4994 			xdp_status |= res;
4995 			break;
4996 		}
4997 
4998 		buf->xdp = NULL;
4999 		dirty++;
5000 		count++;
5001 	}
5002 
5003 	if (status & rx_not_ls) {
5004 		rx_q->state_saved = true;
5005 		rx_q->state.error = error;
5006 		rx_q->state.len = len;
5007 	}
5008 
5009 	stmmac_finalize_xdp_rx(priv, xdp_status);
5010 
5011 	if (xsk_uses_need_wakeup(rx_q->xsk_pool)) {
5012 		if (failure || stmmac_rx_dirty(priv, queue) > 0)
5013 			xsk_set_rx_need_wakeup(rx_q->xsk_pool);
5014 		else
5015 			xsk_clear_rx_need_wakeup(rx_q->xsk_pool);
5016 
5017 		return (int)count;
5018 	}
5019 
5020 	return failure ? limit : (int)count;
5021 }
5022 
5023 /**
5024  * stmmac_rx - manage the receive process
5025  * @priv: driver private structure
5026  * @limit: napi bugget
5027  * @queue: RX queue index.
5028  * Description :  this the function called by the napi poll method.
5029  * It gets all the frames inside the ring.
5030  */
5031 static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
5032 {
5033 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
5034 	struct stmmac_channel *ch = &priv->channel[queue];
5035 	unsigned int count = 0, error = 0, len = 0;
5036 	int status = 0, coe = priv->hw->rx_csum;
5037 	unsigned int next_entry = rx_q->cur_rx;
5038 	enum dma_data_direction dma_dir;
5039 	unsigned int desc_size;
5040 	struct sk_buff *skb = NULL;
5041 	struct xdp_buff xdp;
5042 	int xdp_status = 0;
5043 	int buf_sz;
5044 
5045 	dma_dir = page_pool_get_dma_dir(rx_q->page_pool);
5046 	buf_sz = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE) * PAGE_SIZE;
5047 
5048 	if (netif_msg_rx_status(priv)) {
5049 		void *rx_head;
5050 
5051 		netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
5052 		if (priv->extend_desc) {
5053 			rx_head = (void *)rx_q->dma_erx;
5054 			desc_size = sizeof(struct dma_extended_desc);
5055 		} else {
5056 			rx_head = (void *)rx_q->dma_rx;
5057 			desc_size = sizeof(struct dma_desc);
5058 		}
5059 
5060 		stmmac_display_ring(priv, rx_head, priv->dma_rx_size, true,
5061 				    rx_q->dma_rx_phy, desc_size);
5062 	}
5063 	while (count < limit) {
5064 		unsigned int buf1_len = 0, buf2_len = 0;
5065 		enum pkt_hash_types hash_type;
5066 		struct stmmac_rx_buffer *buf;
5067 		struct dma_desc *np, *p;
5068 		int entry;
5069 		u32 hash;
5070 
5071 		if (!count && rx_q->state_saved) {
5072 			skb = rx_q->state.skb;
5073 			error = rx_q->state.error;
5074 			len = rx_q->state.len;
5075 		} else {
5076 			rx_q->state_saved = false;
5077 			skb = NULL;
5078 			error = 0;
5079 			len = 0;
5080 		}
5081 
5082 		if (count >= limit)
5083 			break;
5084 
5085 read_again:
5086 		buf1_len = 0;
5087 		buf2_len = 0;
5088 		entry = next_entry;
5089 		buf = &rx_q->buf_pool[entry];
5090 
5091 		if (priv->extend_desc)
5092 			p = (struct dma_desc *)(rx_q->dma_erx + entry);
5093 		else
5094 			p = rx_q->dma_rx + entry;
5095 
5096 		/* read the status of the incoming frame */
5097 		status = stmmac_rx_status(priv, &priv->dev->stats,
5098 				&priv->xstats, p);
5099 		/* check if managed by the DMA otherwise go ahead */
5100 		if (unlikely(status & dma_own))
5101 			break;
5102 
5103 		rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx,
5104 						priv->dma_rx_size);
5105 		next_entry = rx_q->cur_rx;
5106 
5107 		if (priv->extend_desc)
5108 			np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
5109 		else
5110 			np = rx_q->dma_rx + next_entry;
5111 
5112 		prefetch(np);
5113 
5114 		if (priv->extend_desc)
5115 			stmmac_rx_extended_status(priv, &priv->dev->stats,
5116 					&priv->xstats, rx_q->dma_erx + entry);
5117 		if (unlikely(status == discard_frame)) {
5118 			page_pool_recycle_direct(rx_q->page_pool, buf->page);
5119 			buf->page = NULL;
5120 			error = 1;
5121 			if (!priv->hwts_rx_en)
5122 				priv->dev->stats.rx_errors++;
5123 		}
5124 
5125 		if (unlikely(error && (status & rx_not_ls)))
5126 			goto read_again;
5127 		if (unlikely(error)) {
5128 			dev_kfree_skb(skb);
5129 			skb = NULL;
5130 			count++;
5131 			continue;
5132 		}
5133 
5134 		/* Buffer is good. Go on. */
5135 
5136 		prefetch(page_address(buf->page));
5137 		if (buf->sec_page)
5138 			prefetch(page_address(buf->sec_page));
5139 
5140 		buf1_len = stmmac_rx_buf1_len(priv, p, status, len);
5141 		len += buf1_len;
5142 		buf2_len = stmmac_rx_buf2_len(priv, p, status, len);
5143 		len += buf2_len;
5144 
5145 		/* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
5146 		 * Type frames (LLC/LLC-SNAP)
5147 		 *
5148 		 * llc_snap is never checked in GMAC >= 4, so this ACS
5149 		 * feature is always disabled and packets need to be
5150 		 * stripped manually.
5151 		 */
5152 		if (likely(!(status & rx_not_ls)) &&
5153 		    (likely(priv->synopsys_id >= DWMAC_CORE_4_00) ||
5154 		     unlikely(status != llc_snap))) {
5155 			if (buf2_len)
5156 				buf2_len -= ETH_FCS_LEN;
5157 			else
5158 				buf1_len -= ETH_FCS_LEN;
5159 
5160 			len -= ETH_FCS_LEN;
5161 		}
5162 
5163 		if (!skb) {
5164 			unsigned int pre_len, sync_len;
5165 
5166 			dma_sync_single_for_cpu(priv->device, buf->addr,
5167 						buf1_len, dma_dir);
5168 
5169 			xdp.data = page_address(buf->page) + buf->page_offset;
5170 			xdp.data_end = xdp.data + buf1_len;
5171 			xdp.data_hard_start = page_address(buf->page);
5172 			xdp_set_data_meta_invalid(&xdp);
5173 			xdp.frame_sz = buf_sz;
5174 			xdp.rxq = &rx_q->xdp_rxq;
5175 
5176 			pre_len = xdp.data_end - xdp.data_hard_start -
5177 				  buf->page_offset;
5178 			skb = stmmac_xdp_run_prog(priv, &xdp);
5179 			/* Due xdp_adjust_tail: DMA sync for_device
5180 			 * cover max len CPU touch
5181 			 */
5182 			sync_len = xdp.data_end - xdp.data_hard_start -
5183 				   buf->page_offset;
5184 			sync_len = max(sync_len, pre_len);
5185 
5186 			/* For Not XDP_PASS verdict */
5187 			if (IS_ERR(skb)) {
5188 				unsigned int xdp_res = -PTR_ERR(skb);
5189 
5190 				if (xdp_res & STMMAC_XDP_CONSUMED) {
5191 					page_pool_put_page(rx_q->page_pool,
5192 							   virt_to_head_page(xdp.data),
5193 							   sync_len, true);
5194 					buf->page = NULL;
5195 					priv->dev->stats.rx_dropped++;
5196 
5197 					/* Clear skb as it was set as
5198 					 * status by XDP program.
5199 					 */
5200 					skb = NULL;
5201 
5202 					if (unlikely((status & rx_not_ls)))
5203 						goto read_again;
5204 
5205 					count++;
5206 					continue;
5207 				} else if (xdp_res & (STMMAC_XDP_TX |
5208 						      STMMAC_XDP_REDIRECT)) {
5209 					xdp_status |= xdp_res;
5210 					buf->page = NULL;
5211 					skb = NULL;
5212 					count++;
5213 					continue;
5214 				}
5215 			}
5216 		}
5217 
5218 		if (!skb) {
5219 			/* XDP program may expand or reduce tail */
5220 			buf1_len = xdp.data_end - xdp.data;
5221 
5222 			skb = napi_alloc_skb(&ch->rx_napi, buf1_len);
5223 			if (!skb) {
5224 				priv->dev->stats.rx_dropped++;
5225 				count++;
5226 				goto drain_data;
5227 			}
5228 
5229 			/* XDP program may adjust header */
5230 			skb_copy_to_linear_data(skb, xdp.data, buf1_len);
5231 			skb_put(skb, buf1_len);
5232 
5233 			/* Data payload copied into SKB, page ready for recycle */
5234 			page_pool_recycle_direct(rx_q->page_pool, buf->page);
5235 			buf->page = NULL;
5236 		} else if (buf1_len) {
5237 			dma_sync_single_for_cpu(priv->device, buf->addr,
5238 						buf1_len, dma_dir);
5239 			skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
5240 					buf->page, buf->page_offset, buf1_len,
5241 					priv->dma_buf_sz);
5242 
5243 			/* Data payload appended into SKB */
5244 			page_pool_release_page(rx_q->page_pool, buf->page);
5245 			buf->page = NULL;
5246 		}
5247 
5248 		if (buf2_len) {
5249 			dma_sync_single_for_cpu(priv->device, buf->sec_addr,
5250 						buf2_len, dma_dir);
5251 			skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
5252 					buf->sec_page, 0, buf2_len,
5253 					priv->dma_buf_sz);
5254 
5255 			/* Data payload appended into SKB */
5256 			page_pool_release_page(rx_q->page_pool, buf->sec_page);
5257 			buf->sec_page = NULL;
5258 		}
5259 
5260 drain_data:
5261 		if (likely(status & rx_not_ls))
5262 			goto read_again;
5263 		if (!skb)
5264 			continue;
5265 
5266 		/* Got entire packet into SKB. Finish it. */
5267 
5268 		stmmac_get_rx_hwtstamp(priv, p, np, skb);
5269 		stmmac_rx_vlan(priv->dev, skb);
5270 		skb->protocol = eth_type_trans(skb, priv->dev);
5271 
5272 		if (unlikely(!coe))
5273 			skb_checksum_none_assert(skb);
5274 		else
5275 			skb->ip_summed = CHECKSUM_UNNECESSARY;
5276 
5277 		if (!stmmac_get_rx_hash(priv, p, &hash, &hash_type))
5278 			skb_set_hash(skb, hash, hash_type);
5279 
5280 		skb_record_rx_queue(skb, queue);
5281 		napi_gro_receive(&ch->rx_napi, skb);
5282 		skb = NULL;
5283 
5284 		priv->dev->stats.rx_packets++;
5285 		priv->dev->stats.rx_bytes += len;
5286 		count++;
5287 	}
5288 
5289 	if (status & rx_not_ls || skb) {
5290 		rx_q->state_saved = true;
5291 		rx_q->state.skb = skb;
5292 		rx_q->state.error = error;
5293 		rx_q->state.len = len;
5294 	}
5295 
5296 	stmmac_finalize_xdp_rx(priv, xdp_status);
5297 
5298 	stmmac_rx_refill(priv, queue);
5299 
5300 	priv->xstats.rx_pkt_n += count;
5301 
5302 	return count;
5303 }
5304 
5305 static int stmmac_napi_poll_rx(struct napi_struct *napi, int budget)
5306 {
5307 	struct stmmac_channel *ch =
5308 		container_of(napi, struct stmmac_channel, rx_napi);
5309 	struct stmmac_priv *priv = ch->priv_data;
5310 	u32 chan = ch->index;
5311 	int work_done;
5312 
5313 	priv->xstats.napi_poll++;
5314 
5315 	work_done = stmmac_rx(priv, budget, chan);
5316 	if (work_done < budget && napi_complete_done(napi, work_done)) {
5317 		unsigned long flags;
5318 
5319 		spin_lock_irqsave(&ch->lock, flags);
5320 		stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
5321 		spin_unlock_irqrestore(&ch->lock, flags);
5322 	}
5323 
5324 	return work_done;
5325 }
5326 
5327 static int stmmac_napi_poll_tx(struct napi_struct *napi, int budget)
5328 {
5329 	struct stmmac_channel *ch =
5330 		container_of(napi, struct stmmac_channel, tx_napi);
5331 	struct stmmac_priv *priv = ch->priv_data;
5332 	u32 chan = ch->index;
5333 	int work_done;
5334 
5335 	priv->xstats.napi_poll++;
5336 
5337 	work_done = stmmac_tx_clean(priv, budget, chan);
5338 	work_done = min(work_done, budget);
5339 
5340 	if (work_done < budget && napi_complete_done(napi, work_done)) {
5341 		unsigned long flags;
5342 
5343 		spin_lock_irqsave(&ch->lock, flags);
5344 		stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
5345 		spin_unlock_irqrestore(&ch->lock, flags);
5346 	}
5347 
5348 	return work_done;
5349 }
5350 
5351 static int stmmac_napi_poll_rxtx(struct napi_struct *napi, int budget)
5352 {
5353 	struct stmmac_channel *ch =
5354 		container_of(napi, struct stmmac_channel, rxtx_napi);
5355 	struct stmmac_priv *priv = ch->priv_data;
5356 	int rx_done, tx_done;
5357 	u32 chan = ch->index;
5358 
5359 	priv->xstats.napi_poll++;
5360 
5361 	tx_done = stmmac_tx_clean(priv, budget, chan);
5362 	tx_done = min(tx_done, budget);
5363 
5364 	rx_done = stmmac_rx_zc(priv, budget, chan);
5365 
5366 	/* If either TX or RX work is not complete, return budget
5367 	 * and keep pooling
5368 	 */
5369 	if (tx_done >= budget || rx_done >= budget)
5370 		return budget;
5371 
5372 	/* all work done, exit the polling mode */
5373 	if (napi_complete_done(napi, rx_done)) {
5374 		unsigned long flags;
5375 
5376 		spin_lock_irqsave(&ch->lock, flags);
5377 		/* Both RX and TX work done are compelte,
5378 		 * so enable both RX & TX IRQs.
5379 		 */
5380 		stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 1);
5381 		spin_unlock_irqrestore(&ch->lock, flags);
5382 	}
5383 
5384 	return min(rx_done, budget - 1);
5385 }
5386 
5387 /**
5388  *  stmmac_tx_timeout
5389  *  @dev : Pointer to net device structure
5390  *  @txqueue: the index of the hanging transmit queue
5391  *  Description: this function is called when a packet transmission fails to
5392  *   complete within a reasonable time. The driver will mark the error in the
5393  *   netdev structure and arrange for the device to be reset to a sane state
5394  *   in order to transmit a new packet.
5395  */
5396 static void stmmac_tx_timeout(struct net_device *dev, unsigned int txqueue)
5397 {
5398 	struct stmmac_priv *priv = netdev_priv(dev);
5399 
5400 	stmmac_global_err(priv);
5401 }
5402 
5403 /**
5404  *  stmmac_set_rx_mode - entry point for multicast addressing
5405  *  @dev : pointer to the device structure
5406  *  Description:
5407  *  This function is a driver entry point which gets called by the kernel
5408  *  whenever multicast addresses must be enabled/disabled.
5409  *  Return value:
5410  *  void.
5411  */
5412 static void stmmac_set_rx_mode(struct net_device *dev)
5413 {
5414 	struct stmmac_priv *priv = netdev_priv(dev);
5415 
5416 	stmmac_set_filter(priv, priv->hw, dev);
5417 }
5418 
5419 /**
5420  *  stmmac_change_mtu - entry point to change MTU size for the device.
5421  *  @dev : device pointer.
5422  *  @new_mtu : the new MTU size for the device.
5423  *  Description: the Maximum Transfer Unit (MTU) is used by the network layer
5424  *  to drive packet transmission. Ethernet has an MTU of 1500 octets
5425  *  (ETH_DATA_LEN). This value can be changed with ifconfig.
5426  *  Return value:
5427  *  0 on success and an appropriate (-)ve integer as defined in errno.h
5428  *  file on failure.
5429  */
5430 static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
5431 {
5432 	struct stmmac_priv *priv = netdev_priv(dev);
5433 	int txfifosz = priv->plat->tx_fifo_size;
5434 	const int mtu = new_mtu;
5435 
5436 	if (txfifosz == 0)
5437 		txfifosz = priv->dma_cap.tx_fifo_size;
5438 
5439 	txfifosz /= priv->plat->tx_queues_to_use;
5440 
5441 	if (netif_running(dev)) {
5442 		netdev_err(priv->dev, "must be stopped to change its MTU\n");
5443 		return -EBUSY;
5444 	}
5445 
5446 	if (stmmac_xdp_is_enabled(priv) && new_mtu > ETH_DATA_LEN) {
5447 		netdev_dbg(priv->dev, "Jumbo frames not supported for XDP\n");
5448 		return -EINVAL;
5449 	}
5450 
5451 	new_mtu = STMMAC_ALIGN(new_mtu);
5452 
5453 	/* If condition true, FIFO is too small or MTU too large */
5454 	if ((txfifosz < new_mtu) || (new_mtu > BUF_SIZE_16KiB))
5455 		return -EINVAL;
5456 
5457 	dev->mtu = mtu;
5458 
5459 	netdev_update_features(dev);
5460 
5461 	return 0;
5462 }
5463 
5464 static netdev_features_t stmmac_fix_features(struct net_device *dev,
5465 					     netdev_features_t features)
5466 {
5467 	struct stmmac_priv *priv = netdev_priv(dev);
5468 
5469 	if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
5470 		features &= ~NETIF_F_RXCSUM;
5471 
5472 	if (!priv->plat->tx_coe)
5473 		features &= ~NETIF_F_CSUM_MASK;
5474 
5475 	/* Some GMAC devices have a bugged Jumbo frame support that
5476 	 * needs to have the Tx COE disabled for oversized frames
5477 	 * (due to limited buffer sizes). In this case we disable
5478 	 * the TX csum insertion in the TDES and not use SF.
5479 	 */
5480 	if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
5481 		features &= ~NETIF_F_CSUM_MASK;
5482 
5483 	/* Disable tso if asked by ethtool */
5484 	if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
5485 		if (features & NETIF_F_TSO)
5486 			priv->tso = true;
5487 		else
5488 			priv->tso = false;
5489 	}
5490 
5491 	return features;
5492 }
5493 
5494 static int stmmac_set_features(struct net_device *netdev,
5495 			       netdev_features_t features)
5496 {
5497 	struct stmmac_priv *priv = netdev_priv(netdev);
5498 	bool sph_en;
5499 	u32 chan;
5500 
5501 	/* Keep the COE Type in case of csum is supporting */
5502 	if (features & NETIF_F_RXCSUM)
5503 		priv->hw->rx_csum = priv->plat->rx_coe;
5504 	else
5505 		priv->hw->rx_csum = 0;
5506 	/* No check needed because rx_coe has been set before and it will be
5507 	 * fixed in case of issue.
5508 	 */
5509 	stmmac_rx_ipc(priv, priv->hw);
5510 
5511 	sph_en = (priv->hw->rx_csum > 0) && priv->sph;
5512 
5513 	for (chan = 0; chan < priv->plat->rx_queues_to_use; chan++)
5514 		stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
5515 
5516 	return 0;
5517 }
5518 
5519 static void stmmac_fpe_event_status(struct stmmac_priv *priv, int status)
5520 {
5521 	struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
5522 	enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
5523 	enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
5524 	bool *hs_enable = &fpe_cfg->hs_enable;
5525 
5526 	if (status == FPE_EVENT_UNKNOWN || !*hs_enable)
5527 		return;
5528 
5529 	/* If LP has sent verify mPacket, LP is FPE capable */
5530 	if ((status & FPE_EVENT_RVER) == FPE_EVENT_RVER) {
5531 		if (*lp_state < FPE_STATE_CAPABLE)
5532 			*lp_state = FPE_STATE_CAPABLE;
5533 
5534 		/* If user has requested FPE enable, quickly response */
5535 		if (*hs_enable)
5536 			stmmac_fpe_send_mpacket(priv, priv->ioaddr,
5537 						MPACKET_RESPONSE);
5538 	}
5539 
5540 	/* If Local has sent verify mPacket, Local is FPE capable */
5541 	if ((status & FPE_EVENT_TVER) == FPE_EVENT_TVER) {
5542 		if (*lo_state < FPE_STATE_CAPABLE)
5543 			*lo_state = FPE_STATE_CAPABLE;
5544 	}
5545 
5546 	/* If LP has sent response mPacket, LP is entering FPE ON */
5547 	if ((status & FPE_EVENT_RRSP) == FPE_EVENT_RRSP)
5548 		*lp_state = FPE_STATE_ENTERING_ON;
5549 
5550 	/* If Local has sent response mPacket, Local is entering FPE ON */
5551 	if ((status & FPE_EVENT_TRSP) == FPE_EVENT_TRSP)
5552 		*lo_state = FPE_STATE_ENTERING_ON;
5553 
5554 	if (!test_bit(__FPE_REMOVING, &priv->fpe_task_state) &&
5555 	    !test_and_set_bit(__FPE_TASK_SCHED, &priv->fpe_task_state) &&
5556 	    priv->fpe_wq) {
5557 		queue_work(priv->fpe_wq, &priv->fpe_task);
5558 	}
5559 }
5560 
5561 static void stmmac_common_interrupt(struct stmmac_priv *priv)
5562 {
5563 	u32 rx_cnt = priv->plat->rx_queues_to_use;
5564 	u32 tx_cnt = priv->plat->tx_queues_to_use;
5565 	u32 queues_count;
5566 	u32 queue;
5567 	bool xmac;
5568 
5569 	xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
5570 	queues_count = (rx_cnt > tx_cnt) ? rx_cnt : tx_cnt;
5571 
5572 	if (priv->irq_wake)
5573 		pm_wakeup_event(priv->device, 0);
5574 
5575 	if (priv->dma_cap.estsel)
5576 		stmmac_est_irq_status(priv, priv->ioaddr, priv->dev,
5577 				      &priv->xstats, tx_cnt);
5578 
5579 	if (priv->dma_cap.fpesel) {
5580 		int status = stmmac_fpe_irq_status(priv, priv->ioaddr,
5581 						   priv->dev);
5582 
5583 		stmmac_fpe_event_status(priv, status);
5584 	}
5585 
5586 	/* To handle GMAC own interrupts */
5587 	if ((priv->plat->has_gmac) || xmac) {
5588 		int status = stmmac_host_irq_status(priv, priv->hw, &priv->xstats);
5589 		int mtl_status;
5590 
5591 		if (unlikely(status)) {
5592 			/* For LPI we need to save the tx status */
5593 			if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
5594 				priv->tx_path_in_lpi_mode = true;
5595 			if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
5596 				priv->tx_path_in_lpi_mode = false;
5597 		}
5598 
5599 		for (queue = 0; queue < queues_count; queue++) {
5600 			struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
5601 
5602 			mtl_status = stmmac_host_mtl_irq_status(priv, priv->hw,
5603 								queue);
5604 			if (mtl_status != -EINVAL)
5605 				status |= mtl_status;
5606 
5607 			if (status & CORE_IRQ_MTL_RX_OVERFLOW)
5608 				stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
5609 						       rx_q->rx_tail_addr,
5610 						       queue);
5611 		}
5612 
5613 		/* PCS link status */
5614 		if (priv->hw->pcs) {
5615 			if (priv->xstats.pcs_link)
5616 				netif_carrier_on(priv->dev);
5617 			else
5618 				netif_carrier_off(priv->dev);
5619 		}
5620 
5621 		stmmac_timestamp_interrupt(priv, priv);
5622 	}
5623 }
5624 
5625 /**
5626  *  stmmac_interrupt - main ISR
5627  *  @irq: interrupt number.
5628  *  @dev_id: to pass the net device pointer.
5629  *  Description: this is the main driver interrupt service routine.
5630  *  It can call:
5631  *  o DMA service routine (to manage incoming frame reception and transmission
5632  *    status)
5633  *  o Core interrupts to manage: remote wake-up, management counter, LPI
5634  *    interrupts.
5635  */
5636 static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
5637 {
5638 	struct net_device *dev = (struct net_device *)dev_id;
5639 	struct stmmac_priv *priv = netdev_priv(dev);
5640 
5641 	/* Check if adapter is up */
5642 	if (test_bit(STMMAC_DOWN, &priv->state))
5643 		return IRQ_HANDLED;
5644 
5645 	/* Check if a fatal error happened */
5646 	if (stmmac_safety_feat_interrupt(priv))
5647 		return IRQ_HANDLED;
5648 
5649 	/* To handle Common interrupts */
5650 	stmmac_common_interrupt(priv);
5651 
5652 	/* To handle DMA interrupts */
5653 	stmmac_dma_interrupt(priv);
5654 
5655 	return IRQ_HANDLED;
5656 }
5657 
5658 static irqreturn_t stmmac_mac_interrupt(int irq, void *dev_id)
5659 {
5660 	struct net_device *dev = (struct net_device *)dev_id;
5661 	struct stmmac_priv *priv = netdev_priv(dev);
5662 
5663 	if (unlikely(!dev)) {
5664 		netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5665 		return IRQ_NONE;
5666 	}
5667 
5668 	/* Check if adapter is up */
5669 	if (test_bit(STMMAC_DOWN, &priv->state))
5670 		return IRQ_HANDLED;
5671 
5672 	/* To handle Common interrupts */
5673 	stmmac_common_interrupt(priv);
5674 
5675 	return IRQ_HANDLED;
5676 }
5677 
5678 static irqreturn_t stmmac_safety_interrupt(int irq, void *dev_id)
5679 {
5680 	struct net_device *dev = (struct net_device *)dev_id;
5681 	struct stmmac_priv *priv = netdev_priv(dev);
5682 
5683 	if (unlikely(!dev)) {
5684 		netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5685 		return IRQ_NONE;
5686 	}
5687 
5688 	/* Check if adapter is up */
5689 	if (test_bit(STMMAC_DOWN, &priv->state))
5690 		return IRQ_HANDLED;
5691 
5692 	/* Check if a fatal error happened */
5693 	stmmac_safety_feat_interrupt(priv);
5694 
5695 	return IRQ_HANDLED;
5696 }
5697 
5698 static irqreturn_t stmmac_msi_intr_tx(int irq, void *data)
5699 {
5700 	struct stmmac_tx_queue *tx_q = (struct stmmac_tx_queue *)data;
5701 	int chan = tx_q->queue_index;
5702 	struct stmmac_priv *priv;
5703 	int status;
5704 
5705 	priv = container_of(tx_q, struct stmmac_priv, tx_queue[chan]);
5706 
5707 	if (unlikely(!data)) {
5708 		netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5709 		return IRQ_NONE;
5710 	}
5711 
5712 	/* Check if adapter is up */
5713 	if (test_bit(STMMAC_DOWN, &priv->state))
5714 		return IRQ_HANDLED;
5715 
5716 	status = stmmac_napi_check(priv, chan, DMA_DIR_TX);
5717 
5718 	if (unlikely(status & tx_hard_error_bump_tc)) {
5719 		/* Try to bump up the dma threshold on this failure */
5720 		if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
5721 		    tc <= 256) {
5722 			tc += 64;
5723 			if (priv->plat->force_thresh_dma_mode)
5724 				stmmac_set_dma_operation_mode(priv,
5725 							      tc,
5726 							      tc,
5727 							      chan);
5728 			else
5729 				stmmac_set_dma_operation_mode(priv,
5730 							      tc,
5731 							      SF_DMA_MODE,
5732 							      chan);
5733 			priv->xstats.threshold = tc;
5734 		}
5735 	} else if (unlikely(status == tx_hard_error)) {
5736 		stmmac_tx_err(priv, chan);
5737 	}
5738 
5739 	return IRQ_HANDLED;
5740 }
5741 
5742 static irqreturn_t stmmac_msi_intr_rx(int irq, void *data)
5743 {
5744 	struct stmmac_rx_queue *rx_q = (struct stmmac_rx_queue *)data;
5745 	int chan = rx_q->queue_index;
5746 	struct stmmac_priv *priv;
5747 
5748 	priv = container_of(rx_q, struct stmmac_priv, rx_queue[chan]);
5749 
5750 	if (unlikely(!data)) {
5751 		netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
5752 		return IRQ_NONE;
5753 	}
5754 
5755 	/* Check if adapter is up */
5756 	if (test_bit(STMMAC_DOWN, &priv->state))
5757 		return IRQ_HANDLED;
5758 
5759 	stmmac_napi_check(priv, chan, DMA_DIR_RX);
5760 
5761 	return IRQ_HANDLED;
5762 }
5763 
5764 #ifdef CONFIG_NET_POLL_CONTROLLER
5765 /* Polling receive - used by NETCONSOLE and other diagnostic tools
5766  * to allow network I/O with interrupts disabled.
5767  */
5768 static void stmmac_poll_controller(struct net_device *dev)
5769 {
5770 	struct stmmac_priv *priv = netdev_priv(dev);
5771 	int i;
5772 
5773 	/* If adapter is down, do nothing */
5774 	if (test_bit(STMMAC_DOWN, &priv->state))
5775 		return;
5776 
5777 	if (priv->plat->multi_msi_en) {
5778 		for (i = 0; i < priv->plat->rx_queues_to_use; i++)
5779 			stmmac_msi_intr_rx(0, &priv->rx_queue[i]);
5780 
5781 		for (i = 0; i < priv->plat->tx_queues_to_use; i++)
5782 			stmmac_msi_intr_tx(0, &priv->tx_queue[i]);
5783 	} else {
5784 		disable_irq(dev->irq);
5785 		stmmac_interrupt(dev->irq, dev);
5786 		enable_irq(dev->irq);
5787 	}
5788 }
5789 #endif
5790 
5791 /**
5792  *  stmmac_ioctl - Entry point for the Ioctl
5793  *  @dev: Device pointer.
5794  *  @rq: An IOCTL specefic structure, that can contain a pointer to
5795  *  a proprietary structure used to pass information to the driver.
5796  *  @cmd: IOCTL command
5797  *  Description:
5798  *  Currently it supports the phy_mii_ioctl(...) and HW time stamping.
5799  */
5800 static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
5801 {
5802 	struct stmmac_priv *priv = netdev_priv (dev);
5803 	int ret = -EOPNOTSUPP;
5804 
5805 	if (!netif_running(dev))
5806 		return -EINVAL;
5807 
5808 	switch (cmd) {
5809 	case SIOCGMIIPHY:
5810 	case SIOCGMIIREG:
5811 	case SIOCSMIIREG:
5812 		ret = phylink_mii_ioctl(priv->phylink, rq, cmd);
5813 		break;
5814 	case SIOCSHWTSTAMP:
5815 		ret = stmmac_hwtstamp_set(dev, rq);
5816 		break;
5817 	case SIOCGHWTSTAMP:
5818 		ret = stmmac_hwtstamp_get(dev, rq);
5819 		break;
5820 	default:
5821 		break;
5822 	}
5823 
5824 	return ret;
5825 }
5826 
5827 static int stmmac_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
5828 				    void *cb_priv)
5829 {
5830 	struct stmmac_priv *priv = cb_priv;
5831 	int ret = -EOPNOTSUPP;
5832 
5833 	if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
5834 		return ret;
5835 
5836 	__stmmac_disable_all_queues(priv);
5837 
5838 	switch (type) {
5839 	case TC_SETUP_CLSU32:
5840 		ret = stmmac_tc_setup_cls_u32(priv, priv, type_data);
5841 		break;
5842 	case TC_SETUP_CLSFLOWER:
5843 		ret = stmmac_tc_setup_cls(priv, priv, type_data);
5844 		break;
5845 	default:
5846 		break;
5847 	}
5848 
5849 	stmmac_enable_all_queues(priv);
5850 	return ret;
5851 }
5852 
5853 static LIST_HEAD(stmmac_block_cb_list);
5854 
5855 static int stmmac_setup_tc(struct net_device *ndev, enum tc_setup_type type,
5856 			   void *type_data)
5857 {
5858 	struct stmmac_priv *priv = netdev_priv(ndev);
5859 
5860 	switch (type) {
5861 	case TC_SETUP_BLOCK:
5862 		return flow_block_cb_setup_simple(type_data,
5863 						  &stmmac_block_cb_list,
5864 						  stmmac_setup_tc_block_cb,
5865 						  priv, priv, true);
5866 	case TC_SETUP_QDISC_CBS:
5867 		return stmmac_tc_setup_cbs(priv, priv, type_data);
5868 	case TC_SETUP_QDISC_TAPRIO:
5869 		return stmmac_tc_setup_taprio(priv, priv, type_data);
5870 	case TC_SETUP_QDISC_ETF:
5871 		return stmmac_tc_setup_etf(priv, priv, type_data);
5872 	default:
5873 		return -EOPNOTSUPP;
5874 	}
5875 }
5876 
5877 static u16 stmmac_select_queue(struct net_device *dev, struct sk_buff *skb,
5878 			       struct net_device *sb_dev)
5879 {
5880 	int gso = skb_shinfo(skb)->gso_type;
5881 
5882 	if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6 | SKB_GSO_UDP_L4)) {
5883 		/*
5884 		 * There is no way to determine the number of TSO/USO
5885 		 * capable Queues. Let's use always the Queue 0
5886 		 * because if TSO/USO is supported then at least this
5887 		 * one will be capable.
5888 		 */
5889 		return 0;
5890 	}
5891 
5892 	return netdev_pick_tx(dev, skb, NULL) % dev->real_num_tx_queues;
5893 }
5894 
5895 static int stmmac_set_mac_address(struct net_device *ndev, void *addr)
5896 {
5897 	struct stmmac_priv *priv = netdev_priv(ndev);
5898 	int ret = 0;
5899 
5900 	ret = eth_mac_addr(ndev, addr);
5901 	if (ret)
5902 		return ret;
5903 
5904 	stmmac_set_umac_addr(priv, priv->hw, ndev->dev_addr, 0);
5905 
5906 	return ret;
5907 }
5908 
5909 #ifdef CONFIG_DEBUG_FS
5910 static struct dentry *stmmac_fs_dir;
5911 
5912 static void sysfs_display_ring(void *head, int size, int extend_desc,
5913 			       struct seq_file *seq, dma_addr_t dma_phy_addr)
5914 {
5915 	int i;
5916 	struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
5917 	struct dma_desc *p = (struct dma_desc *)head;
5918 	dma_addr_t dma_addr;
5919 
5920 	for (i = 0; i < size; i++) {
5921 		if (extend_desc) {
5922 			dma_addr = dma_phy_addr + i * sizeof(*ep);
5923 			seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n",
5924 				   i, &dma_addr,
5925 				   le32_to_cpu(ep->basic.des0),
5926 				   le32_to_cpu(ep->basic.des1),
5927 				   le32_to_cpu(ep->basic.des2),
5928 				   le32_to_cpu(ep->basic.des3));
5929 			ep++;
5930 		} else {
5931 			dma_addr = dma_phy_addr + i * sizeof(*p);
5932 			seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n",
5933 				   i, &dma_addr,
5934 				   le32_to_cpu(p->des0), le32_to_cpu(p->des1),
5935 				   le32_to_cpu(p->des2), le32_to_cpu(p->des3));
5936 			p++;
5937 		}
5938 		seq_printf(seq, "\n");
5939 	}
5940 }
5941 
5942 static int stmmac_rings_status_show(struct seq_file *seq, void *v)
5943 {
5944 	struct net_device *dev = seq->private;
5945 	struct stmmac_priv *priv = netdev_priv(dev);
5946 	u32 rx_count = priv->plat->rx_queues_to_use;
5947 	u32 tx_count = priv->plat->tx_queues_to_use;
5948 	u32 queue;
5949 
5950 	if ((dev->flags & IFF_UP) == 0)
5951 		return 0;
5952 
5953 	for (queue = 0; queue < rx_count; queue++) {
5954 		struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
5955 
5956 		seq_printf(seq, "RX Queue %d:\n", queue);
5957 
5958 		if (priv->extend_desc) {
5959 			seq_printf(seq, "Extended descriptor ring:\n");
5960 			sysfs_display_ring((void *)rx_q->dma_erx,
5961 					   priv->dma_rx_size, 1, seq, rx_q->dma_rx_phy);
5962 		} else {
5963 			seq_printf(seq, "Descriptor ring:\n");
5964 			sysfs_display_ring((void *)rx_q->dma_rx,
5965 					   priv->dma_rx_size, 0, seq, rx_q->dma_rx_phy);
5966 		}
5967 	}
5968 
5969 	for (queue = 0; queue < tx_count; queue++) {
5970 		struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
5971 
5972 		seq_printf(seq, "TX Queue %d:\n", queue);
5973 
5974 		if (priv->extend_desc) {
5975 			seq_printf(seq, "Extended descriptor ring:\n");
5976 			sysfs_display_ring((void *)tx_q->dma_etx,
5977 					   priv->dma_tx_size, 1, seq, tx_q->dma_tx_phy);
5978 		} else if (!(tx_q->tbs & STMMAC_TBS_AVAIL)) {
5979 			seq_printf(seq, "Descriptor ring:\n");
5980 			sysfs_display_ring((void *)tx_q->dma_tx,
5981 					   priv->dma_tx_size, 0, seq, tx_q->dma_tx_phy);
5982 		}
5983 	}
5984 
5985 	return 0;
5986 }
5987 DEFINE_SHOW_ATTRIBUTE(stmmac_rings_status);
5988 
5989 static int stmmac_dma_cap_show(struct seq_file *seq, void *v)
5990 {
5991 	struct net_device *dev = seq->private;
5992 	struct stmmac_priv *priv = netdev_priv(dev);
5993 
5994 	if (!priv->hw_cap_support) {
5995 		seq_printf(seq, "DMA HW features not supported\n");
5996 		return 0;
5997 	}
5998 
5999 	seq_printf(seq, "==============================\n");
6000 	seq_printf(seq, "\tDMA HW features\n");
6001 	seq_printf(seq, "==============================\n");
6002 
6003 	seq_printf(seq, "\t10/100 Mbps: %s\n",
6004 		   (priv->dma_cap.mbps_10_100) ? "Y" : "N");
6005 	seq_printf(seq, "\t1000 Mbps: %s\n",
6006 		   (priv->dma_cap.mbps_1000) ? "Y" : "N");
6007 	seq_printf(seq, "\tHalf duplex: %s\n",
6008 		   (priv->dma_cap.half_duplex) ? "Y" : "N");
6009 	seq_printf(seq, "\tHash Filter: %s\n",
6010 		   (priv->dma_cap.hash_filter) ? "Y" : "N");
6011 	seq_printf(seq, "\tMultiple MAC address registers: %s\n",
6012 		   (priv->dma_cap.multi_addr) ? "Y" : "N");
6013 	seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfaces): %s\n",
6014 		   (priv->dma_cap.pcs) ? "Y" : "N");
6015 	seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
6016 		   (priv->dma_cap.sma_mdio) ? "Y" : "N");
6017 	seq_printf(seq, "\tPMT Remote wake up: %s\n",
6018 		   (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
6019 	seq_printf(seq, "\tPMT Magic Frame: %s\n",
6020 		   (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
6021 	seq_printf(seq, "\tRMON module: %s\n",
6022 		   (priv->dma_cap.rmon) ? "Y" : "N");
6023 	seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
6024 		   (priv->dma_cap.time_stamp) ? "Y" : "N");
6025 	seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp: %s\n",
6026 		   (priv->dma_cap.atime_stamp) ? "Y" : "N");
6027 	seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE): %s\n",
6028 		   (priv->dma_cap.eee) ? "Y" : "N");
6029 	seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
6030 	seq_printf(seq, "\tChecksum Offload in TX: %s\n",
6031 		   (priv->dma_cap.tx_coe) ? "Y" : "N");
6032 	if (priv->synopsys_id >= DWMAC_CORE_4_00) {
6033 		seq_printf(seq, "\tIP Checksum Offload in RX: %s\n",
6034 			   (priv->dma_cap.rx_coe) ? "Y" : "N");
6035 	} else {
6036 		seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
6037 			   (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
6038 		seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
6039 			   (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
6040 	}
6041 	seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
6042 		   (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
6043 	seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
6044 		   priv->dma_cap.number_rx_channel);
6045 	seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
6046 		   priv->dma_cap.number_tx_channel);
6047 	seq_printf(seq, "\tNumber of Additional RX queues: %d\n",
6048 		   priv->dma_cap.number_rx_queues);
6049 	seq_printf(seq, "\tNumber of Additional TX queues: %d\n",
6050 		   priv->dma_cap.number_tx_queues);
6051 	seq_printf(seq, "\tEnhanced descriptors: %s\n",
6052 		   (priv->dma_cap.enh_desc) ? "Y" : "N");
6053 	seq_printf(seq, "\tTX Fifo Size: %d\n", priv->dma_cap.tx_fifo_size);
6054 	seq_printf(seq, "\tRX Fifo Size: %d\n", priv->dma_cap.rx_fifo_size);
6055 	seq_printf(seq, "\tHash Table Size: %d\n", priv->dma_cap.hash_tb_sz);
6056 	seq_printf(seq, "\tTSO: %s\n", priv->dma_cap.tsoen ? "Y" : "N");
6057 	seq_printf(seq, "\tNumber of PPS Outputs: %d\n",
6058 		   priv->dma_cap.pps_out_num);
6059 	seq_printf(seq, "\tSafety Features: %s\n",
6060 		   priv->dma_cap.asp ? "Y" : "N");
6061 	seq_printf(seq, "\tFlexible RX Parser: %s\n",
6062 		   priv->dma_cap.frpsel ? "Y" : "N");
6063 	seq_printf(seq, "\tEnhanced Addressing: %d\n",
6064 		   priv->dma_cap.addr64);
6065 	seq_printf(seq, "\tReceive Side Scaling: %s\n",
6066 		   priv->dma_cap.rssen ? "Y" : "N");
6067 	seq_printf(seq, "\tVLAN Hash Filtering: %s\n",
6068 		   priv->dma_cap.vlhash ? "Y" : "N");
6069 	seq_printf(seq, "\tSplit Header: %s\n",
6070 		   priv->dma_cap.sphen ? "Y" : "N");
6071 	seq_printf(seq, "\tVLAN TX Insertion: %s\n",
6072 		   priv->dma_cap.vlins ? "Y" : "N");
6073 	seq_printf(seq, "\tDouble VLAN: %s\n",
6074 		   priv->dma_cap.dvlan ? "Y" : "N");
6075 	seq_printf(seq, "\tNumber of L3/L4 Filters: %d\n",
6076 		   priv->dma_cap.l3l4fnum);
6077 	seq_printf(seq, "\tARP Offloading: %s\n",
6078 		   priv->dma_cap.arpoffsel ? "Y" : "N");
6079 	seq_printf(seq, "\tEnhancements to Scheduled Traffic (EST): %s\n",
6080 		   priv->dma_cap.estsel ? "Y" : "N");
6081 	seq_printf(seq, "\tFrame Preemption (FPE): %s\n",
6082 		   priv->dma_cap.fpesel ? "Y" : "N");
6083 	seq_printf(seq, "\tTime-Based Scheduling (TBS): %s\n",
6084 		   priv->dma_cap.tbssel ? "Y" : "N");
6085 	return 0;
6086 }
6087 DEFINE_SHOW_ATTRIBUTE(stmmac_dma_cap);
6088 
6089 /* Use network device events to rename debugfs file entries.
6090  */
6091 static int stmmac_device_event(struct notifier_block *unused,
6092 			       unsigned long event, void *ptr)
6093 {
6094 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6095 	struct stmmac_priv *priv = netdev_priv(dev);
6096 
6097 	if (dev->netdev_ops != &stmmac_netdev_ops)
6098 		goto done;
6099 
6100 	switch (event) {
6101 	case NETDEV_CHANGENAME:
6102 		if (priv->dbgfs_dir)
6103 			priv->dbgfs_dir = debugfs_rename(stmmac_fs_dir,
6104 							 priv->dbgfs_dir,
6105 							 stmmac_fs_dir,
6106 							 dev->name);
6107 		break;
6108 	}
6109 done:
6110 	return NOTIFY_DONE;
6111 }
6112 
6113 static struct notifier_block stmmac_notifier = {
6114 	.notifier_call = stmmac_device_event,
6115 };
6116 
6117 static void stmmac_init_fs(struct net_device *dev)
6118 {
6119 	struct stmmac_priv *priv = netdev_priv(dev);
6120 
6121 	rtnl_lock();
6122 
6123 	/* Create per netdev entries */
6124 	priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
6125 
6126 	/* Entry to report DMA RX/TX rings */
6127 	debugfs_create_file("descriptors_status", 0444, priv->dbgfs_dir, dev,
6128 			    &stmmac_rings_status_fops);
6129 
6130 	/* Entry to report the DMA HW features */
6131 	debugfs_create_file("dma_cap", 0444, priv->dbgfs_dir, dev,
6132 			    &stmmac_dma_cap_fops);
6133 
6134 	rtnl_unlock();
6135 }
6136 
6137 static void stmmac_exit_fs(struct net_device *dev)
6138 {
6139 	struct stmmac_priv *priv = netdev_priv(dev);
6140 
6141 	debugfs_remove_recursive(priv->dbgfs_dir);
6142 }
6143 #endif /* CONFIG_DEBUG_FS */
6144 
6145 static u32 stmmac_vid_crc32_le(__le16 vid_le)
6146 {
6147 	unsigned char *data = (unsigned char *)&vid_le;
6148 	unsigned char data_byte = 0;
6149 	u32 crc = ~0x0;
6150 	u32 temp = 0;
6151 	int i, bits;
6152 
6153 	bits = get_bitmask_order(VLAN_VID_MASK);
6154 	for (i = 0; i < bits; i++) {
6155 		if ((i % 8) == 0)
6156 			data_byte = data[i / 8];
6157 
6158 		temp = ((crc & 1) ^ data_byte) & 1;
6159 		crc >>= 1;
6160 		data_byte >>= 1;
6161 
6162 		if (temp)
6163 			crc ^= 0xedb88320;
6164 	}
6165 
6166 	return crc;
6167 }
6168 
6169 static int stmmac_vlan_update(struct stmmac_priv *priv, bool is_double)
6170 {
6171 	u32 crc, hash = 0;
6172 	__le16 pmatch = 0;
6173 	int count = 0;
6174 	u16 vid = 0;
6175 
6176 	for_each_set_bit(vid, priv->active_vlans, VLAN_N_VID) {
6177 		__le16 vid_le = cpu_to_le16(vid);
6178 		crc = bitrev32(~stmmac_vid_crc32_le(vid_le)) >> 28;
6179 		hash |= (1 << crc);
6180 		count++;
6181 	}
6182 
6183 	if (!priv->dma_cap.vlhash) {
6184 		if (count > 2) /* VID = 0 always passes filter */
6185 			return -EOPNOTSUPP;
6186 
6187 		pmatch = cpu_to_le16(vid);
6188 		hash = 0;
6189 	}
6190 
6191 	return stmmac_update_vlan_hash(priv, priv->hw, hash, pmatch, is_double);
6192 }
6193 
6194 static int stmmac_vlan_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid)
6195 {
6196 	struct stmmac_priv *priv = netdev_priv(ndev);
6197 	bool is_double = false;
6198 	int ret;
6199 
6200 	ret = pm_runtime_get_sync(priv->device);
6201 	if (ret < 0) {
6202 		pm_runtime_put_noidle(priv->device);
6203 		return ret;
6204 	}
6205 
6206 	if (be16_to_cpu(proto) == ETH_P_8021AD)
6207 		is_double = true;
6208 
6209 	set_bit(vid, priv->active_vlans);
6210 	ret = stmmac_vlan_update(priv, is_double);
6211 	if (ret) {
6212 		clear_bit(vid, priv->active_vlans);
6213 		return ret;
6214 	}
6215 
6216 	if (priv->hw->num_vlan) {
6217 		ret = stmmac_add_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid);
6218 		if (ret)
6219 			return ret;
6220 	}
6221 
6222 	return 0;
6223 }
6224 
6225 static int stmmac_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid)
6226 {
6227 	struct stmmac_priv *priv = netdev_priv(ndev);
6228 	bool is_double = false;
6229 	int ret;
6230 
6231 	if (be16_to_cpu(proto) == ETH_P_8021AD)
6232 		is_double = true;
6233 
6234 	clear_bit(vid, priv->active_vlans);
6235 
6236 	if (priv->hw->num_vlan) {
6237 		ret = stmmac_del_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid);
6238 		if (ret)
6239 			goto del_vlan_error;
6240 	}
6241 
6242 	ret = stmmac_vlan_update(priv, is_double);
6243 
6244 del_vlan_error:
6245 	pm_runtime_put(priv->device);
6246 
6247 	return ret;
6248 }
6249 
6250 static int stmmac_bpf(struct net_device *dev, struct netdev_bpf *bpf)
6251 {
6252 	struct stmmac_priv *priv = netdev_priv(dev);
6253 
6254 	switch (bpf->command) {
6255 	case XDP_SETUP_PROG:
6256 		return stmmac_xdp_set_prog(priv, bpf->prog, bpf->extack);
6257 	case XDP_SETUP_XSK_POOL:
6258 		return stmmac_xdp_setup_pool(priv, bpf->xsk.pool,
6259 					     bpf->xsk.queue_id);
6260 	default:
6261 		return -EOPNOTSUPP;
6262 	}
6263 }
6264 
6265 static int stmmac_xdp_xmit(struct net_device *dev, int num_frames,
6266 			   struct xdp_frame **frames, u32 flags)
6267 {
6268 	struct stmmac_priv *priv = netdev_priv(dev);
6269 	int cpu = smp_processor_id();
6270 	struct netdev_queue *nq;
6271 	int i, nxmit = 0;
6272 	int queue;
6273 
6274 	if (unlikely(test_bit(STMMAC_DOWN, &priv->state)))
6275 		return -ENETDOWN;
6276 
6277 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
6278 		return -EINVAL;
6279 
6280 	queue = stmmac_xdp_get_tx_queue(priv, cpu);
6281 	nq = netdev_get_tx_queue(priv->dev, queue);
6282 
6283 	__netif_tx_lock(nq, cpu);
6284 	/* Avoids TX time-out as we are sharing with slow path */
6285 	nq->trans_start = jiffies;
6286 
6287 	for (i = 0; i < num_frames; i++) {
6288 		int res;
6289 
6290 		res = stmmac_xdp_xmit_xdpf(priv, queue, frames[i], true);
6291 		if (res == STMMAC_XDP_CONSUMED)
6292 			break;
6293 
6294 		nxmit++;
6295 	}
6296 
6297 	if (flags & XDP_XMIT_FLUSH) {
6298 		stmmac_flush_tx_descriptors(priv, queue);
6299 		stmmac_tx_timer_arm(priv, queue);
6300 	}
6301 
6302 	__netif_tx_unlock(nq);
6303 
6304 	return nxmit;
6305 }
6306 
6307 void stmmac_disable_rx_queue(struct stmmac_priv *priv, u32 queue)
6308 {
6309 	struct stmmac_channel *ch = &priv->channel[queue];
6310 	unsigned long flags;
6311 
6312 	spin_lock_irqsave(&ch->lock, flags);
6313 	stmmac_disable_dma_irq(priv, priv->ioaddr, queue, 1, 0);
6314 	spin_unlock_irqrestore(&ch->lock, flags);
6315 
6316 	stmmac_stop_rx_dma(priv, queue);
6317 	__free_dma_rx_desc_resources(priv, queue);
6318 }
6319 
6320 void stmmac_enable_rx_queue(struct stmmac_priv *priv, u32 queue)
6321 {
6322 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
6323 	struct stmmac_channel *ch = &priv->channel[queue];
6324 	unsigned long flags;
6325 	u32 buf_size;
6326 	int ret;
6327 
6328 	ret = __alloc_dma_rx_desc_resources(priv, queue);
6329 	if (ret) {
6330 		netdev_err(priv->dev, "Failed to alloc RX desc.\n");
6331 		return;
6332 	}
6333 
6334 	ret = __init_dma_rx_desc_rings(priv, queue, GFP_KERNEL);
6335 	if (ret) {
6336 		__free_dma_rx_desc_resources(priv, queue);
6337 		netdev_err(priv->dev, "Failed to init RX desc.\n");
6338 		return;
6339 	}
6340 
6341 	stmmac_clear_rx_descriptors(priv, queue);
6342 
6343 	stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6344 			    rx_q->dma_rx_phy, rx_q->queue_index);
6345 
6346 	rx_q->rx_tail_addr = rx_q->dma_rx_phy + (rx_q->buf_alloc_num *
6347 			     sizeof(struct dma_desc));
6348 	stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
6349 			       rx_q->rx_tail_addr, rx_q->queue_index);
6350 
6351 	if (rx_q->xsk_pool && rx_q->buf_alloc_num) {
6352 		buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool);
6353 		stmmac_set_dma_bfsize(priv, priv->ioaddr,
6354 				      buf_size,
6355 				      rx_q->queue_index);
6356 	} else {
6357 		stmmac_set_dma_bfsize(priv, priv->ioaddr,
6358 				      priv->dma_buf_sz,
6359 				      rx_q->queue_index);
6360 	}
6361 
6362 	stmmac_start_rx_dma(priv, queue);
6363 
6364 	spin_lock_irqsave(&ch->lock, flags);
6365 	stmmac_enable_dma_irq(priv, priv->ioaddr, queue, 1, 0);
6366 	spin_unlock_irqrestore(&ch->lock, flags);
6367 }
6368 
6369 void stmmac_disable_tx_queue(struct stmmac_priv *priv, u32 queue)
6370 {
6371 	struct stmmac_channel *ch = &priv->channel[queue];
6372 	unsigned long flags;
6373 
6374 	spin_lock_irqsave(&ch->lock, flags);
6375 	stmmac_disable_dma_irq(priv, priv->ioaddr, queue, 0, 1);
6376 	spin_unlock_irqrestore(&ch->lock, flags);
6377 
6378 	stmmac_stop_tx_dma(priv, queue);
6379 	__free_dma_tx_desc_resources(priv, queue);
6380 }
6381 
6382 void stmmac_enable_tx_queue(struct stmmac_priv *priv, u32 queue)
6383 {
6384 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
6385 	struct stmmac_channel *ch = &priv->channel[queue];
6386 	unsigned long flags;
6387 	int ret;
6388 
6389 	ret = __alloc_dma_tx_desc_resources(priv, queue);
6390 	if (ret) {
6391 		netdev_err(priv->dev, "Failed to alloc TX desc.\n");
6392 		return;
6393 	}
6394 
6395 	ret = __init_dma_tx_desc_rings(priv, queue);
6396 	if (ret) {
6397 		__free_dma_tx_desc_resources(priv, queue);
6398 		netdev_err(priv->dev, "Failed to init TX desc.\n");
6399 		return;
6400 	}
6401 
6402 	stmmac_clear_tx_descriptors(priv, queue);
6403 
6404 	stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
6405 			    tx_q->dma_tx_phy, tx_q->queue_index);
6406 
6407 	if (tx_q->tbs & STMMAC_TBS_AVAIL)
6408 		stmmac_enable_tbs(priv, priv->ioaddr, 1, tx_q->queue_index);
6409 
6410 	tx_q->tx_tail_addr = tx_q->dma_tx_phy;
6411 	stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
6412 			       tx_q->tx_tail_addr, tx_q->queue_index);
6413 
6414 	stmmac_start_tx_dma(priv, queue);
6415 
6416 	spin_lock_irqsave(&ch->lock, flags);
6417 	stmmac_enable_dma_irq(priv, priv->ioaddr, queue, 0, 1);
6418 	spin_unlock_irqrestore(&ch->lock, flags);
6419 }
6420 
6421 int stmmac_xsk_wakeup(struct net_device *dev, u32 queue, u32 flags)
6422 {
6423 	struct stmmac_priv *priv = netdev_priv(dev);
6424 	struct stmmac_rx_queue *rx_q;
6425 	struct stmmac_tx_queue *tx_q;
6426 	struct stmmac_channel *ch;
6427 
6428 	if (test_bit(STMMAC_DOWN, &priv->state) ||
6429 	    !netif_carrier_ok(priv->dev))
6430 		return -ENETDOWN;
6431 
6432 	if (!stmmac_xdp_is_enabled(priv))
6433 		return -ENXIO;
6434 
6435 	if (queue >= priv->plat->rx_queues_to_use ||
6436 	    queue >= priv->plat->tx_queues_to_use)
6437 		return -EINVAL;
6438 
6439 	rx_q = &priv->rx_queue[queue];
6440 	tx_q = &priv->tx_queue[queue];
6441 	ch = &priv->channel[queue];
6442 
6443 	if (!rx_q->xsk_pool && !tx_q->xsk_pool)
6444 		return -ENXIO;
6445 
6446 	if (!napi_if_scheduled_mark_missed(&ch->rxtx_napi)) {
6447 		/* EQoS does not have per-DMA channel SW interrupt,
6448 		 * so we schedule RX Napi straight-away.
6449 		 */
6450 		if (likely(napi_schedule_prep(&ch->rxtx_napi)))
6451 			__napi_schedule(&ch->rxtx_napi);
6452 	}
6453 
6454 	return 0;
6455 }
6456 
6457 static const struct net_device_ops stmmac_netdev_ops = {
6458 	.ndo_open = stmmac_open,
6459 	.ndo_start_xmit = stmmac_xmit,
6460 	.ndo_stop = stmmac_release,
6461 	.ndo_change_mtu = stmmac_change_mtu,
6462 	.ndo_fix_features = stmmac_fix_features,
6463 	.ndo_set_features = stmmac_set_features,
6464 	.ndo_set_rx_mode = stmmac_set_rx_mode,
6465 	.ndo_tx_timeout = stmmac_tx_timeout,
6466 	.ndo_do_ioctl = stmmac_ioctl,
6467 	.ndo_setup_tc = stmmac_setup_tc,
6468 	.ndo_select_queue = stmmac_select_queue,
6469 #ifdef CONFIG_NET_POLL_CONTROLLER
6470 	.ndo_poll_controller = stmmac_poll_controller,
6471 #endif
6472 	.ndo_set_mac_address = stmmac_set_mac_address,
6473 	.ndo_vlan_rx_add_vid = stmmac_vlan_rx_add_vid,
6474 	.ndo_vlan_rx_kill_vid = stmmac_vlan_rx_kill_vid,
6475 	.ndo_bpf = stmmac_bpf,
6476 	.ndo_xdp_xmit = stmmac_xdp_xmit,
6477 	.ndo_xsk_wakeup = stmmac_xsk_wakeup,
6478 };
6479 
6480 static void stmmac_reset_subtask(struct stmmac_priv *priv)
6481 {
6482 	if (!test_and_clear_bit(STMMAC_RESET_REQUESTED, &priv->state))
6483 		return;
6484 	if (test_bit(STMMAC_DOWN, &priv->state))
6485 		return;
6486 
6487 	netdev_err(priv->dev, "Reset adapter.\n");
6488 
6489 	rtnl_lock();
6490 	netif_trans_update(priv->dev);
6491 	while (test_and_set_bit(STMMAC_RESETING, &priv->state))
6492 		usleep_range(1000, 2000);
6493 
6494 	set_bit(STMMAC_DOWN, &priv->state);
6495 	dev_close(priv->dev);
6496 	dev_open(priv->dev, NULL);
6497 	clear_bit(STMMAC_DOWN, &priv->state);
6498 	clear_bit(STMMAC_RESETING, &priv->state);
6499 	rtnl_unlock();
6500 }
6501 
6502 static void stmmac_service_task(struct work_struct *work)
6503 {
6504 	struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
6505 			service_task);
6506 
6507 	stmmac_reset_subtask(priv);
6508 	clear_bit(STMMAC_SERVICE_SCHED, &priv->state);
6509 }
6510 
6511 /**
6512  *  stmmac_hw_init - Init the MAC device
6513  *  @priv: driver private structure
6514  *  Description: this function is to configure the MAC device according to
6515  *  some platform parameters or the HW capability register. It prepares the
6516  *  driver to use either ring or chain modes and to setup either enhanced or
6517  *  normal descriptors.
6518  */
6519 static int stmmac_hw_init(struct stmmac_priv *priv)
6520 {
6521 	int ret;
6522 
6523 	/* dwmac-sun8i only work in chain mode */
6524 	if (priv->plat->has_sun8i)
6525 		chain_mode = 1;
6526 	priv->chain_mode = chain_mode;
6527 
6528 	/* Initialize HW Interface */
6529 	ret = stmmac_hwif_init(priv);
6530 	if (ret)
6531 		return ret;
6532 
6533 	/* Get the HW capability (new GMAC newer than 3.50a) */
6534 	priv->hw_cap_support = stmmac_get_hw_features(priv);
6535 	if (priv->hw_cap_support) {
6536 		dev_info(priv->device, "DMA HW capability register supported\n");
6537 
6538 		/* We can override some gmac/dma configuration fields: e.g.
6539 		 * enh_desc, tx_coe (e.g. that are passed through the
6540 		 * platform) with the values from the HW capability
6541 		 * register (if supported).
6542 		 */
6543 		priv->plat->enh_desc = priv->dma_cap.enh_desc;
6544 		priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
6545 		priv->hw->pmt = priv->plat->pmt;
6546 		if (priv->dma_cap.hash_tb_sz) {
6547 			priv->hw->multicast_filter_bins =
6548 					(BIT(priv->dma_cap.hash_tb_sz) << 5);
6549 			priv->hw->mcast_bits_log2 =
6550 					ilog2(priv->hw->multicast_filter_bins);
6551 		}
6552 
6553 		/* TXCOE doesn't work in thresh DMA mode */
6554 		if (priv->plat->force_thresh_dma_mode)
6555 			priv->plat->tx_coe = 0;
6556 		else
6557 			priv->plat->tx_coe = priv->dma_cap.tx_coe;
6558 
6559 		/* In case of GMAC4 rx_coe is from HW cap register. */
6560 		priv->plat->rx_coe = priv->dma_cap.rx_coe;
6561 
6562 		if (priv->dma_cap.rx_coe_type2)
6563 			priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
6564 		else if (priv->dma_cap.rx_coe_type1)
6565 			priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
6566 
6567 	} else {
6568 		dev_info(priv->device, "No HW DMA feature register supported\n");
6569 	}
6570 
6571 	if (priv->plat->rx_coe) {
6572 		priv->hw->rx_csum = priv->plat->rx_coe;
6573 		dev_info(priv->device, "RX Checksum Offload Engine supported\n");
6574 		if (priv->synopsys_id < DWMAC_CORE_4_00)
6575 			dev_info(priv->device, "COE Type %d\n", priv->hw->rx_csum);
6576 	}
6577 	if (priv->plat->tx_coe)
6578 		dev_info(priv->device, "TX Checksum insertion supported\n");
6579 
6580 	if (priv->plat->pmt) {
6581 		dev_info(priv->device, "Wake-Up On Lan supported\n");
6582 		device_set_wakeup_capable(priv->device, 1);
6583 	}
6584 
6585 	if (priv->dma_cap.tsoen)
6586 		dev_info(priv->device, "TSO supported\n");
6587 
6588 	priv->hw->vlan_fail_q_en = priv->plat->vlan_fail_q_en;
6589 	priv->hw->vlan_fail_q = priv->plat->vlan_fail_q;
6590 
6591 	/* Run HW quirks, if any */
6592 	if (priv->hwif_quirks) {
6593 		ret = priv->hwif_quirks(priv);
6594 		if (ret)
6595 			return ret;
6596 	}
6597 
6598 	/* Rx Watchdog is available in the COREs newer than the 3.40.
6599 	 * In some case, for example on bugged HW this feature
6600 	 * has to be disable and this can be done by passing the
6601 	 * riwt_off field from the platform.
6602 	 */
6603 	if (((priv->synopsys_id >= DWMAC_CORE_3_50) ||
6604 	    (priv->plat->has_xgmac)) && (!priv->plat->riwt_off)) {
6605 		priv->use_riwt = 1;
6606 		dev_info(priv->device,
6607 			 "Enable RX Mitigation via HW Watchdog Timer\n");
6608 	}
6609 
6610 	return 0;
6611 }
6612 
6613 static void stmmac_napi_add(struct net_device *dev)
6614 {
6615 	struct stmmac_priv *priv = netdev_priv(dev);
6616 	u32 queue, maxq;
6617 
6618 	maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
6619 
6620 	for (queue = 0; queue < maxq; queue++) {
6621 		struct stmmac_channel *ch = &priv->channel[queue];
6622 
6623 		ch->priv_data = priv;
6624 		ch->index = queue;
6625 		spin_lock_init(&ch->lock);
6626 
6627 		if (queue < priv->plat->rx_queues_to_use) {
6628 			netif_napi_add(dev, &ch->rx_napi, stmmac_napi_poll_rx,
6629 				       NAPI_POLL_WEIGHT);
6630 		}
6631 		if (queue < priv->plat->tx_queues_to_use) {
6632 			netif_tx_napi_add(dev, &ch->tx_napi,
6633 					  stmmac_napi_poll_tx,
6634 					  NAPI_POLL_WEIGHT);
6635 		}
6636 		if (queue < priv->plat->rx_queues_to_use &&
6637 		    queue < priv->plat->tx_queues_to_use) {
6638 			netif_napi_add(dev, &ch->rxtx_napi,
6639 				       stmmac_napi_poll_rxtx,
6640 				       NAPI_POLL_WEIGHT);
6641 		}
6642 	}
6643 }
6644 
6645 static void stmmac_napi_del(struct net_device *dev)
6646 {
6647 	struct stmmac_priv *priv = netdev_priv(dev);
6648 	u32 queue, maxq;
6649 
6650 	maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
6651 
6652 	for (queue = 0; queue < maxq; queue++) {
6653 		struct stmmac_channel *ch = &priv->channel[queue];
6654 
6655 		if (queue < priv->plat->rx_queues_to_use)
6656 			netif_napi_del(&ch->rx_napi);
6657 		if (queue < priv->plat->tx_queues_to_use)
6658 			netif_napi_del(&ch->tx_napi);
6659 		if (queue < priv->plat->rx_queues_to_use &&
6660 		    queue < priv->plat->tx_queues_to_use) {
6661 			netif_napi_del(&ch->rxtx_napi);
6662 		}
6663 	}
6664 }
6665 
6666 int stmmac_reinit_queues(struct net_device *dev, u32 rx_cnt, u32 tx_cnt)
6667 {
6668 	struct stmmac_priv *priv = netdev_priv(dev);
6669 	int ret = 0;
6670 
6671 	if (netif_running(dev))
6672 		stmmac_release(dev);
6673 
6674 	stmmac_napi_del(dev);
6675 
6676 	priv->plat->rx_queues_to_use = rx_cnt;
6677 	priv->plat->tx_queues_to_use = tx_cnt;
6678 
6679 	stmmac_napi_add(dev);
6680 
6681 	if (netif_running(dev))
6682 		ret = stmmac_open(dev);
6683 
6684 	return ret;
6685 }
6686 
6687 int stmmac_reinit_ringparam(struct net_device *dev, u32 rx_size, u32 tx_size)
6688 {
6689 	struct stmmac_priv *priv = netdev_priv(dev);
6690 	int ret = 0;
6691 
6692 	if (netif_running(dev))
6693 		stmmac_release(dev);
6694 
6695 	priv->dma_rx_size = rx_size;
6696 	priv->dma_tx_size = tx_size;
6697 
6698 	if (netif_running(dev))
6699 		ret = stmmac_open(dev);
6700 
6701 	return ret;
6702 }
6703 
6704 #define SEND_VERIFY_MPAKCET_FMT "Send Verify mPacket lo_state=%d lp_state=%d\n"
6705 static void stmmac_fpe_lp_task(struct work_struct *work)
6706 {
6707 	struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
6708 						fpe_task);
6709 	struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg;
6710 	enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state;
6711 	enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state;
6712 	bool *hs_enable = &fpe_cfg->hs_enable;
6713 	bool *enable = &fpe_cfg->enable;
6714 	int retries = 20;
6715 
6716 	while (retries-- > 0) {
6717 		/* Bail out immediately if FPE handshake is OFF */
6718 		if (*lo_state == FPE_STATE_OFF || !*hs_enable)
6719 			break;
6720 
6721 		if (*lo_state == FPE_STATE_ENTERING_ON &&
6722 		    *lp_state == FPE_STATE_ENTERING_ON) {
6723 			stmmac_fpe_configure(priv, priv->ioaddr,
6724 					     priv->plat->tx_queues_to_use,
6725 					     priv->plat->rx_queues_to_use,
6726 					     *enable);
6727 
6728 			netdev_info(priv->dev, "configured FPE\n");
6729 
6730 			*lo_state = FPE_STATE_ON;
6731 			*lp_state = FPE_STATE_ON;
6732 			netdev_info(priv->dev, "!!! BOTH FPE stations ON\n");
6733 			break;
6734 		}
6735 
6736 		if ((*lo_state == FPE_STATE_CAPABLE ||
6737 		     *lo_state == FPE_STATE_ENTERING_ON) &&
6738 		     *lp_state != FPE_STATE_ON) {
6739 			netdev_info(priv->dev, SEND_VERIFY_MPAKCET_FMT,
6740 				    *lo_state, *lp_state);
6741 			stmmac_fpe_send_mpacket(priv, priv->ioaddr,
6742 						MPACKET_VERIFY);
6743 		}
6744 		/* Sleep then retry */
6745 		msleep(500);
6746 	}
6747 
6748 	clear_bit(__FPE_TASK_SCHED, &priv->fpe_task_state);
6749 }
6750 
6751 void stmmac_fpe_handshake(struct stmmac_priv *priv, bool enable)
6752 {
6753 	if (priv->plat->fpe_cfg->hs_enable != enable) {
6754 		if (enable) {
6755 			stmmac_fpe_send_mpacket(priv, priv->ioaddr,
6756 						MPACKET_VERIFY);
6757 		} else {
6758 			priv->plat->fpe_cfg->lo_fpe_state = FPE_STATE_OFF;
6759 			priv->plat->fpe_cfg->lp_fpe_state = FPE_STATE_OFF;
6760 		}
6761 
6762 		priv->plat->fpe_cfg->hs_enable = enable;
6763 	}
6764 }
6765 
6766 /**
6767  * stmmac_dvr_probe
6768  * @device: device pointer
6769  * @plat_dat: platform data pointer
6770  * @res: stmmac resource pointer
6771  * Description: this is the main probe function used to
6772  * call the alloc_etherdev, allocate the priv structure.
6773  * Return:
6774  * returns 0 on success, otherwise errno.
6775  */
6776 int stmmac_dvr_probe(struct device *device,
6777 		     struct plat_stmmacenet_data *plat_dat,
6778 		     struct stmmac_resources *res)
6779 {
6780 	struct net_device *ndev = NULL;
6781 	struct stmmac_priv *priv;
6782 	u32 rxq;
6783 	int i, ret = 0;
6784 
6785 	ndev = devm_alloc_etherdev_mqs(device, sizeof(struct stmmac_priv),
6786 				       MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES);
6787 	if (!ndev)
6788 		return -ENOMEM;
6789 
6790 	SET_NETDEV_DEV(ndev, device);
6791 
6792 	priv = netdev_priv(ndev);
6793 	priv->device = device;
6794 	priv->dev = ndev;
6795 
6796 	stmmac_set_ethtool_ops(ndev);
6797 	priv->pause = pause;
6798 	priv->plat = plat_dat;
6799 	priv->ioaddr = res->addr;
6800 	priv->dev->base_addr = (unsigned long)res->addr;
6801 	priv->plat->dma_cfg->multi_msi_en = priv->plat->multi_msi_en;
6802 
6803 	priv->dev->irq = res->irq;
6804 	priv->wol_irq = res->wol_irq;
6805 	priv->lpi_irq = res->lpi_irq;
6806 	priv->sfty_ce_irq = res->sfty_ce_irq;
6807 	priv->sfty_ue_irq = res->sfty_ue_irq;
6808 	for (i = 0; i < MTL_MAX_RX_QUEUES; i++)
6809 		priv->rx_irq[i] = res->rx_irq[i];
6810 	for (i = 0; i < MTL_MAX_TX_QUEUES; i++)
6811 		priv->tx_irq[i] = res->tx_irq[i];
6812 
6813 	if (!is_zero_ether_addr(res->mac))
6814 		memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
6815 
6816 	dev_set_drvdata(device, priv->dev);
6817 
6818 	/* Verify driver arguments */
6819 	stmmac_verify_args();
6820 
6821 	priv->af_xdp_zc_qps = bitmap_zalloc(MTL_MAX_TX_QUEUES, GFP_KERNEL);
6822 	if (!priv->af_xdp_zc_qps)
6823 		return -ENOMEM;
6824 
6825 	/* Allocate workqueue */
6826 	priv->wq = create_singlethread_workqueue("stmmac_wq");
6827 	if (!priv->wq) {
6828 		dev_err(priv->device, "failed to create workqueue\n");
6829 		return -ENOMEM;
6830 	}
6831 
6832 	INIT_WORK(&priv->service_task, stmmac_service_task);
6833 
6834 	/* Initialize Link Partner FPE workqueue */
6835 	INIT_WORK(&priv->fpe_task, stmmac_fpe_lp_task);
6836 
6837 	/* Override with kernel parameters if supplied XXX CRS XXX
6838 	 * this needs to have multiple instances
6839 	 */
6840 	if ((phyaddr >= 0) && (phyaddr <= 31))
6841 		priv->plat->phy_addr = phyaddr;
6842 
6843 	if (priv->plat->stmmac_rst) {
6844 		ret = reset_control_assert(priv->plat->stmmac_rst);
6845 		reset_control_deassert(priv->plat->stmmac_rst);
6846 		/* Some reset controllers have only reset callback instead of
6847 		 * assert + deassert callbacks pair.
6848 		 */
6849 		if (ret == -ENOTSUPP)
6850 			reset_control_reset(priv->plat->stmmac_rst);
6851 	}
6852 
6853 	/* Init MAC and get the capabilities */
6854 	ret = stmmac_hw_init(priv);
6855 	if (ret)
6856 		goto error_hw_init;
6857 
6858 	/* Only DWMAC core version 5.20 onwards supports HW descriptor prefetch.
6859 	 */
6860 	if (priv->synopsys_id < DWMAC_CORE_5_20)
6861 		priv->plat->dma_cfg->dche = false;
6862 
6863 	stmmac_check_ether_addr(priv);
6864 
6865 	ndev->netdev_ops = &stmmac_netdev_ops;
6866 
6867 	ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
6868 			    NETIF_F_RXCSUM;
6869 
6870 	ret = stmmac_tc_init(priv, priv);
6871 	if (!ret) {
6872 		ndev->hw_features |= NETIF_F_HW_TC;
6873 	}
6874 
6875 	if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
6876 		ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
6877 		if (priv->plat->has_gmac4)
6878 			ndev->hw_features |= NETIF_F_GSO_UDP_L4;
6879 		priv->tso = true;
6880 		dev_info(priv->device, "TSO feature enabled\n");
6881 	}
6882 
6883 	if (priv->dma_cap.sphen) {
6884 		ndev->hw_features |= NETIF_F_GRO;
6885 		priv->sph_cap = true;
6886 		priv->sph = priv->sph_cap;
6887 		dev_info(priv->device, "SPH feature enabled\n");
6888 	}
6889 
6890 	/* The current IP register MAC_HW_Feature1[ADDR64] only define
6891 	 * 32/40/64 bit width, but some SOC support others like i.MX8MP
6892 	 * support 34 bits but it map to 40 bits width in MAC_HW_Feature1[ADDR64].
6893 	 * So overwrite dma_cap.addr64 according to HW real design.
6894 	 */
6895 	if (priv->plat->addr64)
6896 		priv->dma_cap.addr64 = priv->plat->addr64;
6897 
6898 	if (priv->dma_cap.addr64) {
6899 		ret = dma_set_mask_and_coherent(device,
6900 				DMA_BIT_MASK(priv->dma_cap.addr64));
6901 		if (!ret) {
6902 			dev_info(priv->device, "Using %d bits DMA width\n",
6903 				 priv->dma_cap.addr64);
6904 
6905 			/*
6906 			 * If more than 32 bits can be addressed, make sure to
6907 			 * enable enhanced addressing mode.
6908 			 */
6909 			if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
6910 				priv->plat->dma_cfg->eame = true;
6911 		} else {
6912 			ret = dma_set_mask_and_coherent(device, DMA_BIT_MASK(32));
6913 			if (ret) {
6914 				dev_err(priv->device, "Failed to set DMA Mask\n");
6915 				goto error_hw_init;
6916 			}
6917 
6918 			priv->dma_cap.addr64 = 32;
6919 		}
6920 	}
6921 
6922 	ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
6923 	ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
6924 #ifdef STMMAC_VLAN_TAG_USED
6925 	/* Both mac100 and gmac support receive VLAN tag detection */
6926 	ndev->features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX;
6927 	if (priv->dma_cap.vlhash) {
6928 		ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
6929 		ndev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
6930 	}
6931 	if (priv->dma_cap.vlins) {
6932 		ndev->features |= NETIF_F_HW_VLAN_CTAG_TX;
6933 		if (priv->dma_cap.dvlan)
6934 			ndev->features |= NETIF_F_HW_VLAN_STAG_TX;
6935 	}
6936 #endif
6937 	priv->msg_enable = netif_msg_init(debug, default_msg_level);
6938 
6939 	/* Initialize RSS */
6940 	rxq = priv->plat->rx_queues_to_use;
6941 	netdev_rss_key_fill(priv->rss.key, sizeof(priv->rss.key));
6942 	for (i = 0; i < ARRAY_SIZE(priv->rss.table); i++)
6943 		priv->rss.table[i] = ethtool_rxfh_indir_default(i, rxq);
6944 
6945 	if (priv->dma_cap.rssen && priv->plat->rss_en)
6946 		ndev->features |= NETIF_F_RXHASH;
6947 
6948 	/* MTU range: 46 - hw-specific max */
6949 	ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
6950 	if (priv->plat->has_xgmac)
6951 		ndev->max_mtu = XGMAC_JUMBO_LEN;
6952 	else if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
6953 		ndev->max_mtu = JUMBO_LEN;
6954 	else
6955 		ndev->max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
6956 	/* Will not overwrite ndev->max_mtu if plat->maxmtu > ndev->max_mtu
6957 	 * as well as plat->maxmtu < ndev->min_mtu which is a invalid range.
6958 	 */
6959 	if ((priv->plat->maxmtu < ndev->max_mtu) &&
6960 	    (priv->plat->maxmtu >= ndev->min_mtu))
6961 		ndev->max_mtu = priv->plat->maxmtu;
6962 	else if (priv->plat->maxmtu < ndev->min_mtu)
6963 		dev_warn(priv->device,
6964 			 "%s: warning: maxmtu having invalid value (%d)\n",
6965 			 __func__, priv->plat->maxmtu);
6966 
6967 	if (flow_ctrl)
6968 		priv->flow_ctrl = FLOW_AUTO;	/* RX/TX pause on */
6969 
6970 	/* Setup channels NAPI */
6971 	stmmac_napi_add(ndev);
6972 
6973 	mutex_init(&priv->lock);
6974 
6975 	/* If a specific clk_csr value is passed from the platform
6976 	 * this means that the CSR Clock Range selection cannot be
6977 	 * changed at run-time and it is fixed. Viceversa the driver'll try to
6978 	 * set the MDC clock dynamically according to the csr actual
6979 	 * clock input.
6980 	 */
6981 	if (priv->plat->clk_csr >= 0)
6982 		priv->clk_csr = priv->plat->clk_csr;
6983 	else
6984 		stmmac_clk_csr_set(priv);
6985 
6986 	stmmac_check_pcs_mode(priv);
6987 
6988 	pm_runtime_get_noresume(device);
6989 	pm_runtime_set_active(device);
6990 	pm_runtime_enable(device);
6991 
6992 	if (priv->hw->pcs != STMMAC_PCS_TBI &&
6993 	    priv->hw->pcs != STMMAC_PCS_RTBI) {
6994 		/* MDIO bus Registration */
6995 		ret = stmmac_mdio_register(ndev);
6996 		if (ret < 0) {
6997 			dev_err(priv->device,
6998 				"%s: MDIO bus (id: %d) registration failed",
6999 				__func__, priv->plat->bus_id);
7000 			goto error_mdio_register;
7001 		}
7002 	}
7003 
7004 	ret = stmmac_phy_setup(priv);
7005 	if (ret) {
7006 		netdev_err(ndev, "failed to setup phy (%d)\n", ret);
7007 		goto error_phy_setup;
7008 	}
7009 
7010 	ret = register_netdev(ndev);
7011 	if (ret) {
7012 		dev_err(priv->device, "%s: ERROR %i registering the device\n",
7013 			__func__, ret);
7014 		goto error_netdev_register;
7015 	}
7016 
7017 	if (priv->plat->serdes_powerup) {
7018 		ret = priv->plat->serdes_powerup(ndev,
7019 						 priv->plat->bsp_priv);
7020 
7021 		if (ret < 0)
7022 			goto error_serdes_powerup;
7023 	}
7024 
7025 #ifdef CONFIG_DEBUG_FS
7026 	stmmac_init_fs(ndev);
7027 #endif
7028 
7029 	/* Let pm_runtime_put() disable the clocks.
7030 	 * If CONFIG_PM is not enabled, the clocks will stay powered.
7031 	 */
7032 	pm_runtime_put(device);
7033 
7034 	return ret;
7035 
7036 error_serdes_powerup:
7037 	unregister_netdev(ndev);
7038 error_netdev_register:
7039 	phylink_destroy(priv->phylink);
7040 error_phy_setup:
7041 	if (priv->hw->pcs != STMMAC_PCS_TBI &&
7042 	    priv->hw->pcs != STMMAC_PCS_RTBI)
7043 		stmmac_mdio_unregister(ndev);
7044 error_mdio_register:
7045 	stmmac_napi_del(ndev);
7046 error_hw_init:
7047 	destroy_workqueue(priv->wq);
7048 	stmmac_bus_clks_config(priv, false);
7049 	bitmap_free(priv->af_xdp_zc_qps);
7050 
7051 	return ret;
7052 }
7053 EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
7054 
7055 /**
7056  * stmmac_dvr_remove
7057  * @dev: device pointer
7058  * Description: this function resets the TX/RX processes, disables the MAC RX/TX
7059  * changes the link status, releases the DMA descriptor rings.
7060  */
7061 int stmmac_dvr_remove(struct device *dev)
7062 {
7063 	struct net_device *ndev = dev_get_drvdata(dev);
7064 	struct stmmac_priv *priv = netdev_priv(ndev);
7065 
7066 	netdev_info(priv->dev, "%s: removing driver", __func__);
7067 
7068 	stmmac_stop_all_dma(priv);
7069 	stmmac_mac_set(priv, priv->ioaddr, false);
7070 	netif_carrier_off(ndev);
7071 	unregister_netdev(ndev);
7072 
7073 	/* Serdes power down needs to happen after VLAN filter
7074 	 * is deleted that is triggered by unregister_netdev().
7075 	 */
7076 	if (priv->plat->serdes_powerdown)
7077 		priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv);
7078 
7079 #ifdef CONFIG_DEBUG_FS
7080 	stmmac_exit_fs(ndev);
7081 #endif
7082 	phylink_destroy(priv->phylink);
7083 	if (priv->plat->stmmac_rst)
7084 		reset_control_assert(priv->plat->stmmac_rst);
7085 	pm_runtime_put(dev);
7086 	pm_runtime_disable(dev);
7087 	if (priv->hw->pcs != STMMAC_PCS_TBI &&
7088 	    priv->hw->pcs != STMMAC_PCS_RTBI)
7089 		stmmac_mdio_unregister(ndev);
7090 	destroy_workqueue(priv->wq);
7091 	mutex_destroy(&priv->lock);
7092 	bitmap_free(priv->af_xdp_zc_qps);
7093 
7094 	return 0;
7095 }
7096 EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
7097 
7098 /**
7099  * stmmac_suspend - suspend callback
7100  * @dev: device pointer
7101  * Description: this is the function to suspend the device and it is called
7102  * by the platform driver to stop the network queue, release the resources,
7103  * program the PMT register (for WoL), clean and release driver resources.
7104  */
7105 int stmmac_suspend(struct device *dev)
7106 {
7107 	struct net_device *ndev = dev_get_drvdata(dev);
7108 	struct stmmac_priv *priv = netdev_priv(ndev);
7109 	u32 chan;
7110 	int ret;
7111 
7112 	if (!ndev || !netif_running(ndev))
7113 		return 0;
7114 
7115 	phylink_mac_change(priv->phylink, false);
7116 
7117 	mutex_lock(&priv->lock);
7118 
7119 	netif_device_detach(ndev);
7120 
7121 	stmmac_disable_all_queues(priv);
7122 
7123 	for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
7124 		hrtimer_cancel(&priv->tx_queue[chan].txtimer);
7125 
7126 	if (priv->eee_enabled) {
7127 		priv->tx_path_in_lpi_mode = false;
7128 		del_timer_sync(&priv->eee_ctrl_timer);
7129 	}
7130 
7131 	/* Stop TX/RX DMA */
7132 	stmmac_stop_all_dma(priv);
7133 
7134 	if (priv->plat->serdes_powerdown)
7135 		priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv);
7136 
7137 	/* Enable Power down mode by programming the PMT regs */
7138 	if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7139 		stmmac_pmt(priv, priv->hw, priv->wolopts);
7140 		priv->irq_wake = 1;
7141 	} else {
7142 		mutex_unlock(&priv->lock);
7143 		rtnl_lock();
7144 		if (device_may_wakeup(priv->device))
7145 			phylink_speed_down(priv->phylink, false);
7146 		phylink_stop(priv->phylink);
7147 		rtnl_unlock();
7148 		mutex_lock(&priv->lock);
7149 
7150 		stmmac_mac_set(priv, priv->ioaddr, false);
7151 		pinctrl_pm_select_sleep_state(priv->device);
7152 		/* Disable clock in case of PWM is off */
7153 		clk_disable_unprepare(priv->plat->clk_ptp_ref);
7154 		ret = pm_runtime_force_suspend(dev);
7155 		if (ret) {
7156 			mutex_unlock(&priv->lock);
7157 			return ret;
7158 		}
7159 	}
7160 
7161 	mutex_unlock(&priv->lock);
7162 
7163 	if (priv->dma_cap.fpesel) {
7164 		/* Disable FPE */
7165 		stmmac_fpe_configure(priv, priv->ioaddr,
7166 				     priv->plat->tx_queues_to_use,
7167 				     priv->plat->rx_queues_to_use, false);
7168 
7169 		stmmac_fpe_handshake(priv, false);
7170 	}
7171 
7172 	priv->speed = SPEED_UNKNOWN;
7173 	return 0;
7174 }
7175 EXPORT_SYMBOL_GPL(stmmac_suspend);
7176 
7177 /**
7178  * stmmac_reset_queues_param - reset queue parameters
7179  * @priv: device pointer
7180  */
7181 static void stmmac_reset_queues_param(struct stmmac_priv *priv)
7182 {
7183 	u32 rx_cnt = priv->plat->rx_queues_to_use;
7184 	u32 tx_cnt = priv->plat->tx_queues_to_use;
7185 	u32 queue;
7186 
7187 	for (queue = 0; queue < rx_cnt; queue++) {
7188 		struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
7189 
7190 		rx_q->cur_rx = 0;
7191 		rx_q->dirty_rx = 0;
7192 	}
7193 
7194 	for (queue = 0; queue < tx_cnt; queue++) {
7195 		struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
7196 
7197 		tx_q->cur_tx = 0;
7198 		tx_q->dirty_tx = 0;
7199 		tx_q->mss = 0;
7200 
7201 		netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
7202 	}
7203 }
7204 
7205 /**
7206  * stmmac_resume - resume callback
7207  * @dev: device pointer
7208  * Description: when resume this function is invoked to setup the DMA and CORE
7209  * in a usable state.
7210  */
7211 int stmmac_resume(struct device *dev)
7212 {
7213 	struct net_device *ndev = dev_get_drvdata(dev);
7214 	struct stmmac_priv *priv = netdev_priv(ndev);
7215 	int ret;
7216 
7217 	if (!netif_running(ndev))
7218 		return 0;
7219 
7220 	/* Power Down bit, into the PM register, is cleared
7221 	 * automatically as soon as a magic packet or a Wake-up frame
7222 	 * is received. Anyway, it's better to manually clear
7223 	 * this bit because it can generate problems while resuming
7224 	 * from another devices (e.g. serial console).
7225 	 */
7226 	if (device_may_wakeup(priv->device) && priv->plat->pmt) {
7227 		mutex_lock(&priv->lock);
7228 		stmmac_pmt(priv, priv->hw, 0);
7229 		mutex_unlock(&priv->lock);
7230 		priv->irq_wake = 0;
7231 	} else {
7232 		pinctrl_pm_select_default_state(priv->device);
7233 		/* enable the clk previously disabled */
7234 		ret = pm_runtime_force_resume(dev);
7235 		if (ret)
7236 			return ret;
7237 		if (priv->plat->clk_ptp_ref)
7238 			clk_prepare_enable(priv->plat->clk_ptp_ref);
7239 		/* reset the phy so that it's ready */
7240 		if (priv->mii)
7241 			stmmac_mdio_reset(priv->mii);
7242 	}
7243 
7244 	if (priv->plat->serdes_powerup) {
7245 		ret = priv->plat->serdes_powerup(ndev,
7246 						 priv->plat->bsp_priv);
7247 
7248 		if (ret < 0)
7249 			return ret;
7250 	}
7251 
7252 	if (!device_may_wakeup(priv->device) || !priv->plat->pmt) {
7253 		rtnl_lock();
7254 		phylink_start(priv->phylink);
7255 		/* We may have called phylink_speed_down before */
7256 		phylink_speed_up(priv->phylink);
7257 		rtnl_unlock();
7258 	}
7259 
7260 	rtnl_lock();
7261 	mutex_lock(&priv->lock);
7262 
7263 	stmmac_reset_queues_param(priv);
7264 
7265 	stmmac_free_tx_skbufs(priv);
7266 	stmmac_clear_descriptors(priv);
7267 
7268 	stmmac_hw_setup(ndev, false);
7269 	stmmac_init_coalesce(priv);
7270 	stmmac_set_rx_mode(ndev);
7271 
7272 	stmmac_restore_hw_vlan_rx_fltr(priv, ndev, priv->hw);
7273 
7274 	stmmac_enable_all_queues(priv);
7275 
7276 	mutex_unlock(&priv->lock);
7277 	rtnl_unlock();
7278 
7279 	phylink_mac_change(priv->phylink, true);
7280 
7281 	netif_device_attach(ndev);
7282 
7283 	return 0;
7284 }
7285 EXPORT_SYMBOL_GPL(stmmac_resume);
7286 
7287 #ifndef MODULE
7288 static int __init stmmac_cmdline_opt(char *str)
7289 {
7290 	char *opt;
7291 
7292 	if (!str || !*str)
7293 		return -EINVAL;
7294 	while ((opt = strsep(&str, ",")) != NULL) {
7295 		if (!strncmp(opt, "debug:", 6)) {
7296 			if (kstrtoint(opt + 6, 0, &debug))
7297 				goto err;
7298 		} else if (!strncmp(opt, "phyaddr:", 8)) {
7299 			if (kstrtoint(opt + 8, 0, &phyaddr))
7300 				goto err;
7301 		} else if (!strncmp(opt, "buf_sz:", 7)) {
7302 			if (kstrtoint(opt + 7, 0, &buf_sz))
7303 				goto err;
7304 		} else if (!strncmp(opt, "tc:", 3)) {
7305 			if (kstrtoint(opt + 3, 0, &tc))
7306 				goto err;
7307 		} else if (!strncmp(opt, "watchdog:", 9)) {
7308 			if (kstrtoint(opt + 9, 0, &watchdog))
7309 				goto err;
7310 		} else if (!strncmp(opt, "flow_ctrl:", 10)) {
7311 			if (kstrtoint(opt + 10, 0, &flow_ctrl))
7312 				goto err;
7313 		} else if (!strncmp(opt, "pause:", 6)) {
7314 			if (kstrtoint(opt + 6, 0, &pause))
7315 				goto err;
7316 		} else if (!strncmp(opt, "eee_timer:", 10)) {
7317 			if (kstrtoint(opt + 10, 0, &eee_timer))
7318 				goto err;
7319 		} else if (!strncmp(opt, "chain_mode:", 11)) {
7320 			if (kstrtoint(opt + 11, 0, &chain_mode))
7321 				goto err;
7322 		}
7323 	}
7324 	return 0;
7325 
7326 err:
7327 	pr_err("%s: ERROR broken module parameter conversion", __func__);
7328 	return -EINVAL;
7329 }
7330 
7331 __setup("stmmaceth=", stmmac_cmdline_opt);
7332 #endif /* MODULE */
7333 
7334 static int __init stmmac_init(void)
7335 {
7336 #ifdef CONFIG_DEBUG_FS
7337 	/* Create debugfs main directory if it doesn't exist yet */
7338 	if (!stmmac_fs_dir)
7339 		stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
7340 	register_netdevice_notifier(&stmmac_notifier);
7341 #endif
7342 
7343 	return 0;
7344 }
7345 
7346 static void __exit stmmac_exit(void)
7347 {
7348 #ifdef CONFIG_DEBUG_FS
7349 	unregister_netdevice_notifier(&stmmac_notifier);
7350 	debugfs_remove_recursive(stmmac_fs_dir);
7351 #endif
7352 }
7353 
7354 module_init(stmmac_init)
7355 module_exit(stmmac_exit)
7356 
7357 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
7358 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
7359 MODULE_LICENSE("GPL");
7360