Skip to contentSkip to conversation

ZPODD patches

PATCH Superseded Aaron Lu 3 participants 11 messages Aug 31, 2012

Subsystems (4)

v5 of 14 Superseded Latest is v14 →

lore
Share & tools
lore

Aaron Lu

author
#0 lore raw

v5 cover ZPODD patches

v5:
Add may_power_off flag to scsi device.
Alan Stern suggested that I should not mess runtime suspend with runtime power off, but the current zpodd implementation made it not easy to seperate. So I re-wrote the zpodd implementation, the end result is, normal ODD can also enter runtime suspended state, but their power won't be removed.

v4:
Rebase on top of Linus' tree, due to this, the problem of a missing flag in v3 is gone;
Add a new function scsi_autopm_put_device_autosuspend to first mark last busy for the device and then put autosuspend it as suggested by Oliver Neukum.
Typo fix as pointed by Sergei Shtylyov.
Check can_power_off flag before any runtime pm operations in sr.

v3:
Rebase on top of scsi-misc tree;
Add the sr related patches previously in Jeff's libata tree; Re-organize the sr patches.
A problem for now: for patch
scsi: sr: support zero power ODD(ZPODD)
I can't set a flag in libata-acpi.c since a related function is missing in scsi-misc tree. Will fix this when 3.6-rc1 released.

v2:
Bug fix for v1;
Use scsi_autopm_* in sr driver instead of pm_runtime_*;

v1:
Here are some patches to make ZPODD easier to use for end users and a fix for using ZPODD with system suspend.

Aaron Lu (7):
scsi: sr: support runtime pm for ODD
block: genhd: export disk_(un)block_events
scsi: sr: block events checking when suspended for zpodd
libata: acpi: set can_power_off for both ODD and HD
scsi: pm: add may_power_off flag
scsi: sr: use may_power_off
libata: acpi: respect may_power_off flag

 block/genhd.c              |  2 ++
 drivers/ata/libata-acpi.c  | 35 +++++++++++++------
 drivers/scsi/scsi_sysfs.c  | 10 +++---
 drivers/scsi/sr.c          | 85 ++++++++++++++++++++++++++++++++++++++++++++++
 drivers/scsi/sr.h          |  1 +
 include/scsi/scsi_device.h |  2 ++
 6 files changed, 120 insertions(+), 15 deletions(-)
Signature · 1 line
1.7.11.5
author
#1 lore raw

PATCH v5 1/7 scsi: sr: support runtime pm for ODD

The ODD will be placed into suspend state when:
1 For tray type ODD, no media inside and door closed;
2 For slot type ODD, no media inside;
And together with ACPI, when we suspend the ODD's parent(the port it attached to), we will omit the power altogether to reduce power consumption(done in libata-acpi.c).

The ODD can be resumed either by user or by software.

For user to resume the suspended ODD:
1 For tray type ODD, press the eject button;
2 For slot type ODD, insert a disc;
Once such events happened, an ACPI notification will be sent and in our handler, we will resume the ODD(again in libata-acpi.c). This kind of resume requires platform and device support and is reflected by the can_power_off flag.

For software to resume the suspended ODD, we did this in ODD's open/release and check_event function.

---
 drivers/ata/libata-acpi.c  |  6 ++--
 drivers/scsi/sr.c          | 72 ++++++++++++++++++++++++++++++++++++++++++++++
 include/scsi/scsi_device.h |  1 +
 3 files changed, 77 insertions(+), 2 deletions(-)
drivers/ata/libata-acpi.c +4 -2
index 902b5a4..64ba43d 100644
@@ -985,8 +985,10 @@ static void ata_acpi_wake_dev(acpi_handle handle, u32 event, void *context)
 	struct ata_device *ata_dev = context;
 
 	if (event == ACPI_NOTIFY_DEVICE_WAKE && ata_dev &&
-			pm_runtime_suspended(&ata_dev->sdev->sdev_gendev))
-		scsi_autopm_get_device(ata_dev->sdev);
+			pm_runtime_suspended(&ata_dev->sdev->sdev_gendev)) {
+		ata_dev->sdev->wakeup_by_user = 1;
+		pm_runtime_resume(&ata_dev->sdev->sdev_gendev);
+	}
 }
 
 static void ata_acpi_add_pm_notifier(struct ata_device *dev)
drivers/scsi/sr.c +72 -0

133 lines not on this page · raw message

include/scsi/scsi_device.h +1 -0
index 9895f69..72d946f 100644
@@ -156,6 +156,7 @@ struct scsi_device {
 	unsigned is_visible:1;	/* is the device visible in sysfs */
 	unsigned can_power_off:1; /* Device supports runtime power off */
 	unsigned wce_default_on:1;	/* Cache is ON by default */
+	unsigned wakeup_by_user:1;	/* User wakes up the device */
 
 	DECLARE_BITMAP(supported_events, SDEV_EVT_MAXBITS); /* supported events */
 	struct list_head event_list;	/* asserted events */
Signature · 1 line
1.7.11.5
author
#2 lore raw

PATCH v5 2/7 block: genhd: export disk_(un)block_events

In reply to Aaron Lu

When ODD is runtime powered off, there is no meaning to check events for it, so disk_(un)block_events will be called in its suspend/resume callback.

---
 block/genhd.c | 2 ++
 1 file changed, 2 insertions(+)
block/genhd.c +2 -0
index cac7366..f630150 100644
@@ -1468,6 +1468,7 @@ void disk_block_events(struct gendisk *disk)
 
 	mutex_unlock(&ev->block_mutex);
 }
+EXPORT_SYMBOL(disk_block_events);
 
 static void __disk_unblock_events(struct gendisk *disk, bool check_now)
 {
@@ -1512,6 +1513,7 @@ void disk_unblock_events(struct gendisk *disk)
 	if (disk->ev)
 		__disk_unblock_events(disk, false);
 }
+EXPORT_SYMBOL(disk_unblock_events);
 
 /**
  * disk_flush_events - schedule immediate event checking and flushing
Signature · 1 line
1.7.11.5
author
#3 lore raw

PATCH v5 3/7 scsi: sr: block events checking when suspended for zpodd

In reply to Aaron Lu

When ODD is runtime suspended and if it can be powered off(reflected by the can_power_off flag), we will block events checking for it so that it can stay in powered off state until resumed by:
1 user pressing the eject button or inserting a disc;
2 software opening the block device.
But not by events checking that normally occurs every 2 seconds.

Old versions of udisk will poll the ODD periodically, so it is advised to inhibit the polling:

$ udisks --inhibit-polling /dev/sr0
---
 drivers/scsi/sr.c | 10 ++++++++++
 drivers/scsi/sr.h |  1 +
 2 files changed, 11 insertions(+)
drivers/scsi/sr.c +10 -0
index 04a658b..1acfe0e 100644
@@ -204,6 +204,11 @@ static int sr_suspend(struct device *dev, pm_message_t msg)
 	if (!suspend)
 		return -EBUSY;
 
+	if (cd->device->can_power_off) {
+		cd->disk_events_blocked = 1;
+		disk_block_events(cd->disk);
+	}
+
 	return 0;
 }
 
@@ -224,6 +229,11 @@ static int sr_resume(struct device *dev)
 			sr_tray_move(&cd->cdi, 1);
 	}
 
+	if (cd->disk_events_blocked) {
+		cd->disk_events_blocked = 0;
+		disk_unblock_events(cd->disk);
+	}
+
 	return 0;
 }
 
drivers/scsi/sr.h +1 -0
index 37c8f6b..9d9a76f 100644
@@ -41,6 +41,7 @@ typedef struct scsi_cd {
 	unsigned readcd_known:1;	/* drive supports READ_CD (0xbe) */
 	unsigned readcd_cdda:1;	/* reading audio data using READ_CD */
 	unsigned media_present:1;	/* media is present */
+	unsigned disk_events_blocked:1; /* disk events is blocked */
 
 	/* GET_EVENT spurious event handling, blk layer guarantees exclusion */
 	int tur_mismatch;		/* nr of get_event TUR mismatches */
Signature · 1 line
1.7.11.5
author
#4 lore raw

PATCH v5 4/7 libata: acpi: set can_power_off for both ODD and HD

In reply to Aaron Lu

Hard disk may also be runtime powered off, so set can_power_off flag for it too if condition satisfies.

---
 drivers/ata/libata-acpi.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)
drivers/ata/libata-acpi.c +17 -8
index 64ba43d..6c8f89c 100644
@@ -1005,7 +1005,7 @@ static void ata_acpi_add_pm_notifier(struct ata_device *dev)
 	if (ACPI_FAILURE(status))
 		return;
 
-	if (dev->sdev->can_power_off) {
+	if (dev->class == ATA_DEV_ATAPI && dev->sdev->can_power_off) {
 		acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
 			ata_acpi_wake_dev, dev);
 		device_set_run_wake(&dev->sdev->sdev_gendev, true);
@@ -1026,7 +1026,7 @@ static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
 	if (ACPI_FAILURE(status))
 		return;
 
-	if (dev->sdev->can_power_off) {
+	if (dev->class == ATA_DEV_ATAPI && dev->sdev->can_power_off) {
 		device_set_run_wake(&dev->sdev->sdev_gendev, false);
 		acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
 			ata_acpi_wake_dev);
@@ -1130,14 +1130,23 @@ static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev,
 
 	/*
 	 * If firmware has _PS3 or _PR3 for this device,
-	 * and this ata ODD device support device attention,
-	 * it means this device can be powered off
+	 * it means this device can be powered off runtime
 	 */
 	states = acpi_dev->power.states;
-	if ((states[ACPI_STATE_D3_HOT].flags.valid ||
-			states[ACPI_STATE_D3_COLD].flags.explicit_set) &&
-			ata_dev->flags & ATA_DFLAG_DA)
-		sdev->can_power_off = 1;
+	if (states[ACPI_STATE_D3_HOT].flags.valid ||
+			states[ACPI_STATE_D3_COLD].flags.explicit_set) {
+		/*
+		 * For ODD, it needs to support device attention or
+		 * it can't be powered up back by user
+		 */
+		if (ata_dev->class == ATA_DEV_ATAPI &&
+				ata_dev->flags & ATA_DFLAG_DA)
+			sdev->can_power_off = 1;
+
+		/* No requirement for hard disk */
+		if (ata_dev->class == ATA_DEV_ATA)
+			sdev->can_power_off = 1;
+	}
 
 	return 0;
 }
Signature · 1 line
1.7.11.5
author
#5 lore raw

PATCH v5 5/7 scsi: pm: add may_power_off flag

In reply to Aaron Lu

Add a new flag may_power_off for scsi device, it gives the user a chance to control when the device is runtime suspended, can we remove its power if possible.

And if the device can be powered off(reflected by can_power_off flag, determined by individual driver), create a sysfs entry named may_power_off to let user control the flag.

I'm planning using this flag for sr and sd.

---
 drivers/scsi/scsi_sysfs.c  | 10 ++++++----
 include/scsi/scsi_device.h |  1 +
 2 files changed, 7 insertions(+), 4 deletions(-)
drivers/scsi/scsi_sysfs.c +6 -4
index 093d4f6..92ea17c 100644
@@ -484,9 +484,6 @@ sdev_store_##field (struct device *dev, struct device_attribute *attr,	\
 }									\
 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
 
-/* Currently we don't export bit fields, but we might in future,
- * so leave this code in */
-#if 0
 /*
  * sdev_rd_attr: create a function and attribute variable for a
  * read/write bit field.
@@ -526,7 +523,7 @@ static int scsi_sdev_check_buf_bit(const char *buf)
 	} else
 		return -EINVAL;
 }
-#endif
+
 /*
  * Create the actual show/store functions and data structures.
  */
@@ -860,6 +857,8 @@ static struct device_attribute sdev_attr_queue_type_rw =
 	__ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
 	       sdev_store_queue_type_rw);
 
+sdev_rw_attr_bit (may_power_off);
+
 /**
  * scsi_sysfs_add_sdev - add scsi device to sysfs
  * @sdev:	scsi_device to add
@@ -950,6 +949,9 @@ int scsi_sysfs_add_sdev(struct scsi_device *sdev)
 		}
 	}
 
+	if (sdev->can_power_off)
+		device_create_file(&sdev->sdev_gendev, &dev_attr_may_power_off);
+
 	return error;
 }
 
include/scsi/scsi_device.h +1 -0
index 72d946f..41cec7f 100644
@@ -157,6 +157,7 @@ struct scsi_device {
 	unsigned can_power_off:1; /* Device supports runtime power off */
 	unsigned wce_default_on:1;	/* Cache is ON by default */
 	unsigned wakeup_by_user:1;	/* User wakes up the device */
+	unsigned may_power_off:1;	/* Power off is allowed by user */
 
 	DECLARE_BITMAP(supported_events, SDEV_EVT_MAXBITS); /* supported events */
 	struct list_head event_list;	/* asserted events */
Signature · 1 line
1.7.11.5
author
#6 lore raw

PATCH v5 6/7 scsi: sr: use may_power_off

In reply to Aaron Lu

With the introduction of may_power_off, when deciding if we can block events checking, may_power_off should be used.

---
 drivers/scsi/sr.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/scsi/sr.c +4 -1
index 1acfe0e..6b843d1 100644
@@ -204,7 +204,7 @@ static int sr_suspend(struct device *dev, pm_message_t msg)
 	if (!suspend)
 		return -EBUSY;
 
-	if (cd->device->can_power_off) {
+	if (cd->device->may_power_off) {
 		cd->disk_events_blocked = 1;
 		disk_block_events(cd->disk);
 	}
@@ -794,6 +794,9 @@ static int sr_probe(struct device *dev)
 	sdev_printk(KERN_DEBUG, sdev,
 		    "Attached scsi CD-ROM %s\n", cd->cdi.name);
 
+	if (cd->device->can_power_off)
+		cd->device->may_power_off = 1;
+
 	/* enable runtime pm */
 	scsi_autopm_put_device(cd->device);
 
Signature · 1 line
1.7.11.5
author
#7 lore raw

PATCH v5 7/7 libata: acpi: respect may_power_off flag

In reply to Aaron Lu

If user does not want the device being powered off when runtime suspended by setting may_power_off flag to 0, we will not choose D3 cold ACPI D-State for it.

---
 drivers/ata/libata-acpi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/ata/libata-acpi.c +3 -1
index 6c8f89c..774180d 100644
@@ -869,7 +869,9 @@ void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
 
 		if (state.event != PM_EVENT_ON) {
 			acpi_state = acpi_pm_device_sleep_state(
-				&dev->sdev->sdev_gendev, NULL, ACPI_STATE_D3);
+				&dev->sdev->sdev_gendev, NULL,
+				dev->sdev->may_power_off ?
+				ACPI_STATE_D3_COLD : ACPI_STATE_D3_HOT);
 			if (acpi_state > 0)
 				acpi_bus_set_power(handle, acpi_state);
 			/* TBD: need to check if it's runtime pm request */
Signature · 1 line
1.7.11.5
#8 lore raw
In reply to Aaron Lu
Quoted 7 lines · Aaron Lu

v5:
Add may_power_off flag to scsi device.
Alan Stern suggested that I should not mess runtime suspend with runtime power off, but the current zpodd implementation made it not easy to seperate. So I re-wrote the zpodd implementation, the end result is, normal ODD can also enter runtime suspended state, but their power won't be removed.

This looks good. I noticed only a few things:

In patch 5/7, your implementation of may_power_off is written in such a way that if the drive is already powered off when userspace clears the flag, the drive is not automatically powered back on. Maybe this is what you want?

In patch 1/7 you call both scsi_autopm_get_device() and scsi_autopm_put_device() twice in sr_check_events(). With a little rewriting it should be possible to call them only once. Just replace the "return events" lines with gotos.

What happens if you have an idle ZPODD with may_power_off clear? A regular ODD would get runtime-suspended. In the same way, a ZPODD should also be runtime-suspended but left at full power. Does patch 7/7 work this way? It seems to, but I can't tell for sure.

Alan Stern

#9 lore raw
Quoted 10 lines · Alan Stern
Quoted 7 lines · Aaron Lu

v5:
Add may_power_off flag to scsi device.
Alan Stern suggested that I should not mess runtime suspend with runtime power off, but the current zpodd implementation made it not easy to seperate. So I re-wrote the zpodd implementation, the end result is, normal ODD can also enter runtime suspended state, but their power won't be removed.

Quoted 6 lines

This looks good. I noticed only a few things:

In patch 5/7, your implementation of may_power_off is written in such a way that if the drive is already powered off when userspace clears the flag, the drive is not automatically powered back on. Maybe this is what you want?

No, actually I didn't consider this.

What about I do this when may_power_off is set to 0:
In its store function, if the device is runtime suspended(which means it is in powered off state since may_power_off was 1 before this store call) I'll set the flag to 0 and then runtime resume this device.

Quoted 5 lines

In patch 1/7 you call both scsi_autopm_get_device() and scsi_autopm_put_device() twice in sr_check_events(). With a little rewriting it should be possible to call them only once. Just replace the "return events" lines with gotos.

Thanks, will change this.

Quoted 5 lines

What happens if you have an idle ZPODD with may_power_off clear? A regular ODD would get runtime-suspended. In the same way, a ZPODD should also be runtime-suspended but left at full power. Does patch 7/7 work this way? It seems to, but I can't tell for sure.

Yes, if may_power_off is cleared, d3 cold state will not be allowed, which means it won't be powered off.

Thanks,
Aaron

#10 lore raw
Quoted 11 lines · Aaron Lu
Quoted 4 lines

In patch 5/7, your implementation of may_power_off is written in such a way that if the drive is already powered off when userspace clears the flag, the drive is not automatically powered back on. Maybe this is what you want?

No, actually I didn't consider this.

What about I do this when may_power_off is set to 0:
In its store function, if the device is runtime suspended(which means it is in powered off state since may_power_off was 1 before this store call) I'll set the flag to 0 and then runtime resume this device.

You might also want to runtime resume the device if may_power_off is changed from 0 to 1. Then it will power down when it suspends again instead of just remaining powered up.

Alan Stern