Skip to contentSkip to conversation

shutdown processing

PATCH In review Randy Dunlap 2 participants 6 messages Feb 15, 2006 – Feb 17, 2006

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

PATCH 1/1 shutdown processing

From: Randy Dunlap <[email protected]>

Add ability for SCSI drivers to invoke a shutdown method. This allows drivers to make drives safe for shutdown/poweroff, for example. Some drives need this to prevent possible problems.

---
 drivers/scsi/ahci.c        |    1 +
 drivers/scsi/ata_piix.c    |    1 +
 drivers/scsi/libata-core.c |   23 +++++++++++++++++++++++
 drivers/scsi/libata-scsi.c |    8 ++++++++
 drivers/scsi/scsi_sysfs.c  |   16 ++++++++++++++++
 include/linux/libata.h     |    2 ++
 include/scsi/scsi_host.h   |    1 +
 7 files changed, 52 insertions(+)
drivers/scsi/libata-scsi.c +8 -0
@@ -412,6 +412,14 @@ int ata_scsi_device_suspend(struct scsi_
 	return ata_device_suspend(ap, dev);
 }
 
+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
drivers/scsi/libata-core.c +23 -0
@@ -4315,6 +4315,27 @@ int ata_device_suspend(struct ata_port *
 	return 0;
 }
 
+/**
+ *	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;
+}
+
 int ata_port_start (struct ata_port *ap)
 {
 	struct device *dev = ap->host_set->dev;
@@ -5190,5 +5211,7 @@ EXPORT_SYMBOL_GPL(ata_pci_device_resume)
 
 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);
include/linux/libata.h +2 -0
@@ -454,8 +454,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 *);
+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 *);
+extern int ata_device_shutdown(struct ata_port *, struct ata_device *);
 extern int ata_ratelimit(void);
 
 /*
drivers/scsi/scsi_sysfs.c +16 -0
@@ -295,11 +295,27 @@ 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)
+		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)
include/scsi/scsi_host.h +1 -0
@@ -301,6 +301,7 @@ struct scsi_host_template {
 	 */
 	int (*resume)(struct scsi_device *);
 	int (*suspend)(struct scsi_device *);
+	int (*shutdown)(struct scsi_device *);
 
 	/*
 	 * Name of proc directory
drivers/scsi/ahci.c +1 -0
@@ -214,6 +214,7 @@ static struct scsi_host_template ahci_sh
 	.dma_boundary		= AHCI_DMA_BOUNDARY,
 	.slave_configure	= ata_scsi_slave_config,
 	.bios_param		= ata_std_bios_param,
+	.shutdown		= ata_scsi_device_shutdown,
 };
 
 static const struct ata_port_operations ahci_ops = {
drivers/scsi/ata_piix.c +1 -0
@@ -192,6 +192,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 = {

---

#1 lore raw
Quoted 7 lines · Randy Dunlap

From: Randy Dunlap <[email protected]>

Add ability for SCSI drivers to invoke a shutdown method. This allows drivers to make drives safe for shutdown/poweroff, for example. Some drives need this to prevent possible problems.

Why are you calling these from SCSI? Wouldn't ahci_pci_driver.remove() and piix_pci_driver.remove() be a proper place to perform what you are doing in ata_device_shutdown?

[...]

drivers/scsi/libata-core.c EXPORT_SYMBOL_GPL(ata_pci_device_resume) · line 5217

Quoted 6 lines

EXPORT_SYMBOL_GPL(ata_device_suspend);
EXPORT_SYMBOL_GPL(ata_device_resume);

drivers/scsi/libata-core.c +2 -0
+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);

[...]

Side note: If you would prepare the host template in libata, you wouldn't need to export these and other symbols. As was AFAIR discussed by other people before, libata could certainly hide scsi_host_template from ATA drivers entirely.

Signature · 3 lines
Stefan Richter
-=====-=-==- --=- =----
http://arcgraph.de/sr/
#2 lore raw
Quoted 7 lines · On Thu, 16 Feb 2006 09:58:33 +0100 Stefan Richter <[email protected]>…
Quoted 6 lines · Randy Dunlap

From: Randy Dunlap <[email protected]>

Add ability for SCSI drivers to invoke a shutdown method. This allows drivers to make drives safe for shutdown/poweroff, for example. Some drives need this to prevent possible problems.

Quoted 5 lines
Quoted 1 line

Why are you calling these from SCSI? Wouldn't ahci_pci_driver.remove() and piix_pci_driver.remove() be a proper place to perform what you are doing in ata_device_shutdown?

Mostly to have the scsi_device pointers available. I'll try your suggestion.

Quoted 9 lines

[...]

Quoted 6 lines

EXPORT_SYMBOL_GPL(ata_device_suspend);
EXPORT_SYMBOL_GPL(ata_device_resume);

drivers/scsi/libata-core.c +2 -0
+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);

[...]

Quoted 4 lines

Side note: If you would prepare the host template in libata, you wouldn't need to export these and other symbols. As was AFAIR discussed by other people before, libata could certainly hide scsi_host_template from ATA drivers entirely.

OK. Thanks for your review and comments.

---
~Randy

#3 lore raw
Quoted 5 lines · Randy Dunlap
Quoted 1 line · On Thu, 16 Feb 2006 09:58:33 +0100 Stefan Richter <[email protected]>…

Why are you calling these from SCSI? Wouldn't ahci_pci_driver.remove() and piix_pci_driver.remove() be a proper place to perform what you are doing in ata_device_shutdown?

Mostly to have the scsi_device pointers available.

Note that roughly as long a scsi_device exists, SCSI high-level drivers expect to be able to send commands to them. In particular, when scsi_remove_device is called, (or scsi_remove_host, which calls scsi_remove_device for all still existing devices of a host), the SCSI high-level drivers' shutdwon methods get executed. Some of them send SCSI commands. The upshot is, a SCSI low-level driver has to be able to handle newly enqueued command while it is calling scsi_remove_{device,host}. Moreover it must not block a SCSI host at this moment.

IOW the most natural order for layers to shut down would be first SCSI, then ATA. (But then, I don't really comprehend whether your shutdown code would actually collide with that of the SCSI subsystem at all.)

Signature · 3 lines
Stefan Richter
-=====-=-==- --=- =----
http://arcgraph.de/sr/
#4 lore raw
Quoted 19 lines · On Thu, 16 Feb 2006 19:56:52 +0100 Stefan Richter <[email protected]>…
Quoted 7 lines · Randy Dunlap
Quoted 3 lines · On Thu, 16 Feb 2006 09:58:33 +0100 Stefan Richter <[email protected]>…

Why are you calling these from SCSI? Wouldn't ahci_pci_driver.remove() and piix_pci_driver.remove() be a proper place to perform what you are doing in ata_device_shutdown?

Mostly to have the scsi_device pointers available.

Note that roughly as long a scsi_device exists, SCSI high-level drivers expect to be able to send commands to them. In particular, when scsi_remove_device is called, (or scsi_remove_host, which calls scsi_remove_device for all still existing devices of a host), the SCSI high-level drivers' shutdwon methods get executed. Some of them send SCSI commands. The upshot is, a SCSI low-level driver has to be able to handle newly enqueued command while it is calling scsi_remove_{device,host}. Moreover it must not block a SCSI host at this moment.

Quoted 3 lines

IOW the most natural order for layers to shut down would be first SCSI, then ATA. (But then, I don't really comprehend whether your shutdown code would actually collide with that of the SCSI subsystem at all.)

OK, now it seems like you just told me why I shouldn't use the pci_driver.remove() interface. But thanks for your comments, I do appreciate them and will dig deeper [into a twisty maze :].

anyone else care to comment on this?

---
~Randy

#5 lore raw
Quoted 2 lines · Randy Dunlap

On Thu, 16 Feb 2006 19:56:52 +0100
Stefan Richter <[email protected]> wrote:

...

Quoted 1 line
Quoted 1 line

Note that roughly as long a scsi_device exists, SCSI high-level drivers expect to be able to send commands to them.

...

Quoted 2 lines

OK, now it seems like you just told me why I shouldn't use the pci_driver.remove() interface.

:-) Really? Depends on what you are trying to solve. From what I understood, the function you require does not interact with a scsi_device.

You could also have a look into scsi_host_template.slave_destroy. This is called from SCSI core when a scsi_device is being removed. In particular, it is called _after_ the upper SCSI layers finished all their business with your device.

BTW, ata_scsi_release looks suspicious WRT what I said above. I see

	ap->ops->port_disable(ap);
	ata_host_remove(ap, 0);

there. Since libata apparently never uses scsi_remove_device, ata_host_remove's call to scsi_remove_host will call scsi_remove_device which will cause SCSI high-level drivers to run their shutdown methods. It looks wrong to me to place something like ata_port_disable before that, judging from what the comment at the definition of ata_port_disable says.

Another suspicious code: ahci_remove_one calls free_irq before ata_scsi_release.

I never looked at this code before, never even used it --- am I missing something fundamental?

Signature · 3 lines
Stefan Richter
-=====-=-==- --=- =---=
http://arcgraph.de/sr/