blob: bf513f886f3c73bdee3235ff4658a91193a58d9a [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
Christoph Hellwigb7819b92017-04-20 16:02:55 +020030 blk_execute_rq(drive->queue, NULL, rq, 0);
31 ret = rq->errors ? -EIO : 0;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010032 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));
Christoph Hellwigaebf5262017-01-31 16:57:31 +010093 rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +010094 scsi_req_init(rq);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +010095 ide_req(rq)->type = ATA_PRIV_PM_RESUME;
Christoph Hellwige8064022016-10-20 15:12:13 +020096 rq->rq_flags |= RQF_PREEMPT;
Tejun Heofc38b522009-04-19 07:00:43 +090097 rq->special = &rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010098 rqpm.pm_step = IDE_PM_START_RESUME;
99 rqpm.pm_state = PM_EVENT_ON;
100
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200101 err = ide_pm_execute_rq(rq);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100102 blk_put_request(rq);
103
104 if (err == 0 && dev->driver) {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100105 struct ide_driver *drv = to_ide_driver(dev->driver);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100106
107 if (drv->resume)
108 drv->resume(drive);
109 }
110
111 return err;
112}
113
114void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
115{
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200116 struct ide_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100117
118#ifdef DEBUG_PM
119 printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
120 drive->name, pm->pm_step);
121#endif
122 if (drive->media != ide_disk)
123 return;
124
125 switch (pm->pm_step) {
126 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
127 if (pm->pm_state == PM_EVENT_FREEZE)
128 pm->pm_step = IDE_PM_COMPLETED;
129 else
130 pm->pm_step = IDE_PM_STANDBY;
131 break;
132 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
133 pm->pm_step = IDE_PM_COMPLETED;
134 break;
135 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
136 pm->pm_step = IDE_PM_IDLE;
137 break;
138 case IDE_PM_IDLE: /* Resume step 2 (idle)*/
139 pm->pm_step = IDE_PM_RESTORE_DMA;
140 break;
141 }
142}
143
144ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
145{
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200146 struct ide_pm_state *pm = rq->special;
Tejun Heofc38b522009-04-19 07:00:43 +0900147 struct ide_cmd cmd = { };
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100148
149 switch (pm->pm_step) {
150 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
151 if (drive->media != ide_disk)
152 break;
153 /* Not supported? Switch to next step now. */
154 if (ata_id_flush_enabled(drive->id) == 0 ||
155 (drive->dev_flags & IDE_DFLAG_WCACHE) == 0) {
156 ide_complete_power_step(drive, rq);
157 return ide_stopped;
158 }
159 if (ata_id_flush_ext_enabled(drive->id))
Tejun Heofc38b522009-04-19 07:00:43 +0900160 cmd.tf.command = ATA_CMD_FLUSH_EXT;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100161 else
Tejun Heofc38b522009-04-19 07:00:43 +0900162 cmd.tf.command = ATA_CMD_FLUSH;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100163 goto out_do_tf;
164 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
Tejun Heofc38b522009-04-19 07:00:43 +0900165 cmd.tf.command = ATA_CMD_STANDBYNOW1;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100166 goto out_do_tf;
167 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
168 ide_set_max_pio(drive);
169 /*
170 * skip IDE_PM_IDLE for ATAPI devices
171 */
172 if (drive->media != ide_disk)
173 pm->pm_step = IDE_PM_RESTORE_DMA;
174 else
175 ide_complete_power_step(drive, rq);
176 return ide_stopped;
177 case IDE_PM_IDLE: /* Resume step 2 (idle) */
Tejun Heofc38b522009-04-19 07:00:43 +0900178 cmd.tf.command = ATA_CMD_IDLEIMMEDIATE;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100179 goto out_do_tf;
180 case IDE_PM_RESTORE_DMA: /* Resume step 3 (restore DMA) */
181 /*
182 * Right now, all we do is call ide_set_dma(drive),
183 * we could be smarter and check for current xfer_speed
184 * in struct drive etc...
185 */
186 if (drive->hwif->dma_ops == NULL)
187 break;
188 /*
189 * TODO: respect IDE_DFLAG_USING_DMA
190 */
191 ide_set_dma(drive);
192 break;
193 }
194
195 pm->pm_step = IDE_PM_COMPLETED;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100196
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100197 return ide_stopped;
198
199out_do_tf:
Tejun Heofc38b522009-04-19 07:00:43 +0900200 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
201 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
202 cmd.protocol = ATA_PROT_NODATA;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100203
Tejun Heofc38b522009-04-19 07:00:43 +0900204 return do_rw_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100205}
206
207/**
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100208 * ide_complete_pm_rq - end the current Power Management request
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100209 * @drive: target drive
210 * @rq: request
211 *
212 * This function cleans up the current PM request and stops the queue
213 * if necessary.
214 */
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100215void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq)
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100216{
217 struct request_queue *q = drive->queue;
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200218 struct ide_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100219 unsigned long flags;
220
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100221 ide_complete_power_step(drive, rq);
222 if (pm->pm_step != IDE_PM_COMPLETED)
223 return;
224
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100225#ifdef DEBUG_PM
226 printk("%s: completing PM request, %s\n", drive->name,
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100227 (ide_req(rq)->type == ATA_PRIV_PM_SUSPEND) ? "suspend" : "resume");
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100228#endif
229 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100230 if (ide_req(rq)->type == ATA_PRIV_PM_SUSPEND)
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100231 blk_stop_queue(q);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100232 else
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100233 drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100234 spin_unlock_irqrestore(q->queue_lock, flags);
235
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100236 drive->hwif->rq = NULL;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100237
238 if (blk_end_request(rq, 0, 0))
239 BUG();
240}
241
242void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
243{
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200244 struct ide_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100245
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100246 if (blk_rq_is_private(rq) &&
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100247 ide_req(rq)->type == ATA_PRIV_PM_SUSPEND &&
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100248 pm->pm_step == IDE_PM_START_SUSPEND)
249 /* Mark drive blocked when starting the suspend sequence. */
250 drive->dev_flags |= IDE_DFLAG_BLOCKED;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100251 else if (blk_rq_is_private(rq) &&
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100252 ide_req(rq)->type == ATA_PRIV_PM_RESUME &&
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100253 pm->pm_step == IDE_PM_START_RESUME) {
254 /*
255 * The first thing we do on wakeup is to wait for BSY bit to
256 * go away (with a looong timeout) as a drive on this hwif may
257 * just be POSTing itself.
258 * We do that before even selecting as the "other" device on
259 * the bus may be broken enough to walk on our toes at this
260 * point.
261 */
262 ide_hwif_t *hwif = drive->hwif;
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200263 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100264 struct request_queue *q = drive->queue;
265 unsigned long flags;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100266 int rc;
267#ifdef DEBUG_PM
268 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
269#endif
270 rc = ide_wait_not_busy(hwif, 35000);
271 if (rc)
272 printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200273 tp_ops->dev_select(drive);
274 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100275 rc = ide_wait_not_busy(hwif, 100000);
276 if (rc)
277 printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100278
279 spin_lock_irqsave(q->queue_lock, flags);
280 blk_start_queue(q);
281 spin_unlock_irqrestore(q->queue_lock, flags);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100282 }
283}