scsi_pm.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/async.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. err = scsi_dev_type_suspend(dev, msg);
  44. return err;
  45. }
  46. static int scsi_bus_resume_common(struct device *dev)
  47. {
  48. int err = 0;
  49. if (scsi_is_sdev_device(dev))
  50. err = scsi_dev_type_resume(dev);
  51. if (err == 0) {
  52. pm_runtime_disable(dev);
  53. pm_runtime_set_active(dev);
  54. pm_runtime_enable(dev);
  55. }
  56. return err;
  57. }
  58. static int scsi_bus_prepare(struct device *dev)
  59. {
  60. if (scsi_is_sdev_device(dev)) {
  61. /* sd probing uses async_schedule. Wait until it finishes. */
  62. async_synchronize_full();
  63. } else if (scsi_is_host_device(dev)) {
  64. /* Wait until async scanning is finished */
  65. scsi_complete_async_scans();
  66. }
  67. return 0;
  68. }
  69. static int scsi_bus_suspend(struct device *dev)
  70. {
  71. return scsi_bus_suspend_common(dev, PMSG_SUSPEND);
  72. }
  73. static int scsi_bus_freeze(struct device *dev)
  74. {
  75. return scsi_bus_suspend_common(dev, PMSG_FREEZE);
  76. }
  77. static int scsi_bus_poweroff(struct device *dev)
  78. {
  79. return scsi_bus_suspend_common(dev, PMSG_HIBERNATE);
  80. }
  81. #else /* CONFIG_PM_SLEEP */
  82. #define scsi_bus_resume_common NULL
  83. #define scsi_bus_prepare NULL
  84. #define scsi_bus_suspend NULL
  85. #define scsi_bus_freeze NULL
  86. #define scsi_bus_poweroff NULL
  87. #endif /* CONFIG_PM_SLEEP */
  88. #ifdef CONFIG_PM_RUNTIME
  89. static int scsi_runtime_suspend(struct device *dev)
  90. {
  91. int err = 0;
  92. dev_dbg(dev, "scsi_runtime_suspend\n");
  93. if (scsi_is_sdev_device(dev)) {
  94. err = scsi_dev_type_suspend(dev, PMSG_AUTO_SUSPEND);
  95. if (err == -EAGAIN)
  96. pm_schedule_suspend(dev, jiffies_to_msecs(
  97. round_jiffies_up_relative(HZ/10)));
  98. }
  99. /* Insert hooks here for targets, hosts, and transport classes */
  100. return err;
  101. }
  102. static int scsi_runtime_resume(struct device *dev)
  103. {
  104. int err = 0;
  105. dev_dbg(dev, "scsi_runtime_resume\n");
  106. if (scsi_is_sdev_device(dev))
  107. err = scsi_dev_type_resume(dev);
  108. /* Insert hooks here for targets, hosts, and transport classes */
  109. return err;
  110. }
  111. static int scsi_runtime_idle(struct device *dev)
  112. {
  113. int err;
  114. dev_dbg(dev, "scsi_runtime_idle\n");
  115. /* Insert hooks here for targets, hosts, and transport classes */
  116. if (scsi_is_sdev_device(dev))
  117. err = pm_schedule_suspend(dev, 100);
  118. else
  119. err = pm_runtime_suspend(dev);
  120. return err;
  121. }
  122. int scsi_autopm_get_device(struct scsi_device *sdev)
  123. {
  124. int err;
  125. err = pm_runtime_get_sync(&sdev->sdev_gendev);
  126. if (err < 0)
  127. pm_runtime_put_sync(&sdev->sdev_gendev);
  128. else if (err > 0)
  129. err = 0;
  130. return err;
  131. }
  132. EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
  133. void scsi_autopm_put_device(struct scsi_device *sdev)
  134. {
  135. pm_runtime_put_sync(&sdev->sdev_gendev);
  136. }
  137. EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
  138. void scsi_autopm_get_target(struct scsi_target *starget)
  139. {
  140. pm_runtime_get_sync(&starget->dev);
  141. }
  142. void scsi_autopm_put_target(struct scsi_target *starget)
  143. {
  144. pm_runtime_put_sync(&starget->dev);
  145. }
  146. int scsi_autopm_get_host(struct Scsi_Host *shost)
  147. {
  148. int err;
  149. err = pm_runtime_get_sync(&shost->shost_gendev);
  150. if (err < 0)
  151. pm_runtime_put_sync(&shost->shost_gendev);
  152. else if (err > 0)
  153. err = 0;
  154. return err;
  155. }
  156. void scsi_autopm_put_host(struct Scsi_Host *shost)
  157. {
  158. pm_runtime_put_sync(&shost->shost_gendev);
  159. }
  160. #else
  161. #define scsi_runtime_suspend NULL
  162. #define scsi_runtime_resume NULL
  163. #define scsi_runtime_idle NULL
  164. #endif /* CONFIG_PM_RUNTIME */
  165. const struct dev_pm_ops scsi_bus_pm_ops = {
  166. .prepare = scsi_bus_prepare,
  167. .suspend = scsi_bus_suspend,
  168. .resume = scsi_bus_resume_common,
  169. .freeze = scsi_bus_freeze,
  170. .thaw = scsi_bus_resume_common,
  171. .poweroff = scsi_bus_poweroff,
  172. .restore = scsi_bus_resume_common,
  173. .runtime_suspend = scsi_runtime_suspend,
  174. .runtime_resume = scsi_runtime_resume,
  175. .runtime_idle = scsi_runtime_idle,
  176. };