blob: 92406097efebcb5e0052fba38a0f59b5eb8c45eb [file] [log] [blame]
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +01001#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09002#include <linux/gfp.h>
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +01003#include <linux/ide.h>
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +01004
5int generic_ide_suspend(struct device *dev, pm_message_t mesg)
6{
Greg Kroah-Hartmanfcb52072009-04-30 14:43:31 -07007 ide_drive_t *drive = dev_get_drvdata(dev);
8 ide_drive_t *pair = ide_get_pair_dev(drive);
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +01009 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010010 struct request *rq;
11 struct request_pm_state rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010012 int ret;
13
Bartlomiej Zolnierkiewicz2bf427b2009-06-29 19:20:42 -070014 if (ide_port_acpi(hwif)) {
15 /* call ACPI _GTM only once */
16 if ((drive->dn & 1) == 0 || pair == NULL)
17 ide_acpi_get_timing(hwif);
18 }
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010019
20 memset(&rqpm, 0, sizeof(rqpm));
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010021 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
22 rq->cmd_type = REQ_TYPE_PM_SUSPEND;
Tejun Heofc38b522009-04-19 07:00:43 +090023 rq->special = &rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010024 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;
28
29 ret = blk_execute_rq(drive->queue, NULL, rq, 0);
30 blk_put_request(rq);
31
Bartlomiej Zolnierkiewicz2bf427b2009-06-29 19:20:42 -070032 if (ret == 0 && ide_port_acpi(hwif)) {
33 /* call ACPI _PS3 only after both devices are suspended */
34 if ((drive->dn & 1) || pair == NULL)
35 ide_acpi_set_state(hwif, 0);
36 }
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010037
38 return ret;
39}
40
41int generic_ide_resume(struct device *dev)
42{
Greg Kroah-Hartmanfcb52072009-04-30 14:43:31 -070043 ide_drive_t *drive = dev_get_drvdata(dev);
44 ide_drive_t *pair = ide_get_pair_dev(drive);
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +010045 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010046 struct request *rq;
47 struct request_pm_state rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010048 int err;
49
Bartlomiej Zolnierkiewicz2bf427b2009-06-29 19:20:42 -070050 if (ide_port_acpi(hwif)) {
51 /* call ACPI _PS0 / _STM only once */
52 if ((drive->dn & 1) == 0 || pair == NULL) {
53 ide_acpi_set_state(hwif, 1);
54 ide_acpi_push_timing(hwif);
55 }
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010056
Bartlomiej Zolnierkiewicz2bf427b2009-06-29 19:20:42 -070057 ide_acpi_exec_tfs(drive);
58 }
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010059
60 memset(&rqpm, 0, sizeof(rqpm));
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010061 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
62 rq->cmd_type = REQ_TYPE_PM_RESUME;
63 rq->cmd_flags |= REQ_PREEMPT;
Tejun Heofc38b522009-04-19 07:00:43 +090064 rq->special = &rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010065 rqpm.pm_step = IDE_PM_START_RESUME;
66 rqpm.pm_state = PM_EVENT_ON;
67
68 err = blk_execute_rq(drive->queue, NULL, rq, 1);
69 blk_put_request(rq);
70
71 if (err == 0 && dev->driver) {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +010072 struct ide_driver *drv = to_ide_driver(dev->driver);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010073
74 if (drv->resume)
75 drv->resume(drive);
76 }
77
78 return err;
79}
80
81void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
82{
Tejun Heofc38b522009-04-19 07:00:43 +090083 struct request_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010084
85#ifdef DEBUG_PM
86 printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
87 drive->name, pm->pm_step);
88#endif
89 if (drive->media != ide_disk)
90 return;
91
92 switch (pm->pm_step) {
93 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
94 if (pm->pm_state == PM_EVENT_FREEZE)
95 pm->pm_step = IDE_PM_COMPLETED;
96 else
97 pm->pm_step = IDE_PM_STANDBY;
98 break;
99 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
100 pm->pm_step = IDE_PM_COMPLETED;
101 break;
102 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
103 pm->pm_step = IDE_PM_IDLE;
104 break;
105 case IDE_PM_IDLE: /* Resume step 2 (idle)*/
106 pm->pm_step = IDE_PM_RESTORE_DMA;
107 break;
108 }
109}
110
111ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
112{
Tejun Heofc38b522009-04-19 07:00:43 +0900113 struct request_pm_state *pm = rq->special;
114 struct ide_cmd cmd = { };
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100115
116 switch (pm->pm_step) {
117 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
118 if (drive->media != ide_disk)
119 break;
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);
124 return ide_stopped;
125 }
126 if (ata_id_flush_ext_enabled(drive->id))
Tejun Heofc38b522009-04-19 07:00:43 +0900127 cmd.tf.command = ATA_CMD_FLUSH_EXT;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100128 else
Tejun Heofc38b522009-04-19 07:00:43 +0900129 cmd.tf.command = ATA_CMD_FLUSH;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100130 goto out_do_tf;
131 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
Tejun Heofc38b522009-04-19 07:00:43 +0900132 cmd.tf.command = ATA_CMD_STANDBYNOW1;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100133 goto out_do_tf;
134 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
135 ide_set_max_pio(drive);
136 /*
137 * skip IDE_PM_IDLE for ATAPI devices
138 */
139 if (drive->media != ide_disk)
140 pm->pm_step = IDE_PM_RESTORE_DMA;
141 else
142 ide_complete_power_step(drive, rq);
143 return ide_stopped;
144 case IDE_PM_IDLE: /* Resume step 2 (idle) */
Tejun Heofc38b522009-04-19 07:00:43 +0900145 cmd.tf.command = ATA_CMD_IDLEIMMEDIATE;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100146 goto out_do_tf;
147 case IDE_PM_RESTORE_DMA: /* Resume step 3 (restore DMA) */
148 /*
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...
152 */
153 if (drive->hwif->dma_ops == NULL)
154 break;
155 /*
156 * TODO: respect IDE_DFLAG_USING_DMA
157 */
158 ide_set_dma(drive);
159 break;
160 }
161
162 pm->pm_step = IDE_PM_COMPLETED;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100163
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100164 return ide_stopped;
165
166out_do_tf:
Tejun Heofc38b522009-04-19 07:00:43 +0900167 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
168 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
169 cmd.protocol = ATA_PROT_NODATA;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100170
Tejun Heofc38b522009-04-19 07:00:43 +0900171 return do_rw_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100172}
173
174/**
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100175 * ide_complete_pm_rq - end the current Power Management request
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100176 * @drive: target drive
177 * @rq: request
178 *
179 * This function cleans up the current PM request and stops the queue
180 * if necessary.
181 */
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100182void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq)
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100183{
184 struct request_queue *q = drive->queue;
Tejun Heofc38b522009-04-19 07:00:43 +0900185 struct request_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100186 unsigned long flags;
187
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100188 ide_complete_power_step(drive, rq);
189 if (pm->pm_step != IDE_PM_COMPLETED)
190 return;
191
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100192#ifdef DEBUG_PM
193 printk("%s: completing PM request, %s\n", drive->name,
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200194 (rq->cmd_type == REQ_TYPE_PM_SUSPEND) ? "suspend" : "resume");
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100195#endif
196 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200197 if (rq->cmd_type == REQ_TYPE_PM_SUSPEND)
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100198 blk_stop_queue(q);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100199 else
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100200 drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100201 spin_unlock_irqrestore(q->queue_lock, flags);
202
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100203 drive->hwif->rq = NULL;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100204
205 if (blk_end_request(rq, 0, 0))
206 BUG();
207}
208
209void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
210{
Tejun Heofc38b522009-04-19 07:00:43 +0900211 struct request_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100212
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200213 if (rq->cmd_type == REQ_TYPE_PM_SUSPEND &&
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100214 pm->pm_step == IDE_PM_START_SUSPEND)
215 /* Mark drive blocked when starting the suspend sequence. */
216 drive->dev_flags |= IDE_DFLAG_BLOCKED;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200217 else if (rq->cmd_type == REQ_TYPE_PM_RESUME &&
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100218 pm->pm_step == IDE_PM_START_RESUME) {
219 /*
220 * The first thing we do on wakeup is to wait for BSY bit to
221 * go away (with a looong timeout) as a drive on this hwif may
222 * just be POSTing itself.
223 * We do that before even selecting as the "other" device on
224 * the bus may be broken enough to walk on our toes at this
225 * point.
226 */
227 ide_hwif_t *hwif = drive->hwif;
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200228 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100229 struct request_queue *q = drive->queue;
230 unsigned long flags;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100231 int rc;
232#ifdef DEBUG_PM
233 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
234#endif
235 rc = ide_wait_not_busy(hwif, 35000);
236 if (rc)
237 printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200238 tp_ops->dev_select(drive);
239 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100240 rc = ide_wait_not_busy(hwif, 100000);
241 if (rc)
242 printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100243
244 spin_lock_irqsave(q->queue_lock, flags);
245 blk_start_queue(q);
246 spin_unlock_irqrestore(q->queue_lock, flags);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100247 }
248}