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