1 #include <linux/kernel.h>
3 #include <linux/hdreg.h>
5 int generic_ide_suspend(struct device
*dev
, pm_message_t mesg
)
7 ide_drive_t
*drive
= dev
->driver_data
, *pair
= ide_get_pair_dev(drive
);
8 ide_hwif_t
*hwif
= drive
->hwif
;
10 struct request_pm_state rqpm
;
14 /* call ACPI _GTM only once */
15 if ((drive
->dn
& 1) == 0 || pair
== NULL
)
16 ide_acpi_get_timing(hwif
);
18 memset(&rqpm
, 0, sizeof(rqpm
));
19 memset(&args
, 0, sizeof(args
));
20 rq
= blk_get_request(drive
->queue
, READ
, __GFP_WAIT
);
21 rq
->cmd_type
= REQ_TYPE_PM_SUSPEND
;
24 rqpm
.pm_step
= IDE_PM_START_SUSPEND
;
25 if (mesg
.event
== PM_EVENT_PRETHAW
)
26 mesg
.event
= PM_EVENT_FREEZE
;
27 rqpm
.pm_state
= mesg
.event
;
29 ret
= blk_execute_rq(drive
->queue
, NULL
, rq
, 0);
32 /* call ACPI _PS3 only after both devices are suspended */
33 if (ret
== 0 && ((drive
->dn
& 1) || pair
== NULL
))
34 ide_acpi_set_state(hwif
, 0);
39 int generic_ide_resume(struct device
*dev
)
41 ide_drive_t
*drive
= dev
->driver_data
, *pair
= ide_get_pair_dev(drive
);
42 ide_hwif_t
*hwif
= drive
->hwif
;
44 struct request_pm_state rqpm
;
48 /* call ACPI _PS0 / _STM only once */
49 if ((drive
->dn
& 1) == 0 || pair
== NULL
) {
50 ide_acpi_set_state(hwif
, 1);
51 ide_acpi_push_timing(hwif
);
54 ide_acpi_exec_tfs(drive
);
56 memset(&rqpm
, 0, sizeof(rqpm
));
57 memset(&args
, 0, sizeof(args
));
58 rq
= blk_get_request(drive
->queue
, READ
, __GFP_WAIT
);
59 rq
->cmd_type
= REQ_TYPE_PM_RESUME
;
60 rq
->cmd_flags
|= REQ_PREEMPT
;
63 rqpm
.pm_step
= IDE_PM_START_RESUME
;
64 rqpm
.pm_state
= PM_EVENT_ON
;
66 err
= blk_execute_rq(drive
->queue
, NULL
, rq
, 1);
69 if (err
== 0 && dev
->driver
) {
70 struct ide_driver
*drv
= to_ide_driver(dev
->driver
);
79 void ide_complete_power_step(ide_drive_t
*drive
, struct request
*rq
)
81 struct request_pm_state
*pm
= rq
->data
;
84 printk(KERN_INFO
"%s: complete_power_step(step: %d)\n",
85 drive
->name
, pm
->pm_step
);
87 if (drive
->media
!= ide_disk
)
90 switch (pm
->pm_step
) {
91 case IDE_PM_FLUSH_CACHE
: /* Suspend step 1 (flush cache) */
92 if (pm
->pm_state
== PM_EVENT_FREEZE
)
93 pm
->pm_step
= IDE_PM_COMPLETED
;
95 pm
->pm_step
= IDE_PM_STANDBY
;
97 case IDE_PM_STANDBY
: /* Suspend step 2 (standby) */
98 pm
->pm_step
= IDE_PM_COMPLETED
;
100 case IDE_PM_RESTORE_PIO
: /* Resume step 1 (restore PIO) */
101 pm
->pm_step
= IDE_PM_IDLE
;
103 case IDE_PM_IDLE
: /* Resume step 2 (idle)*/
104 pm
->pm_step
= IDE_PM_RESTORE_DMA
;
109 ide_startstop_t
ide_start_power_step(ide_drive_t
*drive
, struct request
*rq
)
111 struct request_pm_state
*pm
= rq
->data
;
112 ide_task_t
*args
= rq
->special
;
114 memset(args
, 0, sizeof(*args
));
116 switch (pm
->pm_step
) {
117 case IDE_PM_FLUSH_CACHE
: /* Suspend step 1 (flush cache) */
118 if (drive
->media
!= ide_disk
)
120 /* Not supported? Switch to next step now. */
121 if (ata_id_flush_enabled(drive
->id
) == 0 ||
122 (drive
->dev_flags
& IDE_DFLAG_WCACHE
) == 0) {
123 ide_complete_power_step(drive
, rq
);
126 if (ata_id_flush_ext_enabled(drive
->id
))
127 args
->tf
.command
= ATA_CMD_FLUSH_EXT
;
129 args
->tf
.command
= ATA_CMD_FLUSH
;
131 case IDE_PM_STANDBY
: /* Suspend step 2 (standby) */
132 args
->tf
.command
= ATA_CMD_STANDBYNOW1
;
134 case IDE_PM_RESTORE_PIO
: /* Resume step 1 (restore PIO) */
135 ide_set_max_pio(drive
);
137 * skip IDE_PM_IDLE for ATAPI devices
139 if (drive
->media
!= ide_disk
)
140 pm
->pm_step
= IDE_PM_RESTORE_DMA
;
142 ide_complete_power_step(drive
, rq
);
144 case IDE_PM_IDLE
: /* Resume step 2 (idle) */
145 args
->tf
.command
= ATA_CMD_IDLEIMMEDIATE
;
147 case IDE_PM_RESTORE_DMA
: /* Resume step 3 (restore DMA) */
149 * Right now, all we do is call ide_set_dma(drive),
150 * we could be smarter and check for current xfer_speed
151 * in struct drive etc...
153 if (drive
->hwif
->dma_ops
== NULL
)
156 * TODO: respect IDE_DFLAG_USING_DMA
162 pm
->pm_step
= IDE_PM_COMPLETED
;
166 args
->tf_flags
= IDE_TFLAG_TF
| IDE_TFLAG_DEVICE
;
167 args
->data_phase
= TASKFILE_NO_DATA
;
168 return do_rw_taskfile(drive
, args
);
172 * ide_complete_pm_request - end the current Power Management request
173 * @drive: target drive
176 * This function cleans up the current PM request and stops the queue
179 void ide_complete_pm_request(ide_drive_t
*drive
, struct request
*rq
)
181 struct request_queue
*q
= drive
->queue
;
185 printk("%s: completing PM request, %s\n", drive
->name
,
186 blk_pm_suspend_request(rq
) ? "suspend" : "resume");
188 spin_lock_irqsave(q
->queue_lock
, flags
);
189 if (blk_pm_suspend_request(rq
))
192 drive
->dev_flags
&= ~IDE_DFLAG_BLOCKED
;
193 spin_unlock_irqrestore(q
->queue_lock
, flags
);
195 drive
->hwif
->rq
= NULL
;
197 if (blk_end_request(rq
, 0, 0))
201 void ide_check_pm_state(ide_drive_t
*drive
, struct request
*rq
)
203 struct request_pm_state
*pm
= rq
->data
;
205 if (blk_pm_suspend_request(rq
) &&
206 pm
->pm_step
== IDE_PM_START_SUSPEND
)
207 /* Mark drive blocked when starting the suspend sequence. */
208 drive
->dev_flags
|= IDE_DFLAG_BLOCKED
;
209 else if (blk_pm_resume_request(rq
) &&
210 pm
->pm_step
== IDE_PM_START_RESUME
) {
212 * The first thing we do on wakeup is to wait for BSY bit to
213 * go away (with a looong timeout) as a drive on this hwif may
214 * just be POSTing itself.
215 * We do that before even selecting as the "other" device on
216 * the bus may be broken enough to walk on our toes at this
219 ide_hwif_t
*hwif
= drive
->hwif
;
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
);
230 hwif
->tp_ops
->set_irq(hwif
, 1);
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
);