ide-pm.c 6.3 KB

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