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