blob: ec951be4b0c8ab0950ca31dbeb070520c925230e [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));
Christoph Hellwigaebf5262017-01-31 16:57:31 +010021 rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +010022 scsi_req_init(rq);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +010023 ide_req(rq)->type = ATA_PRIV_PM_SUSPEND;
Tejun Heofc38b522009-04-19 07:00:43 +090024 rq->special = &rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010025 rqpm.pm_step = IDE_PM_START_SUSPEND;
26 if (mesg.event == PM_EVENT_PRETHAW)
27 mesg.event = PM_EVENT_FREEZE;
28 rqpm.pm_state = mesg.event;
29
30 ret = blk_execute_rq(drive->queue, NULL, rq, 0);
31 blk_put_request(rq);
32
Bartlomiej Zolnierkiewicz2bf427b2009-06-29 19:20:42 -070033 if (ret == 0 && ide_port_acpi(hwif)) {
34 /* call ACPI _PS3 only after both devices are suspended */
35 if ((drive->dn & 1) || pair == NULL)
36 ide_acpi_set_state(hwif, 0);
37 }
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010038
39 return ret;
40}
41
Christoph Hellwiga7928c12015-04-17 22:37:20 +020042static void ide_end_sync_rq(struct request *rq, int error)
43{
44 complete(rq->end_io_data);
45}
46
47static int ide_pm_execute_rq(struct request *rq)
48{
49 struct request_queue *q = rq->q;
50 DECLARE_COMPLETION_ONSTACK(wait);
51
52 rq->end_io_data = &wait;
53 rq->end_io = ide_end_sync_rq;
54
55 spin_lock_irq(q->queue_lock);
56 if (unlikely(blk_queue_dying(q))) {
Christoph Hellwige8064022016-10-20 15:12:13 +020057 rq->rq_flags |= RQF_QUIET;
Christoph Hellwiga7928c12015-04-17 22:37:20 +020058 rq->errors = -ENXIO;
59 __blk_end_request_all(rq, rq->errors);
60 spin_unlock_irq(q->queue_lock);
61 return -ENXIO;
62 }
63 __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT);
64 __blk_run_queue_uncond(q);
65 spin_unlock_irq(q->queue_lock);
66
67 wait_for_completion_io(&wait);
68
69 return rq->errors ? -EIO : 0;
70}
71
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010072int generic_ide_resume(struct device *dev)
73{
Miklos Szeredi9974e432012-08-21 17:20:30 +020074 ide_drive_t *drive = to_ide_device(dev);
Greg Kroah-Hartmanfcb52072009-04-30 14:43:31 -070075 ide_drive_t *pair = ide_get_pair_dev(drive);
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +010076 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010077 struct request *rq;
Christoph Hellwiga7928c12015-04-17 22:37:20 +020078 struct ide_pm_state rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010079 int err;
80
Bartlomiej Zolnierkiewicz2bf427b2009-06-29 19:20:42 -070081 if (ide_port_acpi(hwif)) {
82 /* call ACPI _PS0 / _STM only once */
83 if ((drive->dn & 1) == 0 || pair == NULL) {
84 ide_acpi_set_state(hwif, 1);
85 ide_acpi_push_timing(hwif);
86 }
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010087
Bartlomiej Zolnierkiewicz2bf427b2009-06-29 19:20:42 -070088 ide_acpi_exec_tfs(drive);
89 }
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010090
91 memset(&rqpm, 0, sizeof(rqpm));
Christoph Hellwigaebf5262017-01-31 16:57:31 +010092 rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +010093 scsi_req_init(rq);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +010094 ide_req(rq)->type = ATA_PRIV_PM_RESUME;
Christoph Hellwige8064022016-10-20 15:12:13 +020095 rq->rq_flags |= RQF_PREEMPT;
Tejun Heofc38b522009-04-19 07:00:43 +090096 rq->special = &rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010097 rqpm.pm_step = IDE_PM_START_RESUME;
98 rqpm.pm_state = PM_EVENT_ON;
99
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200100 err = ide_pm_execute_rq(rq);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100101 blk_put_request(rq);
102
103 if (err == 0 && dev->driver) {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100104 struct ide_driver *drv = to_ide_driver(dev->driver);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100105
106 if (drv->resume)
107 drv->resume(drive);
108 }
109
110 return err;
111}
112
113void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
114{
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200115 struct ide_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100116
117#ifdef DEBUG_PM
118 printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
119 drive->name, pm->pm_step);
120#endif
121 if (drive->media != ide_disk)
122 return;
123
124 switch (pm->pm_step) {
125 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
126 if (pm->pm_state == PM_EVENT_FREEZE)
127 pm->pm_step = IDE_PM_COMPLETED;
128 else
129 pm->pm_step = IDE_PM_STANDBY;
130 break;
131 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
132 pm->pm_step = IDE_PM_COMPLETED;
133 break;
134 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
135 pm->pm_step = IDE_PM_IDLE;
136 break;
137 case IDE_PM_IDLE: /* Resume step 2 (idle)*/
138 pm->pm_step = IDE_PM_RESTORE_DMA;
139 break;
140 }
141}
142
143ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
144{
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200145 struct ide_pm_state *pm = rq->special;
Tejun Heofc38b522009-04-19 07:00:43 +0900146 struct ide_cmd cmd = { };
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100147
148 switch (pm->pm_step) {
149 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
150 if (drive->media != ide_disk)
151 break;
152 /* Not supported? Switch to next step now. */
153 if (ata_id_flush_enabled(drive->id) == 0 ||
154 (drive->dev_flags & IDE_DFLAG_WCACHE) == 0) {
155 ide_complete_power_step(drive, rq);
156 return ide_stopped;
157 }
158 if (ata_id_flush_ext_enabled(drive->id))
Tejun Heofc38b522009-04-19 07:00:43 +0900159 cmd.tf.command = ATA_CMD_FLUSH_EXT;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100160 else
Tejun Heofc38b522009-04-19 07:00:43 +0900161 cmd.tf.command = ATA_CMD_FLUSH;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100162 goto out_do_tf;
163 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
Tejun Heofc38b522009-04-19 07:00:43 +0900164 cmd.tf.command = ATA_CMD_STANDBYNOW1;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100165 goto out_do_tf;
166 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
167 ide_set_max_pio(drive);
168 /*
169 * skip IDE_PM_IDLE for ATAPI devices
170 */
171 if (drive->media != ide_disk)
172 pm->pm_step = IDE_PM_RESTORE_DMA;
173 else
174 ide_complete_power_step(drive, rq);
175 return ide_stopped;
176 case IDE_PM_IDLE: /* Resume step 2 (idle) */
Tejun Heofc38b522009-04-19 07:00:43 +0900177 cmd.tf.command = ATA_CMD_IDLEIMMEDIATE;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100178 goto out_do_tf;
179 case IDE_PM_RESTORE_DMA: /* Resume step 3 (restore DMA) */
180 /*
181 * Right now, all we do is call ide_set_dma(drive),
182 * we could be smarter and check for current xfer_speed
183 * in struct drive etc...
184 */
185 if (drive->hwif->dma_ops == NULL)
186 break;
187 /*
188 * TODO: respect IDE_DFLAG_USING_DMA
189 */
190 ide_set_dma(drive);
191 break;
192 }
193
194 pm->pm_step = IDE_PM_COMPLETED;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100195
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100196 return ide_stopped;
197
198out_do_tf:
Tejun Heofc38b522009-04-19 07:00:43 +0900199 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
200 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
201 cmd.protocol = ATA_PROT_NODATA;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100202
Tejun Heofc38b522009-04-19 07:00:43 +0900203 return do_rw_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100204}
205
206/**
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100207 * ide_complete_pm_rq - end the current Power Management request
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100208 * @drive: target drive
209 * @rq: request
210 *
211 * This function cleans up the current PM request and stops the queue
212 * if necessary.
213 */
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100214void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq)
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100215{
216 struct request_queue *q = drive->queue;
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200217 struct ide_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100218 unsigned long flags;
219
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100220 ide_complete_power_step(drive, rq);
221 if (pm->pm_step != IDE_PM_COMPLETED)
222 return;
223
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100224#ifdef DEBUG_PM
225 printk("%s: completing PM request, %s\n", drive->name,
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100226 (ide_req(rq)->type == ATA_PRIV_PM_SUSPEND) ? "suspend" : "resume");
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100227#endif
228 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100229 if (ide_req(rq)->type == ATA_PRIV_PM_SUSPEND)
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100230 blk_stop_queue(q);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100231 else
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100232 drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100233 spin_unlock_irqrestore(q->queue_lock, flags);
234
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100235 drive->hwif->rq = NULL;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100236
237 if (blk_end_request(rq, 0, 0))
238 BUG();
239}
240
241void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
242{
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200243 struct ide_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100244
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100245 if (blk_rq_is_private(rq) &&
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100246 ide_req(rq)->type == ATA_PRIV_PM_SUSPEND &&
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100247 pm->pm_step == IDE_PM_START_SUSPEND)
248 /* Mark drive blocked when starting the suspend sequence. */
249 drive->dev_flags |= IDE_DFLAG_BLOCKED;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100250 else if (blk_rq_is_private(rq) &&
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100251 ide_req(rq)->type == ATA_PRIV_PM_RESUME &&
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100252 pm->pm_step == IDE_PM_START_RESUME) {
253 /*
254 * The first thing we do on wakeup is to wait for BSY bit to
255 * go away (with a looong timeout) as a drive on this hwif may
256 * just be POSTing itself.
257 * We do that before even selecting as the "other" device on
258 * the bus may be broken enough to walk on our toes at this
259 * point.
260 */
261 ide_hwif_t *hwif = drive->hwif;
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200262 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100263 struct request_queue *q = drive->queue;
264 unsigned long flags;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100265 int rc;
266#ifdef DEBUG_PM
267 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
268#endif
269 rc = ide_wait_not_busy(hwif, 35000);
270 if (rc)
271 printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200272 tp_ops->dev_select(drive);
273 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100274 rc = ide_wait_not_busy(hwif, 100000);
275 if (rc)
276 printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100277
278 spin_lock_irqsave(q->queue_lock, flags);
279 blk_start_queue(q);
280 spin_unlock_irqrestore(q->queue_lock, flags);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100281 }
282}