Skip to contentSkip to conversation

ptp: ocp: fix timestamp teardown and DPLL routing

PATCH Superseded Ahmad Byagowi 3 participants 15 messages Nov 18 – Nov 23

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover ptp: ocp: fix timestamp teardown and DPLL routing

These fixes affect existing ptp_ocp boards and do not add WiWi support. They were identified in the public review of the WiWi series and are split out so they can be reviewed and backported independently.

Prevent timestamp rearming during detach with a mutex-protected stopping state. Then correct DPLL selector values, disabled-input frequency reporting and the DCF77 output frequency. Each fix has its own Fixes tag.

Validation used source-derived C tests under ASan/UBSan. The old source reproduces rearming after detach's disable. The new source rejects late enables, waits for an enable already in progress, and drains a mocked active IRQ. Targeted mutations of each DPLL fix fail the regression tests. The driver object builds against this net base with W=1 and KCFLAGS=-Werror. No physical card, real IRQ delivery or in-kernel use-after-free reproduction was tested for these fixes.

The report came from netdev's Sashiko AI review. Codex prepared the code, tests and changelogs from that review and the upstream sources, following the request to separate common fixes and validate removal ordering. This origin information follows Documentation/process/generated-content.rst.

Related review:
https://lore.kernel.org/r/[email protected]

WiWi v4 will be based on these fixes after they are available in net-next; it will not carry a second copy of the common fixes.

Ahmad Byagowi (4):
ptp: ocp: prevent timestamp rearming during detach
ptp: ocp: use hardware selectors in DPLL frequency operations
ptp: ocp: report disabled SMA inputs consistently over DPLL
ptp: ocp: correct the DCF output frequency

 drivers/ptp/ptp_ocp.c | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

base-commit: 2a324a58a084c5cfd7e5a2f55e0786bee170a4a5

Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw

PATCH 1/4 ptp: ocp: prevent timestamp rearming during detach

Detach disables timestamp interrupts before unregistering the PTP clock. Until the POSIX clock is unregistered, an ioctl can rearm a channel after that disable. The clock can then be released while the timestamp IRQ is still registered. The core cleanup does not necessarily disable the channel: EXTTS requests do not require a PTP_PF_EXTTS pin assignment.

Serialize timestamp enables and disables with a mutex. Mark timestampers as stopping under that mutex before the detach disable loop, so an enable already in progress finishes first and later enables fail with -ENODEV. Keep disables available for the PTP core cleanup. The existing MMIO readback and synchronize_irq() then drain events before ptp_clock_unregister() can release the clock.

Reported by the netdev Sashiko AI review, then checked against the current PTP and POSIX-clock paths. A source-derived userspace harness forces an enable after detach's disable and reproduces the rearm with the old source. With this fix it rejects that enable, waits for an enable already in progress, and waits for a mocked active IRQ. Both timestamp and combined PPS/EXTTS paths pass with ASan/UBSan. This validates ordering with stubbed MMIO/IRQ operations, not an in-kernel use-after-free reproduction. No physical card was tested.

Codex prepared the fix, tests and changelog from the review and upstream source, at the request to separate common-driver fixes from WiWi support and validate removal ordering.

Fixes 627366c51145 ("ptp: ocp: fix resource freeing order")
---
 drivers/ptp/ptp_ocp.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
drivers/ptp/ptp_ocp.c +20 -2
index 625a85b2dc..478f7012b2 100644
@@ -367,6 +367,9 @@ struct ptp_ocp {
 	struct pci_dev		*pdev;
 	struct device		dev;
 	spinlock_t		lock;
+	/* Serializes timestamp arming and teardown. */
+	struct mutex		ts_lock;
+	bool			ts_stopping;
 	struct ocp_reg __iomem	*reg;
 	struct tod_reg __iomem	*tod;
 	struct pps_reg __iomem	*pps_to_ext;
@@ -2550,6 +2553,13 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
 	struct ptp_ocp_ext_src *ext = priv;
 	struct ts_reg __iomem *reg = ext->mem;
 	struct ptp_ocp *bp = ext->bp;
+	int err = 0;
+
+	mutex_lock(&bp->ts_lock);
+	if (enable && bp->ts_stopping) {
+		err = -ENODEV;
+		goto out;
+	}
 
 	if (ext == bp->pps) {
 		u32 old_map = bp->pps_req_map;
@@ -2561,7 +2571,7 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
 
 		/* if no state change, just return */
 		if ((!!old_map ^ !!bp->pps_req_map) == 0)
-			return 0;
+			goto out;
 	}
 
 	if (enable) {
@@ -2578,7 +2588,9 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
 			synchronize_irq(irq_vec);
 	}
 
-	return 0;
+out:
+	mutex_unlock(&bp->ts_lock);
+	return err;
 }
 
 static void
@@ -5613,6 +5625,7 @@ ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev)
 
 	bp->ptp_info = ptp_ocp_clock_info;
 	spin_lock_init(&bp->lock);
+	mutex_init(&bp->ts_lock);
 
 	for (i = 0; i < __PORT_COUNT; i++)
 		bp->port[i].uport = NULL;
@@ -5755,6 +5768,10 @@ ptp_ocp_detach(struct ptp_ocp *bp)
 	ptp_ocp_detach_sysfs(bp);
 	ptp_ocp_attr_group_del(bp);
 	timer_delete_sync(&bp->watchdog);
+	/* Drain in-flight enables and prevent rearming before unregister. */
+	mutex_lock(&bp->ts_lock);
+	bp->ts_stopping = true;
+	mutex_unlock(&bp->ts_lock);
 	/* Disable interrupts on all timestampers */
 	if (bp->ts0)
 		ptp_ocp_ts_enable(bp->ts0, 0, false);
@@ -5789,6 +5806,7 @@ ptp_ocp_detach(struct ptp_ocp *bp)
 	if (bp->n_irqs)
 		pci_free_irq_vectors(bp->pdev);
 	mutex_destroy(&bp->cpld_lock);
+	mutex_destroy(&bp->ts_lock);
 	device_unregister(&bp->dev);
 }
 
Signature · 1 line
2.54.0 (Apple Git-157)
maintainer
#2 lore raw

Re: [PATCH net 1/4] ptp: ocp: prevent timestamp rearming during detach

Quoted 51 lines · Ahmad Byagowi

Detach disables timestamp interrupts before unregistering the PTP clock. Until the POSIX clock is unregistered, an ioctl can rearm a channel after that disable. The clock can then be released while the timestamp IRQ is still registered. The core cleanup does not necessarily disable the channel: EXTTS requests do not require a PTP_PF_EXTTS pin assignment.

Serialize timestamp enables and disables with a mutex. Mark timestampers as stopping under that mutex before the detach disable loop, so an enable already in progress finishes first and later enables fail with -ENODEV. Keep disables available for the PTP core cleanup. The existing MMIO readback and synchronize_irq() then drain events before ptp_clock_unregister() can release the clock.

Reported by the netdev Sashiko AI review, then checked against the current PTP and POSIX-clock paths. A source-derived userspace harness forces an enable after detach's disable and reproduces the rearm with the old source. With this fix it rejects that enable, waits for an enable already in progress, and waits for a mocked active IRQ. Both timestamp and combined PPS/EXTTS paths pass with ASan/UBSan. This validates ordering with stubbed MMIO/IRQ operations, not an in-kernel use-after-free reproduction. No physical card was tested.

Codex prepared the fix, tests and changelog from the review and upstream source, at the request to separate common-driver fixes from WiWi support and validate removal ordering.

Fixes 627366c51145 ("ptp: ocp: fix resource freeing order")
---
  drivers/ptp/ptp_ocp.c | 22 ++++++++++++++++++++--
  1 file changed, 20 insertions(+), 2 deletions(-)
drivers/ptp/ptp_ocp.c +3 -0
index 625a85b2dc..478f7012b2 100644
@@ -367,6 +367,9 @@ struct ptp_ocp {
  	struct pci_dev		*pdev;
  	struct device		dev;
  	spinlock_t		lock;
+	/* Serializes timestamp arming and teardown. */
+	struct mutex		ts_lock;
+	bool			ts_stopping;
  	struct ocp_reg __iomem	*reg;
  	struct tod_reg __iomem	*tod;
  	struct pps_reg __iomem	*pps_to_ext;

drivers/ptp/ptp_ocp.c ptp_ocp_ts_enable(void *priv, u32 req, bool enable) · line 2562

Quoted 10 lines
drivers/ptp/ptp_ocp.c +7 -0
@@ -2550,6 +2553,13 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
  	struct ptp_ocp_ext_src *ext = priv;
  	struct ts_reg __iomem *reg = ext->mem;
  	struct ptp_ocp *bp = ext->bp;
+	int err = 0;
+
+	mutex_lock(&bp->ts_lock);
+	if (enable && bp->ts_stopping) {
+		err = -ENODEV;
+		goto out;
+	}

ts_stopping is changed only in device detach callback, there is actually no need to cover whole ptp_ocp_ts_enable() with mutex, I believe existing spinlock can be used to serialize this access, no?

Quoted 50 lines
drivers/ptp/ptp_ocp.c +10 -2
  	if (ext == bp->pps) {
  		u32 old_map = bp->pps_req_map;
@@ -2561,7 +2571,7 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
 
  		/* if no state change, just return */
  		if ((!!old_map ^ !!bp->pps_req_map) == 0)
-			return 0;
+			goto out;
  	}
 
  	if (enable) {
@@ -2578,7 +2588,9 @@ ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
  			synchronize_irq(irq_vec);
  	}
 
-	return 0;
+out:
+	mutex_unlock(&bp->ts_lock);
+	return err;
  }
 
  static void
@@ -5613,6 +5625,7 @@ ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev)
 
  	bp->ptp_info = ptp_ocp_clock_info;
  	spin_lock_init(&bp->lock);
+	mutex_init(&bp->ts_lock);
 
  	for (i = 0; i < __PORT_COUNT; i++)
  		bp->port[i].uport = NULL;
@@ -5755,6 +5768,10 @@ ptp_ocp_detach(struct ptp_ocp *bp)
  	ptp_ocp_detach_sysfs(bp);
  	ptp_ocp_attr_group_del(bp);
  	timer_delete_sync(&bp->watchdog);
+	/* Drain in-flight enables and prevent rearming before unregister. */
+	mutex_lock(&bp->ts_lock);
+	bp->ts_stopping = true;
+	mutex_unlock(&bp->ts_lock);
  	/* Disable interrupts on all timestampers */
  	if (bp->ts0)
  		ptp_ocp_ts_enable(bp->ts0, 0, false);
@@ -5789,6 +5806,7 @@ ptp_ocp_detach(struct ptp_ocp *bp)
  	if (bp->n_irqs)
  		pci_free_irq_vectors(bp->pdev);
  	mutex_destroy(&bp->cpld_lock);
+	mutex_destroy(&bp->ts_lock);
  	device_unregister(&bp->dev);
  }
 
#3 lore raw

Re: [PATCH net 1/4] ptp: ocp: prevent timestamp rearming during detach

Quoted 10 lines · Vadim Fedorenko

drivers/ptp/ptp_ocp.c ptp_ocp_ts_enable(void *priv, u32 req, bool enable) · line 2562

Quoted 5 lines · Ahmad Byagowi

+ mutex_lock(&bp->ts_lock);
+ if (enable && bp->ts_stopping) {

drivers/ptp/ptp_ocp.c +3 -0
+         err = -ENODEV;
+         goto out;
+ }

ts_stopping is changed only in device detach callback, there is actually no need to cover whole ptp_ocp_ts_enable() with mutex, I believe existing spinlock can be used to serialize this access, no?

Yes, the existing bp->lock is sufficient. I have sent the four-patch v2 common-fix series to net:
https://lore.kernel.org/r/[email protected]

The revised timestamp fix uses spin_lock_irqsave() for the stopping
check, PPS subscription updates and MMIO. Detach sets ts_stopping under the same lock. Keeping the register writes in that critical section prevents an enable which
passed the check from writing after detach has disabled the channel.

The disable path still flushes the interrupt-mask write, but drops the spinlock and restores IRQ flags before both pci_irq_vector() and synchronize_irq(). The former takes the MSI descriptor mutex on this kernel; the latter may sleep. There is no new driver mutex or associated initialization/destruction.

One additional detail is the PPS no-state-change shortcut. Once the IRQ wait is outside the lock, another disable can clear pps_req_map and unlock before draining its IRQ. While stopping, the candidate bypasses that shortcut so detach still disables and drains the channel itself. Outside teardown, removing one of the PPS/EXTTS subscriptions still leaves the other subscription armed without unnecessary MMIO.

The source-derived ASan/UBSan harness passes late-enable, in-flight-enable, active-IRQ and in-flight-disable cases, shared PPS/EXTTS handling, and all six channels. It asserts that MMIO is locked and vector lookup and IRQ waits are unlocked; six deliberately broken variants are rejected. Both common and WiWi integration PTP objects compile for x86-64 with W=1 and KCFLAGS=-Werror.
This is software evidence, not kernel lockdep or physical unbind testing.

Thanks for the suggestion.

Ahmad

#4 lore raw

Re: [PATCH net 1/4] ptp: ocp: prevent timestamp rearming during detach

In reply to Ahmad Byagowi
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message for "ptp: ocp: prevent timestamp rearming during detach" has a broken trailer block and keeps workflow notes in…

--

ptp: ocp: prevent timestamp rearming during detach

This patch adds a ts_lock mutex and a ts_stopping flag to struct ptp_ocp, which serializes ptp_ocp_ts_enable() against ptp_ocp_detach(). After detach sets ts_stopping, later enable requests fail with -ENODEV. Disables still go through for the PTP core cleanup.

Quoted 13 lines

Reported by the netdev Sashiko AI review, then checked against the current PTP and POSIX-clock paths. A source-derived userspace harness forces an enable after detach's disable and reproduces the rearm with the old source. With this fix it rejects that enable, waits for an enable already in progress, and waits for a mocked active IRQ. Both timestamp and combined PPS/EXTTS paths pass with ASan/UBSan. This validates ordering with stubbed MMIO/IRQ operations, not an in-kernel use-after-free reproduction. No physical card was tested.

Codex prepared the fix, tests and changelog from the review and upstream source, at the request to separate common-driver fixes from WiWi support and validate removal ordering.

Quoted 5 lines
Fixes 627366c51145 ("ptp: ocp: fix resource freeing order")

[Severity: Low]
This isn't a bug, but blank lines separate the Fixes:, Link: and
Signed-off-by: lines from each other.

Tools such as git interpret-trailers, b4 and patchwork only treat the last paragraph of the message as the trailer block. As written, is
Signed-off-by: the only trailer they will find, with Fixes: and Link:
missed?

Could these three lines go into one trailer block with no blank lines?

The report credit is written as prose ("Reported by the netdev Sashiko AI review"). Would a Reported-by: tag work better, with the lore URL as a
Closes: or Link: trailer?

Three parts of the body describe how the patch was produced, not what the change does:

- the userspace harness and ASan/UBSan paragraph
- the "No physical card was tested" note
- the Codex/WiWi paragraph

Could these move below the --- line so they stay out of git history? If the tool assistance should be recorded, an Assisted-by: style trailer may fit better than a paragraph in the body.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118-timecard-common-301f823ddd-0-ahmadexp%40gmail.com
#5 lore raw

Re: [PATCH net 1/4] ptp: ocp: prevent timestamp rearming during detach

Quoted 9 lines · On Sun, 22 Nov 2026 at 20:41:06 +0000, [email protected] wrote:

This isn't a bug, but blank lines separate the Fixes:, Link: and
Signed-off-by: lines from each other.

Tools such as git interpret-trailers, b4 and patchwork only treat the last paragraph of the message as the trailer block. As written, is
Signed-off-by: the only trailer they will find, with Fixes: and Link:
missed?

Could these three lines go into one trailer block with no blank lines?

Agreed. The trailer block was made contiguous in v3 and remains so in the v4 draft. Any new personal sign-off will be appended to that same block after review, not separated by a blank paragraph.

Quoted 3 lines

Could these move below the --- line so they stay out of git history? If the tool assistance should be recorded, an Assisted-by: style trailer may fit better than a paragraph in the body.

I moved detailed testing and revision notes below the separator and removed the repeated cross-patch workflow descriptions. Each commit retains a brief, patch-specific validation statement, including the lack of physical-card testing, as required by maintainer-netdev.rst. A short provenance statement also remains as required by generated-content.rst. I have not invented a new trailer or added a reporting tag not supplied.

Also, this report is on v1's mutex/stopping-flag approach. That approach was replaced following Vadim's review: final channel shutdown is handled by the PTP core after interface removal. The latest revision uses explicit timestamp-channel requests, leaving the pin helper only in the pin-configuration path.

The five-patch v4 series is posted separately to net:
https://lore.kernel.org/r/[email protected]

Ahmad

#6 lore raw

PATCH 2/4 ptp: ocp: use hardware selectors in DPLL frequency operations

In reply to Ahmad Byagowi

The selector table index is not the hardware selector value. Passing the index to the SMA setter can select unrelated signals, or fail on ART. Use the table value, as the sysfs path already does.

Mask the direction enable bit before the frequency lookup. Otherwise an enabled, direction-configurable pin has no matching table entry and its DPLL get or dump fails. Apply both corrections to all board variants.

Reported by the netdev Sashiko AI review and confirmed by code inspection. Source-derived C regression tests pass with ASan/UBSan; restoring the faulty selector, mask, disabled-state or DCF value makes the corresponding regression fail. Tests use stubbed MMIO, not hardware. The driver object builds on current net with W=1 and KCFLAGS=-Werror. No physical card was tested.

Codex prepared the fix, tests and changelog from the review and upstream source, at the request to separate common-driver fixes from WiWi support and validate removal ordering.

Fixes 09eeb3aecc6c ("ptp_ocp: implement DPLL ops")
---
 drivers/ptp/ptp_ocp.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/ptp/ptp_ocp.c +3 -1
index 478f7012b2..d449ed8ca2 100644
@@ -5900,7 +5900,8 @@ static int ptp_ocp_dpll_frequency_set(const struct dpll_pin *pin,
 	tbl = bp->sma_op->tbl[sma->mode];
 	for (i = 0; tbl[i].name; i++)
 		if (tbl[i].frequency == frequency)
-			return ptp_ocp_sma_store_val(bp, i, sma->mode, sma_nr + 1);
+			return ptp_ocp_sma_store_val(bp, tbl[i].value,
+						     sma->mode, sma_nr + 1);
 	return -EINVAL;
 }
 
@@ -5918,6 +5919,7 @@ static int ptp_ocp_dpll_frequency_get(const struct dpll_pin *pin,
 	int i;
 
 	val = bp->sma_op->get(bp, sma_nr + 1);
+	val &= SMA_SELECT_MASK;
 	tbl = bp->sma_op->tbl[sma->mode];
 	for (i = 0; tbl[i].name; i++)
 		if (val == tbl[i].value) {
Signature · 1 line
2.54.0 (Apple Git-157)
#7 lore raw

Re: [PATCH net 2/4] ptp: ocp: use hardware selectors in DPLL frequency operations

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] ptp_ocp_dpll_frequency_get() (drivers/ptp/ptp_ocp.c) still fails with -EINVAL for an enabled, direction-configurable SMA input that has…
- [Medium] The patch says index passing makes DPLL frequency_set "fail on ART" and fixes that by choosing the table entry by frequency and…

--

ptp: ocp: use hardware selectors in DPLL frequency operations

This commit changes ptp_ocp_dpll_frequency_set() to pass the selector table value instead of the table index to ptp_ocp_sma_store_val(). It also masks the direction enable bit in ptp_ocp_dpll_frequency_get() before the table lookup, so enabled pins whose direction can be changed find a matching entry.

drivers/ptp/ptp_ocp.c static int ptp_ocp_dpll_frequency_set(const struct dpll_pin *pin, · line 5904

Quoted 11 lines
drivers/ptp/ptp_ocp.c +2 -1
index 478f7012b23e2..d449ed8ca2d8a 100644
@@ -5900,7 +5900,8 @@ static int ptp_ocp_dpll_frequency_set(const struct dpll_pin *pin,
 	tbl = bp->sma_op->tbl[sma->mode];
 	for (i = 0; tbl[i].name; i++)
 		if (tbl[i].frequency == frequency)
-			return ptp_ocp_sma_store_val(bp, i, sma->mode, sma_nr + 1);
+			return ptp_ocp_sma_store_val(bp, tbl[i].value,
+						     sma->mode, sma_nr + 1);

[Severity: Medium]
The commit message says passing the index can "fail on ART". Can a 10 MHz request on an ART input reach this line?

The ART input table lists the 10Mhz entry at 1 MHz:

static const struct ocp_selector ptp_ocp_art_sma_in[] = {
	...
	{ .name = "10Mhz",	.value = 0x0008,  .frequency = 1000000 },

ART pins advertise ptp_ocp_sma_freq (1PPS, 10MHz, IRIG-B, DCF77). So dpll_pin_freq_set() accepts 10000000 and rejects 1000000.

A PIN_SET of 10000000 on an ART input passes the core check. It then matches no entry in this loop and returns -EINVAL, so the corrected tbl[i].value is never passed on. With this patch, it looks like PPS1 is the only input function that can be programmed on ART through DPLL.

The same entry also affects ptp_ocp_dpll_frequency_get(). By default ART SMA1 selects 0x08:

ptp_ocp_art_sma_init() {
	...
	bp->sma[0].default_fcn = 0x08;	/* IN: 10Mhz */

For that pin, the getter reports 1000000 Hz, which is not in the advertised frequency list.

The table entry predates this patch, but the ART fix depends on tbl[i].frequency being correct. The later patch "ptp: ocp: correct the DCF output frequency" fixes a similar table mismatch for DCF but leaves this entry unchanged.

Should ptp_ocp_art_sma_in[] list the 10Mhz input as 10000000 as part of this fix?

drivers/ptp/ptp_ocp.c static int ptp_ocp_dpll_frequency_get(const struct dpll_pin *pin, · line 5925

Quoted 11 lines
drivers/ptp/ptp_ocp.c +1 -0
 	return -EINVAL;
 }
 
@@ -5918,6 +5919,7 @@ static int ptp_ocp_dpll_frequency_get(const struct dpll_pin *pin,
 	int i;
 
 	val = bp->sma_op->get(bp, sma_nr + 1);
+	val &= SMA_SELECT_MASK;
 	tbl = bp->sma_op->tbl[sma->mode];
 	for (i = 0; tbl[i].name; i++)
 		if (val == tbl[i].value) {

[Severity: High]
Does this still return -EINVAL for an enabled input that has more than one destination?

The commit message says that without the mask "an enabled, direction-configurable pin has no matching table entry and its DPLL get or dump fails". The sysfs ABI allows several input destinations, and sma_parse_inputs() ORs them into one selector:

sma_parse_inputs() {
	...
	ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]);

For example, writing "IN: TS1 TS2" to smaN gives 0x000C. After ptp_ocp_sma_store_val() adds SMA_ENABLE, 0x800C is written to the GPIO map.

On readback, sma_op->get() returns 0x800C, and the new mask turns it into 0x000C. Each entry in ptp_ocp_sma_in[], ptp_ocp_adva_sma_in[] and ptp_ocp_adva_x1_sma_in[] has a single bit set. Nothing matches val == tbl[i].value, so the function returns -EINVAL.

The sysfs reader for the same register treats it as a bitmask:

ptp_ocp_show_inputs() {
	...
		if (val & tbl[i].value) {

The -EINVAL reaches the DPLL netlink code:

dpll_nl_pin_get_dumpit()
dpll_cmd_pin_get_one()

    dpll_msg_add_pin_freq()
      ptp_ocp_dpll_frequency_get()  -> -EINVAL

dpll_nl_pin_get_dumpit() only skips -ENODEV and -EMSGSIZE, so this error stops the global pin dump. Pins after this one, including pins from other DPLL drivers, are not reported.

dpll_pin_freq_set() also calls frequency_get first and returns early with "unable to get old frequency value". As a result, the pin cannot be recovered through DPLL frequency_set.

The later patch "ptp: ocp: report disabled SMA inputs consistently over DPLL" only adds the disabled-input override, so this remains at the end of the series.

Should this lookup match selector bits the way ptp_ocp_show_inputs() does, instead of requiring an exact match?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118-timecard-common-301f823ddd-0-ahmadexp%40gmail.com
#8 lore raw

Re: [PATCH net 2/4] ptp: ocp: use hardware selectors in DPLL frequency operations

Quoted 8 lines · On Sun, 22 Nov 2026 at 20:41:07 +0000, [email protected] wrote:

Does this still return -EINVAL for an enabled input that has more than one destination?

The commit message says that without the mask "an enabled, direction-configurable pin has no matching table entry and its DPLL get or dump fails". The sysfs ABI allows several input destinations, and sma_parse_inputs() ORs them into one selector:

Quoted 7 lines
sma_parse_inputs() {
	...
	ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]);

For example, writing "IN: TS1 TS2" to smaN gives 0x000C. After ptp_ocp_sma_store_val() adds SMA_ENABLE, 0x800C is written to the GPIO map.

Yes. I reproduced the error against the previous candidate. Masking the enable bit alone did not fix combined destinations, and the DPLL core's get-before-set path could still prevent recovery.

The v4 getter handles input combinations after the exact-match lookup. Frequency-bearing destinations must agree to produce a known frequency. Timestamp/counter destinations do not constrain it. A route with no known frequency, conflicting frequencies or unknown bits returns zero, as an individual timestamp route already does. This is a routing interpretation, not a measurement of the input signal. Output selectors still require an exact match.

The source-derived tests cover 4,669 masks across FB, ADVA and ADVA X1, including TS1|TS2, timestamp-plus-PPS, conflicting destinations and unknown bits. Recovery exercises the actual DPLL core get-before-set function. The previous getter and a variant that chooses the first conflicting frequency both fail runtime assertions.

Quoted 11 lines

The commit message says passing the index can "fail on ART". Can a 10 MHz request on an ART input reach this line?

The ART input table lists the 10Mhz entry at 1 MHz:

static const struct ocp_selector ptp_ocp_art_sma_in[] = {
	...
	{ .name = "10Mhz",	.value = 0x0008,  .frequency = 1000000 },

ART pins advertise ptp_ocp_sma_freq (1PPS, 10MHz, IRIG-B, DCF77). So dpll_pin_freq_set() accepts 10000000 and rejects 1000000.

Agreed. The selector fix was necessary but did not fix that separate table error. I removed the misleading implication from patch 2 and added a separate patch 5 changing the ART input frequency to 10000000. The tests use the actual ART init/set/get functions and DPLL core
validation: 10000000 succeeds, 1000000 is rejected, and the fixed 0x08 default reads as 10000000. The old source fails those assertions.

Both issues are addressed in the v4 draft. Patch 2's expanded behavior needs fresh review, so its earlier Reviewed-by is not retained. All testing here is software-only with stubbed hardware operations.

The five-patch v4 series is posted separately to net:
https://lore.kernel.org/r/[email protected]

Ahmad

pw-bot: cr

#9 lore raw

PATCH 3/4 ptp: ocp: report disabled SMA inputs consistently over DPLL

In reply to Ahmad Byagowi

Disabling an SMA input clears its mux field. Zero is also the selector for 10 MHz, so a DPLL read reports 10 MHz while sysfs reports None. The DPLL core then treats a request to enable 10 MHz as unchanged and skips the setter, leaving the input disabled.

Substitute SMA_DISABLE for disabled inputs before looking up the frequency, as the sysfs path already does. The None entry reports zero Hz and a subsequent 10 MHz request reaches the setter.

Reported by the netdev Sashiko AI review and confirmed by code inspection. Source-derived C regression tests pass with ASan/UBSan; restoring the faulty selector, mask, disabled-state or DCF value makes the corresponding regression fail. Tests use stubbed MMIO, not hardware. The driver object builds on current net with W=1 and KCFLAGS=-Werror. No physical card was tested.

Codex prepared the fix, tests and changelog from the review and upstream source, at the request to separate common-driver fixes from WiWi support and validate removal ordering.

Fixes 09eeb3aecc6c ("ptp_ocp: implement DPLL ops")
---
 drivers/ptp/ptp_ocp.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/ptp/ptp_ocp.c +2 -0
index d449ed8ca2..650047581f 100644
@@ -5920,6 +5920,8 @@ static int ptp_ocp_dpll_frequency_get(const struct dpll_pin *pin,
 
 	val = bp->sma_op->get(bp, sma_nr + 1);
 	val &= SMA_SELECT_MASK;
+	if (sma->mode == SMA_MODE_IN && sma->disabled)
+		val = SMA_DISABLE;
 	tbl = bp->sma_op->tbl[sma->mode];
 	for (i = 0; tbl[i].name; i++)
 		if (val == tbl[i].value) {
Signature · 1 line
2.54.0 (Apple Git-157)
#10 lore raw

Re: [PATCH net 3/4] ptp: ocp: report disabled SMA inputs consistently over DPLL

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 2

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The fix decides whether an input is disabled only from the software flag sma->disabled.
- [Low] The commit message has a testing/provenance paragraph that does not describe this patch.
- [Low] The commit message only describes the 10 MHz case: a disabled input reads back as 10 MHz and the DPLL core skips the setter.

--

ptp: ocp: report disabled SMA inputs consistently over DPLL

This patch makes ptp_ocp_dpll_frequency_get() use SMA_DISABLE for SMA inputs flagged as disabled before it looks up the selector table. Before this, a disabled input read back as the 10 MHz selector. The DPLL core then skipped a later 10 MHz request as unchanged.

Quoted 4 lines

Disabling an SMA input clears its mux field. Zero is also the selector for 10 MHz, so a DPLL read reports 10 MHz while sysfs reports None. The DPLL core then treats a request to enable 10 MHz as unchanged and skips the setter, leaving the input disabled.

[Severity: Low]
This covers the 10 MHz case. Was the old behaviour worse on adva_x1 boards? ptp_ocp_adva_x1_sma_in[] has no entry with value 0x0000:

static const struct ocp_selector ptp_ocp_adva_x1_sma_in[] = {
	{ .name = "PPS1",	.value = 0x0001,      .frequency = 1 },
	...
	{ .name = "None",	.value = SMA_DISABLE, .frequency = 0 },
	{ }
};

So after ptp_ocp_sma_store_val() wrote 0 for a disabled input, ptp_ocp_dpll_frequency_get() found no match and returned -EINVAL.

dpll_msg_add_pin_freq() passed that error up, so DPLL_CMD_PIN_GET failed for the pin. dpll_nl_pin_get_dumpit() skips only -ENODEV, so the pin dump also stopped at that pin. dpll_pin_freq_set() failed with "unable to get old frequency value", so the input could not be re-enabled over DPLL.

This patch fixes that as well, because SMA_DISABLE matches the x1 "None" entry. Should the changelog mention this case for stable backporters?

Quoted 4 lines

Reported by the netdev Sashiko AI review and confirmed by code inspection. Source-derived C regression tests pass with ASan/UBSan; restoring the faulty selector, mask, disabled-state or DCF value makes the corresponding regression fail. Tests use stubbed MMIO, not hardware.

[ ... ]

Quoted 3 lines

Codex prepared the fix, tests and changelog from the review and upstream source, at the request to separate common-driver fixes from WiWi support and validate removal ordering.

[Severity: Low]
Does this paragraph describe this patch? The same text appears word for word in all four patches of the series. This patch only adds the sma->disabled check in ptp_ocp_dpll_frequency_get().

The selector and SMA_SELECT_MASK changes are in "ptp: ocp: use hardware selectors in DPLL frequency operations". The DCF value is in "ptp: ocp:
correct the DCF output frequency". The removal ordering work is in "ptp:
ocp: prevent timestamp rearming during detach".

Could the paragraph be cut down to the testing that applies to this patch? Someone backporting only this commit could otherwise think it covers those other defects too.

drivers/ptp/ptp_ocp.c static int ptp_ocp_dpll_frequency_get(const struct dpll_pin *pin, · line 5924

Quoted 10 lines
drivers/ptp/ptp_ocp.c +2 -0
index d449ed8ca2d8a..650047581fd41 100644
@@ -5920,6 +5920,8 @@ static int ptp_ocp_dpll_frequency_get(const struct dpll_pin *pin,
 
 	val = bp->sma_op->get(bp, sma_nr + 1);
 	val &= SMA_SELECT_MASK;
+	if (sma->mode == SMA_MODE_IN && sma->disabled)
+		val = SMA_DISABLE;

[Severity: Medium]
Can this check give the wrong answer after the driver is unbound and rebound, or after a module reload? sma->disabled exists only in software, and ptp_ocp_sma_store_val() is the only code that sets it. The FPGA keeps the mux values that the previous driver instance wrote.

ptp_ocp_sma_fb_init() reads the mux registers only to work out the direction. It never sets sma->disabled:

	reg = ioread32(&bp->sma_map1->gpio1);
	bp->sma[0].mode = reg & BIT(15) ? SMA_MODE_IN : SMA_MODE_OUT;
	bp->sma[1].mode = reg & BIT(31) ? SMA_MODE_IN : SMA_MODE_OUT;

	reg = ioread32(&bp->sma_map1->gpio2);
	bp->sma[2].mode = reg & BIT(15) ? SMA_MODE_OUT : SMA_MODE_IN;
	bp->sma[3].mode = reg & BIT(31) ? SMA_MODE_OUT : SMA_MODE_IN;

Disabling a direction-changeable input writes 0 to its input field, which also clears SMA_ENABLE. In this function, val &= SMA_SELECT_MASK drops the enable bit before sma->disabled is checked.

Take SMA3/SMA4 after a rebind: mode comes back as SMA_MODE_IN and disabled is false. The input field reads 0, which matches "10Mhz". A 10 MHz request then hits this check in dpll_pin_freq_set():

	if (freq == old_freq)
		return 0;

So it never reaches ptp_ocp_dpll_frequency_set(). Isn't that the case this patch sets out to fix?

For SMA1/SMA2, BIT(15) or BIT(31) in gpio1 is clear for a disabled input. The pin therefore comes back as SMA_MODE_OUT, and DPLL reports its output field instead.

Would it help to set sma->disabled in ptp_ocp_sma_fb_init() from the enable bits, or to test SMA_ENABLE before masking?

All of this assumes the FPGA treats mux 0 without SMA_ENABLE as disabled rather than as the 10 MHz selection, which I could not confirm.

Quoted 3 lines
drivers/ptp/ptp_ocp.c +0 -0
 	tbl = bp->sma_op->tbl[sma->mode];
 	for (i = 0; tbl[i].name; i++)
 		if (val == tbl[i].value) {
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118-timecard-common-301f823ddd-0-ahmadexp%40gmail.com
#11 lore raw

Re: [PATCH net 3/4] ptp: ocp: report disabled SMA inputs consistently over DPLL

Quoted 7 lines · On Sun, 22 Nov 2026 at 20:41:09 +0000, [email protected] wrote:

Can this check give the wrong answer after the driver is unbound and rebound, or after a module reload? sma->disabled exists only in software, and ptp_ocp_sma_store_val() is the only code that sets it. The FPGA keeps the mux values that the previous driver instance wrote.

ptp_ocp_sma_fb_init() reads the mux registers only to work out the direction. It never sets sma->disabled:

Yes. I reproduced the stale software state using retained mux values and a fresh driver instance. The v4 patch initializes disabled state from the enable bits for direction-configurable FB-style connectors. The existing direction selection picks an enabled side if either side is enabled. If that selected side is also disabled, both buffers are off, and the connector is represented as IN: None. The hardware does not retain the previous direction in that disconnected state.

Initialization does not write the mux. Fixed-direction firmware, which does not provide these enable bits, keeps its existing interpretation. The ART initialization path is also unchanged.

Quoted 5 lines

Would it help to set sma->disabled in ptp_ocp_sma_fb_init() from the enable bits, or to test SMA_ENABLE before masking?

All of this assumes the FPGA treats mux 0 without SMA_ENABLE as disabled rather than as the 10 MHz selection, which I could not confirm.

The public SMA-selector RTL assigns bit 15 to the input/output enable signals. The production top level uses those signals to control the external input/output buffer enables. Thus a zero selector without its enable bit does not enable the external 10 MHz input buffer.

Pinned sources:
https://github.com/Time-Appliances-Project/Time-Card/blob/1042bd6f9ddb1844c8b471efbf560c96c8e6cecc/FPGA/Open-Source/Ips/SmaSelector/SmaSelector.vhd#L293-L301
https://github.com/Time-Appliances-Project/Time-Card/blob/1042bd6f9ddb1844c8b471efbf560c96c8e6cecc/FPGA/Open-Source/Implementation/Xilinx/TimeCard_Production/Top/TimeCardTop.vhd#L473-L481

The software tests cover 243 configurations across FB/ADVA/ADVA X1 and all four connectors, including enabled inputs, outputs and disconnected states. They assert zero initialization writes and unchanged mux data, then exercise re-enabling through the actual DPLL core get-before-set function. The previous implementation fails the rebind assertion. This is source/RTL validation, not a physical-card rebind test.

Quoted 2 lines

This patch fixes that as well, because SMA_DISABLE matches the x1 "None" entry. Should the changelog mention this case for stable backporters?

Yes. The changelog now explains that ADVA X1 has no zero selector, so the original disabled-input failure also blocked DPLL reads and later frequency changes, rather than merely reporting a false 10 MHz.

Quoted 3 lines

Could the paragraph be cut down to the testing that applies to this patch? Someone backporting only this commit could otherwise think it covers those other defects too.

Agreed. I removed the repeated cross-patch testing claims, kept the commit explanation specific to disabled inputs, and placed this patch's test details below the separator. Since initialization behavior changed, I have not carried forward the earlier Reviewed-by for this patch.

The five-patch v4 series is posted separately to net:
https://lore.kernel.org/r/[email protected]

Ahmad

#12 lore raw

PATCH 4/4 ptp: ocp: correct the DCF output frequency

In reply to Ahmad Byagowi

The output selector table uses 77000 Hz for DCF, whereas the input table and the DPLL supported frequency list use 77500 Hz. A request for the advertised DCF77 frequency consequently fails on an output, and reading that output reports a frequency outside its advertised set.

Use 77500 Hz for the DCF output selector as well.

Reported by the netdev Sashiko AI review and confirmed by code inspection. Source-derived C regression tests pass with ASan/UBSan; restoring the faulty selector, mask, disabled-state or DCF value makes the corresponding regression fail. Tests use stubbed MMIO, not hardware. The driver object builds on current net with W=1 and KCFLAGS=-Werror. No physical card was tested.

Codex prepared the fix, tests and changelog from the review and upstream source, at the request to separate common-driver fixes from WiWi support and validate removal ordering.

Fixes 09eeb3aecc6c ("ptp_ocp: implement DPLL ops")
---
 drivers/ptp/ptp_ocp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/ptp/ptp_ocp.c +1 -1
index 650047581f..aa950cae68 100644
@@ -1376,7 +1376,7 @@ static const struct ocp_selector ptp_ocp_sma_out[] = {
 	{ .name = "GNSS1",	.value = 0x0004,  .frequency = 1 },
 	{ .name = "GNSS2",	.value = 0x0008,  .frequency = 1 },
 	{ .name = "IRIG",	.value = 0x0010,  .frequency = 10000 },
-	{ .name = "DCF",	.value = 0x0020,  .frequency = 77000 },
+	{ .name = "DCF",	.value = 0x0020,  .frequency = 77500 },
 	{ .name = "GEN1",	.value = 0x0040 },
 	{ .name = "GEN2",	.value = 0x0080 },
 	{ .name = "GEN3",	.value = 0x0100 },
Signature · 1 line
2.54.0 (Apple Git-157)
#13 lore raw

Re: [PATCH net 4/4] ptp: ocp: correct the DCF output frequency

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The patch fixes one bad entry in a selector table from commit 09eeb3aecc6c, the one named in its Fixes: tag.

--

ptp: ocp: correct the DCF output frequency

The patch changes the DCF entry in the ptp_ocp_sma_out[] selector table from 77000 Hz to 77500 Hz. That brings it in line with the input table and the DPLL supported frequency list, so the advertised DCF77 frequency can be set and read back on an output pin.

drivers/ptp/ptp_ocp.c static const struct ocp_selector ptp_ocp_sma_out[] = { · line 1379

Quoted 10 lines
drivers/ptp/ptp_ocp.c +1 -1
index 650047581fd41..aa950cae68f46 100644
@@ -1376,7 +1376,7 @@ static const struct ocp_selector ptp_ocp_sma_out[] = {
 	{ .name = "GNSS1",	.value = 0x0004,  .frequency = 1 },
 	{ .name = "GNSS2",	.value = 0x0008,  .frequency = 1 },
 	{ .name = "IRIG",	.value = 0x0010,  .frequency = 10000 },
-	{ .name = "DCF",	.value = 0x0020,  .frequency = 77000 },
+	{ .name = "DCF",	.value = 0x0020,  .frequency = 77500 },

[Severity: Medium]
Should the ART input table get the same kind of fix? Commit 09eeb3aecc6c, the one in the Fixes: tag, also added this entry to ptp_ocp_art_sma_in[]:

drivers/ptp/ptp_ocp.c:

static const struct ocp_selector ptp_ocp_art_sma_in[] = {
	{ .name = "PPS1",	.value = 0x0001,  .frequency = 1 },
	{ .name = "10Mhz",	.value = 0x0008,  .frequency = 1000000 },
	{ }
};

The entry is named "10Mhz" but has a frequency of 1 MHz. The "10Mhz" entry in ptp_ocp_art_sma_out[] uses 10000000.

ptp_ocp_art_sma_init() registers the ART pins with ptp_ocp_sma_freq as their supported list:

.freq_supported_num = ARRAY_SIZE(ptp_ocp_sma_freq), .freq_supported = ptp_ocp_sma_freq,

That list contains DPLL_PIN_FREQUENCY_10MHZ but not 1 MHz.

This seems to give the same two symptoms the commit message describes for DCF, but on ART input pins.

On set, a DPLL netlink request for 10000000 passes dpll_pin_is_freq_supported() in the core. It then reaches the exact-match loop in ptp_ocp_dpll_frequency_set():

	for (i = 0; tbl[i].name; i++)
		if (tbl[i].frequency == frequency)
			return ptp_ocp_sma_store_val(bp, tbl[i].value,
						     sma->mode, sma_nr + 1);
	return -EINVAL;

Nothing in the table matches, so the call returns -EINVAL.

On get, ptp_ocp_dpll_frequency_get() reports 1000000 for an ART input that selects 0x08. That value is not in the pin's advertised set. Fixed pins hit this too, because sma[0].default_fcn is 0x08 ("IN: 10Mhz").

Would it make sense to change that entry to 10000000 in this patch, or in a follow-up?

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118-timecard-common-301f823ddd-0-ahmadexp%40gmail.com
#14 lore raw

Re: [PATCH net 4/4] ptp: ocp: correct the DCF output frequency

Quoted 4 lines · On Sun, 22 Nov 2026 at 20:41:10 +0000, [email protected] wrote:

Should the ART input table get the same kind of fix? Commit 09eeb3aecc6c, the one in the Fixes: tag, also added this entry to ptp_ocp_art_sma_in[]:

drivers/ptp/ptp_ocp.c:

Quoted 8 lines
static const struct ocp_selector ptp_ocp_art_sma_in[] = {
	{ .name = "PPS1",	.value = 0x0001,  .frequency = 1 },
	{ .name = "10Mhz",	.value = 0x0008,  .frequency = 1000000 },
	{ }
};

The entry is named "10Mhz" but has a frequency of 1 MHz. The "10Mhz" entry in ptp_ocp_art_sma_out[] uses 10000000.

Yes. The v4 series adds a separate fifth patch correcting that ART input entry to 10000000. Keeping it separate preserves the narrow DCF correction and its existing review tag.

Quoted 6 lines

On get, ptp_ocp_dpll_frequency_get() reports 1000000 for an ART input that selects 0x08. That value is not in the pin's advertised set. Fixed pins hit this too, because sma[0].default_fcn is 0x08 ("IN: 10Mhz").

Would it make sense to change that entry to 10000000 in this patch, or in a follow-up?

The new regression uses actual ART init/set/get functions and the DPLL core's supported-frequency and get-before-set functions. It checks a literal 10000000 Hz request, rejects 1000000, and verifies the fixed 0x08 default. The old source fails; the corrected source passes. These tests use stubbed hardware operations, not an ART card.

The five-patch v4 series is posted separately to net:
https://lore.kernel.org/r/[email protected]

Ahmad