[ltp] [PATCH] 2.6.17: Unload disks heads before powering down

Henrique de Moraes Holschuh linux-thinkpad@linux-thinkpad.org
Fri, 22 Sep 2006 11:44:30 -0300


--6c2NcOVqGQ03X4Wi
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

The attached patch applies to 2.6.17.  It won't work with 2.6.18, and I
wouldn't try using them on anything else but 2.6.17.

They are based on patches found in the SuSE kernels. They are not mine.

They command the HDs to unload heads before the system is shutdown (hint:
most BIOSes, including the ThinkPads, will NOT do it for you).  They are
essential for S4 sleep, where not even the userland halt binary has a chance
to save your disk's longevity by trying to do what the kernel doesn't
(without the patches).

Patches sent to the list by request of Whoppie/ThinkWiki.org

-- 
  "One disk to rule them all, One disk to find them. One disk to bring
  them all and in the darkness grind them. In the Land of Redmond
  where the shadows lie." -- The Silicon Valley Tarot
  Henrique Holschuh

--6c2NcOVqGQ03X4Wi
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="SUSE-2.6.17-libata-ata_piix-device-shutdown.patch"

diff -uprN -Xlinux-2.6.17.orig/Documentation/dontdiff linux-2.6.17+t43/drivers/scsi/ata_piix.c linux-2.6.17+t43.patched/drivers/scsi/ata_piix.c
--- linux-2.6.17+t43/drivers/scsi/ata_piix.c	2006-06-17 15:42:08.000000000 -0300
+++ linux-2.6.17+t43.patched/drivers/scsi/ata_piix.c	2006-06-19 22:14:06.000000000 -0300
@@ -221,6 +221,7 @@ static struct scsi_host_template piix_sh
 	.bios_param		= ata_std_bios_param,
 	.resume			= ata_scsi_device_resume,
 	.suspend		= ata_scsi_device_suspend,
+	.shutdown		= ata_scsi_device_shutdown,
 };
 
 static const struct ata_port_operations piix_pata_ops = {
diff -uprN -Xlinux-2.6.17.orig/Documentation/dontdiff linux-2.6.17+t43/drivers/scsi/libata-core.c linux-2.6.17+t43.patched/drivers/scsi/libata-core.c
--- linux-2.6.17+t43/drivers/scsi/libata-core.c	2006-06-17 15:42:08.000000000 -0300
+++ linux-2.6.17+t43.patched/drivers/scsi/libata-core.c	2006-06-21 09:48:17.000000000 -0300
@@ -4332,6 +4332,27 @@ int ata_device_suspend(struct ata_port *
 }
 
 /**
+ *	ata_device_shutdown - send Standby Immediate command to drive
+ *	@ap: target ata_port
+ *	@dev: target device on the ata_port
+ *
+ *	This command makes it safe to power-off a drive.
+ *	Otherwise the heads may be flying at the wrong place
+ *	when the power is removed.
+ */
+int ata_device_shutdown(struct ata_port *ap, struct ata_device *dev)
+{
+
+	if (!ata_dev_present(dev))
+		return 0;
+
+	ata_standby_drive(ap, dev);
+	ap->flags |= ATA_FLAG_SUSPENDED;
+
+	return 0;
+}
+
+/**
  *	ata_port_start - Set port up for dma.
  *	@ap: Port to initialize
  *
@@ -4977,5 +4998,7 @@ EXPORT_SYMBOL_GPL(ata_pci_clear_simplex)
 
 EXPORT_SYMBOL_GPL(ata_device_suspend);
 EXPORT_SYMBOL_GPL(ata_device_resume);
+EXPORT_SYMBOL_GPL(ata_device_shutdown);
 EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
 EXPORT_SYMBOL_GPL(ata_scsi_device_resume);
+EXPORT_SYMBOL_GPL(ata_scsi_device_shutdown);
diff -uprN -Xlinux-2.6.17.orig/Documentation/dontdiff linux-2.6.17+t43/drivers/scsi/libata-scsi.c linux-2.6.17+t43.patched/drivers/scsi/libata-scsi.c
--- linux-2.6.17+t43/drivers/scsi/libata-scsi.c	2006-06-17 15:42:08.000000000 -0300
+++ linux-2.6.17+t43.patched/drivers/scsi/libata-scsi.c	2006-06-21 09:55:53.000000000 -0300
@@ -411,6 +411,14 @@ int ata_scsi_device_suspend(struct scsi_
 	return ata_device_suspend(ap, dev, state);
 }
 
+int ata_scsi_device_shutdown(struct scsi_device *sdev)
+{
+	struct ata_port *ap = (struct ata_port *) &sdev->host->hostdata[0];
+	struct ata_device *dev = &ap->device[sdev->id];
+
+	return ata_device_shutdown(ap, dev);
+}
+
 /**
  *	ata_to_sense_error - convert ATA error to SCSI error
  *	@id: ATA device number
diff -uprN -Xlinux-2.6.17.orig/Documentation/dontdiff linux-2.6.17+t43/drivers/scsi/scsi_sysfs.c linux-2.6.17+t43.patched/drivers/scsi/scsi_sysfs.c
--- linux-2.6.17+t43/drivers/scsi/scsi_sysfs.c	2006-06-17 15:42:08.000000000 -0300
+++ linux-2.6.17+t43.patched/drivers/scsi/scsi_sysfs.c	2006-06-21 09:53:07.000000000 -0300
@@ -304,11 +304,28 @@ static int scsi_bus_resume(struct device
 	return err;
 }
 
+static void scsi_bus_shutdown(struct device * dev)
+{
+	struct scsi_device *sdev = to_scsi_device(dev);
+	struct scsi_host_template *sht = sdev->host->hostt;
+	int err;
+
+	err = scsi_device_quiesce(sdev);
+	if (err)
+		printk(KERN_DEBUG "%s: error (0x%x) during shutdown\n",
+			__FUNCTION__, err);
+
+	if (sht->shutdown)
+		err = sht->shutdown(sdev);
+
+}
+
 struct bus_type scsi_bus_type = {
         .name		= "scsi",
         .match		= scsi_bus_match,
 	.suspend	= scsi_bus_suspend,
 	.resume		= scsi_bus_resume,
+	.shutdown	= scsi_bus_shutdown,
 };
 
 int scsi_sysfs_register(void)
diff -uprN -Xlinux-2.6.17.orig/Documentation/dontdiff linux-2.6.17+t43/include/linux/libata.h linux-2.6.17+t43.patched/include/linux/libata.h
--- linux-2.6.17+t43/include/linux/libata.h	2006-06-17 15:42:16.000000000 -0300
+++ linux-2.6.17+t43.patched/include/linux/libata.h	2006-06-21 09:54:52.000000000 -0300
@@ -529,8 +529,10 @@ extern int ata_scsi_release(struct Scsi_
 extern unsigned int ata_host_intr(struct ata_port *ap, struct ata_queued_cmd *qc);
 extern int ata_scsi_device_resume(struct scsi_device *);
 extern int ata_scsi_device_suspend(struct scsi_device *, pm_message_t state);
+extern int ata_scsi_device_shutdown(struct scsi_device *);
 extern int ata_device_resume(struct ata_port *, struct ata_device *);
 extern int ata_device_suspend(struct ata_port *, struct ata_device *, pm_message_t state);
+extern int ata_device_shutdown(struct ata_port *, struct ata_device *);
 extern int ata_ratelimit(void);
 extern unsigned int ata_busy_sleep(struct ata_port *ap,
 				   unsigned long timeout_pat,
diff -uprN -Xlinux-2.6.17.orig/Documentation/dontdiff linux-2.6.17+t43/include/scsi/scsi_host.h linux-2.6.17+t43.patched/include/scsi/scsi_host.h
--- linux-2.6.17+t43/include/scsi/scsi_host.h	2006-06-17 15:42:18.000000000 -0300
+++ linux-2.6.17+t43.patched/include/scsi/scsi_host.h	2006-06-21 09:57:11.000000000 -0300
@@ -286,6 +286,7 @@ struct scsi_host_template {
 	 */
 	int (*resume)(struct scsi_device *);
 	int (*suspend)(struct scsi_device *, pm_message_t state);
+	int (*shutdown)(struct scsi_device *);
 
 	/*
 	 * Name of proc directory

--6c2NcOVqGQ03X4Wi--