mtd: spi-nor: simplify write loop
Linux-MTD Mailing List
linux-mtd at lists.infradead.org
Mon Aug 1 18:59:02 PDT 2016
Gitweb: http://git.infradead.org/?p=mtd-2.6.git;a=commit;h=e5d05cbd6d8b01f08c95c427a36c66aac769af4f
Commit: e5d05cbd6d8b01f08c95c427a36c66aac769af4f
Parent: 2dd087b16946cf168f401526adf26afa771bb740
Author: Michal Suchanek <hramrach at gmail.com>
AuthorDate: Thu May 5 17:31:54 2016 -0700
Committer: Brian Norris <computersforpeace at gmail.com>
CommitDate: Wed Jun 1 17:22:57 2016 -0700
mtd: spi-nor: simplify write loop
The spi-nor write loop assumes that what is passed to the hardware
driver write() is what gets written.
When write() writes less than page size at once data is dropped on the
floor. Check the amount of data writen and exit if it does not match
requested amount.
Signed-off-by: Michal Suchanek <hramrach at gmail.com>
Signed-off-by: Brian Norris <computersforpeace at gmail.com>
Tested-by Cyrille Pitchen <cyrille.pitchen at atmel.com>
Acked-by: Michal Suchanek <hramrach at gmail.com>
Tested-by: Michal Suchanek <hramrach at gmail.com>
---
drivers/mtd/spi-nor/spi-nor.c | 58 +++++++++++++++++++------------------------
1 file changed, 25 insertions(+), 33 deletions(-)
diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c
index 34dd953..fe55b48 100644
--- a/drivers/mtd/spi-nor/spi-nor.c
+++ b/drivers/mtd/spi-nor/spi-nor.c
@@ -1129,8 +1129,8 @@ static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
size_t *retlen, const u_char *buf)
{
struct spi_nor *nor = mtd_to_spi_nor(mtd);
- u32 page_offset, page_size, i;
- int ret;
+ size_t page_offset, page_remain, i;
+ ssize_t ret;
dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
@@ -1138,45 +1138,37 @@ static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
if (ret)
return ret;
- write_enable(nor);
-
- page_offset = to & (nor->page_size - 1);
+ for (i = 0; i < len; ) {
+ ssize_t written;
- /* do all the bytes fit onto one page? */
- if (page_offset + len <= nor->page_size) {
- ret = nor->write(nor, to, len, buf);
- if (ret < 0)
- goto write_err;
- *retlen += ret;
- } else {
+ page_offset = (to + i) & (nor->page_size - 1);
+ WARN_ONCE(page_offset,
+ "Writing at offset %zu into a NOR page. Writing partial pages may decrease reliability and increase wear of NOR flash.",
+ page_offset);
/* the size of data remaining on the first page */
- page_size = nor->page_size - page_offset;
- ret = nor->write(nor, to, page_size, buf);
+ page_remain = min_t(size_t,
+ nor->page_size - page_offset, len - i);
+
+ write_enable(nor);
+ ret = nor->write(nor, to + i, page_remain, buf + i);
if (ret < 0)
goto write_err;
- *retlen += ret;
-
- /* write everything in nor->page_size chunks */
- for (i = ret; i < len; ) {
- page_size = len - i;
- if (page_size > nor->page_size)
- page_size = nor->page_size;
-
- ret = spi_nor_wait_till_ready(nor);
- if (ret)
- goto write_err;
+ written = ret;
- write_enable(nor);
-
- ret = nor->write(nor, to + i, page_size, buf + i);
- if (ret < 0)
- goto write_err;
- *retlen += ret;
- i += ret;
+ ret = spi_nor_wait_till_ready(nor);
+ if (ret)
+ goto write_err;
+ *retlen += written;
+ i += written;
+ if (written != page_remain) {
+ dev_err(nor->dev,
+ "While writing %zu bytes written %zd bytes\n",
+ page_remain, written);
+ ret = -EIO;
+ goto write_err;
}
}
- ret = spi_nor_wait_till_ready(nor);
write_err:
spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
return ret;
More information about the linux-mtd-cvs
mailing list