scsi_pm.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 sdev_blk_runtime_suspend(struct scsi_device *sdev,
  120. int (*cb)(struct device *))
  121. {
  122. int err;
  123. err = blk_pre_runtime_suspend(sdev->request_queue);
  124. if (err)
  125. return err;
  126. if (cb)
  127. err = cb(&sdev->sdev_gendev);
  128. blk_post_runtime_suspend(sdev->request_queue, err);
  129. return err;
  130. }
  131. static int sdev_runtime_suspend(struct device *dev)
  132. {
  133. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  134. int (*cb)(struct device *) = pm ? pm->runtime_suspend : NULL;
  135. struct scsi_device *sdev = to_scsi_device(dev);
  136. int err;
  137. if (sdev->request_queue->dev)
  138. return sdev_blk_runtime_suspend(sdev, cb);
  139. err = scsi_dev_type_suspend(dev, cb);
  140. if (err == -EAGAIN)
  141. pm_schedule_suspend(dev, jiffies_to_msecs(
  142. round_jiffies_up_relative(HZ/10)));
  143. return err;
  144. }
  145. static int scsi_runtime_suspend(struct device *dev)
  146. {
  147. int err = 0;
  148. dev_dbg(dev, "scsi_runtime_suspend\n");
  149. if (scsi_is_sdev_device(dev))
  150. err = sdev_runtime_suspend(dev);
  151. /* Insert hooks here for targets, hosts, and transport classes */
  152. return err;
  153. }
  154. static int sdev_blk_runtime_resume(struct scsi_device *sdev,
  155. int (*cb)(struct device *))
  156. {
  157. int err = 0;
  158. blk_pre_runtime_resume(sdev->request_queue);
  159. if (cb)
  160. err = cb(&sdev->sdev_gendev);
  161. blk_post_runtime_resume(sdev->request_queue, err);
  162. return err;
  163. }
  164. static int sdev_runtime_resume(struct device *dev)
  165. {
  166. struct scsi_device *sdev = to_scsi_device(dev);
  167. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  168. int (*cb)(struct device *) = pm ? pm->runtime_resume : NULL;
  169. if (sdev->request_queue->dev)
  170. return sdev_blk_runtime_resume(sdev, cb);
  171. else
  172. return scsi_dev_type_resume(dev, cb);
  173. }
  174. static int scsi_runtime_resume(struct device *dev)
  175. {
  176. int err = 0;
  177. dev_dbg(dev, "scsi_runtime_resume\n");
  178. if (scsi_is_sdev_device(dev))
  179. err = sdev_runtime_resume(dev);
  180. /* Insert hooks here for targets, hosts, and transport classes */
  181. return err;
  182. }
  183. static int scsi_runtime_idle(struct device *dev)
  184. {
  185. dev_dbg(dev, "scsi_runtime_idle\n");
  186. /* Insert hooks here for targets, hosts, and transport classes */
  187. if (scsi_is_sdev_device(dev)) {
  188. struct scsi_device *sdev = to_scsi_device(dev);
  189. if (sdev->request_queue->dev) {
  190. pm_runtime_mark_last_busy(dev);
  191. pm_runtime_autosuspend(dev);
  192. return -EBUSY;
  193. }
  194. }
  195. return 0;
  196. }
  197. int scsi_autopm_get_device(struct scsi_device *sdev)
  198. {
  199. int err;
  200. err = pm_runtime_get_sync(&sdev->sdev_gendev);
  201. if (err < 0 && err !=-EACCES)
  202. pm_runtime_put_sync(&sdev->sdev_gendev);
  203. else
  204. err = 0;
  205. return err;
  206. }
  207. EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
  208. void scsi_autopm_put_device(struct scsi_device *sdev)
  209. {
  210. pm_runtime_put_sync(&sdev->sdev_gendev);
  211. }
  212. EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
  213. void scsi_autopm_get_target(struct scsi_target *starget)
  214. {
  215. pm_runtime_get_sync(&starget->dev);
  216. }
  217. void scsi_autopm_put_target(struct scsi_target *starget)
  218. {
  219. pm_runtime_put_sync(&starget->dev);
  220. }
  221. int scsi_autopm_get_host(struct Scsi_Host *shost)
  222. {
  223. int err;
  224. err = pm_runtime_get_sync(&shost->shost_gendev);
  225. if (err < 0 && err !=-EACCES)
  226. pm_runtime_put_sync(&shost->shost_gendev);
  227. else
  228. err = 0;
  229. return err;
  230. }
  231. void scsi_autopm_put_host(struct Scsi_Host *shost)
  232. {
  233. pm_runtime_put_sync(&shost->shost_gendev);
  234. }
  235. #else
  236. #define scsi_runtime_suspend NULL
  237. #define scsi_runtime_resume NULL
  238. #define scsi_runtime_idle NULL
  239. #endif /* CONFIG_PM_RUNTIME */
  240. const struct dev_pm_ops scsi_bus_pm_ops = {
  241. .prepare = scsi_bus_prepare,
  242. .suspend = scsi_bus_suspend,
  243. .resume = scsi_bus_resume,
  244. .freeze = scsi_bus_freeze,
  245. .thaw = scsi_bus_thaw,
  246. .poweroff = scsi_bus_poweroff,
  247. .restore = scsi_bus_restore,
  248. .runtime_suspend = scsi_runtime_suspend,
  249. .runtime_resume = scsi_runtime_resume,
  250. .runtime_idle = scsi_runtime_idle,
  251. };