gitlab.arm.com will be in the maintainance mode on Wednesday June 29th 01:00 - 10:00 (UTC+1). Repositories is read only during the maintainance.

stmmac_main.c 125 KB
Newer Older
1
2
3
4
/*******************************************************************************
  This is the driver for the ST MAC 10/100/1000 on-chip Ethernet controllers.
  ST Ethernet IPs are built around a Synopsys IP Core.

5
	Copyright(C) 2007-2011 STMicroelectronics Ltd
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

  This program is free software; you can redistribute it and/or modify it
  under the terms and conditions of the GNU General Public License,
  version 2, as published by the Free Software Foundation.

  This program is distributed in the hope it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  The full GNU General Public License is included in this distribution in
  the file called "COPYING".

  Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>

  Documentation available at:
	http://www.stlinux.com
  Support available at:
	https://bugzilla.stlinux.com/
*******************************************************************************/

27
#include <linux/clk.h>
28
29
30
31
32
33
34
35
36
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/skbuff.h>
#include <linux/ethtool.h>
#include <linux/if_ether.h>
#include <linux/crc32.h>
#include <linux/mii.h>
37
#include <linux/if.h>
38
39
#include <linux/if_vlan.h>
#include <linux/dma-mapping.h>
40
#include <linux/slab.h>
41
#include <linux/prefetch.h>
42
#include <linux/pinctrl/consumer.h>
43
#ifdef CONFIG_DEBUG_FS
44
45
#include <linux/debugfs.h>
#include <linux/seq_file.h>
46
#endif /* CONFIG_DEBUG_FS */
47
#include <linux/net_tstamp.h>
48
#include <net/pkt_cls.h>
49
#include "stmmac_ptp.h"
50
#include "stmmac.h"
51
#include <linux/reset.h>
52
#include <linux/of_mdio.h>
53
#include "dwmac1000.h"
54
#include "hwif.h"
55
56

#define STMMAC_ALIGN(x)	L1_CACHE_ALIGN(x)
Alexandre TORGUE's avatar
Alexandre TORGUE committed
57
#define	TSO_MAX_BUFF_SIZE	(SZ_16K - 1)
58
59

/* Module parameters */
60
#define TX_TIMEO	5000
61
static int watchdog = TX_TIMEO;
62
module_param(watchdog, int, 0644);
63
MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
64

65
static int debug = -1;
66
module_param(debug, int, 0644);
67
MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
68

69
static int phyaddr = -1;
70
module_param(phyaddr, int, 0444);
71
72
MODULE_PARM_DESC(phyaddr, "Physical device address");

73
#define STMMAC_TX_THRESH	(DMA_TX_SIZE / 4)
74
#define STMMAC_RX_THRESH	(DMA_RX_SIZE / 4)
75
76

static int flow_ctrl = FLOW_OFF;
77
module_param(flow_ctrl, int, 0644);
78
79
80
MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");

static int pause = PAUSE_TIME;
81
module_param(pause, int, 0644);
82
83
84
85
MODULE_PARM_DESC(pause, "Flow Control Pause Time");

#define TC_DEFAULT 64
static int tc = TC_DEFAULT;
86
module_param(tc, int, 0644);
87
88
MODULE_PARM_DESC(tc, "DMA threshold control value");

89
90
#define	DEFAULT_BUFSIZE	1536
static int buf_sz = DEFAULT_BUFSIZE;
91
module_param(buf_sz, int, 0644);
92
93
MODULE_PARM_DESC(buf_sz, "DMA buffer size");

94
95
#define	STMMAC_RX_COPYBREAK	256

96
97
98
99
static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
				      NETIF_MSG_LINK | NETIF_MSG_IFUP |
				      NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);

100
101
#define STMMAC_DEFAULT_LPI_TIMER	1000
static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
102
module_param(eee_timer, int, 0644);
103
MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
104
#define STMMAC_LPI_T(x) (jiffies + msecs_to_jiffies(x))
105

106
107
/* By default the driver will use the ring mode to manage tx and rx descriptors,
 * but allow user to force to use the chain instead of the ring
108
109
 */
static unsigned int chain_mode;
110
module_param(chain_mode, int, 0444);
111
112
MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");

113
114
static irqreturn_t stmmac_interrupt(int irq, void *dev_id);

115
#ifdef CONFIG_DEBUG_FS
116
static int stmmac_init_fs(struct net_device *dev);
117
static void stmmac_exit_fs(struct net_device *dev);
118
119
#endif

120
121
#define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))

122
123
/**
 * stmmac_verify_args - verify the driver parameters.
124
125
 * Description: it checks the driver parameters and set a default in case of
 * errors.
126
127
128
129
130
 */
static void stmmac_verify_args(void)
{
	if (unlikely(watchdog < 0))
		watchdog = TX_TIMEO;
131
132
	if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
		buf_sz = DEFAULT_BUFSIZE;
133
134
135
136
137
138
	if (unlikely(flow_ctrl > 1))
		flow_ctrl = FLOW_AUTO;
	else if (likely(flow_ctrl < 0))
		flow_ctrl = FLOW_OFF;
	if (unlikely((pause < 0) || (pause > 0xffff)))
		pause = PAUSE_TIME;
139
140
	if (eee_timer < 0)
		eee_timer = STMMAC_DEFAULT_LPI_TIMER;
141
142
}

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
 * stmmac_disable_all_queues - Disable all queues
 * @priv: driver private structure
 */
static void stmmac_disable_all_queues(struct stmmac_priv *priv)
{
	u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
	u32 queue;

	for (queue = 0; queue < rx_queues_cnt; queue++) {
		struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];

		napi_disable(&rx_q->napi);
	}
}

/**
 * stmmac_enable_all_queues - Enable all queues
 * @priv: driver private structure
 */
static void stmmac_enable_all_queues(struct stmmac_priv *priv)
{
	u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
	u32 queue;

	for (queue = 0; queue < rx_queues_cnt; queue++) {
		struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];

		napi_enable(&rx_q->napi);
	}
}

/**
 * stmmac_stop_all_queues - Stop all queues
 * @priv: driver private structure
 */
static void stmmac_stop_all_queues(struct stmmac_priv *priv)
{
	u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
	u32 queue;

	for (queue = 0; queue < tx_queues_cnt; queue++)
		netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
}

/**
 * stmmac_start_all_queues - Start all queues
 * @priv: driver private structure
 */
static void stmmac_start_all_queues(struct stmmac_priv *priv)
{
	u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
	u32 queue;

	for (queue = 0; queue < tx_queues_cnt; queue++)
		netif_tx_start_queue(netdev_get_tx_queue(priv->dev, queue));
}

201
202
203
204
205
206
207
208
209
210
211
212
213
214
static void stmmac_service_event_schedule(struct stmmac_priv *priv)
{
	if (!test_bit(STMMAC_DOWN, &priv->state) &&
	    !test_and_set_bit(STMMAC_SERVICE_SCHED, &priv->state))
		queue_work(priv->wq, &priv->service_task);
}

static void stmmac_global_err(struct stmmac_priv *priv)
{
	netif_carrier_off(priv->dev);
	set_bit(STMMAC_RESET_REQUESTED, &priv->state);
	stmmac_service_event_schedule(priv);
}

215
216
217
218
219
220
221
222
223
224
225
226
/**
 * stmmac_clk_csr_set - dynamically set the MDC clock
 * @priv: driver private structure
 * Description: this is to dynamically set the MDC clock according to the csr
 * clock input.
 * Note:
 *	If a specific clk_csr value is passed from the platform
 *	this means that the CSR Clock Range selection cannot be
 *	changed at run-time and it is fixed (as reported in the driver
 *	documentation). Viceversa the driver will try to set the MDC
 *	clock dynamically according to the actual clock input.
 */
227
228
229
230
static void stmmac_clk_csr_set(struct stmmac_priv *priv)
{
	u32 clk_rate;

231
	clk_rate = clk_get_rate(priv->plat->stmmac_clk);
232
233

	/* Platform provided default clk_csr would be assumed valid
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
234
235
236
237
238
239
	 * for all other cases except for the below mentioned ones.
	 * For values higher than the IEEE 802.3 specified frequency
	 * we can not estimate the proper divider as it is not known
	 * the frequency of clk_csr_i. So we do not change the default
	 * divider.
	 */
240
241
242
243
244
245
246
247
248
249
250
	if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
		if (clk_rate < CSR_F_35M)
			priv->clk_csr = STMMAC_CSR_20_35M;
		else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
			priv->clk_csr = STMMAC_CSR_35_60M;
		else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
			priv->clk_csr = STMMAC_CSR_60_100M;
		else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
			priv->clk_csr = STMMAC_CSR_100_150M;
		else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
			priv->clk_csr = STMMAC_CSR_150_250M;
251
		else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
252
			priv->clk_csr = STMMAC_CSR_250_300M;
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
253
	}
254
255
256
257
258
259
260
261
262
263
264

	if (priv->plat->has_sun8i) {
		if (clk_rate > 160000000)
			priv->clk_csr = 0x03;
		else if (clk_rate > 80000000)
			priv->clk_csr = 0x02;
		else if (clk_rate > 40000000)
			priv->clk_csr = 0x01;
		else
			priv->clk_csr = 0;
	}
265
266
}

267
268
static void print_pkt(unsigned char *buf, int len)
{
269
270
	pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
	print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
271
272
}

273
static inline u32 stmmac_tx_avail(struct stmmac_priv *priv, u32 queue)
274
{
275
	struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
276
	u32 avail;
277

278
279
	if (tx_q->dirty_tx > tx_q->cur_tx)
		avail = tx_q->dirty_tx - tx_q->cur_tx - 1;
280
	else
281
		avail = DMA_TX_SIZE - tx_q->cur_tx + tx_q->dirty_tx - 1;
282
283
284
285

	return avail;
}

286
287
288
289
290
291
/**
 * stmmac_rx_dirty - Get RX queue dirty
 * @priv: driver private structure
 * @queue: RX queue index
 */
static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv, u32 queue)
292
{
293
	struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
294
	u32 dirty;
295

296
297
	if (rx_q->dirty_rx <= rx_q->cur_rx)
		dirty = rx_q->cur_rx - rx_q->dirty_rx;
298
	else
299
		dirty = DMA_RX_SIZE - rx_q->dirty_rx + rx_q->cur_rx;
300
301

	return dirty;
302
303
}

304
/**
305
 * stmmac_hw_fix_mac_speed - callback for speed selection
306
 * @priv: driver private structure
307
 * Description: on some platforms (e.g. ST), some HW system configuration
308
 * registers have to be set according to the link speed negotiated.
309
310
311
 */
static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
{
312
313
	struct net_device *ndev = priv->dev;
	struct phy_device *phydev = ndev->phydev;
314
315

	if (likely(priv->plat->fix_mac_speed))
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
316
		priv->plat->fix_mac_speed(priv->plat->bsp_priv, phydev->speed);
317
318
}

319
/**
320
 * stmmac_enable_eee_mode - check and enter in LPI mode
321
 * @priv: driver private structure
322
323
 * Description: this function is to verify and enter in LPI mode in case of
 * EEE.
324
 */
325
326
static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
{
327
328
329
330
331
332
333
334
335
336
337
	u32 tx_cnt = priv->plat->tx_queues_to_use;
	u32 queue;

	/* check if all TX queues have the work finished */
	for (queue = 0; queue < tx_cnt; queue++) {
		struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];

		if (tx_q->dirty_tx != tx_q->cur_tx)
			return; /* still unfinished work */
	}

338
	/* Check and enter in LPI mode */
339
	if (!priv->tx_path_in_lpi_mode)
340
341
		stmmac_set_eee_mode(priv, priv->hw,
				priv->plat->en_tx_lpi_clockgating);
342
343
}

344
/**
345
 * stmmac_disable_eee_mode - disable and exit from LPI mode
346
347
348
349
 * @priv: driver private structure
 * Description: this function is to exit and disable EEE in case of
 * LPI state is true. This is called by the xmit.
 */
350
351
void stmmac_disable_eee_mode(struct stmmac_priv *priv)
{
352
	stmmac_reset_eee_mode(priv, priv->hw);
353
354
355
356
357
	del_timer_sync(&priv->eee_ctrl_timer);
	priv->tx_path_in_lpi_mode = false;
}

/**
358
 * stmmac_eee_ctrl_timer - EEE TX SW timer.
359
360
 * @arg : data hook
 * Description:
361
 *  if there is no data transfer and if we are not in LPI state,
362
363
 *  then MAC Transmitter can be moved to LPI state.
 */
364
static void stmmac_eee_ctrl_timer(struct timer_list *t)
365
{
366
	struct stmmac_priv *priv = from_timer(priv, t, eee_ctrl_timer);
367
368

	stmmac_enable_eee_mode(priv);
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
369
	mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
370
371
372
}

/**
373
 * stmmac_eee_init - init EEE
374
 * @priv: driver private structure
375
 * Description:
376
377
378
 *  if the GMAC supports the EEE (from the HW cap reg) and the phy device
 *  can also manage EEE, this function enable the LPI state and start related
 *  timer.
379
380
381
 */
bool stmmac_eee_init(struct stmmac_priv *priv)
{
382
	struct net_device *ndev = priv->dev;
383
	int interface = priv->plat->interface;
384
385
	bool ret = false;

386
387
388
389
390
	if ((interface != PHY_INTERFACE_MODE_MII) &&
	    (interface != PHY_INTERFACE_MODE_GMII) &&
	    !phy_interface_mode_is_rgmii(interface))
		goto out;

Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
391
392
393
	/* Using PCS we cannot dial with the phy registers at this stage
	 * so we do not support extra feature like EEE.
	 */
394
395
396
	if ((priv->hw->pcs == STMMAC_PCS_RGMII) ||
	    (priv->hw->pcs == STMMAC_PCS_TBI) ||
	    (priv->hw->pcs == STMMAC_PCS_RTBI))
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
397
398
		goto out;

399
400
	/* MAC core supports the EEE feature. */
	if (priv->dma_cap.eee) {
401
402
		int tx_lpi_timer = priv->tx_lpi_timer;

403
		/* Check if the PHY supports EEE */
404
		if (phy_init_eee(ndev->phydev, 1)) {
405
406
407
408
409
			/* To manage at run-time if the EEE cannot be supported
			 * anymore (for example because the lp caps have been
			 * changed).
			 * In that case the driver disable own timers.
			 */
410
			mutex_lock(&priv->lock);
411
			if (priv->eee_active) {
412
				netdev_dbg(priv->dev, "disable EEE\n");
413
				del_timer_sync(&priv->eee_ctrl_timer);
414
415
				stmmac_set_eee_timer(priv, priv->hw, 0,
						tx_lpi_timer);
416
417
			}
			priv->eee_active = 0;
418
			mutex_unlock(&priv->lock);
419
			goto out;
420
421
		}
		/* Activate the EEE and start timers */
422
		mutex_lock(&priv->lock);
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
423
424
		if (!priv->eee_active) {
			priv->eee_active = 1;
425
426
			timer_setup(&priv->eee_ctrl_timer,
				    stmmac_eee_ctrl_timer, 0);
427
428
			mod_timer(&priv->eee_ctrl_timer,
				  STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
429

430
431
			stmmac_set_eee_timer(priv, priv->hw,
					STMMAC_DEFAULT_LIT_LS, tx_lpi_timer);
432
433
		}
		/* Set HW EEE according to the speed */
434
		stmmac_set_eee_pls(priv, priv->hw, ndev->phydev->link);
435
436

		ret = true;
437
		mutex_unlock(&priv->lock);
438

439
		netdev_dbg(priv->dev, "Energy-Efficient Ethernet initialized\n");
440
441
442
443
444
	}
out:
	return ret;
}

445
/* stmmac_get_tx_hwtstamp - get HW TX timestamps
446
 * @priv: driver private structure
447
 * @p : descriptor pointer
448
449
450
451
452
453
 * @skb : the socket buffer
 * Description :
 * This function will read timestamp from the descriptor & pass it to stack.
 * and also perform some sanity checks.
 */
static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
454
				   struct dma_desc *p, struct sk_buff *skb)
455
456
457
458
459
460
461
{
	struct skb_shared_hwtstamps shhwtstamp;
	u64 ns;

	if (!priv->hwts_tx_en)
		return;

Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
462
	/* exit if skb doesn't support hw tstamp */
463
	if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
464
465
466
		return;

	/* check tx tstamp status */
467
	if (stmmac_get_tx_timestamp_status(priv, p)) {
468
		/* get the valid tstamp */
469
		stmmac_get_timestamp(priv, p, priv->adv_ts, &ns);
470

471
472
		memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
		shhwtstamp.hwtstamp = ns_to_ktime(ns);
473

474
		netdev_dbg(priv->dev, "get valid TX hw timestamp %llu\n", ns);
475
476
477
		/* pass tstamp to stack */
		skb_tstamp_tx(skb, &shhwtstamp);
	}
478
479
480
481

	return;
}

482
/* stmmac_get_rx_hwtstamp - get HW RX timestamps
483
 * @priv: driver private structure
484
485
 * @p : descriptor pointer
 * @np : next descriptor pointer
486
487
488
489
490
 * @skb : the socket buffer
 * Description :
 * This function will read received packet's timestamp from the descriptor
 * and pass it to stack. It also perform some sanity checks.
 */
491
492
static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,
				   struct dma_desc *np, struct sk_buff *skb)
493
494
{
	struct skb_shared_hwtstamps *shhwtstamp = NULL;
495
	struct dma_desc *desc = p;
496
497
498
499
	u64 ns;

	if (!priv->hwts_rx_en)
		return;
500
501
502
	/* For GMAC4, the valid timestamp is from CTX next desc. */
	if (priv->plat->has_gmac4)
		desc = np;
503

504
	/* Check if timestamp is available */
505
506
	if (stmmac_get_rx_timestamp_status(priv, p, np, priv->adv_ts)) {
		stmmac_get_timestamp(priv, desc, priv->adv_ts, &ns);
507
		netdev_dbg(priv->dev, "get valid RX hw timestamp %llu\n", ns);
508
509
510
511
		shhwtstamp = skb_hwtstamps(skb);
		memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
		shhwtstamp->hwtstamp = ns_to_ktime(ns);
	} else  {
512
		netdev_dbg(priv->dev, "cannot get RX hw timestamp\n");
513
	}
514
515
516
517
518
}

/**
 *  stmmac_hwtstamp_ioctl - control hardware timestamping.
 *  @dev: device pointer.
519
 *  @ifr: An IOCTL specific structure, that can contain a pointer to
520
521
522
523
524
525
526
527
528
529
530
 *  a proprietary structure used to pass information to the driver.
 *  Description:
 *  This function configures the MAC to enable/disable both outgoing(TX)
 *  and incoming(RX) packets time stamping based on user input.
 *  Return Value:
 *  0 on success and an appropriate -ve integer on failure.
 */
static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
{
	struct stmmac_priv *priv = netdev_priv(dev);
	struct hwtstamp_config config;
531
	struct timespec64 now;
532
533
534
535
536
537
538
539
540
541
	u64 temp = 0;
	u32 ptp_v2 = 0;
	u32 tstamp_all = 0;
	u32 ptp_over_ipv4_udp = 0;
	u32 ptp_over_ipv6_udp = 0;
	u32 ptp_over_ethernet = 0;
	u32 snap_type_sel = 0;
	u32 ts_master_en = 0;
	u32 ts_event_en = 0;
	u32 value = 0;
542
	u32 sec_inc;
543
544
545
546
547
548
549
550
551
552

	if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
		netdev_alert(priv->dev, "No support for HW time stamping\n");
		priv->hwts_tx_en = 0;
		priv->hwts_rx_en = 0;

		return -EOPNOTSUPP;
	}

	if (copy_from_user(&config, ifr->ifr_data,
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
553
			   sizeof(struct hwtstamp_config)))
554
555
		return -EFAULT;

556
557
	netdev_dbg(priv->dev, "%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
		   __func__, config.flags, config.tx_type, config.rx_filter);
558
559
560
561
562

	/* reserved for future extensions */
	if (config.flags)
		return -EINVAL;

563
564
	if (config.tx_type != HWTSTAMP_TX_OFF &&
	    config.tx_type != HWTSTAMP_TX_ON)
565
566
567
568
569
		return -ERANGE;

	if (priv->adv_ts) {
		switch (config.rx_filter) {
		case HWTSTAMP_FILTER_NONE:
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
570
			/* time stamp no incoming packet at all */
571
572
573
574
			config.rx_filter = HWTSTAMP_FILTER_NONE;
			break;

		case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
575
			/* PTP v1, UDP, any kind of event packet */
576
577
			config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
			/* take time stamp for all event messages */
578
579
580
581
			if (priv->plat->has_gmac4)
				snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
			else
				snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
582
583
584
585
586
587

			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
			break;

		case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
588
			/* PTP v1, UDP, Sync packet */
589
590
591
592
593
594
595
596
597
			config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
			/* take time stamp for SYNC messages only */
			ts_event_en = PTP_TCR_TSEVNTENA;

			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
			break;

		case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
598
			/* PTP v1, UDP, Delay_req packet */
599
600
601
602
603
604
605
606
607
608
			config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
			/* take time stamp for Delay_Req messages only */
			ts_master_en = PTP_TCR_TSMSTRENA;
			ts_event_en = PTP_TCR_TSEVNTENA;

			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
			break;

		case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
609
			/* PTP v2, UDP, any kind of event packet */
610
611
612
			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
			ptp_v2 = PTP_TCR_TSVER2ENA;
			/* take time stamp for all event messages */
613
614
615
616
			if (priv->plat->has_gmac4)
				snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
			else
				snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
617
618
619
620
621
622

			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
			break;

		case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
623
			/* PTP v2, UDP, Sync packet */
624
625
626
627
628
629
630
631
632
633
			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
			ptp_v2 = PTP_TCR_TSVER2ENA;
			/* take time stamp for SYNC messages only */
			ts_event_en = PTP_TCR_TSEVNTENA;

			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
			break;

		case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
634
			/* PTP v2, UDP, Delay_req packet */
635
636
637
638
639
640
641
642
643
644
645
			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
			ptp_v2 = PTP_TCR_TSVER2ENA;
			/* take time stamp for Delay_Req messages only */
			ts_master_en = PTP_TCR_TSMSTRENA;
			ts_event_en = PTP_TCR_TSEVNTENA;

			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
			break;

		case HWTSTAMP_FILTER_PTP_V2_EVENT:
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
646
			/* PTP v2/802.AS1 any layer, any kind of event packet */
647
648
649
			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
			ptp_v2 = PTP_TCR_TSVER2ENA;
			/* take time stamp for all event messages */
650
651
652
653
			if (priv->plat->has_gmac4)
				snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
			else
				snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
654
655
656
657
658
659
660

			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
			ptp_over_ethernet = PTP_TCR_TSIPENA;
			break;

		case HWTSTAMP_FILTER_PTP_V2_SYNC:
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
661
			/* PTP v2/802.AS1, any layer, Sync packet */
662
663
664
665
666
667
668
669
670
671
672
			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
			ptp_v2 = PTP_TCR_TSVER2ENA;
			/* take time stamp for SYNC messages only */
			ts_event_en = PTP_TCR_TSEVNTENA;

			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
			ptp_over_ethernet = PTP_TCR_TSIPENA;
			break;

		case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
673
			/* PTP v2/802.AS1, any layer, Delay_req packet */
674
675
676
677
678
679
680
681
682
683
684
			config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
			ptp_v2 = PTP_TCR_TSVER2ENA;
			/* take time stamp for Delay_Req messages only */
			ts_master_en = PTP_TCR_TSMSTRENA;
			ts_event_en = PTP_TCR_TSEVNTENA;

			ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
			ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
			ptp_over_ethernet = PTP_TCR_TSIPENA;
			break;

685
		case HWTSTAMP_FILTER_NTP_ALL:
686
		case HWTSTAMP_FILTER_ALL:
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
687
			/* time stamp any incoming packet */
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
			config.rx_filter = HWTSTAMP_FILTER_ALL;
			tstamp_all = PTP_TCR_TSENALL;
			break;

		default:
			return -ERANGE;
		}
	} else {
		switch (config.rx_filter) {
		case HWTSTAMP_FILTER_NONE:
			config.rx_filter = HWTSTAMP_FILTER_NONE;
			break;
		default:
			/* PTP v1, UDP, any kind of event packet */
			config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
			break;
		}
	}
	priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
707
	priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
708
709

	if (!priv->hwts_tx_en && !priv->hwts_rx_en)
710
		stmmac_config_hw_tstamping(priv, priv->ptpaddr, 0);
711
712
	else {
		value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
713
714
715
			 tstamp_all | ptp_v2 | ptp_over_ethernet |
			 ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
			 ts_master_en | snap_type_sel);
716
		stmmac_config_hw_tstamping(priv, priv->ptpaddr, value);
717
718

		/* program Sub Second Increment reg */
719
720
721
		stmmac_config_sub_second_increment(priv,
				priv->ptpaddr, priv->plat->clk_ptp_rate,
				priv->plat->has_gmac4, &sec_inc);
722
		temp = div_u64(1000000000ULL, sec_inc);
723

724
725
726
727
		/* Store sub second increment and flags for later use */
		priv->sub_second_inc = sec_inc;
		priv->systime_flags = value;

728
729
730
		/* calculate default added value:
		 * formula is :
		 * addend = (2^32)/freq_div_ratio;
731
		 * where, freq_div_ratio = 1e9ns/sec_inc
732
		 */
733
		temp = (u64)(temp << 32);
734
		priv->default_addend = div_u64(temp, priv->plat->clk_ptp_rate);
735
		stmmac_config_addend(priv, priv->ptpaddr, priv->default_addend);
736
737

		/* initialize system time */
738
739
740
		ktime_get_real_ts64(&now);

		/* lower 32 bits of tv_sec are safe until y2106 */
741
742
		stmmac_init_systime(priv, priv->ptpaddr,
				(u32)now.tv_sec, now.tv_nsec);
743
744
745
746
747
748
	}

	return copy_to_user(ifr->ifr_data, &config,
			    sizeof(struct hwtstamp_config)) ? -EFAULT : 0;
}

749
/**
750
 * stmmac_init_ptp - init PTP
751
 * @priv: driver private structure
752
 * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
753
 * This is done by looking at the HW cap. register.
754
 * This function also registers the ptp driver.
755
 */
756
static int stmmac_init_ptp(struct stmmac_priv *priv)
757
{
758
759
760
	if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
		return -EOPNOTSUPP;

761
	priv->adv_ts = 0;
762
763
764
765
766
	/* Check if adv_ts can be enabled for dwmac 4.x core */
	if (priv->plat->has_gmac4 && priv->dma_cap.atime_stamp)
		priv->adv_ts = 1;
	/* Dwmac 3.x core with extend_desc can support adv_ts */
	else if (priv->extend_desc && priv->dma_cap.atime_stamp)
767
768
		priv->adv_ts = 1;

769
770
	if (priv->dma_cap.time_stamp)
		netdev_info(priv->dev, "IEEE 1588-2002 Timestamp supported\n");
771

772
773
774
	if (priv->adv_ts)
		netdev_info(priv->dev,
			    "IEEE 1588-2008 Advanced Timestamp supported\n");
775
776
777

	priv->hwts_tx_en = 0;
	priv->hwts_rx_en = 0;
778

779
780
781
	stmmac_ptp_register(priv);

	return 0;
782
783
784
785
}

static void stmmac_release_ptp(struct stmmac_priv *priv)
{
786
787
	if (priv->plat->clk_ptp_ref)
		clk_disable_unprepare(priv->plat->clk_ptp_ref);
788
	stmmac_ptp_unregister(priv);
789
790
}

791
792
793
794
795
796
797
798
799
/**
 *  stmmac_mac_flow_ctrl - Configure flow control in all queues
 *  @priv: driver private structure
 *  Description: It is used for configuring the flow control in all queues
 */
static void stmmac_mac_flow_ctrl(struct stmmac_priv *priv, u32 duplex)
{
	u32 tx_cnt = priv->plat->tx_queues_to_use;

800
801
	stmmac_flow_ctrl(priv, priv->hw, duplex, priv->flow_ctrl,
			priv->pause, tx_cnt);
802
803
}

804
/**
805
 * stmmac_adjust_link - adjusts the link parameters
806
 * @dev: net device structure
807
808
809
810
811
 * Description: this is the helper called by the physical abstraction layer
 * drivers to communicate the phy link status. According the speed and duplex
 * this driver can invoke registered glue-logic as well.
 * It also invoke the eee initialization because it could happen when switch
 * on different networks (that are eee capable).
812
813
814
815
 */
static void stmmac_adjust_link(struct net_device *dev)
{
	struct stmmac_priv *priv = netdev_priv(dev);
816
	struct phy_device *phydev = dev->phydev;
817
	bool new_state = false;
818

819
	if (!phydev)
820
821
		return;

822
	mutex_lock(&priv->lock);
823

824
	if (phydev->link) {
825
		u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
826
827
828
829

		/* Now we make sure that we can be in full duplex mode.
		 * If not, we operate in half-duplex mode. */
		if (phydev->duplex != priv->oldduplex) {
830
			new_state = true;
831
			if (!phydev->duplex)
832
				ctrl &= ~priv->hw->link.duplex;
833
			else
834
				ctrl |= priv->hw->link.duplex;
835
836
837
838
			priv->oldduplex = phydev->duplex;
		}
		/* Flow Control operation */
		if (phydev->pause)
839
			stmmac_mac_flow_ctrl(priv, phydev->duplex);
840
841

		if (phydev->speed != priv->speed) {
842
			new_state = true;
843
			ctrl &= ~priv->hw->link.speed_mask;
844
			switch (phydev->speed) {
845
			case SPEED_1000:
846
				ctrl |= priv->hw->link.speed1000;
847
				break;
848
			case SPEED_100:
849
				ctrl |= priv->hw->link.speed100;
850
				break;
851
			case SPEED_10:
852
				ctrl |= priv->hw->link.speed10;
853
854
				break;
			default:
855
				netif_warn(priv, link, priv->dev,
856
					   "broken speed: %d\n", phydev->speed);
857
				phydev->speed = SPEED_UNKNOWN;
858
859
				break;
			}
860
861
			if (phydev->speed != SPEED_UNKNOWN)
				stmmac_hw_fix_mac_speed(priv);
862
863
864
			priv->speed = phydev->speed;
		}

865
		writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
866
867

		if (!priv->oldlink) {
868
			new_state = true;
869
			priv->oldlink = true;
870
871
		}
	} else if (priv->oldlink) {
872
		new_state = true;
873
		priv->oldlink = false;
874
875
		priv->speed = SPEED_UNKNOWN;
		priv->oldduplex = DUPLEX_UNKNOWN;
876
877
878
879
880
	}

	if (new_state && netif_msg_link(priv))
		phy_print_status(phydev);

881
	mutex_unlock(&priv->lock);
882

883
884
885
886
887
888
889
890
891
892
	if (phydev->is_pseudo_fixed_link)
		/* Stop PHY layer to call the hook to adjust the link in case
		 * of a switch is attached to the stmmac driver.
		 */
		phydev->irq = PHY_IGNORE_INTERRUPT;
	else
		/* At this stage, init the EEE if supported.
		 * Never called in case of fixed_link.
		 */
		priv->eee_enabled = stmmac_eee_init(priv);
893
894
}

895
/**
896
 * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
897
898
899
900
901
 * @priv: driver private structure
 * Description: this is to verify if the HW supports the PCS.
 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
 * configured for the TBI, RTBI, or SGMII PHY interface.
 */
902
903
904
905
906
static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
{
	int interface = priv->plat->interface;

	if (priv->dma_cap.pcs) {
Byungho An's avatar
Byungho An committed
907
908
909
910
		if ((interface == PHY_INTERFACE_MODE_RGMII) ||
		    (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
		    (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
		    (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
911
			netdev_dbg(priv->dev, "PCS RGMII support enabled\n");
912
			priv->hw->pcs = STMMAC_PCS_RGMII;
Byungho An's avatar
Byungho An committed
913
		} else if (interface == PHY_INTERFACE_MODE_SGMII) {
914
			netdev_dbg(priv->dev, "PCS SGMII support enabled\n");
915
			priv->hw->pcs = STMMAC_PCS_SGMII;
916
917
918
919
		}
	}
}

920
921
922
923
924
925
926
927
928
929
930
931
/**
 * stmmac_init_phy - PHY initialization
 * @dev: net device structure
 * Description: it initializes the driver's PHY state, and attaches the PHY
 * to the mac driver.
 *  Return value:
 *  0 on success
 */
static int stmmac_init_phy(struct net_device *dev)
{
	struct stmmac_priv *priv = netdev_priv(dev);
	struct phy_device *phydev;
932
	char phy_id_fmt[MII_BUS_ID_SIZE + 3];
933
	char bus_id[MII_BUS_ID_SIZE];
934
	int interface = priv->plat->interface;
935
	int max_speed = priv->plat->max_speed;
936
	priv->oldlink = false;
937
938
	priv->speed = SPEED_UNKNOWN;
	priv->oldduplex = DUPLEX_UNKNOWN;
939

940
941
942
943
	if (priv->plat->phy_node) {
		phydev = of_phy_connect(dev, priv->plat->phy_node,
					&stmmac_adjust_link, 0, interface);
	} else {
Giuseppe CAVALLARO's avatar
Giuseppe CAVALLARO committed
944
945
		snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
			 priv->plat->bus_id);
946
947
948

		snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
			 priv->plat->phy_addr);
949
		netdev_dbg(priv->dev, "%s: trying to attach to %s\n", __func__,
950
			   phy_id_fmt);
951
952
953
954

		phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link,
				     interface);
	}
955

956
	if (IS_ERR_OR_NULL(phydev)) {
957
		netdev_err(priv->dev, "Could not attach to PHY\n");
958
959
960
		if (!phydev)
			return -ENODEV;

961
962
963
		return PTR_ERR(phydev);
	}

964
	/* Stop Advertising 1000BASE Capability if interface is not GMII */
965
	if ((interface == PHY_INTERFACE_MODE_MII) ||
966
	    (interface == PHY_INTERFACE_MODE_RMII) ||
Pavel Machek's avatar
Pavel Machek committed
967
		(max_speed < 1000 && max_speed > 0))
968
969
		phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
					 SUPPORTED_1000baseT_Full);
970

971
972
973
974
975
976
977
	/*
	 * Broken HW is sometimes missing the pull-up resistor on the
	 * MDIO line, which results in reads to non-existent devices returning
	 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
	 * device as well.
	 * Note: phydev->phy_id is the result of reading the UID PHY registers.
	 */
978
	if (!priv->plat->phy_node && phydev->phy_id == 0) {
979
980
981
		phy_disconnect(phydev);
		return -ENODEV;
	}
982

983
984
985
986
987
988
989
	/* stmmac_adjust_link will change this to PHY_IGNORE_INTERRUPT to avoid
	 * subsequent PHY polling, make sure we force a link transition if
	 * we have a UP/DOWN/UP transition
	 */
	if (phydev->is_pseudo_fixed_link)
		phydev->irq = PHY_POLL;

990
	phy_attached_info(phydev);
991
992
993
	return 0;
}

994
static void stmmac_display_rx_rings(struct stmmac_priv *priv)
995
{
996
	u32 rx_cnt = priv->plat->rx_queues_to_use;
997
	void *head_rx;
998
	u32 queue;
999

1000
1001
1002
	/* Display RX rings */
	for (queue = 0; queue < rx_cnt; queue++) {
		struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
Alexandre TORGUE's avatar