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