blob: ea10507e5190abf293b1042bcfcc3e5c299666d5 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +01002#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09003#include <linux/gfp.h>
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +01004#include <linux/ide.h>
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +01005
6int generic_ide_suspend(struct device *dev, pm_message_t mesg)
7{
Miklos Szeredi9974e432012-08-21 17:20:30 +02008 ide_drive_t *drive = to_ide_device(dev);
Greg Kroah-Hartmanfcb52072009-04-30 14:43:31 -07009 ide_drive_t *pair = ide_get_pair_dev(drive);
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +010010 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010011 struct request *rq;
Christoph Hellwiga7928c12015-04-17 22:37:20 +020012 struct ide_pm_state rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010013 int ret;
14
Bartlomiej Zolnierkiewicz2bf427b2009-06-29 19:20:42 -070015 if (ide_port_acpi(hwif)) {
16 /* call ACPI _GTM only once */
17 if ((drive->dn & 1) == 0 || pair == NULL)
18 ide_acpi_get_timing(hwif);
19 }
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010020
21 memset(&rqpm, 0, sizeof(rqpm));
Christoph Hellwigff005a02018-05-09 09:54:05 +020022 rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0);
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);
Christoph Hellwig17d53632017-04-20 16:03:01 +020031 ret = scsi_req(rq)->result ? -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 int ide_pm_execute_rq(struct request *rq)
44{
45 struct request_queue *q = rq->q;
Christoph Hellwiga7928c12015-04-17 22:37:20 +020046
47 spin_lock_irq(q->queue_lock);
48 if (unlikely(blk_queue_dying(q))) {
Christoph Hellwige8064022016-10-20 15:12:13 +020049 rq->rq_flags |= RQF_QUIET;
Christoph Hellwig17d53632017-04-20 16:03:01 +020050 scsi_req(rq)->result = -ENXIO;
Christoph Hellwiga7928c12015-04-17 22:37:20 +020051 spin_unlock_irq(q->queue_lock);
Jens Axboe60033522018-10-26 09:53:52 -060052 blk_mq_end_request(rq, BLK_STS_OK);
Christoph Hellwiga7928c12015-04-17 22:37:20 +020053 return -ENXIO;
54 }
Christoph Hellwiga7928c12015-04-17 22:37:20 +020055 spin_unlock_irq(q->queue_lock);
Jens Axboe60033522018-10-26 09:53:52 -060056 blk_execute_rq(q, NULL, rq, true);
Christoph Hellwiga7928c12015-04-17 22:37:20 +020057
Christoph Hellwig17d53632017-04-20 16:03:01 +020058 return scsi_req(rq)->result ? -EIO : 0;
Christoph Hellwiga7928c12015-04-17 22:37:20 +020059}
60
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010061int generic_ide_resume(struct device *dev)
62{
Miklos Szeredi9974e432012-08-21 17:20:30 +020063 ide_drive_t *drive = to_ide_device(dev);
Greg Kroah-Hartmanfcb52072009-04-30 14:43:31 -070064 ide_drive_t *pair = ide_get_pair_dev(drive);
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +010065 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010066 struct request *rq;
Christoph Hellwiga7928c12015-04-17 22:37:20 +020067 struct ide_pm_state rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010068 int err;
69
Jens Axboe60033522018-10-26 09:53:52 -060070 blk_mq_start_stopped_hw_queues(drive->queue, true);
71
Bartlomiej Zolnierkiewicz2bf427b2009-06-29 19:20:42 -070072 if (ide_port_acpi(hwif)) {
73 /* call ACPI _PS0 / _STM only once */
74 if ((drive->dn & 1) == 0 || pair == NULL) {
75 ide_acpi_set_state(hwif, 1);
76 ide_acpi_push_timing(hwif);
77 }
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010078
Bartlomiej Zolnierkiewicz2bf427b2009-06-29 19:20:42 -070079 ide_acpi_exec_tfs(drive);
80 }
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010081
82 memset(&rqpm, 0, sizeof(rqpm));
Christoph Hellwigff005a02018-05-09 09:54:05 +020083 rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, BLK_MQ_REQ_PREEMPT);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +010084 ide_req(rq)->type = ATA_PRIV_PM_RESUME;
Tejun Heofc38b522009-04-19 07:00:43 +090085 rq->special = &rqpm;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010086 rqpm.pm_step = IDE_PM_START_RESUME;
87 rqpm.pm_state = PM_EVENT_ON;
88
Christoph Hellwiga7928c12015-04-17 22:37:20 +020089 err = ide_pm_execute_rq(rq);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010090 blk_put_request(rq);
91
92 if (err == 0 && dev->driver) {
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +010093 struct ide_driver *drv = to_ide_driver(dev->driver);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +010094
95 if (drv->resume)
96 drv->resume(drive);
97 }
98
99 return err;
100}
101
102void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
103{
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200104 struct ide_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100105
106#ifdef DEBUG_PM
107 printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
108 drive->name, pm->pm_step);
109#endif
110 if (drive->media != ide_disk)
111 return;
112
113 switch (pm->pm_step) {
114 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
115 if (pm->pm_state == PM_EVENT_FREEZE)
116 pm->pm_step = IDE_PM_COMPLETED;
117 else
118 pm->pm_step = IDE_PM_STANDBY;
119 break;
120 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
121 pm->pm_step = IDE_PM_COMPLETED;
122 break;
123 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
124 pm->pm_step = IDE_PM_IDLE;
125 break;
126 case IDE_PM_IDLE: /* Resume step 2 (idle)*/
127 pm->pm_step = IDE_PM_RESTORE_DMA;
128 break;
129 }
130}
131
132ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
133{
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200134 struct ide_pm_state *pm = rq->special;
Tejun Heofc38b522009-04-19 07:00:43 +0900135 struct ide_cmd cmd = { };
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100136
137 switch (pm->pm_step) {
138 case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
139 if (drive->media != ide_disk)
140 break;
141 /* Not supported? Switch to next step now. */
142 if (ata_id_flush_enabled(drive->id) == 0 ||
143 (drive->dev_flags & IDE_DFLAG_WCACHE) == 0) {
144 ide_complete_power_step(drive, rq);
145 return ide_stopped;
146 }
147 if (ata_id_flush_ext_enabled(drive->id))
Tejun Heofc38b522009-04-19 07:00:43 +0900148 cmd.tf.command = ATA_CMD_FLUSH_EXT;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100149 else
Tejun Heofc38b522009-04-19 07:00:43 +0900150 cmd.tf.command = ATA_CMD_FLUSH;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100151 goto out_do_tf;
152 case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
Tejun Heofc38b522009-04-19 07:00:43 +0900153 cmd.tf.command = ATA_CMD_STANDBYNOW1;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100154 goto out_do_tf;
155 case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
156 ide_set_max_pio(drive);
157 /*
158 * skip IDE_PM_IDLE for ATAPI devices
159 */
160 if (drive->media != ide_disk)
161 pm->pm_step = IDE_PM_RESTORE_DMA;
162 else
163 ide_complete_power_step(drive, rq);
164 return ide_stopped;
165 case IDE_PM_IDLE: /* Resume step 2 (idle) */
Tejun Heofc38b522009-04-19 07:00:43 +0900166 cmd.tf.command = ATA_CMD_IDLEIMMEDIATE;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100167 goto out_do_tf;
168 case IDE_PM_RESTORE_DMA: /* Resume step 3 (restore DMA) */
169 /*
170 * Right now, all we do is call ide_set_dma(drive),
171 * we could be smarter and check for current xfer_speed
172 * in struct drive etc...
173 */
174 if (drive->hwif->dma_ops == NULL)
175 break;
176 /*
177 * TODO: respect IDE_DFLAG_USING_DMA
178 */
179 ide_set_dma(drive);
180 break;
181 }
182
183 pm->pm_step = IDE_PM_COMPLETED;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100184
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100185 return ide_stopped;
186
187out_do_tf:
Tejun Heofc38b522009-04-19 07:00:43 +0900188 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
189 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
190 cmd.protocol = ATA_PROT_NODATA;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100191
Tejun Heofc38b522009-04-19 07:00:43 +0900192 return do_rw_taskfile(drive, &cmd);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100193}
194
195/**
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100196 * ide_complete_pm_rq - end the current Power Management request
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100197 * @drive: target drive
198 * @rq: request
199 *
200 * This function cleans up the current PM request and stops the queue
201 * if necessary.
202 */
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100203void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq)
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100204{
205 struct request_queue *q = drive->queue;
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200206 struct ide_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100207 unsigned long flags;
208
Bartlomiej Zolnierkiewicz3616b652009-03-27 12:46:29 +0100209 ide_complete_power_step(drive, rq);
210 if (pm->pm_step != IDE_PM_COMPLETED)
211 return;
212
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100213#ifdef DEBUG_PM
214 printk("%s: completing PM request, %s\n", drive->name,
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100215 (ide_req(rq)->type == ATA_PRIV_PM_SUSPEND) ? "suspend" : "resume");
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100216#endif
217 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100218 if (ide_req(rq)->type == ATA_PRIV_PM_SUSPEND)
Jens Axboe60033522018-10-26 09:53:52 -0600219 blk_mq_stop_hw_queues(q);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100220 else
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100221 drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100222 spin_unlock_irqrestore(q->queue_lock, flags);
223
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100224 drive->hwif->rq = NULL;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100225
Jens Axboe60033522018-10-26 09:53:52 -0600226 blk_mq_end_request(rq, BLK_STS_OK);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100227}
228
229void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
230{
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200231 struct ide_pm_state *pm = rq->special;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100232
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100233 if (blk_rq_is_private(rq) &&
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100234 ide_req(rq)->type == ATA_PRIV_PM_SUSPEND &&
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100235 pm->pm_step == IDE_PM_START_SUSPEND)
236 /* Mark drive blocked when starting the suspend sequence. */
237 drive->dev_flags |= IDE_DFLAG_BLOCKED;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100238 else if (blk_rq_is_private(rq) &&
Christoph Hellwig2f5a8e802017-01-31 16:57:30 +0100239 ide_req(rq)->type == ATA_PRIV_PM_RESUME &&
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100240 pm->pm_step == IDE_PM_START_RESUME) {
241 /*
242 * The first thing we do on wakeup is to wait for BSY bit to
243 * go away (with a looong timeout) as a drive on this hwif may
244 * just be POSTing itself.
245 * We do that before even selecting as the "other" device on
246 * the bus may be broken enough to walk on our toes at this
247 * point.
248 */
249 ide_hwif_t *hwif = drive->hwif;
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200250 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100251 struct request_queue *q = drive->queue;
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100252 int rc;
253#ifdef DEBUG_PM
254 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
255#endif
256 rc = ide_wait_not_busy(hwif, 35000);
257 if (rc)
258 printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200259 tp_ops->dev_select(drive);
260 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100261 rc = ide_wait_not_busy(hwif, 100000);
262 if (rc)
263 printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
Bartlomiej Zolnierkiewicz2ea55212009-01-14 19:19:04 +0100264
Jens Axboe60033522018-10-26 09:53:52 -0600265 blk_mq_start_hw_queues(q);
Bartlomiej Zolnierkiewicze2984c62008-12-29 20:27:37 +0100266 }
267}