scsi_pm.c 5.2 KB

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