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