ide-pm.c 7.3 KB

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