ide-pm.c 6.4 KB

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