ide-pm.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include <linux/kernel.h>
  2. #include <linux/ide.h>
  3. #include <linux/hdreg.h>
  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 = HWIF(drive);
  8. struct request *rq;
  9. struct request_pm_state rqpm;
  10. ide_task_t args;
  11. int ret;
  12. /* call ACPI _GTM only once */
  13. if ((drive->dn & 1) == 0 || pair == NULL)
  14. ide_acpi_get_timing(hwif);
  15. memset(&rqpm, 0, sizeof(rqpm));
  16. memset(&args, 0, sizeof(args));
  17. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  18. rq->cmd_type = REQ_TYPE_PM_SUSPEND;
  19. rq->special = &args;
  20. rq->data = &rqpm;
  21. rqpm.pm_step = IDE_PM_START_SUSPEND;
  22. if (mesg.event == PM_EVENT_PRETHAW)
  23. mesg.event = PM_EVENT_FREEZE;
  24. rqpm.pm_state = mesg.event;
  25. ret = blk_execute_rq(drive->queue, NULL, rq, 0);
  26. blk_put_request(rq);
  27. /* call ACPI _PS3 only after both devices are suspended */
  28. if (ret == 0 && ((drive->dn & 1) || pair == NULL))
  29. ide_acpi_set_state(hwif, 0);
  30. return ret;
  31. }
  32. int generic_ide_resume(struct device *dev)
  33. {
  34. ide_drive_t *drive = dev->driver_data, *pair = ide_get_pair_dev(drive);
  35. ide_hwif_t *hwif = HWIF(drive);
  36. struct request *rq;
  37. struct request_pm_state rqpm;
  38. ide_task_t args;
  39. int err;
  40. /* call ACPI _PS0 / _STM only once */
  41. if ((drive->dn & 1) == 0 || pair == NULL) {
  42. ide_acpi_set_state(hwif, 1);
  43. ide_acpi_push_timing(hwif);
  44. }
  45. ide_acpi_exec_tfs(drive);
  46. memset(&rqpm, 0, sizeof(rqpm));
  47. memset(&args, 0, sizeof(args));
  48. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  49. rq->cmd_type = REQ_TYPE_PM_RESUME;
  50. rq->cmd_flags |= REQ_PREEMPT;
  51. rq->special = &args;
  52. rq->data = &rqpm;
  53. rqpm.pm_step = IDE_PM_START_RESUME;
  54. rqpm.pm_state = PM_EVENT_ON;
  55. err = blk_execute_rq(drive->queue, NULL, rq, 1);
  56. blk_put_request(rq);
  57. if (err == 0 && dev->driver) {
  58. ide_driver_t *drv = to_ide_driver(dev->driver);
  59. if (drv->resume)
  60. drv->resume(drive);
  61. }
  62. return err;
  63. }
  64. void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
  65. {
  66. struct request_pm_state *pm = rq->data;
  67. #ifdef DEBUG_PM
  68. printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
  69. drive->name, pm->pm_step);
  70. #endif
  71. if (drive->media != ide_disk)
  72. return;
  73. switch (pm->pm_step) {
  74. case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
  75. if (pm->pm_state == PM_EVENT_FREEZE)
  76. pm->pm_step = IDE_PM_COMPLETED;
  77. else
  78. pm->pm_step = IDE_PM_STANDBY;
  79. break;
  80. case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
  81. pm->pm_step = IDE_PM_COMPLETED;
  82. break;
  83. case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
  84. pm->pm_step = IDE_PM_IDLE;
  85. break;
  86. case IDE_PM_IDLE: /* Resume step 2 (idle)*/
  87. pm->pm_step = IDE_PM_RESTORE_DMA;
  88. break;
  89. }
  90. }
  91. ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
  92. {
  93. struct request_pm_state *pm = rq->data;
  94. ide_task_t *args = rq->special;
  95. memset(args, 0, sizeof(*args));
  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. args->tf.command = ATA_CMD_FLUSH_EXT;
  108. else
  109. args->tf.command = ATA_CMD_FLUSH;
  110. goto out_do_tf;
  111. case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
  112. args->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. args->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. args->tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  145. args->data_phase = TASKFILE_NO_DATA;
  146. return do_rw_taskfile(drive, args);
  147. }
  148. /**
  149. * ide_complete_pm_request - end the current Power Management request
  150. * @drive: target drive
  151. * @rq: request
  152. *
  153. * This function cleans up the current PM request and stops the queue
  154. * if necessary.
  155. */
  156. void ide_complete_pm_request(ide_drive_t *drive, struct request *rq)
  157. {
  158. struct request_queue *q = drive->queue;
  159. unsigned long flags;
  160. #ifdef DEBUG_PM
  161. printk("%s: completing PM request, %s\n", drive->name,
  162. blk_pm_suspend_request(rq) ? "suspend" : "resume");
  163. #endif
  164. spin_lock_irqsave(q->queue_lock, flags);
  165. if (blk_pm_suspend_request(rq)) {
  166. blk_stop_queue(q);
  167. } else {
  168. drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
  169. blk_start_queue(q);
  170. }
  171. spin_unlock_irqrestore(q->queue_lock, flags);
  172. drive->hwif->hwgroup->rq = NULL;
  173. if (blk_end_request(rq, 0, 0))
  174. BUG();
  175. }
  176. void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
  177. {
  178. struct request_pm_state *pm = rq->data;
  179. if (blk_pm_suspend_request(rq) &&
  180. pm->pm_step == IDE_PM_START_SUSPEND)
  181. /* Mark drive blocked when starting the suspend sequence. */
  182. drive->dev_flags |= IDE_DFLAG_BLOCKED;
  183. else if (blk_pm_resume_request(rq) &&
  184. pm->pm_step == IDE_PM_START_RESUME) {
  185. /*
  186. * The first thing we do on wakeup is to wait for BSY bit to
  187. * go away (with a looong timeout) as a drive on this hwif may
  188. * just be POSTing itself.
  189. * We do that before even selecting as the "other" device on
  190. * the bus may be broken enough to walk on our toes at this
  191. * point.
  192. */
  193. ide_hwif_t *hwif = drive->hwif;
  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. SELECT_DRIVE(drive);
  202. hwif->tp_ops->set_irq(hwif, 1);
  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. }
  207. }