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