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, 0);
  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(drive->queue, REQ_OP_DRV_IN, BLK_MQ_REQ_PREEMPT);
  78. ide_req(rq)->type = ATA_PRIV_PM_RESUME;
  79. rq->special = &rqpm;
  80. rqpm.pm_step = IDE_PM_START_RESUME;
  81. rqpm.pm_state = PM_EVENT_ON;
  82. err = ide_pm_execute_rq(rq);
  83. blk_put_request(rq);
  84. if (err == 0 && dev->driver) {
  85. struct ide_driver *drv = to_ide_driver(dev->driver);
  86. if (drv->resume)
  87. drv->resume(drive);
  88. }
  89. return err;
  90. }
  91. void ide_complete_power_step(ide_drive_t *drive, struct request *rq)
  92. {
  93. struct ide_pm_state *pm = rq->special;
  94. #ifdef DEBUG_PM
  95. printk(KERN_INFO "%s: complete_power_step(step: %d)\n",
  96. drive->name, pm->pm_step);
  97. #endif
  98. if (drive->media != ide_disk)
  99. return;
  100. switch (pm->pm_step) {
  101. case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
  102. if (pm->pm_state == PM_EVENT_FREEZE)
  103. pm->pm_step = IDE_PM_COMPLETED;
  104. else
  105. pm->pm_step = IDE_PM_STANDBY;
  106. break;
  107. case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
  108. pm->pm_step = IDE_PM_COMPLETED;
  109. break;
  110. case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
  111. pm->pm_step = IDE_PM_IDLE;
  112. break;
  113. case IDE_PM_IDLE: /* Resume step 2 (idle)*/
  114. pm->pm_step = IDE_PM_RESTORE_DMA;
  115. break;
  116. }
  117. }
  118. ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
  119. {
  120. struct ide_pm_state *pm = rq->special;
  121. struct ide_cmd cmd = { };
  122. switch (pm->pm_step) {
  123. case IDE_PM_FLUSH_CACHE: /* Suspend step 1 (flush cache) */
  124. if (drive->media != ide_disk)
  125. break;
  126. /* Not supported? Switch to next step now. */
  127. if (ata_id_flush_enabled(drive->id) == 0 ||
  128. (drive->dev_flags & IDE_DFLAG_WCACHE) == 0) {
  129. ide_complete_power_step(drive, rq);
  130. return ide_stopped;
  131. }
  132. if (ata_id_flush_ext_enabled(drive->id))
  133. cmd.tf.command = ATA_CMD_FLUSH_EXT;
  134. else
  135. cmd.tf.command = ATA_CMD_FLUSH;
  136. goto out_do_tf;
  137. case IDE_PM_STANDBY: /* Suspend step 2 (standby) */
  138. cmd.tf.command = ATA_CMD_STANDBYNOW1;
  139. goto out_do_tf;
  140. case IDE_PM_RESTORE_PIO: /* Resume step 1 (restore PIO) */
  141. ide_set_max_pio(drive);
  142. /*
  143. * skip IDE_PM_IDLE for ATAPI devices
  144. */
  145. if (drive->media != ide_disk)
  146. pm->pm_step = IDE_PM_RESTORE_DMA;
  147. else
  148. ide_complete_power_step(drive, rq);
  149. return ide_stopped;
  150. case IDE_PM_IDLE: /* Resume step 2 (idle) */
  151. cmd.tf.command = ATA_CMD_IDLEIMMEDIATE;
  152. goto out_do_tf;
  153. case IDE_PM_RESTORE_DMA: /* Resume step 3 (restore DMA) */
  154. /*
  155. * Right now, all we do is call ide_set_dma(drive),
  156. * we could be smarter and check for current xfer_speed
  157. * in struct drive etc...
  158. */
  159. if (drive->hwif->dma_ops == NULL)
  160. break;
  161. /*
  162. * TODO: respect IDE_DFLAG_USING_DMA
  163. */
  164. ide_set_dma(drive);
  165. break;
  166. }
  167. pm->pm_step = IDE_PM_COMPLETED;
  168. return ide_stopped;
  169. out_do_tf:
  170. cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
  171. cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
  172. cmd.protocol = ATA_PROT_NODATA;
  173. return do_rw_taskfile(drive, &cmd);
  174. }
  175. /**
  176. * ide_complete_pm_rq - end the current Power Management request
  177. * @drive: target drive
  178. * @rq: request
  179. *
  180. * This function cleans up the current PM request and stops the queue
  181. * if necessary.
  182. */
  183. void ide_complete_pm_rq(ide_drive_t *drive, struct request *rq)
  184. {
  185. struct request_queue *q = drive->queue;
  186. struct ide_pm_state *pm = rq->special;
  187. unsigned long flags;
  188. ide_complete_power_step(drive, rq);
  189. if (pm->pm_step != IDE_PM_COMPLETED)
  190. return;
  191. #ifdef DEBUG_PM
  192. printk("%s: completing PM request, %s\n", drive->name,
  193. (ide_req(rq)->type == ATA_PRIV_PM_SUSPEND) ? "suspend" : "resume");
  194. #endif
  195. spin_lock_irqsave(q->queue_lock, flags);
  196. if (ide_req(rq)->type == ATA_PRIV_PM_SUSPEND)
  197. blk_stop_queue(q);
  198. else
  199. drive->dev_flags &= ~IDE_DFLAG_BLOCKED;
  200. spin_unlock_irqrestore(q->queue_lock, flags);
  201. drive->hwif->rq = NULL;
  202. if (blk_end_request(rq, BLK_STS_OK, 0))
  203. BUG();
  204. }
  205. void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
  206. {
  207. struct ide_pm_state *pm = rq->special;
  208. if (blk_rq_is_private(rq) &&
  209. ide_req(rq)->type == ATA_PRIV_PM_SUSPEND &&
  210. pm->pm_step == IDE_PM_START_SUSPEND)
  211. /* Mark drive blocked when starting the suspend sequence. */
  212. drive->dev_flags |= IDE_DFLAG_BLOCKED;
  213. else if (blk_rq_is_private(rq) &&
  214. ide_req(rq)->type == ATA_PRIV_PM_RESUME &&
  215. pm->pm_step == IDE_PM_START_RESUME) {
  216. /*
  217. * The first thing we do on wakeup is to wait for BSY bit to
  218. * go away (with a looong timeout) as a drive on this hwif may
  219. * just be POSTing itself.
  220. * We do that before even selecting as the "other" device on
  221. * the bus may be broken enough to walk on our toes at this
  222. * point.
  223. */
  224. ide_hwif_t *hwif = drive->hwif;
  225. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  226. struct request_queue *q = drive->queue;
  227. unsigned long flags;
  228. int rc;
  229. #ifdef DEBUG_PM
  230. printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
  231. #endif
  232. rc = ide_wait_not_busy(hwif, 35000);
  233. if (rc)
  234. printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
  235. tp_ops->dev_select(drive);
  236. tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
  237. rc = ide_wait_not_busy(hwif, 100000);
  238. if (rc)
  239. printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
  240. spin_lock_irqsave(q->queue_lock, flags);
  241. blk_start_queue(q);
  242. spin_unlock_irqrestore(q->queue_lock, flags);
  243. }
  244. }