Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 1 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 2 | #include <linux/gfp.h> |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 3 | #include <linux/ide.h> |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 4 | |
| 5 | int generic_ide_suspend(struct device *dev, pm_message_t mesg) |
| 6 | { |
Miklos Szeredi | 9974e43 | 2012-08-21 17:20:30 +0200 | [diff] [blame] | 7 | ide_drive_t *drive = to_ide_device(dev); |
Greg Kroah-Hartman | fcb5207 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 8 | ide_drive_t *pair = ide_get_pair_dev(drive); |
Bartlomiej Zolnierkiewicz | 898ec22 | 2009-01-06 17:20:52 +0100 | [diff] [blame] | 9 | ide_hwif_t *hwif = drive->hwif; |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 10 | struct request *rq; |
Christoph Hellwig | a7928c1 | 2015-04-17 22:37:20 +0200 | [diff] [blame] | 11 | struct ide_pm_state rqpm; |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 12 | int ret; |
| 13 | |
Bartlomiej Zolnierkiewicz | 2bf427b | 2009-06-29 19:20:42 -0700 | [diff] [blame] | 14 | 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 Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 19 | |
| 20 | memset(&rqpm, 0, sizeof(rqpm)); |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 21 | rq = blk_get_request(drive->queue, READ, __GFP_RECLAIM); |
Christoph Hellwig | a7928c1 | 2015-04-17 22:37:20 +0200 | [diff] [blame] | 22 | rq->cmd_type = REQ_TYPE_ATA_PM_SUSPEND; |
Tejun Heo | fc38b52 | 2009-04-19 07:00:43 +0900 | [diff] [blame] | 23 | rq->special = &rqpm; |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 24 | 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 | |
| 29 | ret = blk_execute_rq(drive->queue, NULL, rq, 0); |
| 30 | blk_put_request(rq); |
| 31 | |
Bartlomiej Zolnierkiewicz | 2bf427b | 2009-06-29 19:20:42 -0700 | [diff] [blame] | 32 | if (ret == 0 && ide_port_acpi(hwif)) { |
| 33 | /* call ACPI _PS3 only after both devices are suspended */ |
| 34 | if ((drive->dn & 1) || pair == NULL) |
| 35 | ide_acpi_set_state(hwif, 0); |
| 36 | } |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 37 | |
| 38 | return ret; |
| 39 | } |
| 40 | |
Christoph Hellwig | a7928c1 | 2015-04-17 22:37:20 +0200 | [diff] [blame] | 41 | static void ide_end_sync_rq(struct request *rq, int error) |
| 42 | { |
| 43 | complete(rq->end_io_data); |
| 44 | } |
| 45 | |
| 46 | static int ide_pm_execute_rq(struct request *rq) |
| 47 | { |
| 48 | struct request_queue *q = rq->q; |
| 49 | DECLARE_COMPLETION_ONSTACK(wait); |
| 50 | |
| 51 | rq->end_io_data = &wait; |
| 52 | rq->end_io = ide_end_sync_rq; |
| 53 | |
| 54 | spin_lock_irq(q->queue_lock); |
| 55 | if (unlikely(blk_queue_dying(q))) { |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 56 | rq->rq_flags |= RQF_QUIET; |
Christoph Hellwig | a7928c1 | 2015-04-17 22:37:20 +0200 | [diff] [blame] | 57 | rq->errors = -ENXIO; |
| 58 | __blk_end_request_all(rq, rq->errors); |
| 59 | spin_unlock_irq(q->queue_lock); |
| 60 | return -ENXIO; |
| 61 | } |
| 62 | __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT); |
| 63 | __blk_run_queue_uncond(q); |
| 64 | spin_unlock_irq(q->queue_lock); |
| 65 | |
| 66 | wait_for_completion_io(&wait); |
| 67 | |
| 68 | return rq->errors ? -EIO : 0; |
| 69 | } |
| 70 | |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 71 | int generic_ide_resume(struct device *dev) |
| 72 | { |
Miklos Szeredi | 9974e43 | 2012-08-21 17:20:30 +0200 | [diff] [blame] | 73 | ide_drive_t *drive = to_ide_device(dev); |
Greg Kroah-Hartman | fcb5207 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 74 | ide_drive_t *pair = ide_get_pair_dev(drive); |
Bartlomiej Zolnierkiewicz | 898ec22 | 2009-01-06 17:20:52 +0100 | [diff] [blame] | 75 | ide_hwif_t *hwif = drive->hwif; |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 76 | struct request *rq; |
Christoph Hellwig | a7928c1 | 2015-04-17 22:37:20 +0200 | [diff] [blame] | 77 | struct ide_pm_state rqpm; |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 78 | int err; |
| 79 | |
Bartlomiej Zolnierkiewicz | 2bf427b | 2009-06-29 19:20:42 -0700 | [diff] [blame] | 80 | if (ide_port_acpi(hwif)) { |
| 81 | /* call ACPI _PS0 / _STM only once */ |
| 82 | if ((drive->dn & 1) == 0 || pair == NULL) { |
| 83 | ide_acpi_set_state(hwif, 1); |
| 84 | ide_acpi_push_timing(hwif); |
| 85 | } |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 86 | |
Bartlomiej Zolnierkiewicz | 2bf427b | 2009-06-29 19:20:42 -0700 | [diff] [blame] | 87 | ide_acpi_exec_tfs(drive); |
| 88 | } |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 89 | |
| 90 | memset(&rqpm, 0, sizeof(rqpm)); |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 91 | rq = blk_get_request(drive->queue, READ, __GFP_RECLAIM); |
Christoph Hellwig | a7928c1 | 2015-04-17 22:37:20 +0200 | [diff] [blame] | 92 | rq->cmd_type = REQ_TYPE_ATA_PM_RESUME; |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 93 | rq->rq_flags |= RQF_PREEMPT; |
Tejun Heo | fc38b52 | 2009-04-19 07:00:43 +0900 | [diff] [blame] | 94 | rq->special = &rqpm; |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 95 | rqpm.pm_step = IDE_PM_START_RESUME; |
| 96 | rqpm.pm_state = PM_EVENT_ON; |
| 97 | |
Christoph Hellwig | a7928c1 | 2015-04-17 22:37:20 +0200 | [diff] [blame] | 98 | err = ide_pm_execute_rq(rq); |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 99 | blk_put_request(rq); |
| 100 | |
| 101 | if (err == 0 && dev->driver) { |
Bartlomiej Zolnierkiewicz | 7f3c868 | 2009-01-06 17:20:53 +0100 | [diff] [blame] | 102 | struct ide_driver *drv = to_ide_driver(dev->driver); |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 103 | |
| 104 | if (drv->resume) |
| 105 | drv->resume(drive); |
| 106 | } |
| 107 | |
| 108 | return err; |
| 109 | } |
| 110 | |
| 111 | void ide_complete_power_step(ide_drive_t *drive, struct request *rq) |
| 112 | { |
Christoph Hellwig | a7928c1 | 2015-04-17 22:37:20 +0200 | [diff] [blame] | 113 | struct ide_pm_state *pm = rq->special; |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 114 | |
| 115 | #ifdef DEBUG_PM |
| 116 | printk(KERN_INFO "%s: complete_power_step(step: %d)\n", |
| 117 | drive->name, pm->pm_step); |
| 118 | #endif |
| 119 | if (drive->media != ide_disk) |
| 120 | return; |
| 121 | |
| 122 | switch (pm->pm_step) { |
| 123 | case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */ |
| 124 | if (pm->pm_state == PM_EVENT_FREEZE) |
| 125 | pm->pm_step = IDE_PM_COMPLETED; |
| 126 | else |
| 127 | pm->pm_step = IDE_PM_STANDBY; |
| 128 | break; |
| 129 | case IDE_PM_STANDBY: /* Suspend step 2 (standby) */ |
| 130 | pm->pm_step = IDE_PM_COMPLETED; |
| 131 | break; |
| 132 | case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */ |
| 133 | pm->pm_step = IDE_PM_IDLE; |
| 134 | break; |
| 135 | case IDE_PM_IDLE: /* Resume step 2 (idle)*/ |
| 136 | pm->pm_step = IDE_PM_RESTORE_DMA; |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq) |
| 142 | { |
Christoph Hellwig | a7928c1 | 2015-04-17 22:37:20 +0200 | [diff] [blame] | 143 | struct ide_pm_state *pm = rq->special; |
Tejun Heo | fc38b52 | 2009-04-19 07:00:43 +0900 | [diff] [blame] | 144 | struct ide_cmd cmd = { }; |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 145 | |
| 146 | switch (pm->pm_step) { |
| 147 | case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */ |
| 148 | if (drive->media != ide_disk) |
| 149 | break; |
| 150 | /* Not supported? Switch to next step now. */ |
| 151 | if (ata_id_flush_enabled(drive->id) == 0 || |
| 152 | (drive->dev_flags & IDE_DFLAG_WCACHE) == 0) { |
| 153 | ide_complete_power_step(drive, rq); |
| 154 | return ide_stopped; |
| 155 | } |
| 156 | if (ata_id_flush_ext_enabled(drive->id)) |
Tejun Heo | fc38b52 | 2009-04-19 07:00:43 +0900 | [diff] [blame] | 157 | cmd.tf.command = ATA_CMD_FLUSH_EXT; |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 158 | else |
Tejun Heo | fc38b52 | 2009-04-19 07:00:43 +0900 | [diff] [blame] | 159 | cmd.tf.command = ATA_CMD_FLUSH; |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 160 | goto out_do_tf; |
| 161 | case IDE_PM_STANDBY: /* Suspend step 2 (standby) */ |
Tejun Heo | fc38b52 | 2009-04-19 07:00:43 +0900 | [diff] [blame] | 162 | cmd.tf.command = ATA_CMD_STANDBYNOW1; |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 163 | goto out_do_tf; |
| 164 | case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */ |
| 165 | ide_set_max_pio(drive); |
| 166 | /* |
| 167 | * skip IDE_PM_IDLE for ATAPI devices |
| 168 | */ |
| 169 | if (drive->media != ide_disk) |
| 170 | pm->pm_step = IDE_PM_RESTORE_DMA; |
| 171 | else |
| 172 | ide_complete_power_step(drive, rq); |
| 173 | return ide_stopped; |
| 174 | case IDE_PM_IDLE: /* Resume step 2 (idle) */ |
Tejun Heo | fc38b52 | 2009-04-19 07:00:43 +0900 | [diff] [blame] | 175 | cmd.tf.command = ATA_CMD_IDLEIMMEDIATE; |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 176 | goto out_do_tf; |
| 177 | case IDE_PM_RESTORE_DMA: /* Resume step 3 (restore DMA) */ |
| 178 | /* |
| 179 | * Right now, all we do is call ide_set_dma(drive), |
| 180 | * we could be smarter and check for current xfer_speed |
| 181 | * in struct drive etc... |
| 182 | */ |
| 183 | if (drive->hwif->dma_ops == NULL) |
| 184 | break; |
| 185 | /* |
| 186 | * TODO: respect IDE_DFLAG_USING_DMA |
| 187 | */ |
| 188 | ide_set_dma(drive); |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | pm->pm_step = IDE_PM_COMPLETED; |
Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 193 | |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 194 | return ide_stopped; |
| 195 | |
| 196 | out_do_tf: |
Tejun Heo | fc38b52 | 2009-04-19 07:00:43 +0900 | [diff] [blame] | 197 | cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE; |
| 198 | cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE; |
| 199 | cmd.protocol = ATA_PROT_NODATA; |
Bartlomiej Zolnierkiewicz | 22aa4b3 | 2009-03-27 12:46:37 +0100 | [diff] [blame] | 200 | |
Tejun Heo | fc38b52 | 2009-04-19 07:00:43 +0900 | [diff] [blame] | 201 | return do_rw_taskfile(drive, &cmd); |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
Bartlomiej Zolnierkiewicz | 3616b65 | 2009-03-27 12:46:29 +0100 | [diff] [blame] | 205 | * ide_complete_pm_rq - end the current Power Management request |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 206 | * @drive: target drive |
| 207 | * @rq: request |
| 208 | * |
| 209 | * This function cleans up the current PM request and stops the queue |
| 210 | * if necessary. |
| 211 | */ |
Bartlomiej Zolnierkiewicz | 3616b65 | 2009-03-27 12:46:29 +0100 | [diff] [blame] | 212 | void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq) |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 213 | { |
| 214 | struct request_queue *q = drive->queue; |
Christoph Hellwig | a7928c1 | 2015-04-17 22:37:20 +0200 | [diff] [blame] | 215 | struct ide_pm_state *pm = rq->special; |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 216 | unsigned long flags; |
| 217 | |
Bartlomiej Zolnierkiewicz | 3616b65 | 2009-03-27 12:46:29 +0100 | [diff] [blame] | 218 | ide_complete_power_step(drive, rq); |
| 219 | if (pm->pm_step != IDE_PM_COMPLETED) |
| 220 | return; |
| 221 | |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 222 | #ifdef DEBUG_PM |
| 223 | printk("%s: completing PM request, %s\n", drive->name, |
Christoph Hellwig | a7928c1 | 2015-04-17 22:37:20 +0200 | [diff] [blame] | 224 | (rq->cmd_type == REQ_TYPE_ATA_PM_SUSPEND) ? "suspend" : "resume"); |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 225 | #endif |
| 226 | spin_lock_irqsave(q->queue_lock, flags); |
Christoph Hellwig | a7928c1 | 2015-04-17 22:37:20 +0200 | [diff] [blame] | 227 | if (rq->cmd_type == REQ_TYPE_ATA_PM_SUSPEND) |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 228 | blk_stop_queue(q); |
Bartlomiej Zolnierkiewicz | 2ea5521 | 2009-01-14 19:19:04 +0100 | [diff] [blame] | 229 | else |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 230 | drive->dev_flags &= ~IDE_DFLAG_BLOCKED; |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 231 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 232 | |
Bartlomiej Zolnierkiewicz | b65fac3 | 2009-01-06 17:20:50 +0100 | [diff] [blame] | 233 | drive->hwif->rq = NULL; |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 234 | |
| 235 | if (blk_end_request(rq, 0, 0)) |
| 236 | BUG(); |
| 237 | } |
| 238 | |
| 239 | void ide_check_pm_state(ide_drive_t *drive, struct request *rq) |
| 240 | { |
Christoph Hellwig | a7928c1 | 2015-04-17 22:37:20 +0200 | [diff] [blame] | 241 | struct ide_pm_state *pm = rq->special; |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 242 | |
Christoph Hellwig | a7928c1 | 2015-04-17 22:37:20 +0200 | [diff] [blame] | 243 | if (rq->cmd_type == REQ_TYPE_ATA_PM_SUSPEND && |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 244 | pm->pm_step == IDE_PM_START_SUSPEND) |
| 245 | /* Mark drive blocked when starting the suspend sequence. */ |
| 246 | drive->dev_flags |= IDE_DFLAG_BLOCKED; |
Christoph Hellwig | a7928c1 | 2015-04-17 22:37:20 +0200 | [diff] [blame] | 247 | else if (rq->cmd_type == REQ_TYPE_ATA_PM_RESUME && |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 248 | pm->pm_step == IDE_PM_START_RESUME) { |
| 249 | /* |
| 250 | * The first thing we do on wakeup is to wait for BSY bit to |
| 251 | * go away (with a looong timeout) as a drive on this hwif may |
| 252 | * just be POSTing itself. |
| 253 | * We do that before even selecting as the "other" device on |
| 254 | * the bus may be broken enough to walk on our toes at this |
| 255 | * point. |
| 256 | */ |
| 257 | ide_hwif_t *hwif = drive->hwif; |
Sergei Shtylyov | fdd88f0 | 2009-03-31 20:15:33 +0200 | [diff] [blame] | 258 | const struct ide_tp_ops *tp_ops = hwif->tp_ops; |
Bartlomiej Zolnierkiewicz | 2ea5521 | 2009-01-14 19:19:04 +0100 | [diff] [blame] | 259 | struct request_queue *q = drive->queue; |
| 260 | unsigned long flags; |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 261 | int rc; |
| 262 | #ifdef DEBUG_PM |
| 263 | printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name); |
| 264 | #endif |
| 265 | rc = ide_wait_not_busy(hwif, 35000); |
| 266 | if (rc) |
| 267 | printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name); |
Sergei Shtylyov | fdd88f0 | 2009-03-31 20:15:33 +0200 | [diff] [blame] | 268 | tp_ops->dev_select(drive); |
| 269 | tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS); |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 270 | rc = ide_wait_not_busy(hwif, 100000); |
| 271 | if (rc) |
| 272 | printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name); |
Bartlomiej Zolnierkiewicz | 2ea5521 | 2009-01-14 19:19:04 +0100 | [diff] [blame] | 273 | |
| 274 | spin_lock_irqsave(q->queue_lock, flags); |
| 275 | blk_start_queue(q); |
| 276 | spin_unlock_irqrestore(q->queue_lock, flags); |
Bartlomiej Zolnierkiewicz | e2984c6 | 2008-12-29 20:27:37 +0100 | [diff] [blame] | 277 | } |
| 278 | } |