xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 <net/pkt_cls.h>
42 #include "stmmac_ptp.h"
43 #include "stmmac.h"
44 #include <linux/reset.h>
45 #include <linux/of_mdio.h>
46 #include "dwmac1000.h"
47 #include "dwxgmac2.h"
48 #include "hwif.h"
49 
50 /* As long as the interface is active, we keep the timestamping counter enabled
51  * with fine resolution and binary rollover. This avoid non-monotonic behavior
52  * (clock jumps) when changing timestamping settings at runtime.
53  */
54 #define STMMAC_HWTS_ACTIVE	(PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | \
55 				 PTP_TCR_TSCTRLSSR)
56 
57 #define	STMMAC_ALIGN(x)		ALIGN(ALIGN(x, SMP_CACHE_BYTES), 16)
58 #define	TSO_MAX_BUFF_SIZE	(SZ_16K - 1)
59 
60 /* Module parameters */
61 #define TX_TIMEO	5000
62 static int watchdog = TX_TIMEO;
63 module_param(watchdog, int, 0644);
64 MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
65 
66 static int debug = -1;
67 module_param(debug, int, 0644);
68 MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
69 
70 static int phyaddr = -1;
71 module_param(phyaddr, int, 0444);
72 MODULE_PARM_DESC(phyaddr, "Physical device address");
73 
74 #define STMMAC_TX_THRESH(x)	((x)->dma_tx_size / 4)
75 #define STMMAC_RX_THRESH(x)	((x)->dma_rx_size / 4)
76 
77 static int flow_ctrl = FLOW_AUTO;
78 module_param(flow_ctrl, int, 0644);
79 MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
80 
81 static int pause = PAUSE_TIME;
82 module_param(pause, int, 0644);
83 MODULE_PARM_DESC(pause, "Flow Control Pause Time");
84 
85 #define TC_DEFAULT 64
86 static int tc = TC_DEFAULT;
87 module_param(tc, int, 0644);
88 MODULE_PARM_DESC(tc, "DMA threshold control value");
89 
90 #define	DEFAULT_BUFSIZE	1536
91 static int buf_sz = DEFAULT_BUFSIZE;
92 module_param(buf_sz, int, 0644);
93 MODULE_PARM_DESC(buf_sz, "DMA buffer size");
94 
95 #define	STMMAC_RX_COPYBREAK	256
96 
97 static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
98 				      NETIF_MSG_LINK | NETIF_MSG_IFUP |
99 				      NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
100 
101 #define STMMAC_DEFAULT_LPI_TIMER	1000
102 static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
103 module_param(eee_timer, int, 0644);
104 MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
105 #define STMMAC_LPI_T(x) (jiffies + usecs_to_jiffies(x))
106 
107 /* By default the driver will use the ring mode to manage tx and rx descriptors,
108  * but allow user to force to use the chain instead of the ring
109  */
110 static unsigned int chain_mode;
111 module_param(chain_mode, int, 0444);
112 MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
113 
114 static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
115 
116 #ifdef CONFIG_DEBUG_FS
117 static const struct net_device_ops stmmac_netdev_ops;
118 static void stmmac_init_fs(struct net_device *dev);
119 static void stmmac_exit_fs(struct net_device *dev);
120 #endif
121 
122 #define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
123 
stmmac_bus_clks_config(struct stmmac_priv * priv,bool enabled)124 int stmmac_bus_clks_config(struct stmmac_priv *priv, bool enabled)
125 {
126 	int ret = 0;
127 
128 	if (enabled) {
129 		ret = clk_prepare_enable(priv->plat->stmmac_clk);
130 		if (ret)
131 			return ret;
132 		ret = clk_prepare_enable(priv->plat->pclk);
133 		if (ret) {
134 			clk_disable_unprepare(priv->plat->stmmac_clk);
135 			return ret;
136 		}
137 	} else {
138 		clk_disable_unprepare(priv->plat->stmmac_clk);
139 		clk_disable_unprepare(priv->plat->pclk);
140 	}
141 
142 	return ret;
143 }
144 EXPORT_SYMBOL_GPL(stmmac_bus_clks_config);
145 
146 /**
147  * stmmac_verify_args - verify the driver parameters.
148  * Description: it checks the driver parameters and set a default in case of
149  * errors.
150  */
stmmac_verify_args(void)151 static void stmmac_verify_args(void)
152 {
153 	if (unlikely(watchdog < 0))
154 		watchdog = TX_TIMEO;
155 	if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
156 		buf_sz = DEFAULT_BUFSIZE;
157 	if (unlikely(flow_ctrl > 1))
158 		flow_ctrl = FLOW_AUTO;
159 	else if (likely(flow_ctrl < 0))
160 		flow_ctrl = FLOW_OFF;
161 	if (unlikely((pause < 0) || (pause > 0xffff)))
162 		pause = PAUSE_TIME;
163 	if (eee_timer < 0)
164 		eee_timer = STMMAC_DEFAULT_LPI_TIMER;
165 }
166 
167 /**
168  * stmmac_disable_all_queues - Disable all queues
169  * @priv: driver private structure
170  */
stmmac_disable_all_queues(struct stmmac_priv * priv)171 static void stmmac_disable_all_queues(struct stmmac_priv *priv)
172 {
173 	u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
174 	u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
175 	u32 maxq = max(rx_queues_cnt, tx_queues_cnt);
176 	u32 queue;
177 
178 	for (queue = 0; queue < maxq; queue++) {
179 		struct stmmac_channel *ch = &priv->channel[queue];
180 
181 		if (queue < rx_queues_cnt)
182 			napi_disable(&ch->rx_napi);
183 		if (queue < tx_queues_cnt)
184 			napi_disable(&ch->tx_napi);
185 	}
186 }
187 
188 /**
189  * stmmac_enable_all_queues - Enable all queues
190  * @priv: driver private structure
191  */
stmmac_enable_all_queues(struct stmmac_priv * priv)192 static void stmmac_enable_all_queues(struct stmmac_priv *priv)
193 {
194 	u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
195 	u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
196 	u32 maxq = max(rx_queues_cnt, tx_queues_cnt);
197 	u32 queue;
198 
199 	for (queue = 0; queue < maxq; queue++) {
200 		struct stmmac_channel *ch = &priv->channel[queue];
201 
202 		if (queue < rx_queues_cnt)
203 			napi_enable(&ch->rx_napi);
204 		if (queue < tx_queues_cnt)
205 			napi_enable(&ch->tx_napi);
206 	}
207 }
208 
stmmac_service_event_schedule(struct stmmac_priv * priv)209 static void stmmac_service_event_schedule(struct stmmac_priv *priv)
210 {
211 	if (!test_bit(STMMAC_DOWN, &priv->state) &&
212 	    !test_and_set_bit(STMMAC_SERVICE_SCHED, &priv->state))
213 		queue_work(priv->wq, &priv->service_task);
214 }
215 
stmmac_global_err(struct stmmac_priv * priv)216 static void stmmac_global_err(struct stmmac_priv *priv)
217 {
218 	netif_carrier_off(priv->dev);
219 	set_bit(STMMAC_RESET_REQUESTED, &priv->state);
220 	stmmac_service_event_schedule(priv);
221 }
222 
223 /**
224  * stmmac_clk_csr_set - dynamically set the MDC clock
225  * @priv: driver private structure
226  * Description: this is to dynamically set the MDC clock according to the csr
227  * clock input.
228  * Note:
229  *	If a specific clk_csr value is passed from the platform
230  *	this means that the CSR Clock Range selection cannot be
231  *	changed at run-time and it is fixed (as reported in the driver
232  *	documentation). Viceversa the driver will try to set the MDC
233  *	clock dynamically according to the actual clock input.
234  */
stmmac_clk_csr_set(struct stmmac_priv * priv)235 static void stmmac_clk_csr_set(struct stmmac_priv *priv)
236 {
237 	u32 clk_rate;
238 
239 	clk_rate = clk_get_rate(priv->plat->pclk);
240 
241 	/* Platform provided default clk_csr would be assumed valid
242 	 * for all other cases except for the below mentioned ones.
243 	 * For values higher than the IEEE 802.3 specified frequency
244 	 * we can not estimate the proper divider as it is not known
245 	 * the frequency of clk_csr_i. So we do not change the default
246 	 * divider.
247 	 */
248 	if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
249 		if (clk_rate < CSR_F_35M)
250 			priv->clk_csr = STMMAC_CSR_20_35M;
251 		else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
252 			priv->clk_csr = STMMAC_CSR_35_60M;
253 		else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
254 			priv->clk_csr = STMMAC_CSR_60_100M;
255 		else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
256 			priv->clk_csr = STMMAC_CSR_100_150M;
257 		else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
258 			priv->clk_csr = STMMAC_CSR_150_250M;
259 		else if ((clk_rate >= CSR_F_250M) && (clk_rate <= CSR_F_300M))
260 			priv->clk_csr = STMMAC_CSR_250_300M;
261 	}
262 
263 	if (priv->plat->has_sun8i) {
264 		if (clk_rate > 160000000)
265 			priv->clk_csr = 0x03;
266 		else if (clk_rate > 80000000)
267 			priv->clk_csr = 0x02;
268 		else if (clk_rate > 40000000)
269 			priv->clk_csr = 0x01;
270 		else
271 			priv->clk_csr = 0;
272 	}
273 
274 	if (priv->plat->has_xgmac) {
275 		if (clk_rate > 400000000)
276 			priv->clk_csr = 0x5;
277 		else if (clk_rate > 350000000)
278 			priv->clk_csr = 0x4;
279 		else if (clk_rate > 300000000)
280 			priv->clk_csr = 0x3;
281 		else if (clk_rate > 250000000)
282 			priv->clk_csr = 0x2;
283 		else if (clk_rate > 150000000)
284 			priv->clk_csr = 0x1;
285 		else
286 			priv->clk_csr = 0x0;
287 	}
288 }
289 
print_pkt(unsigned char * buf,int len)290 static void print_pkt(unsigned char *buf, int len)
291 {
292 	pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
293 	print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
294 }
295 
stmmac_tx_avail(struct stmmac_priv * priv,u32 queue)296 static inline u32 stmmac_tx_avail(struct stmmac_priv *priv, u32 queue)
297 {
298 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
299 	u32 avail;
300 
301 	if (tx_q->dirty_tx > tx_q->cur_tx)
302 		avail = tx_q->dirty_tx - tx_q->cur_tx - 1;
303 	else
304 		avail = priv->dma_tx_size - tx_q->cur_tx + tx_q->dirty_tx - 1;
305 
306 	return avail;
307 }
308 
309 /**
310  * stmmac_rx_dirty - Get RX queue dirty
311  * @priv: driver private structure
312  * @queue: RX queue index
313  */
stmmac_rx_dirty(struct stmmac_priv * priv,u32 queue)314 static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv, u32 queue)
315 {
316 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
317 	u32 dirty;
318 
319 	if (rx_q->dirty_rx <= rx_q->cur_rx)
320 		dirty = rx_q->cur_rx - rx_q->dirty_rx;
321 	else
322 		dirty = priv->dma_rx_size - rx_q->dirty_rx + rx_q->cur_rx;
323 
324 	return dirty;
325 }
326 
327 /**
328  * stmmac_enable_eee_mode - check and enter in LPI mode
329  * @priv: driver private structure
330  * Description: this function is to verify and enter in LPI mode in case of
331  * EEE.
332  */
stmmac_enable_eee_mode(struct stmmac_priv * priv)333 static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
334 {
335 	u32 tx_cnt = priv->plat->tx_queues_to_use;
336 	u32 queue;
337 
338 	/* check if all TX queues have the work finished */
339 	for (queue = 0; queue < tx_cnt; queue++) {
340 		struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
341 
342 		if (tx_q->dirty_tx != tx_q->cur_tx)
343 			return; /* still unfinished work */
344 	}
345 
346 	/* Check and enter in LPI mode */
347 	if (!priv->tx_path_in_lpi_mode)
348 		stmmac_set_eee_mode(priv, priv->hw,
349 				priv->plat->en_tx_lpi_clockgating);
350 }
351 
352 /**
353  * stmmac_disable_eee_mode - disable and exit from LPI mode
354  * @priv: driver private structure
355  * Description: this function is to exit and disable EEE in case of
356  * LPI state is true. This is called by the xmit.
357  */
stmmac_disable_eee_mode(struct stmmac_priv * priv)358 void stmmac_disable_eee_mode(struct stmmac_priv *priv)
359 {
360 	stmmac_reset_eee_mode(priv, priv->hw);
361 	del_timer_sync(&priv->eee_ctrl_timer);
362 	priv->tx_path_in_lpi_mode = false;
363 }
364 
365 /**
366  * stmmac_eee_ctrl_timer - EEE TX SW timer.
367  * @t:  timer_list struct containing private info
368  * Description:
369  *  if there is no data transfer and if we are not in LPI state,
370  *  then MAC Transmitter can be moved to LPI state.
371  */
stmmac_eee_ctrl_timer(struct timer_list * t)372 static void stmmac_eee_ctrl_timer(struct timer_list *t)
373 {
374 	struct stmmac_priv *priv = from_timer(priv, t, eee_ctrl_timer);
375 
376 	stmmac_enable_eee_mode(priv);
377 	mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer));
378 }
379 
380 /**
381  * stmmac_eee_init - init EEE
382  * @priv: driver private structure
383  * Description:
384  *  if the GMAC supports the EEE (from the HW cap reg) and the phy device
385  *  can also manage EEE, this function enable the LPI state and start related
386  *  timer.
387  */
stmmac_eee_init(struct stmmac_priv * priv)388 bool stmmac_eee_init(struct stmmac_priv *priv)
389 {
390 	int eee_tw_timer = priv->eee_tw_timer;
391 
392 	/* Using PCS we cannot dial with the phy registers at this stage
393 	 * so we do not support extra feature like EEE.
394 	 */
395 	if (priv->hw->pcs == STMMAC_PCS_TBI ||
396 	    priv->hw->pcs == STMMAC_PCS_RTBI)
397 		return false;
398 
399 	/* Check if MAC core supports the EEE feature. */
400 	if (!priv->dma_cap.eee)
401 		return false;
402 
403 	mutex_lock(&priv->lock);
404 
405 	/* Check if it needs to be deactivated */
406 	if (!priv->eee_active) {
407 		if (priv->eee_enabled) {
408 			netdev_dbg(priv->dev, "disable EEE\n");
409 			del_timer_sync(&priv->eee_ctrl_timer);
410 			stmmac_set_eee_timer(priv, priv->hw, 0, eee_tw_timer);
411 		}
412 		mutex_unlock(&priv->lock);
413 		return false;
414 	}
415 
416 	if (priv->eee_active && !priv->eee_enabled) {
417 		timer_setup(&priv->eee_ctrl_timer, stmmac_eee_ctrl_timer, 0);
418 		stmmac_set_eee_timer(priv, priv->hw, STMMAC_DEFAULT_LIT_LS,
419 				     eee_tw_timer);
420 	}
421 
422 	mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer));
423 
424 	mutex_unlock(&priv->lock);
425 	netdev_dbg(priv->dev, "Energy-Efficient Ethernet initialized\n");
426 	return true;
427 }
428 
429 /* stmmac_get_tx_hwtstamp - get HW TX timestamps
430  * @priv: driver private structure
431  * @p : descriptor pointer
432  * @skb : the socket buffer
433  * Description :
434  * This function will read timestamp from the descriptor & pass it to stack.
435  * and also perform some sanity checks.
436  */
stmmac_get_tx_hwtstamp(struct stmmac_priv * priv,struct dma_desc * p,struct sk_buff * skb)437 static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
438 				   struct dma_desc *p, struct sk_buff *skb)
439 {
440 	struct skb_shared_hwtstamps shhwtstamp;
441 	bool found = false;
442 	u64 ns = 0;
443 
444 	if (!priv->hwts_tx_en)
445 		return;
446 
447 	/* exit if skb doesn't support hw tstamp */
448 	if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
449 		return;
450 
451 	/* check tx tstamp status */
452 	if (stmmac_get_tx_timestamp_status(priv, p)) {
453 		stmmac_get_timestamp(priv, p, priv->adv_ts, &ns);
454 		found = true;
455 	} else if (!stmmac_get_mac_tx_timestamp(priv, priv->hw, &ns)) {
456 		found = true;
457 	}
458 
459 	if (found) {
460 		memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
461 		shhwtstamp.hwtstamp = ns_to_ktime(ns);
462 
463 		netdev_dbg(priv->dev, "get valid TX hw timestamp %llu\n", ns);
464 		/* pass tstamp to stack */
465 		skb_tstamp_tx(skb, &shhwtstamp);
466 	}
467 }
468 
469 /* stmmac_get_rx_hwtstamp - get HW RX timestamps
470  * @priv: driver private structure
471  * @p : descriptor pointer
472  * @np : next descriptor pointer
473  * @skb : the socket buffer
474  * Description :
475  * This function will read received packet's timestamp from the descriptor
476  * and pass it to stack. It also perform some sanity checks.
477  */
stmmac_get_rx_hwtstamp(struct stmmac_priv * priv,struct dma_desc * p,struct dma_desc * np,struct sk_buff * skb)478 static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,
479 				   struct dma_desc *np, struct sk_buff *skb)
480 {
481 	struct skb_shared_hwtstamps *shhwtstamp = NULL;
482 	struct dma_desc *desc = p;
483 	u64 ns = 0;
484 
485 	if (!priv->hwts_rx_en)
486 		return;
487 	/* For GMAC4, the valid timestamp is from CTX next desc. */
488 	if (priv->plat->has_gmac4 || priv->plat->has_xgmac)
489 		desc = np;
490 
491 	/* Check if timestamp is available */
492 	if (stmmac_get_rx_timestamp_status(priv, p, np, priv->adv_ts)) {
493 		stmmac_get_timestamp(priv, desc, priv->adv_ts, &ns);
494 		netdev_dbg(priv->dev, "get valid RX hw timestamp %llu\n", ns);
495 		shhwtstamp = skb_hwtstamps(skb);
496 		memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
497 		shhwtstamp->hwtstamp = ns_to_ktime(ns);
498 	} else  {
499 		netdev_dbg(priv->dev, "cannot get RX hw timestamp\n");
500 	}
501 }
502 
503 /**
504  *  stmmac_hwtstamp_set - control hardware timestamping.
505  *  @dev: device pointer.
506  *  @ifr: An IOCTL specific structure, that can contain a pointer to
507  *  a proprietary structure used to pass information to the driver.
508  *  Description:
509  *  This function configures the MAC to enable/disable both outgoing(TX)
510  *  and incoming(RX) packets time stamping based on user input.
511  *  Return Value:
512  *  0 on success and an appropriate -ve integer on failure.
513  */
stmmac_hwtstamp_set(struct net_device * dev,struct ifreq * ifr)514 static int stmmac_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
515 {
516 	struct stmmac_priv *priv = netdev_priv(dev);
517 	struct hwtstamp_config config;
518 	u32 ptp_v2 = 0;
519 	u32 tstamp_all = 0;
520 	u32 ptp_over_ipv4_udp = 0;
521 	u32 ptp_over_ipv6_udp = 0;
522 	u32 ptp_over_ethernet = 0;
523 	u32 snap_type_sel = 0;
524 	u32 ts_master_en = 0;
525 	u32 ts_event_en = 0;
526 
527 	if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
528 		netdev_alert(priv->dev, "No support for HW time stamping\n");
529 		priv->hwts_tx_en = 0;
530 		priv->hwts_rx_en = 0;
531 
532 		return -EOPNOTSUPP;
533 	}
534 
535 	if (copy_from_user(&config, ifr->ifr_data,
536 			   sizeof(config)))
537 		return -EFAULT;
538 
539 	netdev_dbg(priv->dev, "%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
540 		   __func__, config.flags, config.tx_type, config.rx_filter);
541 
542 	/* reserved for future extensions */
543 	if (config.flags)
544 		return -EINVAL;
545 
546 	if (config.tx_type != HWTSTAMP_TX_OFF &&
547 	    config.tx_type != HWTSTAMP_TX_ON)
548 		return -ERANGE;
549 
550 	if (priv->adv_ts) {
551 		switch (config.rx_filter) {
552 		case HWTSTAMP_FILTER_NONE:
553 			/* time stamp no incoming packet at all */
554 			config.rx_filter = HWTSTAMP_FILTER_NONE;
555 			break;
556 
557 		case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
558 			/* PTP v1, UDP, any kind of event packet */
559 			config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
560 			/* 'xmac' hardware can support Sync, Pdelay_Req and
561 			 * Pdelay_resp by setting bit14 and bits17/16 to 01
562 			 * This leaves Delay_Req timestamps out.
563 			 * Enable all events *and* general purpose message
564 			 * timestamping
565 			 */
566 			snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
567 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
568 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
569 			break;
570 
571 		case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
572 			/* PTP v1, UDP, Sync packet */
573 			config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
574 			/* take time stamp for SYNC messages only */
575 			ts_event_en = PTP_TCR_TSEVNTENA;
576 
577 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
578 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
579 			break;
580 
581 		case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
582 			/* PTP v1, UDP, Delay_req packet */
583 			config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
584 			/* take time stamp for Delay_Req messages only */
585 			ts_master_en = PTP_TCR_TSMSTRENA;
586 			ts_event_en = PTP_TCR_TSEVNTENA;
587 
588 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
589 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
590 			break;
591 
592 		case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
593 			/* PTP v2, UDP, any kind of event packet */
594 			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
595 			ptp_v2 = PTP_TCR_TSVER2ENA;
596 			/* take time stamp for all event messages */
597 			snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
598 
599 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
600 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
601 			break;
602 
603 		case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
604 			/* PTP v2, UDP, Sync packet */
605 			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
606 			ptp_v2 = PTP_TCR_TSVER2ENA;
607 			/* take time stamp for SYNC messages only */
608 			ts_event_en = PTP_TCR_TSEVNTENA;
609 
610 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
611 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
612 			break;
613 
614 		case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
615 			/* PTP v2, UDP, Delay_req packet */
616 			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
617 			ptp_v2 = PTP_TCR_TSVER2ENA;
618 			/* take time stamp for Delay_Req messages only */
619 			ts_master_en = PTP_TCR_TSMSTRENA;
620 			ts_event_en = PTP_TCR_TSEVNTENA;
621 
622 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
623 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
624 			break;
625 
626 		case HWTSTAMP_FILTER_PTP_V2_EVENT:
627 			/* PTP v2/802.AS1 any layer, any kind of event packet */
628 			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
629 			ptp_v2 = PTP_TCR_TSVER2ENA;
630 			snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
631 			if (priv->synopsys_id < DWMAC_CORE_4_10)
632 				ts_event_en = PTP_TCR_TSEVNTENA;
633 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
634 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
635 			ptp_over_ethernet = PTP_TCR_TSIPENA;
636 			break;
637 
638 		case HWTSTAMP_FILTER_PTP_V2_SYNC:
639 			/* PTP v2/802.AS1, any layer, Sync packet */
640 			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
641 			ptp_v2 = PTP_TCR_TSVER2ENA;
642 			/* take time stamp for SYNC messages only */
643 			ts_event_en = PTP_TCR_TSEVNTENA;
644 
645 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
646 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
647 			ptp_over_ethernet = PTP_TCR_TSIPENA;
648 			break;
649 
650 		case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
651 			/* PTP v2/802.AS1, any layer, Delay_req packet */
652 			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
653 			ptp_v2 = PTP_TCR_TSVER2ENA;
654 			/* take time stamp for Delay_Req messages only */
655 			ts_master_en = PTP_TCR_TSMSTRENA;
656 			ts_event_en = PTP_TCR_TSEVNTENA;
657 
658 			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
659 			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
660 			ptp_over_ethernet = PTP_TCR_TSIPENA;
661 			break;
662 
663 		case HWTSTAMP_FILTER_NTP_ALL:
664 		case HWTSTAMP_FILTER_ALL:
665 			/* time stamp any incoming packet */
666 			config.rx_filter = HWTSTAMP_FILTER_ALL;
667 			tstamp_all = PTP_TCR_TSENALL;
668 			break;
669 
670 		default:
671 			return -ERANGE;
672 		}
673 	} else {
674 		switch (config.rx_filter) {
675 		case HWTSTAMP_FILTER_NONE:
676 			config.rx_filter = HWTSTAMP_FILTER_NONE;
677 			break;
678 		default:
679 			/* PTP v1, UDP, any kind of event packet */
680 			config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
681 			break;
682 		}
683 	}
684 	priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
685 	priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
686 
687 	priv->systime_flags = STMMAC_HWTS_ACTIVE;
688 
689 	if (priv->hwts_tx_en || priv->hwts_rx_en) {
690 		priv->systime_flags |= tstamp_all | ptp_v2 |
691 				       ptp_over_ethernet | ptp_over_ipv6_udp |
692 				       ptp_over_ipv4_udp | ts_event_en |
693 				       ts_master_en | snap_type_sel;
694 	}
695 
696 	stmmac_config_hw_tstamping(priv, priv->ptpaddr, priv->systime_flags);
697 
698 	memcpy(&priv->tstamp_config, &config, sizeof(config));
699 
700 	return copy_to_user(ifr->ifr_data, &config,
701 			    sizeof(config)) ? -EFAULT : 0;
702 }
703 
704 /**
705  *  stmmac_hwtstamp_get - read hardware timestamping.
706  *  @dev: device pointer.
707  *  @ifr: An IOCTL specific structure, that can contain a pointer to
708  *  a proprietary structure used to pass information to the driver.
709  *  Description:
710  *  This function obtain the current hardware timestamping settings
711  *  as requested.
712  */
stmmac_hwtstamp_get(struct net_device * dev,struct ifreq * ifr)713 static int stmmac_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
714 {
715 	struct stmmac_priv *priv = netdev_priv(dev);
716 	struct hwtstamp_config *config = &priv->tstamp_config;
717 
718 	if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
719 		return -EOPNOTSUPP;
720 
721 	return copy_to_user(ifr->ifr_data, config,
722 			    sizeof(*config)) ? -EFAULT : 0;
723 }
724 
725 /**
726  * stmmac_init_tstamp_counter - init hardware timestamping counter
727  * @priv: driver private structure
728  * @systime_flags: timestamping flags
729  * Description:
730  * Initialize hardware counter for packet timestamping.
731  * This is valid as long as the interface is open and not suspended.
732  * Will be rerun after resuming from suspend, case in which the timestamping
733  * flags updated by stmmac_hwtstamp_set() also need to be restored.
734  */
stmmac_init_tstamp_counter(struct stmmac_priv * priv,u32 systime_flags)735 int stmmac_init_tstamp_counter(struct stmmac_priv *priv, u32 systime_flags)
736 {
737 	bool xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
738 	struct timespec64 now;
739 	u32 sec_inc = 0;
740 	u64 temp = 0;
741 
742 	if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
743 		return -EOPNOTSUPP;
744 
745 	stmmac_config_hw_tstamping(priv, priv->ptpaddr, systime_flags);
746 	priv->systime_flags = systime_flags;
747 
748 	/* program Sub Second Increment reg */
749 	stmmac_config_sub_second_increment(priv, priv->ptpaddr,
750 					   priv->plat->clk_ptp_rate,
751 					   xmac, &sec_inc);
752 	temp = div_u64(1000000000ULL, sec_inc);
753 
754 	/* Store sub second increment for later use */
755 	priv->sub_second_inc = sec_inc;
756 
757 	/* calculate default added value:
758 	 * formula is :
759 	 * addend = (2^32)/freq_div_ratio;
760 	 * where, freq_div_ratio = 1e9ns/sec_inc
761 	 */
762 	temp = (u64)(temp << 32);
763 	priv->default_addend = div_u64(temp, priv->plat->clk_ptp_rate);
764 	stmmac_config_addend(priv, priv->ptpaddr, priv->default_addend);
765 
766 	/* initialize system time */
767 	ktime_get_real_ts64(&now);
768 
769 	/* lower 32 bits of tv_sec are safe until y2106 */
770 	stmmac_init_systime(priv, priv->ptpaddr, (u32)now.tv_sec, now.tv_nsec);
771 
772 	return 0;
773 }
774 EXPORT_SYMBOL_GPL(stmmac_init_tstamp_counter);
775 
776 /**
777  * stmmac_init_ptp - init PTP
778  * @priv: driver private structure
779  * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
780  * This is done by looking at the HW cap. register.
781  * This function also registers the ptp driver.
782  */
stmmac_init_ptp(struct stmmac_priv * priv)783 static int stmmac_init_ptp(struct stmmac_priv *priv)
784 {
785 	bool xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
786 	int ret;
787 
788 	ret = stmmac_init_tstamp_counter(priv, STMMAC_HWTS_ACTIVE);
789 	if (ret)
790 		return ret;
791 
792 	priv->adv_ts = 0;
793 	/* Check if adv_ts can be enabled for dwmac 4.x / xgmac core */
794 	if (xmac && priv->dma_cap.atime_stamp)
795 		priv->adv_ts = 1;
796 	/* Dwmac 3.x core with extend_desc can support adv_ts */
797 	else if (priv->extend_desc && priv->dma_cap.atime_stamp)
798 		priv->adv_ts = 1;
799 
800 	if (priv->dma_cap.time_stamp)
801 		netdev_info(priv->dev, "IEEE 1588-2002 Timestamp supported\n");
802 
803 	if (priv->adv_ts)
804 		netdev_info(priv->dev,
805 			    "IEEE 1588-2008 Advanced Timestamp supported\n");
806 
807 	priv->hwts_tx_en = 0;
808 	priv->hwts_rx_en = 0;
809 
810 	return 0;
811 }
812 
stmmac_release_ptp(struct stmmac_priv * priv)813 static void stmmac_release_ptp(struct stmmac_priv *priv)
814 {
815 	clk_disable_unprepare(priv->plat->clk_ptp_ref);
816 	stmmac_ptp_unregister(priv);
817 }
818 
819 /**
820  *  stmmac_mac_flow_ctrl - Configure flow control in all queues
821  *  @priv: driver private structure
822  *  @duplex: duplex passed to the next function
823  *  Description: It is used for configuring the flow control in all queues
824  */
stmmac_mac_flow_ctrl(struct stmmac_priv * priv,u32 duplex)825 static void stmmac_mac_flow_ctrl(struct stmmac_priv *priv, u32 duplex)
826 {
827 	u32 tx_cnt = priv->plat->tx_queues_to_use;
828 
829 	stmmac_flow_ctrl(priv, priv->hw, duplex, priv->flow_ctrl & priv->plat->flow_ctrl,
830 			 priv->pause, tx_cnt);
831 }
832 
stmmac_validate(struct phylink_config * config,unsigned long * supported,struct phylink_link_state * state)833 static void stmmac_validate(struct phylink_config *config,
834 			    unsigned long *supported,
835 			    struct phylink_link_state *state)
836 {
837 	struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
838 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mac_supported) = { 0, };
839 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
840 	int tx_cnt = priv->plat->tx_queues_to_use;
841 	int max_speed = priv->plat->max_speed;
842 
843 	phylink_set(mac_supported, 10baseT_Half);
844 	phylink_set(mac_supported, 10baseT_Full);
845 	phylink_set(mac_supported, 100baseT_Half);
846 	phylink_set(mac_supported, 100baseT_Full);
847 	phylink_set(mac_supported, 1000baseT_Half);
848 	phylink_set(mac_supported, 1000baseT_Full);
849 	phylink_set(mac_supported, 1000baseKX_Full);
850 	phylink_set(mac_supported, 100baseT1_Full);
851 	phylink_set(mac_supported, 1000baseT1_Full);
852 
853 	phylink_set(mac_supported, Autoneg);
854 	phylink_set(mac_supported, Pause);
855 	phylink_set(mac_supported, Asym_Pause);
856 	phylink_set_port_modes(mac_supported);
857 
858 	/* Cut down 1G if asked to */
859 	if ((max_speed > 0) && (max_speed < 1000)) {
860 		phylink_set(mask, 1000baseT_Full);
861 		phylink_set(mask, 1000baseX_Full);
862 	} else if (priv->plat->has_xgmac) {
863 		if (!max_speed || (max_speed >= 2500)) {
864 			phylink_set(mac_supported, 2500baseT_Full);
865 			phylink_set(mac_supported, 2500baseX_Full);
866 		}
867 		if (!max_speed || (max_speed >= 5000)) {
868 			phylink_set(mac_supported, 5000baseT_Full);
869 		}
870 		if (!max_speed || (max_speed >= 10000)) {
871 			phylink_set(mac_supported, 10000baseSR_Full);
872 			phylink_set(mac_supported, 10000baseLR_Full);
873 			phylink_set(mac_supported, 10000baseER_Full);
874 			phylink_set(mac_supported, 10000baseLRM_Full);
875 			phylink_set(mac_supported, 10000baseT_Full);
876 			phylink_set(mac_supported, 10000baseKX4_Full);
877 			phylink_set(mac_supported, 10000baseKR_Full);
878 		}
879 		if (!max_speed || (max_speed >= 25000)) {
880 			phylink_set(mac_supported, 25000baseCR_Full);
881 			phylink_set(mac_supported, 25000baseKR_Full);
882 			phylink_set(mac_supported, 25000baseSR_Full);
883 		}
884 		if (!max_speed || (max_speed >= 40000)) {
885 			phylink_set(mac_supported, 40000baseKR4_Full);
886 			phylink_set(mac_supported, 40000baseCR4_Full);
887 			phylink_set(mac_supported, 40000baseSR4_Full);
888 			phylink_set(mac_supported, 40000baseLR4_Full);
889 		}
890 		if (!max_speed || (max_speed >= 50000)) {
891 			phylink_set(mac_supported, 50000baseCR2_Full);
892 			phylink_set(mac_supported, 50000baseKR2_Full);
893 			phylink_set(mac_supported, 50000baseSR2_Full);
894 			phylink_set(mac_supported, 50000baseKR_Full);
895 			phylink_set(mac_supported, 50000baseSR_Full);
896 			phylink_set(mac_supported, 50000baseCR_Full);
897 			phylink_set(mac_supported, 50000baseLR_ER_FR_Full);
898 			phylink_set(mac_supported, 50000baseDR_Full);
899 		}
900 		if (!max_speed || (max_speed >= 100000)) {
901 			phylink_set(mac_supported, 100000baseKR4_Full);
902 			phylink_set(mac_supported, 100000baseSR4_Full);
903 			phylink_set(mac_supported, 100000baseCR4_Full);
904 			phylink_set(mac_supported, 100000baseLR4_ER4_Full);
905 			phylink_set(mac_supported, 100000baseKR2_Full);
906 			phylink_set(mac_supported, 100000baseSR2_Full);
907 			phylink_set(mac_supported, 100000baseCR2_Full);
908 			phylink_set(mac_supported, 100000baseLR2_ER2_FR2_Full);
909 			phylink_set(mac_supported, 100000baseDR2_Full);
910 		}
911 	}
912 
913 	/* Half-Duplex can only work with single queue */
914 	if (tx_cnt > 1) {
915 		phylink_set(mask, 10baseT_Half);
916 		phylink_set(mask, 100baseT_Half);
917 		phylink_set(mask, 1000baseT_Half);
918 	}
919 
920 	linkmode_and(supported, supported, mac_supported);
921 	linkmode_andnot(supported, supported, mask);
922 
923 	linkmode_and(state->advertising, state->advertising, mac_supported);
924 	linkmode_andnot(state->advertising, state->advertising, mask);
925 
926 	/* If PCS is supported, check which modes it supports. */
927 	stmmac_xpcs_validate(priv, &priv->hw->xpcs_args, supported, state);
928 }
929 
stmmac_mac_pcs_get_state(struct phylink_config * config,struct phylink_link_state * state)930 static void stmmac_mac_pcs_get_state(struct phylink_config *config,
931 				     struct phylink_link_state *state)
932 {
933 	struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
934 
935 	state->link = 0;
936 	stmmac_xpcs_get_state(priv, &priv->hw->xpcs_args, state);
937 }
938 
stmmac_mac_config(struct phylink_config * config,unsigned int mode,const struct phylink_link_state * state)939 static void stmmac_mac_config(struct phylink_config *config, unsigned int mode,
940 			      const struct phylink_link_state *state)
941 {
942 	struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
943 
944 	stmmac_xpcs_config(priv, &priv->hw->xpcs_args, state);
945 }
946 
stmmac_mac_an_restart(struct phylink_config * config)947 static void stmmac_mac_an_restart(struct phylink_config *config)
948 {
949 	/* Not Supported */
950 }
951 
stmmac_mac_link_down(struct phylink_config * config,unsigned int mode,phy_interface_t interface)952 static void stmmac_mac_link_down(struct phylink_config *config,
953 				 unsigned int mode, phy_interface_t interface)
954 {
955 	struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
956 
957 	stmmac_mac_set(priv, priv->ioaddr, false);
958 	priv->eee_active = false;
959 	priv->tx_lpi_enabled = false;
960 	stmmac_eee_init(priv);
961 	stmmac_set_eee_pls(priv, priv->hw, false);
962 }
963 
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)964 static void stmmac_mac_link_up(struct phylink_config *config,
965 			       struct phy_device *phy,
966 			       unsigned int mode, phy_interface_t interface,
967 			       int speed, int duplex,
968 			       bool tx_pause, bool rx_pause)
969 {
970 	struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
971 	u32 ctrl;
972 
973 	stmmac_xpcs_link_up(priv, &priv->hw->xpcs_args, speed, interface);
974 
975 	ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
976 	ctrl &= ~priv->hw->link.speed_mask;
977 
978 	if (interface == PHY_INTERFACE_MODE_USXGMII) {
979 		switch (speed) {
980 		case SPEED_10000:
981 			ctrl |= priv->hw->link.xgmii.speed10000;
982 			break;
983 		case SPEED_5000:
984 			ctrl |= priv->hw->link.xgmii.speed5000;
985 			break;
986 		case SPEED_2500:
987 			ctrl |= priv->hw->link.xgmii.speed2500;
988 			break;
989 		default:
990 			return;
991 		}
992 	} else if (interface == PHY_INTERFACE_MODE_XLGMII) {
993 		switch (speed) {
994 		case SPEED_100000:
995 			ctrl |= priv->hw->link.xlgmii.speed100000;
996 			break;
997 		case SPEED_50000:
998 			ctrl |= priv->hw->link.xlgmii.speed50000;
999 			break;
1000 		case SPEED_40000:
1001 			ctrl |= priv->hw->link.xlgmii.speed40000;
1002 			break;
1003 		case SPEED_25000:
1004 			ctrl |= priv->hw->link.xlgmii.speed25000;
1005 			break;
1006 		case SPEED_10000:
1007 			ctrl |= priv->hw->link.xgmii.speed10000;
1008 			break;
1009 		case SPEED_2500:
1010 			ctrl |= priv->hw->link.speed2500;
1011 			break;
1012 		case SPEED_1000:
1013 			ctrl |= priv->hw->link.speed1000;
1014 			break;
1015 		default:
1016 			return;
1017 		}
1018 	} else {
1019 		switch (speed) {
1020 		case SPEED_2500:
1021 			ctrl |= priv->hw->link.speed2500;
1022 			break;
1023 		case SPEED_1000:
1024 			ctrl |= priv->hw->link.speed1000;
1025 			break;
1026 		case SPEED_100:
1027 			ctrl |= priv->hw->link.speed100;
1028 			break;
1029 		case SPEED_10:
1030 			ctrl |= priv->hw->link.speed10;
1031 			break;
1032 		default:
1033 			return;
1034 		}
1035 	}
1036 
1037 	priv->speed = speed;
1038 
1039 	if (priv->plat->fix_mac_speed)
1040 		priv->plat->fix_mac_speed(priv->plat->bsp_priv, speed);
1041 
1042 	if (!duplex)
1043 		ctrl &= ~priv->hw->link.duplex;
1044 	else
1045 		ctrl |= priv->hw->link.duplex;
1046 
1047 	/* Flow Control operation */
1048 	if (rx_pause && tx_pause)
1049 		priv->flow_ctrl = FLOW_AUTO;
1050 	else if (rx_pause && !tx_pause)
1051 		priv->flow_ctrl = FLOW_RX;
1052 	else if (!rx_pause && tx_pause)
1053 		priv->flow_ctrl = FLOW_TX;
1054 	else
1055 		priv->flow_ctrl = FLOW_OFF;
1056 
1057 	stmmac_mac_flow_ctrl(priv, duplex);
1058 
1059 	writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
1060 
1061 	stmmac_mac_set(priv, priv->ioaddr, true);
1062 	if (phy && priv->dma_cap.eee) {
1063 		priv->eee_active = phy_init_eee(phy, 1) >= 0;
1064 		priv->eee_enabled = stmmac_eee_init(priv);
1065 		priv->tx_lpi_enabled = priv->eee_enabled;
1066 		stmmac_set_eee_pls(priv, priv->hw, true);
1067 	}
1068 }
1069 
1070 static const struct phylink_mac_ops stmmac_phylink_mac_ops = {
1071 	.validate = stmmac_validate,
1072 	.mac_pcs_get_state = stmmac_mac_pcs_get_state,
1073 	.mac_config = stmmac_mac_config,
1074 	.mac_an_restart = stmmac_mac_an_restart,
1075 	.mac_link_down = stmmac_mac_link_down,
1076 	.mac_link_up = stmmac_mac_link_up,
1077 };
1078 
1079 /**
1080  * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
1081  * @priv: driver private structure
1082  * Description: this is to verify if the HW supports the PCS.
1083  * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
1084  * configured for the TBI, RTBI, or SGMII PHY interface.
1085  */
stmmac_check_pcs_mode(struct stmmac_priv * priv)1086 static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
1087 {
1088 	int interface = priv->plat->interface;
1089 
1090 	if (priv->dma_cap.pcs) {
1091 		if ((interface == PHY_INTERFACE_MODE_RGMII) ||
1092 		    (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
1093 		    (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
1094 		    (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
1095 			netdev_dbg(priv->dev, "PCS RGMII support enabled\n");
1096 			priv->hw->pcs = STMMAC_PCS_RGMII;
1097 		} else if (interface == PHY_INTERFACE_MODE_SGMII) {
1098 			netdev_dbg(priv->dev, "PCS SGMII support enabled\n");
1099 			priv->hw->pcs = STMMAC_PCS_SGMII;
1100 		}
1101 	}
1102 }
1103 
1104 /**
1105  * stmmac_init_phy - PHY initialization
1106  * @dev: net device structure
1107  * Description: it initializes the driver's PHY state, and attaches the PHY
1108  * to the mac driver.
1109  *  Return value:
1110  *  0 on success
1111  */
stmmac_init_phy(struct net_device * dev)1112 static int stmmac_init_phy(struct net_device *dev)
1113 {
1114 	struct stmmac_priv *priv = netdev_priv(dev);
1115 	struct device_node *node;
1116 	int ret;
1117 
1118 	if (priv->plat->integrated_phy_power)
1119 		ret = priv->plat->integrated_phy_power(priv->plat->bsp_priv, true);
1120 
1121 	node = priv->plat->phylink_node;
1122 
1123 	if (node)
1124 		ret = phylink_of_phy_connect(priv->phylink, node, 0);
1125 
1126 	/* Some DT bindings do not set-up the PHY handle. Let's try to
1127 	 * manually parse it
1128 	 */
1129 	if (!node || ret) {
1130 		int addr = priv->plat->phy_addr;
1131 		struct phy_device *phydev;
1132 
1133 		phydev = mdiobus_get_phy(priv->mii, addr);
1134 		if (!phydev) {
1135 			netdev_err(priv->dev, "no phy at addr %d\n", addr);
1136 			return -ENODEV;
1137 		}
1138 
1139 		ret = phylink_connect_phy(priv->phylink, phydev);
1140 	}
1141 
1142 	if (!priv->plat->pmt) {
1143 		struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1144 
1145 		phylink_ethtool_get_wol(priv->phylink, &wol);
1146 		device_set_wakeup_capable(priv->device, !!wol.supported);
1147 	}
1148 
1149 	return ret;
1150 }
1151 
stmmac_phy_setup(struct stmmac_priv * priv)1152 static int stmmac_phy_setup(struct stmmac_priv *priv)
1153 {
1154 	struct fwnode_handle *fwnode = of_fwnode_handle(priv->plat->phylink_node);
1155 	int mode = priv->plat->phy_interface;
1156 	struct phylink *phylink;
1157 
1158 	priv->phylink_config.dev = &priv->dev->dev;
1159 	priv->phylink_config.type = PHYLINK_NETDEV;
1160 	priv->phylink_config.pcs_poll = true;
1161 
1162 	if (!fwnode)
1163 		fwnode = dev_fwnode(priv->device);
1164 
1165 	phylink = phylink_create(&priv->phylink_config, fwnode,
1166 				 mode, &stmmac_phylink_mac_ops);
1167 	if (IS_ERR(phylink))
1168 		return PTR_ERR(phylink);
1169 
1170 	priv->phylink = phylink;
1171 	return 0;
1172 }
1173 
stmmac_display_rx_rings(struct stmmac_priv * priv)1174 static void stmmac_display_rx_rings(struct stmmac_priv *priv)
1175 {
1176 	u32 rx_cnt = priv->plat->rx_queues_to_use;
1177 	unsigned int desc_size;
1178 	void *head_rx;
1179 	u32 queue;
1180 
1181 	/* Display RX rings */
1182 	for (queue = 0; queue < rx_cnt; queue++) {
1183 		struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1184 
1185 		pr_info("\tRX Queue %u rings\n", queue);
1186 
1187 		if (priv->extend_desc) {
1188 			head_rx = (void *)rx_q->dma_erx;
1189 			desc_size = sizeof(struct dma_extended_desc);
1190 		} else {
1191 			head_rx = (void *)rx_q->dma_rx;
1192 			desc_size = sizeof(struct dma_desc);
1193 		}
1194 
1195 		/* Display RX ring */
1196 		stmmac_display_ring(priv, head_rx, priv->dma_rx_size, true,
1197 				    rx_q->dma_rx_phy, desc_size);
1198 	}
1199 }
1200 
stmmac_display_tx_rings(struct stmmac_priv * priv)1201 static void stmmac_display_tx_rings(struct stmmac_priv *priv)
1202 {
1203 	u32 tx_cnt = priv->plat->tx_queues_to_use;
1204 	unsigned int desc_size;
1205 	void *head_tx;
1206 	u32 queue;
1207 
1208 	/* Display TX rings */
1209 	for (queue = 0; queue < tx_cnt; queue++) {
1210 		struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1211 
1212 		pr_info("\tTX Queue %d rings\n", queue);
1213 
1214 		if (priv->extend_desc) {
1215 			head_tx = (void *)tx_q->dma_etx;
1216 			desc_size = sizeof(struct dma_extended_desc);
1217 		} else if (tx_q->tbs & STMMAC_TBS_AVAIL) {
1218 			head_tx = (void *)tx_q->dma_entx;
1219 			desc_size = sizeof(struct dma_edesc);
1220 		} else {
1221 			head_tx = (void *)tx_q->dma_tx;
1222 			desc_size = sizeof(struct dma_desc);
1223 		}
1224 
1225 		stmmac_display_ring(priv, head_tx, priv->dma_tx_size, false,
1226 				    tx_q->dma_tx_phy, desc_size);
1227 	}
1228 }
1229 
stmmac_display_rings(struct stmmac_priv * priv)1230 static void stmmac_display_rings(struct stmmac_priv *priv)
1231 {
1232 	/* Display RX ring */
1233 	stmmac_display_rx_rings(priv);
1234 
1235 	/* Display TX ring */
1236 	stmmac_display_tx_rings(priv);
1237 }
1238 
stmmac_set_bfsize(int mtu,int bufsize)1239 static int stmmac_set_bfsize(int mtu, int bufsize)
1240 {
1241 	int ret = bufsize;
1242 
1243 	if (mtu >= BUF_SIZE_8KiB)
1244 		ret = BUF_SIZE_16KiB;
1245 	else if (mtu >= BUF_SIZE_4KiB)
1246 		ret = BUF_SIZE_8KiB;
1247 	else if (mtu >= BUF_SIZE_2KiB)
1248 		ret = BUF_SIZE_4KiB;
1249 	else if (mtu > DEFAULT_BUFSIZE)
1250 		ret = BUF_SIZE_2KiB;
1251 	else
1252 		ret = DEFAULT_BUFSIZE;
1253 
1254 	return ret;
1255 }
1256 
1257 /**
1258  * stmmac_clear_rx_descriptors - clear RX descriptors
1259  * @priv: driver private structure
1260  * @queue: RX queue index
1261  * Description: this function is called to clear the RX descriptors
1262  * in case of both basic and extended descriptors are used.
1263  */
stmmac_clear_rx_descriptors(struct stmmac_priv * priv,u32 queue)1264 static void stmmac_clear_rx_descriptors(struct stmmac_priv *priv, u32 queue)
1265 {
1266 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1267 	int i;
1268 
1269 	/* Clear the RX descriptors */
1270 	for (i = 0; i < priv->dma_rx_size; i++)
1271 		if (priv->extend_desc)
1272 			stmmac_init_rx_desc(priv, &rx_q->dma_erx[i].basic,
1273 					priv->use_riwt, priv->mode,
1274 					(i == priv->dma_rx_size - 1),
1275 					priv->dma_buf_sz);
1276 		else
1277 			stmmac_init_rx_desc(priv, &rx_q->dma_rx[i],
1278 					priv->use_riwt, priv->mode,
1279 					(i == priv->dma_rx_size - 1),
1280 					priv->dma_buf_sz);
1281 }
1282 
1283 /**
1284  * stmmac_clear_tx_descriptors - clear tx descriptors
1285  * @priv: driver private structure
1286  * @queue: TX queue index.
1287  * Description: this function is called to clear the TX descriptors
1288  * in case of both basic and extended descriptors are used.
1289  */
stmmac_clear_tx_descriptors(struct stmmac_priv * priv,u32 queue)1290 static void stmmac_clear_tx_descriptors(struct stmmac_priv *priv, u32 queue)
1291 {
1292 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1293 	int i;
1294 
1295 	/* Clear the TX descriptors */
1296 	for (i = 0; i < priv->dma_tx_size; i++) {
1297 		int last = (i == (priv->dma_tx_size - 1));
1298 		struct dma_desc *p;
1299 
1300 		if (priv->extend_desc)
1301 			p = &tx_q->dma_etx[i].basic;
1302 		else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1303 			p = &tx_q->dma_entx[i].basic;
1304 		else
1305 			p = &tx_q->dma_tx[i];
1306 
1307 		stmmac_init_tx_desc(priv, p, priv->mode, last);
1308 	}
1309 }
1310 
1311 /**
1312  * stmmac_clear_descriptors - clear descriptors
1313  * @priv: driver private structure
1314  * Description: this function is called to clear the TX and RX descriptors
1315  * in case of both basic and extended descriptors are used.
1316  */
stmmac_clear_descriptors(struct stmmac_priv * priv)1317 static void stmmac_clear_descriptors(struct stmmac_priv *priv)
1318 {
1319 	u32 rx_queue_cnt = priv->plat->rx_queues_to_use;
1320 	u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1321 	u32 queue;
1322 
1323 	/* Clear the RX descriptors */
1324 	for (queue = 0; queue < rx_queue_cnt; queue++)
1325 		stmmac_clear_rx_descriptors(priv, queue);
1326 
1327 	/* Clear the TX descriptors */
1328 	for (queue = 0; queue < tx_queue_cnt; queue++)
1329 		stmmac_clear_tx_descriptors(priv, queue);
1330 }
1331 
1332 /**
1333  * stmmac_init_rx_buffers - init the RX descriptor buffer.
1334  * @priv: driver private structure
1335  * @p: descriptor pointer
1336  * @i: descriptor index
1337  * @flags: gfp flag
1338  * @queue: RX queue index
1339  * Description: this function is called to allocate a receive buffer, perform
1340  * the DMA mapping and init the descriptor.
1341  */
stmmac_init_rx_buffers(struct stmmac_priv * priv,struct dma_desc * p,int i,gfp_t flags,u32 queue)1342 static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
1343 				  int i, gfp_t flags, u32 queue)
1344 {
1345 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1346 	struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1347 	gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
1348 
1349 	if (priv->dma_cap.addr64 <= 32)
1350 		gfp |= GFP_DMA32;
1351 
1352 	buf->page = page_pool_alloc_pages(rx_q->page_pool, gfp);
1353 	if (!buf->page)
1354 		return -ENOMEM;
1355 
1356 	if (priv->sph) {
1357 		buf->sec_page = page_pool_alloc_pages(rx_q->page_pool, gfp);
1358 		if (!buf->sec_page)
1359 			return -ENOMEM;
1360 
1361 		buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
1362 		stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true);
1363 	} else {
1364 		buf->sec_page = NULL;
1365 		stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false);
1366 	}
1367 
1368 	buf->addr = page_pool_get_dma_addr(buf->page);
1369 	stmmac_set_desc_addr(priv, p, buf->addr);
1370 	if (priv->dma_buf_sz == BUF_SIZE_16KiB)
1371 		stmmac_init_desc3(priv, p);
1372 
1373 	return 0;
1374 }
1375 
1376 /**
1377  * stmmac_free_rx_buffer - free RX dma buffers
1378  * @priv: private structure
1379  * @queue: RX queue index
1380  * @i: buffer index.
1381  */
stmmac_free_rx_buffer(struct stmmac_priv * priv,u32 queue,int i)1382 static void stmmac_free_rx_buffer(struct stmmac_priv *priv, u32 queue, int i)
1383 {
1384 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1385 	struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1386 
1387 	if (buf->page)
1388 		page_pool_put_full_page(rx_q->page_pool, buf->page, false);
1389 	buf->page = NULL;
1390 
1391 	if (buf->sec_page)
1392 		page_pool_put_full_page(rx_q->page_pool, buf->sec_page, false);
1393 	buf->sec_page = NULL;
1394 }
1395 
1396 /**
1397  * stmmac_free_tx_buffer - free RX dma buffers
1398  * @priv: private structure
1399  * @queue: RX queue index
1400  * @i: buffer index.
1401  */
stmmac_free_tx_buffer(struct stmmac_priv * priv,u32 queue,int i)1402 static void stmmac_free_tx_buffer(struct stmmac_priv *priv, u32 queue, int i)
1403 {
1404 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1405 
1406 	if (tx_q->tx_skbuff_dma[i].buf) {
1407 		if (tx_q->tx_skbuff_dma[i].map_as_page)
1408 			dma_unmap_page(priv->device,
1409 				       tx_q->tx_skbuff_dma[i].buf,
1410 				       tx_q->tx_skbuff_dma[i].len,
1411 				       DMA_TO_DEVICE);
1412 		else
1413 			dma_unmap_single(priv->device,
1414 					 tx_q->tx_skbuff_dma[i].buf,
1415 					 tx_q->tx_skbuff_dma[i].len,
1416 					 DMA_TO_DEVICE);
1417 	}
1418 
1419 	if (tx_q->tx_skbuff[i]) {
1420 		dev_kfree_skb_any(tx_q->tx_skbuff[i]);
1421 		tx_q->tx_skbuff[i] = NULL;
1422 		tx_q->tx_skbuff_dma[i].buf = 0;
1423 		tx_q->tx_skbuff_dma[i].map_as_page = false;
1424 	}
1425 }
1426 
1427 /**
1428  * init_dma_rx_desc_rings - init the RX descriptor rings
1429  * @dev: net device structure
1430  * @flags: gfp flag.
1431  * Description: this function initializes the DMA RX descriptors
1432  * and allocates the socket buffers. It supports the chained and ring
1433  * modes.
1434  */
init_dma_rx_desc_rings(struct net_device * dev,gfp_t flags)1435 static int init_dma_rx_desc_rings(struct net_device *dev, gfp_t flags)
1436 {
1437 	struct stmmac_priv *priv = netdev_priv(dev);
1438 	u32 rx_count = priv->plat->rx_queues_to_use;
1439 	int ret = -ENOMEM;
1440 	int queue;
1441 	int i;
1442 
1443 	/* RX INITIALIZATION */
1444 	netif_dbg(priv, probe, priv->dev,
1445 		  "SKB addresses:\nskb\t\tskb data\tdma data\n");
1446 
1447 	for (queue = 0; queue < rx_count; queue++) {
1448 		struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1449 
1450 		netif_dbg(priv, probe, priv->dev,
1451 			  "(%s) dma_rx_phy=0x%08x\n", __func__,
1452 			  (u32)rx_q->dma_rx_phy);
1453 
1454 		stmmac_clear_rx_descriptors(priv, queue);
1455 
1456 		for (i = 0; i < priv->dma_rx_size; i++) {
1457 			struct dma_desc *p;
1458 
1459 			if (priv->extend_desc)
1460 				p = &((rx_q->dma_erx + i)->basic);
1461 			else
1462 				p = rx_q->dma_rx + i;
1463 
1464 			ret = stmmac_init_rx_buffers(priv, p, i, flags,
1465 						     queue);
1466 			if (ret)
1467 				goto err_init_rx_buffers;
1468 		}
1469 
1470 		rx_q->cur_rx = 0;
1471 		rx_q->dirty_rx = (unsigned int)(i - priv->dma_rx_size);
1472 
1473 		/* Setup the chained descriptor addresses */
1474 		if (priv->mode == STMMAC_CHAIN_MODE) {
1475 			if (priv->extend_desc)
1476 				stmmac_mode_init(priv, rx_q->dma_erx,
1477 						 rx_q->dma_rx_phy,
1478 						 priv->dma_rx_size, 1);
1479 			else
1480 				stmmac_mode_init(priv, rx_q->dma_rx,
1481 						 rx_q->dma_rx_phy,
1482 						 priv->dma_rx_size, 0);
1483 		}
1484 	}
1485 
1486 	return 0;
1487 
1488 err_init_rx_buffers:
1489 	while (queue >= 0) {
1490 		while (--i >= 0)
1491 			stmmac_free_rx_buffer(priv, queue, i);
1492 
1493 		if (queue == 0)
1494 			break;
1495 
1496 		i = priv->dma_rx_size;
1497 		queue--;
1498 	}
1499 
1500 	return ret;
1501 }
1502 
1503 /**
1504  * init_dma_tx_desc_rings - init the TX descriptor rings
1505  * @dev: net device structure.
1506  * Description: this function initializes the DMA TX descriptors
1507  * and allocates the socket buffers. It supports the chained and ring
1508  * modes.
1509  */
init_dma_tx_desc_rings(struct net_device * dev)1510 static int init_dma_tx_desc_rings(struct net_device *dev)
1511 {
1512 	struct stmmac_priv *priv = netdev_priv(dev);
1513 	u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1514 	u32 queue;
1515 	int i;
1516 
1517 	for (queue = 0; queue < tx_queue_cnt; queue++) {
1518 		struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1519 
1520 		netif_dbg(priv, probe, priv->dev,
1521 			  "(%s) dma_tx_phy=0x%08x\n", __func__,
1522 			 (u32)tx_q->dma_tx_phy);
1523 
1524 		/* Setup the chained descriptor addresses */
1525 		if (priv->mode == STMMAC_CHAIN_MODE) {
1526 			if (priv->extend_desc)
1527 				stmmac_mode_init(priv, tx_q->dma_etx,
1528 						 tx_q->dma_tx_phy,
1529 						 priv->dma_tx_size, 1);
1530 			else if (!(tx_q->tbs & STMMAC_TBS_AVAIL))
1531 				stmmac_mode_init(priv, tx_q->dma_tx,
1532 						 tx_q->dma_tx_phy,
1533 						 priv->dma_tx_size, 0);
1534 		}
1535 
1536 		for (i = 0; i < priv->dma_tx_size; i++) {
1537 			struct dma_desc *p;
1538 			if (priv->extend_desc)
1539 				p = &((tx_q->dma_etx + i)->basic);
1540 			else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1541 				p = &((tx_q->dma_entx + i)->basic);
1542 			else
1543 				p = tx_q->dma_tx + i;
1544 
1545 			stmmac_clear_desc(priv, p);
1546 
1547 			tx_q->tx_skbuff_dma[i].buf = 0;
1548 			tx_q->tx_skbuff_dma[i].map_as_page = false;
1549 			tx_q->tx_skbuff_dma[i].len = 0;
1550 			tx_q->tx_skbuff_dma[i].last_segment = false;
1551 			tx_q->tx_skbuff[i] = NULL;
1552 		}
1553 
1554 		tx_q->dirty_tx = 0;
1555 		tx_q->cur_tx = 0;
1556 		tx_q->mss = 0;
1557 
1558 		netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
1559 	}
1560 
1561 	return 0;
1562 }
1563 
1564 /**
1565  * init_dma_desc_rings - init the RX/TX descriptor rings
1566  * @dev: net device structure
1567  * @flags: gfp flag.
1568  * Description: this function initializes the DMA RX/TX descriptors
1569  * and allocates the socket buffers. It supports the chained and ring
1570  * modes.
1571  */
init_dma_desc_rings(struct net_device * dev,gfp_t flags)1572 static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
1573 {
1574 	struct stmmac_priv *priv = netdev_priv(dev);
1575 	int ret;
1576 
1577 	ret = init_dma_rx_desc_rings(dev, flags);
1578 	if (ret)
1579 		return ret;
1580 
1581 	ret = init_dma_tx_desc_rings(dev);
1582 
1583 	stmmac_clear_descriptors(priv);
1584 
1585 	if (netif_msg_hw(priv))
1586 		stmmac_display_rings(priv);
1587 
1588 	return ret;
1589 }
1590 
1591 /**
1592  * dma_free_rx_skbufs - free RX dma buffers
1593  * @priv: private structure
1594  * @queue: RX queue index
1595  */
dma_free_rx_skbufs(struct stmmac_priv * priv,u32 queue)1596 static void dma_free_rx_skbufs(struct stmmac_priv *priv, u32 queue)
1597 {
1598 	int i;
1599 
1600 	for (i = 0; i < priv->dma_rx_size; i++)
1601 		stmmac_free_rx_buffer(priv, queue, i);
1602 }
1603 
1604 /**
1605  * dma_free_tx_skbufs - free TX dma buffers
1606  * @priv: private structure
1607  * @queue: TX queue index
1608  */
dma_free_tx_skbufs(struct stmmac_priv * priv,u32 queue)1609 static void dma_free_tx_skbufs(struct stmmac_priv *priv, u32 queue)
1610 {
1611 	int i;
1612 
1613 	for (i = 0; i < priv->dma_tx_size; i++)
1614 		stmmac_free_tx_buffer(priv, queue, i);
1615 }
1616 
1617 /**
1618  * stmmac_free_tx_skbufs - free TX skb buffers
1619  * @priv: private structure
1620  */
stmmac_free_tx_skbufs(struct stmmac_priv * priv)1621 static void stmmac_free_tx_skbufs(struct stmmac_priv *priv)
1622 {
1623 	u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1624 	u32 queue;
1625 
1626 	for (queue = 0; queue < tx_queue_cnt; queue++)
1627 		dma_free_tx_skbufs(priv, queue);
1628 }
1629 
1630 /**
1631  * free_dma_rx_desc_resources - free RX dma desc resources
1632  * @priv: private structure
1633  */
free_dma_rx_desc_resources(struct stmmac_priv * priv)1634 static void free_dma_rx_desc_resources(struct stmmac_priv *priv)
1635 {
1636 	u32 rx_count = priv->plat->rx_queues_to_use;
1637 	u32 queue;
1638 
1639 	/* Free RX queue resources */
1640 	for (queue = 0; queue < rx_count; queue++) {
1641 		struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1642 
1643 		/* Release the DMA RX socket buffers */
1644 		dma_free_rx_skbufs(priv, queue);
1645 
1646 		/* Free DMA regions of consistent memory previously allocated */
1647 		if (!priv->extend_desc)
1648 			dma_free_coherent(priv->device, priv->dma_rx_size *
1649 					  sizeof(struct dma_desc),
1650 					  rx_q->dma_rx, rx_q->dma_rx_phy);
1651 		else
1652 			dma_free_coherent(priv->device, priv->dma_rx_size *
1653 					  sizeof(struct dma_extended_desc),
1654 					  rx_q->dma_erx, rx_q->dma_rx_phy);
1655 
1656 		kfree(rx_q->buf_pool);
1657 		if (rx_q->page_pool)
1658 			page_pool_destroy(rx_q->page_pool);
1659 	}
1660 }
1661 
1662 /**
1663  * free_dma_tx_desc_resources - free TX dma desc resources
1664  * @priv: private structure
1665  */
free_dma_tx_desc_resources(struct stmmac_priv * priv)1666 static void free_dma_tx_desc_resources(struct stmmac_priv *priv)
1667 {
1668 	u32 tx_count = priv->plat->tx_queues_to_use;
1669 	u32 queue;
1670 
1671 	/* Free TX queue resources */
1672 	for (queue = 0; queue < tx_count; queue++) {
1673 		struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1674 		size_t size;
1675 		void *addr;
1676 
1677 		/* Release the DMA TX socket buffers */
1678 		dma_free_tx_skbufs(priv, queue);
1679 
1680 		if (priv->extend_desc) {
1681 			size = sizeof(struct dma_extended_desc);
1682 			addr = tx_q->dma_etx;
1683 		} else if (tx_q->tbs & STMMAC_TBS_AVAIL) {
1684 			size = sizeof(struct dma_edesc);
1685 			addr = tx_q->dma_entx;
1686 		} else {
1687 			size = sizeof(struct dma_desc);
1688 			addr = tx_q->dma_tx;
1689 		}
1690 
1691 		size *= priv->dma_tx_size;
1692 
1693 		dma_free_coherent(priv->device, size, addr, tx_q->dma_tx_phy);
1694 
1695 		kfree(tx_q->tx_skbuff_dma);
1696 		kfree(tx_q->tx_skbuff);
1697 	}
1698 }
1699 
1700 /**
1701  * alloc_dma_rx_desc_resources - alloc RX resources.
1702  * @priv: private structure
1703  * Description: according to which descriptor can be used (extend or basic)
1704  * this function allocates the resources for TX and RX paths. In case of
1705  * reception, for example, it pre-allocated the RX socket buffer in order to
1706  * allow zero-copy mechanism.
1707  */
alloc_dma_rx_desc_resources(struct stmmac_priv * priv)1708 static int alloc_dma_rx_desc_resources(struct stmmac_priv *priv)
1709 {
1710 	u32 rx_count = priv->plat->rx_queues_to_use;
1711 	int ret = -ENOMEM;
1712 	u32 queue;
1713 
1714 	/* RX queues buffers and DMA */
1715 	for (queue = 0; queue < rx_count; queue++) {
1716 		struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1717 		struct page_pool_params pp_params = { 0 };
1718 		unsigned int num_pages;
1719 
1720 		rx_q->queue_index = queue;
1721 		rx_q->priv_data = priv;
1722 
1723 		pp_params.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
1724 		pp_params.pool_size = priv->dma_rx_size;
1725 		num_pages = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE);
1726 		pp_params.order = ilog2(num_pages);
1727 		pp_params.nid = dev_to_node(priv->device);
1728 		pp_params.dev = priv->device;
1729 		pp_params.dma_dir = DMA_FROM_DEVICE;
1730 		pp_params.max_len = num_pages * PAGE_SIZE;
1731 
1732 		rx_q->page_pool = page_pool_create(&pp_params);
1733 		if (IS_ERR(rx_q->page_pool)) {
1734 			ret = PTR_ERR(rx_q->page_pool);
1735 			rx_q->page_pool = NULL;
1736 			goto err_dma;
1737 		}
1738 
1739 		rx_q->buf_pool = kcalloc(priv->dma_rx_size,
1740 					 sizeof(*rx_q->buf_pool),
1741 					 GFP_KERNEL);
1742 		if (!rx_q->buf_pool)
1743 			goto err_dma;
1744 
1745 		if (priv->extend_desc) {
1746 			rx_q->dma_erx = dma_alloc_coherent(priv->device,
1747 							   priv->dma_rx_size *
1748 							   sizeof(struct dma_extended_desc),
1749 							   &rx_q->dma_rx_phy,
1750 							   GFP_KERNEL);
1751 			if (!rx_q->dma_erx)
1752 				goto err_dma;
1753 
1754 		} else {
1755 			rx_q->dma_rx = dma_alloc_coherent(priv->device,
1756 							  priv->dma_rx_size *
1757 							  sizeof(struct dma_desc),
1758 							  &rx_q->dma_rx_phy,
1759 							  GFP_KERNEL);
1760 			if (!rx_q->dma_rx)
1761 				goto err_dma;
1762 		}
1763 	}
1764 
1765 	return 0;
1766 
1767 err_dma:
1768 	free_dma_rx_desc_resources(priv);
1769 
1770 	return ret;
1771 }
1772 
1773 /**
1774  * alloc_dma_tx_desc_resources - alloc TX resources.
1775  * @priv: private structure
1776  * Description: according to which descriptor can be used (extend or basic)
1777  * this function allocates the resources for TX and RX paths. In case of
1778  * reception, for example, it pre-allocated the RX socket buffer in order to
1779  * allow zero-copy mechanism.
1780  */
alloc_dma_tx_desc_resources(struct stmmac_priv * priv)1781 static int alloc_dma_tx_desc_resources(struct stmmac_priv *priv)
1782 {
1783 	u32 tx_count = priv->plat->tx_queues_to_use;
1784 	int ret = -ENOMEM;
1785 	u32 queue;
1786 
1787 	/* TX queues buffers and DMA */
1788 	for (queue = 0; queue < tx_count; queue++) {
1789 		struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1790 		size_t size;
1791 		void *addr;
1792 
1793 		tx_q->queue_index = queue;
1794 		tx_q->priv_data = priv;
1795 
1796 		tx_q->tx_skbuff_dma = kcalloc(priv->dma_tx_size,
1797 					      sizeof(*tx_q->tx_skbuff_dma),
1798 					      GFP_KERNEL);
1799 		if (!tx_q->tx_skbuff_dma)
1800 			goto err_dma;
1801 
1802 		tx_q->tx_skbuff = kcalloc(priv->dma_tx_size,
1803 					  sizeof(struct sk_buff *),
1804 					  GFP_KERNEL);
1805 		if (!tx_q->tx_skbuff)
1806 			goto err_dma;
1807 
1808 		if (priv->extend_desc)
1809 			size = sizeof(struct dma_extended_desc);
1810 		else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1811 			size = sizeof(struct dma_edesc);
1812 		else
1813 			size = sizeof(struct dma_desc);
1814 
1815 		size *= priv->dma_tx_size;
1816 
1817 		addr = dma_alloc_coherent(priv->device, size,
1818 					  &tx_q->dma_tx_phy, GFP_KERNEL);
1819 		if (!addr)
1820 			goto err_dma;
1821 
1822 		if (priv->extend_desc)
1823 			tx_q->dma_etx = addr;
1824 		else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1825 			tx_q->dma_entx = addr;
1826 		else
1827 			tx_q->dma_tx = addr;
1828 	}
1829 
1830 	return 0;
1831 
1832 err_dma:
1833 	free_dma_tx_desc_resources(priv);
1834 	return ret;
1835 }
1836 
1837 /**
1838  * alloc_dma_desc_resources - alloc TX/RX resources.
1839  * @priv: private structure
1840  * Description: according to which descriptor can be used (extend or basic)
1841  * this function allocates the resources for TX and RX paths. In case of
1842  * reception, for example, it pre-allocated the RX socket buffer in order to
1843  * allow zero-copy mechanism.
1844  */
alloc_dma_desc_resources(struct stmmac_priv * priv)1845 static int alloc_dma_desc_resources(struct stmmac_priv *priv)
1846 {
1847 	/* RX Allocation */
1848 	int ret = alloc_dma_rx_desc_resources(priv);
1849 
1850 	if (ret)
1851 		return ret;
1852 
1853 	ret = alloc_dma_tx_desc_resources(priv);
1854 
1855 	return ret;
1856 }
1857 
1858 /**
1859  * free_dma_desc_resources - free dma desc resources
1860  * @priv: private structure
1861  */
free_dma_desc_resources(struct stmmac_priv * priv)1862 static void free_dma_desc_resources(struct stmmac_priv *priv)
1863 {
1864 	/* Release the DMA RX socket buffers */
1865 	free_dma_rx_desc_resources(priv);
1866 
1867 	/* Release the DMA TX socket buffers */
1868 	free_dma_tx_desc_resources(priv);
1869 }
1870 
1871 /**
1872  *  stmmac_mac_enable_rx_queues - Enable MAC rx queues
1873  *  @priv: driver private structure
1874  *  Description: It is used for enabling the rx queues in the MAC
1875  */
stmmac_mac_enable_rx_queues(struct stmmac_priv * priv)1876 static void stmmac_mac_enable_rx_queues(struct stmmac_priv *priv)
1877 {
1878 	u32 rx_queues_count = priv->plat->rx_queues_to_use;
1879 	int queue;
1880 	u8 mode;
1881 
1882 	for (queue = 0; queue < rx_queues_count; queue++) {
1883 		mode = priv->plat->rx_queues_cfg[queue].mode_to_use;
1884 		stmmac_rx_queue_enable(priv, priv->hw, mode, queue);
1885 	}
1886 }
1887 
1888 /**
1889  * stmmac_start_rx_dma - start RX DMA channel
1890  * @priv: driver private structure
1891  * @chan: RX channel index
1892  * Description:
1893  * This starts a RX DMA channel
1894  */
stmmac_start_rx_dma(struct stmmac_priv * priv,u32 chan)1895 static void stmmac_start_rx_dma(struct stmmac_priv *priv, u32 chan)
1896 {
1897 	netdev_dbg(priv->dev, "DMA RX processes started in channel %d\n", chan);
1898 	stmmac_start_rx(priv, priv->ioaddr, chan);
1899 }
1900 
1901 /**
1902  * stmmac_start_tx_dma - start TX DMA channel
1903  * @priv: driver private structure
1904  * @chan: TX channel index
1905  * Description:
1906  * This starts a TX DMA channel
1907  */
stmmac_start_tx_dma(struct stmmac_priv * priv,u32 chan)1908 static void stmmac_start_tx_dma(struct stmmac_priv *priv, u32 chan)
1909 {
1910 	netdev_dbg(priv->dev, "DMA TX processes started in channel %d\n", chan);
1911 	stmmac_start_tx(priv, priv->ioaddr, chan);
1912 }
1913 
1914 /**
1915  * stmmac_stop_rx_dma - stop RX DMA channel
1916  * @priv: driver private structure
1917  * @chan: RX channel index
1918  * Description:
1919  * This stops a RX DMA channel
1920  */
stmmac_stop_rx_dma(struct stmmac_priv * priv,u32 chan)1921 static void stmmac_stop_rx_dma(struct stmmac_priv *priv, u32 chan)
1922 {
1923 	netdev_dbg(priv->dev, "DMA RX processes stopped in channel %d\n", chan);
1924 	stmmac_stop_rx(priv, priv->ioaddr, chan);
1925 }
1926 
1927 /**
1928  * stmmac_stop_tx_dma - stop TX DMA channel
1929  * @priv: driver private structure
1930  * @chan: TX channel index
1931  * Description:
1932  * This stops a TX DMA channel
1933  */
stmmac_stop_tx_dma(struct stmmac_priv * priv,u32 chan)1934 static void stmmac_stop_tx_dma(struct stmmac_priv *priv, u32 chan)
1935 {
1936 	netdev_dbg(priv->dev, "DMA TX processes stopped in channel %d\n", chan);
1937 	stmmac_stop_tx(priv, priv->ioaddr, chan);
1938 }
1939 
1940 /**
1941  * stmmac_start_all_dma - start all RX and TX DMA channels
1942  * @priv: driver private structure
1943  * Description:
1944  * This starts all the RX and TX DMA channels
1945  */
stmmac_start_all_dma(struct stmmac_priv * priv)1946 static void stmmac_start_all_dma(struct stmmac_priv *priv)
1947 {
1948 	u32 rx_channels_count = priv->plat->rx_queues_to_use;
1949 	u32 tx_channels_count = priv->plat->tx_queues_to_use;
1950 	u32 chan = 0;
1951 
1952 	for (chan = 0; chan < rx_channels_count; chan++)
1953 		stmmac_start_rx_dma(priv, chan);
1954 
1955 	for (chan = 0; chan < tx_channels_count; chan++)
1956 		stmmac_start_tx_dma(priv, chan);
1957 }
1958 
1959 /**
1960  * stmmac_stop_all_dma - stop all RX and TX DMA channels
1961  * @priv: driver private structure
1962  * Description:
1963  * This stops the RX and TX DMA channels
1964  */
stmmac_stop_all_dma(struct stmmac_priv * priv)1965 static void stmmac_stop_all_dma(struct stmmac_priv *priv)
1966 {
1967 	u32 rx_channels_count = priv->plat->rx_queues_to_use;
1968 	u32 tx_channels_count = priv->plat->tx_queues_to_use;
1969 	u32 chan = 0;
1970 
1971 	for (chan = 0; chan < rx_channels_count; chan++)
1972 		stmmac_stop_rx_dma(priv, chan);
1973 
1974 	for (chan = 0; chan < tx_channels_count; chan++)
1975 		stmmac_stop_tx_dma(priv, chan);
1976 }
1977 
1978 /**
1979  *  stmmac_dma_operation_mode - HW DMA operation mode
1980  *  @priv: driver private structure
1981  *  Description: it is used for configuring the DMA operation mode register in
1982  *  order to program the tx/rx DMA thresholds or Store-And-Forward mode.
1983  */
stmmac_dma_operation_mode(struct stmmac_priv * priv)1984 static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
1985 {
1986 	u32 rx_channels_count = priv->plat->rx_queues_to_use;
1987 	u32 tx_channels_count = priv->plat->tx_queues_to_use;
1988 	int rxfifosz = priv->plat->rx_fifo_size;
1989 	int txfifosz = priv->plat->tx_fifo_size;
1990 	u32 txmode = 0;
1991 	u32 rxmode = 0;
1992 	u32 chan = 0;
1993 	u8 qmode = 0;
1994 
1995 	if (rxfifosz == 0)
1996 		rxfifosz = priv->dma_cap.rx_fifo_size;
1997 	if (txfifosz == 0)
1998 		txfifosz = priv->dma_cap.tx_fifo_size;
1999 
2000 	/* Adjust for real per queue fifo size */
2001 	rxfifosz /= rx_channels_count;
2002 	txfifosz /= tx_channels_count;
2003 
2004 	if (priv->plat->force_thresh_dma_mode) {
2005 		txmode = tc;
2006 		rxmode = tc;
2007 	} else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
2008 		/*
2009 		 * In case of GMAC, SF mode can be enabled
2010 		 * to perform the TX COE in HW. This depends on:
2011 		 * 1) TX COE if actually supported
2012 		 * 2) There is no bugged Jumbo frame support
2013 		 *    that needs to not insert csum in the TDES.
2014 		 */
2015 		txmode = SF_DMA_MODE;
2016 		rxmode = SF_DMA_MODE;
2017 		priv->xstats.threshold = SF_DMA_MODE;
2018 	} else {
2019 		txmode = tc;
2020 		rxmode = SF_DMA_MODE;
2021 	}
2022 
2023 	/* configure all channels */
2024 	for (chan = 0; chan < rx_channels_count; chan++) {
2025 		qmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
2026 
2027 		stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan,
2028 				rxfifosz, qmode);
2029 		stmmac_set_dma_bfsize(priv, priv->ioaddr, priv->dma_buf_sz,
2030 				chan);
2031 	}
2032 
2033 	for (chan = 0; chan < tx_channels_count; chan++) {
2034 		qmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
2035 
2036 		stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan,
2037 				txfifosz, qmode);
2038 	}
2039 }
2040 
2041 /**
2042  * stmmac_tx_clean - to manage the transmission completion
2043  * @priv: driver private structure
2044  * @budget: napi budget limiting this functions packet handling
2045  * @queue: TX queue index
2046  * Description: it reclaims the transmit resources after transmission completes.
2047  */
stmmac_tx_clean(struct stmmac_priv * priv,int budget,u32 queue)2048 static int stmmac_tx_clean(struct stmmac_priv *priv, int budget, u32 queue)
2049 {
2050 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2051 	unsigned int bytes_compl = 0, pkts_compl = 0;
2052 	unsigned int entry, count = 0;
2053 
2054 	__netif_tx_lock_bh(netdev_get_tx_queue(priv->dev, queue));
2055 
2056 	priv->xstats.tx_clean++;
2057 
2058 	entry = tx_q->dirty_tx;
2059 	while ((entry != tx_q->cur_tx) && (count < budget)) {
2060 		struct sk_buff *skb = tx_q->tx_skbuff[entry];
2061 		struct dma_desc *p;
2062 		int status;
2063 
2064 		if (priv->extend_desc)
2065 			p = (struct dma_desc *)(tx_q->dma_etx + entry);
2066 		else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2067 			p = &tx_q->dma_entx[entry].basic;
2068 		else
2069 			p = tx_q->dma_tx + entry;
2070 
2071 		status = stmmac_tx_status(priv, &priv->dev->stats,
2072 				&priv->xstats, p, priv->ioaddr);
2073 		/* Check if the descriptor is owned by the DMA */
2074 		if (unlikely(status & tx_dma_own))
2075 			break;
2076 
2077 		count++;
2078 
2079 		/* Make sure descriptor fields are read after reading
2080 		 * the own bit.
2081 		 */
2082 		dma_rmb();
2083 
2084 		/* Just consider the last segment and ...*/
2085 		if (likely(!(status & tx_not_ls))) {
2086 			/* ... verify the status error condition */
2087 			if (unlikely(status & tx_err)) {
2088 				priv->dev->stats.tx_errors++;
2089 			} else {
2090 				priv->dev->stats.tx_packets++;
2091 				priv->xstats.tx_pkt_n++;
2092 			}
2093 			stmmac_get_tx_hwtstamp(priv, p, skb);
2094 		}
2095 
2096 		if (likely(tx_q->tx_skbuff_dma[entry].buf)) {
2097 			if (tx_q->tx_skbuff_dma[entry].map_as_page)
2098 				dma_unmap_page(priv->device,
2099 					       tx_q->tx_skbuff_dma[entry].buf,
2100 					       tx_q->tx_skbuff_dma[entry].len,
2101 					       DMA_TO_DEVICE);
2102 			else
2103 				dma_unmap_single(priv->device,
2104 						 tx_q->tx_skbuff_dma[entry].buf,
2105 						 tx_q->tx_skbuff_dma[entry].len,
2106 						 DMA_TO_DEVICE);
2107 			tx_q->tx_skbuff_dma[entry].buf = 0;
2108 			tx_q->tx_skbuff_dma[entry].len = 0;
2109 			tx_q->tx_skbuff_dma[entry].map_as_page = false;
2110 		}
2111 
2112 		stmmac_clean_desc3(priv, tx_q, p);
2113 
2114 		tx_q->tx_skbuff_dma[entry].last_segment = false;
2115 		tx_q->tx_skbuff_dma[entry].is_jumbo = false;
2116 
2117 		if (likely(skb != NULL)) {
2118 			pkts_compl++;
2119 			bytes_compl += skb->len;
2120 			dev_consume_skb_any(skb);
2121 			tx_q->tx_skbuff[entry] = NULL;
2122 		}
2123 
2124 		stmmac_release_tx_desc(priv, p, priv->mode);
2125 
2126 		entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
2127 	}
2128 	tx_q->dirty_tx = entry;
2129 
2130 	netdev_tx_completed_queue(netdev_get_tx_queue(priv->dev, queue),
2131 				  pkts_compl, bytes_compl);
2132 
2133 	if (unlikely(netif_tx_queue_stopped(netdev_get_tx_queue(priv->dev,
2134 								queue))) &&
2135 	    stmmac_tx_avail(priv, queue) > STMMAC_TX_THRESH(priv)) {
2136 
2137 		netif_dbg(priv, tx_done, priv->dev,
2138 			  "%s: restart transmit\n", __func__);
2139 		netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, queue));
2140 	}
2141 
2142 	if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
2143 		stmmac_enable_eee_mode(priv);
2144 		mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer));
2145 	}
2146 
2147 	/* We still have pending packets, let's call for a new scheduling */
2148 	if (tx_q->dirty_tx != tx_q->cur_tx)
2149 		mod_timer(&tx_q->txtimer, STMMAC_COAL_TIMER(priv->tx_coal_timer));
2150 
2151 	__netif_tx_unlock_bh(netdev_get_tx_queue(priv->dev, queue));
2152 
2153 	return count;
2154 }
2155 
2156 /**
2157  * stmmac_tx_err - to manage the tx error
2158  * @priv: driver private structure
2159  * @chan: channel index
2160  * Description: it cleans the descriptors and restarts the transmission
2161  * in case of transmission errors.
2162  */
stmmac_tx_err(struct stmmac_priv * priv,u32 chan)2163 static void stmmac_tx_err(struct stmmac_priv *priv, u32 chan)
2164 {
2165 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2166 
2167 	netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, chan));
2168 
2169 	stmmac_stop_tx_dma(priv, chan);
2170 	dma_free_tx_skbufs(priv, chan);
2171 	stmmac_clear_tx_descriptors(priv, chan);
2172 	tx_q->dirty_tx = 0;
2173 	tx_q->cur_tx = 0;
2174 	tx_q->mss = 0;
2175 	netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, chan));
2176 	stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2177 			    tx_q->dma_tx_phy, chan);
2178 	stmmac_start_tx_dma(priv, chan);
2179 
2180 	priv->dev->stats.tx_errors++;
2181 	netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, chan));
2182 }
2183 
2184 /**
2185  *  stmmac_set_dma_operation_mode - Set DMA operation mode by channel
2186  *  @priv: driver private structure
2187  *  @txmode: TX operating mode
2188  *  @rxmode: RX operating mode
2189  *  @chan: channel index
2190  *  Description: it is used for configuring of the DMA operation mode in
2191  *  runtime in order to program the tx/rx DMA thresholds or Store-And-Forward
2192  *  mode.
2193  */
stmmac_set_dma_operation_mode(struct stmmac_priv * priv,u32 txmode,u32 rxmode,u32 chan)2194 static void stmmac_set_dma_operation_mode(struct stmmac_priv *priv, u32 txmode,
2195 					  u32 rxmode, u32 chan)
2196 {
2197 	u8 rxqmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
2198 	u8 txqmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
2199 	u32 rx_channels_count = priv->plat->rx_queues_to_use;
2200 	u32 tx_channels_count = priv->plat->tx_queues_to_use;
2201 	int rxfifosz = priv->plat->rx_fifo_size;
2202 	int txfifosz = priv->plat->tx_fifo_size;
2203 
2204 	if (rxfifosz == 0)
2205 		rxfifosz = priv->dma_cap.rx_fifo_size;
2206 	if (txfifosz == 0)
2207 		txfifosz = priv->dma_cap.tx_fifo_size;
2208 
2209 	/* Adjust for real per queue fifo size */
2210 	rxfifosz /= rx_channels_count;
2211 	txfifosz /= tx_channels_count;
2212 
2213 	stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan, rxfifosz, rxqmode);
2214 	stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan, txfifosz, txqmode);
2215 }
2216 
stmmac_safety_feat_interrupt(struct stmmac_priv * priv)2217 static bool stmmac_safety_feat_interrupt(struct stmmac_priv *priv)
2218 {
2219 	int ret;
2220 
2221 	ret = stmmac_safety_feat_irq_status(priv, priv->dev,
2222 			priv->ioaddr, priv->dma_cap.asp, &priv->sstats);
2223 	if (ret && (ret != -EINVAL)) {
2224 		stmmac_global_err(priv);
2225 		return true;
2226 	}
2227 
2228 	return false;
2229 }
2230 
stmmac_napi_check(struct stmmac_priv * priv,u32 chan)2231 static int stmmac_napi_check(struct stmmac_priv *priv, u32 chan)
2232 {
2233 	int status = stmmac_dma_interrupt_status(priv, priv->ioaddr,
2234 						 &priv->xstats, chan);
2235 	struct stmmac_channel *ch = &priv->channel[chan];
2236 	unsigned long flags;
2237 
2238 	if ((status & handle_rx) && (chan < priv->plat->rx_queues_to_use)) {
2239 		if (napi_schedule_prep(&ch->rx_napi)) {
2240 			spin_lock_irqsave(&ch->lock, flags);
2241 			stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
2242 			spin_unlock_irqrestore(&ch->lock, flags);
2243 			__napi_schedule(&ch->rx_napi);
2244 		}
2245 	}
2246 
2247 	if ((status & handle_tx) && (chan < priv->plat->tx_queues_to_use)) {
2248 		if (napi_schedule_prep(&ch->tx_napi)) {
2249 			spin_lock_irqsave(&ch->lock, flags);
2250 			stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
2251 			spin_unlock_irqrestore(&ch->lock, flags);
2252 			__napi_schedule(&ch->tx_napi);
2253 		}
2254 	}
2255 
2256 	return status;
2257 }
2258 
2259 /**
2260  * stmmac_dma_interrupt - DMA ISR
2261  * @priv: driver private structure
2262  * Description: this is the DMA ISR. It is called by the main ISR.
2263  * It calls the dwmac dma routine and schedule poll method in case of some
2264  * work can be done.
2265  */
stmmac_dma_interrupt(struct stmmac_priv * priv)2266 static void stmmac_dma_interrupt(struct stmmac_priv *priv)
2267 {
2268 	u32 tx_channel_count = priv->plat->tx_queues_to_use;
2269 	u32 rx_channel_count = priv->plat->rx_queues_to_use;
2270 	u32 channels_to_check = tx_channel_count > rx_channel_count ?
2271 				tx_channel_count : rx_channel_count;
2272 	u32 chan;
2273 	int status[max_t(u32, MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES)];
2274 
2275 	/* Make sure we never check beyond our status buffer. */
2276 	if (WARN_ON_ONCE(channels_to_check > ARRAY_SIZE(status)))
2277 		channels_to_check = ARRAY_SIZE(status);
2278 
2279 	for (chan = 0; chan < channels_to_check; chan++)
2280 		status[chan] = stmmac_napi_check(priv, chan);
2281 
2282 	for (chan = 0; chan < tx_channel_count; chan++) {
2283 		if (unlikely(status[chan] & tx_hard_error_bump_tc)) {
2284 			/* Try to bump up the dma threshold on this failure */
2285 			if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
2286 			    (tc <= 256)) {
2287 				tc += 64;
2288 				if (priv->plat->force_thresh_dma_mode)
2289 					stmmac_set_dma_operation_mode(priv,
2290 								      tc,
2291 								      tc,
2292 								      chan);
2293 				else
2294 					stmmac_set_dma_operation_mode(priv,
2295 								    tc,
2296 								    SF_DMA_MODE,
2297 								    chan);
2298 				priv->xstats.threshold = tc;
2299 			}
2300 		} else if (unlikely(status[chan] == tx_hard_error)) {
2301 			stmmac_tx_err(priv, chan);
2302 		}
2303 	}
2304 }
2305 
2306 /**
2307  * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
2308  * @priv: driver private structure
2309  * Description: this masks the MMC irq, in fact, the counters are managed in SW.
2310  */
stmmac_mmc_setup(struct stmmac_priv * priv)2311 static void stmmac_mmc_setup(struct stmmac_priv *priv)
2312 {
2313 	unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
2314 			    MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
2315 
2316 	stmmac_mmc_intr_all_mask(priv, priv->mmcaddr);
2317 
2318 	if (priv->dma_cap.rmon) {
2319 		stmmac_mmc_ctrl(priv, priv->mmcaddr, mode);
2320 		memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
2321 	} else
2322 		netdev_info(priv->dev, "No MAC Management Counters available\n");
2323 }
2324 
2325 /**
2326  * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
2327  * @priv: driver private structure
2328  * Description:
2329  *  new GMAC chip generations have a new register to indicate the
2330  *  presence of the optional feature/functions.
2331  *  This can be also used to override the value passed through the
2332  *  platform and necessary for old MAC10/100 and GMAC chips.
2333  */
stmmac_get_hw_features(struct stmmac_priv * priv)2334 static int stmmac_get_hw_features(struct stmmac_priv *priv)
2335 {
2336 	return stmmac_get_hw_feature(priv, priv->ioaddr, &priv->dma_cap) == 0;
2337 }
2338 
2339 /**
2340  * stmmac_check_ether_addr - check if the MAC addr is valid
2341  * @priv: driver private structure
2342  * Description:
2343  * it is to verify if the MAC address is valid, in case of failures it
2344  * generates a random MAC address
2345  */
stmmac_check_ether_addr(struct stmmac_priv * priv)2346 static void stmmac_check_ether_addr(struct stmmac_priv *priv)
2347 {
2348 	if (!is_valid_ether_addr(priv->dev->dev_addr)) {
2349 		stmmac_get_umac_addr(priv, priv->hw, priv->dev->dev_addr, 0);
2350 		if (likely(priv->plat->get_eth_addr))
2351 			priv->plat->get_eth_addr(priv->plat->bsp_priv,
2352 				priv->dev->dev_addr);
2353 		if (!is_valid_ether_addr(priv->dev->dev_addr))
2354 			eth_hw_addr_random(priv->dev);
2355 		dev_info(priv->device, "device MAC address %pM\n",
2356 			 priv->dev->dev_addr);
2357 	}
2358 }
2359 
2360 /**
2361  * stmmac_init_dma_engine - DMA init.
2362  * @priv: driver private structure
2363  * Description:
2364  * It inits the DMA invoking the specific MAC/GMAC callback.
2365  * Some DMA parameters can be passed from the platform;
2366  * in case of these are not passed a default is kept for the MAC or GMAC.
2367  */
stmmac_init_dma_engine(struct stmmac_priv * priv)2368 static int stmmac_init_dma_engine(struct stmmac_priv *priv)
2369 {
2370 	u32 rx_channels_count = priv->plat->rx_queues_to_use;
2371 	u32 tx_channels_count = priv->plat->tx_queues_to_use;
2372 	u32 dma_csr_ch = max(rx_channels_count, tx_channels_count);
2373 	struct stmmac_rx_queue *rx_q;
2374 	struct stmmac_tx_queue *tx_q;
2375 	u32 chan = 0;
2376 	int atds = 0;
2377 	int ret = 0;
2378 
2379 	if (!priv->plat->dma_cfg || !priv->plat->dma_cfg->pbl) {
2380 		dev_err(priv->device, "Invalid DMA configuration\n");
2381 		return -EINVAL;
2382 	}
2383 
2384 	if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
2385 		atds = 1;
2386 
2387 	ret = stmmac_reset(priv, priv->ioaddr);
2388 	if (ret) {
2389 		dev_err(priv->device, "Failed to reset the dma\n");
2390 		return ret;
2391 	}
2392 
2393 	/* DMA Configuration */
2394 	stmmac_dma_init(priv, priv->ioaddr, priv->plat->dma_cfg, atds);
2395 
2396 	if (priv->plat->axi)
2397 		stmmac_axi(priv, priv->ioaddr, priv->plat->axi);
2398 
2399 	/* DMA CSR Channel configuration */
2400 	for (chan = 0; chan < dma_csr_ch; chan++)
2401 		stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan);
2402 
2403 	/* DMA RX Channel Configuration */
2404 	for (chan = 0; chan < rx_channels_count; chan++) {
2405 		rx_q = &priv->rx_queue[chan];
2406 
2407 		stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2408 				    rx_q->dma_rx_phy, chan);
2409 
2410 		rx_q->rx_tail_addr = rx_q->dma_rx_phy +
2411 				     (priv->dma_rx_size *
2412 				      sizeof(struct dma_desc));
2413 		stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
2414 				       rx_q->rx_tail_addr, chan);
2415 	}
2416 
2417 	/* DMA TX Channel Configuration */
2418 	for (chan = 0; chan < tx_channels_count; chan++) {
2419 		tx_q = &priv->tx_queue[chan];
2420 
2421 		stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2422 				    tx_q->dma_tx_phy, chan);
2423 
2424 		tx_q->tx_tail_addr = tx_q->dma_tx_phy;
2425 		stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
2426 				       tx_q->tx_tail_addr, chan);
2427 	}
2428 
2429 	return ret;
2430 }
2431 
stmmac_tx_timer_arm(struct stmmac_priv * priv,u32 queue)2432 static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue)
2433 {
2434 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2435 
2436 	mod_timer(&tx_q->txtimer, STMMAC_COAL_TIMER(priv->tx_coal_timer));
2437 }
2438 
2439 /**
2440  * stmmac_tx_timer - mitigation sw timer for tx.
2441  * @t: data pointer
2442  * Description:
2443  * This is the timer handler to directly invoke the stmmac_tx_clean.
2444  */
stmmac_tx_timer(struct timer_list * t)2445 static void stmmac_tx_timer(struct timer_list *t)
2446 {
2447 	struct stmmac_tx_queue *tx_q = from_timer(tx_q, t, txtimer);
2448 	struct stmmac_priv *priv = tx_q->priv_data;
2449 	struct stmmac_channel *ch;
2450 
2451 	ch = &priv->channel[tx_q->queue_index];
2452 
2453 	if (likely(napi_schedule_prep(&ch->tx_napi))) {
2454 		unsigned long flags;
2455 
2456 		spin_lock_irqsave(&ch->lock, flags);
2457 		stmmac_disable_dma_irq(priv, priv->ioaddr, ch->index, 0, 1);
2458 		spin_unlock_irqrestore(&ch->lock, flags);
2459 		__napi_schedule(&ch->tx_napi);
2460 	}
2461 }
2462 
2463 /**
2464  * stmmac_init_coalesce - init mitigation options.
2465  * @priv: driver private structure
2466  * Description:
2467  * This inits the coalesce parameters: i.e. timer rate,
2468  * timer handler and default threshold used for enabling the
2469  * interrupt on completion bit.
2470  */
stmmac_init_coalesce(struct stmmac_priv * priv)2471 static void stmmac_init_coalesce(struct stmmac_priv *priv)
2472 {
2473 	u32 tx_channel_count = priv->plat->tx_queues_to_use;
2474 	u32 chan;
2475 
2476 	priv->tx_coal_frames = STMMAC_TX_FRAMES;
2477 	priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
2478 	priv->rx_coal_frames = STMMAC_RX_FRAMES;
2479 
2480 	for (chan = 0; chan < tx_channel_count; chan++) {
2481 		struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2482 
2483 		timer_setup(&tx_q->txtimer, stmmac_tx_timer, 0);
2484 	}
2485 }
2486 
stmmac_set_rings_length(struct stmmac_priv * priv)2487 static void stmmac_set_rings_length(struct stmmac_priv *priv)
2488 {
2489 	u32 rx_channels_count = priv->plat->rx_queues_to_use;
2490 	u32 tx_channels_count = priv->plat->tx_queues_to_use;
2491 	u32 chan;
2492 
2493 	/* set TX ring length */
2494 	for (chan = 0; chan < tx_channels_count; chan++)
2495 		stmmac_set_tx_ring_len(priv, priv->ioaddr,
2496 				       (priv->dma_tx_size - 1), chan);
2497 
2498 	/* set RX ring length */
2499 	for (chan = 0; chan < rx_channels_count; chan++)
2500 		stmmac_set_rx_ring_len(priv, priv->ioaddr,
2501 				       (priv->dma_rx_size - 1), chan);
2502 }
2503 
2504 /**
2505  *  stmmac_set_tx_queue_weight - Set TX queue weight
2506  *  @priv: driver private structure
2507  *  Description: It is used for setting TX queues weight
2508  */
stmmac_set_tx_queue_weight(struct stmmac_priv * priv)2509 static void stmmac_set_tx_queue_weight(struct stmmac_priv *priv)
2510 {
2511 	u32 tx_queues_count = priv->plat->tx_queues_to_use;
2512 	u32 weight;
2513 	u32 queue;
2514 
2515 	for (queue = 0; queue < tx_queues_count; queue++) {
2516 		weight = priv->plat->tx_queues_cfg[queue].weight;
2517 		stmmac_set_mtl_tx_queue_weight(priv, priv->hw, weight, queue);
2518 	}
2519 }
2520 
2521 /**
2522  *  stmmac_configure_cbs - Configure CBS in TX queue
2523  *  @priv: driver private structure
2524  *  Description: It is used for configuring CBS in AVB TX queues
2525  */
stmmac_configure_cbs(struct stmmac_priv * priv)2526 static void stmmac_configure_cbs(struct stmmac_priv *priv)
2527 {
2528 	u32 tx_queues_count = priv->plat->tx_queues_to_use;
2529 	u32 mode_to_use;
2530 	u32 queue;
2531 
2532 	/* queue 0 is reserved for legacy traffic */
2533 	for (queue = 1; queue < tx_queues_count; queue++) {
2534 		mode_to_use = priv->plat->tx_queues_cfg[queue].mode_to_use;
2535 		if (mode_to_use == MTL_QUEUE_DCB)
2536 			continue;
2537 
2538 		stmmac_config_cbs(priv, priv->hw,
2539 				priv->plat->tx_queues_cfg[queue].send_slope,
2540 				priv->plat->tx_queues_cfg[queue].idle_slope,
2541 				priv->plat->tx_queues_cfg[queue].high_credit,
2542 				priv->plat->tx_queues_cfg[queue].low_credit,
2543 				queue);
2544 	}
2545 }
2546 
2547 /**
2548  *  stmmac_rx_queue_dma_chan_map - Map RX queue to RX dma channel
2549  *  @priv: driver private structure
2550  *  Description: It is used for mapping RX queues to RX dma channels
2551  */
stmmac_rx_queue_dma_chan_map(struct stmmac_priv * priv)2552 static void stmmac_rx_queue_dma_chan_map(struct stmmac_priv *priv)
2553 {
2554 	u32 rx_queues_count = priv->plat->rx_queues_to_use;
2555 	u32 queue;
2556 	u32 chan;
2557 
2558 	for (queue = 0; queue < rx_queues_count; queue++) {
2559 		chan = priv->plat->rx_queues_cfg[queue].chan;
2560 		stmmac_map_mtl_to_dma(priv, priv->hw, queue, chan);
2561 	}
2562 }
2563 
2564 /**
2565  *  stmmac_mac_config_rx_queues_prio - Configure RX Queue priority
2566  *  @priv: driver private structure
2567  *  Description: It is used for configuring the RX Queue Priority
2568  */
stmmac_mac_config_rx_queues_prio(struct stmmac_priv * priv)2569 static void stmmac_mac_config_rx_queues_prio(struct stmmac_priv *priv)
2570 {
2571 	u32 rx_queues_count = priv->plat->rx_queues_to_use;
2572 	u32 queue;
2573 	u32 prio;
2574 
2575 	for (queue = 0; queue < rx_queues_count; queue++) {
2576 		if (!priv->plat->rx_queues_cfg[queue].use_prio)
2577 			continue;
2578 
2579 		prio = priv->plat->rx_queues_cfg[queue].prio;
2580 		stmmac_rx_queue_prio(priv, priv->hw, prio, queue);
2581 	}
2582 }
2583 
2584 /**
2585  *  stmmac_mac_config_tx_queues_prio - Configure TX Queue priority
2586  *  @priv: driver private structure
2587  *  Description: It is used for configuring the TX Queue Priority
2588  */
stmmac_mac_config_tx_queues_prio(struct stmmac_priv * priv)2589 static void stmmac_mac_config_tx_queues_prio(struct stmmac_priv *priv)
2590 {
2591 	u32 tx_queues_count = priv->plat->tx_queues_to_use;
2592 	u32 queue;
2593 	u32 prio;
2594 
2595 	for (queue = 0; queue < tx_queues_count; queue++) {
2596 		if (!priv->plat->tx_queues_cfg[queue].use_prio)
2597 			continue;
2598 
2599 		prio = priv->plat->tx_queues_cfg[queue].prio;
2600 		stmmac_tx_queue_prio(priv, priv->hw, prio, queue);
2601 	}
2602 }
2603 
2604 /**
2605  *  stmmac_mac_config_rx_queues_routing - Configure RX Queue Routing
2606  *  @priv: driver private structure
2607  *  Description: It is used for configuring the RX queue routing
2608  */
stmmac_mac_config_rx_queues_routing(struct stmmac_priv * priv)2609 static void stmmac_mac_config_rx_queues_routing(struct stmmac_priv *priv)
2610 {
2611 	u32 rx_queues_count = priv->plat->rx_queues_to_use;
2612 	u32 queue;
2613 	u8 packet;
2614 
2615 	for (queue = 0; queue < rx_queues_count; queue++) {
2616 		/* no specific packet type routing specified for the queue */
2617 		if (priv->plat->rx_queues_cfg[queue].pkt_route == 0x0)
2618 			continue;
2619 
2620 		packet = priv->plat->rx_queues_cfg[queue].pkt_route;
2621 		stmmac_rx_queue_routing(priv, priv->hw, packet, queue);
2622 	}
2623 }
2624 
stmmac_mac_config_rss(struct stmmac_priv * priv)2625 static void stmmac_mac_config_rss(struct stmmac_priv *priv)
2626 {
2627 	if (!priv->dma_cap.rssen || !priv->plat->rss_en) {
2628 		priv->rss.enable = false;
2629 		return;
2630 	}
2631 
2632 	if (priv->dev->features & NETIF_F_RXHASH)
2633 		priv->rss.enable = true;
2634 	else
2635 		priv->rss.enable = false;
2636 
2637 	stmmac_rss_configure(priv, priv->hw, &priv->rss,
2638 			     priv->plat->rx_queues_to_use);
2639 }
2640 
2641 /**
2642  *  stmmac_mtl_configuration - Configure MTL
2643  *  @priv: driver private structure
2644  *  Description: It is used for configurring MTL
2645  */
stmmac_mtl_configuration(struct stmmac_priv * priv)2646 static void stmmac_mtl_configuration(struct stmmac_priv *priv)
2647 {
2648 	u32 rx_queues_count = priv->plat->rx_queues_to_use;
2649 	u32 tx_queues_count = priv->plat->tx_queues_to_use;
2650 
2651 	if (tx_queues_count > 1)
2652 		stmmac_set_tx_queue_weight(priv);
2653 
2654 	/* Configure MTL RX algorithms */
2655 	if (rx_queues_count > 1)
2656 		stmmac_prog_mtl_rx_algorithms(priv, priv->hw,
2657 				priv->plat->rx_sched_algorithm);
2658 
2659 	/* Configure MTL TX algorithms */
2660 	if (tx_queues_count > 1)
2661 		stmmac_prog_mtl_tx_algorithms(priv, priv->hw,
2662 				priv->plat->tx_sched_algorithm);
2663 
2664 	/* Configure CBS in AVB TX queues */
2665 	if (tx_queues_count > 1)
2666 		stmmac_configure_cbs(priv);
2667 
2668 	/* Map RX MTL to DMA channels */
2669 	stmmac_rx_queue_dma_chan_map(priv);
2670 
2671 	/* Enable MAC RX Queues */
2672 	stmmac_mac_enable_rx_queues(priv);
2673 
2674 	/* Set RX priorities */
2675 	if (rx_queues_count > 1)
2676 		stmmac_mac_config_rx_queues_prio(priv);
2677 
2678 	/* Set TX priorities */
2679 	if (tx_queues_count > 1)
2680 		stmmac_mac_config_tx_queues_prio(priv);
2681 
2682 	/* Set RX routing */
2683 	if (rx_queues_count > 1)
2684 		stmmac_mac_config_rx_queues_routing(priv);
2685 
2686 	/* Receive Side Scaling */
2687 	if (rx_queues_count > 1)
2688 		stmmac_mac_config_rss(priv);
2689 }
2690 
stmmac_safety_feat_configuration(struct stmmac_priv * priv)2691 static void stmmac_safety_feat_configuration(struct stmmac_priv *priv)
2692 {
2693 	if (priv->dma_cap.asp) {
2694 		netdev_info(priv->dev, "Enabling Safety Features\n");
2695 		stmmac_safety_feat_config(priv, priv->ioaddr, priv->dma_cap.asp);
2696 	} else {
2697 		netdev_info(priv->dev, "No Safety Features support found\n");
2698 	}
2699 }
2700 
2701 /**
2702  * stmmac_hw_setup - setup mac in a usable state.
2703  *  @dev : pointer to the device structure.
2704  *  @ptp_register: register PTP if set
2705  *  Description:
2706  *  this is the main function to setup the HW in a usable state because the
2707  *  dma engine is reset, the core registers are configured (e.g. AXI,
2708  *  Checksum features, timers). The DMA is ready to start receiving and
2709  *  transmitting.
2710  *  Return value:
2711  *  0 on success and an appropriate (-)ve integer as defined in errno.h
2712  *  file on failure.
2713  */
stmmac_hw_setup(struct net_device * dev,bool ptp_register)2714 static int stmmac_hw_setup(struct net_device *dev, bool ptp_register)
2715 {
2716 	struct stmmac_priv *priv = netdev_priv(dev);
2717 	u32 rx_cnt = priv->plat->rx_queues_to_use;
2718 	u32 tx_cnt = priv->plat->tx_queues_to_use;
2719 	u32 chan;
2720 	int ret;
2721 
2722 	/* DMA initialization and SW reset */
2723 	ret = stmmac_init_dma_engine(priv);
2724 	if (ret < 0) {
2725 		netdev_err(priv->dev, "%s: DMA engine initialization failed\n",
2726 			   __func__);
2727 		return ret;
2728 	}
2729 
2730 	/* Copy the MAC addr into the HW  */
2731 	stmmac_set_umac_addr(priv, priv->hw, dev->dev_addr, 0);
2732 
2733 	/* PS and related bits will be programmed according to the speed */
2734 	if (priv->hw->pcs) {
2735 		int speed = priv->plat->mac_port_sel_speed;
2736 
2737 		if ((speed == SPEED_10) || (speed == SPEED_100) ||
2738 		    (speed == SPEED_1000)) {
2739 			priv->hw->ps = speed;
2740 		} else {
2741 			dev_warn(priv->device, "invalid port speed\n");
2742 			priv->hw->ps = 0;
2743 		}
2744 	}
2745 
2746 	/* Initialize the MAC Core */
2747 	stmmac_core_init(priv, priv->hw, dev);
2748 
2749 	/* Initialize MTL*/
2750 	stmmac_mtl_configuration(priv);
2751 
2752 	/* Initialize Safety Features */
2753 	stmmac_safety_feat_configuration(priv);
2754 
2755 	ret = stmmac_rx_ipc(priv, priv->hw);
2756 	if (!ret) {
2757 		netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n");
2758 		priv->plat->rx_coe = STMMAC_RX_COE_NONE;
2759 		priv->hw->rx_csum = 0;
2760 	}
2761 
2762 	/* Enable the MAC Rx/Tx */
2763 	stmmac_mac_set(priv, priv->ioaddr, true);
2764 
2765 	/* Set the HW DMA mode and the COE */
2766 	stmmac_dma_operation_mode(priv);
2767 
2768 	stmmac_mmc_setup(priv);
2769 
2770 	if (ptp_register) {
2771 		ret = clk_prepare_enable(priv->plat->clk_ptp_ref);
2772 		if (ret < 0)
2773 			netdev_warn(priv->dev,
2774 				    "failed to enable PTP reference clock: %pe\n",
2775 				    ERR_PTR(ret));
2776 	}
2777 
2778 	ret = stmmac_init_ptp(priv);
2779 	if (ret == -EOPNOTSUPP)
2780 		netdev_warn(priv->dev, "PTP not supported by HW\n");
2781 	else if (ret)
2782 		netdev_warn(priv->dev, "PTP init failed\n");
2783 	else if (ptp_register)
2784 		stmmac_ptp_register(priv);
2785 
2786 	priv->eee_tw_timer = STMMAC_DEFAULT_TWT_LS;
2787 
2788 	/* Convert the timer from msec to usec */
2789 	if (!priv->tx_lpi_timer)
2790 		priv->tx_lpi_timer = eee_timer * 1000;
2791 
2792 	if (priv->use_riwt) {
2793 		if (!priv->rx_riwt)
2794 			priv->rx_riwt = DEF_DMA_RIWT;
2795 
2796 		ret = stmmac_rx_watchdog(priv, priv->ioaddr, priv->rx_riwt, rx_cnt);
2797 	}
2798 
2799 	if (priv->hw->pcs)
2800 		stmmac_pcs_ctrl_ane(priv, priv->ioaddr, 1, priv->hw->ps, 0);
2801 
2802 	/* set TX and RX rings length */
2803 	stmmac_set_rings_length(priv);
2804 
2805 	/* Enable TSO */
2806 	if (priv->tso) {
2807 		for (chan = 0; chan < tx_cnt; chan++) {
2808 			struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2809 
2810 			/* TSO and TBS cannot co-exist */
2811 			if (tx_q->tbs & STMMAC_TBS_AVAIL)
2812 				continue;
2813 
2814 			stmmac_enable_tso(priv, priv->ioaddr, 1, chan);
2815 		}
2816 	}
2817 
2818 	/* Enable Split Header */
2819 	if (priv->sph && priv->hw->rx_csum) {
2820 		for (chan = 0; chan < rx_cnt; chan++)
2821 			stmmac_enable_sph(priv, priv->ioaddr, 1, chan);
2822 	}
2823 
2824 	/* VLAN Tag Insertion */
2825 	if (priv->dma_cap.vlins)
2826 		stmmac_enable_vlan(priv, priv->hw, STMMAC_VLAN_INSERT);
2827 
2828 	/* TBS */
2829 	for (chan = 0; chan < tx_cnt; chan++) {
2830 		struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2831 		int enable = tx_q->tbs & STMMAC_TBS_AVAIL;
2832 
2833 		stmmac_enable_tbs(priv, priv->ioaddr, enable, chan);
2834 	}
2835 
2836 	/* Configure real RX and TX queues */
2837 	netif_set_real_num_rx_queues(dev, priv->plat->rx_queues_to_use);
2838 	netif_set_real_num_tx_queues(dev, priv->plat->tx_queues_to_use);
2839 
2840 	/* Start the ball rolling... */
2841 	stmmac_start_all_dma(priv);
2842 
2843 	return 0;
2844 }
2845 
stmmac_hw_teardown(struct net_device * dev)2846 static void stmmac_hw_teardown(struct net_device *dev)
2847 {
2848 	struct stmmac_priv *priv = netdev_priv(dev);
2849 
2850 	clk_disable_unprepare(priv->plat->clk_ptp_ref);
2851 }
2852 
2853 /**
2854  *  stmmac_open - open entry point of the driver
2855  *  @dev : pointer to the device structure.
2856  *  Description:
2857  *  This function is the open entry point of the driver.
2858  *  Return value:
2859  *  0 on success and an appropriate (-)ve integer as defined in errno.h
2860  *  file on failure.
2861  */
stmmac_open(struct net_device * dev)2862 static int stmmac_open(struct net_device *dev)
2863 {
2864 	struct stmmac_priv *priv = netdev_priv(dev);
2865 	int bfsize = 0;
2866 	u32 chan;
2867 	int ret;
2868 
2869 	ret = pm_runtime_get_sync(priv->device);
2870 	if (ret < 0) {
2871 		pm_runtime_put_noidle(priv->device);
2872 		return ret;
2873 	}
2874 
2875 	if (priv->hw->pcs != STMMAC_PCS_TBI &&
2876 	    priv->hw->pcs != STMMAC_PCS_RTBI &&
2877 	    priv->hw->xpcs == NULL) {
2878 		ret = stmmac_init_phy(dev);
2879 		if (ret) {
2880 			netdev_err(priv->dev,
2881 				   "%s: Cannot attach to PHY (error: %d)\n",
2882 				   __func__, ret);
2883 			goto init_phy_error;
2884 		}
2885 	}
2886 
2887 	/* Extra statistics */
2888 	memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
2889 	priv->xstats.threshold = tc;
2890 
2891 	bfsize = stmmac_set_16kib_bfsize(priv, dev->mtu);
2892 	if (bfsize < 0)
2893 		bfsize = 0;
2894 
2895 	if (bfsize < BUF_SIZE_16KiB)
2896 		bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
2897 
2898 	priv->dma_buf_sz = bfsize;
2899 	buf_sz = bfsize;
2900 
2901 	priv->rx_copybreak = STMMAC_RX_COPYBREAK;
2902 
2903 	if (!priv->dma_tx_size)
2904 		priv->dma_tx_size = priv->plat->dma_tx_size ? priv->plat->dma_tx_size :
2905 				    DMA_DEFAULT_TX_SIZE;
2906 
2907 	if (!priv->dma_rx_size)
2908 		priv->dma_rx_size = priv->plat->dma_rx_size ? priv->plat->dma_rx_size :
2909 				    DMA_DEFAULT_RX_SIZE;
2910 
2911 	/* Earlier check for TBS */
2912 	for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++) {
2913 		struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2914 		int tbs_en = priv->plat->tx_queues_cfg[chan].tbs_en;
2915 
2916 		/* Setup per-TXQ tbs flag before TX descriptor alloc */
2917 		tx_q->tbs |= tbs_en ? STMMAC_TBS_AVAIL : 0;
2918 	}
2919 
2920 	ret = alloc_dma_desc_resources(priv);
2921 	if (ret < 0) {
2922 		netdev_err(priv->dev, "%s: DMA descriptors allocation failed\n",
2923 			   __func__);
2924 		goto dma_desc_error;
2925 	}
2926 
2927 	ret = init_dma_desc_rings(dev, GFP_KERNEL);
2928 	if (ret < 0) {
2929 		netdev_err(priv->dev, "%s: DMA descriptors initialization failed\n",
2930 			   __func__);
2931 		goto init_error;
2932 	}
2933 
2934 	if (priv->plat->serdes_powerup) {
2935 		ret = priv->plat->serdes_powerup(dev, priv->plat->bsp_priv);
2936 		if (ret < 0) {
2937 			netdev_err(priv->dev, "%s: Serdes powerup failed\n",
2938 				   __func__);
2939 			goto init_error;
2940 		}
2941 	}
2942 
2943 	ret = stmmac_hw_setup(dev, true);
2944 	if (ret < 0) {
2945 		netdev_err(priv->dev, "%s: Hw setup failed\n", __func__);
2946 		goto init_error;
2947 	}
2948 
2949 	stmmac_init_coalesce(priv);
2950 
2951 	phylink_start(priv->phylink);
2952 	/* We may have called phylink_speed_down before */
2953 	phylink_speed_up(priv->phylink);
2954 
2955 	/* Request the IRQ lines */
2956 	ret = request_irq(dev->irq, stmmac_interrupt,
2957 			  IRQF_SHARED, dev->name, dev);
2958 	if (unlikely(ret < 0)) {
2959 		netdev_err(priv->dev,
2960 			   "%s: ERROR: allocating the IRQ %d (error: %d)\n",
2961 			   __func__, dev->irq, ret);
2962 		goto irq_error;
2963 	}
2964 
2965 	/* Request the Wake IRQ in case of another line is used for WoL */
2966 	if (priv->wol_irq != dev->irq) {
2967 		ret = request_irq(priv->wol_irq, stmmac_interrupt,
2968 				  IRQF_SHARED, dev->name, dev);
2969 		if (unlikely(ret < 0)) {
2970 			netdev_err(priv->dev,
2971 				   "%s: ERROR: allocating the WoL IRQ %d (%d)\n",
2972 				   __func__, priv->wol_irq, ret);
2973 			goto wolirq_error;
2974 		}
2975 	}
2976 
2977 	/* Request the IRQ lines */
2978 	if (priv->lpi_irq > 0) {
2979 		ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
2980 				  dev->name, dev);
2981 		if (unlikely(ret < 0)) {
2982 			netdev_err(priv->dev,
2983 				   "%s: ERROR: allocating the LPI IRQ %d (%d)\n",
2984 				   __func__, priv->lpi_irq, ret);
2985 			goto lpiirq_error;
2986 		}
2987 	}
2988 
2989 	stmmac_enable_all_queues(priv);
2990 	netif_tx_start_all_queues(priv->dev);
2991 
2992 	return 0;
2993 
2994 lpiirq_error:
2995 	if (priv->wol_irq != dev->irq)
2996 		free_irq(priv->wol_irq, dev);
2997 wolirq_error:
2998 	free_irq(dev->irq, dev);
2999 irq_error:
3000 	phylink_stop(priv->phylink);
3001 
3002 	for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
3003 		del_timer_sync(&priv->tx_queue[chan].txtimer);
3004 
3005 	stmmac_hw_teardown(dev);
3006 init_error:
3007 	free_dma_desc_resources(priv);
3008 dma_desc_error:
3009 	phylink_disconnect_phy(priv->phylink);
3010 init_phy_error:
3011 	pm_runtime_put(priv->device);
3012 	return ret;
3013 }
3014 
3015 /**
3016  *  stmmac_release - close entry point of the driver
3017  *  @dev : device pointer.
3018  *  Description:
3019  *  This is the stop entry point of the driver.
3020  */
stmmac_release(struct net_device * dev)3021 static int stmmac_release(struct net_device *dev)
3022 {
3023 	struct stmmac_priv *priv = netdev_priv(dev);
3024 	u32 chan;
3025 
3026 	if (device_may_wakeup(priv->device))
3027 		phylink_speed_down(priv->phylink, false);
3028 	/* Stop and disconnect the PHY */
3029 	phylink_stop(priv->phylink);
3030 	phylink_disconnect_phy(priv->phylink);
3031 
3032 	if (priv->plat->integrated_phy_power)
3033 		priv->plat->integrated_phy_power(priv->plat->bsp_priv, false);
3034 
3035 	stmmac_disable_all_queues(priv);
3036 
3037 	for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
3038 		del_timer_sync(&priv->tx_queue[chan].txtimer);
3039 
3040 	/* Free the IRQ lines */
3041 	free_irq(dev->irq, dev);
3042 	if (priv->wol_irq != dev->irq)
3043 		free_irq(priv->wol_irq, dev);
3044 	if (priv->lpi_irq > 0)
3045 		free_irq(priv->lpi_irq, dev);
3046 
3047 	if (priv->eee_enabled) {
3048 		priv->tx_path_in_lpi_mode = false;
3049 		del_timer_sync(&priv->eee_ctrl_timer);
3050 	}
3051 
3052 	/* Stop TX/RX DMA and clear the descriptors */
3053 	stmmac_stop_all_dma(priv);
3054 
3055 	/* Release and free the Rx/Tx resources */
3056 	free_dma_desc_resources(priv);
3057 
3058 	/* Disable the MAC Rx/Tx */
3059 	stmmac_mac_set(priv, priv->ioaddr, false);
3060 
3061 	/* Powerdown Serdes if there is */
3062 	if (priv->plat->serdes_powerdown)
3063 		priv->plat->serdes_powerdown(dev, priv->plat->bsp_priv);
3064 
3065 	netif_carrier_off(dev);
3066 
3067 	stmmac_release_ptp(priv);
3068 
3069 	pm_runtime_put(priv->device);
3070 
3071 	return 0;
3072 }
3073 
stmmac_vlan_insert(struct stmmac_priv * priv,struct sk_buff * skb,struct stmmac_tx_queue * tx_q)3074 static bool stmmac_vlan_insert(struct stmmac_priv *priv, struct sk_buff *skb,
3075 			       struct stmmac_tx_queue *tx_q)
3076 {
3077 	u16 tag = 0x0, inner_tag = 0x0;
3078 	u32 inner_type = 0x0;
3079 	struct dma_desc *p;
3080 
3081 	if (!priv->dma_cap.vlins)
3082 		return false;
3083 	if (!skb_vlan_tag_present(skb))
3084 		return false;
3085 	if (skb->vlan_proto == htons(ETH_P_8021AD)) {
3086 		inner_tag = skb_vlan_tag_get(skb);
3087 		inner_type = STMMAC_VLAN_INSERT;
3088 	}
3089 
3090 	tag = skb_vlan_tag_get(skb);
3091 
3092 	if (tx_q->tbs & STMMAC_TBS_AVAIL)
3093 		p = &tx_q->dma_entx[tx_q->cur_tx].basic;
3094 	else
3095 		p = &tx_q->dma_tx[tx_q->cur_tx];
3096 
3097 	if (stmmac_set_desc_vlan_tag(priv, p, tag, inner_tag, inner_type))
3098 		return false;
3099 
3100 	stmmac_set_tx_owner(priv, p);
3101 	tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
3102 	return true;
3103 }
3104 
3105 /**
3106  *  stmmac_tso_allocator - close entry point of the driver
3107  *  @priv: driver private structure
3108  *  @des: buffer start address
3109  *  @total_len: total length to fill in descriptors
3110  *  @last_segment: condition for the last descriptor
3111  *  @queue: TX queue index
3112  *  Description:
3113  *  This function fills descriptor and request new descriptors according to
3114  *  buffer length to fill
3115  */
stmmac_tso_allocator(struct stmmac_priv * priv,dma_addr_t des,int total_len,bool last_segment,u32 queue)3116 static void stmmac_tso_allocator(struct stmmac_priv *priv, dma_addr_t des,
3117 				 int total_len, bool last_segment, u32 queue)
3118 {
3119 	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
3120 	struct dma_desc *desc;
3121 	u32 buff_size;
3122 	int tmp_len;
3123 
3124 	tmp_len = total_len;
3125 
3126 	while (tmp_len > 0) {
3127 		dma_addr_t curr_addr;
3128 
3129 		tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx,
3130 						priv->dma_tx_size);
3131 		WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
3132 
3133 		if (tx_q->tbs & STMMAC_TBS_AVAIL)
3134 			desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
3135 		else
3136 			desc = &tx_q->dma_tx[tx_q->cur_tx];
3137 
3138 		curr_addr = des + (total_len - tmp_len);
3139 		if (priv->dma_cap.addr64 <= 32)
3140 			desc->des0 = cpu_to_le32(curr_addr);
3141 		else
3142 			stmmac_set_desc_addr(priv, desc, curr_addr);
3143 
3144 		buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ?
3145 			    TSO_MAX_BUFF_SIZE : tmp_len;
3146 
3147 		stmmac_prepare_tso_tx_desc(priv, desc, 0, buff_size,
3148 				0, 1,
3149 				(last_segment) && (tmp_len <= TSO_MAX_BUFF_SIZE),
3150 				0, 0);
3151 
3152 		tmp_len -= TSO_MAX_BUFF_SIZE;
3153 	}
3154 }
3155 
3156 /**
3157  *  stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO)
3158  *  @skb : the socket buffer
3159  *  @dev : device pointer
3160  *  Description: this is the transmit function that is called on TSO frames
3161  *  (support available on GMAC4 and newer chips).
3162  *  Diagram below show the ring programming in case of TSO frames:
3163  *
3164  *  First Descriptor
3165  *   --------
3166  *   | DES0 |---> buffer1 = L2/L3/L4 header
3167  *   | DES1 |---> TCP Payload (can continue on next descr...)
3168  *   | DES2 |---> buffer 1 and 2 len
3169  *   | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0]
3170  *   --------
3171  *	|
3172  *     ...
3173  *	|
3174  *   --------
3175  *   | DES0 | --| Split TCP Payload on Buffers 1 and 2
3176  *   | DES1 | --|
3177  *   | DES2 | --> buffer 1 and 2 len
3178  *   | DES3 |
3179  *   --------
3180  *
3181  * mss is fixed when enable tso, so w/o programming the TDES3 ctx field.
3182  */
stmmac_tso_xmit(struct sk_buff * skb,struct net_device * dev)3183 static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev)
3184 {
3185 	struct dma_desc *desc, *first, *mss_desc = NULL;
3186 	struct stmmac_priv *priv = netdev_priv(dev);
3187 	int desc_size, tmp_pay_len = 0, first_tx;
3188 	int nfrags = skb_shinfo(skb)->nr_frags;
3189 	u32 queue = skb_get_queue_mapping(skb);
3190 	unsigned int first_entry, tx_packets;
3191 	struct stmmac_tx_queue *tx_q;
3192 	bool has_vlan, set_ic;
3193 	u8 proto_hdr_len, hdr;
3194 	u32 pay_len, mss;
3195 	dma_addr_t des;
3196 	int i;
3197 
3198 	tx_q = &priv->tx_queue[queue];
3199 	first_tx = tx_q->cur_tx;
3200 
3201 	/* Compute header lengths */
3202 	if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
3203 		proto_hdr_len = skb_transport_offset(skb) + sizeof(struct udphdr);
3204 		hdr = sizeof(struct udphdr);
3205 	} else {
3206 		proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
3207 		hdr = tcp_hdrlen(skb);
3208 	}
3209 
3210 	/* Desc availability based on threshold should be enough safe */
3211 	if (unlikely(stmmac_tx_avail(priv, queue) <
3212 		(((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) {
3213 		if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
3214 			netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
3215 								queue));
3216 			/* This is a hard error, log it. */
3217 			netdev_err(priv->dev,
3218 				   "%s: Tx Ring full when queue awake\n",
3219 				   __func__);
3220 		}
3221 		return NETDEV_TX_BUSY;
3222 	}
3223 
3224 	pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */
3225 
3226 	mss = skb_shinfo(skb)->gso_size;
3227 
3228 	/* set new MSS value if needed */
3229 	if (mss != tx_q->mss) {
3230 		if (tx_q->tbs & STMMAC_TBS_AVAIL)
3231 			mss_desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
3232 		else
3233 			mss_desc = &tx_q->dma_tx[tx_q->cur_tx];
3234 
3235 		stmmac_set_mss(priv, mss_desc, mss);
3236 		tx_q->mss = mss;
3237 		tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx,
3238 						priv->dma_tx_size);
3239 		WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
3240 	}
3241 
3242 	if (netif_msg_tx_queued(priv)) {
3243 		pr_info("%s: hdrlen %d, hdr_len %d, pay_len %d, mss %d\n",
3244 			__func__, hdr, proto_hdr_len, pay_len, mss);
3245 		pr_info("\tskb->len %d, skb->data_len %d\n", skb->len,
3246 			skb->data_len);
3247 	}
3248 
3249 	/* Check if VLAN can be inserted by HW */
3250 	has_vlan = stmmac_vlan_insert(priv, skb, tx_q);
3251 
3252 	first_entry = tx_q->cur_tx;
3253 	WARN_ON(tx_q->tx_skbuff[first_entry]);
3254 
3255 	if (tx_q->tbs & STMMAC_TBS_AVAIL)
3256 		desc = &tx_q->dma_entx[first_entry].basic;
3257 	else
3258 		desc = &tx_q->dma_tx[first_entry];
3259 	first = desc;
3260 
3261 	if (has_vlan)
3262 		stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT);
3263 
3264 	/* first descriptor: fill Headers on Buf1 */
3265 	des = dma_map_single(priv->device, skb->data, skb_headlen(skb),
3266 			     DMA_TO_DEVICE);
3267 	if (dma_mapping_error(priv->device, des))
3268 		goto dma_map_err;
3269 
3270 	tx_q->tx_skbuff_dma[first_entry].buf = des;
3271 	tx_q->tx_skbuff_dma[first_entry].len = skb_headlen(skb);
3272 
3273 	if (priv->dma_cap.addr64 <= 32) {
3274 		first->des0 = cpu_to_le32(des);
3275 
3276 		/* Fill start of payload in buff2 of first descriptor */
3277 		if (pay_len)
3278 			first->des1 = cpu_to_le32(des + proto_hdr_len);
3279 
3280 		/* If needed take extra descriptors to fill the remaining payload */
3281 		tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE;
3282 	} else {
3283 		stmmac_set_desc_addr(priv, first, des);
3284 		tmp_pay_len = pay_len;
3285 		des += proto_hdr_len;
3286 		pay_len = 0;
3287 	}
3288 
3289 	stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0), queue);
3290 
3291 	/* Prepare fragments */
3292 	for (i = 0; i < nfrags; i++) {
3293 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3294 
3295 		des = skb_frag_dma_map(priv->device, frag, 0,
3296 				       skb_frag_size(frag),
3297 				       DMA_TO_DEVICE);
3298 		if (dma_mapping_error(priv->device, des))
3299 			goto dma_map_err;
3300 
3301 		stmmac_tso_allocator(priv, des, skb_frag_size(frag),
3302 				     (i == nfrags - 1), queue);
3303 
3304 		tx_q->tx_skbuff_dma[tx_q->cur_tx].buf = des;
3305 		tx_q->tx_skbuff_dma[tx_q->cur_tx].len = skb_frag_size(frag);
3306 		tx_q->tx_skbuff_dma[tx_q->cur_tx].map_as_page = true;
3307 	}
3308 
3309 	tx_q->tx_skbuff_dma[tx_q->cur_tx].last_segment = true;
3310 
3311 	/* Only the last descriptor gets to point to the skb. */
3312 	tx_q->tx_skbuff[tx_q->cur_tx] = skb;
3313 
3314 	/* Manage tx mitigation */
3315 	tx_packets = (tx_q->cur_tx + 1) - first_tx;
3316 	tx_q->tx_count_frames += tx_packets;
3317 
3318 	if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en)
3319 		set_ic = true;
3320 	else if (!priv->tx_coal_frames)
3321 		set_ic = false;
3322 	else if (tx_packets > priv->tx_coal_frames)
3323 		set_ic = true;
3324 	else if ((tx_q->tx_count_frames % priv->tx_coal_frames) < tx_packets)
3325 		set_ic = true;
3326 	else
3327 		set_ic = false;
3328 
3329 	if (set_ic) {
3330 		if (tx_q->tbs & STMMAC_TBS_AVAIL)
3331 			desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
3332 		else
3333 			desc = &tx_q->dma_tx[tx_q->cur_tx];
3334 
3335 		tx_q->tx_count_frames = 0;
3336 		stmmac_set_tx_ic(priv, desc);
3337 		priv->xstats.tx_set_ic_bit++;
3338 	}
3339 
3340 	/* We've used all descriptors we need for this skb, however,
3341 	 * advance cur_tx so that it references a fresh descriptor.
3342 	 * ndo_start_xmit will fill this descriptor the next time it's
3343 	 * called and stmmac_tx_clean may clean up to this descriptor.
3344 	 */
3345 	tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
3346 
3347 	if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
3348 		netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
3349 			  __func__);
3350 		netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
3351 	}
3352 
3353 	dev->stats.tx_bytes += skb->len;
3354 	priv->xstats.tx_tso_frames++;
3355 	priv->xstats.tx_tso_nfrags += nfrags;
3356 
3357 	if (priv->sarc_type)
3358 		stmmac_set_desc_sarc(priv, first, priv->sarc_type);
3359 
3360 	skb_tx_timestamp(skb);
3361 
3362 	if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
3363 		     priv->hwts_tx_en)) {
3364 		/* declare that device is doing timestamping */
3365 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3366 		stmmac_enable_tx_timestamp(priv, first);
3367 	}
3368 
3369 	/* Complete the first descriptor before granting the DMA */
3370 	stmmac_prepare_tso_tx_desc(priv, first, 1,
3371 			proto_hdr_len,
3372 			pay_len,
3373 			1, tx_q->tx_skbuff_dma[first_entry].last_segment,
3374 			hdr / 4, (skb->len - proto_hdr_len));
3375 
3376 	/* If context desc is used to change MSS */
3377 	if (mss_desc) {
3378 		/* Make sure that first descriptor has been completely
3379 		 * written, including its own bit. This is because MSS is
3380 		 * actually before first descriptor, so we need to make
3381 		 * sure that MSS's own bit is the last thing written.
3382 		 */
3383 		dma_wmb();
3384 		stmmac_set_tx_owner(priv, mss_desc);
3385 	}
3386 
3387 	/* The own bit must be the latest setting done when prepare the
3388 	 * descriptor and then barrier is needed to make sure that
3389 	 * all is coherent before granting the DMA engine.
3390 	 */
3391 	wmb();
3392 
3393 	if (netif_msg_pktdata(priv)) {
3394 		pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n",
3395 			__func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
3396 			tx_q->cur_tx, first, nfrags);
3397 		pr_info(">>> frame to be transmitted: ");
3398 		print_pkt(skb->data, skb_headlen(skb));
3399 	}
3400 
3401 	netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
3402 
3403 	if (tx_q->tbs & STMMAC_TBS_AVAIL)
3404 		desc_size = sizeof(struct dma_edesc);
3405 	else
3406 		desc_size = sizeof(struct dma_desc);
3407 
3408 	tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * desc_size);
3409 	stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue);
3410 	stmmac_tx_timer_arm(priv, queue);
3411 
3412 	return NETDEV_TX_OK;
3413 
3414 dma_map_err:
3415 	dev_err(priv->device, "Tx dma map failed\n");
3416 	dev_kfree_skb(skb);
3417 	priv->dev->stats.tx_dropped++;
3418 	return NETDEV_TX_OK;
3419 }
3420 
3421 /**
3422  *  stmmac_xmit - Tx entry point of the driver
3423  *  @skb : the socket buffer
3424  *  @dev : device pointer
3425  *  Description : this is the tx entry point of the driver.
3426  *  It programs the chain or the ring and supports oversized frames
3427  *  and SG feature.
3428  */
stmmac_xmit(struct sk_buff * skb,struct net_device * dev)3429 static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
3430 {
3431 	unsigned int first_entry, tx_packets, enh_desc;
3432 	struct stmmac_priv *priv = netdev_priv(dev);
3433 	unsigned int nopaged_len = skb_headlen(skb);
3434 	int i, csum_insertion = 0, is_jumbo = 0;
3435 	u32 queue = skb_get_queue_mapping(skb);
3436 	int nfrags = skb_shinfo(skb)->nr_frags;
3437 	int gso = skb_shinfo(skb)->gso_type;
3438 	struct dma_edesc *tbs_desc = NULL;
3439 	int entry, desc_size, first_tx;
3440 	struct dma_desc *desc, *first;
3441 	struct stmmac_tx_queue *tx_q;
3442 	bool has_vlan, set_ic;
3443 	dma_addr_t des;
3444 
3445 	tx_q = &priv->tx_queue[queue];
3446 	first_tx = tx_q->cur_tx;
3447 
3448 	if (priv->tx_path_in_lpi_mode)
3449 		stmmac_disable_eee_mode(priv);
3450 
3451 	/* Manage oversized TCP frames for GMAC4 device */
3452 	if (skb_is_gso(skb) && priv->tso) {
3453 		if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
3454 			return stmmac_tso_xmit(skb, dev);
3455 		if (priv->plat->has_gmac4 && (gso & SKB_GSO_UDP_L4))
3456 			return stmmac_tso_xmit(skb, dev);
3457 	}
3458 
3459 	if (unlikely(stmmac_tx_avail(priv, queue) < nfrags + 1)) {
3460 		if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
3461 			netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
3462 								queue));
3463 			/* This is a hard error, log it. */
3464 			netdev_err(priv->dev,
3465 				   "%s: Tx Ring full when queue awake\n",
3466 				   __func__);
3467 		}
3468 		return NETDEV_TX_BUSY;
3469 	}
3470 
3471 	/* Check if VLAN can be inserted by HW */
3472 	has_vlan = stmmac_vlan_insert(priv, skb, tx_q);
3473 
3474 	entry = tx_q->cur_tx;
3475 	first_entry = entry;
3476 	WARN_ON(tx_q->tx_skbuff[first_entry]);
3477 
3478 	csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
3479 
3480 	if (likely(priv->extend_desc))
3481 		desc = (struct dma_desc *)(tx_q->dma_etx + entry);
3482 	else if (tx_q->tbs & STMMAC_TBS_AVAIL)
3483 		desc = &tx_q->dma_entx[entry].basic;
3484 	else
3485 		desc = tx_q->dma_tx + entry;
3486 
3487 	first = desc;
3488 
3489 	if (has_vlan)
3490 		stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT);
3491 
3492 	enh_desc = priv->plat->enh_desc;
3493 	/* To program the descriptors according to the size of the frame */
3494 	if (enh_desc)
3495 		is_jumbo = stmmac_is_jumbo_frm(priv, skb->len, enh_desc);
3496 
3497 	if (unlikely(is_jumbo)) {
3498 		entry = stmmac_jumbo_frm(priv, tx_q, skb, csum_insertion);
3499 		if (unlikely(entry < 0) && (entry != -EINVAL))
3500 			goto dma_map_err;
3501 	}
3502 
3503 	for (i = 0; i < nfrags; i++) {
3504 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3505 		int len = skb_frag_size(frag);
3506 		bool last_segment = (i == (nfrags - 1));
3507 
3508 		entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
3509 		WARN_ON(tx_q->tx_skbuff[entry]);
3510 
3511 		if (likely(priv->extend_desc))
3512 			desc = (struct dma_desc *)(tx_q->dma_etx + entry);
3513 		else if (tx_q->tbs & STMMAC_TBS_AVAIL)
3514 			desc = &tx_q->dma_entx[entry].basic;
3515 		else
3516 			desc = tx_q->dma_tx + entry;
3517 
3518 		des = skb_frag_dma_map(priv->device, frag, 0, len,
3519 				       DMA_TO_DEVICE);
3520 		if (dma_mapping_error(priv->device, des))
3521 			goto dma_map_err; /* should reuse desc w/o issues */
3522 
3523 		tx_q->tx_skbuff_dma[entry].buf = des;
3524 
3525 		stmmac_set_desc_addr(priv, desc, des);
3526 
3527 		tx_q->tx_skbuff_dma[entry].map_as_page = true;
3528 		tx_q->tx_skbuff_dma[entry].len = len;
3529 		tx_q->tx_skbuff_dma[entry].last_segment = last_segment;
3530 
3531 		/* Prepare the descriptor and set the own bit too */
3532 		stmmac_prepare_tx_desc(priv, desc, 0, len, csum_insertion,
3533 				priv->mode, 1, last_segment, skb->len);
3534 	}
3535 
3536 	/* Only the last descriptor gets to point to the skb. */
3537 	tx_q->tx_skbuff[entry] = skb;
3538 
3539 	/* According to the coalesce parameter the IC bit for the latest
3540 	 * segment is reset and the timer re-started to clean the tx status.
3541 	 * This approach takes care about the fragments: desc is the first
3542 	 * element in case of no SG.
3543 	 */
3544 	tx_packets = (entry + 1) - first_tx;
3545 	tx_q->tx_count_frames += tx_packets;
3546 
3547 	if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en)
3548 		set_ic = true;
3549 	else if (!priv->tx_coal_frames)
3550 		set_ic = false;
3551 	else if (tx_packets > priv->tx_coal_frames)
3552 		set_ic = true;
3553 	else if ((tx_q->tx_count_frames % priv->tx_coal_frames) < tx_packets)
3554 		set_ic = true;
3555 	else
3556 		set_ic = false;
3557 
3558 	if (set_ic) {
3559 		if (likely(priv->extend_desc))
3560 			desc = &tx_q->dma_etx[entry].basic;
3561 		else if (tx_q->tbs & STMMAC_TBS_AVAIL)
3562 			desc = &tx_q->dma_entx[entry].basic;
3563 		else
3564 			desc = &tx_q->dma_tx[entry];
3565 
3566 		tx_q->tx_count_frames = 0;
3567 		stmmac_set_tx_ic(priv, desc);
3568 		priv->xstats.tx_set_ic_bit++;
3569 	}
3570 
3571 	/* We've used all descriptors we need for this skb, however,
3572 	 * advance cur_tx so that it references a fresh descriptor.
3573 	 * ndo_start_xmit will fill this descriptor the next time it's
3574 	 * called and stmmac_tx_clean may clean up to this descriptor.
3575 	 */
3576 	entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
3577 	tx_q->cur_tx = entry;
3578 
3579 	if (netif_msg_pktdata(priv)) {
3580 		netdev_dbg(priv->dev,
3581 			   "%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
3582 			   __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
3583 			   entry, first, nfrags);
3584 
3585 		netdev_dbg(priv->dev, ">>> frame to be transmitted: ");
3586 		print_pkt(skb->data, skb->len);
3587 	}
3588 
3589 	if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
3590 		netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
3591 			  __func__);
3592 		netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
3593 	}
3594 
3595 	dev->stats.tx_bytes += skb->len;
3596 
3597 	if (priv->sarc_type)
3598 		stmmac_set_desc_sarc(priv, first, priv->sarc_type);
3599 
3600 	skb_tx_timestamp(skb);
3601 
3602 	/* Ready to fill the first descriptor and set the OWN bit w/o any
3603 	 * problems because all the descriptors are actually ready to be
3604 	 * passed to the DMA engine.
3605 	 */
3606 	if (likely(!is_jumbo)) {
3607 		bool last_segment = (nfrags == 0);
3608 
3609 		des = dma_map_single(priv->device, skb->data,
3610 				     nopaged_len, DMA_TO_DEVICE);
3611 		if (dma_mapping_error(priv->device, des))
3612 			goto dma_map_err;
3613 
3614 		tx_q->tx_skbuff_dma[first_entry].buf = des;
3615 
3616 		stmmac_set_desc_addr(priv, first, des);
3617 
3618 		tx_q->tx_skbuff_dma[first_entry].len = nopaged_len;
3619 		tx_q->tx_skbuff_dma[first_entry].last_segment = last_segment;
3620 
3621 		if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
3622 			     priv->hwts_tx_en)) {
3623 			/* declare that device is doing timestamping */
3624 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3625 			stmmac_enable_tx_timestamp(priv, first);
3626 		}
3627 
3628 		/* Prepare the first descriptor setting the OWN bit too */
3629 		stmmac_prepare_tx_desc(priv, first, 1, nopaged_len,
3630 				csum_insertion, priv->mode, 0, last_segment,
3631 				skb->len);
3632 	}
3633 
3634 	if (tx_q->tbs & STMMAC_TBS_EN) {
3635 		struct timespec64 ts = ns_to_timespec64(skb->tstamp);
3636 
3637 		tbs_desc = &tx_q->dma_entx[first_entry];
3638 		stmmac_set_desc_tbs(priv, tbs_desc, ts.tv_sec, ts.tv_nsec);
3639 	}
3640 
3641 	stmmac_set_tx_owner(priv, first);
3642 
3643 	/* The own bit must be the latest setting done when prepare the
3644 	 * descriptor and then barrier is needed to make sure that
3645 	 * all is coherent before granting the DMA engine.
3646 	 */
3647 	wmb();
3648 
3649 	netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
3650 
3651 	stmmac_enable_dma_transmission(priv, priv->ioaddr);
3652 
3653 	if (likely(priv->extend_desc))
3654 		desc_size = sizeof(struct dma_extended_desc);
3655 	else if (tx_q->tbs & STMMAC_TBS_AVAIL)
3656 		desc_size = sizeof(struct dma_edesc);
3657 	else
3658 		desc_size = sizeof(struct dma_desc);
3659 
3660 	tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * desc_size);
3661 	stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue);
3662 	stmmac_tx_timer_arm(priv, queue);
3663 
3664 	return NETDEV_TX_OK;
3665 
3666 dma_map_err:
3667 	netdev_err(priv->dev, "Tx DMA map failed\n");
3668 	dev_kfree_skb(skb);
3669 	priv->dev->stats.tx_dropped++;
3670 	return NETDEV_TX_OK;
3671 }
3672 
stmmac_rx_vlan(struct net_device * dev,struct sk_buff * skb)3673 static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
3674 {
3675 	struct vlan_ethhdr *veth;
3676 	__be16 vlan_proto;
3677 	u16 vlanid;
3678 
3679 	veth = (struct vlan_ethhdr *)skb->data;
3680 	vlan_proto = veth->h_vlan_proto;
3681 
3682 	if ((vlan_proto == htons(ETH_P_8021Q) &&
3683 	     dev->features & NETIF_F_HW_VLAN_CTAG_RX) ||
3684 	    (vlan_proto == htons(ETH_P_8021AD) &&
3685 	     dev->features & NETIF_F_HW_VLAN_STAG_RX)) {
3686 		/* pop the vlan tag */
3687 		vlanid = ntohs(veth->h_vlan_TCI);
3688 		memmove(skb->data + VLAN_HLEN, veth, ETH_ALEN * 2);
3689 		skb_pull(skb, VLAN_HLEN);
3690 		__vlan_hwaccel_put_tag(skb, vlan_proto, vlanid);
3691 	}
3692 }
3693 
3694 /**
3695  * stmmac_rx_refill - refill used skb preallocated buffers
3696  * @priv: driver private structure
3697  * @queue: RX queue index
3698  * Description : this is to reallocate the skb for the reception process
3699  * that is based on zero-copy.
3700  */
stmmac_rx_refill(struct stmmac_priv * priv,u32 queue)3701 static inline void stmmac_rx_refill(struct stmmac_priv *priv, u32 queue)
3702 {
3703 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
3704 	int len, dirty = stmmac_rx_dirty(priv, queue);
3705 	unsigned int entry = rx_q->dirty_rx;
3706 	gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
3707 
3708 	if (priv->dma_cap.addr64 <= 32)
3709 		gfp |= GFP_DMA32;
3710 
3711 	len = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE) * PAGE_SIZE;
3712 
3713 	while (dirty-- > 0) {
3714 		struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry];
3715 		struct dma_desc *p;
3716 		bool use_rx_wd;
3717 
3718 		if (priv->extend_desc)
3719 			p = (struct dma_desc *)(rx_q->dma_erx + entry);
3720 		else
3721 			p = rx_q->dma_rx + entry;
3722 
3723 		if (!buf->page) {
3724 			buf->page = page_pool_alloc_pages(rx_q->page_pool, gfp);
3725 			if (!buf->page)
3726 				break;
3727 		}
3728 
3729 		if (priv->sph && !buf->sec_page) {
3730 			buf->sec_page = page_pool_alloc_pages(rx_q->page_pool, gfp);
3731 			if (!buf->sec_page)
3732 				break;
3733 
3734 			buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
3735 		}
3736 
3737 		buf->addr = page_pool_get_dma_addr(buf->page);
3738 		stmmac_set_desc_addr(priv, p, buf->addr);
3739 		if (priv->sph)
3740 			stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true);
3741 		else
3742 			stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false);
3743 		stmmac_refill_desc3(priv, rx_q, p);
3744 
3745 		rx_q->rx_count_frames++;
3746 		rx_q->rx_count_frames += priv->rx_coal_frames;
3747 		if (rx_q->rx_count_frames > priv->rx_coal_frames)
3748 			rx_q->rx_count_frames = 0;
3749 
3750 		use_rx_wd = !priv->rx_coal_frames;
3751 		use_rx_wd |= rx_q->rx_count_frames > 0;
3752 		if (!priv->use_riwt)
3753 			use_rx_wd = false;
3754 
3755 		dma_wmb();
3756 		stmmac_set_rx_owner(priv, p, use_rx_wd);
3757 
3758 		entry = STMMAC_GET_ENTRY(entry, priv->dma_rx_size);
3759 	}
3760 	rx_q->dirty_rx = entry;
3761 	rx_q->rx_tail_addr = rx_q->dma_rx_phy +
3762 			    (rx_q->dirty_rx * sizeof(struct dma_desc));
3763 	stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue);
3764 }
3765 
stmmac_rx_buf1_len(struct stmmac_priv * priv,struct dma_desc * p,int status,unsigned int len)3766 static unsigned int stmmac_rx_buf1_len(struct stmmac_priv *priv,
3767 				       struct dma_desc *p,
3768 				       int status, unsigned int len)
3769 {
3770 	unsigned int plen = 0, hlen = 0;
3771 	int coe = priv->hw->rx_csum;
3772 
3773 	/* Not first descriptor, buffer is always zero */
3774 	if (priv->sph && len)
3775 		return 0;
3776 
3777 	/* First descriptor, get split header length */
3778 	stmmac_get_rx_header_len(priv, p, &hlen);
3779 	if (priv->sph && hlen) {
3780 		priv->xstats.rx_split_hdr_pkt_n++;
3781 		return hlen;
3782 	}
3783 
3784 	/* First descriptor, not last descriptor and not split header */
3785 	if (status & rx_not_ls)
3786 		return priv->dma_buf_sz;
3787 
3788 	plen = stmmac_get_rx_frame_len(priv, p, coe);
3789 
3790 	/* First descriptor and last descriptor and not split header */
3791 	return min_t(unsigned int, priv->dma_buf_sz, plen);
3792 }
3793 
stmmac_rx_buf2_len(struct stmmac_priv * priv,struct dma_desc * p,int status,unsigned int len)3794 static unsigned int stmmac_rx_buf2_len(struct stmmac_priv *priv,
3795 				       struct dma_desc *p,
3796 				       int status, unsigned int len)
3797 {
3798 	int coe = priv->hw->rx_csum;
3799 	unsigned int plen = 0;
3800 
3801 	/* Not split header, buffer is not available */
3802 	if (!priv->sph)
3803 		return 0;
3804 
3805 	/* Not last descriptor */
3806 	if (status & rx_not_ls)
3807 		return priv->dma_buf_sz;
3808 
3809 	plen = stmmac_get_rx_frame_len(priv, p, coe);
3810 
3811 	/* Last descriptor */
3812 	return plen - len;
3813 }
3814 
3815 /**
3816  * stmmac_rx - manage the receive process
3817  * @priv: driver private structure
3818  * @limit: napi bugget
3819  * @queue: RX queue index.
3820  * Description :  this the function called by the napi poll method.
3821  * It gets all the frames inside the ring.
3822  */
stmmac_rx(struct stmmac_priv * priv,int limit,u32 queue)3823 static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
3824 {
3825 	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
3826 	struct stmmac_channel *ch = &priv->channel[queue];
3827 	unsigned int count = 0, error = 0, len = 0;
3828 	int status = 0, coe = priv->hw->rx_csum;
3829 	unsigned int next_entry = rx_q->cur_rx;
3830 	unsigned int desc_size;
3831 	struct sk_buff *skb = NULL;
3832 
3833 	if (netif_msg_rx_status(priv)) {
3834 		void *rx_head;
3835 
3836 		netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
3837 		if (priv->extend_desc) {
3838 			rx_head = (void *)rx_q->dma_erx;
3839 			desc_size = sizeof(struct dma_extended_desc);
3840 		} else {
3841 			rx_head = (void *)rx_q->dma_rx;
3842 			desc_size = sizeof(struct dma_desc);
3843 		}
3844 
3845 		stmmac_display_ring(priv, rx_head, priv->dma_rx_size, true,
3846 				    rx_q->dma_rx_phy, desc_size);
3847 	}
3848 	while (count < limit) {
3849 		unsigned int buf1_len = 0, buf2_len = 0;
3850 		enum pkt_hash_types hash_type;
3851 		struct stmmac_rx_buffer *buf;
3852 		struct dma_desc *np, *p;
3853 		int entry;
3854 		u32 hash;
3855 
3856 		if (!count && rx_q->state_saved) {
3857 			skb = rx_q->state.skb;
3858 			error = rx_q->state.error;
3859 			len = rx_q->state.len;
3860 		} else {
3861 			rx_q->state_saved = false;
3862 			skb = NULL;
3863 			error = 0;
3864 			len = 0;
3865 		}
3866 
3867 		if ((count >= limit - 1) && limit > 1)
3868 			break;
3869 
3870 read_again:
3871 		buf1_len = 0;
3872 		buf2_len = 0;
3873 		entry = next_entry;
3874 		buf = &rx_q->buf_pool[entry];
3875 
3876 		if (priv->extend_desc)
3877 			p = (struct dma_desc *)(rx_q->dma_erx + entry);
3878 		else
3879 			p = rx_q->dma_rx + entry;
3880 
3881 		/* read the status of the incoming frame */
3882 		status = stmmac_rx_status(priv, &priv->dev->stats,
3883 				&priv->xstats, p);
3884 		/* check if managed by the DMA otherwise go ahead */
3885 		if (unlikely(status & dma_own))
3886 			break;
3887 
3888 		rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx,
3889 						priv->dma_rx_size);
3890 		next_entry = rx_q->cur_rx;
3891 
3892 		if (priv->extend_desc)
3893 			np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
3894 		else
3895 			np = rx_q->dma_rx + next_entry;
3896 
3897 		prefetch(np);
3898 
3899 		if (priv->extend_desc)
3900 			stmmac_rx_extended_status(priv, &priv->dev->stats,
3901 					&priv->xstats, rx_q->dma_erx + entry);
3902 		if (unlikely(status == discard_frame)) {
3903 			page_pool_recycle_direct(rx_q->page_pool, buf->page);
3904 			buf->page = NULL;
3905 			error = 1;
3906 			if (!priv->hwts_rx_en)
3907 				priv->dev->stats.rx_errors++;
3908 		}
3909 
3910 		if (unlikely(error && (status & rx_not_ls)))
3911 			goto read_again;
3912 		if (unlikely(error)) {
3913 			dev_kfree_skb(skb);
3914 			skb = NULL;
3915 			count++;
3916 			continue;
3917 		}
3918 
3919 		/* Buffer is good. Go on. */
3920 
3921 		prefetch(page_address(buf->page));
3922 		if (buf->sec_page)
3923 			prefetch(page_address(buf->sec_page));
3924 
3925 		buf1_len = stmmac_rx_buf1_len(priv, p, status, len);
3926 		len += buf1_len;
3927 		buf2_len = stmmac_rx_buf2_len(priv, p, status, len);
3928 		len += buf2_len;
3929 
3930 		/* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
3931 		 * Type frames (LLC/LLC-SNAP)
3932 		 *
3933 		 * llc_snap is never checked in GMAC >= 4, so this ACS
3934 		 * feature is always disabled and packets need to be
3935 		 * stripped manually.
3936 		 */
3937 		if (likely(!(status & rx_not_ls)) &&
3938 		    (likely(priv->synopsys_id >= DWMAC_CORE_4_00) ||
3939 		     unlikely(status != llc_snap))) {
3940 			if (buf2_len)
3941 				buf2_len -= ETH_FCS_LEN;
3942 			else
3943 				buf1_len -= ETH_FCS_LEN;
3944 
3945 			len -= ETH_FCS_LEN;
3946 		}
3947 
3948 		if (!skb) {
3949 			skb = napi_alloc_skb(&ch->rx_napi, buf1_len);
3950 			if (!skb) {
3951 				priv->dev->stats.rx_dropped++;
3952 				count++;
3953 				goto drain_data;
3954 			}
3955 
3956 			dma_sync_single_for_cpu(priv->device, buf->addr,
3957 						buf1_len, DMA_FROM_DEVICE);
3958 			skb_copy_to_linear_data(skb, page_address(buf->page),
3959 						buf1_len);
3960 			skb_put(skb, buf1_len);
3961 
3962 			/* Data payload copied into SKB, page ready for recycle */
3963 			page_pool_recycle_direct(rx_q->page_pool, buf->page);
3964 			buf->page = NULL;
3965 		} else if (buf1_len) {
3966 			dma_sync_single_for_cpu(priv->device, buf->addr,
3967 						buf1_len, DMA_FROM_DEVICE);
3968 			skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
3969 					buf->page, 0, buf1_len,
3970 					priv->dma_buf_sz);
3971 
3972 			/* Data payload appended into SKB */
3973 			page_pool_release_page(rx_q->page_pool, buf->page);
3974 			buf->page = NULL;
3975 		}
3976 
3977 		if (buf2_len) {
3978 			dma_sync_single_for_cpu(priv->device, buf->sec_addr,
3979 						buf2_len, DMA_FROM_DEVICE);
3980 			skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
3981 					buf->sec_page, 0, buf2_len,
3982 					priv->dma_buf_sz);
3983 
3984 			/* Data payload appended into SKB */
3985 			page_pool_release_page(rx_q->page_pool, buf->sec_page);
3986 			buf->sec_page = NULL;
3987 		}
3988 
3989 drain_data:
3990 		if (likely(status & rx_not_ls))
3991 			goto read_again;
3992 		if (!skb)
3993 			continue;
3994 
3995 		/* Got entire packet into SKB. Finish it. */
3996 
3997 		stmmac_get_rx_hwtstamp(priv, p, np, skb);
3998 		stmmac_rx_vlan(priv->dev, skb);
3999 		skb->protocol = eth_type_trans(skb, priv->dev);
4000 
4001 		if (unlikely(!coe))
4002 			skb_checksum_none_assert(skb);
4003 		else
4004 			skb->ip_summed = CHECKSUM_UNNECESSARY;
4005 
4006 		if (!stmmac_get_rx_hash(priv, p, &hash, &hash_type))
4007 			skb_set_hash(skb, hash, hash_type);
4008 
4009 		skb_record_rx_queue(skb, queue);
4010 		napi_gro_receive(&ch->rx_napi, skb);
4011 		skb = NULL;
4012 
4013 		priv->dev->stats.rx_packets++;
4014 		priv->dev->stats.rx_bytes += len;
4015 		count++;
4016 	}
4017 
4018 	if (status & rx_not_ls || skb) {
4019 		rx_q->state_saved = true;
4020 		rx_q->state.skb = skb;
4021 		rx_q->state.error = error;
4022 		rx_q->state.len = len;
4023 	}
4024 
4025 	stmmac_rx_refill(priv, queue);
4026 
4027 	priv->xstats.rx_pkt_n += count;
4028 
4029 	return count;
4030 }
4031 
stmmac_napi_poll_rx(struct napi_struct * napi,int budget)4032 static int stmmac_napi_poll_rx(struct napi_struct *napi, int budget)
4033 {
4034 	struct stmmac_channel *ch =
4035 		container_of(napi, struct stmmac_channel, rx_napi);
4036 	struct stmmac_priv *priv = ch->priv_data;
4037 	u32 chan = ch->index;
4038 	int work_done;
4039 
4040 	priv->xstats.napi_poll++;
4041 
4042 	work_done = stmmac_rx(priv, budget, chan);
4043 	if (work_done < budget && napi_complete_done(napi, work_done)) {
4044 		unsigned long flags;
4045 
4046 		spin_lock_irqsave(&ch->lock, flags);
4047 		stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
4048 		spin_unlock_irqrestore(&ch->lock, flags);
4049 	}
4050 
4051 	return work_done;
4052 }
4053 
stmmac_napi_poll_tx(struct napi_struct * napi,int budget)4054 static int stmmac_napi_poll_tx(struct napi_struct *napi, int budget)
4055 {
4056 	struct stmmac_channel *ch =
4057 		container_of(napi, struct stmmac_channel, tx_napi);
4058 	struct stmmac_priv *priv = ch->priv_data;
4059 	u32 chan = ch->index;
4060 	int work_done;
4061 
4062 	priv->xstats.napi_poll++;
4063 
4064 	work_done = stmmac_tx_clean(priv, priv->dma_tx_size, chan);
4065 	work_done = min(work_done, budget);
4066 
4067 	if (work_done < budget && napi_complete_done(napi, work_done)) {
4068 		unsigned long flags;
4069 
4070 		spin_lock_irqsave(&ch->lock, flags);
4071 		stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
4072 		spin_unlock_irqrestore(&ch->lock, flags);
4073 	}
4074 
4075 	return work_done;
4076 }
4077 
4078 /**
4079  *  stmmac_tx_timeout
4080  *  @dev : Pointer to net device structure
4081  *  @txqueue: the index of the hanging transmit queue
4082  *  Description: this function is called when a packet transmission fails to
4083  *   complete within a reasonable time. The driver will mark the error in the
4084  *   netdev structure and arrange for the device to be reset to a sane state
4085  *   in order to transmit a new packet.
4086  */
stmmac_tx_timeout(struct net_device * dev,unsigned int txqueue)4087 static void stmmac_tx_timeout(struct net_device *dev, unsigned int txqueue)
4088 {
4089 	struct stmmac_priv *priv = netdev_priv(dev);
4090 
4091 	stmmac_global_err(priv);
4092 }
4093 
4094 /**
4095  *  stmmac_set_rx_mode - entry point for multicast addressing
4096  *  @dev : pointer to the device structure
4097  *  Description:
4098  *  This function is a driver entry point which gets called by the kernel
4099  *  whenever multicast addresses must be enabled/disabled.
4100  *  Return value:
4101  *  void.
4102  */
stmmac_set_rx_mode(struct net_device * dev)4103 static void stmmac_set_rx_mode(struct net_device *dev)
4104 {
4105 	struct stmmac_priv *priv = netdev_priv(dev);
4106 
4107 	stmmac_set_filter(priv, priv->hw, dev);
4108 }
4109 
4110 /**
4111  *  stmmac_change_mtu - entry point to change MTU size for the device.
4112  *  @dev : device pointer.
4113  *  @new_mtu : the new MTU size for the device.
4114  *  Description: the Maximum Transfer Unit (MTU) is used by the network layer
4115  *  to drive packet transmission. Ethernet has an MTU of 1500 octets
4116  *  (ETH_DATA_LEN). This value can be changed with ifconfig.
4117  *  Return value:
4118  *  0 on success and an appropriate (-)ve integer as defined in errno.h
4119  *  file on failure.
4120  */
stmmac_change_mtu(struct net_device * dev,int new_mtu)4121 static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
4122 {
4123 	struct stmmac_priv *priv = netdev_priv(dev);
4124 	int txfifosz = priv->plat->tx_fifo_size;
4125 	const int mtu = new_mtu;
4126 
4127 	if (txfifosz == 0)
4128 		txfifosz = priv->dma_cap.tx_fifo_size;
4129 
4130 	txfifosz /= priv->plat->tx_queues_to_use;
4131 
4132 	if (netif_running(dev)) {
4133 		netdev_err(priv->dev, "must be stopped to change its MTU\n");
4134 		return -EBUSY;
4135 	}
4136 
4137 	new_mtu = STMMAC_ALIGN(new_mtu);
4138 
4139 	/* If condition true, FIFO is too small or MTU too large */
4140 	if ((txfifosz < new_mtu) || (new_mtu > BUF_SIZE_16KiB))
4141 		return -EINVAL;
4142 
4143 	dev->mtu = mtu;
4144 
4145 	netdev_update_features(dev);
4146 
4147 	return 0;
4148 }
4149 
stmmac_fix_features(struct net_device * dev,netdev_features_t features)4150 static netdev_features_t stmmac_fix_features(struct net_device *dev,
4151 					     netdev_features_t features)
4152 {
4153 	struct stmmac_priv *priv = netdev_priv(dev);
4154 
4155 	if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
4156 		features &= ~NETIF_F_RXCSUM;
4157 
4158 	if (!priv->plat->tx_coe)
4159 		features &= ~NETIF_F_CSUM_MASK;
4160 
4161 	/* Some GMAC devices have a bugged Jumbo frame support that
4162 	 * needs to have the Tx COE disabled for oversized frames
4163 	 * (due to limited buffer sizes). In this case we disable
4164 	 * the TX csum insertion in the TDES and not use SF.
4165 	 */
4166 	if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
4167 		features &= ~NETIF_F_CSUM_MASK;
4168 
4169 	/* Disable tso if asked by ethtool */
4170 	if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
4171 		if (features & NETIF_F_TSO)
4172 			priv->tso = true;
4173 		else
4174 			priv->tso = false;
4175 	}
4176 
4177 	return features;
4178 }
4179 
stmmac_set_features(struct net_device * netdev,netdev_features_t features)4180 static int stmmac_set_features(struct net_device *netdev,
4181 			       netdev_features_t features)
4182 {
4183 	struct stmmac_priv *priv = netdev_priv(netdev);
4184 	bool sph_en;
4185 	u32 chan;
4186 
4187 	/* Keep the COE Type in case of csum is supporting */
4188 	if (features & NETIF_F_RXCSUM)
4189 		priv->hw->rx_csum = priv->plat->rx_coe;
4190 	else
4191 		priv->hw->rx_csum = 0;
4192 	/* No check needed because rx_coe has been set before and it will be
4193 	 * fixed in case of issue.
4194 	 */
4195 	stmmac_rx_ipc(priv, priv->hw);
4196 
4197 	sph_en = (priv->hw->rx_csum > 0) && priv->sph;
4198 	for (chan = 0; chan < priv->plat->rx_queues_to_use; chan++)
4199 		stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
4200 
4201 	return 0;
4202 }
4203 
4204 /**
4205  *  stmmac_interrupt - main ISR
4206  *  @irq: interrupt number.
4207  *  @dev_id: to pass the net device pointer (must be valid).
4208  *  Description: this is the main driver interrupt service routine.
4209  *  It can call:
4210  *  o DMA service routine (to manage incoming frame reception and transmission
4211  *    status)
4212  *  o Core interrupts to manage: remote wake-up, management counter, LPI
4213  *    interrupts.
4214  */
stmmac_interrupt(int irq,void * dev_id)4215 static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
4216 {
4217 	struct net_device *dev = (struct net_device *)dev_id;
4218 	struct stmmac_priv *priv = netdev_priv(dev);
4219 	u32 rx_cnt = priv->plat->rx_queues_to_use;
4220 	u32 tx_cnt = priv->plat->tx_queues_to_use;
4221 	u32 queues_count;
4222 	u32 queue;
4223 	bool xmac;
4224 
4225 	xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
4226 	queues_count = (rx_cnt > tx_cnt) ? rx_cnt : tx_cnt;
4227 
4228 	if (priv->irq_wake)
4229 		pm_wakeup_event(priv->device, 0);
4230 
4231 	/* Check if adapter is up */
4232 	if (test_bit(STMMAC_DOWN, &priv->state))
4233 		return IRQ_HANDLED;
4234 	/* Check if a fatal error happened */
4235 	if (stmmac_safety_feat_interrupt(priv))
4236 		return IRQ_HANDLED;
4237 
4238 	/* To handle GMAC own interrupts */
4239 	if ((priv->plat->has_gmac) || xmac) {
4240 		int status = stmmac_host_irq_status(priv, priv->hw, &priv->xstats);
4241 
4242 		if (unlikely(status)) {
4243 			/* For LPI we need to save the tx status */
4244 			if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
4245 				priv->tx_path_in_lpi_mode = true;
4246 			if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
4247 				priv->tx_path_in_lpi_mode = false;
4248 		}
4249 
4250 		for (queue = 0; queue < queues_count; queue++) {
4251 			status = stmmac_host_mtl_irq_status(priv, priv->hw,
4252 							    queue);
4253 		}
4254 
4255 		/* PCS link status */
4256 		if (priv->hw->pcs) {
4257 			if (priv->xstats.pcs_link)
4258 				netif_carrier_on(dev);
4259 			else
4260 				netif_carrier_off(dev);
4261 		}
4262 	}
4263 
4264 	/* To handle DMA interrupts */
4265 	stmmac_dma_interrupt(priv);
4266 
4267 	return IRQ_HANDLED;
4268 }
4269 
4270 #ifdef CONFIG_NET_POLL_CONTROLLER
4271 /* Polling receive - used by NETCONSOLE and other diagnostic tools
4272  * to allow network I/O with interrupts disabled.
4273  */
stmmac_poll_controller(struct net_device * dev)4274 static void stmmac_poll_controller(struct net_device *dev)
4275 {
4276 	disable_irq(dev->irq);
4277 	stmmac_interrupt(dev->irq, dev);
4278 	enable_irq(dev->irq);
4279 }
4280 #endif
4281 
4282 /**
4283  *  stmmac_ioctl - Entry point for the Ioctl
4284  *  @dev: Device pointer.
4285  *  @rq: An IOCTL specefic structure, that can contain a pointer to
4286  *  a proprietary structure used to pass information to the driver.
4287  *  @cmd: IOCTL command
4288  *  Description:
4289  *  Currently it supports the phy_mii_ioctl(...) and HW time stamping.
4290  */
stmmac_ioctl(struct net_device * dev,struct ifreq * rq,int cmd)4291 static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
4292 {
4293 	struct stmmac_priv *priv = netdev_priv (dev);
4294 	int ret = -EOPNOTSUPP;
4295 
4296 	if (!netif_running(dev))
4297 		return -EINVAL;
4298 
4299 	switch (cmd) {
4300 	case SIOCGMIIPHY:
4301 	case SIOCGMIIREG:
4302 	case SIOCSMIIREG:
4303 		ret = phylink_mii_ioctl(priv->phylink, rq, cmd);
4304 		break;
4305 	case SIOCSHWTSTAMP:
4306 		ret = stmmac_hwtstamp_set(dev, rq);
4307 		break;
4308 	case SIOCGHWTSTAMP:
4309 		ret = stmmac_hwtstamp_get(dev, rq);
4310 		break;
4311 	default:
4312 		break;
4313 	}
4314 
4315 	return ret;
4316 }
4317 
stmmac_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)4318 static int stmmac_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
4319 				    void *cb_priv)
4320 {
4321 	struct stmmac_priv *priv = cb_priv;
4322 	int ret = -EOPNOTSUPP;
4323 
4324 	if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
4325 		return ret;
4326 
4327 	stmmac_disable_all_queues(priv);
4328 
4329 	switch (type) {
4330 	case TC_SETUP_CLSU32:
4331 		ret = stmmac_tc_setup_cls_u32(priv, priv, type_data);
4332 		break;
4333 	case TC_SETUP_CLSFLOWER:
4334 		ret = stmmac_tc_setup_cls(priv, priv, type_data);
4335 		break;
4336 	default:
4337 		break;
4338 	}
4339 
4340 	stmmac_enable_all_queues(priv);
4341 	return ret;
4342 }
4343 
4344 static LIST_HEAD(stmmac_block_cb_list);
4345 
stmmac_setup_tc(struct net_device * ndev,enum tc_setup_type type,void * type_data)4346 static int stmmac_setup_tc(struct net_device *ndev, enum tc_setup_type type,
4347 			   void *type_data)
4348 {
4349 	struct stmmac_priv *priv = netdev_priv(ndev);
4350 
4351 	switch (type) {
4352 	case TC_SETUP_BLOCK:
4353 		return flow_block_cb_setup_simple(type_data,
4354 						  &stmmac_block_cb_list,
4355 						  stmmac_setup_tc_block_cb,
4356 						  priv, priv, true);
4357 	case TC_SETUP_QDISC_CBS:
4358 		return stmmac_tc_setup_cbs(priv, priv, type_data);
4359 	case TC_SETUP_QDISC_TAPRIO:
4360 		return stmmac_tc_setup_taprio(priv, priv, type_data);
4361 	case TC_SETUP_QDISC_ETF:
4362 		return stmmac_tc_setup_etf(priv, priv, type_data);
4363 	default:
4364 		return -EOPNOTSUPP;
4365 	}
4366 }
4367 
stmmac_select_queue(struct net_device * dev,struct sk_buff * skb,struct net_device * sb_dev)4368 static u16 stmmac_select_queue(struct net_device *dev, struct sk_buff *skb,
4369 			       struct net_device *sb_dev)
4370 {
4371 	int gso = skb_shinfo(skb)->gso_type;
4372 
4373 	if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6 | SKB_GSO_UDP_L4)) {
4374 		/*
4375 		 * There is no way to determine the number of TSO/USO
4376 		 * capable Queues. Let's use always the Queue 0
4377 		 * because if TSO/USO is supported then at least this
4378 		 * one will be capable.
4379 		 */
4380 		return 0;
4381 	}
4382 
4383 	return netdev_pick_tx(dev, skb, NULL) % dev->real_num_tx_queues;
4384 }
4385 
stmmac_set_mac_address(struct net_device * ndev,void * addr)4386 static int stmmac_set_mac_address(struct net_device *ndev, void *addr)
4387 {
4388 	struct stmmac_priv *priv = netdev_priv(ndev);
4389 	int ret = 0;
4390 
4391 	ret = pm_runtime_get_sync(priv->device);
4392 	if (ret < 0) {
4393 		pm_runtime_put_noidle(priv->device);
4394 		return ret;
4395 	}
4396 
4397 	ret = eth_mac_addr(ndev, addr);
4398 	if (ret)
4399 		goto set_mac_error;
4400 
4401 	stmmac_set_umac_addr(priv, priv->hw, ndev->dev_addr, 0);
4402 
4403 set_mac_error:
4404 	pm_runtime_put(priv->device);
4405 
4406 	return ret;
4407 }
4408 
4409 #ifdef CONFIG_DEBUG_FS
4410 static struct dentry *stmmac_fs_dir;
4411 
sysfs_display_ring(void * head,int size,int extend_desc,struct seq_file * seq,dma_addr_t dma_phy_addr)4412 static void sysfs_display_ring(void *head, int size, int extend_desc,
4413 			       struct seq_file *seq, dma_addr_t dma_phy_addr)
4414 {
4415 	int i;
4416 	struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
4417 	struct dma_desc *p = (struct dma_desc *)head;
4418 	dma_addr_t dma_addr;
4419 
4420 	for (i = 0; i < size; i++) {
4421 		if (extend_desc) {
4422 			dma_addr = dma_phy_addr + i * sizeof(*ep);
4423 			seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n",
4424 				   i, &dma_addr,
4425 				   le32_to_cpu(ep->basic.des0),
4426 				   le32_to_cpu(ep->basic.des1),
4427 				   le32_to_cpu(ep->basic.des2),
4428 				   le32_to_cpu(ep->basic.des3));
4429 			ep++;
4430 		} else {
4431 			dma_addr = dma_phy_addr + i * sizeof(*p);
4432 			seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n",
4433 				   i, &dma_addr,
4434 				   le32_to_cpu(p->des0), le32_to_cpu(p->des1),
4435 				   le32_to_cpu(p->des2), le32_to_cpu(p->des3));
4436 			p++;
4437 		}
4438 		seq_printf(seq, "\n");
4439 	}
4440 }
4441 
stmmac_rings_status_show(struct seq_file * seq,void * v)4442 static int stmmac_rings_status_show(struct seq_file *seq, void *v)
4443 {
4444 	struct net_device *dev = seq->private;
4445 	struct stmmac_priv *priv = netdev_priv(dev);
4446 	u32 rx_count = priv->plat->rx_queues_to_use;
4447 	u32 tx_count = priv->plat->tx_queues_to_use;
4448 	u32 queue;
4449 
4450 	if ((dev->flags & IFF_UP) == 0)
4451 		return 0;
4452 
4453 	for (queue = 0; queue < rx_count; queue++) {
4454 		struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4455 
4456 		seq_printf(seq, "RX Queue %d:\n", queue);
4457 
4458 		if (priv->extend_desc) {
4459 			seq_printf(seq, "Extended descriptor ring:\n");
4460 			sysfs_display_ring((void *)rx_q->dma_erx,
4461 					   priv->dma_rx_size, 1, seq, rx_q->dma_rx_phy);
4462 		} else {
4463 			seq_printf(seq, "Descriptor ring:\n");
4464 			sysfs_display_ring((void *)rx_q->dma_rx,
4465 					   priv->dma_rx_size, 0, seq, rx_q->dma_rx_phy);
4466 		}
4467 	}
4468 
4469 	for (queue = 0; queue < tx_count; queue++) {
4470 		struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
4471 
4472 		seq_printf(seq, "TX Queue %d:\n", queue);
4473 
4474 		if (priv->extend_desc) {
4475 			seq_printf(seq, "Extended descriptor ring:\n");
4476 			sysfs_display_ring((void *)tx_q->dma_etx,
4477 					   priv->dma_tx_size, 1, seq, tx_q->dma_tx_phy);
4478 		} else if (!(tx_q->tbs & STMMAC_TBS_AVAIL)) {
4479 			seq_printf(seq, "Descriptor ring:\n");
4480 			sysfs_display_ring((void *)tx_q->dma_tx,
4481 					   priv->dma_tx_size, 0, seq, tx_q->dma_tx_phy);
4482 		}
4483 	}
4484 
4485 	return 0;
4486 }
4487 DEFINE_SHOW_ATTRIBUTE(stmmac_rings_status);
4488 
stmmac_dma_cap_show(struct seq_file * seq,void * v)4489 static int stmmac_dma_cap_show(struct seq_file *seq, void *v)
4490 {
4491 	struct net_device *dev = seq->private;
4492 	struct stmmac_priv *priv = netdev_priv(dev);
4493 
4494 	if (!priv->hw_cap_support) {
4495 		seq_printf(seq, "DMA HW features not supported\n");
4496 		return 0;
4497 	}
4498 
4499 	seq_printf(seq, "==============================\n");
4500 	seq_printf(seq, "\tDMA HW features\n");
4501 	seq_printf(seq, "==============================\n");
4502 
4503 	seq_printf(seq, "\t10/100 Mbps: %s\n",
4504 		   (priv->dma_cap.mbps_10_100) ? "Y" : "N");
4505 	seq_printf(seq, "\t1000 Mbps: %s\n",
4506 		   (priv->dma_cap.mbps_1000) ? "Y" : "N");
4507 	seq_printf(seq, "\tHalf duplex: %s\n",
4508 		   (priv->dma_cap.half_duplex) ? "Y" : "N");
4509 	seq_printf(seq, "\tHash Filter: %s\n",
4510 		   (priv->dma_cap.hash_filter) ? "Y" : "N");
4511 	seq_printf(seq, "\tMultiple MAC address registers: %s\n",
4512 		   (priv->dma_cap.multi_addr) ? "Y" : "N");
4513 	seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfaces): %s\n",
4514 		   (priv->dma_cap.pcs) ? "Y" : "N");
4515 	seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
4516 		   (priv->dma_cap.sma_mdio) ? "Y" : "N");
4517 	seq_printf(seq, "\tPMT Remote wake up: %s\n",
4518 		   (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
4519 	seq_printf(seq, "\tPMT Magic Frame: %s\n",
4520 		   (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
4521 	seq_printf(seq, "\tRMON module: %s\n",
4522 		   (priv->dma_cap.rmon) ? "Y" : "N");
4523 	seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
4524 		   (priv->dma_cap.time_stamp) ? "Y" : "N");
4525 	seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp: %s\n",
4526 		   (priv->dma_cap.atime_stamp) ? "Y" : "N");
4527 	seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE): %s\n",
4528 		   (priv->dma_cap.eee) ? "Y" : "N");
4529 	seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
4530 	seq_printf(seq, "\tChecksum Offload in TX: %s\n",
4531 		   (priv->dma_cap.tx_coe) ? "Y" : "N");
4532 	if (priv->synopsys_id >= DWMAC_CORE_4_00) {
4533 		seq_printf(seq, "\tIP Checksum Offload in RX: %s\n",
4534 			   (priv->dma_cap.rx_coe) ? "Y" : "N");
4535 	} else {
4536 		seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
4537 			   (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
4538 		seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
4539 			   (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
4540 	}
4541 	seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
4542 		   (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
4543 	seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
4544 		   priv->dma_cap.number_rx_channel);
4545 	seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
4546 		   priv->dma_cap.number_tx_channel);
4547 	seq_printf(seq, "\tNumber of Additional RX queues: %d\n",
4548 		   priv->dma_cap.number_rx_queues);
4549 	seq_printf(seq, "\tNumber of Additional TX queues: %d\n",
4550 		   priv->dma_cap.number_tx_queues);
4551 	seq_printf(seq, "\tEnhanced descriptors: %s\n",
4552 		   (priv->dma_cap.enh_desc) ? "Y" : "N");
4553 	seq_printf(seq, "\tTX Fifo Size: %d\n", priv->dma_cap.tx_fifo_size);
4554 	seq_printf(seq, "\tRX Fifo Size: %d\n", priv->dma_cap.rx_fifo_size);
4555 	seq_printf(seq, "\tHash Table Size: %d\n", priv->dma_cap.hash_tb_sz);
4556 	seq_printf(seq, "\tTSO: %s\n", priv->dma_cap.tsoen ? "Y" : "N");
4557 	seq_printf(seq, "\tNumber of PPS Outputs: %d\n",
4558 		   priv->dma_cap.pps_out_num);
4559 	seq_printf(seq, "\tSafety Features: %s\n",
4560 		   priv->dma_cap.asp ? "Y" : "N");
4561 	seq_printf(seq, "\tFlexible RX Parser: %s\n",
4562 		   priv->dma_cap.frpsel ? "Y" : "N");
4563 	seq_printf(seq, "\tEnhanced Addressing: %d\n",
4564 		   priv->dma_cap.addr64);
4565 	seq_printf(seq, "\tReceive Side Scaling: %s\n",
4566 		   priv->dma_cap.rssen ? "Y" : "N");
4567 	seq_printf(seq, "\tVLAN Hash Filtering: %s\n",
4568 		   priv->dma_cap.vlhash ? "Y" : "N");
4569 	seq_printf(seq, "\tSplit Header: %s\n",
4570 		   priv->dma_cap.sphen ? "Y" : "N");
4571 	seq_printf(seq, "\tVLAN TX Insertion: %s\n",
4572 		   priv->dma_cap.vlins ? "Y" : "N");
4573 	seq_printf(seq, "\tDouble VLAN: %s\n",
4574 		   priv->dma_cap.dvlan ? "Y" : "N");
4575 	seq_printf(seq, "\tNumber of L3/L4 Filters: %d\n",
4576 		   priv->dma_cap.l3l4fnum);
4577 	seq_printf(seq, "\tARP Offloading: %s\n",
4578 		   priv->dma_cap.arpoffsel ? "Y" : "N");
4579 	seq_printf(seq, "\tEnhancements to Scheduled Traffic (EST): %s\n",
4580 		   priv->dma_cap.estsel ? "Y" : "N");
4581 	seq_printf(seq, "\tFrame Preemption (FPE): %s\n",
4582 		   priv->dma_cap.fpesel ? "Y" : "N");
4583 	seq_printf(seq, "\tTime-Based Scheduling (TBS): %s\n",
4584 		   priv->dma_cap.tbssel ? "Y" : "N");
4585 	return 0;
4586 }
4587 DEFINE_SHOW_ATTRIBUTE(stmmac_dma_cap);
4588 
4589 /* Use network device events to rename debugfs file entries.
4590  */
stmmac_device_event(struct notifier_block * unused,unsigned long event,void * ptr)4591 static int stmmac_device_event(struct notifier_block *unused,
4592 			       unsigned long event, void *ptr)
4593 {
4594 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4595 	struct stmmac_priv *priv = netdev_priv(dev);
4596 
4597 	if (dev->netdev_ops != &stmmac_netdev_ops)
4598 		goto done;
4599 
4600 	switch (event) {
4601 	case NETDEV_CHANGENAME:
4602 		if (priv->dbgfs_dir)
4603 			priv->dbgfs_dir = debugfs_rename(stmmac_fs_dir,
4604 							 priv->dbgfs_dir,
4605 							 stmmac_fs_dir,
4606 							 dev->name);
4607 		break;
4608 	}
4609 done:
4610 	return NOTIFY_DONE;
4611 }
4612 
4613 static struct notifier_block stmmac_notifier = {
4614 	.notifier_call = stmmac_device_event,
4615 };
4616 
stmmac_init_fs(struct net_device * dev)4617 static void stmmac_init_fs(struct net_device *dev)
4618 {
4619 	struct stmmac_priv *priv = netdev_priv(dev);
4620 
4621 	rtnl_lock();
4622 
4623 	/* Create per netdev entries */
4624 	priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
4625 
4626 	/* Entry to report DMA RX/TX rings */
4627 	debugfs_create_file("descriptors_status", 0444, priv->dbgfs_dir, dev,
4628 			    &stmmac_rings_status_fops);
4629 
4630 	/* Entry to report the DMA HW features */
4631 	debugfs_create_file("dma_cap", 0444, priv->dbgfs_dir, dev,
4632 			    &stmmac_dma_cap_fops);
4633 
4634 	rtnl_unlock();
4635 }
4636 
stmmac_exit_fs(struct net_device * dev)4637 static void stmmac_exit_fs(struct net_device *dev)
4638 {
4639 	struct stmmac_priv *priv = netdev_priv(dev);
4640 
4641 	debugfs_remove_recursive(priv->dbgfs_dir);
4642 }
4643 #endif /* CONFIG_DEBUG_FS */
4644 
stmmac_vid_crc32_le(__le16 vid_le)4645 static u32 stmmac_vid_crc32_le(__le16 vid_le)
4646 {
4647 	unsigned char *data = (unsigned char *)&vid_le;
4648 	unsigned char data_byte = 0;
4649 	u32 crc = ~0x0;
4650 	u32 temp = 0;
4651 	int i, bits;
4652 
4653 	bits = get_bitmask_order(VLAN_VID_MASK);
4654 	for (i = 0; i < bits; i++) {
4655 		if ((i % 8) == 0)
4656 			data_byte = data[i / 8];
4657 
4658 		temp = ((crc & 1) ^ data_byte) & 1;
4659 		crc >>= 1;
4660 		data_byte >>= 1;
4661 
4662 		if (temp)
4663 			crc ^= 0xedb88320;
4664 	}
4665 
4666 	return crc;
4667 }
4668 
stmmac_vlan_update(struct stmmac_priv * priv,bool is_double)4669 static int stmmac_vlan_update(struct stmmac_priv *priv, bool is_double)
4670 {
4671 	u32 crc, hash = 0;
4672 	__le16 pmatch = 0;
4673 	int count = 0;
4674 	u16 vid = 0;
4675 
4676 	for_each_set_bit(vid, priv->active_vlans, VLAN_N_VID) {
4677 		__le16 vid_le = cpu_to_le16(vid);
4678 		crc = bitrev32(~stmmac_vid_crc32_le(vid_le)) >> 28;
4679 		hash |= (1 << crc);
4680 		count++;
4681 	}
4682 
4683 	if (!priv->dma_cap.vlhash) {
4684 		if (count > 2) /* VID = 0 always passes filter */
4685 			return -EOPNOTSUPP;
4686 
4687 		pmatch = cpu_to_le16(vid);
4688 		hash = 0;
4689 	}
4690 
4691 	return stmmac_update_vlan_hash(priv, priv->hw, hash, pmatch, is_double);
4692 }
4693 
stmmac_vlan_rx_add_vid(struct net_device * ndev,__be16 proto,u16 vid)4694 static int stmmac_vlan_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid)
4695 {
4696 	struct stmmac_priv *priv = netdev_priv(ndev);
4697 	bool is_double = false;
4698 	int ret;
4699 
4700 	if (be16_to_cpu(proto) == ETH_P_8021AD)
4701 		is_double = true;
4702 
4703 	set_bit(vid, priv->active_vlans);
4704 	ret = stmmac_vlan_update(priv, is_double);
4705 	if (ret) {
4706 		clear_bit(vid, priv->active_vlans);
4707 		return ret;
4708 	}
4709 
4710 	if (priv->hw->num_vlan) {
4711 		ret = stmmac_add_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid);
4712 		if (ret)
4713 			return ret;
4714 	}
4715 
4716 	return 0;
4717 }
4718 
stmmac_vlan_rx_kill_vid(struct net_device * ndev,__be16 proto,u16 vid)4719 static int stmmac_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid)
4720 {
4721 	struct stmmac_priv *priv = netdev_priv(ndev);
4722 	bool is_double = false;
4723 	int ret;
4724 
4725 	ret = pm_runtime_get_sync(priv->device);
4726 	if (ret < 0) {
4727 		pm_runtime_put_noidle(priv->device);
4728 		return ret;
4729 	}
4730 
4731 	if (be16_to_cpu(proto) == ETH_P_8021AD)
4732 		is_double = true;
4733 
4734 	clear_bit(vid, priv->active_vlans);
4735 
4736 	if (priv->hw->num_vlan) {
4737 		ret = stmmac_del_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid);
4738 		if (ret)
4739 			goto del_vlan_error;
4740 	}
4741 
4742 	ret = stmmac_vlan_update(priv, is_double);
4743 
4744 del_vlan_error:
4745 	pm_runtime_put(priv->device);
4746 
4747 	return ret;
4748 }
4749 
4750 static const struct net_device_ops stmmac_netdev_ops = {
4751 	.ndo_open = stmmac_open,
4752 	.ndo_start_xmit = stmmac_xmit,
4753 	.ndo_stop = stmmac_release,
4754 	.ndo_change_mtu = stmmac_change_mtu,
4755 	.ndo_fix_features = stmmac_fix_features,
4756 	.ndo_set_features = stmmac_set_features,
4757 	.ndo_set_rx_mode = stmmac_set_rx_mode,
4758 	.ndo_tx_timeout = stmmac_tx_timeout,
4759 	.ndo_do_ioctl = stmmac_ioctl,
4760 	.ndo_setup_tc = stmmac_setup_tc,
4761 	.ndo_select_queue = stmmac_select_queue,
4762 #ifdef CONFIG_NET_POLL_CONTROLLER
4763 	.ndo_poll_controller = stmmac_poll_controller,
4764 #endif
4765 	.ndo_set_mac_address = stmmac_set_mac_address,
4766 	.ndo_vlan_rx_add_vid = stmmac_vlan_rx_add_vid,
4767 	.ndo_vlan_rx_kill_vid = stmmac_vlan_rx_kill_vid,
4768 };
4769 
stmmac_reset_subtask(struct stmmac_priv * priv)4770 static void stmmac_reset_subtask(struct stmmac_priv *priv)
4771 {
4772 	if (!test_and_clear_bit(STMMAC_RESET_REQUESTED, &priv->state))
4773 		return;
4774 	if (test_bit(STMMAC_DOWN, &priv->state))
4775 		return;
4776 
4777 	netdev_err(priv->dev, "Reset adapter.\n");
4778 
4779 	rtnl_lock();
4780 	netif_trans_update(priv->dev);
4781 	while (test_and_set_bit(STMMAC_RESETING, &priv->state))
4782 		usleep_range(1000, 2000);
4783 
4784 	set_bit(STMMAC_DOWN, &priv->state);
4785 	dev_close(priv->dev);
4786 	dev_open(priv->dev, NULL);
4787 	clear_bit(STMMAC_DOWN, &priv->state);
4788 	clear_bit(STMMAC_RESETING, &priv->state);
4789 	rtnl_unlock();
4790 }
4791 
stmmac_service_task(struct work_struct * work)4792 static void stmmac_service_task(struct work_struct *work)
4793 {
4794 	struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
4795 			service_task);
4796 
4797 	stmmac_reset_subtask(priv);
4798 	clear_bit(STMMAC_SERVICE_SCHED, &priv->state);
4799 }
4800 
4801 /**
4802  *  stmmac_hw_init - Init the MAC device
4803  *  @priv: driver private structure
4804  *  Description: this function is to configure the MAC device according to
4805  *  some platform parameters or the HW capability register. It prepares the
4806  *  driver to use either ring or chain modes and to setup either enhanced or
4807  *  normal descriptors.
4808  */
stmmac_hw_init(struct stmmac_priv * priv)4809 static int stmmac_hw_init(struct stmmac_priv *priv)
4810 {
4811 	int ret;
4812 
4813 	/* dwmac-sun8i only work in chain mode */
4814 	if (priv->plat->has_sun8i)
4815 		chain_mode = 1;
4816 	priv->chain_mode = chain_mode;
4817 
4818 	/* Initialize HW Interface */
4819 	ret = stmmac_hwif_init(priv);
4820 	if (ret)
4821 		return ret;
4822 
4823 	/* Get the HW capability (new GMAC newer than 3.50a) */
4824 	priv->hw_cap_support = stmmac_get_hw_features(priv);
4825 	if (priv->hw_cap_support) {
4826 		dev_info(priv->device, "DMA HW capability register supported\n");
4827 
4828 		/* We can override some gmac/dma configuration fields: e.g.
4829 		 * enh_desc, tx_coe (e.g. that are passed through the
4830 		 * platform) with the values from the HW capability
4831 		 * register (if supported).
4832 		 */
4833 		priv->plat->enh_desc = priv->dma_cap.enh_desc;
4834 		priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
4835 		priv->hw->pmt = priv->plat->pmt;
4836 		if (priv->dma_cap.hash_tb_sz) {
4837 			priv->hw->multicast_filter_bins =
4838 					(BIT(priv->dma_cap.hash_tb_sz) << 5);
4839 			priv->hw->mcast_bits_log2 =
4840 					ilog2(priv->hw->multicast_filter_bins);
4841 		}
4842 
4843 		/* TXCOE doesn't work in thresh DMA mode */
4844 		if (priv->plat->force_thresh_dma_mode)
4845 			priv->plat->tx_coe = 0;
4846 		else
4847 			priv->plat->tx_coe = priv->dma_cap.tx_coe;
4848 
4849 		/* In case of GMAC4 rx_coe is from HW cap register. */
4850 		priv->plat->rx_coe = priv->dma_cap.rx_coe;
4851 
4852 		if (priv->dma_cap.rx_coe_type2)
4853 			priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
4854 		else if (priv->dma_cap.rx_coe_type1)
4855 			priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
4856 
4857 	} else {
4858 		dev_info(priv->device, "No HW DMA feature register supported\n");
4859 	}
4860 
4861 	if (priv->plat->rx_coe) {
4862 		priv->hw->rx_csum = priv->plat->rx_coe;
4863 		dev_info(priv->device, "RX Checksum Offload Engine supported\n");
4864 		if (priv->synopsys_id < DWMAC_CORE_4_00)
4865 			dev_info(priv->device, "COE Type %d\n", priv->hw->rx_csum);
4866 	}
4867 	if (priv->plat->tx_coe)
4868 		dev_info(priv->device, "TX Checksum insertion supported\n");
4869 
4870 	if (priv->plat->pmt) {
4871 		dev_info(priv->device, "Wake-Up On Lan supported\n");
4872 		device_set_wakeup_capable(priv->device, 1);
4873 	}
4874 
4875 	if (priv->dma_cap.tsoen)
4876 		dev_info(priv->device, "TSO supported\n");
4877 
4878 	priv->hw->vlan_fail_q_en = priv->plat->vlan_fail_q_en;
4879 	priv->hw->vlan_fail_q = priv->plat->vlan_fail_q;
4880 
4881 	/* Run HW quirks, if any */
4882 	if (priv->hwif_quirks) {
4883 		ret = priv->hwif_quirks(priv);
4884 		if (ret)
4885 			return ret;
4886 	}
4887 
4888 	/* Rx Watchdog is available in the COREs newer than the 3.40.
4889 	 * In some case, for example on bugged HW this feature
4890 	 * has to be disable and this can be done by passing the
4891 	 * riwt_off field from the platform.
4892 	 */
4893 	if (((priv->synopsys_id >= DWMAC_CORE_3_50) ||
4894 	    (priv->plat->has_xgmac)) && (!priv->plat->riwt_off)) {
4895 		priv->use_riwt = 1;
4896 		dev_info(priv->device,
4897 			 "Enable RX Mitigation via HW Watchdog Timer\n");
4898 	}
4899 
4900 	return 0;
4901 }
4902 
stmmac_napi_add(struct net_device * dev)4903 static void stmmac_napi_add(struct net_device *dev)
4904 {
4905 	struct stmmac_priv *priv = netdev_priv(dev);
4906 	u32 queue, maxq;
4907 
4908 	maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
4909 
4910 	for (queue = 0; queue < maxq; queue++) {
4911 		struct stmmac_channel *ch = &priv->channel[queue];
4912 		int rx_budget = ((priv->plat->dma_rx_size < NAPI_POLL_WEIGHT) &&
4913 				 (priv->plat->dma_rx_size > 0)) ?
4914 				 priv->plat->dma_rx_size : NAPI_POLL_WEIGHT;
4915 		int tx_budget = ((priv->plat->dma_tx_size < NAPI_POLL_WEIGHT) &&
4916 				 (priv->plat->dma_tx_size > 0)) ?
4917 				 priv->plat->dma_tx_size : NAPI_POLL_WEIGHT;
4918 
4919 		ch->priv_data = priv;
4920 		ch->index = queue;
4921 		spin_lock_init(&ch->lock);
4922 
4923 		if (queue < priv->plat->rx_queues_to_use) {
4924 			netif_napi_add(dev, &ch->rx_napi, stmmac_napi_poll_rx,
4925 				       rx_budget);
4926 		}
4927 		if (queue < priv->plat->tx_queues_to_use) {
4928 			netif_tx_napi_add(dev, &ch->tx_napi,
4929 					  stmmac_napi_poll_tx, tx_budget);
4930 		}
4931 	}
4932 }
4933 
stmmac_napi_del(struct net_device * dev)4934 static void stmmac_napi_del(struct net_device *dev)
4935 {
4936 	struct stmmac_priv *priv = netdev_priv(dev);
4937 	u32 queue, maxq;
4938 
4939 	maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
4940 
4941 	for (queue = 0; queue < maxq; queue++) {
4942 		struct stmmac_channel *ch = &priv->channel[queue];
4943 
4944 		if (queue < priv->plat->rx_queues_to_use)
4945 			netif_napi_del(&ch->rx_napi);
4946 		if (queue < priv->plat->tx_queues_to_use)
4947 			netif_napi_del(&ch->tx_napi);
4948 	}
4949 }
4950 
stmmac_reinit_queues(struct net_device * dev,u32 rx_cnt,u32 tx_cnt)4951 int stmmac_reinit_queues(struct net_device *dev, u32 rx_cnt, u32 tx_cnt)
4952 {
4953 	struct stmmac_priv *priv = netdev_priv(dev);
4954 	int ret = 0;
4955 
4956 	if (netif_running(dev))
4957 		stmmac_release(dev);
4958 
4959 	stmmac_napi_del(dev);
4960 
4961 	priv->plat->rx_queues_to_use = rx_cnt;
4962 	priv->plat->tx_queues_to_use = tx_cnt;
4963 
4964 	stmmac_napi_add(dev);
4965 
4966 	if (netif_running(dev))
4967 		ret = stmmac_open(dev);
4968 
4969 	return ret;
4970 }
4971 
stmmac_reinit_ringparam(struct net_device * dev,u32 rx_size,u32 tx_size)4972 int stmmac_reinit_ringparam(struct net_device *dev, u32 rx_size, u32 tx_size)
4973 {
4974 	struct stmmac_priv *priv = netdev_priv(dev);
4975 	int ret = 0;
4976 
4977 	if (netif_running(dev))
4978 		stmmac_release(dev);
4979 
4980 	priv->dma_rx_size = rx_size;
4981 	priv->dma_tx_size = tx_size;
4982 
4983 	if (netif_running(dev))
4984 		ret = stmmac_open(dev);
4985 
4986 	return ret;
4987 }
4988 
4989 /**
4990  * stmmac_dvr_probe
4991  * @device: device pointer
4992  * @plat_dat: platform data pointer
4993  * @res: stmmac resource pointer
4994  * Description: this is the main probe function used to
4995  * call the alloc_etherdev, allocate the priv structure.
4996  * Return:
4997  * returns 0 on success, otherwise errno.
4998  */
stmmac_dvr_probe(struct device * device,struct plat_stmmacenet_data * plat_dat,struct stmmac_resources * res)4999 int stmmac_dvr_probe(struct device *device,
5000 		     struct plat_stmmacenet_data *plat_dat,
5001 		     struct stmmac_resources *res)
5002 {
5003 	struct net_device *ndev = NULL;
5004 	struct stmmac_priv *priv;
5005 	u32 rxq;
5006 	int i, ret = 0;
5007 
5008 	ndev = devm_alloc_etherdev_mqs(device, sizeof(struct stmmac_priv),
5009 				       MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES);
5010 	if (!ndev)
5011 		return -ENOMEM;
5012 
5013 	SET_NETDEV_DEV(ndev, device);
5014 
5015 	priv = netdev_priv(ndev);
5016 	priv->device = device;
5017 	priv->dev = ndev;
5018 
5019 	stmmac_set_ethtool_ops(ndev);
5020 	priv->pause = pause;
5021 	priv->plat = plat_dat;
5022 	priv->ioaddr = res->addr;
5023 	priv->dev->base_addr = (unsigned long)res->addr;
5024 
5025 	priv->dev->irq = res->irq;
5026 	priv->wol_irq = res->wol_irq;
5027 	priv->lpi_irq = res->lpi_irq;
5028 
5029 	if (!IS_ERR_OR_NULL(res->mac))
5030 		memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
5031 
5032 	dev_set_drvdata(device, priv->dev);
5033 
5034 	/* Verify driver arguments */
5035 	stmmac_verify_args();
5036 
5037 	/* Allocate workqueue */
5038 	priv->wq = create_singlethread_workqueue("stmmac_wq");
5039 	if (!priv->wq) {
5040 		dev_err(priv->device, "failed to create workqueue\n");
5041 		return -ENOMEM;
5042 	}
5043 
5044 	INIT_WORK(&priv->service_task, stmmac_service_task);
5045 
5046 	/* Override with kernel parameters if supplied XXX CRS XXX
5047 	 * this needs to have multiple instances
5048 	 */
5049 	if ((phyaddr >= 0) && (phyaddr <= 31))
5050 		priv->plat->phy_addr = phyaddr;
5051 
5052 	if (priv->plat->stmmac_rst) {
5053 		ret = reset_control_assert(priv->plat->stmmac_rst);
5054 		reset_control_deassert(priv->plat->stmmac_rst);
5055 		/* Some reset controllers have only reset callback instead of
5056 		 * assert + deassert callbacks pair.
5057 		 */
5058 		if (ret == -ENOTSUPP)
5059 			reset_control_reset(priv->plat->stmmac_rst);
5060 	}
5061 
5062 	/* Init MAC and get the capabilities */
5063 	ret = stmmac_hw_init(priv);
5064 	if (ret)
5065 		goto error_hw_init;
5066 
5067 	stmmac_check_ether_addr(priv);
5068 
5069 	ndev->netdev_ops = &stmmac_netdev_ops;
5070 
5071 	ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5072 			    NETIF_F_RXCSUM;
5073 
5074 	ret = stmmac_tc_init(priv, priv);
5075 	if (!ret) {
5076 		ndev->hw_features |= NETIF_F_HW_TC;
5077 	}
5078 
5079 	if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
5080 		ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
5081 		if (priv->plat->has_gmac4)
5082 			ndev->hw_features |= NETIF_F_GSO_UDP_L4;
5083 		priv->tso = true;
5084 		dev_info(priv->device, "TSO feature enabled\n");
5085 	}
5086 
5087 	if (priv->dma_cap.sphen && !priv->plat->sph_disable) {
5088 		ndev->hw_features |= NETIF_F_GRO;
5089 		if (!priv->plat->sph_disable) {
5090 			priv->sph = true;
5091 			dev_info(priv->device, "SPH feature enabled\n");
5092 		}
5093 	}
5094 
5095 	/* The current IP register MAC_HW_Feature1[ADDR64] only define
5096 	 * 32/40/64 bit width, but some SOC support others like i.MX8MP
5097 	 * support 34 bits but it map to 40 bits width in MAC_HW_Feature1[ADDR64].
5098 	 * So overwrite dma_cap.addr64 according to HW real design.
5099 	 */
5100 	if (priv->plat->addr64)
5101 		priv->dma_cap.addr64 = priv->plat->addr64;
5102 
5103 	if (priv->dma_cap.addr64) {
5104 		ret = dma_set_mask_and_coherent(device,
5105 				DMA_BIT_MASK(priv->dma_cap.addr64));
5106 		if (!ret) {
5107 			dev_info(priv->device, "Using %d bits DMA width\n",
5108 				 priv->dma_cap.addr64);
5109 
5110 			/*
5111 			 * If more than 32 bits can be addressed, make sure to
5112 			 * enable enhanced addressing mode.
5113 			 */
5114 			if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
5115 				priv->plat->dma_cfg->eame = true;
5116 		} else {
5117 			ret = dma_set_mask_and_coherent(device, DMA_BIT_MASK(32));
5118 			if (ret) {
5119 				dev_err(priv->device, "Failed to set DMA Mask\n");
5120 				goto error_hw_init;
5121 			}
5122 
5123 			priv->dma_cap.addr64 = 32;
5124 		}
5125 	}
5126 
5127 	ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
5128 	ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
5129 #ifdef STMMAC_VLAN_TAG_USED
5130 	/* Both mac100 and gmac support receive VLAN tag detection */
5131 	ndev->features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX;
5132 	if (priv->dma_cap.vlhash) {
5133 		ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
5134 		ndev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
5135 	}
5136 	if (priv->dma_cap.vlins) {
5137 		ndev->features |= NETIF_F_HW_VLAN_CTAG_TX;
5138 		if (priv->dma_cap.dvlan)
5139 			ndev->features |= NETIF_F_HW_VLAN_STAG_TX;
5140 	}
5141 #endif
5142 	priv->msg_enable = netif_msg_init(debug, default_msg_level);
5143 
5144 	/* Initialize RSS */
5145 	rxq = priv->plat->rx_queues_to_use;
5146 	netdev_rss_key_fill(priv->rss.key, sizeof(priv->rss.key));
5147 	for (i = 0; i < ARRAY_SIZE(priv->rss.table); i++)
5148 		priv->rss.table[i] = ethtool_rxfh_indir_default(i, rxq);
5149 
5150 	if (priv->dma_cap.rssen && priv->plat->rss_en)
5151 		ndev->features |= NETIF_F_RXHASH;
5152 
5153 	/* MTU range: 46 - hw-specific max */
5154 	ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
5155 	if (priv->plat->has_xgmac)
5156 		ndev->max_mtu = XGMAC_JUMBO_LEN;
5157 	else if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
5158 		ndev->max_mtu = JUMBO_LEN;
5159 	else
5160 		ndev->max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
5161 	/* Will not overwrite ndev->max_mtu if plat->maxmtu > ndev->max_mtu
5162 	 * as well as plat->maxmtu < ndev->min_mtu which is a invalid range.
5163 	 */
5164 	if ((priv->plat->maxmtu < ndev->max_mtu) &&
5165 	    (priv->plat->maxmtu >= ndev->min_mtu))
5166 		ndev->max_mtu = priv->plat->maxmtu;
5167 	else if (priv->plat->maxmtu < ndev->min_mtu)
5168 		dev_warn(priv->device,
5169 			 "%s: warning: maxmtu having invalid value (%d)\n",
5170 			 __func__, priv->plat->maxmtu);
5171 
5172 	if (flow_ctrl)
5173 		priv->flow_ctrl = FLOW_AUTO;	/* RX/TX pause on */
5174 
5175 	/* Setup channels NAPI */
5176 	stmmac_napi_add(ndev);
5177 
5178 	mutex_init(&priv->lock);
5179 
5180 	/* If a specific clk_csr value is passed from the platform
5181 	 * this means that the CSR Clock Range selection cannot be
5182 	 * changed at run-time and it is fixed. Viceversa the driver'll try to
5183 	 * set the MDC clock dynamically according to the csr actual
5184 	 * clock input.
5185 	 */
5186 	if (priv->plat->clk_csr >= 0)
5187 		priv->clk_csr = priv->plat->clk_csr;
5188 	else
5189 		stmmac_clk_csr_set(priv);
5190 
5191 	stmmac_check_pcs_mode(priv);
5192 
5193 	pm_runtime_get_noresume(device);
5194 	pm_runtime_set_active(device);
5195 	pm_runtime_enable(device);
5196 
5197 	if (priv->hw->pcs != STMMAC_PCS_TBI &&
5198 	    priv->hw->pcs != STMMAC_PCS_RTBI) {
5199 		/* MDIO bus Registration */
5200 		ret = stmmac_mdio_register(ndev);
5201 		if (ret < 0) {
5202 			dev_err(priv->device,
5203 				"%s: MDIO bus (id: %d) registration failed",
5204 				__func__, priv->plat->bus_id);
5205 			goto error_mdio_register;
5206 		}
5207 	}
5208 
5209 	ret = stmmac_phy_setup(priv);
5210 	if (ret) {
5211 		netdev_err(ndev, "failed to setup phy (%d)\n", ret);
5212 		goto error_phy_setup;
5213 	}
5214 
5215 	ret = register_netdev(ndev);
5216 	if (ret) {
5217 		dev_err(priv->device, "%s: ERROR %i registering the device\n",
5218 			__func__, ret);
5219 		goto error_netdev_register;
5220 	}
5221 
5222 #ifdef CONFIG_DEBUG_FS
5223 	stmmac_init_fs(ndev);
5224 #endif
5225 
5226 	/* Let pm_runtime_put() disable the clocks.
5227 	 * If CONFIG_PM is not enabled, the clocks will stay powered.
5228 	 */
5229 	pm_runtime_put(device);
5230 
5231 	return ret;
5232 
5233 error_netdev_register:
5234 	phylink_destroy(priv->phylink);
5235 error_phy_setup:
5236 	if (priv->hw->pcs != STMMAC_PCS_TBI &&
5237 	    priv->hw->pcs != STMMAC_PCS_RTBI)
5238 		stmmac_mdio_unregister(ndev);
5239 error_mdio_register:
5240 	stmmac_napi_del(ndev);
5241 error_hw_init:
5242 	destroy_workqueue(priv->wq);
5243 
5244 	return ret;
5245 }
5246 EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
5247 
5248 /**
5249  * stmmac_dvr_remove
5250  * @dev: device pointer
5251  * Description: this function resets the TX/RX processes, disables the MAC RX/TX
5252  * changes the link status, releases the DMA descriptor rings.
5253  */
stmmac_dvr_remove(struct device * dev)5254 int stmmac_dvr_remove(struct device *dev)
5255 {
5256 	struct net_device *ndev = dev_get_drvdata(dev);
5257 	struct stmmac_priv *priv = netdev_priv(ndev);
5258 
5259 	netdev_info(priv->dev, "%s: removing driver", __func__);
5260 
5261 	stmmac_stop_all_dma(priv);
5262 	stmmac_mac_set(priv, priv->ioaddr, false);
5263 	netif_carrier_off(ndev);
5264 	unregister_netdev(ndev);
5265 
5266 	/* Serdes power down needs to happen after VLAN filter
5267 	 * is deleted that is triggered by unregister_netdev().
5268 	 */
5269 	if (priv->plat->serdes_powerdown)
5270 		priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv);
5271 
5272 #ifdef CONFIG_DEBUG_FS
5273 	stmmac_exit_fs(ndev);
5274 #endif
5275 	phylink_destroy(priv->phylink);
5276 	if (priv->plat->stmmac_rst)
5277 		reset_control_assert(priv->plat->stmmac_rst);
5278 	pm_runtime_put(dev);
5279 	pm_runtime_disable(dev);
5280 	if (priv->hw->pcs != STMMAC_PCS_TBI &&
5281 	    priv->hw->pcs != STMMAC_PCS_RTBI)
5282 		stmmac_mdio_unregister(ndev);
5283 	destroy_workqueue(priv->wq);
5284 	mutex_destroy(&priv->lock);
5285 
5286 	return 0;
5287 }
5288 EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
5289 
5290 /**
5291  * stmmac_suspend - suspend callback
5292  * @dev: device pointer
5293  * Description: this is the function to suspend the device and it is called
5294  * by the platform driver to stop the network queue, release the resources,
5295  * program the PMT register (for WoL), clean and release driver resources.
5296  */
stmmac_suspend(struct device * dev)5297 int stmmac_suspend(struct device *dev)
5298 {
5299 	struct net_device *ndev = dev_get_drvdata(dev);
5300 	struct stmmac_priv *priv = netdev_priv(ndev);
5301 	u32 chan;
5302 
5303 	if (!ndev || !netif_running(ndev))
5304 		return 0;
5305 
5306 	phylink_mac_change(priv->phylink, false);
5307 
5308 	mutex_lock(&priv->lock);
5309 
5310 	netif_device_detach(ndev);
5311 
5312 	stmmac_disable_all_queues(priv);
5313 
5314 	for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
5315 		del_timer_sync(&priv->tx_queue[chan].txtimer);
5316 
5317 	if (priv->eee_enabled) {
5318 		priv->tx_path_in_lpi_mode = false;
5319 		del_timer_sync(&priv->eee_ctrl_timer);
5320 	}
5321 
5322 	/* Stop TX/RX DMA */
5323 	stmmac_stop_all_dma(priv);
5324 
5325 	if (priv->plat->serdes_powerdown)
5326 		priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv);
5327 
5328 	/* Enable Power down mode by programming the PMT regs */
5329 	if (device_may_wakeup(priv->device) && priv->plat->pmt) {
5330 		stmmac_pmt(priv, priv->hw, priv->wolopts);
5331 		priv->irq_wake = 1;
5332 	} else {
5333 		mutex_unlock(&priv->lock);
5334 		rtnl_lock();
5335 		if (device_may_wakeup(priv->device))
5336 			phylink_speed_down(priv->phylink, false);
5337 		if (priv->plat->integrated_phy_power)
5338 			priv->plat->integrated_phy_power(priv->plat->bsp_priv,
5339 							 false);
5340 		phylink_stop(priv->phylink);
5341 		rtnl_unlock();
5342 		mutex_lock(&priv->lock);
5343 
5344 		stmmac_mac_set(priv, priv->ioaddr, false);
5345 		pinctrl_pm_select_sleep_state(priv->device);
5346 	}
5347 	mutex_unlock(&priv->lock);
5348 
5349 	priv->speed = SPEED_UNKNOWN;
5350 	return 0;
5351 }
5352 EXPORT_SYMBOL_GPL(stmmac_suspend);
5353 
5354 /**
5355  * stmmac_reset_queues_param - reset queue parameters
5356  * @priv: device pointer
5357  */
stmmac_reset_queues_param(struct stmmac_priv * priv)5358 static void stmmac_reset_queues_param(struct stmmac_priv *priv)
5359 {
5360 	u32 rx_cnt = priv->plat->rx_queues_to_use;
5361 	u32 tx_cnt = priv->plat->tx_queues_to_use;
5362 	u32 queue;
5363 
5364 	for (queue = 0; queue < rx_cnt; queue++) {
5365 		struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
5366 
5367 		rx_q->cur_rx = 0;
5368 		rx_q->dirty_rx = 0;
5369 	}
5370 
5371 	for (queue = 0; queue < tx_cnt; queue++) {
5372 		struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
5373 
5374 		tx_q->cur_tx = 0;
5375 		tx_q->dirty_tx = 0;
5376 		tx_q->mss = 0;
5377 
5378 		netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
5379 	}
5380 }
5381 
5382 /**
5383  * stmmac_resume - resume callback
5384  * @dev: device pointer
5385  * Description: when resume this function is invoked to setup the DMA and CORE
5386  * in a usable state.
5387  */
stmmac_resume(struct device * dev)5388 int stmmac_resume(struct device *dev)
5389 {
5390 	struct net_device *ndev = dev_get_drvdata(dev);
5391 	struct stmmac_priv *priv = netdev_priv(ndev);
5392 	int ret;
5393 
5394 	if (!netif_running(ndev))
5395 		return 0;
5396 
5397 	/* Power Down bit, into the PM register, is cleared
5398 	 * automatically as soon as a magic packet or a Wake-up frame
5399 	 * is received. Anyway, it's better to manually clear
5400 	 * this bit because it can generate problems while resuming
5401 	 * from another devices (e.g. serial console).
5402 	 */
5403 	if (device_may_wakeup(priv->device) && priv->plat->pmt) {
5404 		mutex_lock(&priv->lock);
5405 		stmmac_pmt(priv, priv->hw, 0);
5406 		mutex_unlock(&priv->lock);
5407 		priv->irq_wake = 0;
5408 	} else {
5409 		pinctrl_pm_select_default_state(priv->device);
5410 		/* reset the phy so that it's ready */
5411 		if (priv->mii)
5412 			stmmac_mdio_reset(priv->mii);
5413 		if (priv->plat->integrated_phy_power)
5414 			priv->plat->integrated_phy_power(priv->plat->bsp_priv,
5415 							 true);
5416 	}
5417 
5418 	if (priv->plat->serdes_powerup) {
5419 		ret = priv->plat->serdes_powerup(ndev,
5420 						 priv->plat->bsp_priv);
5421 
5422 		if (ret < 0)
5423 			return ret;
5424 	}
5425 
5426 	if (!device_may_wakeup(priv->device) || !priv->plat->pmt) {
5427 		rtnl_lock();
5428 		phylink_start(priv->phylink);
5429 		/* We may have called phylink_speed_down before */
5430 		phylink_speed_up(priv->phylink);
5431 		rtnl_unlock();
5432 	}
5433 
5434 	rtnl_lock();
5435 	mutex_lock(&priv->lock);
5436 
5437 	stmmac_reset_queues_param(priv);
5438 
5439 	stmmac_free_tx_skbufs(priv);
5440 	stmmac_clear_descriptors(priv);
5441 
5442 	stmmac_hw_setup(ndev, false);
5443 	stmmac_init_coalesce(priv);
5444 	stmmac_set_rx_mode(ndev);
5445 
5446 	stmmac_restore_hw_vlan_rx_fltr(priv, ndev, priv->hw);
5447 
5448 	stmmac_enable_all_queues(priv);
5449 
5450 	mutex_unlock(&priv->lock);
5451 	rtnl_unlock();
5452 
5453 	phylink_mac_change(priv->phylink, true);
5454 
5455 	netif_device_attach(ndev);
5456 
5457 	return 0;
5458 }
5459 EXPORT_SYMBOL_GPL(stmmac_resume);
5460 
5461 #ifndef MODULE
stmmac_cmdline_opt(char * str)5462 static int __init stmmac_cmdline_opt(char *str)
5463 {
5464 	char *opt;
5465 
5466 	if (!str || !*str)
5467 		return 1;
5468 	while ((opt = strsep(&str, ",")) != NULL) {
5469 		if (!strncmp(opt, "debug:", 6)) {
5470 			if (kstrtoint(opt + 6, 0, &debug))
5471 				goto err;
5472 		} else if (!strncmp(opt, "phyaddr:", 8)) {
5473 			if (kstrtoint(opt + 8, 0, &phyaddr))
5474 				goto err;
5475 		} else if (!strncmp(opt, "buf_sz:", 7)) {
5476 			if (kstrtoint(opt + 7, 0, &buf_sz))
5477 				goto err;
5478 		} else if (!strncmp(opt, "tc:", 3)) {
5479 			if (kstrtoint(opt + 3, 0, &tc))
5480 				goto err;
5481 		} else if (!strncmp(opt, "watchdog:", 9)) {
5482 			if (kstrtoint(opt + 9, 0, &watchdog))
5483 				goto err;
5484 		} else if (!strncmp(opt, "flow_ctrl:", 10)) {
5485 			if (kstrtoint(opt + 10, 0, &flow_ctrl))
5486 				goto err;
5487 		} else if (!strncmp(opt, "pause:", 6)) {
5488 			if (kstrtoint(opt + 6, 0, &pause))
5489 				goto err;
5490 		} else if (!strncmp(opt, "eee_timer:", 10)) {
5491 			if (kstrtoint(opt + 10, 0, &eee_timer))
5492 				goto err;
5493 		} else if (!strncmp(opt, "chain_mode:", 11)) {
5494 			if (kstrtoint(opt + 11, 0, &chain_mode))
5495 				goto err;
5496 		}
5497 	}
5498 	return 1;
5499 
5500 err:
5501 	pr_err("%s: ERROR broken module parameter conversion", __func__);
5502 	return 1;
5503 }
5504 
5505 __setup("stmmaceth=", stmmac_cmdline_opt);
5506 #endif /* MODULE */
5507 
stmmac_init(void)5508 static int __init stmmac_init(void)
5509 {
5510 #ifdef CONFIG_DEBUG_FS
5511 	/* Create debugfs main directory if it doesn't exist yet */
5512 	if (!stmmac_fs_dir)
5513 		stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
5514 	register_netdevice_notifier(&stmmac_notifier);
5515 #endif
5516 
5517 	return 0;
5518 }
5519 
stmmac_exit(void)5520 static void __exit stmmac_exit(void)
5521 {
5522 #ifdef CONFIG_DEBUG_FS
5523 	unregister_netdevice_notifier(&stmmac_notifier);
5524 	debugfs_remove_recursive(stmmac_fs_dir);
5525 #endif
5526 }
5527 
5528 module_init(stmmac_init)
5529 module_exit(stmmac_exit)
5530 
5531 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
5532 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
5533 MODULE_LICENSE("GPL");
5534