scsi_pm.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. if (scsi_is_sdev_device(dev)) {
  67. /*
  68. * Parent device may have runtime suspended as soon as
  69. * it is woken up during the system resume.
  70. *
  71. * Resume it on behalf of child.
  72. */
  73. pm_runtime_get_sync(dev->parent);
  74. err = scsi_dev_type_resume(dev);
  75. pm_runtime_put_sync(dev->parent);
  76. }
  77. if (err == 0) {
  78. pm_runtime_disable(dev);
  79. pm_runtime_set_active(dev);
  80. pm_runtime_enable(dev);
  81. }
  82. return err;
  83. }
  84. static int scsi_bus_prepare(struct device *dev)
  85. {
  86. if (scsi_is_sdev_device(dev)) {
  87. /* sd probing uses async_schedule. Wait until it finishes. */
  88. async_synchronize_full_domain(&scsi_sd_probe_domain);
  89. } else if (scsi_is_host_device(dev)) {
  90. /* Wait until async scanning is finished */
  91. scsi_complete_async_scans();
  92. }
  93. return 0;
  94. }
  95. static int scsi_bus_suspend(struct device *dev)
  96. {
  97. return scsi_bus_suspend_common(dev, PMSG_SUSPEND);
  98. }
  99. static int scsi_bus_freeze(struct device *dev)
  100. {
  101. return scsi_bus_suspend_common(dev, PMSG_FREEZE);
  102. }
  103. static int scsi_bus_poweroff(struct device *dev)
  104. {
  105. return scsi_bus_suspend_common(dev, PMSG_HIBERNATE);
  106. }
  107. #else /* CONFIG_PM_SLEEP */
  108. #define scsi_bus_resume_common NULL
  109. #define scsi_bus_prepare NULL
  110. #define scsi_bus_suspend NULL
  111. #define scsi_bus_freeze NULL
  112. #define scsi_bus_poweroff NULL
  113. #endif /* CONFIG_PM_SLEEP */
  114. #ifdef CONFIG_PM_RUNTIME
  115. static int scsi_runtime_suspend(struct device *dev)
  116. {
  117. int err = 0;
  118. dev_dbg(dev, "scsi_runtime_suspend\n");
  119. if (scsi_is_sdev_device(dev)) {
  120. err = scsi_dev_type_suspend(dev, PMSG_AUTO_SUSPEND);
  121. if (err == -EAGAIN)
  122. pm_schedule_suspend(dev, jiffies_to_msecs(
  123. round_jiffies_up_relative(HZ/10)));
  124. }
  125. /* Insert hooks here for targets, hosts, and transport classes */
  126. return err;
  127. }
  128. static int scsi_runtime_resume(struct device *dev)
  129. {
  130. int err = 0;
  131. dev_dbg(dev, "scsi_runtime_resume\n");
  132. if (scsi_is_sdev_device(dev))
  133. err = scsi_dev_type_resume(dev);
  134. /* Insert hooks here for targets, hosts, and transport classes */
  135. return err;
  136. }
  137. static int scsi_runtime_idle(struct device *dev)
  138. {
  139. int err;
  140. dev_dbg(dev, "scsi_runtime_idle\n");
  141. /* Insert hooks here for targets, hosts, and transport classes */
  142. if (scsi_is_sdev_device(dev))
  143. err = pm_schedule_suspend(dev, 100);
  144. else
  145. err = pm_runtime_suspend(dev);
  146. return err;
  147. }
  148. int scsi_autopm_get_device(struct scsi_device *sdev)
  149. {
  150. int err;
  151. err = pm_runtime_get_sync(&sdev->sdev_gendev);
  152. if (err < 0 && err !=-EACCES)
  153. pm_runtime_put_sync(&sdev->sdev_gendev);
  154. else
  155. err = 0;
  156. return err;
  157. }
  158. EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
  159. void scsi_autopm_put_device(struct scsi_device *sdev)
  160. {
  161. pm_runtime_put_sync(&sdev->sdev_gendev);
  162. }
  163. EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
  164. void scsi_autopm_get_target(struct scsi_target *starget)
  165. {
  166. pm_runtime_get_sync(&starget->dev);
  167. }
  168. void scsi_autopm_put_target(struct scsi_target *starget)
  169. {
  170. pm_runtime_put_sync(&starget->dev);
  171. }
  172. int scsi_autopm_get_host(struct Scsi_Host *shost)
  173. {
  174. int err;
  175. err = pm_runtime_get_sync(&shost->shost_gendev);
  176. if (err < 0 && err !=-EACCES)
  177. pm_runtime_put_sync(&shost->shost_gendev);
  178. else
  179. err = 0;
  180. return err;
  181. }
  182. void scsi_autopm_put_host(struct Scsi_Host *shost)
  183. {
  184. pm_runtime_put_sync(&shost->shost_gendev);
  185. }
  186. #else
  187. #define scsi_runtime_suspend NULL
  188. #define scsi_runtime_resume NULL
  189. #define scsi_runtime_idle NULL
  190. #endif /* CONFIG_PM_RUNTIME */
  191. const struct dev_pm_ops scsi_bus_pm_ops = {
  192. .prepare = scsi_bus_prepare,
  193. .suspend = scsi_bus_suspend,
  194. .resume = scsi_bus_resume_common,
  195. .freeze = scsi_bus_freeze,
  196. .thaw = scsi_bus_resume_common,
  197. .poweroff = scsi_bus_poweroff,
  198. .restore = scsi_bus_resume_common,
  199. .runtime_suspend = scsi_runtime_suspend,
  200. .runtime_resume = scsi_runtime_resume,
  201. .runtime_idle = scsi_runtime_idle,
  202. };