scsi_pm.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * scsi_pm.c Copyright (C) 2010 Alan Stern
  3. *
  4. * SCSI dynamic Power Management
  5. * Initial version: Alan Stern <stern@rowland.harvard.edu>
  6. */
  7. #include <linux/pm_runtime.h>
  8. #include <linux/export.h>
  9. #include <linux/async.h>
  10. #include <scsi/scsi.h>
  11. #include <scsi/scsi_device.h>
  12. #include <scsi/scsi_driver.h>
  13. #include <scsi/scsi_host.h>
  14. #include "scsi_priv.h"
  15. static int scsi_dev_type_suspend(struct device *dev, pm_message_t msg)
  16. {
  17. struct device_driver *drv;
  18. int err;
  19. err = scsi_device_quiesce(to_scsi_device(dev));
  20. if (err == 0) {
  21. drv = dev->driver;
  22. if (drv && drv->suspend) {
  23. err = drv->suspend(dev, msg);
  24. if (err)
  25. scsi_device_resume(to_scsi_device(dev));
  26. }
  27. }
  28. dev_dbg(dev, "scsi suspend: %d\n", err);
  29. return err;
  30. }
  31. static int scsi_dev_type_resume(struct device *dev)
  32. {
  33. struct device_driver *drv;
  34. int err = 0;
  35. drv = dev->driver;
  36. if (drv && drv->resume)
  37. err = drv->resume(dev);
  38. scsi_device_resume(to_scsi_device(dev));
  39. dev_dbg(dev, "scsi resume: %d\n", err);
  40. return err;
  41. }
  42. #ifdef CONFIG_PM_SLEEP
  43. static int scsi_bus_suspend_common(struct device *dev, pm_message_t msg)
  44. {
  45. int err = 0;
  46. if (scsi_is_sdev_device(dev)) {
  47. /*
  48. * sd is the only high-level SCSI driver to implement runtime
  49. * PM, and sd treats runtime suspend, system suspend, and
  50. * system hibernate identically (but not system freeze).
  51. */
  52. if (pm_runtime_suspended(dev)) {
  53. if (msg.event == PM_EVENT_SUSPEND ||
  54. msg.event == PM_EVENT_HIBERNATE)
  55. return 0; /* already suspended */
  56. /* wake up device so that FREEZE will succeed */
  57. pm_runtime_resume(dev);
  58. }
  59. err = scsi_dev_type_suspend(dev, msg);
  60. }
  61. return err;
  62. }
  63. static int scsi_bus_resume_common(struct device *dev)
  64. {
  65. int err = 0;
  66. /*
  67. * Parent device may have runtime suspended as soon as
  68. * it is woken up during the system resume.
  69. *
  70. * Resume it on behalf of child.
  71. */
  72. pm_runtime_get_sync(dev->parent);
  73. if (scsi_is_sdev_device(dev))
  74. err = scsi_dev_type_resume(dev);
  75. if (err == 0) {
  76. pm_runtime_disable(dev);
  77. pm_runtime_set_active(dev);
  78. pm_runtime_enable(dev);
  79. }
  80. pm_runtime_put_sync(dev->parent);
  81. return err;
  82. }
  83. static int scsi_bus_prepare(struct device *dev)
  84. {
  85. if (scsi_is_sdev_device(dev)) {
  86. /* sd probing uses async_schedule. Wait until it finishes. */
  87. async_synchronize_full_domain(&scsi_sd_probe_domain);
  88. } else if (scsi_is_host_device(dev)) {
  89. /* Wait until async scanning is finished */
  90. scsi_complete_async_scans();
  91. }
  92. return 0;
  93. }
  94. static int scsi_bus_suspend(struct device *dev)
  95. {
  96. return scsi_bus_suspend_common(dev, PMSG_SUSPEND);
  97. }
  98. static int scsi_bus_freeze(struct device *dev)
  99. {
  100. return scsi_bus_suspend_common(dev, PMSG_FREEZE);
  101. }
  102. static int scsi_bus_poweroff(struct device *dev)
  103. {
  104. return scsi_bus_suspend_common(dev, PMSG_HIBERNATE);
  105. }
  106. #else /* CONFIG_PM_SLEEP */
  107. #define scsi_bus_resume_common NULL
  108. #define scsi_bus_prepare NULL
  109. #define scsi_bus_suspend NULL
  110. #define scsi_bus_freeze NULL
  111. #define scsi_bus_poweroff NULL
  112. #endif /* CONFIG_PM_SLEEP */
  113. #ifdef CONFIG_PM_RUNTIME
  114. static int scsi_runtime_suspend(struct device *dev)
  115. {
  116. int err = 0;
  117. dev_dbg(dev, "scsi_runtime_suspend\n");
  118. if (scsi_is_sdev_device(dev)) {
  119. err = scsi_dev_type_suspend(dev, PMSG_AUTO_SUSPEND);
  120. if (err == -EAGAIN)
  121. pm_schedule_suspend(dev, jiffies_to_msecs(
  122. round_jiffies_up_relative(HZ/10)));
  123. }
  124. /* Insert hooks here for targets, hosts, and transport classes */
  125. return err;
  126. }
  127. static int scsi_runtime_resume(struct device *dev)
  128. {
  129. int err = 0;
  130. dev_dbg(dev, "scsi_runtime_resume\n");
  131. if (scsi_is_sdev_device(dev))
  132. err = scsi_dev_type_resume(dev);
  133. /* Insert hooks here for targets, hosts, and transport classes */
  134. return err;
  135. }
  136. static int scsi_runtime_idle(struct device *dev)
  137. {
  138. int err;
  139. dev_dbg(dev, "scsi_runtime_idle\n");
  140. /* Insert hooks here for targets, hosts, and transport classes */
  141. if (scsi_is_sdev_device(dev))
  142. err = pm_schedule_suspend(dev, 100);
  143. else
  144. err = pm_runtime_suspend(dev);
  145. return err;
  146. }
  147. int scsi_autopm_get_device(struct scsi_device *sdev)
  148. {
  149. int err;
  150. err = pm_runtime_get_sync(&sdev->sdev_gendev);
  151. if (err < 0 && err !=-EACCES)
  152. pm_runtime_put_sync(&sdev->sdev_gendev);
  153. else
  154. err = 0;
  155. return err;
  156. }
  157. EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
  158. void scsi_autopm_put_device(struct scsi_device *sdev)
  159. {
  160. pm_runtime_put_sync(&sdev->sdev_gendev);
  161. }
  162. EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
  163. void scsi_autopm_get_target(struct scsi_target *starget)
  164. {
  165. pm_runtime_get_sync(&starget->dev);
  166. }
  167. void scsi_autopm_put_target(struct scsi_target *starget)
  168. {
  169. pm_runtime_put_sync(&starget->dev);
  170. }
  171. int scsi_autopm_get_host(struct Scsi_Host *shost)
  172. {
  173. int err;
  174. err = pm_runtime_get_sync(&shost->shost_gendev);
  175. if (err < 0 && err !=-EACCES)
  176. pm_runtime_put_sync(&shost->shost_gendev);
  177. else
  178. err = 0;
  179. return err;
  180. }
  181. void scsi_autopm_put_host(struct Scsi_Host *shost)
  182. {
  183. pm_runtime_put_sync(&shost->shost_gendev);
  184. }
  185. #else
  186. #define scsi_runtime_suspend NULL
  187. #define scsi_runtime_resume NULL
  188. #define scsi_runtime_idle NULL
  189. #endif /* CONFIG_PM_RUNTIME */
  190. const struct dev_pm_ops scsi_bus_pm_ops = {
  191. .prepare = scsi_bus_prepare,
  192. .suspend = scsi_bus_suspend,
  193. .resume = scsi_bus_resume_common,
  194. .freeze = scsi_bus_freeze,
  195. .thaw = scsi_bus_resume_common,
  196. .poweroff = scsi_bus_poweroff,
  197. .restore = scsi_bus_resume_common,
  198. .runtime_suspend = scsi_runtime_suspend,
  199. .runtime_resume = scsi_runtime_resume,
  200. .runtime_idle = scsi_runtime_idle,
  201. };