Skip to contentSkip to conversation

ptp: ocp: add TAP CPLD support for ADVA TimeCard X1

PATCH Superseded Sagi Maimon 3 participants 9 messages Sep 8 – Sep 14

Subsystems (6)

v13 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#0 lore raw

v13 cover ptp: ocp: add TAP CPLD support for ADVA TimeCard X1

The ADVA TimeCard X1 (PCI device 0x0410) carries a Lattice MachXO3 CPLD on the TMC I2C bus. This series adds access to it and an update path through the kernel firmware-upload subsystem.

The card has two I2C controllers. Linux registers only the one at 0x00150000, normally routed to the config EEPROMs; the one at 0x00120000 belongs to the on-card MicroBlaze and is routed to the TMC or M.2 bus as its firmware selects. A request/grant handshake re-routes the host controller onto the TMC bus, where the PCA9548 and the CPLD behind its channel 0 live. Everything here is built on that handshake.

Patch 1 adds the arbitration and the read-only interfaces on top of it. Patch 2 adds the flashing.

v12: https://lore.kernel.org/netdev/[email protected]/T/#u

Changes since v12 - all from the automated review of v12:

- The CPLD device ID is no longer read from ptp_ocp_devlink_info_get(). DEVLINK_CMD_INFO_GET carries no GENL_ADMIN_PERM, and that read claims the i2c adapter and drives the on-card mux for up to two seconds, which any user could have repeated to keep the EEPROM, nvmem and at24 paths off the controller. The same operation is root-only through cpld_status. The driver now reads the ID once from its own worker, as soon as the i2c adapter has registered, and info_get() reports the cached value. Patch 1.
- As a result info_get() can no longer fail because of the CPLD: it skips cpld.id when the value is not available. Returning an error there made devlink discard the whole reply, and -ENODEV was reachable on any card where the i2c adapter never bound, which also aborted 'devlink dev info' for every other instance on the host. Patch 1.
- cpld.id is now reported before the EEPROM data rather than after its early return, so it no longer depends on the EEPROM having been read. Patch 1.
- adva_x1_bus_claim() re-checks that the adapter it just took a reference to is still this card's: i2c_del_adapter() frees the idr slot before the bus notifier clears the cached number, so the number could already have been recycled. Patch 1.
- The probe error paths unregister devlink before ptp_ocp_detach(), which destroys cpld_lock and releases the i2c controller. Patch 1.
- I2C_M_DMA_SAFE is no longer set. No adapter that can bind here does DMA, and the read half of the scratch buffer is an interior pointer that does not satisfy the flag's alignment contract; the core is left free to bounce. Patch 1.
- The firmware-upload callbacks map errnos onto the documented fw_upload_err categories: -ENODEV and a failed transfer are no longer reported as a timeout, and a CPLD FAILED status is no longer reported as one either. Patch 2.
- adva_cpld_prepare() checks for a cancellation on entry, as its own comment promised, instead of cycling the CPLD into configuration mode for an upload that was already aborted. Patch 2.
- Added Documentation/networking/devlink/ptp_ocp.rst describing the driver-specific cpld.id version, with the index and MAINTAINERS entries. Patch 1.
- Comments in patch 1 no longer describe the firmware-upload path that only patch 2 adds, and the lines that exceeded 80 columns are wrapped.

Three points from that review are deliberately not addressed here, because changing them needs a full programming cycle on the card to validate and I would rather not fold untested changes into a respin:

- poll_complete() samples DONE before REFRESH only, and does not check the sysCONFIG error field the way drivers/fpga/lattice-sysconfig.c does.
- adva_x1_mblaze_release() does not wait for the firmware to route the controller back before the adapter lock is dropped.
- A cancel arriving between cleanup() and fw_upload_prog_complete() stays set and aborts the next upload.

Tested on an ADVA TimeCard X1: the driver loads with the i2c adapter registering after probe, cpld.id and cpld_status read back correctly, and the wire byte sequences are unchanged from v12, which programmed the part successfully.

Sagi Maimon (2):
ptp: ocp: add TAP CPLD access for ADVA TimeCard X1
ptp: ocp: add TAP CPLD firmware upload for ADVA TimeCard X1

 Documentation/ABI/testing/sysfs-timecard     |  24 +
 Documentation/networking/devlink/index.rst   |   1 +
 Documentation/networking/devlink/ptp_ocp.rst |  35 +
 MAINTAINERS                                  |   1 +
 drivers/ptp/Kconfig                          |   2 +
 drivers/ptp/ptp_ocp.c                        | 761 ++++++++++++++++++-
 6 files changed, 818 insertions(+), 6 deletions(-)
 create mode 100644 Documentation/networking/devlink/ptp_ocp.rst

base-commit: c29b37ed7a4d9856ed758a82282456d69cee2ed1

Signature · 1 line
2.47.0
#1 lore raw

PATCH v13 1/2 ptp: ocp: add TAP CPLD access for ADVA TimeCard X1

The ADVA TimeCard X1 (PCI device 0x0410) carries a Lattice MachXO3 CPLD on the TMC I2C bus. The card has two I2C controllers: Linux registers only the one at 0x00150000, normally routed to the config EEPROMs, while the one at 0x00120000 belongs to the on-card MicroBlaze and is routed to either the TMC or the M.2 bus as its firmware selects.

A request/grant handshake re-routes the host controller from the EEPROM segment onto the TMC bus, leaving the MicroBlaze with M.2 only. The PCA9548 at 0x74 and the CPLD at 0x40 behind its channel 0 are reachable from the host only while that grant is held.

Add the arbitration and the read-only interfaces built on it. Because the handshake changes what the host adapter is wired to, an operation takes the i2c core adapter lock for the whole grant window and uses __i2c_transfer() internally; without that, a concurrent transfer from ptp_ocp_read_eeprom(), from the nvmem attributes or from the at24 sysfs files would be issued onto the TMC bus instead of to the EEPROM.

The Lattice device ID is a fixed property of the part, so report it as the fixed devlink version "cpld.id" rather than as a sysfs attribute, and cache it so the arbitration is paid once per card.

Add one X1-only attribute:

/sys/class/timecard/ocpN/cpld_status
report the CPLD status register, including the DONE, BUSY and FAILED indicators

A read arbitrates for the shared bus and reprograms the on-card mux, so it is root-only and takes cpld_lock interruptibly.

---
 Documentation/ABI/testing/sysfs-timecard     |  20 +
 Documentation/networking/devlink/index.rst   |   1 +
 Documentation/networking/devlink/ptp_ocp.rst |  35 ++
 MAINTAINERS                                  |   1 +
 drivers/ptp/ptp_ocp.c                        | 388 ++++++++++++++++++-
 5 files changed, 439 insertions(+), 6 deletions(-)
 create mode 100644 Documentation/networking/devlink/ptp_ocp.rst
Documentation/ABI/testing/sysfs-timecard +20 -0
index 3ae41b7634ac..70ecc15b347f 100644
@@ -11,6 +11,26 @@ Contact:	Jonathan Lemon <[email protected]>
 Description:	This directory contains the attributes of the Nth timecard
 		registered.
 
+What:		/sys/class/timecard/ocpN/cpld_status
+Date:		July 2026
+Contact:	Sagi Maimon <[email protected]>
+Description:	(RO, root only) The status register of the TAP CPLD, in
+		human-readable form:
+
+		  done=<0|1> busy=<0|1> failed=<0|1>
+
+		Only present on ADVA x1 TAP boards (PCI ID 0xad5a:0x0410).
+
+		done=1 indicates the configuration flash was successfully
+		programmed and is active.  busy=1 means an internal operation
+		is in progress.  failed=1 means the last ISC operation failed.
+
+		A read arbitrates for the shared I2C bus and reprograms the
+		on-card mux, so it is restricted to root.  The Lattice device
+		ID of the CPLD is read once at probe and reported as the
+		fixed "cpld.id" version by devlink dev info.
+
+
 What:		/sys/class/timecard/ocpN/available_clock_sources
 Date:		September 2021
 Contact:	Jonathan Lemon <[email protected]>
Documentation/networking/devlink/index.rst +1 -0
index d4a83fdcff7f..1af780c811ee 100644
@@ -99,6 +99,7 @@ parameters, info versions, and other features it supports.
    nfp
    octeontx2
    prestera
+   ptp_ocp
    qed
    sfc
    stmmac
Documentation/networking/devlink/ptp_ocp.rst new file +35 -0
new file mode 100644
index 000000000000..3f997cd9f3a3
@@ -0,0 +1,35 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+========================
+ptp_ocp devlink support
+========================
+
+This document describes the devlink features implemented by the ``ptp_ocp``
+device driver.
+
+Info versions
+=============
+
+The ``ptp_ocp`` driver reports the following versions
+
+.. list-table:: devlink info versions implemented
+   :widths: 5 5 90
+
+   * - Name
+     - Type
+     - Description
+   * - ``fw``
+     - running
+     - Version of the firmware running on the card.  Reported as ``loader``
+       instead when the card is running the firmware loader.
+   * - ``board.id``
+     - fixed
+     - Board identifier, read from the on-card EEPROM.
+   * - ``cpld.id``
+     - fixed
+     - Lattice device ID (IDCODE) of the TAP CPLD, formatted as ``0x%08x``.
+       Only present on ADVA TimeCard X1 boards, which are the only ones
+       carrying that CPLD.  Reading it claims the shared I2C bus and
+       reprograms the on-card mux, so the driver does that once from its
+       own worker and reports the cached value here; the version is
+       omitted until that read has succeeded.
MAINTAINERS +1 -0
index 0b42e898f4d8..6216244edb04 100644
@@ -20485,6 +20485,7 @@ OPENCOMPUTE PTP CLOCK DRIVER
 M:	Vadim Fedorenko <[email protected]>
 L:	[email protected]
 S:	Maintained
+F:	Documentation/networking/devlink/ptp_ocp.rst
 F:	drivers/ptp/ptp_ocp.c
 
 OPENCORES I2C BUS DRIVER
drivers/ptp/ptp_ocp.c +382 -6

484 lines not on this page · raw message

Signature · 1 line
2.47.0
#5 lore raw

PATCH v13 2/2 ptp: ocp: add TAP CPLD firmware upload for ADVA TimeCard X1

In reply to Sagi Maimon

The Lattice MachXO3 CPLD on the ADVA TimeCard X1 is programmed over I2C using in-system programming (ISP). Build on the TMC bus arbitration added previously and expose the update path through the kernel firmware-upload subsystem.

The framework acquires the bus, erases the configuration flash, programs the image page-by-page and activates it with the MachXO3 REFRESH command. The upload node is registered per card as adva-cpld.N, using the same index as the owning ocpN device, so a host with more than one X1 board gets one node each:

/sys/class/firmware/adva-cpld.N/

The whole prepare/write/poll_complete/cleanup sequence runs under cpld_lock and the i2c adapter lock, so an EEPROM read blocks for as long as programming takes; the alternative is reading the TMC bus instead. The upload is unregistered first on detach, which cancels and flushes an in-flight programming cycle while the I2C controller is still up.

Select FW_LOADER and FW_UPLOAD, as the documented update path does not exist without them.

---
 Documentation/ABI/testing/sysfs-timecard |   4 +
 drivers/ptp/Kconfig                      |   2 +
 drivers/ptp/ptp_ocp.c                    | 381 ++++++++++++++++++++++-
 3 files changed, 383 insertions(+), 4 deletions(-)
Documentation/ABI/testing/sysfs-timecard +4 -0
index 70ecc15b347f..8c0d41c941ad 100644
@@ -30,6 +30,10 @@ Description:	(RO, root only) The status register of the TAP CPLD, in
 		ID of the CPLD is read once at probe and reported as the
 		fixed "cpld.id" version by devlink dev info.
 
+		To program new CPLD firmware use the standard kernel
+		firmware-upload interface, registered per card at:
+		/sys/class/firmware/adva-cpld.N/
+		where N is the index of this ocpN device.
 
 What:		/sys/class/timecard/ocpN/available_clock_sources
 Date:		September 2021
drivers/ptp/Kconfig +2 -0
index feb50f8cc406..24f047c37e4c 100644
@@ -219,6 +219,8 @@ config PTP_1588_CLOCK_OCP
 	select NET_DEVLINK
 	select CRC16
 	select DPLL
+	select FW_LOADER
+	select FW_UPLOAD
 	help
 	  This driver adds support for an OpenCompute time card.
 
drivers/ptp/ptp_ocp.c +377 -4

453 lines not on this page · raw message

Signature · 1 line
2.47.0
#2 lore raw

Re: [PATCH net-next v13 1/2] ptp: ocp: add TAP CPLD access for ADVA TimeCard X1

In reply to Sagi Maimon
Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:

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

Critical: 0 · High: 0 (1) · Medium: 3 (1) · Low: 5

- [Medium] ptp_ocp_probe() silently fixes a pre-existing devlink teardown bug.
- [Medium] In ptp_ocp_sync_work() (drivers/ptp/ptp_ocp.c:5406-5409) the one-shot flag bp->cpld_id_tried is latched for every return value of…
- [Medium] The grant hand-back is fire-and-forget.
- [Low] The comment above adva_x1_cpld_read_id() (drivers/ptp/ptp_ocp.c:4503-4508) states "Returns -EBUSY if the bus is…
- [Low] ptp_ocp_i2c_notifier_call() (drivers/ptp/ptp_ocp.c:5590-5602) publishes and invalidates bp->cpld_i2c_adap_nr for *any* i2c adapter…
- [Low] The new Documentation/ABI/testing/sysfs-timecard entry for cpld_status states "The Lattice device ID of the CPLD is read once at…
- [Low] Documentation/networking/devlink/ptp_ocp.rst claims to describe "the devlink features implemented by the ptp_ocp device driver" and is…
- [Low] bp->cpld_id is written by adva_x1_cpld_read_id() (drivers/ptp/ptp_ocp.c:4525) from the sync worker under cpld_lock, but…

Pre-existing issues:
- [High] In ptp_ocp_probe(), if any dpll_pin_get() or dpll_pin_register() fails after dpll_device_register() succeeded, the out_dpll path…
- [Medium] In ptp_ocp_probe(), when dpll_device_register() fails (line 5493-5494) the code jumps to the new out_devlink label, which calls…

--

ptp: ocp: add TAP CPLD access for ADVA TimeCard X1

This adds a request/grant handshake that re-routes the host I2C controller from the config EEPROMs onto the TMC bus, plus read-only access to the Lattice CPLD behind the PCA9548 on that bus. The CPLD device ID is read once from the sync worker and reported as the fixed devlink version "cpld.id", and a new root-only cpld_status sysfs attribute reports the CPLD status register.

A few questions below.

Quoted 16 lines
Documentation/ABI/testing/sysfs-timecard +8 -0
index 3ae41b7634ac..70ecc15b347f 100644
@@ -11,6 +11,26 @@ Contact:	Jonathan Lemon <[email protected]>
 Description:	This directory contains the attributes of the Nth timecard
 		registered.
 
+What:		/sys/class/timecard/ocpN/cpld_status
+Date:		July 2026
+Contact:	Sagi Maimon <[email protected]>
+Description:	(RO, root only) The status register of the TAP CPLD, in
+		human-readable form:
+
+		  done=<0|1> busy=<0|1> failed=<0|1>
+

Documentation/ABI/testing/sysfs-timecard Contact: Jonathan Lemon <[email protected]> · line 31

Quoted 10 lines
Documentation/ABI/testing/sysfs-timecard +10 -0
+		Only present on ADVA x1 TAP boards (PCI ID 0xad5a:0x0410).
+
+		done=1 indicates the configuration flash was successfully
+		programmed and is active.  busy=1 means an internal operation
+		is in progress.  failed=1 means the last ISC operation failed.
+
+		A read arbitrates for the shared I2C bus and reprograms the
+		on-card mux, so it is restricted to root.  The Lattice device
+		ID of the CPLD is read once at probe and reported as the
+		fixed "cpld.id" version by devlink dev info.

[Severity: Low]
Is the "read once at probe" wording accurate? ptp_ocp_probe() only initialises cpld_lock and sets cpld_i2c_adap_nr = -1; the only caller of adva_x1_cpld_read_id() is ptp_ocp_sync_work(), so the earliest the ID can be read is one sync_work tick (HZ) after probe returns, and only once the adapter number has been cached by the notifier.

Documentation/networking/devlink/ptp_ocp.rst added by the same patch says the opposite:

so the driver does that once from its
own worker and reports the cached value here

Should the ABI text be reworded to match, so the two files agree?

Quoted 7 lines
Documentation/networking/devlink/ptp_ocp.rst new file +1 -0
new file mode 100644
index 000000000000..3f997cd9f3a3
@@ -0,0 +1,35 @@
+.. SPDX-License-Identifier: GPL-2.0

Documentation/networking/devlink/ptp_ocp.rst line 11

Quoted 10 lines
Documentation/networking/devlink/ptp_ocp.rst new file +10 -0
+
+========================
+ptp_ocp devlink support
+========================
+
+This document describes the devlink features implemented by the ``ptp_ocp``
+device driver.
+
+Info versions
+=============

[Severity: Low]
The file states it describes "the devlink features implemented by the ptp_ocp device driver", and index.rst says each driver file documents "parameters, info versions, and other features it supports", but only info versions are covered here.

ptp_ocp_devlink_ops also implements flash update:

.flash_update = ptp_ocp_devlink_flash_update, .info_get = ptp_ocp_devlink_info_get,

Would it make sense to add a flash update section, or to narrow the introductory sentence to info versions?

[ ... ]

Quoted 4 lines
drivers/ptp/ptp_ocp.c +0 -0
index 3d26ec1f7b9e..7db1dc9b94bd 100644

[ ... ]

Quoted 4 lines
drivers/ptp/ptp_ocp.c +0 -0
@@ -2166,6 +2187,17 @@ ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
 	if (err)
 		return err;
 

drivers/ptp/ptp_ocp.c ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req, · line 2199

Quoted 10 lines
drivers/ptp/ptp_ocp.c +10 -0
+	/* Read by the worker, not here: this command is unprivileged and
+	 * reading the ID claims the I2C bus.  0 means unread - skip it
+	 * rather than fail, devlink discards the whole reply on error.
+	 */
+	if (bp->cpld_id) {
+		sprintf(buf, "0x%08x", bp->cpld_id);
+		err = devlink_info_version_fixed_put(req, "cpld.id", buf);
+		if (err)
+			return err;
+	}

[Severity: Low]
Should these two reads of bp->cpld_id use READ_ONCE()?

The field is written in adva_x1_cpld_read_id() under cpld_lock:

	if (!ret)
		bp->cpld_id = get_unaligned_be32(data);

while this path reads it with no lock at all, so the test and the sprintf() argument are two independent plain loads that the compiler may reload. Today the value only ever transitions 0 -> ID once, so the effect is limited to a KCSAN report, but the neighbouring lock-free field is already annotated (READ_ONCE(bp->cpld_i2c_adap_nr) / WRITE_ONCE()), so the asymmetry stands out. bp->cpld_id_tried is a plain read/write as well.

[ ... ]

Quoted 4 lines
drivers/ptp/ptp_ocp.c +0 -0
@@ -4226,6 +4259,319 @@ static const struct ocp_attr_group art_timecard_groups[] = {
 	{ },
 };
 

[ ... ]

Quoted 43 lines
drivers/ptp/ptp_ocp.c +43 -0
+static void adva_x1_mblaze_release(struct ptp_ocp *bp)
+{
+	if (bp->pps_select)
+		iowrite32(MBLAZE_RELEASE, &bp->pps_select->i2c_bus_ctrl);
+}
+
+/* Acquire the shared I2C bus from the MicroBlaze firmware.  Returns with no
+ * request outstanding on failure, so the firmware is never left granting a
+ * segment to a host that has given up waiting for it.
+ */
+static int adva_x1_mblaze_acquire(struct ptp_ocp *bp)
+{
+	u32 val;
+	int i;
+
+	if (!bp->pps_select)
+		return -ENODEV;
+
+	/* Drop a request left by a caller that died mid-sequence; cpld_lock
+	 * keeps live ones out.  The read back only flushes the posted write.
+	 */
+	iowrite32(0, &bp->pps_select->i2c_bus_ctrl);
+	ioread32(&bp->pps_select->i2c_bus_ctrl);
+
+	iowrite32(MBLAZE_REQUEST, &bp->pps_select->i2c_bus_ctrl);
+	for (i = 0; i < MBLAZE_RETRIES; i++) {
+		usleep_range(MBLAZE_RETRY_US, MBLAZE_RETRY_US + 1000);
+		val = ioread32(&bp->pps_select->i2c_bus_ctrl);
+		if (val == MBLAZE_GRANTED)
+			return 0;
+	}
+
+	adva_x1_mblaze_release(bp);
+	return -ETIMEDOUT;
+}
+
+/* Route the host controller back to the EEPROMs and release the adapter.
+ * Safe after a failed claim: it also clears a request that was never granted.
+ */
+static void adva_x1_bus_release(struct ptp_ocp *bp)
+{
+	struct i2c_adapter *adap = bp->cpld_adap;
+

drivers/ptp/ptp_ocp.c static const struct ocp_attr_group art_timecard_groups[] = { · line 4423

Quoted 10 lines
drivers/ptp/ptp_ocp.c +10 -0
+	if (!adap)
+		return;
+
+	adva_x1_mblaze_release(bp);
+	bp->cpld_adap = NULL;
+	kfree(bp->cpld_buf);
+	bp->cpld_buf = NULL;
+	i2c_unlock_bus(adap, I2C_LOCK_ROOT_ADAPTER);
+	i2c_put_adapter(adap);
+}

[Severity: Medium]
Can I2C traffic be issued on the TMC bus after the release?

The acquire direction of this handshake is fully synchronised: the posted write is flushed with ioread32() and then the grant is polled for up to 200 x 10 ms. The release direction in adva_x1_mblaze_release() is a bare iowrite32(MBLAZE_RELEASE, ...) with no read-back to flush it, and adva_x1_bus_release() then calls i2c_unlock_bus() immediately, without polling for confirmation that the MicroBlaze has re-routed the 0x150000 controller back to the EEPROM segment.

As soon as i2c_unlock_bus() returns, a waiting ptp_ocp_read_eeprom(), an nvmem attribute read or an at24 sysfs transfer can start clocking out transactions:

	adva_x1_mblaze_release(bp);	/* posted, not flushed */
	...
	i2c_unlock_bus(adap, I2C_LOCK_ROOT_ADAPTER);

If the firmware's re-route latency is non-zero, would those transactions land on the TMC bus, i.e. the exact wrong-bus traffic the commit message says the adapter lock exists to prevent? An EEPROM read could then fill bp->serial / bp->board_id from an unrelated TMC slave, and an at24 write could be delivered to one. Since the acquire path treats this register transition as slow enough to need a flush plus a 2 s poll, should the release path flush and confirm too?

[ ... ]

drivers/ptp/ptp_ocp.c static const struct ocp_attr_group art_timecard_groups[] = { · line 4515

Quoted 13 lines
drivers/ptp/ptp_ocp.c +13 -0
+/*
+ * Read the Lattice device ID into bp->cpld_id.  Done once, off the
+ * unprivileged devlink path, which reports the cached value only.
+ * Returns -EBUSY if the bus is busy, so the caller can retry rather than
+ * wait out a firmware upload.  The LCMXO3LF-2100 IDCODE is 0xe12bc043.
+ */
+static int adva_x1_cpld_read_id(struct ptp_ocp *bp)
+{
+	u8 data[4];
+	int ret;
+
+	if (!mutex_trylock(&bp->cpld_lock))
+		return -EBUSY;

[Severity: Low]
The comment says "Returns -EBUSY if the bus is busy, so the caller can retry rather than wait", but the only source of -EBUSY is this mutex_trylock() on cpld_lock. Actual I2C bus contention is handled with a blocking lock inside adva_x1_bus_claim():

	i2c_lock_bus(adap, I2C_LOCK_ROOT_ADAPTER);

so when ptp_ocp_read_eeprom(), an nvmem attribute or an at24 transfer holds the root adapter lock, the sync worker waits there instead of returning -EBUSY. Could the comment say cpld_lock rather than "the bus"? The reference to waiting out a firmware upload only becomes meaningful with the later "add TAP CPLD firmware upload for ADVA TimeCard X1" patch, which adds a long-running cpld_lock holder.

Quoted 20 lines
drivers/ptp/ptp_ocp.c +20 -0
+	ret = adva_x1_bus_claim(bp);
+	if (ret)
+		goto out;
+	ret = adva_x1_mux_select(bp, ADVA_MUX_CHANNEL);
+	if (ret)
+		goto release;
+	ret = adva_x1_cpld_cmd_read(bp, CPLD_CMD_READ_ID, data, 4);
+	if (!ret)
+		bp->cpld_id = get_unaligned_be32(data);
+	adva_x1_mux_select(bp, -1);
+release:
+	adva_x1_bus_release(bp);
+out:
+	mutex_unlock(&bp->cpld_lock);
+	if (ret)
+		dev_dbg(&bp->pdev->dev,
+			"CPLD device ID unavailable: %d\n", ret);
+
+	return ret;
+}

[ ... ]

Quoted 4 lines
drivers/ptp/ptp_ocp.c +0 -0
@@ -5052,6 +5400,14 @@ ptp_ocp_sync_work(struct work_struct *work)
 
 	bp->sync = sync;
 

drivers/ptp/ptp_ocp.c ptp_ocp_sync_work(struct work_struct *work) · line 5412

Quoted 10 lines
drivers/ptp/ptp_ocp.c +8 -0
+	/* The i2c adapter is registered after probe returns, so the ID is
+	 * read from here, once, as soon as the adapter shows up.
+	 */
+	if (bp->has_cpld && !bp->cpld_id_tried &&
+	    READ_ONCE(bp->cpld_i2c_adap_nr) >= 0 &&
+	    adva_x1_cpld_read_id(bp) != -EBUSY)
+		bp->cpld_id_tried = true;
+
 	queue_delayed_work(system_power_efficient_wq, &bp->sync_work, HZ);
 }

[Severity: Medium]
Should transient failures also keep the retry alive here? Only -EBUSY avoids latching cpld_id_tried, and -EBUSY comes solely from the mutex_trylock() in adva_x1_cpld_read_id(). Every other failure looks transient:

- -ETIMEDOUT from adva_x1_mblaze_acquire() when the MicroBlaze does not grant the TMC segment within the 200 x 10 ms poll (e.g. busy firmware right after power-up)
- -ENOMEM from the kzalloc() in adva_x1_bus_claim()
- the controller errno or -EIO from __i2c_transfer()

Any one of those on the single attempt leaves bp->cpld_id == 0 for the life of the card, so the "if (bp->cpld_id)" test in ptp_ocp_devlink_info_get() never becomes true and cpld.id is omitted permanently, with only a dev_dbg() to show why.

Documentation/networking/devlink/ptp_ocp.rst says:

the version is
omitted until that read has succeeded.

which reads as though the driver keeps trying. Would treating -ETIMEDOUT and -ENOMEM the same way as -EBUSY match that description?

drivers/ptp/ptp_ocp.c ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 5494

Quoted 12 lines
drivers/ptp/ptp_ocp.c +2 -2
@@ -5119,12 +5486,12 @@ ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	if (IS_ERR(bp->dpll)) {
 		err = PTR_ERR(bp->dpll);
 		dev_err(&pdev->dev, "dpll_device_alloc failed\n");
-		goto out;
+		goto out_devlink;
 	}
 
 	err = dpll_device_register(bp->dpll, DPLL_TYPE_PPS, &dpll_ops, bp);
 	if (err)
-		goto out;
+		goto out_devlink;

[Severity: Medium]
These two redirects look like a fix for a separate, existing bug rather than part of the CPLD feature.

devlink_register(devlink) runs before both call sites, and in the baseline both used "goto out", reaching ptp_ocp_detach() and then devlink_free(devlink) with the instance still registered. devlink_free() starts with:

net/devlink/core.c:devlink_free() {
	ASSERT_DEVLINK_NOT_REGISTERED(devlink);
	...
}

so the old paths would trip that assertion. The new comment on the label attributes the change to cpld_lock teardown instead:

	/* detach() destroys cpld_lock; no devlink op may still reach it */

Could this be split out as its own patch with a Fixes: tag, so it can be reviewed and backported independently of the CPLD work?

[Severity: Medium]
This isn't a bug introduced by this patch (the baseline used "goto out" here, which skipped the same call), but since these gotos are being rewritten
anyway: on the dpll_device_register() failure path, is the reference taken a few lines earlier by dpll_device_get(clkid, 0, THIS_MODULE, &bp->tracker) ever dropped?

out_devlink falls into "out: ptp_ocp_detach(bp)", ptp_ocp_detach() does not touch bp->dpll, and only out_dpll calls dpll_device_put(). So a failing dpll_device_register() (-ENOMEM or -EEXIST) leaves the dpll_device in dpll_device_xa with a leaked reference and a stale ref-tracker entry.

drivers/ptp/ptp_ocp.c ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 5526

Quoted 13 lines
drivers/ptp/ptp_ocp.c +3 -0
 	for (i = 0; i < OCP_SMA_NUM; i++) {
 		bp->sma[i].dpll_pin = dpll_pin_get(clkid, i, THIS_MODULE,
@@ -5151,6 +5518,9 @@ ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		dpll_pin_put(bp->sma[i].dpll_pin, &bp->sma[i].tracker);
 	}
 	dpll_device_put(bp->dpll, &bp->tracker);
+out_devlink:
+	/* detach() destroys cpld_lock; no devlink op may still reach it */
+	devlink_unregister(devlink);
 out:
 	ptp_ocp_detach(bp);
 out_disable:

[Severity: High]
This is a pre-existing issue and not introduced by this patch (the same code is in the baseline; the patch only redirects the earlier gotos), but it sits directly in the path being edited.

Should out_dpll call dpll_device_unregister(bp->dpll, &dpll_ops, bp) before dpll_device_put()? dpll_device_register() takes a second reference and stores bp:

drivers/dpll/dpll_core.c:dpll_device_register() {
	...
	reg->ops = ops;
	reg->priv = priv;
	dpll->type = type;
	__dpll_device_hold(dpll, &reg->tracker);
	...
}

and only dpll_device_unregister() drops it:

drivers/dpll/dpll_core.c:dpll_device_unregister() {
	...
	list_del(&reg->list);
	__dpll_device_put(dpll, &reg->tracker);
	kfree(reg);
	...
}

If a dpll_pin_get() or dpll_pin_register() fails after dpll_device_register() succeeded, out_dpll drops only bp->tracker, so the device stays registered and visible to userspace with reg->priv still pointing at bp, while probe continues to ptp_ocp_detach() and devlink_free(devlink), which frees the storage holding bp. Can a later dpll netlink get or dump then call the dpll_ops callbacks on freed memory, in addition to leaking the registration object?

Quoted 10 lines
drivers/ptp/ptp_ocp.c +3 -2
@@ -5219,11 +5589,17 @@ ptp_ocp_i2c_notifier_call(struct notifier_block *nb,
 
 found:
 	bp = dev_get_drvdata(dev);
-	if (add)
+	if (add) {
 		ptp_ocp_symlink(bp, child, "i2c");
-	else
+		/* Cache the number; adva_x1_bus_claim() takes the reference
+		 * and re-checks the parent, as the number can be recycled.

drivers/ptp/ptp_ocp.c ptp_ocp_i2c_notifier_call(struct notifier_block *nb, · line 5604

Quoted 10 lines
drivers/ptp/ptp_ocp.c +6 -1
+		 */
+		WRITE_ONCE(bp->cpld_i2c_adap_nr, i2c_verify_adapter(child)->nr);
+	} else {
+		/* invalidate before free */
+		WRITE_ONCE(bp->cpld_i2c_adap_nr, -1);
 		sysfs_remove_link(&bp->dev.kobj, "i2c");
-
+	}
 	return 0;
 }

[Severity: Low]
Should this side apply the same identity check that the consumer applies?

The notifier publishes and invalidates cpld_i2c_adap_nr for any i2c adapter whose ancestor walk reaches a device bound to ptp_ocp, while adva_x1_bus_claim() insists on the card's own controller:

	if (!bp->i2c_ctrl || adap->dev.parent != &bp->i2c_ctrl->dev) {
		i2c_put_adapter(adap);
		return -ENODEV;
	}

i2c-mux child adapters pass the notifier filter but fail that test, because i2c_mux_add_adapter() sets:

	priv->adap.dev.parent = &parent->dev;

so if root instantiates a mux on the card's adapter (writing "pca9548 0x74" to new_device), each child's ADD notification overwrites cpld_i2c_adap_nr with the mux segment number and every later claim returns -ENODEV; a child's DEL notification writes -1 while the card's own controller is still alive. There is no re-arm path in either case, so cpld_status and the one-shot cpld.id read stay disabled for the life of the card. Would testing i2c_verify_adapter(child)->dev.parent == &bp->i2c_ctrl->dev here avoid that?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260908122328.52641-1-maimon.sagi%40gmail.com
#6 lore raw

Re: [PATCH net-next v13 2/2] ptp: ocp: add TAP CPLD firmware upload for ADVA TimeCard X1

In reply to Sagi Maimon
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:

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

Critical: 0 · High: 0 (1) · Medium: 3 · Low: 3

- [Medium] bp->cpld_cancel is a single per-device boolean cleared at driver-chosen points (entry of adva_cpld_prepare(), prepare()'s…
- [Medium] adva_x1_cpld_wait_ready() returns as soon as READ_ONCE(bp->cpld_cancel) is observed (-ECANCELED) or a status read…
- [Medium] adva_cpld_poll_complete() reports FW_UPLOAD_ERR_NONE without verifying that the CPLD reconfigured successfully.
- [Low] The new paragraph in Documentation/ABI/testing/sysfs-timecard directs userspace to program the CPLD via /sys/class/firmware/adva-…
- [Low] In adva_cpld_poll_complete() the post-REFRESH mux-reselect poll `read_poll_timeout(adva_x1_mux_select, err, !err, CPLD_POLL_US, 3000 *…
- [Low] adva_x1_cpld_wait_ready() exits its poll as soon as the status read fails (err is part of the termination condition) but then…

Pre-existing issues:
- [High] ptp_ocp_detach() relies on firmware_upload_unregister() to cancel *and* flush an in-flight upload before cpld_lock, the i2c_ctrl…

--

ptp: ocp: add TAP CPLD firmware upload for ADVA TimeCard X1

This adds an in-system programming path for the Lattice MachXO3 CPLD on the ADVA TimeCard X1, exposed through the kernel firmware-upload subsystem as /sys/class/firmware/adva-cpld.N/. The prepare/write/poll_complete/cleanup callbacks run under cpld_lock with the i2c adapter lock held, and the upload node is unregistered first in ptp_ocp_detach().

A few questions below.

Documentation/ABI/testing/sysfs-timecard Description: (RO, root only) The status register of the TAP CPLD, in · line 36

Quoted 12 lines
Documentation/ABI/testing/sysfs-timecard +4 -0
index 70ecc15b347f..8c0d41c941ad 100644
@@ -30,6 +30,10 @@ Description:	(RO, root only) The status register of the TAP CPLD, in
 		ID of the CPLD is read once at probe and reported as the
 		fixed "cpld.id" version by devlink dev info.
 
+		To program new CPLD firmware use the standard kernel
+		firmware-upload interface, registered per card at:
+		/sys/class/firmware/adva-cpld.N/
+		where N is the index of this ocpN device.

[Severity: Low]
Should this paragraph also state the image format the driver accepts?

adva_cpld_prepare() rejects anything that is not an exact multiple of CPLD_PAGE_SIZE:

	if (!size || size % CPLD_PAGE_SIZE) {
		WRITE_ONCE(bp->cpld_cancel, false);
		return FW_UPLOAD_ERR_INVALID_SIZE;
	}

and adva_cpld_write() copies the bytes verbatim into the page-program payload, so the node takes a raw page-aligned bitstream rather than a Lattice .jed/.hex container. The internal comment above the callbacks does say "Userspace writes the raw binary page data directly", but the ABI text does not, and the only feedback a user gets from a container file is the framework's generic "invalid-file-size" in the error attribute.

[ ... ]

Quoted 4 lines
drivers/ptp/ptp_ocp.c +0 -0
index 7db1dc9b94bd..6eefc3db586f 100644

[ ... ]

Quoted 18 lines
drivers/ptp/ptp_ocp.c +14 -0
@@ -4500,6 +4587,55 @@ static int adva_x1_cpld_read_status(struct ptp_ocp *bp, u32 *status)
 	return 0;
 }
 
+/* Poll the status register until the CPLD goes idle, or @max_ms elapses.
+ * The deadline is on wall time, so the I2C transactions count against it,
+ * and the status is read once more after it expires before giving up.
+ */
+static int adva_x1_cpld_wait_ready(struct ptp_ocp *bp, unsigned int max_ms)
+{
+	u32 status = 0;
+	int err, ret;
+
+	ret = read_poll_timeout(adva_x1_cpld_read_status, err,
+				err || READ_ONCE(bp->cpld_cancel) ||
+				(status & CPLD_STATUS_FAILED) ||
+				!(status & CPLD_STATUS_BUSY),
+				CPLD_POLL_US, max_ms * USEC_PER_MSEC, false,

drivers/ptp/ptp_ocp.c static int adva_x1_cpld_read_status(struct ptp_ocp *bp, u32 *status) · line 4613

Quoted 10 lines
drivers/ptp/ptp_ocp.c +10 -0
+				bp, &status);
+	if (ret)
+		return ret;
+	if (READ_ONCE(bp->cpld_cancel))
+		return -ECANCELED;
+	if (err || (status & CPLD_STATUS_FAILED))
+		return -EIO;
+
+	return 0;
+}

[Severity: Low]
Is collapsing every transport errno to -EIO here intentional?

adva_x1_cpld_read_status() returns the underlying I2C errno (-ETIMEDOUT, -ENXIO, -EREMOTEIO, ...), and err is part of the termination condition, so the poll exits with that errno available. It is then replaced:

	if (err || (status & CPLD_STATUS_FAILED))
		return -EIO;

adva_cpld_err() distinguishes those categories, but by the time it is consulted the distinction is gone:

	case -ETIMEDOUT:
		return FW_UPLOAD_ERR_TIMEOUT;
	...
	case -EIO:		/* FAILED status, or a short transfer */
		return FW_UPLOAD_ERR_HW_ERROR;

so an I2C controller timeout during a status poll is reported as "hw-error" instead of "timeout", and a NACK as "hw-error" instead of "read-write-error". The same -EIO is also used for a genuine CPLD FAILED status, so the two are indistinguishable in the error attribute. Would returning err when it is non-zero, and -EIO only for CPLD_STATUS_FAILED, keep the mapping useful?

Quoted 9 lines
drivers/ptp/ptp_ocp.c +9 -0
+
+/* Map an errno onto the category userspace reads back from
+ * /sys/class/firmware/adva-cpld.N/error.  A step aborted by cancel() must
+ * be reported as such, not as a HW error.
+ */
+static enum fw_upload_err adva_cpld_err(struct ptp_ocp *bp, int err)
+{
+	if (READ_ONCE(bp->cpld_cancel))
+		return FW_UPLOAD_ERR_CANCELED;

[ ... ]

Quoted 4 lines
drivers/ptp/ptp_ocp.c +0 -0
@@ -4572,6 +4708,234 @@ cpld_status_show(struct device *dev, struct device_attribute *attr,
 }
 static DEVICE_ATTR_ADMIN_RO(cpld_status);
 

[ ... ]

Quoted 6 lines
drivers/ptp/ptp_ocp.c +6 -0
+static enum fw_upload_err
+adva_cpld_prepare(struct fw_upload *fwl, const u8 *data, u32 size)
+{
+	enum fw_upload_err ret = FW_UPLOAD_ERR_NONE;
+	struct ptp_ocp *bp = fwl->dd_handle;
+	int err;

drivers/ptp/ptp_ocp.c cpld_status_show(struct device *dev, struct device_attribute *attr, · line 4742

Quoted 10 lines
drivers/ptp/ptp_ocp.c +10 -0
+
+	/* Do not clear cpld_cancel here: fw_upload_start() queues the work
+	 * before this runs, so a cancel may already have arrived - honour it
+	 * before touching the hardware.  It is cleared once the upload is
+	 * over, on every exit below and in cleanup().
+	 */
+	if (READ_ONCE(bp->cpld_cancel)) {
+		WRITE_ONCE(bp->cpld_cancel, false);
+		return FW_UPLOAD_ERR_CANCELED;
+	}

[Severity: Medium]
Can this consume a cpld_cancel left over from the previous upload?

adva_cpld_cleanup() clears the flag and unlocks, but the framework only returns progress to IDLE afterwards:

drivers/base/firmware_loader/sysfs_upload.c:fw_upload_main() {
	...
done:
	if (fwlp->ops->cleanup)
		fwlp->ops->cleanup(fwl);
	...
	fw_upload_prog_complete(fwlp);
}

In that window cancel_store() still accepts a cancel, since it only rejects the IDLE state:

	mutex_lock(&fwlp->lock);
	if (fwlp->progress == FW_UPLOAD_PROG_IDLE) {
		mutex_unlock(&fwlp->lock);
		return -ENODEV;
	}

	fwlp->ops->cancel(fwlp->fw_upload);

so adva_cpld_cancel() sets cpld_cancel = true with no upload running, and nothing clears it until the next adva_cpld_prepare(), which aborts a fresh and unrelated upload with "user-abort".

Relatedly, adva_cpld_err() tests the same flag before looking at err, so a cancel racing a real transfer failure reports a hardware or I2C error as a user abort. Would tying cancellation to an upload generation counter rather than a bare bool cleared from several driver-side points close both cases?

Quoted 15 lines
drivers/ptp/ptp_ocp.c +15 -0
+
+	if (!size || size % CPLD_PAGE_SIZE) {
+		WRITE_ONCE(bp->cpld_cancel, false);
+		return FW_UPLOAD_ERR_INVALID_SIZE;
+	}
+
+	bp->cpld_in_config_mode = false;
+
+	mutex_lock(&bp->cpld_lock);
+
+	err = adva_x1_bus_claim(bp);
+	if (err) {
+		ret = adva_cpld_err(bp, err);
+		goto err_unlock;
+	}

[ ... ]

Quoted 7 lines
drivers/ptp/ptp_ocp.c +7 -0
+	err = adva_x1_cpld_write(bp, CPLD_CMD_ERASE);
+	if (!err)
+		err = adva_x1_cpld_wait_ready(bp, 15000);
+	if (err) {
+		ret = adva_cpld_err(bp, err);
+		goto err_deselect;
+	}

[ ... ]

drivers/ptp/ptp_ocp.c cpld_status_show(struct device *dev, struct device_attribute *attr, · line 4824

Quoted 13 lines
drivers/ptp/ptp_ocp.c +13 -0
+err_deselect:
+	if (bp->cpld_in_config_mode) {
+		adva_x1_cpld_write(bp, CPLD_CMD_DIS_CFG);
+		bp->cpld_in_config_mode = false;
+	}
+	adva_x1_mux_select(bp, -1);
+err_release:
+	adva_x1_bus_release(bp);
+err_unlock:
+	WRITE_ONCE(bp->cpld_cancel, false);
+	mutex_unlock(&bp->cpld_lock);
+	return ret;
+}

[Severity: Medium]
Can DIS_CFG reach the CPLD while it is still busy erasing?

adva_x1_cpld_wait_ready() returns as soon as the cancel flag is seen, or as soon as a status read fails, i.e. possibly with CPLD_STATUS_BUSY still asserted:

	ret = read_poll_timeout(adva_x1_cpld_read_status, err,
				err || READ_ONCE(bp->cpld_cancel) ||
				(status & CPLD_STATUS_FAILED) ||
				!(status & CPLD_STATUS_BUSY),

A "echo 1 > cancel" during the 15 s config-sector erase therefore lands in err_deselect with the erase in flight. The abort path then sends DIS_CFG, discards its result and clears the bookkeeping unconditionally:

	if (bp->cpld_in_config_mode) {
		adva_x1_cpld_write(bp, CPLD_CMD_DIS_CFG);
		bp->cpld_in_config_mode = false;
	}

before deselecting the mux and handing the TMC segment back to the MicroBlaze. If the ISC_DISABLE is not latched (device busy, NACK, I2C error) the part stays in transparent configuration mode with a partially erased configuration flash, while the driver records that it left the mode, so nothing will issue DIS_CFG again. adva_cpld_cleanup() has the same shape for a canceled page write.

Would waiting for BUSY to clear under a hard deadline before DIS_CFG, and keeping cpld_in_config_mode set when the DIS_CFG transfer itself fails, be safer here?

Quoted 5 lines
drivers/ptp/ptp_ocp.c +5 -0
+
+static enum fw_upload_err
+adva_cpld_write(struct fw_upload *fwl, const u8 *data,
+		u32 offset, u32 size, u32 *written)
+{

[ ... ]

Quoted 32 lines
drivers/ptp/ptp_ocp.c +32 -0
+static enum fw_upload_err
+adva_cpld_poll_complete(struct fw_upload *fwl)
+{
+	struct ptp_ocp *bp = fwl->dd_handle;
+	int err;
+	u32 st;
+
+	lockdep_assert_held(&bp->cpld_lock);
+
+	if (READ_ONCE(bp->cpld_cancel))
+		return FW_UPLOAD_ERR_CANCELED;
+
+	err = adva_x1_cpld_write(bp, CPLD_CMD_SET_DONE);
+	if (!err)
+		err = adva_x1_cpld_wait_ready(bp, 1000);
+	if (err)
+		return adva_cpld_err(bp, err);
+
+	err = adva_x1_cpld_read_status(bp, &st);
+	if (err)
+		return adva_cpld_err(bp, err);
+	if (!(st & CPLD_STATUS_DONE))
+		return FW_UPLOAD_ERR_HW_ERROR;
+
+	err = adva_x1_cpld_write(bp, CPLD_CMD_REFRESH);
+	if (err)
+		return adva_cpld_err(bp, err);
+
+	/* REFRESH reboots the CPLD out of configuration mode, so cleanup()
+	 * must not send DIS_CFG afterwards even if the checks below fail.
+	 */
+	bp->cpld_in_config_mode = false;

drivers/ptp/ptp_ocp.c cpld_status_show(struct device *dev, struct device_attribute *attr, · line 4896

Quoted 10 lines
drivers/ptp/ptp_ocp.c +10 -0
+
+	/* The new image is already running at this point, so a segment that
+	 * is not back yet must not be reported as a failed update: retry the
+	 * reselect instead of sampling the mux once at a fixed delay.
+	 */
+	msleep(1500);
+	if (read_poll_timeout(adva_x1_mux_select, err, !err, CPLD_POLL_US,
+			      3000 * USEC_PER_MSEC, false,
+			      bp, ADVA_MUX_CHANNEL))
+		return FW_UPLOAD_ERR_TIMEOUT;

[Severity: Low]
Should this poll also look at bp->cpld_cancel?

The exit condition is only !err, and the failure branch returns FW_UPLOAD_ERR_TIMEOUT without going through adva_cpld_err(), so a cancel arriving while the mux keeps failing neither ends the loop nor produces FW_UPLOAD_ERR_CANCELED, and the flush_work() inside firmware_upload_unregister() waits out the full 3 s on top of the preceding msleep(1500). The adva_x1_cpld_wait_ready() below does honour the flag, but only once the mux answers.

drivers/ptp/ptp_ocp.c cpld_status_show(struct device *dev, struct device_attribute *attr, · line 4903

Quoted 7 lines
drivers/ptp/ptp_ocp.c +7 -0
+
+	err = adva_x1_cpld_wait_ready(bp, 3000);
+	if (err)
+		return adva_cpld_err(bp, err);
+
+	return FW_UPLOAD_ERR_NONE;
+}

[Severity: Medium]
Is the CPLD state validated after REFRESH before reporting success?

CPLD_STATUS_DONE is only sampled before CPLD_CMD_REFRESH. The single post-REFRESH check is adva_x1_cpld_wait_ready(), which accepts any status with BUSY clear and CPLD_STATUS_FAILED clear, and discards the status word, so neither DONE nor the MachXO3 configuration error field is examined after the device reloads the newly programmed image. A CRC or preamble error, or a refresh that leaves DONE deasserted, would be reported to userspace as a successful update.

The other in-tree MachXO drivers do check both after refresh:

drivers/fpga/machxo2-spi.c:machxo2_write_complete() {
	...
		if (!test_bit(BUSY, &status) && test_bit(DONE, &status) &&
		    get_err(&status) == ENOERR)
			break;
	...
}

Would a similar DONE plus error-field check after REFRESH be appropriate here?

Quoted 26 lines
drivers/ptp/ptp_ocp.c +26 -0
+
+static void
+adva_cpld_cancel(struct fw_upload *fwl)
+{
+	struct ptp_ocp *bp = fwl->dd_handle;
+
+	WRITE_ONCE(bp->cpld_cancel, true);
+}
+
+static void
+adva_cpld_cleanup(struct fw_upload *fwl)
+{
+	struct ptp_ocp *bp = fwl->dd_handle;
+
+	__acquire(&bp->cpld_lock);	/* held since prepare() returned ok */
+	lockdep_assert_held(&bp->cpld_lock);
+
+	if (bp->cpld_in_config_mode) {
+		adva_x1_cpld_write(bp, CPLD_CMD_DIS_CFG);
+		bp->cpld_in_config_mode = false;
+	}
+	adva_x1_mux_select(bp, -1);
+	adva_x1_bus_release(bp);
+	WRITE_ONCE(bp->cpld_cancel, false);
+	mutex_unlock(&bp->cpld_lock);
+}

[ ... ]

Quoted 6 lines
drivers/ptp/ptp_ocp.c +2 -0
@@ -5214,6 +5578,15 @@ ptp_ocp_detach(struct ptp_ocp *bp)
 {
 	int i;
 
+	/* Must come first: cancels and flushes an in-flight upload while the
+	 * I2C controller is still up, and drops cpld_lock so a cpld_status

drivers/ptp/ptp_ocp.c ptp_ocp_detach(struct ptp_ocp *bp) · line 5592

Quoted 10 lines
drivers/ptp/ptp_ocp.c +7 -0
+	 * reader cannot stall ptp_ocp_attr_group_del() below.
+	 */
+	if (bp->cpld_fw_upload) {
+		firmware_upload_unregister(bp->cpld_fw_upload);
+		bp->cpld_fw_upload = NULL;
+	}
+
 	ptp_ocp_debugfs_remove_device(bp);
 	ptp_ocp_detach_sysfs(bp);
 	ptp_ocp_attr_group_del(bp);

[Severity: High]
This window is not introduced by this patch, but is the cancel-and-flush guarantee the comment relies on unconditional?

firmware_upload_unregister() skips both the cancel and the flush when it samples IDLE:

drivers/base/firmware_loader/sysfs_upload.c:firmware_upload_unregister() {
	mutex_lock(&fw_upload_priv->lock);
	if (fw_upload_priv->progress == FW_UPLOAD_PROG_IDLE) {
		mutex_unlock(&fw_upload_priv->lock);
		goto unregister;
	}
	...
	flush_work(&fw_upload_priv->work);

unregister:

	device_unregister(&fw_sysfs->dev);
	module_put(module);
}

A "echo 0 > loading" taking the same lock immediately afterwards queues the work:

fw_upload_start() {
	...
	fwlp->progress = FW_UPLOAD_PROG_RECEIVING;
	...
	queue_work(system_long_wq, &fwlp->work);
}

and the device_unregister() above then reaches fw_dev_release() -> fw_upload_free(), which kfree()s fw_upload_priv, the structure embedding that work_struct, and fw_upload itself.

A queued fw_upload_main() would then run adva_cpld_prepare() with fwl->dd_handle == bp after ptp_ocp_detach() has done mutex_destroy(&bp->cpld_lock) and devlink_free() has freed bp. Since prepare() now takes cpld_lock, the i2c root-adapter lock, an i2c_get_adapter() reference and a kzalloc'd cpld_buf, and only drops them in cleanup(), the same window could also leave the i2c bus lock held and the adapter reference leaked.

The commit message states this as a guarantee:

"The upload is unregistered first on detach, which cancels and flushes an in-flight programming cycle while the I2C controller is still up."

Could that be qualified, or could the driver gate adva_cpld_prepare() on its own "detaching" flag so it does not depend on the core flushing?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260908122328.52641-1-maimon.sagi%40gmail.com
maintainer
#7 lore raw

Re: [PATCH net-next v13 2/2] ptp: ocp: add TAP CPLD firmware upload for ADVA TimeCard X1

In reply to Sagi Maimon
Quoted 13 lines · On Tue, 8 Sep 2026 15:23:28 +0300 Sagi Maimon wrote:

The Lattice MachXO3 CPLD on the ADVA TimeCard X1 is programmed over I2C using in-system programming (ISP). Build on the TMC bus arbitration added previously and expose the update path through the kernel firmware-upload subsystem.

The framework acquires the bus, erases the configuration flash, programs the image page-by-page and activates it with the MachXO3 REFRESH command. The upload node is registered per card as adva-cpld.N, using the same index as the owning ocpN device, so a host with more than one X1 board gets one node each:

/sys/class/firmware/adva-cpld.N/

Quoted 8 lines

The whole prepare/write/poll_complete/cleanup sequence runs under cpld_lock and the i2c adapter lock, so an EEPROM read blocks for as long as programming takes; the alternative is reading the TMC bus instead. The upload is unregistered first on detach, which cancels and flushes an in-flight programming cycle while the I2C controller is still up.

Select FW_LOADER and FW_UPLOAD, as the documented update path does not exist without them.

Why add another FW flashing interface when the device already supports devlink flash? You can use component names to disambiguate which FW we are targeting.

maintainer
#3 lore raw

Re: [PATCH net-next v13 1/2] ptp: ocp: add TAP CPLD access for ADVA TimeCard X1

In reply to Sagi Maimon

Documentation/ABI/testing/sysfs-timecard Contact: Jonathan Lemon <[email protected]> · line 20

Quoted 1 line · On Tue, 8 Sep 2026 15:23:27 +0300 Sagi Maimon wrote:
Documentation/ABI/testing/sysfs-timecard +1 -0
+		  done=<0|1> busy=<0|1> failed=<0|1>

I think the sysfs rules are "one value per file"
We can stretch it a bit and say that this file contains flags

So format like done or busy or failed or multiple eg "busy failed" But the X=[0|1] is too much

Also I think the AI is right this one commit does too many things.

#4 lore raw

Re: [PATCH net-next v13 1/2] ptp: ocp: add TAP CPLD access for ADVA TimeCard X1

Quoted 7 lines · On Fri, 11 Sep 2026 16:52:43 -0700 Jakub Kicinski wrote:
Quoted 1 line

+ done=<0|1> busy=<0|1> failed=<0|1>

I think the sysfs rules are "one value per file"
We can stretch it a bit and say that this file contains flags

So format like done or busy or failed or multiple eg "busy failed" But the X=[0|1] is too much

Changed for v14. The file now contains the names of the flags that are set, space separated, and an empty line when none is:

  # cat cpld_status
  done
  # cat cpld_status     # during programming
  busy

The ABI entry lists the three names and what each means.

Quoted 1 line

Also I think the AI is right this one commit does too many things.

Split for v14. The two pre-existing fixes that were buried in it are separate patches now, with Fixes: tags:

1/4 ptp: ocp: unregister devlink before detach on probe error 2/4 ptp: ocp: fix dpll cleanup on probe error
3/4 ptp: ocp: add TAP CPLD access for ADVA TimeCard X1 4/4 ptp: ocp: add TAP CPLD flashing via devlink

2/4 is a second bug in the same error path, found while looking at the
first: out_dpll never called dpll_device_unregister(), so a failing dpll_pin_get() or dpll_pin_register() left the dpll device registered with a priv pointer into storage devlink_free() releases a few lines later, and a failing dpll_device_register() leaked the reference dpll_device_get() had taken.

1 and 2 fix 09eeb3aecc6c, which is in released kernels, but they only trigger when probe fails, so I did not mark them for stable. They are in this series because 3/4 adds a mutex that ptp_ocp_detach() destroys, so devlink has to be unregistered first.

If you would rather see 3/4 split further - the bus arbitration alone, then the two read-only interfaces on top - I am happy to do that.

v14 posted at: https://lore.kernel.org/netdev/[email protected]/T/#u

Thanks for the review,
Sagi

#8 lore raw

Re: [PATCH net-next v13 2/2] ptp: ocp: add TAP CPLD firmware upload for ADVA TimeCard X1

In reply to Jakub Kicinski
Quoted 3 lines · On Fri, 11 Sep 2026 16:51:05 -0700 Jakub Kicinski wrote:

Why add another FW flashing interface when the device already supports devlink flash? You can use component names to disambiguate which FW we are targeting.

You are right, and it is done that way in v14: the firmware-upload node is gone and the CPLD is a devlink flash component instead.

  devlink dev flash pci/0000:02:00.0 file cpld.bin \
      component fw.cpld

->info_get() reports fw.cpld as a running version with DEVLINK_INFO_VERSION_TYPE_COMPONENT, carrying the USERCODE of the image currently in the part; that is also what makes the core accept the component name. The fixed cpld.id keeps reporting the IDCODE, and no component still means the card flash, exactly as before.

It removed more machinery than it added: the driver side is ~50 lines smaller than the firmware-upload version. The fw_upload_ops callbacks, the adva-cpld.N node, the FW_LOADER/FW_UPLOAD selects and the cancellation state machine that went with them are all gone - and with them a race where a cancel arriving after one upload had finished could abort the next one.

The one thing that changes for userspace is that the image now has to be reachable by request_firmware() rather than streamed into sysfs, which is fine here.

Tested on the card: a full erase/program/refresh cycle of a 6526-page image through that command, the CPLD reporting done afterwards and still done across a reboot. A .jed container and an unknown component name are both rejected before anything touches the part.

v14 posted at: https://lore.kernel.org/netdev/[email protected]/T/#u

Thanks for the review,
Sagi