blob: ba1488bd84307bb90850977573d2e80c9778af69 [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{
6 ide_drive_t *drive = dev->driver_data, *pair = ide_get_pair_dev(drive);
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +01007 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +01008 struct request *rq;
9 struct request_pm_state rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010010 int ret;
11
12 /* call ACPI _GTM only once */
13 if ((drive->dn & 1) == 0 || pair == NULL)
14 ide_acpi_get_timing(hwif);
15
16 memset(&rqpm, 0, sizeof(rqpm));
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010017 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
18 rq->cmd_type = REQ_TYPE_PM_SUSPEND;
Tejun Heofc38b522009-04-19 07:00:43 +090019 rq->special = &rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010020 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;
24
25 ret = blk_execute_rq(drive->queue, NULL, rq, 0);
26 blk_put_request(rq);
27
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);
31
32 return ret;
33}
34
35int generic_ide_resume(struct device *dev)
36{
37 ide_drive_t *drive = dev->driver_data, *pair = ide_get_pair_dev(drive);
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +010038 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010039 struct request *rq;
40 struct request_pm_state rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010041 int err;
42
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);
47 }
48
49 ide_acpi_exec_tfs(drive);
50
51 memset(&rqpm, 0, sizeof(rqpm));
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010052 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
53 rq->cmd_type = REQ_TYPE_PM_RESUME;
54 rq->cmd_flags |= REQ_PREEMPT;
Tejun Heofc38b522009-04-19 07:00:43 +090055 rq->special = &rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010056 rqpm.pm_step = IDE_PM_START_RESUME;
57 rqpm.pm_state = PM_EVENT_ON;
58
59 err = blk_execute_rq(drive->queue, NULL, rq, 1);
60 blk_put_request(rq);
61
62 if (err == 0 && dev->driver) {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +010063 struct ide_driver *drv = to_ide_driver(dev->driver);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010064
65 if (drv->resume)
66 drv->resume(drive);
67 }
68
69 return err;
70}
71
72void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
73{
Tejun Heofc38b522009-04-19 07:00:43 +090074 struct request_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010075
76#ifdef DEBUG_PM
77 printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
78 drive->name, pm->pm_step);
79#endif
80 if (drive->media != ide_disk)
81 return;
82
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;
87 else
88 pm->pm_step = IDE_PM_STANDBY;
89 break;
90 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
91 pm->pm_step = IDE_PM_COMPLETED;
92 break;
93 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
94 pm->pm_step = IDE_PM_IDLE;
95 break;
96 case IDE_PM_IDLE: /* Resume step 2 (idle)*/
97 pm->pm_step = IDE_PM_RESTORE_DMA;
98 break;
99 }
100}
101
102ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
103{
Tejun Heofc38b522009-04-19 07:00:43 +0900104 struct request_pm_state *pm = rq->special;
105 struct ide_cmd cmd = { };
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100106
107 switch (pm->pm_step) {
108 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
109 if (drive->media != ide_disk)
110 break;
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);
115 return ide_stopped;
116 }
117 if (ata_id_flush_ext_enabled(drive->id))
Tejun Heofc38b522009-04-19 07:00:43 +0900118 cmd.tf.command = ATA_CMD_FLUSH_EXT;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100119 else
Tejun Heofc38b522009-04-19 07:00:43 +0900120 cmd.tf.command = ATA_CMD_FLUSH;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100121 goto out_do_tf;
122 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
Tejun Heofc38b522009-04-19 07:00:43 +0900123 cmd.tf.command = ATA_CMD_STANDBYNOW1;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100124 goto out_do_tf;
125 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
126 ide_set_max_pio(drive);
127 /*
128 * skip IDE_PM_IDLE for ATAPI devices
129 */
130 if (drive->media != ide_disk)
131 pm->pm_step = IDE_PM_RESTORE_DMA;
132 else
133 ide_complete_power_step(drive, rq);
134 return ide_stopped;
135 case IDE_PM_IDLE: /* Resume step 2 (idle) */
Tejun Heofc38b522009-04-19 07:00:43 +0900136 cmd.tf.command = ATA_CMD_IDLEIMMEDIATE;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100137 goto out_do_tf;
138 case IDE_PM_RESTORE_DMA: /* Resume step 3 (restore DMA) */
139 /*
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...
143 */
144 if (drive->hwif->dma_ops == NULL)
145 break;
146 /*
147 * TODO: respect IDE_DFLAG_USING_DMA
148 */
149 ide_set_dma(drive);
150 break;
151 }
152
153 pm->pm_step = IDE_PM_COMPLETED;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100154
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100155 return ide_stopped;
156
157out_do_tf:
Tejun Heofc38b522009-04-19 07:00:43 +0900158 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;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100161
Tejun Heofc38b522009-04-19 07:00:43 +0900162 return do_rw_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100163}
164
165/**
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100166 * ide_complete_pm_rq - end the current Power Management request
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100167 * @drive: target drive
168 * @rq: request
169 *
170 * This function cleans up the current PM request and stops the queue
171 * if necessary.
172 */
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100173void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq)
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100174{
175 struct request_queue *q = drive->queue;
Tejun Heofc38b522009-04-19 07:00:43 +0900176 struct request_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100177 unsigned long flags;
178
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100179 ide_complete_power_step(drive, rq);
180 if (pm->pm_step != IDE_PM_COMPLETED)
181 return;
182
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100183#ifdef DEBUG_PM
184 printk("%s: completing PM request, %s\n", drive->name,
185 blk_pm_suspend_request(rq) ? "suspend" : "resume");
186#endif
187 spin_lock_irqsave(q->queue_lock, flags);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100188 if (blk_pm_suspend_request(rq))
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100189 blk_stop_queue(q);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100190 else
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100191 drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100192 spin_unlock_irqrestore(q->queue_lock, flags);
193
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100194 drive->hwif->rq = NULL;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100195
196 if (blk_end_request(rq, 0, 0))
197 BUG();
198}
199
200void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
201{
Tejun Heofc38b522009-04-19 07:00:43 +0900202 struct request_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100203
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) {
210 /*
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
216 * point.
217 */
218 ide_hwif_t *hwif = drive->hwif;
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200219 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100220 struct request_queue *q = drive->queue;
221 unsigned long flags;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100222 int rc;
223#ifdef DEBUG_PM
224 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
225#endif
226 rc = ide_wait_not_busy(hwif, 35000);
227 if (rc)
228 printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200229 tp_ops->dev_select(drive);
230 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100231 rc = ide_wait_not_busy(hwif, 100000);
232 if (rc)
233 printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100234
235 spin_lock_irqsave(q->queue_lock, flags);
236 blk_start_queue(q);
237 spin_unlock_irqrestore(q->queue_lock, flags);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100238 }
239}