Skip to content

Commit 2133420

Browse files
Aaron LuJeff Garzik
authored andcommitted
libata: handle power transition of ODD
When ata port is runtime suspended, it will check if the ODD attched to it is a zero power(ZP) capable ODD and if the ZP capable ODD is in zero power ready state. And if this is not the case, the highest acpi state will be limited to ACPI_STATE_D3_HOT to avoid powering off the ODD. And if the ODD can be powered off, runtime wake capability needs to be enabled and powered_off flag will be set to let resume code knows that the ODD was in powered off state. And on resume, before it is powered on, if it was powered off during suspend, runtime wake capability needs to be disabled. After it is recovered, the ODD is considered functional, post power on processing like eject tray if the ODD is drawer type is done, and several ZPODD related fields will also be reset. Signed-off-by: Aaron Lu <[email protected]> Acked-by: Tejun Heo <[email protected]> Signed-off-by: Jeff Garzik <[email protected]>
1 parent 3dc6744 commit 2133420

File tree

4 files changed

+118
-10
lines changed

4 files changed

+118
-10
lines changed

drivers/ata/libata-acpi.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,22 @@ void ata_acpi_on_resume(struct ata_port *ap)
835835
}
836836
}
837837

838+
static int ata_acpi_choose_suspend_state(struct ata_device *dev)
839+
{
840+
int d_max_in = ACPI_STATE_D3_COLD;
841+
842+
/*
843+
* For ATAPI, runtime D3 cold is only allowed
844+
* for ZPODD in zero power ready state
845+
*/
846+
if (dev->class == ATA_DEV_ATAPI &&
847+
!(zpodd_dev_enabled(dev) && zpodd_zpready(dev)))
848+
d_max_in = ACPI_STATE_D3_HOT;
849+
850+
return acpi_pm_device_sleep_state(&dev->sdev->sdev_gendev,
851+
NULL, d_max_in);
852+
}
853+
838854
/**
839855
* ata_acpi_set_state - set the port power state
840856
* @ap: target ATA port
@@ -861,17 +877,16 @@ void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
861877
continue;
862878

863879
if (state.event != PM_EVENT_ON) {
864-
acpi_state = acpi_pm_device_sleep_state(
865-
&dev->sdev->sdev_gendev, NULL, ACPI_STATE_D3);
866-
if (acpi_state > 0)
867-
acpi_bus_set_power(handle, acpi_state);
868-
/* TBD: need to check if it's runtime pm request */
869-
acpi_pm_device_run_wake(
870-
&dev->sdev->sdev_gendev, true);
880+
acpi_state = ata_acpi_choose_suspend_state(dev);
881+
if (acpi_state == ACPI_STATE_D0)
882+
continue;
883+
if (zpodd_dev_enabled(dev) &&
884+
acpi_state == ACPI_STATE_D3_COLD)
885+
zpodd_enable_run_wake(dev);
886+
acpi_bus_set_power(handle, acpi_state);
871887
} else {
872-
/* Ditto */
873-
acpi_pm_device_run_wake(
874-
&dev->sdev->sdev_gendev, false);
888+
if (zpodd_dev_enabled(dev))
889+
zpodd_disable_run_wake(dev);
875890
acpi_bus_set_power(handle, ACPI_STATE_D0);
876891
}
877892
}

drivers/ata/libata-eh.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3857,6 +3857,8 @@ int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
38573857
rc = atapi_eh_clear_ua(dev);
38583858
if (rc)
38593859
goto rest_fail;
3860+
if (zpodd_dev_enabled(dev))
3861+
zpodd_post_poweron(dev);
38603862
}
38613863
}
38623864

drivers/ata/libata-zpodd.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,26 @@ struct zpodd {
2626
bool zp_ready; /* ZP ready state */
2727
unsigned long last_ready; /* last ZP ready timestamp */
2828
bool zp_sampled; /* ZP ready state sampled */
29+
bool powered_off; /* ODD is powered off
30+
* during suspend */
2931
};
3032

33+
static int eject_tray(struct ata_device *dev)
34+
{
35+
struct ata_taskfile tf = {};
36+
const char cdb[] = { GPCMD_START_STOP_UNIT,
37+
0, 0, 0,
38+
0x02, /* LoEj */
39+
0, 0, 0, 0, 0, 0, 0,
40+
};
41+
42+
tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
43+
tf.command = ATA_CMD_PACKET;
44+
tf.protocol = ATAPI_PROT_NODATA;
45+
46+
return ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
47+
}
48+
3149
/* Per the spec, only slot type and drawer type ODD can be supported */
3250
static enum odd_mech_type zpodd_get_mech_type(struct ata_device *dev)
3351
{
@@ -149,6 +167,71 @@ void zpodd_on_suspend(struct ata_device *dev)
149167
zpodd->zp_ready = true;
150168
}
151169

170+
bool zpodd_zpready(struct ata_device *dev)
171+
{
172+
struct zpodd *zpodd = dev->zpodd;
173+
return zpodd->zp_ready;
174+
}
175+
176+
/*
177+
* Enable runtime wake capability through ACPI and set the powered_off flag,
178+
* this flag will be used during resume to decide what operations are needed
179+
* to take.
180+
*/
181+
void zpodd_enable_run_wake(struct ata_device *dev)
182+
{
183+
struct zpodd *zpodd = dev->zpodd;
184+
185+
zpodd->powered_off = true;
186+
device_set_run_wake(&dev->sdev->sdev_gendev, true);
187+
acpi_pm_device_run_wake(&dev->sdev->sdev_gendev, true);
188+
}
189+
190+
/* Disable runtime wake capability if it is enabled */
191+
void zpodd_disable_run_wake(struct ata_device *dev)
192+
{
193+
struct zpodd *zpodd = dev->zpodd;
194+
195+
if (zpodd->powered_off) {
196+
acpi_pm_device_run_wake(&dev->sdev->sdev_gendev, false);
197+
device_set_run_wake(&dev->sdev->sdev_gendev, false);
198+
}
199+
}
200+
201+
/*
202+
* Post power on processing after the ODD has been recovered. If the
203+
* ODD wasn't powered off during suspend, it doesn't do anything.
204+
*
205+
* For drawer type ODD, if it is powered on due to user pressed the
206+
* eject button, the tray needs to be ejected. This can only be done
207+
* after the ODD has been recovered, i.e. link is initialized and
208+
* device is able to process NON_DATA PIO command, as eject needs to
209+
* send command for the ODD to process.
210+
*
211+
* The from_notify flag set in wake notification handler function
212+
* zpodd_wake_dev represents if power on is due to user's action.
213+
*
214+
* For both types of ODD, several fields need to be reset.
215+
*/
216+
void zpodd_post_poweron(struct ata_device *dev)
217+
{
218+
struct zpodd *zpodd = dev->zpodd;
219+
220+
if (!zpodd->powered_off)
221+
return;
222+
223+
zpodd->powered_off = false;
224+
225+
if (zpodd->from_notify) {
226+
zpodd->from_notify = false;
227+
if (zpodd->mech_type == ODD_MECH_TYPE_DRAWER)
228+
eject_tray(dev);
229+
}
230+
231+
zpodd->zp_sampled = false;
232+
zpodd->zp_ready = false;
233+
}
234+
152235
static void zpodd_wake_dev(acpi_handle handle, u32 event, void *context)
153236
{
154237
struct ata_device *ata_dev = context;

drivers/ata/libata.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,19 @@ static inline bool zpodd_dev_enabled(struct ata_device *dev)
242242
return dev->zpodd != NULL;
243243
}
244244
void zpodd_on_suspend(struct ata_device *dev);
245+
bool zpodd_zpready(struct ata_device *dev);
246+
void zpodd_enable_run_wake(struct ata_device *dev);
247+
void zpodd_disable_run_wake(struct ata_device *dev);
248+
void zpodd_post_poweron(struct ata_device *dev);
245249
#else /* CONFIG_SATA_ZPODD */
246250
static inline void zpodd_init(struct ata_device *dev) {}
247251
static inline void zpodd_exit(struct ata_device *dev) {}
248252
static inline bool zpodd_dev_enabled(struct ata_device *dev) { return false; }
249253
static inline void zpodd_on_suspend(struct ata_device *dev) {}
254+
static inline bool zpodd_zpready(struct ata_device *dev) { return false; }
255+
static inline void zpodd_enable_run_wake(struct ata_device *dev) {}
256+
static inline void zpodd_disable_run_wake(struct ata_device *dev) {}
257+
static inline void zpodd_post_poweron(struct ata_device *dev) {}
250258
#endif /* CONFIG_SATA_ZPODD */
251259

252260
#endif /* __LIBATA_H__ */

0 commit comments

Comments
 (0)