blob: f56d742908df65a45816a9bb9dfde0f380742ec9 [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 Hellwig2f5a8e802017-01-31 16:57:30 +010022 ide_req(rq)->type = ATA_PRIV_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
Christoph Hellwigb7819b92017-04-20 16:02:55 +020029 blk_execute_rq(drive->queue, NULL, rq, 0);
Christoph Hellwig17d53632017-04-20 16:03:01 +020030 ret = scsi_req(rq)->result ? -EIO : 0;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010031 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 Hellwig2a842ac2017-06-03 09:38:04 +020042static void ide_end_sync_rq(struct request *rq, blk_status_t error)
Christoph Hellwiga7928c12015-04-17 22:37:20 +020043{
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 Hellwig17d53632017-04-20 16:03:01 +020058 scsi_req(rq)->result = -ENXIO;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020059 __blk_end_request_all(rq, BLK_STS_OK);
Christoph Hellwiga7928c12015-04-17 22:37:20 +020060 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
Christoph Hellwig17d53632017-04-20 16:03:01 +020069 return scsi_req(rq)->result ? -EIO : 0;
Christoph Hellwiga7928c12015-04-17 22:37:20 +020070}
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));
Bart Van Assche039c6352017-11-09 10:49:56 -080092 rq = blk_get_request_flags(drive->queue, REQ_OP_DRV_IN,
93 BLK_MQ_REQ_PREEMPT);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +010094 ide_req(rq)->type = ATA_PRIV_PM_RESUME;
Tejun Heofc38b522009-04-19 07:00:43 +090095 rq->special = &rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010096 rqpm.pm_step = IDE_PM_START_RESUME;
97 rqpm.pm_state = PM_EVENT_ON;
98
Christoph Hellwiga7928c12015-04-17 22:37:20 +020099 err = ide_pm_execute_rq(rq);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100100 blk_put_request(rq);
101
102 if (err == 0 && dev->driver) {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100103 struct ide_driver *drv = to_ide_driver(dev->driver);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100104
105 if (drv->resume)
106 drv->resume(drive);
107 }
108
109 return err;
110}
111
112void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
113{
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200114 struct ide_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100115
116#ifdef DEBUG_PM
117 printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
118 drive->name, pm->pm_step);
119#endif
120 if (drive->media != ide_disk)
121 return;
122
123 switch (pm->pm_step) {
124 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
125 if (pm->pm_state == PM_EVENT_FREEZE)
126 pm->pm_step = IDE_PM_COMPLETED;
127 else
128 pm->pm_step = IDE_PM_STANDBY;
129 break;
130 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
131 pm->pm_step = IDE_PM_COMPLETED;
132 break;
133 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
134 pm->pm_step = IDE_PM_IDLE;
135 break;
136 case IDE_PM_IDLE: /* Resume step 2 (idle)*/
137 pm->pm_step = IDE_PM_RESTORE_DMA;
138 break;
139 }
140}
141
142ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
143{
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200144 struct ide_pm_state *pm = rq->special;
Tejun Heofc38b522009-04-19 07:00:43 +0900145 struct ide_cmd cmd = { };
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100146
147 switch (pm->pm_step) {
148 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
149 if (drive->media != ide_disk)
150 break;
151 /* Not supported? Switch to next step now. */
152 if (ata_id_flush_enabled(drive->id) == 0 ||
153 (drive->dev_flags & IDE_DFLAG_WCACHE) == 0) {
154 ide_complete_power_step(drive, rq);
155 return ide_stopped;
156 }
157 if (ata_id_flush_ext_enabled(drive->id))
Tejun Heofc38b522009-04-19 07:00:43 +0900158 cmd.tf.command = ATA_CMD_FLUSH_EXT;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100159 else
Tejun Heofc38b522009-04-19 07:00:43 +0900160 cmd.tf.command = ATA_CMD_FLUSH;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100161 goto out_do_tf;
162 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
Tejun Heofc38b522009-04-19 07:00:43 +0900163 cmd.tf.command = ATA_CMD_STANDBYNOW1;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100164 goto out_do_tf;
165 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
166 ide_set_max_pio(drive);
167 /*
168 * skip IDE_PM_IDLE for ATAPI devices
169 */
170 if (drive->media != ide_disk)
171 pm->pm_step = IDE_PM_RESTORE_DMA;
172 else
173 ide_complete_power_step(drive, rq);
174 return ide_stopped;
175 case IDE_PM_IDLE: /* Resume step 2 (idle) */
Tejun Heofc38b522009-04-19 07:00:43 +0900176 cmd.tf.command = ATA_CMD_IDLEIMMEDIATE;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100177 goto out_do_tf;
178 case IDE_PM_RESTORE_DMA: /* Resume step 3 (restore DMA) */
179 /*
180 * Right now, all we do is call ide_set_dma(drive),
181 * we could be smarter and check for current xfer_speed
182 * in struct drive etc...
183 */
184 if (drive->hwif->dma_ops == NULL)
185 break;
186 /*
187 * TODO: respect IDE_DFLAG_USING_DMA
188 */
189 ide_set_dma(drive);
190 break;
191 }
192
193 pm->pm_step = IDE_PM_COMPLETED;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100194
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100195 return ide_stopped;
196
197out_do_tf:
Tejun Heofc38b522009-04-19 07:00:43 +0900198 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
199 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
200 cmd.protocol = ATA_PROT_NODATA;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100201
Tejun Heofc38b522009-04-19 07:00:43 +0900202 return do_rw_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100203}
204
205/**
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100206 * ide_complete_pm_rq - end the current Power Management request
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100207 * @drive: target drive
208 * @rq: request
209 *
210 * This function cleans up the current PM request and stops the queue
211 * if necessary.
212 */
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100213void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq)
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100214{
215 struct request_queue *q = drive->queue;
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200216 struct ide_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100217 unsigned long flags;
218
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100219 ide_complete_power_step(drive, rq);
220 if (pm->pm_step != IDE_PM_COMPLETED)
221 return;
222
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100223#ifdef DEBUG_PM
224 printk("%s: completing PM request, %s\n", drive->name,
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100225 (ide_req(rq)->type == ATA_PRIV_PM_SUSPEND) ? "suspend" : "resume");
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100226#endif
227 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100228 if (ide_req(rq)->type == ATA_PRIV_PM_SUSPEND)
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100229 blk_stop_queue(q);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100230 else
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100231 drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100232 spin_unlock_irqrestore(q->queue_lock, flags);
233
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100234 drive->hwif->rq = NULL;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100235
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200236 if (blk_end_request(rq, BLK_STS_OK, 0))
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100237 BUG();
238}
239
240void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
241{
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200242 struct ide_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100243
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100244 if (blk_rq_is_private(rq) &&
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100245 ide_req(rq)->type == ATA_PRIV_PM_SUSPEND &&
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100246 pm->pm_step == IDE_PM_START_SUSPEND)
247 /* Mark drive blocked when starting the suspend sequence. */
248 drive->dev_flags |= IDE_DFLAG_BLOCKED;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100249 else if (blk_rq_is_private(rq) &&
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100250 ide_req(rq)->type == ATA_PRIV_PM_RESUME &&
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100251 pm->pm_step == IDE_PM_START_RESUME) {
252 /*
253 * The first thing we do on wakeup is to wait for BSY bit to
254 * go away (with a looong timeout) as a drive on this hwif may
255 * just be POSTing itself.
256 * We do that before even selecting as the "other" device on
257 * the bus may be broken enough to walk on our toes at this
258 * point.
259 */
260 ide_hwif_t *hwif = drive->hwif;
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200261 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100262 struct request_queue *q = drive->queue;
263 unsigned long flags;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100264 int rc;
265#ifdef DEBUG_PM
266 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
267#endif
268 rc = ide_wait_not_busy(hwif, 35000);
269 if (rc)
270 printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200271 tp_ops->dev_select(drive);
272 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100273 rc = ide_wait_not_busy(hwif, 100000);
274 if (rc)
275 printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100276
277 spin_lock_irqsave(q->queue_lock, flags);
278 blk_start_queue(q);
279 spin_unlock_irqrestore(q->queue_lock, flags);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100280 }
281}