Skip to contentSkip to conversation

Improve drive spinup on resume

PATCH In review Damien Le Moal 4 participants 10 messages Oct 12, 2023 – Oct 15, 2023 libata

lore
Share & tools
lore

Damien Le Moal

author maintainer
#0 lore raw

cover Improve drive spinup on resume

Two patches to improve disk spinup on resume:
- Patch 1: Move the spinup operation to after the drive revalidation to ensure that the VERIFY command is executed with the link speed properly setup.
- Patch 2: Attempt to spinup the drive only if it is not already spun up.

Geert,

It would be great if you could test this with your RCAR setup. I ran this on my usual machines and qemu. All good. But your setup has been useful to flush out issues before :)
Thanks !

Damien Le Moal (2):
ata: libata-eh: Spinup disk on resume after revalidation
ata: libata-core: Improve ata_dev_power_set_active()

 drivers/ata/libata-core.c | 34 ++++++++++++++++++++++++++++++++++
 drivers/ata/libata-eh.c   | 20 +++++++++++---------
 2 files changed, 45 insertions(+), 9 deletions(-)
Signature · 1 line
2.41.0
author maintainer
#1 lore raw

PATCH 1/2 ata: libata-eh: Spinup disk on resume after revalidation

Move the call to ata_dev_power_set_active() to transition a disk in standby power mode to the active power mode from ata_eh_revalidate_and_attach() before doing revalidation to the end of ata_eh_recover(), after the link speed for the device is reconfigured (if that was necessary). This is safer as this ensure that the VERIFY command executed to spinup the disk is executed with the drive properly reconfigured first.

---
 drivers/ata/libata-eh.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
drivers/ata/libata-eh.c +11 -9
index 945675f6b822..b0d6e69c4a5b 100644
@@ -3051,15 +3051,6 @@ static int ata_eh_revalidate_and_attach(struct ata_link *link,
 		if (ehc->i.flags & ATA_EHI_DID_RESET)
 			readid_flags |= ATA_READID_POSTRESET;
 
-		/*
-		 * When resuming, before executing any command, make sure to
-		 * transition the device to the active power mode.
-		 */
-		if ((action & ATA_EH_SET_ACTIVE) && ata_dev_enabled(dev)) {
-			ata_dev_power_set_active(dev);
-			ata_eh_done(link, dev, ATA_EH_SET_ACTIVE);
-		}
-
 		if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
 			WARN_ON(dev->class == ATA_DEV_PMP);
 
@@ -3856,6 +3847,17 @@ int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
 			}
 		}
 
+		/*
+		 * Make sure to transition devices to the active power mode
+		 * if needed (e.g. if we were scheduled on system resume).
+		 */
+		ata_for_each_dev(dev, link, ENABLED) {
+			if (ehc->i.dev_action[dev->devno] & ATA_EH_SET_ACTIVE) {
+				ata_dev_power_set_active(dev);
+				ata_eh_done(link, dev, ATA_EH_SET_ACTIVE);
+			}
+		}
+
 		/* retry flush if necessary */
 		ata_for_each_dev(dev, link, ALL) {
 			if (dev->class != ATA_DEV_ATA &&
Signature · 1 line
2.41.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] ata: libata-eh: Spinup disk on resume after revalidation

Quoted 54 lines · Damien Le Moal

Move the call to ata_dev_power_set_active() to transition a disk in standby power mode to the active power mode from ata_eh_revalidate_and_attach() before doing revalidation to the end of ata_eh_recover(), after the link speed for the device is reconfigured (if that was necessary). This is safer as this ensure that the VERIFY command executed to spinup the disk is executed with the drive properly reconfigured first.

---
 drivers/ata/libata-eh.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
drivers/ata/libata-eh.c +11 -9
index 945675f6b822..b0d6e69c4a5b 100644
@@ -3051,15 +3051,6 @@ static int ata_eh_revalidate_and_attach(struct ata_link *link,
 		if (ehc->i.flags & ATA_EHI_DID_RESET)
 			readid_flags |= ATA_READID_POSTRESET;
 
-		/*
-		 * When resuming, before executing any command, make sure to
-		 * transition the device to the active power mode.
-		 */
-		if ((action & ATA_EH_SET_ACTIVE) && ata_dev_enabled(dev)) {
-			ata_dev_power_set_active(dev);
-			ata_eh_done(link, dev, ATA_EH_SET_ACTIVE);
-		}
-
 		if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
 			WARN_ON(dev->class == ATA_DEV_PMP);
 
@@ -3856,6 +3847,17 @@ int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
 			}
 		}
 
+		/*
+		 * Make sure to transition devices to the active power mode
+		 * if needed (e.g. if we were scheduled on system resume).
+		 */
+		ata_for_each_dev(dev, link, ENABLED) {
+			if (ehc->i.dev_action[dev->devno] & ATA_EH_SET_ACTIVE) {
+				ata_dev_power_set_active(dev);
+				ata_eh_done(link, dev, ATA_EH_SET_ACTIVE);
+			}
+		}
+
 		/* retry flush if necessary */
 		ata_for_each_dev(dev, link, ALL) {
 			if (dev->class != ATA_DEV_ATA &&
Signature · 1 line
2.41.0
author maintainer
#3 lore raw

PATCH 2/2 ata: libata-core: Improve ata_dev_power_set_active()

In reply to Damien Le Moal

Improve the function ata_dev_power_set_active() by having it do nothing for a disk that is already in the active power state. To do that, introduce the function ata_dev_power_is_active() to test the current power state of the disk and return true if the disk is in the PM0:
active or PM1: idle state (0xff value for the count field of the CHECK POWER MODE command output).

To preserve the existing behavior, if the CHECK POWER MODE command issued in ata_dev_power_is_active() fails, the drive is assumed to be in standby mode and false is returned.

With this change, issuing the VERIFY command to access the disk media to spin it up becomes unnecessary most of the time during system resume as the port reset done by libata-eh on resume often result in the drive to spin-up (this behavior is not clearly defined by the ACS specifications and may thus vary between disk models).

---
 drivers/ata/libata-core.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
drivers/ata/libata-core.c +34 -0
index 83613280928b..6fb4e8dc8c3c 100644
@@ -2042,6 +2042,33 @@ void ata_dev_power_set_standby(struct ata_device *dev)
 			    err_mask);
 }
 
+static bool ata_dev_power_is_active(struct ata_device *dev)
+{
+	struct ata_taskfile tf;
+	unsigned int err_mask;
+
+	ata_tf_init(dev, &tf);
+	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
+	tf.protocol = ATA_PROT_NODATA;
+	tf.command = ATA_CMD_CHK_POWER;
+
+	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
+	if (err_mask) {
+		ata_dev_err(dev, "Check power mode failed (err_mask=0x%x)\n",
+			    err_mask);
+		/*
+		 * Assume we are in standby mode so that we always force a
+		 * spinup in ata_dev_power_set_active().
+		 */
+		return false;
+	}
+
+	ata_dev_dbg(dev, "Power mode: 0x%02x\n", tf.nsect);
+
+	/* Active or idle */
+	return tf.nsect == 0xff;
+}
+
 /**
  *	ata_dev_power_set_active -  Set a device power mode to active
  *	@dev: target device
@@ -2065,6 +2092,13 @@ void ata_dev_power_set_active(struct ata_device *dev)
 	if (!ata_dev_power_init_tf(dev, &tf, true))
 		return;
 
+	/*
+	 * Check the device power state & condition and force a spinup with
+	 * VERIFY command only if the drive is not already ACTIVE or IDLE.
+	 */
+	if (ata_dev_power_is_active(dev))
+		return;
+
 	ata_dev_notice(dev, "Entering active power mode\n");
 
 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Signature · 1 line
2.41.0
#4 lore raw

Re: [PATCH 2/2] ata: libata-core: Improve ata_dev_power_set_active()

Quoted 77 lines · Damien Le Moal

Improve the function ata_dev_power_set_active() by having it do nothing for a disk that is already in the active power state. To do that, introduce the function ata_dev_power_is_active() to test the current power state of the disk and return true if the disk is in the PM0:
active or PM1: idle state (0xff value for the count field of the CHECK POWER MODE command output).

To preserve the existing behavior, if the CHECK POWER MODE command issued in ata_dev_power_is_active() fails, the drive is assumed to be in standby mode and false is returned.

With this change, issuing the VERIFY command to access the disk media to spin it up becomes unnecessary most of the time during system resume as the port reset done by libata-eh on resume often result in the drive to spin-up (this behavior is not clearly defined by the ACS specifications and may thus vary between disk models).

---
 drivers/ata/libata-core.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
drivers/ata/libata-core.c +34 -0
index 83613280928b..6fb4e8dc8c3c 100644
@@ -2042,6 +2042,33 @@ void ata_dev_power_set_standby(struct ata_device *dev)
 			    err_mask);
 }
 
+static bool ata_dev_power_is_active(struct ata_device *dev)
+{
+	struct ata_taskfile tf;
+	unsigned int err_mask;
+
+	ata_tf_init(dev, &tf);
+	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
+	tf.protocol = ATA_PROT_NODATA;
+	tf.command = ATA_CMD_CHK_POWER;
+
+	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
+	if (err_mask) {
+		ata_dev_err(dev, "Check power mode failed (err_mask=0x%x)\n",
+			    err_mask);
+		/*
+		 * Assume we are in standby mode so that we always force a
+		 * spinup in ata_dev_power_set_active().
+		 */
+		return false;
+	}
+
+	ata_dev_dbg(dev, "Power mode: 0x%02x\n", tf.nsect);
+
+	/* Active or idle */
+	return tf.nsect == 0xff;
+}
+
 /**
  *	ata_dev_power_set_active -  Set a device power mode to active
  *	@dev: target device
@@ -2065,6 +2092,13 @@ void ata_dev_power_set_active(struct ata_device *dev)
 	if (!ata_dev_power_init_tf(dev, &tf, true))
 		return;
 
+	/*
+	 * Check the device power state & condition and force a spinup with
+	 * VERIFY command only if the drive is not already ACTIVE or IDLE.
+	 */
+	if (ata_dev_power_is_active(dev))
+		return;
+
 	ata_dev_notice(dev, "Entering active power mode\n");
 
 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Signature · 1 line
2.41.0

Hello Damien,

For a disk that is already spun up, is a READ VERIFY SECTOR(S) – 40h command with LBA == 0, NSECTORS == 1, really that much slower than a ATA_CMD_CHK_POWER command?

Kind regards,
Niklas

Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] ata: libata-core: Improve ata_dev_power_set_active()

In reply to Damien Le Moal
Quoted 77 lines · Damien Le Moal

Improve the function ata_dev_power_set_active() by having it do nothing for a disk that is already in the active power state. To do that, introduce the function ata_dev_power_is_active() to test the current power state of the disk and return true if the disk is in the PM0:
active or PM1: idle state (0xff value for the count field of the CHECK POWER MODE command output).

To preserve the existing behavior, if the CHECK POWER MODE command issued in ata_dev_power_is_active() fails, the drive is assumed to be in standby mode and false is returned.

With this change, issuing the VERIFY command to access the disk media to spin it up becomes unnecessary most of the time during system resume as the port reset done by libata-eh on resume often result in the drive to spin-up (this behavior is not clearly defined by the ACS specifications and may thus vary between disk models).

---
 drivers/ata/libata-core.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
drivers/ata/libata-core.c +34 -0
index 83613280928b..6fb4e8dc8c3c 100644
@@ -2042,6 +2042,33 @@ void ata_dev_power_set_standby(struct ata_device *dev)
 			    err_mask);
 }
 
+static bool ata_dev_power_is_active(struct ata_device *dev)
+{
+	struct ata_taskfile tf;
+	unsigned int err_mask;
+
+	ata_tf_init(dev, &tf);
+	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
+	tf.protocol = ATA_PROT_NODATA;
+	tf.command = ATA_CMD_CHK_POWER;
+
+	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
+	if (err_mask) {
+		ata_dev_err(dev, "Check power mode failed (err_mask=0x%x)\n",
+			    err_mask);
+		/*
+		 * Assume we are in standby mode so that we always force a
+		 * spinup in ata_dev_power_set_active().
+		 */
+		return false;
+	}
+
+	ata_dev_dbg(dev, "Power mode: 0x%02x\n", tf.nsect);
+
+	/* Active or idle */
+	return tf.nsect == 0xff;
+}
+
 /**
  *	ata_dev_power_set_active -  Set a device power mode to active
  *	@dev: target device
@@ -2065,6 +2092,13 @@ void ata_dev_power_set_active(struct ata_device *dev)
 	if (!ata_dev_power_init_tf(dev, &tf, true))
 		return;
 
+	/*
+	 * Check the device power state & condition and force a spinup with
+	 * VERIFY command only if the drive is not already ACTIVE or IDLE.
+	 */
+	if (ata_dev_power_is_active(dev))
+		return;
+
 	ata_dev_notice(dev, "Entering active power mode\n");
 
 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Signature · 1 line
2.41.0

From your explaination in:
https://lore.kernel.org/linux-ide/[email protected]/

The drive will be in the active power state when the drive is in the process of spinning up the drive, even if the drive is not fully spun up, and the reply to the first I/O will be delayed until the drive is fully spun up.

Therefore, this patch makes a lot of sense:

#6 lore raw

Re: [PATCH 2/2] ata: libata-core: Improve ata_dev_power_set_active()

In reply to Damien Le Moal
Quoted 7 lines · Damien Le Moal

/**
* ata_dev_power_set_active - Set a device power mode to active
* @dev: target device

drivers/ata/libata-core.c +0 -0
@@ -2065,6 +2092,13 @@ void ata_dev_power_set_active(struct ata_device *dev)
 	if (!ata_dev_power_init_tf(dev, &tf, true))
 		return;
 

drivers/ata/libata-core.c void ata_dev_power_set_active(struct ata_device *dev) · line 2104

Quoted 10 lines
drivers/ata/libata-core.c +7 -0
+	/*
+	 * Check the device power state & condition and force a spinup with
+	 * VERIFY command only if the drive is not already ACTIVE or IDLE.
+	 */
+	if (ata_dev_power_is_active(dev))
+		return;
+
 	ata_dev_notice(dev, "Entering active power mode\n");
 
 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);

This bit didn't apply cleanly to what I just pulled from Linus. It seems there are soem differences in how the tf is set up. Why not move this check to before the tf is set up? There isn't much point in setting it up if it isn't going to be used.

#7 lore raw

Re: [PATCH 2/2] ata: libata-core: Improve ata_dev_power_set_active()

Quoted 17 lines · Phillip Susi
Quoted 16 lines · Damien Le Moal

/**
* ata_dev_power_set_active - Set a device power mode to active
* @dev: target device

drivers/ata/libata-core.c +7 -0
@@ -2065,6 +2092,13 @@ void ata_dev_power_set_active(struct ata_device *dev)
 	if (!ata_dev_power_init_tf(dev, &tf, true))
 		return;
 
+	/*
+	 * Check the device power state & condition and force a spinup with
+	 * VERIFY command only if the drive is not already ACTIVE or IDLE.
+	 */
+	if (ata_dev_power_is_active(dev))
+		return;
+
 	ata_dev_notice(dev, "Entering active power mode\n");
 
Quoted 6 lines

drivers/ata/libata-core.c void ata_dev_power_set_active(struct ata_device *dev) · line 2104

Quoted 1 line
drivers/ata/libata-core.c +0 -0
 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);

This bit didn't apply cleanly to what I just pulled from Linus. It seems there are soem differences in how the tf is set up. Why not move this check to before the tf is set up? There isn't much point in setting it up if it isn't going to be used.

Hello Phillip,

This series applies cleanly to:
https://git.kernel.org/pub/scm/linux/kernel/git/dlemoal/libata.git/log/?h=for-next

The for-next branch also has a bunch of suspend/resume patches that will not go in to v6.6.

Kind regards,
Niklas

author maintainer
#8 lore raw

Re: [PATCH 2/2] ata: libata-core: Improve ata_dev_power_set_active()

In reply to Phillip Susi
Quoted 17 lines · Phillip Susi
Quoted 16 lines · Damien Le Moal

/**
* ata_dev_power_set_active - Set a device power mode to active
* @dev: target device

drivers/ata/libata-core.c +7 -0
@@ -2065,6 +2092,13 @@ void ata_dev_power_set_active(struct ata_device *dev)
 	if (!ata_dev_power_init_tf(dev, &tf, true))
 		return;
 
+	/*
+	 * Check the device power state & condition and force a spinup with
+	 * VERIFY command only if the drive is not already ACTIVE or IDLE.
+	 */
+	if (ata_dev_power_is_active(dev))
+		return;
+
 	ata_dev_notice(dev, "Entering active power mode\n");
 
Quoted 3 lines

drivers/ata/libata-core.c void ata_dev_power_set_active(struct ata_device *dev) · line 2104

Quoted 1 line
drivers/ata/libata-core.c +0 -0
 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);

This bit didn't apply cleanly to what I just pulled from Linus. It

The patches are for 6.7 so they are based on for-6.7 branch (for-next is the same).

Quoted 3 lines

seems there are soem differences in how the tf is set up. Why not move this check to before the tf is set up? There isn't much point in setting it up if it isn't going to be used.

ata_dev_power_init_tf() also checks for the device type and horkage flags and return false if the operation is not necessary or desired. I left these checks in that function instead of moving them to different places. That is easier to maintain this way. This is not the hot path, so I really prefer prioritizing simplicity here.

Signature · 2 lines
Damien Le Moal
Western Digital Research
Tested-by on the v1 series
#9 lore raw
In reply to Damien Le Moal

Hi Damien,

Quoted 6 lines · Damien Le Moal

Two patches to improve disk spinup on resume:
- Patch 1: Move the spinup operation to after the drive revalidation to ensure that the VERIFY command is executed with the link speed properly setup.
- Patch 2: Attempt to spinup the drive only if it is not already spun up.

Thanks for your series!

Quoted 5 lines

Geert,

It would be great if you could test this with your RCAR setup. I ran this on my usual machines and qemu. All good. But your setup has been useful to flush out issues before :)

Nothing special to see here...

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds