scsi_pm.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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, int (*cb)(struct device *))
  16. {
  17. int err;
  18. err = scsi_device_quiesce(to_scsi_device(dev));
  19. if (err == 0) {
  20. if (cb) {
  21. err = cb(dev);
  22. if (err)
  23. scsi_device_resume(to_scsi_device(dev));
  24. }
  25. }
  26. dev_dbg(dev, "scsi suspend: %d\n", err);
  27. return err;
  28. }
  29. static int scsi_dev_type_resume(struct device *dev, int (*cb)(struct device *))
  30. {
  31. int err = 0;
  32. if (cb)
  33. err = cb(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
  40. scsi_bus_suspend_common(struct device *dev, int (*cb)(struct device *))
  41. {
  42. int err = 0;
  43. if (scsi_is_sdev_device(dev)) {
  44. /*
  45. * All the high-level SCSI drivers that implement runtime
  46. * PM treat runtime suspend, system suspend, and system
  47. * hibernate identically.
  48. */
  49. if (pm_runtime_suspended(dev))
  50. return 0;
  51. err = scsi_dev_type_suspend(dev, cb);
  52. }
  53. return err;
  54. }
  55. static int
  56. scsi_bus_resume_common(struct device *dev, int (*cb)(struct device *))
  57. {
  58. int err = 0;
  59. if (scsi_is_sdev_device(dev))
  60. err = scsi_dev_type_resume(dev, cb);
  61. if (err == 0) {
  62. pm_runtime_disable(dev);
  63. pm_runtime_set_active(dev);
  64. pm_runtime_enable(dev);
  65. }
  66. return err;
  67. }
  68. static int scsi_bus_prepare(struct device *dev)
  69. {
  70. if (scsi_is_sdev_device(dev)) {
  71. /* sd probing uses async_schedule. Wait until it finishes. */
  72. async_synchronize_full_domain(&scsi_sd_probe_domain);
  73. } else if (scsi_is_host_device(dev)) {
  74. /* Wait until async scanning is finished */
  75. scsi_complete_async_scans();
  76. }
  77. return 0;
  78. }
  79. static int scsi_bus_suspend(struct device *dev)
  80. {
  81. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  82. return scsi_bus_suspend_common(dev, pm ? pm->suspend : NULL);
  83. }
  84. static int scsi_bus_resume(struct device *dev)
  85. {
  86. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  87. return scsi_bus_resume_common(dev, pm ? pm->resume : NULL);
  88. }
  89. static int scsi_bus_freeze(struct device *dev)
  90. {
  91. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  92. return scsi_bus_suspend_common(dev, pm ? pm->freeze : NULL);
  93. }
  94. static int scsi_bus_thaw(struct device *dev)
  95. {
  96. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  97. return scsi_bus_resume_common(dev, pm ? pm->thaw : NULL);
  98. }
  99. static int scsi_bus_poweroff(struct device *dev)
  100. {
  101. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  102. return scsi_bus_suspend_common(dev, pm ? pm->poweroff : NULL);
  103. }
  104. static int scsi_bus_restore(struct device *dev)
  105. {
  106. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  107. return scsi_bus_resume_common(dev, pm ? pm->restore : NULL);
  108. }
  109. #else /* CONFIG_PM_SLEEP */
  110. #define scsi_bus_prepare NULL
  111. #define scsi_bus_suspend NULL
  112. #define scsi_bus_resume NULL
  113. #define scsi_bus_freeze NULL
  114. #define scsi_bus_thaw NULL
  115. #define scsi_bus_poweroff NULL
  116. #define scsi_bus_restore NULL
  117. #endif /* CONFIG_PM_SLEEP */
  118. #ifdef CONFIG_PM_RUNTIME
  119. static int scsi_runtime_suspend(struct device *dev)
  120. {
  121. int err = 0;
  122. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  123. dev_dbg(dev, "scsi_runtime_suspend\n");
  124. if (scsi_is_sdev_device(dev)) {
  125. err = scsi_dev_type_suspend(dev,
  126. pm ? pm->runtime_suspend : NULL);
  127. if (err == -EAGAIN)
  128. pm_schedule_suspend(dev, jiffies_to_msecs(
  129. round_jiffies_up_relative(HZ/10)));
  130. }
  131. /* Insert hooks here for targets, hosts, and transport classes */
  132. return err;
  133. }
  134. static int scsi_runtime_resume(struct device *dev)
  135. {
  136. int err = 0;
  137. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  138. dev_dbg(dev, "scsi_runtime_resume\n");
  139. if (scsi_is_sdev_device(dev))
  140. err = scsi_dev_type_resume(dev, pm ? pm->runtime_resume : NULL);
  141. /* Insert hooks here for targets, hosts, and transport classes */
  142. return err;
  143. }
  144. static int scsi_runtime_idle(struct device *dev)
  145. {
  146. int err;
  147. dev_dbg(dev, "scsi_runtime_idle\n");
  148. /* Insert hooks here for targets, hosts, and transport classes */
  149. if (scsi_is_sdev_device(dev))
  150. err = pm_schedule_suspend(dev, 100);
  151. else
  152. err = pm_runtime_suspend(dev);
  153. return err;
  154. }
  155. int scsi_autopm_get_device(struct scsi_device *sdev)
  156. {
  157. int err;
  158. err = pm_runtime_get_sync(&sdev->sdev_gendev);
  159. if (err < 0 && err !=-EACCES)
  160. pm_runtime_put_sync(&sdev->sdev_gendev);
  161. else
  162. err = 0;
  163. return err;
  164. }
  165. EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
  166. void scsi_autopm_put_device(struct scsi_device *sdev)
  167. {
  168. pm_runtime_put_sync(&sdev->sdev_gendev);
  169. }
  170. EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
  171. void scsi_autopm_get_target(struct scsi_target *starget)
  172. {
  173. pm_runtime_get_sync(&starget->dev);
  174. }
  175. void scsi_autopm_put_target(struct scsi_target *starget)
  176. {
  177. pm_runtime_put_sync(&starget->dev);
  178. }
  179. int scsi_autopm_get_host(struct Scsi_Host *shost)
  180. {
  181. int err;
  182. err = pm_runtime_get_sync(&shost->shost_gendev);
  183. if (err < 0 && err !=-EACCES)
  184. pm_runtime_put_sync(&shost->shost_gendev);
  185. else
  186. err = 0;
  187. return err;
  188. }
  189. void scsi_autopm_put_host(struct Scsi_Host *shost)
  190. {
  191. pm_runtime_put_sync(&shost->shost_gendev);
  192. }
  193. #else
  194. #define scsi_runtime_suspend NULL
  195. #define scsi_runtime_resume NULL
  196. #define scsi_runtime_idle NULL
  197. #endif /* CONFIG_PM_RUNTIME */
  198. const struct dev_pm_ops scsi_bus_pm_ops = {
  199. .prepare = scsi_bus_prepare,
  200. .suspend = scsi_bus_suspend,
  201. .resume = scsi_bus_resume,
  202. .freeze = scsi_bus_freeze,
  203. .thaw = scsi_bus_thaw,
  204. .poweroff = scsi_bus_poweroff,
  205. .restore = scsi_bus_restore,
  206. .runtime_suspend = scsi_runtime_suspend,
  207. .runtime_resume = scsi_runtime_resume,
  208. .runtime_idle = scsi_runtime_idle,
  209. };