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