Skip to contentSkip to conversation

mtd: spi-nor: handle controller driver limitations in spi_nor_read

PATCH In review Heiner Kallweit 2 participants 3 messages Oct 6, 2015 – Nov 20, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: spi-nor: handle controller driver limitations in spi_nor_read

Back in May there was a discussion on how to handle controller driver limitations like max message size (e.g. spi-fsl-espi supports message sizes up to 64Kb only).
According to Brian extending the API might not be needed and a better handling of the returned actual_length of the message should be sufficient.

Following the suggestion this patch supports reading chunks. As long as something was read also errors (like -EMSGSIZE) are ignored.

Afterwards related hacks can be removed from affected controller drivers, making them work with any protocol driver.
Currently e.g. spi-fsl-espi implicitely assumes that longer reads come from m25p80 protocol driver (if second transfer in message is a read and first transfer is a send then bytes 2-4 of the send are assumed to be a 3 byte address of a flash read).

---
 drivers/mtd/spi-nor/spi-nor.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/mtd/spi-nor/spi-nor.c +14 -1
index 4be41fb..17b5239 100644
@@ -761,6 +761,7 @@ static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
 			size_t *retlen, u_char *buf)
 {
 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
+	size_t bytes_read, pos = 0;
 	int ret;
 
 	dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
@@ -769,9 +770,21 @@ static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
 	if (ret)
 		return ret;
 
-	ret = nor->read(nor, from, len, retlen, buf);
+	/* Consider the case that a controller driver is not able
+	 * to read all requested bytes at once. It might return
+	 * an error and the number of bytes read.
+	 * Therefore ignore errors as long as something was read.
+	 */
+	do {
+		bytes_read = 0;
+		ret = nor->read(nor, from + pos, len - pos,
+				&bytes_read, buf + pos);
+		pos += bytes_read;
+	} while (pos < len && bytes_read);
 
 	spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
+
+	*retlen = pos;
 	return ret;
 }
 
Signature · 1 line
2.6.0
#1 lore raw

Any feedback on this patch and the two others I sent on Oct 6th/7th?

Thanks

Am 06.10.2015 um 20:51 schrieb Heiner Kallweit:

Quoted 58 lines

Back in May there was a discussion on how to handle controller driver limitations like max message size (e.g. spi-fsl-espi supports message sizes up to 64Kb only).
According to Brian extending the API might not be needed and a better handling of the returned actual_length of the message should be sufficient.

Following the suggestion this patch supports reading chunks. As long as something was read also errors (like -EMSGSIZE) are ignored.

Afterwards related hacks can be removed from affected controller drivers, making them work with any protocol driver.
Currently e.g. spi-fsl-espi implicitely assumes that longer reads come from m25p80 protocol driver (if second transfer in message is a read and first transfer is a send then bytes 2-4 of the send are assumed to be a 3 byte address of a flash read).

---
 drivers/mtd/spi-nor/spi-nor.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/mtd/spi-nor/spi-nor.c +14 -1
index 4be41fb..17b5239 100644
@@ -761,6 +761,7 @@ static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
 			size_t *retlen, u_char *buf)
 {
 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
+	size_t bytes_read, pos = 0;
 	int ret;
 
 	dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
@@ -769,9 +770,21 @@ static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
 	if (ret)
 		return ret;
 
-	ret = nor->read(nor, from, len, retlen, buf);
+	/* Consider the case that a controller driver is not able
+	 * to read all requested bytes at once. It might return
+	 * an error and the number of bytes read.
+	 * Therefore ignore errors as long as something was read.
+	 */
+	do {
+		bytes_read = 0;
+		ret = nor->read(nor, from + pos, len - pos,
+				&bytes_read, buf + pos);
+		pos += bytes_read;
+	} while (pos < len && bytes_read);
 
 	spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
+
+	*retlen = pos;
 	return ret;
 }
 
#2 lore raw
In reply to Heiner Kallweit

Hi,

Quoted 38 lines · Heiner Kallweit

Back in May there was a discussion on how to handle controller driver limitations like max message size (e.g. spi-fsl-espi supports message sizes up to 64Kb only).
According to Brian extending the API might not be needed and a better handling of the returned actual_length of the message should be sufficient.

Following the suggestion this patch supports reading chunks. As long as something was read also errors (like -EMSGSIZE) are ignored.

Afterwards related hacks can be removed from affected controller drivers, making them work with any protocol driver.
Currently e.g. spi-fsl-espi implicitely assumes that longer reads come from m25p80 protocol driver (if second transfer in message is a read and first transfer is a send then bytes 2-4 of the send are assumed to be a 3 byte address of a flash read).

---
 drivers/mtd/spi-nor/spi-nor.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/mtd/spi-nor/spi-nor.c +1 -0
index 4be41fb..17b5239 100644
@@ -761,6 +761,7 @@ static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
 			size_t *retlen, u_char *buf)
 {
 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
+	size_t bytes_read, pos = 0;
 	int ret;
 
 	dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
@@ -769,9 +770,21 @@ static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
 	if (ret)
 		return ret;
 

drivers/mtd/spi-nor/spi-nor.c static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len, · line 781

Quoted 10 lines
drivers/mtd/spi-nor/spi-nor.c +9 -1
-	ret = nor->read(nor, from, len, retlen, buf);
+	/* Consider the case that a controller driver is not able
+	 * to read all requested bytes at once. It might return
+	 * an error and the number of bytes read.
+	 * Therefore ignore errors as long as something was read.
+	 */
+	do {
+		bytes_read = 0;
+		ret = nor->read(nor, from + pos, len - pos,
+				&bytes_read, buf + pos);

As we discussed elsewhere, I don't think it's good to ignore all errors here.

drivers/mtd/spi-nor/spi-nor.c static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len, · line 790

Quoted 9 lines
drivers/mtd/spi-nor/spi-nor.c +4 -0
+		pos += bytes_read;
+	} while (pos < len && bytes_read);
 
 	spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
+
+	*retlen = pos;
 	return ret;
 }
 

Brian