Merge branch 'for-linus' of master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6] / drivers / ide / ide-pm.c
1 #include <linux/kernel.h>
2 #include <linux/ide.h>
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);
7         ide_hwif_t *hwif = drive->hwif;
8         struct request *rq;
9         struct request_pm_state rqpm;
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));
17         rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
18         rq->cmd_type = REQ_TYPE_PM_SUSPEND;
19         rq->special = &rqpm;
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);
38         ide_hwif_t *hwif = drive->hwif;
39         struct request *rq;
40         struct request_pm_state rqpm;
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));
52         rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
53         rq->cmd_type = REQ_TYPE_PM_RESUME;
54         rq->cmd_flags |= REQ_PREEMPT;
55         rq->special = &rqpm;
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) {
63                 struct ide_driver *drv = to_ide_driver(dev->driver);
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 {
74         struct request_pm_state *pm = rq->special;
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 {
104         struct request_pm_state *pm = rq->special;
105         struct ide_cmd cmd = { };
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))
118                         cmd.tf.command = ATA_CMD_FLUSH_EXT;
119                 else
120                         cmd.tf.command = ATA_CMD_FLUSH;
121                 goto out_do_tf;
122         case IDE_PM_STANDBY:            /* Suspend step 2 (standby) */
123                 cmd.tf.command = ATA_CMD_STANDBYNOW1;
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) */
136                 cmd.tf.command = ATA_CMD_IDLEIMMEDIATE;
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;
154
155         return ide_stopped;
156
157 out_do_tf:
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;
161
162         return do_rw_taskfile(drive, &cmd);
163 }
164
165 /**
166  *      ide_complete_pm_rq - end the current Power Management request
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  */
173 void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq)
174 {
175         struct request_queue *q = drive->queue;
176         struct request_pm_state *pm = rq->special;
177         unsigned long flags;
178
179         ide_complete_power_step(drive, rq);
180         if (pm->pm_step != IDE_PM_COMPLETED)
181                 return;
182
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);
188         if (blk_pm_suspend_request(rq))
189                 blk_stop_queue(q);
190         else
191                 drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
192         spin_unlock_irqrestore(q->queue_lock, flags);
193
194         drive->hwif->rq = NULL;
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 {
202         struct request_pm_state *pm = rq->special;
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;
219                 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
220                 struct request_queue *q = drive->queue;
221                 unsigned long flags;
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);
229                 tp_ops->dev_select(drive);
230                 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
231                 rc = ide_wait_not_busy(hwif, 100000);
232                 if (rc)
233                         printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
234
235                 spin_lock_irqsave(q->queue_lock, flags);
236                 blk_start_queue(q);
237                 spin_unlock_irqrestore(q->queue_lock, flags);
238         }
239 }