1 #include <linux/kernel.h>
4 int generic_ide_suspend(struct device *dev, pm_message_t mesg)
6 ide_drive_t *drive = dev_get_drvdata(dev);
7 ide_drive_t *pair = ide_get_pair_dev(drive);
8 ide_hwif_t *hwif = drive->hwif;
10 struct request_pm_state rqpm;
13 /* call ACPI _GTM only once */
14 if ((drive->dn & 1) == 0 || pair == NULL)
15 ide_acpi_get_timing(hwif);
17 memset(&rqpm, 0, sizeof(rqpm));
18 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
19 rq->cmd_type = REQ_TYPE_PM_SUSPEND;
21 rqpm.pm_step = IDE_PM_START_SUSPEND;
22 if (mesg.event == PM_EVENT_PRETHAW)
23 mesg.event = PM_EVENT_FREEZE;
24 rqpm.pm_state = mesg.event;
26 ret = blk_execute_rq(drive->queue, NULL, rq, 0);
29 /* call ACPI _PS3 only after both devices are suspended */
30 if (ret == 0 && ((drive->dn & 1) || pair == NULL))
31 ide_acpi_set_state(hwif, 0);
36 int generic_ide_resume(struct device *dev)
38 ide_drive_t *drive = dev_get_drvdata(dev);
39 ide_drive_t *pair = ide_get_pair_dev(drive);
40 ide_hwif_t *hwif = drive->hwif;
42 struct request_pm_state rqpm;
45 /* call ACPI _PS0 / _STM only once */
46 if ((drive->dn & 1) == 0 || pair == NULL) {
47 ide_acpi_set_state(hwif, 1);
48 ide_acpi_push_timing(hwif);
51 ide_acpi_exec_tfs(drive);
53 memset(&rqpm, 0, sizeof(rqpm));
54 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
55 rq->cmd_type = REQ_TYPE_PM_RESUME;
56 rq->cmd_flags |= REQ_PREEMPT;
58 rqpm.pm_step = IDE_PM_START_RESUME;
59 rqpm.pm_state = PM_EVENT_ON;
61 err = blk_execute_rq(drive->queue, NULL, rq, 1);
64 if (err == 0 && dev->driver) {
65 struct ide_driver *drv = to_ide_driver(dev->driver);
74 void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
76 struct request_pm_state *pm = rq->special;
79 printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
80 drive->name, pm->pm_step);
82 if (drive->media != ide_disk)
85 switch (pm->pm_step) {
86 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
87 if (pm->pm_state == PM_EVENT_FREEZE)
88 pm->pm_step = IDE_PM_COMPLETED;
90 pm->pm_step = IDE_PM_STANDBY;
92 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
93 pm->pm_step = IDE_PM_COMPLETED;
95 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
96 pm->pm_step = IDE_PM_IDLE;
98 case IDE_PM_IDLE: /* Resume step 2 (idle)*/
99 pm->pm_step = IDE_PM_RESTORE_DMA;
104 ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
106 struct request_pm_state *pm = rq->special;
107 struct ide_cmd cmd = { };
109 switch (pm->pm_step) {
110 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
111 if (drive->media != ide_disk)
113 /* Not supported? Switch to next step now. */
114 if (ata_id_flush_enabled(drive->id) == 0 ||
115 (drive->dev_flags & IDE_DFLAG_WCACHE) == 0) {
116 ide_complete_power_step(drive, rq);
119 if (ata_id_flush_ext_enabled(drive->id))
120 cmd.tf.command = ATA_CMD_FLUSH_EXT;
122 cmd.tf.command = ATA_CMD_FLUSH;
124 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
125 cmd.tf.command = ATA_CMD_STANDBYNOW1;
127 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
128 ide_set_max_pio(drive);
130 * skip IDE_PM_IDLE for ATAPI devices
132 if (drive->media != ide_disk)
133 pm->pm_step = IDE_PM_RESTORE_DMA;
135 ide_complete_power_step(drive, rq);
137 case IDE_PM_IDLE: /* Resume step 2 (idle) */
138 cmd.tf.command = ATA_CMD_IDLEIMMEDIATE;
140 case IDE_PM_RESTORE_DMA: /* Resume step 3 (restore DMA) */
142 * Right now, all we do is call ide_set_dma(drive),
143 * we could be smarter and check for current xfer_speed
144 * in struct drive etc...
146 if (drive->hwif->dma_ops == NULL)
149 * TODO: respect IDE_DFLAG_USING_DMA
155 pm->pm_step = IDE_PM_COMPLETED;
160 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
161 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
162 cmd.protocol = ATA_PROT_NODATA;
164 return do_rw_taskfile(drive, &cmd);
168 * ide_complete_pm_rq - end the current Power Management request
169 * @drive: target drive
172 * This function cleans up the current PM request and stops the queue
175 void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq)
177 struct request_queue *q = drive->queue;
178 struct request_pm_state *pm = rq->special;
181 ide_complete_power_step(drive, rq);
182 if (pm->pm_step != IDE_PM_COMPLETED)
186 printk("%s: completing PM request, %s\n", drive->name,
187 blk_pm_suspend_request(rq) ? "suspend" : "resume");
189 spin_lock_irqsave(q->queue_lock, flags);
190 if (blk_pm_suspend_request(rq))
193 drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
194 spin_unlock_irqrestore(q->queue_lock, flags);
196 drive->hwif->rq = NULL;
198 if (blk_end_request(rq, 0, 0))
202 void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
204 struct request_pm_state *pm = rq->special;
206 if (blk_pm_suspend_request(rq) &&
207 pm->pm_step == IDE_PM_START_SUSPEND)
208 /* Mark drive blocked when starting the suspend sequence. */
209 drive->dev_flags |= IDE_DFLAG_BLOCKED;
210 else if (blk_pm_resume_request(rq) &&
211 pm->pm_step == IDE_PM_START_RESUME) {
213 * The first thing we do on wakeup is to wait for BSY bit to
214 * go away (with a looong timeout) as a drive on this hwif may
215 * just be POSTing itself.
216 * We do that before even selecting as the "other" device on
217 * the bus may be broken enough to walk on our toes at this
220 ide_hwif_t *hwif = drive->hwif;
221 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
222 struct request_queue *q = drive->queue;
226 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
228 rc = ide_wait_not_busy(hwif, 35000);
230 printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
231 tp_ops->dev_select(drive);
232 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
233 rc = ide_wait_not_busy(hwif, 100000);
235 printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
237 spin_lock_irqsave(q->queue_lock, flags);
239 spin_unlock_irqrestore(q->queue_lock, flags);