scsi_pm.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. /*
  61. * Parent device may have runtime suspended as soon as
  62. * it is woken up during the system resume.
  63. *
  64. * Resume it on behalf of child.
  65. */
  66. pm_runtime_get_sync(dev->parent);
  67. err = scsi_dev_type_resume(dev, cb);
  68. pm_runtime_put_sync(dev->parent);
  69. }
  70. if (err == 0) {
  71. pm_runtime_disable(dev);
  72. pm_runtime_set_active(dev);
  73. pm_runtime_enable(dev);
  74. }
  75. return err;
  76. }
  77. static int scsi_bus_prepare(struct device *dev)
  78. {
  79. if (scsi_is_sdev_device(dev)) {
  80. /* sd probing uses async_schedule. Wait until it finishes. */
  81. async_synchronize_full();
  82. } else if (scsi_is_host_device(dev)) {
  83. /* Wait until async scanning is finished */
  84. scsi_complete_async_scans();
  85. }
  86. return 0;
  87. }
  88. static int scsi_bus_suspend(struct device *dev)
  89. {
  90. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  91. return scsi_bus_suspend_common(dev, pm ? pm->suspend : NULL);
  92. }
  93. static int scsi_bus_resume(struct device *dev)
  94. {
  95. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  96. return scsi_bus_resume_common(dev, pm ? pm->resume : NULL);
  97. }
  98. static int scsi_bus_freeze(struct device *dev)
  99. {
  100. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  101. return scsi_bus_suspend_common(dev, pm ? pm->freeze : NULL);
  102. }
  103. static int scsi_bus_thaw(struct device *dev)
  104. {
  105. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  106. return scsi_bus_resume_common(dev, pm ? pm->thaw : NULL);
  107. }
  108. static int scsi_bus_poweroff(struct device *dev)
  109. {
  110. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  111. return scsi_bus_suspend_common(dev, pm ? pm->poweroff : NULL);
  112. }
  113. static int scsi_bus_restore(struct device *dev)
  114. {
  115. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  116. return scsi_bus_resume_common(dev, pm ? pm->restore : NULL);
  117. }
  118. #else /* CONFIG_PM_SLEEP */
  119. #define scsi_bus_prepare NULL
  120. #define scsi_bus_suspend NULL
  121. #define scsi_bus_resume NULL
  122. #define scsi_bus_freeze NULL
  123. #define scsi_bus_thaw NULL
  124. #define scsi_bus_poweroff NULL
  125. #define scsi_bus_restore NULL
  126. #endif /* CONFIG_PM_SLEEP */
  127. #ifdef CONFIG_PM_RUNTIME
  128. static int sdev_blk_runtime_suspend(struct scsi_device *sdev,
  129. int (*cb)(struct device *))
  130. {
  131. int err;
  132. err = blk_pre_runtime_suspend(sdev->request_queue);
  133. if (err)
  134. return err;
  135. if (cb)
  136. err = cb(&sdev->sdev_gendev);
  137. blk_post_runtime_suspend(sdev->request_queue, err);
  138. return err;
  139. }
  140. static int sdev_runtime_suspend(struct device *dev)
  141. {
  142. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  143. int (*cb)(struct device *) = pm ? pm->runtime_suspend : NULL;
  144. struct scsi_device *sdev = to_scsi_device(dev);
  145. int err;
  146. if (sdev->request_queue->dev)
  147. return sdev_blk_runtime_suspend(sdev, cb);
  148. err = scsi_dev_type_suspend(dev, cb);
  149. if (err == -EAGAIN)
  150. pm_schedule_suspend(dev, jiffies_to_msecs(
  151. round_jiffies_up_relative(HZ/10)));
  152. return err;
  153. }
  154. static int scsi_runtime_suspend(struct device *dev)
  155. {
  156. int err = 0;
  157. dev_dbg(dev, "scsi_runtime_suspend\n");
  158. if (scsi_is_sdev_device(dev))
  159. err = sdev_runtime_suspend(dev);
  160. /* Insert hooks here for targets, hosts, and transport classes */
  161. return err;
  162. }
  163. static int sdev_blk_runtime_resume(struct scsi_device *sdev,
  164. int (*cb)(struct device *))
  165. {
  166. int err = 0;
  167. blk_pre_runtime_resume(sdev->request_queue);
  168. if (cb)
  169. err = cb(&sdev->sdev_gendev);
  170. blk_post_runtime_resume(sdev->request_queue, err);
  171. return err;
  172. }
  173. static int sdev_runtime_resume(struct device *dev)
  174. {
  175. struct scsi_device *sdev = to_scsi_device(dev);
  176. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  177. int (*cb)(struct device *) = pm ? pm->runtime_resume : NULL;
  178. if (sdev->request_queue->dev)
  179. return sdev_blk_runtime_resume(sdev, cb);
  180. else
  181. return scsi_dev_type_resume(dev, cb);
  182. }
  183. static int scsi_runtime_resume(struct device *dev)
  184. {
  185. int err = 0;
  186. dev_dbg(dev, "scsi_runtime_resume\n");
  187. if (scsi_is_sdev_device(dev))
  188. err = sdev_runtime_resume(dev);
  189. /* Insert hooks here for targets, hosts, and transport classes */
  190. return err;
  191. }
  192. static int scsi_runtime_idle(struct device *dev)
  193. {
  194. int err;
  195. dev_dbg(dev, "scsi_runtime_idle\n");
  196. /* Insert hooks here for targets, hosts, and transport classes */
  197. if (scsi_is_sdev_device(dev)) {
  198. struct scsi_device *sdev = to_scsi_device(dev);
  199. if (sdev->request_queue->dev) {
  200. pm_runtime_mark_last_busy(dev);
  201. err = pm_runtime_autosuspend(dev);
  202. } else {
  203. err = pm_runtime_suspend(dev);
  204. }
  205. } else {
  206. err = pm_runtime_suspend(dev);
  207. }
  208. return err;
  209. }
  210. int scsi_autopm_get_device(struct scsi_device *sdev)
  211. {
  212. int err;
  213. err = pm_runtime_get_sync(&sdev->sdev_gendev);
  214. if (err < 0 && err !=-EACCES)
  215. pm_runtime_put_sync(&sdev->sdev_gendev);
  216. else
  217. err = 0;
  218. return err;
  219. }
  220. EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
  221. void scsi_autopm_put_device(struct scsi_device *sdev)
  222. {
  223. pm_runtime_put_sync(&sdev->sdev_gendev);
  224. }
  225. EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
  226. void scsi_autopm_get_target(struct scsi_target *starget)
  227. {
  228. pm_runtime_get_sync(&starget->dev);
  229. }
  230. void scsi_autopm_put_target(struct scsi_target *starget)
  231. {
  232. pm_runtime_put_sync(&starget->dev);
  233. }
  234. int scsi_autopm_get_host(struct Scsi_Host *shost)
  235. {
  236. int err;
  237. err = pm_runtime_get_sync(&shost->shost_gendev);
  238. if (err < 0 && err !=-EACCES)
  239. pm_runtime_put_sync(&shost->shost_gendev);
  240. else
  241. err = 0;
  242. return err;
  243. }
  244. void scsi_autopm_put_host(struct Scsi_Host *shost)
  245. {
  246. pm_runtime_put_sync(&shost->shost_gendev);
  247. }
  248. #else
  249. #define scsi_runtime_suspend NULL
  250. #define scsi_runtime_resume NULL
  251. #define scsi_runtime_idle NULL
  252. #endif /* CONFIG_PM_RUNTIME */
  253. const struct dev_pm_ops scsi_bus_pm_ops = {
  254. .prepare = scsi_bus_prepare,
  255. .suspend = scsi_bus_suspend,
  256. .resume = scsi_bus_resume,
  257. .freeze = scsi_bus_freeze,
  258. .thaw = scsi_bus_thaw,
  259. .poweroff = scsi_bus_poweroff,
  260. .restore = scsi_bus_restore,
  261. .runtime_suspend = scsi_runtime_suspend,
  262. .runtime_resume = scsi_runtime_resume,
  263. .runtime_idle = scsi_runtime_idle,
  264. };