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