Re: [PATCH v2 1/2] mtd: rawnand: macronix: serialize OTP access with NAND operations
From: James Hilliard
Date: Tue Nov 24 2026 - 11:10:24 EST
On Tue, Nov 24, 2026 at 7:55 AM Miquel Raynal <miquel.raynal@xxxxxxxxxxx> wrote:
>
>
> > OTP I/O can also run while the chip is suspended.
>
> Really? Where does that come from?
I was referring to chip->suspended in nand_base.c. nand_suspend() sets
that flag even when the chip has no suspend hook. Normal NAND I/O waits
for resume in nand_get_device(), but MTD calls the Macronix user-OTP
read and write callbacks directly, bypassing that check.
I haven't observed OTP I/O during system suspend. Userspace is normally
frozen before device suspend, and the Macronix driver doesn't install
its deep-power-down hook on the MX30LFxG18AC parts.
>
> [...]
>
> > --- a/drivers/mtd/nand/raw/nand_base.c
> > +++ b/drivers/mtd/nand/raw/nand_base.c
> > @@ -207,7 +207,7 @@ EXPORT_SYMBOL_GPL(nand_deselect_target);
> > *
> > * Release chip lock and wake up anyone waiting on the device.
> > */
> > -static void nand_release_device(struct nand_chip *chip)
> > +void nand_release_device(struct nand_chip *chip)
>
> You export a core helper...
>
> > {
> > /* Release the controller and the chip */
> > mutex_unlock(&chip->controller->lock);
> > @@ -336,7 +336,7 @@ static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs)
> > *
> > * Lock the device and its controller for exclusive access
> > */
> > -static void nand_get_device(struct nand_chip *chip)
> > +void nand_get_device(struct nand_chip *chip)
> > {
> > /* Wait until the device is resumed. */
> > while (1) {
> > diff --git a/drivers/mtd/nand/raw/nand_macronix.c b/drivers/mtd/nand/raw/nand_macronix.c
> > index 16847f28b19d..d7f5a9e7c240 100644
> > --- a/drivers/mtd/nand/raw/nand_macronix.c
> > +++ b/drivers/mtd/nand/raw/nand_macronix.c
> > @@ -380,6 +380,7 @@ static int __macronix_30lfxg18ac_rw_otp(struct mtd_info *mtd,
> > int ret, cleanup_ret;
> >
> > nand = mtd_to_nand(mtd);
> > + nand_get_device(nand);
>
> ...and you use it across the entire OTP operation...
The race doesn't require suspend: an ordinary page read on the same
chip can issue its command after the OTP callback has enabled OTP mode
and before it restores normal mode. Locking each command separately
would still leave that window open.
>
> > nand_select_target(nand, 0);
> >
> > ret = macronix_30lfxg18ac_otp_enable(nand);
> > @@ -426,6 +427,7 @@ static int __macronix_30lfxg18ac_rw_otp(struct mtd_info *mtd,
> > write ? "write" : "read");
> >
> > nand_deselect_target(nand);
> > + nand_release_device(nand);
> >
> > return ret ? ret : cleanup_ret;
> > }
>
> ...and in the commit log you mention:
>
> > Make the existing NAND serialization helpers available within the raw
> > NAND core and hold them across the entire OTP transaction
>
> Why are you doing the serialization in one vendor driver then? Aren't
> all the vendors affected by the exact same issue? Why isn't taking the
> lock once for all in the core a better idea?
Macronix is the only in-tree raw NAND manufacturer that installs MTD
user-OTP read and write callbacks. Both use the same helper, so taking
the locks there covers both paths without changing the interface.
A core wrapper could take the chip and controller locks around a
manufacturer's OTP callback, much as nand_lock() does for
chip->ops.lock_area. That would give future implementations the same
locking, but it would mean adding raw NAND OTP hooks where MTD currently
calls the one implementation directly. Is that the approach you had in
mind?
>
> Miquèl