blob: ad7be2669dcb9020bc5b91612079d80f21fd6869 [file] [log] [blame]
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +01001#include <linux/kernel.h>
2#include <linux/ide.h>
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +01003
4int generic_ide_suspend(struct device *dev, pm_message_t mesg)
5{
Greg Kroah-Hartmanfcb52072009-04-30 14:43:31 -07006 ide_drive_t *drive = dev_get_drvdata(dev);
7 ide_drive_t *pair = ide_get_pair_dev(drive);
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +01008 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +01009 struct request *rq;
10 struct request_pm_state rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010011 int ret;
12
Bartlomiej Zolnierkiewicz2bf427b2009-06-29 19:20:42 -070013 if (ide_port_acpi(hwif)) {
14 /* call ACPI _GTM only once */
15 if ((drive->dn & 1) == 0 || pair == NULL)
16 ide_acpi_get_timing(hwif);
17 }
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010018
19 memset(&rqpm, 0, sizeof(rqpm));
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010020 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
21 rq->cmd_type = REQ_TYPE_PM_SUSPEND;
Tejun Heofc38b522009-04-19 07:00:43 +090022 rq->special = &rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010023 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;
27
28 ret = blk_execute_rq(drive->queue, NULL, rq, 0);
29 blk_put_request(rq);
30
Bartlomiej Zolnierkiewicz2bf427b2009-06-29 19:20:42 -070031 if (ret == 0 && ide_port_acpi(hwif)) {
32 /* call ACPI _PS3 only after both devices are suspended */
33 if ((drive->dn & 1) || pair == NULL)
34 ide_acpi_set_state(hwif, 0);
35 }
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010036
37 return ret;
38}
39
40int generic_ide_resume(struct device *dev)
41{
Greg Kroah-Hartmanfcb52072009-04-30 14:43:31 -070042 ide_drive_t *drive = dev_get_drvdata(dev);
43 ide_drive_t *pair = ide_get_pair_dev(drive);
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +010044 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010045 struct request *rq;
46 struct request_pm_state rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010047 int err;
48
Bartlomiej Zolnierkiewicz2bf427b2009-06-29 19:20:42 -070049 if (ide_port_acpi(hwif)) {
50 /* call ACPI _PS0 / _STM only once */
51 if ((drive->dn & 1) == 0 || pair == NULL) {
52 ide_acpi_set_state(hwif, 1);
53 ide_acpi_push_timing(hwif);
54 }
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010055
Bartlomiej Zolnierkiewicz2bf427b2009-06-29 19:20:42 -070056 ide_acpi_exec_tfs(drive);
57 }
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010058
59 memset(&rqpm, 0, sizeof(rqpm));
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010060 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
61 rq->cmd_type = REQ_TYPE_PM_RESUME;
62 rq->cmd_flags |= REQ_PREEMPT;
Tejun Heofc38b522009-04-19 07:00:43 +090063 rq->special = &rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010064 rqpm.pm_step = IDE_PM_START_RESUME;
65 rqpm.pm_state = PM_EVENT_ON;
66
67 err = blk_execute_rq(drive->queue, NULL, rq, 1);
68 blk_put_request(rq);
69
70 if (err == 0 && dev->driver) {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +010071 struct ide_driver *drv = to_ide_driver(dev->driver);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010072
73 if (drv->resume)
74 drv->resume(drive);
75 }
76
77 return err;
78}
79
80void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
81{
Tejun Heofc38b522009-04-19 07:00:43 +090082 struct request_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010083
84#ifdef DEBUG_PM
85 printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
86 drive->name, pm->pm_step);
87#endif
88 if (drive->media != ide_disk)
89 return;
90
91 switch (pm->pm_step) {
92 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
93 if (pm->pm_state == PM_EVENT_FREEZE)
94 pm->pm_step = IDE_PM_COMPLETED;
95 else
96 pm->pm_step = IDE_PM_STANDBY;
97 break;
98 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
99 pm->pm_step = IDE_PM_COMPLETED;
100 break;
101 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
102 pm->pm_step = IDE_PM_IDLE;
103 break;
104 case IDE_PM_IDLE: /* Resume step 2 (idle)*/
105 pm->pm_step = IDE_PM_RESTORE_DMA;
106 break;
107 }
108}
109
110ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
111{
Tejun Heofc38b522009-04-19 07:00:43 +0900112 struct request_pm_state *pm = rq->special;
113 struct ide_cmd cmd = { };
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100114
115 switch (pm->pm_step) {
116 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
117 if (drive->media != ide_disk)
118 break;
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);
123 return ide_stopped;
124 }
125 if (ata_id_flush_ext_enabled(drive->id))
Tejun Heofc38b522009-04-19 07:00:43 +0900126 cmd.tf.command = ATA_CMD_FLUSH_EXT;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100127 else
Tejun Heofc38b522009-04-19 07:00:43 +0900128 cmd.tf.command = ATA_CMD_FLUSH;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100129 goto out_do_tf;
130 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
Tejun Heofc38b522009-04-19 07:00:43 +0900131 cmd.tf.command = ATA_CMD_STANDBYNOW1;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100132 goto out_do_tf;
133 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
134 ide_set_max_pio(drive);
135 /*
136 * skip IDE_PM_IDLE for ATAPI devices
137 */
138 if (drive->media != ide_disk)
139 pm->pm_step = IDE_PM_RESTORE_DMA;
140 else
141 ide_complete_power_step(drive, rq);
142 return ide_stopped;
143 case IDE_PM_IDLE: /* Resume step 2 (idle) */
Tejun Heofc38b522009-04-19 07:00:43 +0900144 cmd.tf.command = ATA_CMD_IDLEIMMEDIATE;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100145 goto out_do_tf;
146 case IDE_PM_RESTORE_DMA: /* Resume step 3 (restore DMA) */
147 /*
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...
151 */
152 if (drive->hwif->dma_ops == NULL)
153 break;
154 /*
155 * TODO: respect IDE_DFLAG_USING_DMA
156 */
157 ide_set_dma(drive);
158 break;
159 }
160
161 pm->pm_step = IDE_PM_COMPLETED;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100162
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100163 return ide_stopped;
164
165out_do_tf:
Tejun Heofc38b522009-04-19 07:00:43 +0900166 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
167 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
168 cmd.protocol = ATA_PROT_NODATA;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100169
Tejun Heofc38b522009-04-19 07:00:43 +0900170 return do_rw_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100171}
172
173/**
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100174 * ide_complete_pm_rq - end the current Power Management request
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100175 * @drive: target drive
176 * @rq: request
177 *
178 * This function cleans up the current PM request and stops the queue
179 * if necessary.
180 */
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100181void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq)
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100182{
183 struct request_queue *q = drive->queue;
Tejun Heofc38b522009-04-19 07:00:43 +0900184 struct request_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100185 unsigned long flags;
186
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100187 ide_complete_power_step(drive, rq);
188 if (pm->pm_step != IDE_PM_COMPLETED)
189 return;
190
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100191#ifdef DEBUG_PM
192 printk("%s: completing PM request, %s\n", drive->name,
193 blk_pm_suspend_request(rq) ? "suspend" : "resume");
194#endif
195 spin_lock_irqsave(q->queue_lock, flags);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100196 if (blk_pm_suspend_request(rq))
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100197 blk_stop_queue(q);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100198 else
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100199 drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100200 spin_unlock_irqrestore(q->queue_lock, flags);
201
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100202 drive->hwif->rq = NULL;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100203
204 if (blk_end_request(rq, 0, 0))
205 BUG();
206}
207
208void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
209{
Tejun Heofc38b522009-04-19 07:00:43 +0900210 struct request_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100211
212 if (blk_pm_suspend_request(rq) &&
213 pm->pm_step == IDE_PM_START_SUSPEND)
214 /* Mark drive blocked when starting the suspend sequence. */
215 drive->dev_flags |= IDE_DFLAG_BLOCKED;
216 else if (blk_pm_resume_request(rq) &&
217 pm->pm_step == IDE_PM_START_RESUME) {
218 /*
219 * The first thing we do on wakeup is to wait for BSY bit to
220 * go away (with a looong timeout) as a drive on this hwif may
221 * just be POSTing itself.
222 * We do that before even selecting as the "other" device on
223 * the bus may be broken enough to walk on our toes at this
224 * point.
225 */
226 ide_hwif_t *hwif = drive->hwif;
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200227 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100228 struct request_queue *q = drive->queue;
229 unsigned long flags;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100230 int rc;
231#ifdef DEBUG_PM
232 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
233#endif
234 rc = ide_wait_not_busy(hwif, 35000);
235 if (rc)
236 printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200237 tp_ops->dev_select(drive);
238 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100239 rc = ide_wait_not_busy(hwif, 100000);
240 if (rc)
241 printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100242
243 spin_lock_irqsave(q->queue_lock, flags);
244 blk_start_queue(q);
245 spin_unlock_irqrestore(q->queue_lock, flags);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100246 }
247}