Skip to contentSkip to conversation

spi: spi-cadence-quadspi: Fix mapping of buffers for DMA reads

PATCH Superseded Vignesh Raghavendra 2 participants 3 messages Aug 28, 2020 – Aug 31, 2020 SPI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spi: spi-cadence-quadspi: Fix mapping of buffers for DMA reads

Buffers need to mapped to DMA channel's device pointer instead of SPI controller's device pointer as its system DMA that actually does data transfer.
Data inconsistencies have been reported when reading from flash without this fix.

Fixes 31fb632b5d43c ("spi: Move cadence-quadspi driver to drivers/spi/")

---

This issue was present in the original driver under SPI NOR framework as well. But only got exposed as driver started handling probe deferral for DMA channel request and thus uses DMA almost always unlike before.

 drivers/spi/spi-cadence-quadspi.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/spi/spi-cadence-quadspi.c +5 -3
index 508b219eabf8..c6795c684b16 100644
@@ -907,14 +907,16 @@ static int cqspi_direct_read_execute(struct cqspi_flash_pdata *f_pdata,
 	struct dma_async_tx_descriptor *tx;
 	dma_cookie_t cookie;
 	dma_addr_t dma_dst;
+	struct device *ddev;
 
 	if (!cqspi->rx_chan || !virt_addr_valid(buf)) {
 		memcpy_fromio(buf, cqspi->ahb_base + from, len);
 		return 0;
 	}
 
-	dma_dst = dma_map_single(dev, buf, len, DMA_FROM_DEVICE);
-	if (dma_mapping_error(dev, dma_dst)) {
+	ddev = cqspi->rx_chan->device->dev;
+	dma_dst = dma_map_single(ddev, buf, len, DMA_FROM_DEVICE);
+	if (dma_mapping_error(ddev, dma_dst)) {
 		dev_err(dev, "dma mapping failed\n");
 		return -ENOMEM;
 	}
@@ -948,7 +950,7 @@ static int cqspi_direct_read_execute(struct cqspi_flash_pdata *f_pdata,
 	}
 
 err_unmap:
-	dma_unmap_single(dev, dma_dst, len, DMA_FROM_DEVICE);
+	dma_unmap_single(ddev, dma_dst, len, DMA_FROM_DEVICE);
 
 	return ret;
 }
Signature · 1 line
2.28.0
Tested-by on v1 1/1
#1 lore raw
Quoted 7 lines · Vignesh Raghavendra

Buffers need to mapped to DMA channel's device pointer instead of SPI controller's device pointer as its system DMA that actually does data transfer.
Data inconsistencies have been reported when reading from flash without this fix.

Fixes 31fb632b5d43c ("spi: Move cadence-quadspi driver to drivers/spi/")

This looks wrong, ...

Quoted 6 lines

---

This issue was present in the original driver under SPI NOR framework as well. But only got exposed as driver started handling probe deferral for DMA channel request and thus uses DMA almost always unlike before.

...you rather want 935da5e5100f57d843cac4781b21f1c235059aa0 then.

Other than that:

Thanks!
Jan

Quoted 37 lines
 drivers/spi/spi-cadence-quadspi.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/spi/spi-cadence-quadspi.c +5 -3
index 508b219eabf8..c6795c684b16 100644
@@ -907,14 +907,16 @@ static int cqspi_direct_read_execute(struct cqspi_flash_pdata *f_pdata,
 	struct dma_async_tx_descriptor *tx;
 	dma_cookie_t cookie;
 	dma_addr_t dma_dst;
+	struct device *ddev;
 
 	if (!cqspi->rx_chan || !virt_addr_valid(buf)) {
 		memcpy_fromio(buf, cqspi->ahb_base + from, len);
 		return 0;
 	}
 
-	dma_dst = dma_map_single(dev, buf, len, DMA_FROM_DEVICE);
-	if (dma_mapping_error(dev, dma_dst)) {
+	ddev = cqspi->rx_chan->device->dev;
+	dma_dst = dma_map_single(ddev, buf, len, DMA_FROM_DEVICE);
+	if (dma_mapping_error(ddev, dma_dst)) {
 		dev_err(dev, "dma mapping failed\n");
 		return -ENOMEM;
 	}
@@ -948,7 +950,7 @@ static int cqspi_direct_read_execute(struct cqspi_flash_pdata *f_pdata,
 	}
 
 err_unmap:
-	dma_unmap_single(dev, dma_dst, len, DMA_FROM_DEVICE);
+	dma_unmap_single(ddev, dma_dst, len, DMA_FROM_DEVICE);
 
 	return ret;
 }
Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA IOT SES-DE
Corporate Competence Center Embedded Linux
#2 lore raw
Quoted 11 lines · Jan Kiszka
Quoted 7 lines · Vignesh Raghavendra

Buffers need to mapped to DMA channel's device pointer instead of SPI controller's device pointer as its system DMA that actually does data transfer.
Data inconsistencies have been reported when reading from flash without this fix.

Fixes 31fb632b5d43c ("spi: Move cadence-quadspi driver to drivers/spi/")

This looks wrong, ...

Quoted 9 lines
Quoted 6 lines

---

This issue was present in the original driver under SPI NOR framework as well. But only got exposed as driver started handling probe deferral for DMA channel request and thus uses DMA almost always unlike before.

...you rather want 935da5e5100f57d843cac4781b21f1c235059aa0 then.

Actually, the offending code is actually from
commit ffa639e069fb ("mtd: spi-nor: cadence-quadspi: Add DMA support for direct mode reads) which adds dma_{un}map_single() calls.

Will send v2 with Fixes tag updated to point to above commit

Quoted 3 lines

Other than that:

Thanks!

Regards
Vignesh

Quoted 42 lines

Thanks!
Jan

Quoted 37 lines
 drivers/spi/spi-cadence-quadspi.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/spi/spi-cadence-quadspi.c +5 -3
index 508b219eabf8..c6795c684b16 100644
@@ -907,14 +907,16 @@ static int cqspi_direct_read_execute(struct cqspi_flash_pdata *f_pdata,
 	struct dma_async_tx_descriptor *tx;
 	dma_cookie_t cookie;
 	dma_addr_t dma_dst;
+	struct device *ddev;
 
 	if (!cqspi->rx_chan || !virt_addr_valid(buf)) {
 		memcpy_fromio(buf, cqspi->ahb_base + from, len);
 		return 0;
 	}
 
-	dma_dst = dma_map_single(dev, buf, len, DMA_FROM_DEVICE);
-	if (dma_mapping_error(dev, dma_dst)) {
+	ddev = cqspi->rx_chan->device->dev;
+	dma_dst = dma_map_single(ddev, buf, len, DMA_FROM_DEVICE);
+	if (dma_mapping_error(ddev, dma_dst)) {
 		dev_err(dev, "dma mapping failed\n");
 		return -ENOMEM;
 	}
@@ -948,7 +950,7 @@ static int cqspi_direct_read_execute(struct cqspi_flash_pdata *f_pdata,
 	}
 
 err_unmap:
-	dma_unmap_single(dev, dma_dst, len, DMA_FROM_DEVICE);
+	dma_unmap_single(ddev, dma_dst, len, DMA_FROM_DEVICE);
 
 	return ret;
 }