mtd: rawnand: serialize OTP and BBT recovery operations
Subsystems (3)
v3 cover mtd: rawnand: serialize OTP and BBT recovery operations
Route raw NAND OTP callbacks through core wrappers that hold the chip and controller locks, and protect marker writes performed by BBT failure recovery. Both paths issue NAND commands outside the ordinary MTD read/write wrappers, so they need the same serialization and suspend handling as normal operations.
The first patch registers manufacturer OTP operations with the raw NAND core, which invokes them under the normal locks. The second uses those locks around BBT recovery marker writes.
---
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: ec1e844b9486302a488abc03ea918db60709766e
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
prerequisite-message-id: <20261108-submit-macronix-otp-errors-v1-v1-1-8f64a2ad5a10@gmail.com>
prerequisite-patch-id: a542c9cd698d2d4c090932b8d74dc6f520a71166
Best regards,
Signature · 1 line
James Hilliard <[email protected]>
PATCH v3 1/2 mtd: rawnand: serialize OTP operations in the core
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.
--- 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(-)
drivers/mtd/nand/raw/nand_base.c +106 -0
@@ -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.
drivers/mtd/nand/raw/nand_macronix.c +15 -17
@@ -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)
include/linux/mtd/rawnand.h +24 -0
@@ -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); };/**
Signature · 1 line
2.53.0
PATCH v3 2/2 mtd: rawnand: serialize bad-block marker writes during BBT recovery
In reply to James HilliardWhen 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.
--- 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(-)
drivers/mtd/nand/raw/internals.h +2 -0
@@ -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);
drivers/mtd/nand/raw/nand_base.c +2 -2
@@ -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 ) {
drivers/mtd/nand/raw/nand_bbt.c +2 -0
@@ -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);
Signature · 1 line
2.53.0