ide-pm.c 6.8 KB

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