public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH net 0/4] net: fec: fix some PTP related issues
@ 2025-11-25  8:52 Wei Fang
  2025-11-25  8:52 ` [PATCH net 1/4] net: fec: cancel perout_timer when PEROUT is disabled Wei Fang
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Wei Fang @ 2025-11-25  8:52 UTC (permalink / raw)
  To: shenwei.wang, xiaoning.wang, andrew+netdev, davem, edumazet, kuba,
	pabeni, eric, richardcochran
  Cc: imx, netdev, linux-kernel

There are some issues which were introduced by the commit 350749b909bf
("net: fec: Add support for periodic output signal of PPS"). See each
patch for more details.

Wei Fang (4):
  net: fec: cancel perout_timer when PEROUT is disabled
  net: fec: do not update PEROUT if it is enabled
  net: fec: do not allow enabling PPS and PEROUT simultaneously
  net: fec: do not register PPS event for PEROUT

 drivers/net/ethernet/freescale/fec.h     |  1 +
 drivers/net/ethernet/freescale/fec_ptp.c | 64 +++++++++++++++++++-----
 2 files changed, 53 insertions(+), 12 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH net 1/4] net: fec: cancel perout_timer when PEROUT is disabled
  2025-11-25  8:52 [PATCH net 0/4] net: fec: fix some PTP related issues Wei Fang
@ 2025-11-25  8:52 ` Wei Fang
  2025-11-25  8:52 ` [PATCH net 2/4] net: fec: do not update PEROUT if it is enabled Wei Fang
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Wei Fang @ 2025-11-25  8:52 UTC (permalink / raw)
  To: shenwei.wang, xiaoning.wang, andrew+netdev, davem, edumazet, kuba,
	pabeni, eric, richardcochran
  Cc: imx, netdev, linux-kernel

The PEROUT allows the user to set a specified future time to output the
periodic signal. If the future time is far from the current time, the FEC
driver will use hrtimer to configure PEROUT one second before the future
time. However, the hrtimer will not be canceled if the PEROUT is disabled
before the hrtimer expires. So the PEROUT will be configured when the
hrtimer expires, which is not as expected. Therefore, cancel the hrtimer
in fec_ptp_pps_disable() to fix this issue.

Fixes: 350749b909bf ("net: fec: Add support for periodic output signal of PPS")
Signed-off-by: Wei Fang <wei.fang@nxp.com>
---
 drivers/net/ethernet/freescale/fec_ptp.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/net/ethernet/freescale/fec_ptp.c b/drivers/net/ethernet/freescale/fec_ptp.c
index fa88b47d526c..7a5367ea9410 100644
--- a/drivers/net/ethernet/freescale/fec_ptp.c
+++ b/drivers/net/ethernet/freescale/fec_ptp.c
@@ -497,6 +497,8 @@ static int fec_ptp_pps_disable(struct fec_enet_private *fep, uint channel)
 {
 	unsigned long flags;
 
+	hrtimer_cancel(&fep->perout_timer);
+
 	spin_lock_irqsave(&fep->tmreg_lock, flags);
 	writel(0, fep->hwp + FEC_TCSR(channel));
 	spin_unlock_irqrestore(&fep->tmreg_lock, flags);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH net 2/4] net: fec: do not update PEROUT if it is enabled
  2025-11-25  8:52 [PATCH net 0/4] net: fec: fix some PTP related issues Wei Fang
  2025-11-25  8:52 ` [PATCH net 1/4] net: fec: cancel perout_timer when PEROUT is disabled Wei Fang
@ 2025-11-25  8:52 ` Wei Fang
  2025-11-25  8:52 ` [PATCH net 3/4] net: fec: do not allow enabling PPS and PEROUT simultaneously Wei Fang
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Wei Fang @ 2025-11-25  8:52 UTC (permalink / raw)
  To: shenwei.wang, xiaoning.wang, andrew+netdev, davem, edumazet, kuba,
	pabeni, eric, richardcochran
  Cc: imx, netdev, linux-kernel

If the previously set PEROUT is already active, updating it will cause
the new PEROUT to start immediately instead of at the specified time.
This is because fep->reload_period is updated whithout check whether
the PEROUT is enabled, and the old PEROUT is not disabled. Therefore,
the pulse period will be updated immediately in the pulse interrupt
handler fec_pps_interrupt().

Currently, the driver does not support directly updating PEROUT and it
will make the logic be more complicated. To fix the current issue, add
a check before enabling the PEROUT, the driver will return an error if
PEROUT is enabled. If users wants to update a new PEROUT, they should
disable the old PEROUT first.

Fixes: 350749b909bf ("net: fec: Add support for periodic output signal of PPS")
Signed-off-by: Wei Fang <wei.fang@nxp.com>
---
 drivers/net/ethernet/freescale/fec.h     |  1 +
 drivers/net/ethernet/freescale/fec_ptp.c | 43 ++++++++++++++++++------
 2 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/drivers/net/ethernet/freescale/fec.h b/drivers/net/ethernet/freescale/fec.h
index 41e0d85d15da..abf1ef8e76c6 100644
--- a/drivers/net/ethernet/freescale/fec.h
+++ b/drivers/net/ethernet/freescale/fec.h
@@ -687,6 +687,7 @@ struct fec_enet_private {
 	unsigned int reload_period;
 	int pps_enable;
 	unsigned int next_counter;
+	bool perout_enable;
 	struct hrtimer perout_timer;
 	u64 perout_stime;
 
diff --git a/drivers/net/ethernet/freescale/fec_ptp.c b/drivers/net/ethernet/freescale/fec_ptp.c
index 7a5367ea9410..f31b1626c12f 100644
--- a/drivers/net/ethernet/freescale/fec_ptp.c
+++ b/drivers/net/ethernet/freescale/fec_ptp.c
@@ -243,6 +243,7 @@ static int fec_ptp_pps_perout(struct fec_enet_private *fep)
 	 * the FEC_TCCR register in time and missed the start time.
 	 */
 	if (fep->perout_stime < curr_time + 100 * NSEC_PER_MSEC) {
+		fep->perout_enable = false;
 		dev_err(&fep->pdev->dev, "Current time is too close to the start time!\n");
 		spin_unlock_irqrestore(&fep->tmreg_lock, flags);
 		return -1;
@@ -500,6 +501,7 @@ static int fec_ptp_pps_disable(struct fec_enet_private *fep, uint channel)
 	hrtimer_cancel(&fep->perout_timer);
 
 	spin_lock_irqsave(&fep->tmreg_lock, flags);
+	fep->perout_enable = false;
 	writel(0, fep->hwp + FEC_TCSR(channel));
 	spin_unlock_irqrestore(&fep->tmreg_lock, flags);
 
@@ -531,6 +533,8 @@ static int fec_ptp_enable(struct ptp_clock_info *ptp,
 
 		return ret;
 	} else if (rq->type == PTP_CLK_REQ_PEROUT) {
+		u32 reload_period;
+
 		/* Reject requests with unsupported flags */
 		if (rq->perout.flags)
 			return -EOPNOTSUPP;
@@ -550,12 +554,14 @@ static int fec_ptp_enable(struct ptp_clock_info *ptp,
 			return -EOPNOTSUPP;
 		}
 
-		fep->reload_period = div_u64(period_ns, 2);
-		if (on && fep->reload_period) {
+		reload_period = div_u64(period_ns, 2);
+		if (on && reload_period) {
+			u64 perout_stime;
+
 			/* Convert 1588 timestamp to ns*/
 			start_time.tv_sec = rq->perout.start.sec;
 			start_time.tv_nsec = rq->perout.start.nsec;
-			fep->perout_stime = timespec64_to_ns(&start_time);
+			perout_stime = timespec64_to_ns(&start_time);
 
 			mutex_lock(&fep->ptp_clk_mutex);
 			if (!fep->ptp_clk_on) {
@@ -564,18 +570,35 @@ static int fec_ptp_enable(struct ptp_clock_info *ptp,
 				return -EOPNOTSUPP;
 			}
 			spin_lock_irqsave(&fep->tmreg_lock, flags);
+
+			if (fep->perout_enable) {
+				dev_err(&fep->pdev->dev,
+					"PEROUT has been enabled\n");
+				ret = -EBUSY;
+				goto unlock;
+			}
+
 			/* Read current timestamp */
 			curr_time = timecounter_read(&fep->tc);
-			spin_unlock_irqrestore(&fep->tmreg_lock, flags);
-			mutex_unlock(&fep->ptp_clk_mutex);
+			if (perout_stime <= curr_time) {
+				dev_err(&fep->pdev->dev,
+					"Start time must be greater than current time\n");
+				ret = -EINVAL;
+				goto unlock;
+			}
 
 			/* Calculate time difference */
-			delta = fep->perout_stime - curr_time;
+			delta = perout_stime - curr_time;
+			fep->reload_period = reload_period;
+			fep->perout_stime = perout_stime;
+			fep->perout_enable = true;
 
-			if (fep->perout_stime <= curr_time) {
-				dev_err(&fep->pdev->dev, "Start time must larger than current time!\n");
-				return -EINVAL;
-			}
+unlock:
+			spin_unlock_irqrestore(&fep->tmreg_lock, flags);
+			mutex_unlock(&fep->ptp_clk_mutex);
+
+			if (ret)
+				return ret;
 
 			/* Because the timer counter of FEC only has 31-bits, correspondingly,
 			 * the time comparison register FEC_TCCR also only low 31 bits can be
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH net 3/4] net: fec: do not allow enabling PPS and PEROUT simultaneously
  2025-11-25  8:52 [PATCH net 0/4] net: fec: fix some PTP related issues Wei Fang
  2025-11-25  8:52 ` [PATCH net 1/4] net: fec: cancel perout_timer when PEROUT is disabled Wei Fang
  2025-11-25  8:52 ` [PATCH net 2/4] net: fec: do not update PEROUT if it is enabled Wei Fang
@ 2025-11-25  8:52 ` Wei Fang
  2025-11-25  8:52 ` [PATCH net 4/4] net: fec: do not register PPS event for PEROUT Wei Fang
  2025-11-27 11:00 ` [PATCH net 0/4] net: fec: fix some PTP related issues patchwork-bot+netdevbpf
  4 siblings, 0 replies; 6+ messages in thread
From: Wei Fang @ 2025-11-25  8:52 UTC (permalink / raw)
  To: shenwei.wang, xiaoning.wang, andrew+netdev, davem, edumazet, kuba,
	pabeni, eric, richardcochran
  Cc: imx, netdev, linux-kernel

In the current driver, PPS and PEROUT use the same channel to generate
the events, so they cannot be enabled at the same time. Otherwise, the
later configuration will overwrite the earlier configuration. Therefore,
when configuring PPS, the driver will check whether PEROUT is enabled.
Similarly, when configuring PEROUT, the driver will check whether PPS
is enabled.

Fixes: 350749b909bf ("net: fec: Add support for periodic output signal of PPS")
Signed-off-by: Wei Fang <wei.fang@nxp.com>
---
 drivers/net/ethernet/freescale/fec_ptp.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/net/ethernet/freescale/fec_ptp.c b/drivers/net/ethernet/freescale/fec_ptp.c
index f31b1626c12f..ed5d59abeb53 100644
--- a/drivers/net/ethernet/freescale/fec_ptp.c
+++ b/drivers/net/ethernet/freescale/fec_ptp.c
@@ -128,6 +128,12 @@ static int fec_ptp_enable_pps(struct fec_enet_private *fep, uint enable)
 
 	spin_lock_irqsave(&fep->tmreg_lock, flags);
 
+	if (fep->perout_enable) {
+		spin_unlock_irqrestore(&fep->tmreg_lock, flags);
+		dev_err(&fep->pdev->dev, "PEROUT is running");
+		return -EBUSY;
+	}
+
 	if (fep->pps_enable == enable) {
 		spin_unlock_irqrestore(&fep->tmreg_lock, flags);
 		return 0;
@@ -571,6 +577,12 @@ static int fec_ptp_enable(struct ptp_clock_info *ptp,
 			}
 			spin_lock_irqsave(&fep->tmreg_lock, flags);
 
+			if (fep->pps_enable) {
+				dev_err(&fep->pdev->dev, "PPS is running");
+				ret = -EBUSY;
+				goto unlock;
+			}
+
 			if (fep->perout_enable) {
 				dev_err(&fep->pdev->dev,
 					"PEROUT has been enabled\n");
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH net 4/4] net: fec: do not register PPS event for PEROUT
  2025-11-25  8:52 [PATCH net 0/4] net: fec: fix some PTP related issues Wei Fang
                   ` (2 preceding siblings ...)
  2025-11-25  8:52 ` [PATCH net 3/4] net: fec: do not allow enabling PPS and PEROUT simultaneously Wei Fang
@ 2025-11-25  8:52 ` Wei Fang
  2025-11-27 11:00 ` [PATCH net 0/4] net: fec: fix some PTP related issues patchwork-bot+netdevbpf
  4 siblings, 0 replies; 6+ messages in thread
From: Wei Fang @ 2025-11-25  8:52 UTC (permalink / raw)
  To: shenwei.wang, xiaoning.wang, andrew+netdev, davem, edumazet, kuba,
	pabeni, eric, richardcochran
  Cc: imx, netdev, linux-kernel

There are currently two situations that can trigger the PTP interrupt,
one is the PPS event, the other is the PEROUT event. However, the irq
handler fec_pps_interrupt() does not check the irq event type and
directly registers a PPS event into the system, but the event may be
a PEROUT event. This is incorrect because PEROUT is an output signal,
while PPS is the input of the kernel PPS system. Therefore, add a check
for the event type, if pps_enable is true, it means that the current
event is a PPS event, and then the PPS event is registered.

Fixes: 350749b909bf ("net: fec: Add support for periodic output signal of PPS")
Signed-off-by: Wei Fang <wei.fang@nxp.com>
---
 drivers/net/ethernet/freescale/fec_ptp.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/freescale/fec_ptp.c b/drivers/net/ethernet/freescale/fec_ptp.c
index ed5d59abeb53..4b7bad9a485d 100644
--- a/drivers/net/ethernet/freescale/fec_ptp.c
+++ b/drivers/net/ethernet/freescale/fec_ptp.c
@@ -718,8 +718,11 @@ static irqreturn_t fec_pps_interrupt(int irq, void *dev_id)
 		fep->next_counter = (fep->next_counter + fep->reload_period) &
 				fep->cc.mask;
 
-		event.type = PTP_CLOCK_PPS;
-		ptp_clock_event(fep->ptp_clock, &event);
+		if (fep->pps_enable) {
+			event.type = PTP_CLOCK_PPS;
+			ptp_clock_event(fep->ptp_clock, &event);
+		}
+
 		return IRQ_HANDLED;
 	}
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH net 0/4] net: fec: fix some PTP related issues
  2025-11-25  8:52 [PATCH net 0/4] net: fec: fix some PTP related issues Wei Fang
                   ` (3 preceding siblings ...)
  2025-11-25  8:52 ` [PATCH net 4/4] net: fec: do not register PPS event for PEROUT Wei Fang
@ 2025-11-27 11:00 ` patchwork-bot+netdevbpf
  4 siblings, 0 replies; 6+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-11-27 11:00 UTC (permalink / raw)
  To: Wei Fang
  Cc: shenwei.wang, xiaoning.wang, andrew+netdev, davem, edumazet, kuba,
	pabeni, eric, richardcochran, imx, netdev, linux-kernel

Hello:

This series was applied to netdev/net.git (main)
by Paolo Abeni <pabeni@redhat.com>:

On Tue, 25 Nov 2025 16:52:06 +0800 you wrote:
> There are some issues which were introduced by the commit 350749b909bf
> ("net: fec: Add support for periodic output signal of PPS"). See each
> patch for more details.
> 
> Wei Fang (4):
>   net: fec: cancel perout_timer when PEROUT is disabled
>   net: fec: do not update PEROUT if it is enabled
>   net: fec: do not allow enabling PPS and PEROUT simultaneously
>   net: fec: do not register PPS event for PEROUT
> 
> [...]

Here is the summary with links:
  - [net,1/4] net: fec: cancel perout_timer when PEROUT is disabled
    https://git.kernel.org/netdev/net/c/50caa744689e
  - [net,2/4] net: fec: do not update PEROUT if it is enabled
    https://git.kernel.org/netdev/net/c/e97faa0c20ea
  - [net,3/4] net: fec: do not allow enabling PPS and PEROUT simultaneously
    https://git.kernel.org/netdev/net/c/c0a1f3d7e128
  - [net,4/4] net: fec: do not register PPS event for PEROUT
    https://git.kernel.org/netdev/net/c/9a060d0fac9e

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-11-27 11:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-25  8:52 [PATCH net 0/4] net: fec: fix some PTP related issues Wei Fang
2025-11-25  8:52 ` [PATCH net 1/4] net: fec: cancel perout_timer when PEROUT is disabled Wei Fang
2025-11-25  8:52 ` [PATCH net 2/4] net: fec: do not update PEROUT if it is enabled Wei Fang
2025-11-25  8:52 ` [PATCH net 3/4] net: fec: do not allow enabling PPS and PEROUT simultaneously Wei Fang
2025-11-25  8:52 ` [PATCH net 4/4] net: fec: do not register PPS event for PEROUT Wei Fang
2025-11-27 11:00 ` [PATCH net 0/4] net: fec: fix some PTP related issues patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox