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;
12 /* call ACPI _GTM only once */
13 if ((drive->dn & 1) == 0 || pair == NULL)
14 ide_acpi_get_timing(hwif);
16 memset(&rqpm, 0, sizeof(rqpm));
17 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
18 rq->cmd_type = REQ_TYPE_PM_SUSPEND;
20 rqpm.pm_step = IDE_PM_START_SUSPEND;
21 if (mesg.event == PM_EVENT_PRETHAW)
22 mesg.event = PM_EVENT_FREEZE;
23 rqpm.pm_state = mesg.event;
25 ret = blk_execute_rq(drive->queue, NULL, rq, 0);
28 /* call ACPI _PS3 only after both devices are suspended */
29 if (ret == 0 && ((drive->dn & 1) || pair == NULL))
30 ide_acpi_set_state(hwif, 0);
35 int generic_ide_resume(struct device *dev)
37 ide_drive_t *drive = dev->driver_data, *pair = ide_get_pair_dev(drive);
38 ide_hwif_t *hwif = drive->hwif;
40 struct request_pm_state rqpm;
43 /* call ACPI _PS0 / _STM only once */
44 if ((drive->dn & 1) == 0 || pair == NULL) {
45 ide_acpi_set_state(hwif, 1);
46 ide_acpi_push_timing(hwif);
49 ide_acpi_exec_tfs(drive);
51 memset(&rqpm, 0, sizeof(rqpm));
52 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
53 rq->cmd_type = REQ_TYPE_PM_RESUME;
54 rq->cmd_flags |= REQ_PREEMPT;
56 rqpm.pm_step = IDE_PM_START_RESUME;
57 rqpm.pm_state = PM_EVENT_ON;
59 err = blk_execute_rq(drive->queue, NULL, rq, 1);
62 if (err == 0 && dev->driver) {
63 struct ide_driver *drv = to_ide_driver(dev->driver);
72 void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
74 struct request_pm_state *pm = rq->special;
77 printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
78 drive->name, pm->pm_step);
80 if (drive->media != ide_disk)
83 switch (pm->pm_step) {
84 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
85 if (pm->pm_state == PM_EVENT_FREEZE)
86 pm->pm_step = IDE_PM_COMPLETED;
88 pm->pm_step = IDE_PM_STANDBY;
90 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
91 pm->pm_step = IDE_PM_COMPLETED;
93 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
94 pm->pm_step = IDE_PM_IDLE;
96 case IDE_PM_IDLE: /* Resume step 2 (idle)*/
97 pm->pm_step = IDE_PM_RESTORE_DMA;
102 ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
104 struct request_pm_state *pm = rq->special;
105 struct ide_cmd cmd = { };
107 switch (pm->pm_step) {
108 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
109 if (drive->media != ide_disk)
111 /* Not supported? Switch to next step now. */
112 if (ata_id_flush_enabled(drive->id) == 0 ||
113 (drive->dev_flags & IDE_DFLAG_WCACHE) == 0) {
114 ide_complete_power_step(drive, rq);
117 if (ata_id_flush_ext_enabled(drive->id))
118 cmd.tf.command = ATA_CMD_FLUSH_EXT;
120 cmd.tf.command = ATA_CMD_FLUSH;
122 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
123 cmd.tf.command = ATA_CMD_STANDBYNOW1;
125 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
126 ide_set_max_pio(drive);
128 * skip IDE_PM_IDLE for ATAPI devices
130 if (drive->media != ide_disk)
131 pm->pm_step = IDE_PM_RESTORE_DMA;
133 ide_complete_power_step(drive, rq);
135 case IDE_PM_IDLE: /* Resume step 2 (idle) */
136 cmd.tf.command = ATA_CMD_IDLEIMMEDIATE;
138 case IDE_PM_RESTORE_DMA: /* Resume step 3 (restore DMA) */
140 * Right now, all we do is call ide_set_dma(drive),
141 * we could be smarter and check for current xfer_speed
142 * in struct drive etc...
144 if (drive->hwif->dma_ops == NULL)
147 * TODO: respect IDE_DFLAG_USING_DMA
153 pm->pm_step = IDE_PM_COMPLETED;
158 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
159 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
160 cmd.protocol = ATA_PROT_NODATA;
162 return do_rw_taskfile(drive, &cmd);
166 * ide_complete_pm_rq - end the current Power Management request
167 * @drive: target drive
170 * This function cleans up the current PM request and stops the queue
173 void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq)
175 struct request_queue *q = drive->queue;
176 struct request_pm_state *pm = rq->special;
179 ide_complete_power_step(drive, rq);
180 if (pm->pm_step != IDE_PM_COMPLETED)
184 printk("%s: completing PM request, %s\n", drive->name,
185 blk_pm_suspend_request(rq) ? "suspend" : "resume");
187 spin_lock_irqsave(q->queue_lock, flags);
188 if (blk_pm_suspend_request(rq))
191 drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
192 spin_unlock_irqrestore(q->queue_lock, flags);
194 drive->hwif->rq = NULL;
196 if (blk_end_request(rq, 0, 0))
200 void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
202 struct request_pm_state *pm = rq->special;
204 if (blk_pm_suspend_request(rq) &&
205 pm->pm_step == IDE_PM_START_SUSPEND)
206 /* Mark drive blocked when starting the suspend sequence. */
207 drive->dev_flags |= IDE_DFLAG_BLOCKED;
208 else if (blk_pm_resume_request(rq) &&
209 pm->pm_step == IDE_PM_START_RESUME) {
211 * The first thing we do on wakeup is to wait for BSY bit to
212 * go away (with a looong timeout) as a drive on this hwif may
213 * just be POSTing itself.
214 * We do that before even selecting as the "other" device on
215 * the bus may be broken enough to walk on our toes at this
218 ide_hwif_t *hwif = drive->hwif;
219 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
220 struct request_queue *q = drive->queue;
224 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
226 rc = ide_wait_not_busy(hwif, 35000);
228 printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
229 tp_ops->dev_select(drive);
230 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
231 rc = ide_wait_not_busy(hwif, 100000);
233 printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
235 spin_lock_irqsave(q->queue_lock, flags);
237 spin_unlock_irqrestore(q->queue_lock, flags);