scsi_pm.c 4.8 KB

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