LKML Archive on
help / color / mirror / Atom feed
From: Robert Hancock <>
To: linux-kernel <>,,
Subject: [PATCH RFC] sd: spin down disks on removal or power-down
Date: Sun, 28 Jan 2007 19:47:27 -0600	[thread overview]
Message-ID: <> (raw)

[-- Attachment #1: Type: text/plain, Size: 1639 bytes --]

Here's a patch for sd.c I've cooked up which issues a START STOP UNIT
command to stop the drive when the SCSI disk is removed or the machine
is powered down. The rationale behind this is that apparently on many
drives, simply cutting power to the spinning disk forces it to do an
emergency head park/unload which creates more wear on the drive then a
controlled park/unload with power still applied. This change ensures
that the drive will be spun down if the user shuts down the machine or
if they are about to hot-unplug the drive and have done "scsiadd -r" first.

The main potential concern I have about this implementation is that if
the drive is used in a multi-initiator, iSCSI, etc. environment,
stopping the drive may be undesirable as another initiator may still be
accessing it. I'm not familiar enough with these setups to know if this
problem is likely to come up or not. For this and other reasons we may
want to make this behavior controllable - I'm open to suggestions on how
to do this or whether it's needed.

I've tested that this does work on SATA disks through libata (with my
patch "libata: fix translation for START STOP UNIT" applied). I also
tested with some external USB-to-IDE drive enclosures. The support for
START STOP UNIT on those seems rather poor though, on one enclosure with
a Genesys bridge chip it returned a check condition with "Invalid field
in CDB", and on another with a JMicron chip the request succeeded but it
didn't actually spin the drive down.

Robert Hancock      Saskatoon, SK, Canada
To email, remove "nospam" from
Home Page:

[-- Attachment #2: sd-stop-unit-on-shutdown.patch --]
[-- Type: text/plain, Size: 1792 bytes --]

--- linux-2.6.20-rc6nv/drivers/scsi/sd.c	2007-01-28 17:00:00.000000000 -0600
+++ linux-2.6.20-rc6nvedit/drivers/scsi/sd.c	2007-01-28 18:08:53.000000000 -0600
@@ -821,6 +821,39 @@ static int sd_sync_cache(struct scsi_dev
 	return res;
+static int sd_stop_unit(struct scsi_device *sdp, struct scsi_disk* sdkp)
+	int res;
+	struct scsi_sense_hdr sshdr;
+	unsigned char cmd[10] = { 0 };
+	if (!scsi_device_online(sdp))
+		return -ENODEV;
+	cmd[0] = START_STOP;
+	/*
+	 * Leave the rest of the command zero to indicate
+	 * transition to stopped power condition and return
+	 * on completion.
+	 */
+	printk(KERN_NOTICE "Stopping SCSI disk %s\n",
+		sdkp->disk->disk_name);
+	res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
+	if (res) {
+		printk(KERN_WARNING "sd stop failed: status = %x, message = %02x, "
+				    "host = %d, driver = %02x\n",
+				    status_byte(res), msg_byte(res),
+				    host_byte(res), driver_byte(res));
+		if (driver_byte(res) & DRIVER_SENSE)
+			scsi_print_sense_hdr("sd", &sshdr);
+	}
+	return res;
 static int sd_issue_flush(struct device *dev, sector_t *error_sector)
 	int ret = 0;
@@ -1727,11 +1760,13 @@ static int sd_probe(struct device *dev)
 static int sd_remove(struct device *dev)
+	struct scsi_device *sdp = to_scsi_device(dev);
 	struct scsi_disk *sdkp = dev_get_drvdata(dev);
+	sd_stop_unit(sdp,sdkp);
 	dev_set_drvdata(dev, NULL);
@@ -1784,6 +1819,9 @@ static void sd_shutdown(struct device *d
+	if(system_state == SYSTEM_POWER_OFF)
+		sd_stop_unit(sdp,sdkp);

             reply	other threads:[~2007-01-29  1:48 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-01-29  1:47 Robert Hancock [this message]
2007-01-29 23:47 ` Andrew Morton
2007-01-29 23:55   ` Robert Hancock
2007-01-30  0:16   ` James Bottomley
2007-01-30  0:33     ` Robert Hancock
2007-01-31 20:21       ` Stefan Richter
2007-01-31 23:45         ` Robert Hancock

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \ \ \
    --subject='Re: [PATCH RFC] sd: spin down disks on removal or power-down' \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).