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