ide-pm.c 6.5 KB

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