mtd: spi-nor: Reorder the preparation vs. locking steps
authorMiquel Raynal <miquel.raynal@bootlin.com>
Tue, 28 Mar 2023 15:41:00 +0000 (17:41 +0200)
committerTudor Ambarus <tudor.ambarus@linaro.org>
Wed, 29 Mar 2023 10:46:07 +0000 (13:46 +0300)
The ->prepare()/->unprepare() hooks are now legacy, we no longer accept
new drivers supporting them. The only remaining controllers using them
acquires a per-chip mutex, which should not interfere with the rest of
the operation done in the core. As a result, we should be safe to
reorganize these helpers to first perform the preparation, before
acquiring the core locks. This is necessary in order to be able to
improve the locking mechanism in the core (coming next). No side effects
are expected.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/r/20230328154105.448540-4-miquel.raynal@bootlin.com
Signed-off-by: Tudor Ambarus <tudor.ambarus@linaro.org>
drivers/mtd/spi-nor/core.c
drivers/mtd/spi-nor/core.h
drivers/mtd/spi-nor/otp.c
drivers/mtd/spi-nor/sst.c
drivers/mtd/spi-nor/swp.c

index 7d9ca799f76725d24b84246d704055363b54855b..3a7a407919e7375ffe6935cf83732c992fb1d3e7 100644 (file)
@@ -1070,27 +1070,24 @@ static void spi_nor_set_4byte_opcodes(struct spi_nor *nor)
        }
 }
 
-int spi_nor_lock_and_prep(struct spi_nor *nor)
+int spi_nor_prep_and_lock(struct spi_nor *nor)
 {
        int ret = 0;
 
+       if (nor->controller_ops && nor->controller_ops->prepare)
+               ret = nor->controller_ops->prepare(nor);
+
        mutex_lock(&nor->lock);
 
-       if (nor->controller_ops &&  nor->controller_ops->prepare) {
-               ret = nor->controller_ops->prepare(nor);
-               if (ret) {
-                       mutex_unlock(&nor->lock);
-                       return ret;
-               }
-       }
        return ret;
 }
 
 void spi_nor_unlock_and_unprep(struct spi_nor *nor)
 {
+       mutex_unlock(&nor->lock);
+
        if (nor->controller_ops && nor->controller_ops->unprepare)
                nor->controller_ops->unprepare(nor);
-       mutex_unlock(&nor->lock);
 }
 
 static u32 spi_nor_convert_addr(struct spi_nor *nor, loff_t addr)
@@ -1446,7 +1443,7 @@ static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
        addr = instr->addr;
        len = instr->len;
 
-       ret = spi_nor_lock_and_prep(nor);
+       ret = spi_nor_prep_and_lock(nor);
        if (ret)
                return ret;
 
@@ -1706,7 +1703,7 @@ static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
 
        dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
 
-       ret = spi_nor_lock_and_prep(nor);
+       ret = spi_nor_prep_and_lock(nor);
        if (ret)
                return ret;
 
@@ -1752,7 +1749,7 @@ static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
 
        dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
 
-       ret = spi_nor_lock_and_prep(nor);
+       ret = spi_nor_prep_and_lock(nor);
        if (ret)
                return ret;
 
index bf2e64671856d92465a21e50aab27c7219dd9b64..c4c78729cceae7b005658b1f5fa693be5beef6a4 100644 (file)
@@ -647,7 +647,7 @@ int spi_nor_write_disable(struct spi_nor *nor);
 int spi_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable);
 int spi_nor_wait_till_ready(struct spi_nor *nor);
 int spi_nor_global_block_unlock(struct spi_nor *nor);
-int spi_nor_lock_and_prep(struct spi_nor *nor);
+int spi_nor_prep_and_lock(struct spi_nor *nor);
 void spi_nor_unlock_and_unprep(struct spi_nor *nor);
 int spi_nor_sr1_bit6_quad_enable(struct spi_nor *nor);
 int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor);
index 00ab0d2d6d2fe5d00811f2f0b7c31f1d538dbd6c..9a729aa3452d23b09a04b08aba7a7b8064525bc8 100644 (file)
@@ -255,7 +255,7 @@ static int spi_nor_mtd_otp_info(struct mtd_info *mtd, size_t len,
        if (len < n_regions * sizeof(*buf))
                return -ENOSPC;
 
-       ret = spi_nor_lock_and_prep(nor);
+       ret = spi_nor_prep_and_lock(nor);
        if (ret)
                return ret;
 
@@ -325,7 +325,7 @@ static int spi_nor_mtd_otp_read_write(struct mtd_info *mtd, loff_t ofs,
        if (!total_len)
                return 0;
 
-       ret = spi_nor_lock_and_prep(nor);
+       ret = spi_nor_prep_and_lock(nor);
        if (ret)
                return ret;
 
@@ -415,7 +415,7 @@ static int spi_nor_mtd_otp_erase(struct mtd_info *mtd, loff_t from, size_t len)
        if (!IS_ALIGNED(len, rlen) || !IS_ALIGNED(from, rlen))
                return -EINVAL;
 
-       ret = spi_nor_lock_and_prep(nor);
+       ret = spi_nor_prep_and_lock(nor);
        if (ret)
                return ret;
 
@@ -460,7 +460,7 @@ static int spi_nor_mtd_otp_lock(struct mtd_info *mtd, loff_t from, size_t len)
        if (!IS_ALIGNED(len, rlen) || !IS_ALIGNED(from, rlen))
                return -EINVAL;
 
-       ret = spi_nor_lock_and_prep(nor);
+       ret = spi_nor_prep_and_lock(nor);
        if (ret)
                return ret;
 
index 63bcc97bf978f711a070621a250d2952fe0b3fb5..688eb20c763e9b636386fe8bf4a1a9509f78b244 100644 (file)
@@ -126,7 +126,7 @@ static int sst_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
 
        dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
 
-       ret = spi_nor_lock_and_prep(nor);
+       ret = spi_nor_prep_and_lock(nor);
        if (ret)
                return ret;
 
index 1f178313ba8fcdd0450665840df9d971373f3826..0ba716e84377fbf79ee276a5e4e83c242098731d 100644 (file)
@@ -348,7 +348,7 @@ static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
        struct spi_nor *nor = mtd_to_spi_nor(mtd);
        int ret;
 
-       ret = spi_nor_lock_and_prep(nor);
+       ret = spi_nor_prep_and_lock(nor);
        if (ret)
                return ret;
 
@@ -363,7 +363,7 @@ static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
        struct spi_nor *nor = mtd_to_spi_nor(mtd);
        int ret;
 
-       ret = spi_nor_lock_and_prep(nor);
+       ret = spi_nor_prep_and_lock(nor);
        if (ret)
                return ret;
 
@@ -378,7 +378,7 @@ static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
        struct spi_nor *nor = mtd_to_spi_nor(mtd);
        int ret;
 
-       ret = spi_nor_lock_and_prep(nor);
+       ret = spi_nor_prep_and_lock(nor);
        if (ret)
                return ret;