ide-pm.c 6.5 KB

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