Skip to contentSkip to conversation

nvme: few fixups

PATCH In review [email protected] 4 participants 22 messages May 29 – Jul 16 Block layer

lore
Share & tools
lore
#0 lore raw

v2 cover nvme: few fixups

The series adds few fixes for NVMe subsystem.

Patch 1 adds logline for tracing misbehaving NVMe drives. Patch 2 adds plumbing for blk_flush()/blk_dflush().
Patch 3 introduces NVMe flush command.
Patch 4 enables flush for the currently selected block device. Patch 5 makes nvme_shutdown() symbol public to enable use of it from
board_quiesce_devices()
Patch 6 tiny fixup for QEMU command for manual NVMe tests.

Denis Mukhin (6):
drivers: nvme: Log I/O timeouts
drivers: block: Introduce blk_flush()/blk_dflush()
drivers: nvme: Implement flush command
cmd: Add flush support for all blk devices
drivers: nvme: Export nvme_shutdown()
docs: nvme: Update QEMU command for testing

 cmd/blk_common.c                  | 14 ++++++++++++++
 cmd/ide.c                         |  1 +
 cmd/nvme.c                        |  1 +
 cmd/pvblock.c                     |  1 +
 cmd/sata.c                        |  1 +
 cmd/scsi.c                        |  1 +
 cmd/usb.c                         |  1 +
 cmd/virtio.c                      |  1 +
 disk/disk-uclass.c                |  6 ++++++
 doc/develop/driver-model/nvme.rst |  2 +-
 drivers/block/blk-uclass.c        | 18 ++++++++++++++++++
 drivers/nvme/nvme.c               | 24 +++++++++++++++++++++++-
 drivers/nvme/nvme.h               |  7 -------
 include/blk.h                     | 28 ++++++++++++++++++++++++++++
 include/nvme.h                    | 11 +++++++++++
 include/part.h                    |  8 ++++++++
 16 files changed, 116 insertions(+), 9 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH v2 1/6 drivers: nvme: Log I/O timeouts

From: Denis Mukhin <[email protected]>

Current code silently swallows any timed-out commands scheduled to NVMe. Log those to be able to debug any potential problems with the NVMe hardware/firmware.

---
Changes since v1:
- added Neil's R-b

---
 drivers/nvme/nvme.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/nvme/nvme.c +3 -1
index 0631b190b978..c3c44e50f19a 100644
@@ -182,8 +182,10 @@ static int nvme_submit_sync_cmd(struct nvme_queue *nvmeq,
 		if ((status & 0x01) == phase)
 			break;
 		if (timeout_us > 0 && (timer_get_us() - start_time)
-		    >= timeout_us)
+		    >= timeout_us) {
+			pr_warn("nvme: cmd %#x timed out\n", cmd->common.command_id);
 			return -ETIMEDOUT;
+		}
 	}
 
 	ops = (struct nvme_ops *)nvmeq->dev->udev->driver->ops;
Signature · 1 line
2.54.0
Reviewed-by on v2 1/6
#2 lore raw

Re: [PATCH v2 1/6] drivers: nvme: Log I/O timeouts

Hi Denis,

Quoted 14 lines · None

drivers: nvme: Log I/O timeouts

Current code silently swallows any timed-out commands scheduled to NVMe. Log those to be able to debug any potential problems with the NVMe hardware/firmware.

drivers/nvme/nvme.c | 4 +++-

 1 file changed, 3 insertions(+), 1 deletion(-)

drivers/nvme/nvme.c static int nvme_submit_sync_cmd(struct nvme_queue *nvmeq, · line 185

Quoted 8 lines
drivers/nvme/nvme.c +4 -1
@@ -179,8 +179,10 @@ static int nvme_submit_sync_cmd(struct nvme_queue *nvmeq,
              if ((status & 0x01) == phase)
                      break;
              if (timeout_us > 0 && (timer_get_us() - start_time)
-                 >= timeout_us)
+                 >= timeout_us) {
+                     pr_warn("nvme: cmd %#x timed out\n", cmd->common.command_id);
+                     return -ETIMEDOUT;
+             }

A few suggestions:

- command_id is just an incrementing sequence (see nvme_get_cmd_id()), so on its own it tells you very little. How about logging cmd->common.opcode as well - that identifies the command type and is the useful bit for debugging.

- With multiple NVMe controllers this message won't say which one stalled. Since nvmeq->dev->udev is to hand, dev_warn() would be more informative than pr_warn(), and would drop the need for the 'nvme: ' prefix. Or you could use log_warning() and print out what you want

- The new line is ~85 columns; please wrap it.

Regards,
Simon

#3 lore raw

PATCH v2 2/6 drivers: block: Introduce blk_flush()/blk_dflush()

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Add generic flush operations for committing dirty data to the storage device.

This provides a common block-layer interface for flushing pending writes, allowing callers to ensure that data buffered by the block device or its backing implementation is written out to the underlying storage.

---
Changes since v1:
- new patch

---
 disk/disk-uclass.c         |  6 ++++++
 drivers/block/blk-uclass.c | 18 ++++++++++++++++++
 include/blk.h              | 28 ++++++++++++++++++++++++++++
 include/part.h             |  8 ++++++++
 4 files changed, 60 insertions(+)
disk/disk-uclass.c +6 -0
index ee3cc4407d76..1a87f7dffa7c 100644
@@ -122,6 +122,11 @@ unsigned long disk_blk_erase(struct udevice *dev, lbaint_t start,
 			 blkcnt);
 }
 
+unsigned long disk_blk_flush(struct udevice *dev)
+{
+	return blk_flush(dev_get_parent(dev));
+}
+
 UCLASS_DRIVER(partition) = {
 	.id		= UCLASS_PARTITION,
 	.per_device_plat_auto	= sizeof(struct disk_part),
@@ -132,6 +137,7 @@ static const struct blk_ops blk_part_ops = {
 	.read	= disk_blk_read,
 	.write	= disk_blk_write,
 	.erase	= disk_blk_erase,
+	.flush	= disk_blk_flush,
 };
 
 U_BOOT_DRIVER(blk_partition) = {
drivers/block/blk-uclass.c +18 -0
index 73c24fd91763..0be1fdab1ba5 100644
@@ -514,6 +514,19 @@ long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt)
 	return ops->erase(dev, start, blkcnt);
 }
 
+long blk_flush(struct udevice *dev)
+{
+	struct blk_desc *desc = dev_get_uclass_plat(dev);
+	const struct blk_ops *ops = blk_get_ops(dev);
+
+	if (!ops->flush)
+		return -ENOSYS;
+
+	blkcache_invalidate(desc->uclass_id, desc->devnum);
+
+	return ops->flush(dev);
+}
+
 ulong blk_dread(struct blk_desc *desc, lbaint_t start, lbaint_t blkcnt,
 		void *buffer)
 {
@@ -531,6 +544,11 @@ ulong blk_derase(struct blk_desc *desc, lbaint_t start, lbaint_t blkcnt)
 	return blk_erase(desc->bdev, start, blkcnt);
 }
 
+ulong blk_dflush(struct blk_desc *desc)
+{
+	return blk_flush(desc->bdev);
+}
+
 int blk_find_from_parent(struct udevice *parent, struct udevice **devp)
 {
 	struct udevice *dev;
include/blk.h +28 -0
index 8d1b70cabd31..98e1db7a58f1 100644
@@ -99,6 +99,7 @@ struct blk_desc {
 	unsigned long	(*block_erase)(struct blk_desc *block_dev,
 				       lbaint_t start,
 				       lbaint_t blkcnt);
+	unsigned long	(*block_flush)(struct blk_desc *block_dev);
 	void		*priv;		/* driver private struct pointer */
 #endif
 };
@@ -275,6 +276,14 @@ struct blk_ops {
 	 */
 	int (*buffer_aligned)(struct udevice *dev, struct bounce_buffer *state);
 #endif	/* CONFIG_BOUNCE_BUFFER */
+
+	/**
+	 * flush() -  commit all dirty data to storage
+	 *
+	 * @dev:	Device to flush
+	 * @return 0 if OK, -ve on error
+	 */
+	unsigned long (*flush)(struct udevice *dev);
 };
 
 #if CONFIG_IS_ENABLED(BLK)
@@ -291,6 +300,7 @@ unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
 			 lbaint_t blkcnt, const void *buffer);
 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
 			 lbaint_t blkcnt);
+unsigned long blk_dflush(struct blk_desc *block_dev);
 
 #endif /* BLK */
 
@@ -331,6 +341,14 @@ long blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
  */
 long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt);
 
+/**
+ * blk_flush() - Commit data to a block device
+ *
+ * @dev: Device to flush
+ * @return 0 if operation succeeded, or -ve on error.
+ */
+long blk_flush(struct udevice *dev);
+
 /**
  * blk_find_device() - Find a block device
  *
@@ -559,6 +577,16 @@ static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
 	return block_dev->block_erase(block_dev, start, blkcnt);
 }
 
+static inline ulong blk_dflush(struct blk_desc *block_dev)
+{
+	if (!block_dev->block_flush)
+		return -ENOSYS;
+
+	blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
+
+	return block_dev->block_flush(block_dev);
+}
+
 /**
  * struct blk_driver - Driver for block interface types
  *
include/part.h +8 -0
index 15daacd7faaa..63982d7b9370 100644
@@ -454,6 +454,14 @@ ulong disk_blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
  */
 ulong disk_blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt);
 
+/**
+ * disk_blk_flush() - commit data to a disk
+ *
+ * @dev:	Device to flush
+ * Return:	0 success, or -ve error number (see the IS_ERR_VALUE() macro
+ */
+ulong disk_blk_flush(struct udevice *dev);
+
 /*
  * We don't support printing partition information in SPL and only support
  * getting partition information in a few cases.
Signature · 1 line
2.54.0
#4 lore raw

Re: [PATCH v2 2/6] drivers: block: Introduce blk_flush()/blk_dflush()

uHi Denis,

Quoted 22 lines · None

drivers: block: Introduce blk_flush()/blk_dflush()

Add generic flush operations for committing dirty data to the storage device.

This provides a common block-layer interface for flushing pending writes, allowing callers to ensure that data buffered by the block device or its backing implementation is written out to the underlying storage.

disk/disk-uclass.c | 6 ++++++

 drivers/block/blk-uclass.c | 18 ++++++++++++++++++
 include/blk.h              | 28 ++++++++++++++++++++++++++++
 include/part.h             |  8 ++++++++
 4 files changed, 60 insertions(+)
drivers/block/blk-uclass.c +2 -0
@@ -514,6 +514,19 @@ long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt)
+long blk_flush(struct udevice *dev)
+{

drivers/block/blk-uclass.c long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt) · line 525

Quoted 10 lines
drivers/block/blk-uclass.c +10 -0
+     struct blk_desc *desc = dev_get_uclass_plat(dev);
+     const struct blk_ops *ops = blk_get_ops(dev);
+
+     if (!ops->flush)
+             return -ENOSYS;
+
+     blkcache_invalidate(desc->uclass_id, desc->devnum);
+
+     return ops->flush(dev);
+}

Why invalidate the read cache here? Flush pushes dirty data down to the media; it doesn't change what a subsequent read returns, so the cached contents are still valid. Erase/write mutate device state, but flush does not. Please drop the blkcache_invalidate() call (and the matching one in the non-DM inline below).

include/blk.h struct blk_desc { · line 102

Quoted 6 lines
include/blk.h +1 -0
@@ -99,6 +99,7 @@ struct blk_desc {
      unsigned long   (*block_erase)(struct blk_desc *block_dev,
                                     lbaint_t start,
                                     lbaint_t blkcnt);
+     unsigned long   (*block_flush)(struct blk_desc *block_dev);

This field lives in the !CONFIG_BLK (legacy) branch, but no legacy driver in the series wires it up. The only consumer (NVMe) is DM-only. Are you going to need the legacy plumbing?

include/blk.h struct blk_ops { · line 283

Quoted 10 lines
include/blk.h +7 -0
@@ -275,6 +276,14 @@ struct blk_ops {
+     /**
+      * flush() -  commit all dirty data to storage
+      *
+      * @dev:        Device to flush
+      * @return 0 if OK, -ve on error
+      */
+     unsigned long (*flush)(struct udevice *dev);
 };

Two small things: stray double space after the dash (flush() - commit), and please place this next to .erase so the read/write/erase/flush group is together.

include/part.h ulong disk_blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, · line 460

Quoted 9 lines
include/part.h +7 -0
@@ -454,6 +454,14 @@ ulong disk_blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
+/**
+ * disk_blk_flush() - commit data to a disk
+ *
+ * @dev:     Device to flush
+ * Return:   0 success, or -ve error number (see the IS_ERR_VALUE() macro
+ */
+ulong disk_blk_flush(struct udevice *dev);

Missing closing parenthesis after IS_ERR_VALUE() macro.

disk/disk-uclass.c unsigned long disk_blk_erase(struct udevice *dev, lbaint_t start, · line 125

Quoted 6 lines
disk/disk-uclass.c +4 -0
@@ -122,6 +122,11 @@ unsigned long disk_blk_erase(struct udevice *dev, lbaint_t start,
+unsigned long disk_blk_flush(struct udevice *dev)
+{
+     return blk_flush(dev_get_parent(dev));
+}

Please note in the commit message that flushing a partition flushes the entire underlying device (the API has no start/blkcnt), so callers don't expect partition-scoped semantics.

Regards,
Simon

#5 lore raw

Re: [PATCH v2 2/6] drivers: block: Introduce blk_flush()/blk_dflush()

On Fri, May 29, 2026 at 04:57:06AM -0500, Simon Glass wrote:
[..]

Quoted 4 lines

cached contents are still valid. Erase/write mutate device state, but flush does not. Please drop the blkcache_invalidate() call (and the matching one in the non-DM inline below).

Quoted 10 lines

include/blk.h struct blk_desc { · line 102

Quoted 6 lines
include/blk.h +1 -0
@@ -99,6 +99,7 @@ struct blk_desc {
      unsigned long   (*block_erase)(struct blk_desc *block_dev,
                                     lbaint_t start,
                                     lbaint_t blkcnt);
+     unsigned long   (*block_flush)(struct blk_desc *block_dev);

This field lives in the !CONFIG_BLK (legacy) branch, but no legacy driver in the series wires it up. The only consumer (NVMe) is DM-only. Are you going to need the legacy plumbing?

blk_dflush() is used in flush subcommand in cmd/blk_common.c

(I also used it for tests in v3 of the upcoming series).

--
Denis

#6 lore raw

PATCH v2 3/6 drivers: nvme: Implement flush command

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Implement the NVM flush command (opcode 0x00) to allow callers to synchronize any buffered data with the backing NVMe storage.

---
Changes since v1:
- moved generic disk_blk_flush() to a separate patch

---
 drivers/nvme/nvme.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/nvme/nvme.c +20 -0
index c3c44e50f19a..d9f099a11593 100644
@@ -819,9 +819,29 @@ static ulong nvme_blk_write(struct udevice *udev, lbaint_t blknr,
 	return nvme_blk_rw(udev, blknr, blkcnt, (void *)buffer, false);
 }
 
+/*
+ * NVM Flush command (opcode 0x00).
+ *
+ * Applies to a single namespace; the controller must commit all dirty
+ * data for that namespace to storage before completing the command.
+ */
+static ulong nvme_blk_flush(struct udevice *udev)
+{
+	struct nvme_ns *ns = dev_get_priv(udev);
+	struct nvme_dev *dev = ns->dev;
+	struct nvme_command c;
+
+	memset(&c, 0, sizeof(c));
+	c.common.opcode = nvme_cmd_flush;
+	c.common.nsid = cpu_to_le32(ns->ns_id);
+
+	return nvme_submit_sync_cmd(dev->queues[NVME_IO_Q], &c, NULL, IO_TIMEOUT);
+}
+
 static const struct blk_ops nvme_blk_ops = {
 	.read	= nvme_blk_read,
 	.write	= nvme_blk_write,
+	.flush	= nvme_blk_flush,
 };
 
 U_BOOT_DRIVER(nvme_blk) = {
Signature · 1 line
2.54.0
Reviewed-by on v2 3/6
#7 lore raw

Re: [PATCH v2 3/6] drivers: nvme: Implement flush command

Quoted 9 lines · None

drivers: nvme: Implement flush command

Implement the NVM flush command (opcode 0x00) to allow callers to synchronize any buffered data with the backing NVMe storage.

drivers/nvme/nvme.c | 20 ++++++++++++++++++++

 1 file changed, 20 insertions(+)
#8 lore raw

PATCH v2 4/6 cmd: Add flush support for all blk devices

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Introduce flush subcommand for all blk devices to allow committing dirty data explicitly to the given block device.

---
Changes since v1:
- updated commit message
- updated blk commands to enable flush

---
 cmd/blk_common.c | 14 ++++++++++++++
 cmd/ide.c        |  1 +
 cmd/nvme.c       |  1 +
 cmd/pvblock.c    |  1 +
 cmd/sata.c       |  1 +
 cmd/scsi.c       |  1 +
 cmd/usb.c        |  1 +
 cmd/virtio.c     |  1 +
 8 files changed, 21 insertions(+)
cmd/blk_common.c +14 -0
index 56529702a470..d3b00e10f17c 100644
@@ -37,6 +37,20 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
 				printf("\nno %s partition table available\n",
 				       if_name);
 			return CMD_RET_SUCCESS;
+		} else if (strncmp(argv[1], "flush", 5) == 0) {
+			struct blk_desc *desc;
+			int ret;
+
+			ret = blk_get_desc(uclass_id, *cur_devnump, &desc);
+			if (ret)
+				return CMD_RET_FAILURE;
+
+			if (blk_dflush(desc)) {
+				printf("\nfailed to flush device %s\n", if_name);
+				return CMD_RET_FAILURE;
+			}
+
+			return CMD_RET_SUCCESS;
 		}
 		return CMD_RET_USAGE;
 	case 3:
cmd/ide.c +1 -0
index f99fb6f58246..16833e30319c 100644
@@ -63,6 +63,7 @@ U_BOOT_CMD(ide, 5, 1, do_ide,
 	   "IDE sub-system",
 	   "reset - reset IDE controller\n"
 	   "ide info  - show available IDE devices\n"
+	   "ide flush - commit all dirty data to the current IDE device\n"
 	   "ide device [dev] - show or set current device\n"
 	   "ide part [dev] - print partition table of one or all IDE devices\n"
 	   "ide read  addr blk# cnt\n"
cmd/nvme.c +1 -0
index f2c9acba5c32..dbccb69042bc 100644
@@ -47,6 +47,7 @@ U_BOOT_CMD(
 	"scan - scan NVMe devices\n"
 	"nvme detail - show details of current NVMe device\n"
 	"nvme info - show all available NVMe devices\n"
+	"nvme flush - commit all dirty data to the current NVMe device\n"
 	"nvme device [dev] - show or set current NVMe device\n"
 	"nvme part [dev] - print partition table of one or all NVMe devices\n"
 	"nvme read addr blk# cnt - read `cnt' blocks starting at block\n"
cmd/pvblock.c +1 -0
index 3a83ac9cd92c..5e4a98a6b13b 100644
@@ -20,6 +20,7 @@ int do_pvblock(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 U_BOOT_CMD(pvblock, 5, 1, do_pvblock,
 	   "Xen para-virtualized block device",
 	   "info  - show available block devices\n"
+	   "pvblock flush - commit all dirty data to the current device\n"
 	   "pvblock device [dev] - show or set current device\n"
 	   "pvblock part [dev] - print partition table of one or all devices\n"
 	   "pvblock read  addr blk# cnt\n"
cmd/sata.c +1 -0
index 8b923f9378b2..417e027cba4f 100644
@@ -119,6 +119,7 @@ U_BOOT_CMD(
 	"init - init SATA sub system\n"
 	"sata stop [dev] - disable SATA sub system or device\n"
 	"sata info - show available SATA devices\n"
+	"sata flush - commit all dirty data to the current SATA device\n"
 	"sata device [dev] - show or set current device\n"
 	"sata part [dev] - print partition table\n"
 	"sata read addr blk# cnt\n"
cmd/scsi.c +1 -0
index ad7d8a4b6644..39be208f337a 100644
@@ -47,6 +47,7 @@ U_BOOT_CMD(
 	"SCSI sub-system",
 	"scsi info  - show available SCSI devices\n"
 	"scsi scan  - (re-)scan SCSI bus\n"
+	"scsi flush - commit all dirty data to the current SCSI device\n"
 	"scsi device [dev] - show or set current device\n"
 	"scsi part [dev] - print partition table of one or all SCSI devices\n"
 	"scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
cmd/usb.c +1 -0
index 13a2996c1f00..3e156f54c50b 100644
@@ -706,6 +706,7 @@ U_BOOT_CMD(
 	"    Available modes: J, K, S[E0_NAK], P[acket], F[orce_Enable]\n"
 #ifdef CONFIG_USB_STORAGE
 	"usb storage - show details of USB storage devices\n"
+	"usb flush - commit all dirty data to the current USB storage\n"
 	"usb dev [dev] - show or set current USB storage device\n"
 	"usb part [dev] - print partition table of one or all USB storage"
 	"    devices\n"
cmd/virtio.c +1 -0
index a42a563ab727..94f1e82cc902 100644
@@ -44,6 +44,7 @@ U_BOOT_CMD(
 	"virtio block devices sub-system",
 	"scan - initialize virtio bus\n"
 	"virtio info - show all available virtio block devices\n"
+	"virtio flush - commit all dirty data to the current virtio block device\n"
 	"virtio device [dev] - show or set current virtio block device\n"
 	"virtio part [dev] - print partition table of one or all virtio block devices\n"
 	"virtio read addr blk# cnt - read `cnt' blocks starting at block\n"
Signature · 1 line
2.54.0
#9 lore raw

Re: [PATCH v2 4/6] cmd: Add flush support for all blk devices

Hi Denis,

Quoted 24 lines · None

cmd: Add flush support for all blk devices

Introduce flush subcommand for all blk devices to allow committing dirty data explicitly to the given block device.

cmd/blk_common.c | 14 ++++++++++++++

 cmd/ide.c        |  1 +
 cmd/nvme.c       |  1 +
 cmd/pvblock.c    |  1 +
 cmd/sata.c       |  1 +
 cmd/scsi.c       |  1 +
 cmd/usb.c        |  1 +
 cmd/virtio.c     |  1 +
 8 files changed, 21 insertions(+)
cmd/blk_common.c +5 -0
@@ -37,6 +37,20 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
+             } else if (strncmp(argv[1], 'flush', 5) == 0) {
+                     struct blk_desc *desc;
+                     int ret;
+
+                     ret = blk_get_desc(uclass_id, *cur_devnump, &desc);

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, · line 51

Quoted 10 lines
cmd/blk_common.c +10 -0
+                     if (ret)
+                             return CMD_RET_FAILURE;
+
+                     if (blk_dflush(desc)) {
+                             printf("\nfailed to flush device %s\n", if_name);
+                             return CMD_RET_FAILURE;
+                     }
+
+                     return CMD_RET_SUCCESS;
+             }

Please print the return value from blk_dflush() and the device number in the failure message - otherwise a user can't tell -ENOSYS from a genuine I/O error. Compare with the erase arm just below which reports the block count. You can use %dE to get an error string if the board enables it.

Do you want to print something on success?

It is a bit misleading to suggest that all the commands support 'flush'. But I suppose we can worry about that later.

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, · line 37

Quoted 3 lines
cmd/blk_common.c +1 -0
@@ -37,6 +37,20 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
+             } else if (strncmp(argv[1], 'flush', 5) == 0) {

cmd/blkmap.c also routes through blk_common_cmd(), but dispatches via U_BOOT_SUBCMD_MKENT() so flush never reaches this function unless you add a matching subcmd entry. Either add it for consistency or call out in the commit message that blkmap is intentionally excluded.

Regards,
Simon

#10 lore raw

Re: [PATCH v2 4/6] cmd: Add flush support for all blk devices

Quoted 49 lines · Simon Glass

Hi Denis,

Quoted 24 lines · None

cmd: Add flush support for all blk devices

Introduce flush subcommand for all blk devices to allow committing dirty data explicitly to the given block device.

cmd/blk_common.c | 14 ++++++++++++++

 cmd/ide.c        |  1 +
 cmd/nvme.c       |  1 +
 cmd/pvblock.c    |  1 +
 cmd/sata.c       |  1 +
 cmd/scsi.c       |  1 +
 cmd/usb.c        |  1 +
 cmd/virtio.c     |  1 +
 8 files changed, 21 insertions(+)
cmd/blk_common.c +5 -0
@@ -37,6 +37,20 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
+             } else if (strncmp(argv[1], 'flush', 5) == 0) {
+                     struct blk_desc *desc;
+                     int ret;
+
+                     ret = blk_get_desc(uclass_id, *cur_devnump, &desc);

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, · line 51

Quoted 10 lines
cmd/blk_common.c +10 -0
+                     if (ret)
+                             return CMD_RET_FAILURE;
+
+                     if (blk_dflush(desc)) {
+                             printf("\nfailed to flush device %s\n", if_name);
+                             return CMD_RET_FAILURE;
+                     }
+
+                     return CMD_RET_SUCCESS;
+             }

Please print the return value from blk_dflush() and the device number in the failure message - otherwise a user can't tell -ENOSYS from a genuine I/O error. Compare with the erase arm just below which reports the block count. You can use %dE to get an error string if the board enables it.

Do you want to print something on success?

It is a bit misleading to suggest that all the commands support 'flush'. But I suppose we can worry about that later.

Quoted 8 lines

cmd/blk_common.c int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, · line 37

Quoted 3 lines
cmd/blk_common.c +1 -0
@@ -37,6 +37,20 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
+             } else if (strncmp(argv[1], 'flush', 5) == 0) {

cmd/blkmap.c also routes through blk_common_cmd(), but dispatches via U_BOOT_SUBCMD_MKENT() so flush never reaches this function unless you add a matching subcmd entry. Either add it for consistency or call out in the commit message that blkmap is intentionally excluded.

Thanks for the feedback!
Will update the patch.

Quoted 3 lines

Regards,
Simon

#11 lore raw

Re: [PATCH v2 4/6] cmd: Add flush support for all blk devices

In reply to [email protected]
Quoted 4 lines · On 5/28/26 23:44, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Introduce flush subcommand for all blk devices to allow committing dirty data explicitly to the given block device.

Shouldn't this be done automatically as part of blk_write? Similar to how all FS operations in U-Boot look like

- Mount FS
- Perform operation
- Unmount FS

so the FS is never in an inconsistent state once an operation completes.

Is the performance impact too much? I'm just concerned that a lot of existing code assumes that blk_write is persistent and that it's OK to e.g. immediately reset after blk_write returns.

--Sean

#12 lore raw

Re: [PATCH v2 4/6] cmd: Add flush support for all blk devices

Hi Sean,

Quoted 14 lines · Sean Anderson
Quoted 4 lines · On 5/28/26 23:44, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Introduce flush subcommand for all blk devices to allow committing dirty data explicitly to the given block device.

Shouldn't this be done automatically as part of blk_write? Similar to how all FS operations in U-Boot look like

- Mount FS
- Perform operation
- Unmount FS

Quoted 5 lines

so the FS is never in an inconsistent state once an operation completes.

Is the performance impact too much? I'm just concerned that a lot of existing code assumes that blk_write is persistent and that it's OK to e.g. immediately reset after blk_write returns.

This is actually for a device-level flush, so more like a Linux 'sync' than just a write. If the hardware supports / requires it, then it seems valuable to me.

Regards,
Simon

#13 lore raw

Re: [PATCH v2 4/6] cmd: Add flush support for all blk devices

In reply to Sean Anderson

Hi Sean,

Thanks for the feedback!

Quoted 13 lines · Sean Anderson
Quoted 4 lines · On 5/28/26 23:44, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Introduce flush subcommand for all blk devices to allow committing dirty data explicitly to the given block device.

Shouldn't this be done automatically as part of blk_write? Similar to how all FS operations in U-Boot look like

- Mount FS
- Perform operation
- Unmount FS

Quoted 5 lines

so the FS is never in an inconsistent state once an operation completes.

Is the performance impact too much? I'm just concerned that a lot of existing code assumes that blk_write is persistent and that it's OK to e.g. immediately reset after blk_write returns.

In my case I need to update the GPT-backed boot counter; there are two GPT headers, primary and backup, so I used blk_dflush() to guarantee that both headers are updated before jumping into the OS.

So in my scenario I need to ensure that those 2 writes will land to the NVMe.

I think FUA suggested by Neil in v1 feedback should be a robust solution for NVMe, but I went with a 'flush' solution first.

Quoted 2 lines

--Sean

#14 lore raw

PATCH v2 5/6 drivers: nvme: Export nvme_shutdown()

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Make nvme_shutdown() public so that it can be re-used in board-specific board_quiesce_devices() before OS hand-off.

---
Changes since v1:
- n/a

---
 drivers/nvme/nvme.h |  7 -------
 include/nvme.h      | 11 +++++++++++
 2 files changed, 11 insertions(+), 7 deletions(-)
drivers/nvme/nvme.h +0 -7
index bc1d612dde40..25a8b0b17772 100644
@@ -698,11 +698,4 @@ struct nvme_ops {
  */
 int nvme_init(struct udevice *udev);
 
-/**
- * nvme_shutdown() - Shutdown NVM Express device
- * @udev:	The NVM Express device
- * Return: 0 if OK, -ve on error
- */
-int nvme_shutdown(struct udevice *udev);
-
 #endif /* __DRIVER_NVME_H__ */
include/nvme.h +11 -0
index 2cdf8ce320c1..c7b301300097 100644
@@ -90,4 +90,15 @@ int nvme_print_info(struct udevice *udev);
  */
 int nvme_get_namespace_id(struct udevice *udev, u32 *ns_id, u8 *eui64);
 
+/**
+ * nvme_shutdown() - Shutdown NVM Express device
+ *
+ * Must be called before booting an OS to ensure cache is flushed
+ * and the controller is in a clean state for OS to re-initialize.
+ *
+ * @udev:	The NVM Express device
+ * Return: 0 if OK, -ve on error
+ */
+int nvme_shutdown(struct udevice *udev);
+
 #endif /* __NVME_H__ */
Signature · 1 line
2.54.0
Reviewed-by on v2 5/6
#15 lore raw

Re: [PATCH v2 5/6] drivers: nvme: Export nvme_shutdown()

Quoted 10 lines · None

drivers: nvme: Export nvme_shutdown()

Make nvme_shutdown() public so that it can be re-used in board-specific board_quiesce_devices() before OS hand-off.

drivers/nvme/nvme.h | 7 -------

 include/nvme.h      | 11 +++++++++++
 2 files changed, 11 insertions(+), 7 deletions(-)
#16 lore raw

PATCH v2 6/6 docs: nvme: Update QEMU command for testing

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Switch to x86-64 QEMU machine for NVMe testing in the docs. Also, add format=raw for empty NVMe disks QEMU command line.

---
Changes since v1:
- n/a

---
 doc/develop/driver-model/nvme.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
doc/develop/driver-model/nvme.rst +1 -1
index 755181331213..0e0624925e0d 100644
@@ -95,4 +95,4 @@ Example command line to call QEMU x86 below with emulated NVMe device:
 
 .. code-block:: bash
 
-  $ ./qemu-system-i386 -drive file=nvme.img,if=none,id=drv0 -device nvme,drive=drv0,serial=QEMUNVME0001 -bios u-boot.rom
+  $ qemu-system-x86_64 -nographic -drive file=nvme.img,format=raw,if=none,id=drv0 -device nvme,drive=drv0,serial=QEMUNVME0001 -bios u-boot.rom
Signature · 1 line
2.54.0
#17 lore raw

Re: [PATCH v2 6/6] docs: nvme: Update QEMU command for testing

Hi Denis,

Quoted 12 lines · None

docs: nvme: Update QEMU command for testing

Switch to x86-64 QEMU machine for NVMe testing in the docs. Also, add format=raw for empty NVMe disks QEMU command line.

doc/develop/driver-model/nvme.rst | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)

doc/develop/driver-model/nvme.rst Example command line to call QEMU x86 below with emulated NVMe device: · line 98

Quoted 5 lines
doc/develop/driver-model/nvme.rst +1 -1
@@ -95,4 +95,4 @@ Example command line to call QEMU x86 below with emulated NVMe device:
 
 .. code-block:: bash
 
-  $ ./qemu-system-i386 -drive file=nvme.img,if=none,id=drv0 -device nvme,drive=drv0,serial=QEMUNVME0001 -bios u-boot.rom
+  $ qemu-system-x86_64 -nographic -drive file=nvme.img,format=raw,if=none,id=drv0 -device nvme,drive=drv0,serial=QEMUNVME0001 -bios u-boot.rom

The commit message lists two changes but the diff also adds -nographic. Please mention it, or drop it if not required.

Also, format=raw is needed for any raw image to silence QEMU's probing warning, not specifically for empty disks. Something like "specify format=raw explicitly to avoid QEMU's raw-format auto-detection warning" would read better.

Regards,
Simon

#18 lore raw
In reply to [email protected]

Hi Denis,

Quoted 1 line · None

Patch 4 enables flush for the currently selected block device.

It adds the flush subcommand to all the block-uclass front-end commands (ide, nvme, pvblock, sata, scsi, usb, virtio) in one go. Please reword.

Quoted 1 line

The series adds few fixes for NVMe subsystem.

This is more than NVMe fixes - patch 2 adds a new block-uclass op and patch 4 touches seven command files. Since you are adding a new uclass operation, please add a test for blk_flush()/blk_dflush() under test/dm/blk.c covering success and -ENOSYS when .flush is absent.

Quoted 2 lines

Patch 5 makes nvme_shutdown() symbol public to enable use of it from
board_quiesce_devices()

Really we should mark the driver as DM_FLAG_ACTIVE_DMA and then this happens automatically. The board_quiesce_devices() function should go away at some point.

Regards,
Simon

#19 lore raw

Re: (subset) [PATCH v2 0/6] nvme: few fixups

In reply to [email protected]

Hi,

Quoted 11 lines · On Thu, 28 May 2026 20:44:35 -0700, [email protected] wrote:

The series adds few fixes for NVMe subsystem.

Patch 1 adds logline for tracing misbehaving NVMe drives. Patch 2 adds plumbing for blk_flush()/blk_dflush().
Patch 3 introduces NVMe flush command.
Patch 4 enables flush for the currently selected block device. Patch 5 makes nvme_shutdown() symbol public to enable use of it from
board_quiesce_devices()
Patch 6 tiny fixup for QEMU command for manual NVMe tests.

[...]

Thanks, Applied to https://source.denx.de/u-boot/custodians/u-boot-ufs (u-boot-nvme-fixes)

[1/6] drivers: nvme: Log I/O timeouts

      https://source.denx.de/u-boot/custodians/u-boot-ufs/-/commit/389363d287585d8135a554c4419a4f580346b9f8
Signature · 1 line
Neil
#20 lore raw
In reply to [email protected]

Hi,

Quoted 6 lines · On 5/29/26 05:44, [email protected] wrote:

The series adds few fixes for NVMe subsystem.

Patch 1 adds logline for tracing misbehaving NVMe drives. Patch 2 adds plumbing for blk_flush()/blk_dflush(). Patch 3 introduces NVMe flush command.
Patch 4 enables flush for the currently selected block device.

As my thread on v1, please enable FUA or disable WC to be sure we don't use the WC.

Neil

Quoted 30 lines

Patch 5 makes nvme_shutdown() symbol public to enable use of it from
board_quiesce_devices()
Patch 6 tiny fixup for QEMU command for manual NVMe tests.

Denis Mukhin (6):
drivers: nvme: Log I/O timeouts
drivers: block: Introduce blk_flush()/blk_dflush()
drivers: nvme: Implement flush command
cmd: Add flush support for all blk devices
drivers: nvme: Export nvme_shutdown()
docs: nvme: Update QEMU command for testing

  cmd/blk_common.c                  | 14 ++++++++++++++
  cmd/ide.c                         |  1 +
  cmd/nvme.c                        |  1 +
  cmd/pvblock.c                     |  1 +
  cmd/sata.c                        |  1 +
  cmd/scsi.c                        |  1 +
  cmd/usb.c                         |  1 +
  cmd/virtio.c                      |  1 +
  disk/disk-uclass.c                |  6 ++++++
  doc/develop/driver-model/nvme.rst |  2 +-
  drivers/block/blk-uclass.c        | 18 ++++++++++++++++++
  drivers/nvme/nvme.c               | 24 +++++++++++++++++++++++-
  drivers/nvme/nvme.h               |  7 -------
  include/blk.h                     | 28 ++++++++++++++++++++++++++++
  include/nvme.h                    | 11 +++++++++++
  include/part.h                    |  8 ++++++++
  16 files changed, 116 insertions(+), 9 deletions(-)
#21 lore raw

Hi Neil,

Thanks for the feedback!

Quoted 11 lines · On Thu, Jun 04, 2026 at 08:32:50AM +0200, [email protected] wrote:

Hi,

Quoted 6 lines · On 5/29/26 05:44, [email protected] wrote:

The series adds few fixes for NVMe subsystem.

Patch 1 adds logline for tracing misbehaving NVMe drives. Patch 2 adds plumbing for blk_flush()/blk_dflush(). Patch 3 introduces NVMe flush command.
Patch 4 enables flush for the currently selected block device.

As my thread on v1, please enable FUA or disable WC to be sure we don't use the WC.

Yes, sorry, I did not have time to respond to v1 feedback (just sent out an email):

https://lore.kernel.org/u-boot/aiJFzMdcCu5sAktz@kraken/

I will look into FUA for NVMe.

But I also think it makes sense to add flush to the code base as an alternative solution for ensuring data is stored.

--
Denis