[PATCH 55/58] mtd: rawnand: r852: retain chip state across removal
James Hilliard
james.hilliard1 at gmail.com
Sat Nov 7 02:16:52 PST 2026
A removed card can still have open MTD handles. Reinitializing the same
nand_chip for a newly inserted card lets those handles access
replacement media, while freeing it during PCI removal leaves dangling
references.
Allocate a fresh chip for every insertion and separate SmartMedia
scanning from publication so lifetime ownership is installed first.
Disconnect the old chip before disabling its hardware, and retain it and
the controller until their final MTD reference is released. Stop
IRQ-driven card-work scheduling before destroying the workqueue during
PCI removal.
Signed-off-by: James Hilliard <james.hilliard1 at gmail.com>
---
drivers/mtd/nand/raw/r852.c | 111 ++++++++++++++++++++++++---------------
drivers/mtd/nand/raw/r852.h | 1 +
drivers/mtd/nand/raw/sm_common.c | 15 ++----
drivers/mtd/nand/raw/sm_common.h | 2 +-
4 files changed, 75 insertions(+), 54 deletions(-)
diff --git a/drivers/mtd/nand/raw/r852.c b/drivers/mtd/nand/raw/r852.c
index 92c47d351c27..0fb320cd7b77 100644
--- a/drivers/mtd/nand/raw/r852.c
+++ b/drivers/mtd/nand/raw/r852.c
@@ -625,16 +625,34 @@ static void r852_update_media_status(struct r852_device *dev)
spin_unlock_irqrestore(&dev->irqlock, flags);
}
-/*
- * Register the nand device
- * Called when the card is detected
- */
+static void r852_chip_release(struct nand_chip *chip)
+{
+ kfree(chip);
+}
+
+/* Register a new chip instance for each inserted card. */
static int r852_register_nand_device(struct r852_device *dev)
{
- struct mtd_info *mtd = nand_to_mtd(dev->chip);
+ struct nand_chip *chip;
+ struct mtd_info *mtd;
+ int ret;
WARN_ON(dev->card_registered);
+ chip = kzalloc_obj(*chip);
+ if (!chip)
+ goto error1;
+
+ chip->legacy.cmd_ctrl = r852_cmdctl;
+ chip->legacy.waitfunc = r852_wait;
+ chip->legacy.dev_ready = r852_ready;
+ chip->legacy.read_byte = r852_read_byte;
+ chip->legacy.read_buf = r852_read_buf;
+ chip->legacy.write_buf = r852_write_buf;
+ chip->controller = &dev->controller;
+ nand_set_controller_data(chip, dev);
+ dev->chip = chip;
+ mtd = nand_to_mtd(chip);
mtd->dev.parent = &dev->pci_dev->dev;
if (dev->readonly)
@@ -642,8 +660,17 @@ static int r852_register_nand_device(struct r852_device *dev)
r852_engine_enable(dev);
- if (sm_register_device(mtd, dev->sm))
- goto error1;
+ ret = sm_scan_device(mtd, dev->sm);
+ if (ret)
+ goto free_chip;
+ ret = nand_set_release(chip, r852_chip_release);
+ if (ret) {
+ nand_cleanup(chip);
+ goto free_chip;
+ }
+ ret = mtd_device_register(mtd, NULL, 0);
+ if (ret)
+ goto put_chip;
if (device_create_file(&mtd->dev, &dev_attr_media_type)) {
message("can't create media type sysfs attribute");
@@ -653,8 +680,17 @@ static int r852_register_nand_device(struct r852_device *dev)
dev->card_registered = 1;
return 0;
error3:
- WARN_ON(mtd_device_unregister(nand_to_mtd(dev->chip)));
- nand_cleanup(dev->chip);
+ WARN_ON(mtd_device_unregister(mtd));
+put_chip:
+ nand_disconnect(chip);
+ r852_engine_disable(dev);
+ dev->chip = NULL;
+ mtd_put_master(mtd);
+ goto error1;
+free_chip:
+ r852_engine_disable(dev);
+ dev->chip = NULL;
+ kfree(chip);
error1:
/* Force card redetect */
dev->card_detected = 0;
@@ -667,16 +703,19 @@ static int r852_register_nand_device(struct r852_device *dev)
static void r852_unregister_nand_device(struct r852_device *dev)
{
- struct mtd_info *mtd = nand_to_mtd(dev->chip);
+ struct mtd_info *mtd;
if (!dev->card_registered)
return;
+ mtd = nand_to_mtd(dev->chip);
device_remove_file(&mtd->dev, &dev_attr_media_type);
WARN_ON(mtd_device_unregister(mtd));
- nand_cleanup(dev->chip);
+ nand_disconnect(dev->chip);
r852_engine_disable(dev);
+ dev->chip = NULL;
dev->card_registered = 0;
+ mtd_put_master(mtd);
}
/* Card state updater */
@@ -734,7 +773,8 @@ static irqreturn_t r852_irq(int irq, void *data)
card_status = r852_read_reg(dev, R852_CARD_IRQ_STA);
r852_write_reg(dev, R852_CARD_IRQ_STA, card_status);
- if (card_status & (R852_CARD_IRQ_INSERT|R852_CARD_IRQ_REMOVE)) {
+ if (!dev->removing &&
+ (card_status & (R852_CARD_IRQ_INSERT | R852_CARD_IRQ_REMOVE))) {
ret = IRQ_HANDLED;
dev->card_detected = !!(card_status & R852_CARD_IRQ_INSERT);
@@ -841,10 +881,14 @@ static const struct nand_controller_ops r852_ops = {
.attach_chip = r852_attach_chip,
};
+static void r852_controller_release(struct nand_controller *controller)
+{
+ kfree(container_of(controller, struct r852_device, controller));
+}
+
static int r852_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
{
int error;
- struct nand_chip *chip;
struct r852_device *dev;
/* pci initialization */
@@ -866,42 +910,28 @@ static int r852_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
error = -ENOMEM;
- /* init nand chip, but register it only on card insert */
- chip = kzalloc_obj(struct nand_chip);
-
- if (!chip)
- goto error4;
-
- /* commands */
- chip->legacy.cmd_ctrl = r852_cmdctl;
- chip->legacy.waitfunc = r852_wait;
- chip->legacy.dev_ready = r852_ready;
-
- /* I/O */
- chip->legacy.read_byte = r852_read_byte;
- chip->legacy.read_buf = r852_read_buf;
- chip->legacy.write_buf = r852_write_buf;
-
/* init our device structure */
dev = kzalloc_obj(struct r852_device);
if (!dev)
- goto error5;
+ goto error4;
+ error = devm_nand_controller_init(&pci_dev->dev, &dev->controller,
+ r852_controller_release);
+ if (error)
+ goto error4;
- nand_set_controller_data(chip, dev);
- dev->chip = chip;
dev->pci_dev = pci_dev;
pci_set_drvdata(pci_dev, dev);
- nand_controller_init(&dev->controller);
dev->controller.ops = &r852_ops;
- chip->controller = &dev->controller;
dev->bounce_buffer = dma_alloc_coherent(&pci_dev->dev, R852_DMA_LEN,
&dev->phys_bounce_buffer, GFP_KERNEL);
- if (!dev->bounce_buffer)
+ if (!dev->bounce_buffer) {
+ error = -ENOMEM;
goto error6;
+ }
error = -ENODEV;
@@ -961,9 +991,6 @@ static int r852_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
dma_free_coherent(&pci_dev->dev, R852_DMA_LEN, dev->bounce_buffer,
dev->phys_bounce_buffer);
error6:
- kfree(dev);
-error5:
- kfree(chip);
error4:
pci_release_regions(pci_dev);
error3:
@@ -976,6 +1003,11 @@ static int r852_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
static void r852_remove(struct pci_dev *pci_dev)
{
struct r852_device *dev = pci_get_drvdata(pci_dev);
+ unsigned long flags;
+
+ spin_lock_irqsave(&dev->irqlock, flags);
+ dev->removing = true;
+ spin_unlock_irqrestore(&dev->irqlock, flags);
/* Stop detect workqueue -
we are going to unregister the device anyway*/
@@ -995,9 +1027,6 @@ static void r852_remove(struct pci_dev *pci_dev)
dma_free_coherent(&pci_dev->dev, R852_DMA_LEN, dev->bounce_buffer,
dev->phys_bounce_buffer);
- kfree(dev->chip);
- kfree(dev);
-
/* Shutdown the PCI device */
pci_release_regions(pci_dev);
pci_disable_device(pci_dev);
diff --git a/drivers/mtd/nand/raw/r852.h b/drivers/mtd/nand/raw/r852.h
index 96fe301d15da..a89ab61ea51e 100644
--- a/drivers/mtd/nand/raw/r852.h
+++ b/drivers/mtd/nand/raw/r852.h
@@ -128,6 +128,7 @@ struct r852_device {
struct delayed_work card_detect_work;
struct workqueue_struct *card_workqueue;
int card_registered; /* card registered with mtd */
+ bool removing; /* stop card work during PCI removal */
int card_detected; /* card detected in slot */
int card_unstable; /* whenever the card is inserted,
is not known yet */
diff --git a/drivers/mtd/nand/raw/sm_common.c b/drivers/mtd/nand/raw/sm_common.c
index e238784c8c3e..a79e3c3c77d1 100644
--- a/drivers/mtd/nand/raw/sm_common.c
+++ b/drivers/mtd/nand/raw/sm_common.c
@@ -182,28 +182,19 @@ static const struct nand_controller_ops sm_controller_ops = {
.attach_chip = sm_attach_chip,
};
-int sm_register_device(struct mtd_info *mtd, int smartmedia)
+int sm_scan_device(struct mtd_info *mtd, int smartmedia)
{
struct nand_chip *chip = mtd_to_nand(mtd);
struct nand_flash_dev *flash_ids;
- int ret;
chip->options |= NAND_SKIP_BBTSCAN;
/* Scan for card properties */
chip->legacy.dummy_controller.ops = &sm_controller_ops;
flash_ids = smartmedia ? nand_smartmedia_flash_ids : nand_xd_flash_ids;
- ret = nand_scan_with_ids(chip, 1, flash_ids);
- if (ret)
- return ret;
-
- ret = mtd_device_register(mtd, NULL, 0);
- if (ret)
- nand_cleanup(chip);
-
- return ret;
+ return nand_scan_with_ids(chip, 1, flash_ids);
}
-EXPORT_SYMBOL_GPL(sm_register_device);
+EXPORT_SYMBOL_GPL(sm_scan_device);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Maxim Levitsky <maximlevitsky at gmail.com>");
diff --git a/drivers/mtd/nand/raw/sm_common.h b/drivers/mtd/nand/raw/sm_common.h
index 57fc9f86f9ee..d8a330d6b730 100644
--- a/drivers/mtd/nand/raw/sm_common.h
+++ b/drivers/mtd/nand/raw/sm_common.h
@@ -33,7 +33,7 @@ struct sm_oob {
#define SM_SMALL_OOB_SIZE 8
-int sm_register_device(struct mtd_info *mtd, int smartmedia);
+int sm_scan_device(struct mtd_info *mtd, int smartmedia);
static inline int sm_sector_valid(struct sm_oob *oob)
--
2.53.0
More information about the linux-mtd
mailing list