* [PATCH v4 0/2] mtd: rawnand: serialize OTP and BBT recovery operations
@ 2026-12-04 22:28 James Hilliard
2026-12-04 22:28 ` [PATCH v4 1/2] mtd: rawnand: serialize OTP operations in the core James Hilliard
2026-12-04 22:28 ` [PATCH v4 2/2] mtd: rawnand: serialize bad-block marker writes during BBT recovery James Hilliard
0 siblings, 2 replies; 3+ messages in thread
From: James Hilliard @ 2026-12-04 22:28 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Arseniy Krasnov, Kyle Roeschley, Boris Brezillon
Cc: linux-mtd, linux-kernel, James Hilliard, stable
Macronix OTP reads and writes switch the chip into a special mode.
Another NAND operation can reach the chip before it leaves that mode.
Run OTP callbacks under the same chip and controller locks as normal
I/O, including the wait for a suspended chip to resume.
Bad-block table recovery also issues NAND commands outside the normal
read/write wrappers. Take the same locks when writing a bad-block marker
after a table write or erase fails.
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
Changes in v4:
- Rebase on the accepted Macronix OTP error fix and drop the external
prerequisite.
- Link to v3: https://patch.msgid.link/20261126-submit-rawnand-operation-locking-v1-v3-0-cd3794332f23@gmail.com
Changes in v3:
- Run raw NAND OTP callbacks under core-managed chip/controller locking
and resume handling; keep target selection in the device-specific
callbacks.
- Keep helper visibility changes with the BBT recovery patch that uses
them.
- Link to v2: https://patch.msgid.link/20261122-submit-rawnand-operation-locking-v1-v2-0-030bc867f0c7@gmail.com
Changes in v2:
- Rebase on nand/next after the BBT logging change.
- Apply the Macronix locking change on top of the posted OTP error
propagation fix; both touch the OTP cleanup path.
- Link to v1: https://patch.msgid.link/20261108-submit-rawnand-operation-locking-v1-v1-0-565010fc5f8b@gmail.com
---
James Hilliard (2):
mtd: rawnand: serialize OTP operations in the core
mtd: rawnand: serialize bad-block marker writes during BBT recovery
drivers/mtd/nand/raw/internals.h | 2 +
drivers/mtd/nand/raw/nand_base.c | 110 ++++++++++++++++++++++++++++++++++-
drivers/mtd/nand/raw/nand_bbt.c | 2 +
drivers/mtd/nand/raw/nand_macronix.c | 32 +++++-----
include/linux/mtd/rawnand.h | 24 ++++++++
5 files changed, 151 insertions(+), 19 deletions(-)
---
base-commit: dab547f4666f6649360053a95104b8c4480f46a8
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git nand/next
change-id: 20261108-submit-rawnand-operation-locking-v1-1a0b8fe786d4
Best regards,
--
James Hilliard <james.hilliard1@gmail.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v4 1/2] mtd: rawnand: serialize OTP operations in the core
2026-12-04 22:28 [PATCH v4 0/2] mtd: rawnand: serialize OTP and BBT recovery operations James Hilliard
@ 2026-12-04 22:28 ` James Hilliard
2026-12-04 22:28 ` [PATCH v4 2/2] mtd: rawnand: serialize bad-block marker writes during BBT recovery James Hilliard
1 sibling, 0 replies; 3+ messages in thread
From: James Hilliard @ 2026-12-04 22:28 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Arseniy Krasnov, Kyle Roeschley, Boris Brezillon
Cc: linux-mtd, linux-kernel, James Hilliard, stable
OTP operations on Macronix MX30LFxG18AC chips switch the device into a
dedicated mode before reading or programming pages. Their protection-
register MTD callbacks bypass the raw NAND read/write wrappers. A
concurrent NAND operation can therefore access the device while OTP
mode is active, and OTP access can bypass the normal wait for a
suspended chip.
Add OTP callbacks to nand_chip_ops and route raw NAND protection-
register requests through core wrappers. The wrappers hold the chip and
controller locks around each callback, so normal NAND I/O cannot
interleave with OTP access. If the chip is suspended, OTP access waits
for nand_resume() to clear the suspended flag.
Device-specific callbacks retain target selection and OTP mode handling.
Fixes: e87161321a40 ("mtd: rawnand: macronix: OTP access for MX30LFxG18AC")
Cc: stable@vger.kernel.org
Cc: Arseniy Krasnov <AVKrasnov@sberdevices.ru>
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/nand/raw/nand_base.c | 106 +++++++++++++++++++++++++++++++++++
drivers/mtd/nand/raw/nand_macronix.c | 32 +++++------
include/linux/mtd/rawnand.h | 24 ++++++++
3 files changed, 145 insertions(+), 17 deletions(-)
diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c
index 58dbcb004a62..5c877e2536e4 100644
--- a/drivers/mtd/nand/raw/nand_base.c
+++ b/drivers/mtd/nand/raw/nand_base.c
@@ -6180,6 +6180,98 @@ static const struct nand_ops rawnand_ops = {
.isbad = rawnand_isbad,
};
+static int nand_get_fact_prot_info(struct mtd_info *mtd, size_t len,
+ size_t *retlen, struct otp_info *buf)
+{
+ struct nand_chip *chip = mtd_to_nand(mtd);
+ int ret;
+
+ nand_get_device(chip);
+ ret = chip->ops.get_fact_prot_info(chip, len, retlen, buf);
+ nand_release_device(chip);
+
+ return ret;
+}
+
+static int nand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
+ size_t len, size_t *retlen, u_char *buf)
+{
+ struct nand_chip *chip = mtd_to_nand(mtd);
+ int ret;
+
+ nand_get_device(chip);
+ ret = chip->ops.read_fact_prot_reg(chip, from, len, retlen, buf);
+ nand_release_device(chip);
+
+ return ret;
+}
+
+static int nand_get_user_prot_info(struct mtd_info *mtd, size_t len,
+ size_t *retlen, struct otp_info *buf)
+{
+ struct nand_chip *chip = mtd_to_nand(mtd);
+ int ret;
+
+ nand_get_device(chip);
+ ret = chip->ops.get_user_prot_info(chip, len, retlen, buf);
+ nand_release_device(chip);
+
+ return ret;
+}
+
+static int nand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
+ size_t len, size_t *retlen, u_char *buf)
+{
+ struct nand_chip *chip = mtd_to_nand(mtd);
+ int ret;
+
+ nand_get_device(chip);
+ ret = chip->ops.read_user_prot_reg(chip, from, len, retlen, buf);
+ nand_release_device(chip);
+
+ return ret;
+}
+
+static int nand_write_user_prot_reg(struct mtd_info *mtd, loff_t to,
+ size_t len, size_t *retlen,
+ const u_char *buf)
+{
+ struct nand_chip *chip = mtd_to_nand(mtd);
+ int ret;
+
+ nand_get_device(chip);
+ ret = chip->ops.write_user_prot_reg(chip, to, len, retlen, buf);
+ nand_release_device(chip);
+
+ return ret;
+}
+
+static int nand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
+ size_t len)
+{
+ struct nand_chip *chip = mtd_to_nand(mtd);
+ int ret;
+
+ nand_get_device(chip);
+ ret = chip->ops.lock_user_prot_reg(chip, from, len);
+ nand_release_device(chip);
+
+ return ret;
+}
+
+static int nand_erase_user_prot_reg(struct mtd_info *mtd, loff_t from,
+ size_t len)
+{
+ struct nand_chip *chip = mtd_to_nand(mtd);
+ int ret;
+
+ nand_get_device(chip);
+ ret = chip->ops.erase_user_prot_reg(chip, from, len);
+ nand_release_device(chip);
+
+ return ret;
+}
+
/**
* nand_scan_tail - Scan for the NAND device
* @chip: NAND chip object
@@ -6443,6 +6535,20 @@ static int nand_scan_tail(struct nand_chip *chip)
mtd->_block_isbad = nand_block_isbad;
mtd->_block_markbad = nand_block_markbad;
mtd->_max_bad_blocks = nanddev_mtd_max_bad_blocks;
+ if (chip->ops.get_fact_prot_info)
+ mtd->_get_fact_prot_info = nand_get_fact_prot_info;
+ if (chip->ops.read_fact_prot_reg)
+ mtd->_read_fact_prot_reg = nand_read_fact_prot_reg;
+ if (chip->ops.get_user_prot_info)
+ mtd->_get_user_prot_info = nand_get_user_prot_info;
+ if (chip->ops.read_user_prot_reg)
+ mtd->_read_user_prot_reg = nand_read_user_prot_reg;
+ if (chip->ops.write_user_prot_reg)
+ mtd->_write_user_prot_reg = nand_write_user_prot_reg;
+ if (chip->ops.lock_user_prot_reg)
+ mtd->_lock_user_prot_reg = nand_lock_user_prot_reg;
+ if (chip->ops.erase_user_prot_reg)
+ mtd->_erase_user_prot_reg = nand_erase_user_prot_reg;
/*
* Initialize bitflip_threshold to its default prior scan_bbt() call.
diff --git a/drivers/mtd/nand/raw/nand_macronix.c b/drivers/mtd/nand/raw/nand_macronix.c
index 16847f28b19d..30e42594daec 100644
--- a/drivers/mtd/nand/raw/nand_macronix.c
+++ b/drivers/mtd/nand/raw/nand_macronix.c
@@ -326,7 +326,7 @@ static void macronix_nand_deep_power_down_support(struct nand_chip *chip)
chip->ops.resume = mxic_nand_resume;
}
-static int macronix_30lfxg18ac_get_otp_info(struct mtd_info *mtd, size_t len,
+static int macronix_30lfxg18ac_get_otp_info(struct nand_chip *nand, size_t len,
size_t *retlen,
struct otp_info *buf)
{
@@ -368,18 +368,16 @@ static int macronix_30lfxg18ac_otp_disable(struct nand_chip *nand)
feature_buf);
}
-static int __macronix_30lfxg18ac_rw_otp(struct mtd_info *mtd,
+static int __macronix_30lfxg18ac_rw_otp(struct nand_chip *nand,
loff_t offs_in_flash,
size_t len, size_t *retlen,
u_char *buf, bool write)
{
- struct nand_chip *nand;
size_t bytes_handled;
off_t offs_in_page;
u64 page;
int ret, cleanup_ret;
- nand = mtd_to_nand(mtd);
nand_select_target(nand, 0);
ret = macronix_30lfxg18ac_otp_enable(nand);
@@ -418,11 +416,13 @@ static int __macronix_30lfxg18ac_rw_otp(struct mtd_info *mtd,
out_otp:
if (ret)
- dev_err(&mtd->dev, "failed to perform OTP IO: %i\n", ret);
+ dev_err(&nand_to_mtd(nand)->dev,
+ "failed to perform OTP IO: %i\n", ret);
cleanup_ret = macronix_30lfxg18ac_otp_disable(nand);
if (cleanup_ret)
- dev_err(&mtd->dev, "failed to leave OTP mode after %s\n",
+ dev_err(&nand_to_mtd(nand)->dev,
+ "failed to leave OTP mode after %s\n",
write ? "write" : "read");
nand_deselect_target(nand);
@@ -430,22 +430,22 @@ static int __macronix_30lfxg18ac_rw_otp(struct mtd_info *mtd,
return ret ? ret : cleanup_ret;
}
-static int macronix_30lfxg18ac_write_otp(struct mtd_info *mtd, loff_t to,
+static int macronix_30lfxg18ac_write_otp(struct nand_chip *nand, loff_t to,
size_t len, size_t *rlen,
const u_char *buf)
{
- return __macronix_30lfxg18ac_rw_otp(mtd, to, len, rlen, (u_char *)buf,
+ return __macronix_30lfxg18ac_rw_otp(nand, to, len, rlen, (u_char *)buf,
true);
}
-static int macronix_30lfxg18ac_read_otp(struct mtd_info *mtd, loff_t from,
+static int macronix_30lfxg18ac_read_otp(struct nand_chip *nand, loff_t from,
size_t len, size_t *rlen,
u_char *buf)
{
- return __macronix_30lfxg18ac_rw_otp(mtd, from, len, rlen, buf, false);
+ return __macronix_30lfxg18ac_rw_otp(nand, from, len, rlen, buf, false);
}
-static int macronix_30lfxg18ac_lock_otp(struct mtd_info *mtd, loff_t from,
+static int macronix_30lfxg18ac_lock_otp(struct nand_chip *nand, loff_t from,
size_t len)
{
/* See comment in 'macronix_30lfxg18ac_get_otp_info()'. */
@@ -459,7 +459,6 @@ static void macronix_nand_setup_otp(struct nand_chip *chip)
"MX30LF2G18AC",
"MX30LF4G18AC",
};
- struct mtd_info *mtd;
if (match_string(supported_otp_models,
ARRAY_SIZE(supported_otp_models),
@@ -474,11 +473,10 @@ static void macronix_nand_setup_otp(struct nand_chip *chip)
bitmap_set(chip->parameters.set_feature_list,
ONFI_FEATURE_ADDR_30LFXG18AC_OTP, 1);
- mtd = nand_to_mtd(chip);
- mtd->_get_user_prot_info = macronix_30lfxg18ac_get_otp_info;
- mtd->_read_user_prot_reg = macronix_30lfxg18ac_read_otp;
- mtd->_write_user_prot_reg = macronix_30lfxg18ac_write_otp;
- mtd->_lock_user_prot_reg = macronix_30lfxg18ac_lock_otp;
+ chip->ops.get_user_prot_info = macronix_30lfxg18ac_get_otp_info;
+ chip->ops.read_user_prot_reg = macronix_30lfxg18ac_read_otp;
+ chip->ops.write_user_prot_reg = macronix_30lfxg18ac_write_otp;
+ chip->ops.lock_user_prot_reg = macronix_30lfxg18ac_lock_otp;
}
static int macronix_nand_init(struct nand_chip *chip)
diff --git a/include/linux/mtd/rawnand.h b/include/linux/mtd/rawnand.h
index 43fefff02025..b08aaadf133c 100644
--- a/include/linux/mtd/rawnand.h
+++ b/include/linux/mtd/rawnand.h
@@ -1191,6 +1191,16 @@ struct nand_legacy {
* @unlock_area: Unlock operation
* @setup_read_retry: Set the read-retry mode (mostly needed for MLC NANDs)
* @choose_interface_config: Choose the best interface configuration
+ * @get_fact_prot_info: Get information about the factory protection area
+ * @read_fact_prot_reg: Read the factory protection area
+ * @get_user_prot_info: Get information about the user protection area
+ * @read_user_prot_reg: Read the user protection area
+ * @write_user_prot_reg: Write the user protection area
+ * @lock_user_prot_reg: Lock the user protection area
+ * @erase_user_prot_reg: Erase the user protection area
+ *
+ * Protection-register callbacks are called with the chip and controller
+ * locked. They select and deselect the target as required.
*/
struct nand_chip_ops {
int (*suspend)(struct nand_chip *chip);
@@ -1200,6 +1210,20 @@ struct nand_chip_ops {
int (*setup_read_retry)(struct nand_chip *chip, int retry_mode);
int (*choose_interface_config)(struct nand_chip *chip,
struct nand_interface_config *iface);
+ int (*get_fact_prot_info)(struct nand_chip *chip, size_t len,
+ size_t *retlen, struct otp_info *buf);
+ int (*read_fact_prot_reg)(struct nand_chip *chip, loff_t from,
+ size_t len, size_t *retlen, u_char *buf);
+ int (*get_user_prot_info)(struct nand_chip *chip, size_t len,
+ size_t *retlen, struct otp_info *buf);
+ int (*read_user_prot_reg)(struct nand_chip *chip, loff_t from,
+ size_t len, size_t *retlen, u_char *buf);
+ int (*write_user_prot_reg)(struct nand_chip *chip, loff_t to,
+ size_t len, size_t *retlen, const u_char *buf);
+ int (*lock_user_prot_reg)(struct nand_chip *chip, loff_t from,
+ size_t len);
+ int (*erase_user_prot_reg)(struct nand_chip *chip, loff_t from,
+ size_t len);
};
/**
--
2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v4 2/2] mtd: rawnand: serialize bad-block marker writes during BBT recovery
2026-12-04 22:28 [PATCH v4 0/2] mtd: rawnand: serialize OTP and BBT recovery operations James Hilliard
2026-12-04 22:28 ` [PATCH v4 1/2] mtd: rawnand: serialize OTP operations in the core James Hilliard
@ 2026-12-04 22:28 ` James Hilliard
1 sibling, 0 replies; 3+ messages in thread
From: James Hilliard @ 2026-12-04 22:28 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Arseniy Krasnov, Kyle Roeschley, Boris Brezillon
Cc: linux-mtd, linux-kernel, James Hilliard, stable
When erasing or writing a flash-based BBT fails, write_bbt() marks the
failed block bad before trying another reserved block. The erase and
write helpers release the NAND locks before returning, but
mark_bbt_block_bad() calls nand_markbad_bbm() without acquiring them.
A BBT update triggered by a bad-block marking request can therefore
issue the recovery marker commands concurrently with other NAND I/O or
while the chip is suspended. The chip selection and shared page/OOB
buffers are not protected on this path.
Take the chip and controller locks around the recovery marker write,
as nand_block_markbad_lowlevel() already does for normal marker writes.
Expose the raw NAND lock helpers through the internal header for this
cross-file use.
Fixes: 10ffd570f117 ("mtd: nand_bbt: scan for next free bbt block if writing bbt fails")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/nand/raw/internals.h | 2 ++
drivers/mtd/nand/raw/nand_base.c | 4 ++--
drivers/mtd/nand/raw/nand_bbt.c | 2 ++
3 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/drivers/mtd/nand/raw/internals.h b/drivers/mtd/nand/raw/internals.h
index b8b29341b88c..2668f527e637 100644
--- a/drivers/mtd/nand/raw/internals.h
+++ b/drivers/mtd/nand/raw/internals.h
@@ -81,6 +81,8 @@ extern const struct nand_manufacturer_ops toshiba_nand_manuf_ops;
extern const struct mtd_pairing_scheme dist3_pairing_scheme;
/* Core functions */
+void nand_get_device(struct nand_chip *chip);
+void nand_release_device(struct nand_chip *chip);
const struct nand_manufacturer_desc *nand_get_manufacturer_desc(u8 id);
int nand_bbm_get_next_page(struct nand_chip *chip, int page);
int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs);
diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c
index 5c877e2536e4..886512cf87bc 100644
--- 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)
{
/* 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_bbt.c b/drivers/mtd/nand/raw/nand_bbt.c
index a18e10284b9f..a56597dfcea0 100644
--- a/drivers/mtd/nand/raw/nand_bbt.c
+++ b/drivers/mtd/nand/raw/nand_bbt.c
@@ -721,10 +721,12 @@ static void mark_bbt_block_bad(struct nand_chip *this,
loff_t to;
int res;
+ nand_get_device(this);
bbt_mark_entry(this, block, BBT_BLOCK_WORN);
to = (loff_t)block << this->bbt_erase_shift;
res = nand_markbad_bbm(this, to);
+ nand_release_device(this);
if (res)
pr_warn("error %d while marking block %d bad\n",
res, block);
--
2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-04 22:41 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-04 22:28 [PATCH v4 0/2] mtd: rawnand: serialize OTP and BBT recovery operations James Hilliard
2026-12-04 22:28 ` [PATCH v4 1/2] mtd: rawnand: serialize OTP operations in the core James Hilliard
2026-12-04 22:28 ` [PATCH v4 2/2] mtd: rawnand: serialize bad-block marker writes during BBT recovery James Hilliard
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®