blob: 0c1296815dfad25be614f377933fc4f580b7325e [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{
Miklos Szeredi9974e432012-08-21 17:20:30 +02007 ide_drive_t *drive = to_ide_device(dev);
Greg Kroah-Hartmanfcb52072009-04-30 14:43:31 -07008 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;
Christoph Hellwiga7928c12015-04-17 22:37:20 +020011 struct ide_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));
Mel Gorman71baba42015-11-06 16:28:28 -080021 rq = blk_get_request(drive->queue, READ, __GFP_RECLAIM);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +010022 scsi_req_init(rq);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +010023 rq->cmd_type = REQ_TYPE_DRV_PRIV;
24 ide_req(rq)->type = ATA_PRIV_PM_SUSPEND;
Tejun Heofc38b522009-04-19 07:00:43 +090025 rq->special = &rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010026 rqpm.pm_step = IDE_PM_START_SUSPEND;
27 if (mesg.event == PM_EVENT_PRETHAW)
28 mesg.event = PM_EVENT_FREEZE;
29 rqpm.pm_state = mesg.event;
30
31 ret = blk_execute_rq(drive->queue, NULL, rq, 0);
32 blk_put_request(rq);
33
Bartlomiej Zolnierkiewicz2bf427b2009-06-29 19:20:42 -070034 if (ret == 0 && ide_port_acpi(hwif)) {
35 /* call ACPI _PS3 only after both devices are suspended */
36 if ((drive->dn & 1) || pair == NULL)
37 ide_acpi_set_state(hwif, 0);
38 }
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010039
40 return ret;
41}
42
Christoph Hellwiga7928c12015-04-17 22:37:20 +020043static void ide_end_sync_rq(struct request *rq, int error)
44{
45 complete(rq->end_io_data);
46}
47
48static int ide_pm_execute_rq(struct request *rq)
49{
50 struct request_queue *q = rq->q;
51 DECLARE_COMPLETION_ONSTACK(wait);
52
53 rq->end_io_data = &wait;
54 rq->end_io = ide_end_sync_rq;
55
56 spin_lock_irq(q->queue_lock);
57 if (unlikely(blk_queue_dying(q))) {
Christoph Hellwige8064022016-10-20 15:12:13 +020058 rq->rq_flags |= RQF_QUIET;
Christoph Hellwiga7928c12015-04-17 22:37:20 +020059 rq->errors = -ENXIO;
60 __blk_end_request_all(rq, rq->errors);
61 spin_unlock_irq(q->queue_lock);
62 return -ENXIO;
63 }
64 __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT);
65 __blk_run_queue_uncond(q);
66 spin_unlock_irq(q->queue_lock);
67
68 wait_for_completion_io(&wait);
69
70 return rq->errors ? -EIO : 0;
71}
72
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010073int generic_ide_resume(struct device *dev)
74{
Miklos Szeredi9974e432012-08-21 17:20:30 +020075 ide_drive_t *drive = to_ide_device(dev);
Greg Kroah-Hartmanfcb52072009-04-30 14:43:31 -070076 ide_drive_t *pair = ide_get_pair_dev(drive);
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +010077 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010078 struct request *rq;
Christoph Hellwiga7928c12015-04-17 22:37:20 +020079 struct ide_pm_state rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010080 int err;
81
Bartlomiej Zolnierkiewicz2bf427b2009-06-29 19:20:42 -070082 if (ide_port_acpi(hwif)) {
83 /* call ACPI _PS0 / _STM only once */
84 if ((drive->dn & 1) == 0 || pair == NULL) {
85 ide_acpi_set_state(hwif, 1);
86 ide_acpi_push_timing(hwif);
87 }
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010088
Bartlomiej Zolnierkiewicz2bf427b2009-06-29 19:20:42 -070089 ide_acpi_exec_tfs(drive);
90 }
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010091
92 memset(&rqpm, 0, sizeof(rqpm));
Mel Gorman71baba42015-11-06 16:28:28 -080093 rq = blk_get_request(drive->queue, READ, __GFP_RECLAIM);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +010094 scsi_req_init(rq);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +010095 rq->cmd_type = REQ_TYPE_DRV_PRIV;
96 ide_req(rq)->type = ATA_PRIV_PM_RESUME;
Christoph Hellwige8064022016-10-20 15:12:13 +020097 rq->rq_flags |= RQF_PREEMPT;
Tejun Heofc38b522009-04-19 07:00:43 +090098 rq->special = &rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010099 rqpm.pm_step = IDE_PM_START_RESUME;
100 rqpm.pm_state = PM_EVENT_ON;
101
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200102 err = ide_pm_execute_rq(rq);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100103 blk_put_request(rq);
104
105 if (err == 0 && dev->driver) {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100106 struct ide_driver *drv = to_ide_driver(dev->driver);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100107
108 if (drv->resume)
109 drv->resume(drive);
110 }
111
112 return err;
113}
114
115void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
116{
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200117 struct ide_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100118
119#ifdef DEBUG_PM
120 printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
121 drive->name, pm->pm_step);
122#endif
123 if (drive->media != ide_disk)
124 return;
125
126 switch (pm->pm_step) {
127 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
128 if (pm->pm_state == PM_EVENT_FREEZE)
129 pm->pm_step = IDE_PM_COMPLETED;
130 else
131 pm->pm_step = IDE_PM_STANDBY;
132 break;
133 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
134 pm->pm_step = IDE_PM_COMPLETED;
135 break;
136 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
137 pm->pm_step = IDE_PM_IDLE;
138 break;
139 case IDE_PM_IDLE: /* Resume step 2 (idle)*/
140 pm->pm_step = IDE_PM_RESTORE_DMA;
141 break;
142 }
143}
144
145ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
146{
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200147 struct ide_pm_state *pm = rq->special;
Tejun Heofc38b522009-04-19 07:00:43 +0900148 struct ide_cmd cmd = { };
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100149
150 switch (pm->pm_step) {
151 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
152 if (drive->media != ide_disk)
153 break;
154 /* Not supported? Switch to next step now. */
155 if (ata_id_flush_enabled(drive->id) == 0 ||
156 (drive->dev_flags & IDE_DFLAG_WCACHE) == 0) {
157 ide_complete_power_step(drive, rq);
158 return ide_stopped;
159 }
160 if (ata_id_flush_ext_enabled(drive->id))
Tejun Heofc38b522009-04-19 07:00:43 +0900161 cmd.tf.command = ATA_CMD_FLUSH_EXT;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100162 else
Tejun Heofc38b522009-04-19 07:00:43 +0900163 cmd.tf.command = ATA_CMD_FLUSH;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100164 goto out_do_tf;
165 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
Tejun Heofc38b522009-04-19 07:00:43 +0900166 cmd.tf.command = ATA_CMD_STANDBYNOW1;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100167 goto out_do_tf;
168 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
169 ide_set_max_pio(drive);
170 /*
171 * skip IDE_PM_IDLE for ATAPI devices
172 */
173 if (drive->media != ide_disk)
174 pm->pm_step = IDE_PM_RESTORE_DMA;
175 else
176 ide_complete_power_step(drive, rq);
177 return ide_stopped;
178 case IDE_PM_IDLE: /* Resume step 2 (idle) */
Tejun Heofc38b522009-04-19 07:00:43 +0900179 cmd.tf.command = ATA_CMD_IDLEIMMEDIATE;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100180 goto out_do_tf;
181 case IDE_PM_RESTORE_DMA: /* Resume step 3 (restore DMA) */
182 /*
183 * Right now, all we do is call ide_set_dma(drive),
184 * we could be smarter and check for current xfer_speed
185 * in struct drive etc...
186 */
187 if (drive->hwif->dma_ops == NULL)
188 break;
189 /*
190 * TODO: respect IDE_DFLAG_USING_DMA
191 */
192 ide_set_dma(drive);
193 break;
194 }
195
196 pm->pm_step = IDE_PM_COMPLETED;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100197
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100198 return ide_stopped;
199
200out_do_tf:
Tejun Heofc38b522009-04-19 07:00:43 +0900201 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
202 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
203 cmd.protocol = ATA_PROT_NODATA;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100204
Tejun Heofc38b522009-04-19 07:00:43 +0900205 return do_rw_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100206}
207
208/**
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100209 * ide_complete_pm_rq - end the current Power Management request
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100210 * @drive: target drive
211 * @rq: request
212 *
213 * This function cleans up the current PM request and stops the queue
214 * if necessary.
215 */
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100216void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq)
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100217{
218 struct request_queue *q = drive->queue;
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200219 struct ide_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100220 unsigned long flags;
221
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100222 ide_complete_power_step(drive, rq);
223 if (pm->pm_step != IDE_PM_COMPLETED)
224 return;
225
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100226#ifdef DEBUG_PM
227 printk("%s: completing PM request, %s\n", drive->name,
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100228 (ide_req(rq)->type == ATA_PRIV_PM_SUSPEND) ? "suspend" : "resume");
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100229#endif
230 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100231 if (ide_req(rq)->type == ATA_PRIV_PM_SUSPEND)
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100232 blk_stop_queue(q);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100233 else
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100234 drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100235 spin_unlock_irqrestore(q->queue_lock, flags);
236
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100237 drive->hwif->rq = NULL;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100238
239 if (blk_end_request(rq, 0, 0))
240 BUG();
241}
242
243void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
244{
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200245 struct ide_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100246
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100247 if (rq->cmd_type == REQ_TYPE_DRV_PRIV &&
248 ide_req(rq)->type == ATA_PRIV_PM_SUSPEND &&
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100249 pm->pm_step == IDE_PM_START_SUSPEND)
250 /* Mark drive blocked when starting the suspend sequence. */
251 drive->dev_flags |= IDE_DFLAG_BLOCKED;
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100252 else if (rq->cmd_type == REQ_TYPE_DRV_PRIV &&
253 ide_req(rq)->type == ATA_PRIV_PM_RESUME &&
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100254 pm->pm_step == IDE_PM_START_RESUME) {
255 /*
256 * The first thing we do on wakeup is to wait for BSY bit to
257 * go away (with a looong timeout) as a drive on this hwif may
258 * just be POSTing itself.
259 * We do that before even selecting as the "other" device on
260 * the bus may be broken enough to walk on our toes at this
261 * point.
262 */
263 ide_hwif_t *hwif = drive->hwif;
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200264 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100265 struct request_queue *q = drive->queue;
266 unsigned long flags;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100267 int rc;
268#ifdef DEBUG_PM
269 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
270#endif
271 rc = ide_wait_not_busy(hwif, 35000);
272 if (rc)
273 printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200274 tp_ops->dev_select(drive);
275 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100276 rc = ide_wait_not_busy(hwif, 100000);
277 if (rc)
278 printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100279
280 spin_lock_irqsave(q->queue_lock, flags);
281 blk_start_queue(q);
282 spin_unlock_irqrestore(q->queue_lock, flags);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100283 }
284}