ide-pm.c 7.2 KB

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