Skip to contentSkip to conversation

scsi: ensure writes are flushed to disk

PATCH In review Caleb Connolly 3 participants 8 messages Mar 26, 2025 – Apr 10, 2025

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

v2 cover scsi: ensure writes are flushed to disk

SCSI devices like UFS may maintain their own cache to speed up writes, however this is lost on board reset (and may be lost on device removal or reset by OS drivers).

Currently this can be worked around by "waiting for a while" after writing data to disk, but of course this is not an acceptable solution.

Ideally U-Boot would have a mechanism to flush caches during board reset, but until that logic is hooked up let's be sure that all writes are actually propagated to the storage device so that we don't lose data on board reset.

The same logic was already implemented just for the AHCI backend, this duplicated logic has been removed and support for the SYNC_CACHE command is added to AHCI.

This is particularly noticeable during capsule updates, since the update file is deleted and the board is reset immediately afterwards which resulted in the same capsule update being applied over and over again.

This specifically fixes Qualcomm SDM845 devices with UFS 2.1, but likely all UFS devices that use a cache.

---
Changes in v2:
- Only issue the SYNC_CACHE command once for the whole write (thanks
Neil)
- De-duplicate AHCI which also implemented the same logic.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Caleb Connolly (3):
scsi: fix typo in setup_read_ext()
scsi: sync cache on write
ata: ahci: implement SCSI_SYNC_CACHE

 drivers/ata/ahci.c  | 14 +++-----------
 drivers/scsi/scsi.c | 25 ++++++++++++++++++++++++-
 2 files changed, 27 insertions(+), 12 deletions(-)

---
base-commit: a383b9bd4d7e430fe7c254297540bae596649dba

Caleb Connolly <[email protected]>

#1 lore raw

PATCH v2 1/3 scsi: fix typo in setup_read_ext()

This clears the 6th byte of cmd twice rather than setting the 9th byte to 0. Fix it.

The only other command that sets the 9th byte is the 64-bit read, so this likely never caused issues in practise.

---
 drivers/scsi/scsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/scsi.c +1 -1
index cd0b84c062252118d250b9305728e03f61736600..34ac47c03ab397ca999abf130d84ccbd3be4c419 100644
@@ -89,9 +89,9 @@ static void scsi_setup_read_ext(struct scsi_cmd *pccb, lbaint_t start,
 	pccb->cmd[5] = (unsigned char)start & 0xff;
 	pccb->cmd[6] = 0;
 	pccb->cmd[7] = (unsigned char)(blocks >> 8) & 0xff;
 	pccb->cmd[8] = (unsigned char)blocks & 0xff;
-	pccb->cmd[6] = 0;
+	pccb->cmd[9] = 0;
 	pccb->cmdlen = 10;
 	pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
 	debug("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
 	      pccb->cmd[0], pccb->cmd[1],
Signature · 1 line
2.49.0
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] scsi: fix typo in setup_read_ext()

Quoted 17 lines · Caleb Connolly

This clears the 6th byte of cmd twice rather than setting the 9th byte to 0. Fix it.

The only other command that sets the 9th byte is the 64-bit read, so this likely never caused issues in practise.

---
  drivers/scsi/scsi.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/scsi.c +0 -0
index cd0b84c062252118d250b9305728e03f61736600..34ac47c03ab397ca999abf130d84ccbd3be4c419 100644

drivers/scsi/scsi.c static void scsi_setup_read_ext(struct scsi_cmd *pccb, lbaint_t start, · line 97

Quoted 10 lines
drivers/scsi/scsi.c +1 -1
@@ -89,9 +89,9 @@ static void scsi_setup_read_ext(struct scsi_cmd *pccb, lbaint_t start,
  	pccb->cmd[5] = (unsigned char)start & 0xff;
  	pccb->cmd[6] = 0;
  	pccb->cmd[7] = (unsigned char)(blocks >> 8) & 0xff;
  	pccb->cmd[8] = (unsigned char)blocks & 0xff;
-	pccb->cmd[6] = 0;
+	pccb->cmd[9] = 0;
  	pccb->cmdlen = 10;
  	pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
  	debug("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
  	      pccb->cmd[0], pccb->cmd[1],
#3 lore raw

PATCH v2 2/3 scsi: sync cache on write

In reply to Caleb Connolly

We don't have a mechanism to safely shutdown block devices prior to a baord reset or driver removal. Prevent data loss by synchronizing the SCSI cache after every write.

In particular this solves the issue of capsule updates looping on some devices because the board resets immediately after deleting the capsule file and this write wouldn't be flushed in time.

This may impact NAND wear, but should be negligible given the usecases for disk write in U-Boot.

---
 drivers/scsi/scsi.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
drivers/scsi/scsi.c +23 -0
index 34ac47c03ab397ca999abf130d84ccbd3be4c419..3e556540cae46734d3b13a72cf279905134336f3 100644
@@ -77,8 +77,25 @@ static void scsi_setup_inquiry(struct scsi_cmd *pccb)
 	pccb->cmdlen = 6;
 	pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
 }
 
+static void scsi_setup_sync_cache(struct scsi_cmd *pccb, lbaint_t start,
+				  unsigned short blocks)
+{
+	pccb->cmd[0] = SCSI_SYNC_CACHE;
+	pccb->cmd[1] = 0;
+	pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
+	pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
+	pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
+	pccb->cmd[5] = (unsigned char)start & 0xff;
+	pccb->cmd[6] = 0;
+	pccb->cmd[7] = (unsigned char)(blocks >> 8) & 0xff;
+	pccb->cmd[8] = (unsigned char)blocks & 0xff;
+	pccb->cmd[9] = 0;
+	pccb->cmdlen = 10;
+	pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
+}
+
 static void scsi_setup_read_ext(struct scsi_cmd *pccb, lbaint_t start,
 				unsigned short blocks)
 {
 	pccb->cmd[0] = SCSI_READ10;
@@ -239,8 +256,14 @@ static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
 			break;
 		}
 		buf_addr += pccb->datalen;
 	} while (blks != 0);
+
+	/* Flush the SCSI cache so we don't lose data on board reset. */
+	scsi_setup_sync_cache(pccb, 0, 0);
+	if (scsi_exec(bdev, pccb))
+		scsi_print_error(pccb);
+
 	debug("%s: end startblk " LBAF ", blccnt %x buffer %lX\n",
 	      __func__, start, smallblks, buf_addr);
 	return blkcnt;
 }
Signature · 1 line
2.49.0
Reviewed-by on v2 2/3
#4 lore raw

Re: [PATCH v2 2/3] scsi: sync cache on write

Quoted 52 lines · Caleb Connolly

We don't have a mechanism to safely shutdown block devices prior to a baord reset or driver removal. Prevent data loss by synchronizing the SCSI cache after every write.

In particular this solves the issue of capsule updates looping on some devices because the board resets immediately after deleting the capsule file and this write wouldn't be flushed in time.

This may impact NAND wear, but should be negligible given the usecases for disk write in U-Boot.

---
  drivers/scsi/scsi.c | 23 +++++++++++++++++++++++
  1 file changed, 23 insertions(+)
drivers/scsi/scsi.c +17 -0
index 34ac47c03ab397ca999abf130d84ccbd3be4c419..3e556540cae46734d3b13a72cf279905134336f3 100644
@@ -77,8 +77,25 @@ static void scsi_setup_inquiry(struct scsi_cmd *pccb)
  	pccb->cmdlen = 6;
  	pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
  }
 
+static void scsi_setup_sync_cache(struct scsi_cmd *pccb, lbaint_t start,
+				  unsigned short blocks)
+{
+	pccb->cmd[0] = SCSI_SYNC_CACHE;
+	pccb->cmd[1] = 0;
+	pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
+	pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
+	pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
+	pccb->cmd[5] = (unsigned char)start & 0xff;
+	pccb->cmd[6] = 0;
+	pccb->cmd[7] = (unsigned char)(blocks >> 8) & 0xff;
+	pccb->cmd[8] = (unsigned char)blocks & 0xff;
+	pccb->cmd[9] = 0;
+	pccb->cmdlen = 10;
+	pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
+}
+
  static void scsi_setup_read_ext(struct scsi_cmd *pccb, lbaint_t start,
  				unsigned short blocks)
  {
  	pccb->cmd[0] = SCSI_READ10;
@@ -239,8 +256,14 @@ static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
  			break;
  		}
  		buf_addr += pccb->datalen;
  	} while (blks != 0);

drivers/scsi/scsi.c static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, · line 269

Quoted 10 lines
drivers/scsi/scsi.c +6 -0
+
+	/* Flush the SCSI cache so we don't lose data on board reset. */
+	scsi_setup_sync_cache(pccb, 0, 0);
+	if (scsi_exec(bdev, pccb))
+		scsi_print_error(pccb);
+
  	debug("%s: end startblk " LBAF ", blccnt %x buffer %lX\n",
  	      __func__, start, smallblks, buf_addr);
  	return blkcnt;
  }
#5 lore raw

PATCH v2 3/3 ata: ahci: implement SCSI_SYNC_CACHE

In reply to Caleb Connolly

The SCSI layer now issues a SYNC_CACHE command after every write to ensure there is no data loss due to a board reset after write.

Implement support for this command and remove the same logic from the ATA write path to be consistent with other SCSI backends.

Ranges are not supported and the whole cache will be flushed in all cases.

This was done per iteration in ata_scsiop_read_write(), but it's not clear why this was the case, calling it once for the entire write ought to achieve the same result.

---
 drivers/ata/ahci.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
drivers/ata/ahci.c +3 -11
index 8058d5ff1c37f92914f250fa9cc78498a3f27331..deb91efd6d2666016f375feabb16ebc8f68fb697 100644
@@ -735,19 +735,8 @@ static int ata_scsiop_read_write(struct ahci_uc_priv *uc_priv,
 			debug("scsi_ahci: SCSI %s10 command failure.\n",
 			      is_write ? "WRITE" : "READ");
 			return -EIO;
 		}
-
-		/* If this transaction is a write, do a following flush.
-		 * Writes in u-boot are so rare, and the logic to know when is
-		 * the last write and do a flush only there is sufficiently
-		 * difficult. Just do a flush after every write. This incurs,
-		 * usually, one extra flush when the rare writes do happen.
-		 */
-		if (is_write) {
-			if (-EIO == ata_io_flush(uc_priv, pccb->target))
-				return -EIO;
-		}
 		user_buffer += transfer_size;
 		user_buffer_size -= transfer_size;
 		blocks -= now_blocks;
 		lba += now_blocks;
@@ -845,8 +834,11 @@ static int ahci_scsi_exec(struct udevice *dev, struct scsi_cmd *pccb)
 		break;
 	case SCSI_INQUIRY:
 		ret = ata_scsiop_inquiry(uc_priv, pccb);
 		break;
+	case SCSI_SYNC_CACHE:
+		ret = ata_io_flush(uc_priv, pccb->target);
+		break;
 	default:
 		printf("Unsupport SCSI command 0x%02x\n", pccb->cmd[0]);
 		return -ENOTSUPP;
 	}
Signature · 1 line
2.49.0
Reviewed-by on v2 3/3
#6 lore raw

Re: [PATCH v2 3/3] ata: ahci: implement SCSI_SYNC_CACHE

Quoted 45 lines · Caleb Connolly

The SCSI layer now issues a SYNC_CACHE command after every write to ensure there is no data loss due to a board reset after write.

Implement support for this command and remove the same logic from the ATA write path to be consistent with other SCSI backends.

Ranges are not supported and the whole cache will be flushed in all cases.

This was done per iteration in ata_scsiop_read_write(), but it's not clear why this was the case, calling it once for the entire write ought to achieve the same result.

---
  drivers/ata/ahci.c | 14 +++-----------
  1 file changed, 3 insertions(+), 11 deletions(-)
drivers/ata/ahci.c +0 -11
index 8058d5ff1c37f92914f250fa9cc78498a3f27331..deb91efd6d2666016f375feabb16ebc8f68fb697 100644
@@ -735,19 +735,8 @@ static int ata_scsiop_read_write(struct ahci_uc_priv *uc_priv,
  			debug("scsi_ahci: SCSI %s10 command failure.\n",
  			      is_write ? "WRITE" : "READ");
  			return -EIO;
  		}
-
-		/* If this transaction is a write, do a following flush.
-		 * Writes in u-boot are so rare, and the logic to know when is
-		 * the last write and do a flush only there is sufficiently
-		 * difficult. Just do a flush after every write. This incurs,
-		 * usually, one extra flush when the rare writes do happen.
-		 */
-		if (is_write) {
-			if (-EIO == ata_io_flush(uc_priv, pccb->target))
-				return -EIO;
-		}
  		user_buffer += transfer_size;
  		user_buffer_size -= transfer_size;
  		blocks -= now_blocks;
  		lba += now_blocks;
@@ -845,8 +834,11 @@ static int ahci_scsi_exec(struct udevice *dev, struct scsi_cmd *pccb)
  		break;

drivers/ata/ahci.c static int ahci_scsi_exec(struct udevice *dev, struct scsi_cmd *pccb) · line 844

Quoted 10 lines
drivers/ata/ahci.c +3 -0
  	case SCSI_INQUIRY:
  		ret = ata_scsiop_inquiry(uc_priv, pccb);
  		break;
+	case SCSI_SYNC_CACHE:
+		ret = ata_io_flush(uc_priv, pccb->target);
+		break;
  	default:
  		printf("Unsupport SCSI command 0x%02x\n", pccb->cmd[0]);
  		return -ENOTSUPP;
  	}

Good catch, much simpler to move it in the scsi layer !

Can someone test on a SATA enabled device ?

I can have a test run on the Odroid-HC4 with a PCIe-SATA controller.

Thanks,
Neil

#7 lore raw
In reply to Caleb Connolly
Quoted 8 lines · Caleb Connolly

SCSI devices like UFS may maintain their own cache to speed up writes, however this is lost on board reset (and may be lost on device removal or reset by OS drivers).

Currently this can be worked around by "waiting for a while" after writing data to disk, but of course this is not an acceptable solution.

[...]

Applied to u-boot/master, thanks!

[1/3] scsi: fix typo in setup_read_ext()

      commit: 0cd3c1e7d0201cf9e4dbcac1e0179604f4461062
[2/3] scsi: sync cache on write
      commit: ffe4e6ab42d2534302be825e49a2d085acf80f30
[3/3] ata: ahci: implement SCSI_SYNC_CACHE
      commit: 77c13f30b67c1a30b33b4dd4820c5a2528c3a3bb
Signature · 1 line
Tom