1 #include <linux/kernel.h>
4 int generic_ide_suspend(struct device *dev, pm_message_t mesg)
6 ide_drive_t *drive = dev->driver_data, *pair = ide_get_pair_dev(drive);
7 ide_hwif_t *hwif = drive->hwif;
9 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 memset(&cmd, 0, sizeof(cmd));
19 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
20 rq->cmd_type = REQ_TYPE_PM_SUSPEND;
23 rqpm.pm_step = IDE_PM_START_SUSPEND;
24 if (mesg.event == PM_EVENT_PRETHAW)
25 mesg.event = PM_EVENT_FREEZE;
26 rqpm.pm_state = mesg.event;
28 ret = blk_execute_rq(drive->queue, NULL, rq, 0);
31 /* call ACPI _PS3 only after both devices are suspended */
32 if (ret == 0 && ((drive->dn & 1) || pair == NULL))
33 ide_acpi_set_state(hwif, 0);
38 int generic_ide_resume(struct device *dev)
40 ide_drive_t *drive = dev->driver_data, *pair = ide_get_pair_dev(drive);
41 ide_hwif_t *hwif = drive->hwif;
43 struct request_pm_state rqpm;
47 /* call ACPI _PS0 / _STM only once */
48 if ((drive->dn & 1) == 0 || pair == NULL) {
49 ide_acpi_set_state(hwif, 1);
50 ide_acpi_push_timing(hwif);
53 ide_acpi_exec_tfs(drive);
55 memset(&rqpm, 0, sizeof(rqpm));
56 memset(&cmd, 0, sizeof(cmd));
57 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
58 rq->cmd_type = REQ_TYPE_PM_RESUME;
59 rq->cmd_flags |= REQ_PREEMPT;
62 rqpm.pm_step = IDE_PM_START_RESUME;
63 rqpm.pm_state = PM_EVENT_ON;
65 err = blk_execute_rq(drive->queue, NULL, rq, 1);
68 if (err == 0 && dev->driver) {
69 struct ide_driver *drv = to_ide_driver(dev->driver);
78 void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
80 struct request_pm_state *pm = rq->data;
83 printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
84 drive->name, pm->pm_step);
86 if (drive->media != ide_disk)
89 switch (pm->pm_step) {
90 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
91 if (pm->pm_state == PM_EVENT_FREEZE)
92 pm->pm_step = IDE_PM_COMPLETED;
94 pm->pm_step = IDE_PM_STANDBY;
96 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
97 pm->pm_step = IDE_PM_COMPLETED;
99 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
100 pm->pm_step = IDE_PM_IDLE;
102 case IDE_PM_IDLE: /* Resume step 2 (idle)*/
103 pm->pm_step = IDE_PM_RESTORE_DMA;
108 ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
110 struct request_pm_state *pm = rq->data;
111 struct ide_cmd *cmd = rq->special;
113 memset(cmd, 0, sizeof(*cmd));
115 switch (pm->pm_step) {
116 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
117 if (drive->media != ide_disk)
119 /* Not supported? Switch to next step now. */
120 if (ata_id_flush_enabled(drive->id) == 0 ||
121 (drive->dev_flags & IDE_DFLAG_WCACHE) == 0) {
122 ide_complete_power_step(drive, rq);
125 if (ata_id_flush_ext_enabled(drive->id))
126 cmd->tf.command = ATA_CMD_FLUSH_EXT;
128 cmd->tf.command = ATA_CMD_FLUSH;
130 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
131 cmd->tf.command = ATA_CMD_STANDBYNOW1;
133 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
134 ide_set_max_pio(drive);
136 * skip IDE_PM_IDLE for ATAPI devices
138 if (drive->media != ide_disk)
139 pm->pm_step = IDE_PM_RESTORE_DMA;
141 ide_complete_power_step(drive, rq);
143 case IDE_PM_IDLE: /* Resume step 2 (idle) */
144 cmd->tf.command = ATA_CMD_IDLEIMMEDIATE;
146 case IDE_PM_RESTORE_DMA: /* Resume step 3 (restore DMA) */
148 * Right now, all we do is call ide_set_dma(drive),
149 * we could be smarter and check for current xfer_speed
150 * in struct drive etc...
152 if (drive->hwif->dma_ops == NULL)
155 * TODO: respect IDE_DFLAG_USING_DMA
161 pm->pm_step = IDE_PM_COMPLETED;
166 cmd->tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
167 cmd->protocol = ATA_PROT_NODATA;
169 return do_rw_taskfile(drive, cmd);
173 * ide_complete_pm_rq - end the current Power Management request
174 * @drive: target drive
177 * This function cleans up the current PM request and stops the queue
180 void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq)
182 struct request_queue *q = drive->queue;
183 struct request_pm_state *pm = rq->data;
186 ide_complete_power_step(drive, rq);
187 if (pm->pm_step != IDE_PM_COMPLETED)
191 printk("%s: completing PM request, %s\n", drive->name,
192 blk_pm_suspend_request(rq) ? "suspend" : "resume");
194 spin_lock_irqsave(q->queue_lock, flags);
195 if (blk_pm_suspend_request(rq))
198 drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
199 spin_unlock_irqrestore(q->queue_lock, flags);
201 drive->hwif->rq = NULL;
203 if (blk_end_request(rq, 0, 0))
207 void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
209 struct request_pm_state *pm = rq->data;
211 if (blk_pm_suspend_request(rq) &&
212 pm->pm_step == IDE_PM_START_SUSPEND)
213 /* Mark drive blocked when starting the suspend sequence. */
214 drive->dev_flags |= IDE_DFLAG_BLOCKED;
215 else if (blk_pm_resume_request(rq) &&
216 pm->pm_step == IDE_PM_START_RESUME) {
218 * The first thing we do on wakeup is to wait for BSY bit to
219 * go away (with a looong timeout) as a drive on this hwif may
220 * just be POSTing itself.
221 * We do that before even selecting as the "other" device on
222 * the bus may be broken enough to walk on our toes at this
225 ide_hwif_t *hwif = drive->hwif;
226 struct request_queue *q = drive->queue;
230 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
232 rc = ide_wait_not_busy(hwif, 35000);
234 printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
236 hwif->tp_ops->set_irq(hwif, 1);
237 rc = ide_wait_not_busy(hwif, 100000);
239 printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
241 spin_lock_irqsave(q->queue_lock, flags);
243 spin_unlock_irqrestore(q->queue_lock, flags);