scsi_pm.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. #ifdef CONFIG_PM_SLEEP
  16. static int do_scsi_suspend(struct device *dev, const struct dev_pm_ops *pm)
  17. {
  18. return pm && pm->suspend ? pm->suspend(dev) : 0;
  19. }
  20. static int do_scsi_freeze(struct device *dev, const struct dev_pm_ops *pm)
  21. {
  22. return pm && pm->freeze ? pm->freeze(dev) : 0;
  23. }
  24. static int do_scsi_poweroff(struct device *dev, const struct dev_pm_ops *pm)
  25. {
  26. return pm && pm->poweroff ? pm->poweroff(dev) : 0;
  27. }
  28. static int do_scsi_resume(struct device *dev, const struct dev_pm_ops *pm)
  29. {
  30. return pm && pm->resume ? pm->resume(dev) : 0;
  31. }
  32. static int do_scsi_thaw(struct device *dev, const struct dev_pm_ops *pm)
  33. {
  34. return pm && pm->thaw ? pm->thaw(dev) : 0;
  35. }
  36. static int do_scsi_restore(struct device *dev, const struct dev_pm_ops *pm)
  37. {
  38. return pm && pm->restore ? pm->restore(dev) : 0;
  39. }
  40. static int scsi_dev_type_suspend(struct device *dev,
  41. int (*cb)(struct device *, const struct dev_pm_ops *))
  42. {
  43. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  44. int err;
  45. /* flush pending in-flight resume operations, suspend is synchronous */
  46. async_synchronize_full_domain(&scsi_sd_pm_domain);
  47. err = scsi_device_quiesce(to_scsi_device(dev));
  48. if (err == 0) {
  49. err = cb(dev, pm);
  50. if (err)
  51. scsi_device_resume(to_scsi_device(dev));
  52. }
  53. dev_dbg(dev, "scsi suspend: %d\n", err);
  54. return err;
  55. }
  56. static int scsi_dev_type_resume(struct device *dev,
  57. int (*cb)(struct device *, const struct dev_pm_ops *))
  58. {
  59. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  60. int err = 0;
  61. err = cb(dev, pm);
  62. scsi_device_resume(to_scsi_device(dev));
  63. dev_dbg(dev, "scsi resume: %d\n", err);
  64. if (err == 0) {
  65. pm_runtime_disable(dev);
  66. pm_runtime_set_active(dev);
  67. pm_runtime_enable(dev);
  68. }
  69. return err;
  70. }
  71. static int
  72. scsi_bus_suspend_common(struct device *dev,
  73. int (*cb)(struct device *, const struct dev_pm_ops *))
  74. {
  75. int err = 0;
  76. if (scsi_is_sdev_device(dev)) {
  77. /*
  78. * All the high-level SCSI drivers that implement runtime
  79. * PM treat runtime suspend, system suspend, and system
  80. * hibernate nearly identically. In all cases the requirements
  81. * for runtime suspension are stricter.
  82. */
  83. if (pm_runtime_suspended(dev))
  84. return 0;
  85. err = scsi_dev_type_suspend(dev, cb);
  86. }
  87. return err;
  88. }
  89. static void async_sdev_resume(void *dev, async_cookie_t cookie)
  90. {
  91. scsi_dev_type_resume(dev, do_scsi_resume);
  92. }
  93. static void async_sdev_thaw(void *dev, async_cookie_t cookie)
  94. {
  95. scsi_dev_type_resume(dev, do_scsi_thaw);
  96. }
  97. static void async_sdev_restore(void *dev, async_cookie_t cookie)
  98. {
  99. scsi_dev_type_resume(dev, do_scsi_restore);
  100. }
  101. static int scsi_bus_resume_common(struct device *dev,
  102. int (*cb)(struct device *, const struct dev_pm_ops *))
  103. {
  104. async_func_t fn;
  105. if (!scsi_is_sdev_device(dev))
  106. fn = NULL;
  107. else if (cb == do_scsi_resume)
  108. fn = async_sdev_resume;
  109. else if (cb == do_scsi_thaw)
  110. fn = async_sdev_thaw;
  111. else if (cb == do_scsi_restore)
  112. fn = async_sdev_restore;
  113. else
  114. fn = NULL;
  115. if (fn) {
  116. async_schedule_domain(fn, dev, &scsi_sd_pm_domain);
  117. /*
  118. * If a user has disabled async probing a likely reason
  119. * is due to a storage enclosure that does not inject
  120. * staggered spin-ups. For safety, make resume
  121. * synchronous as well in that case.
  122. */
  123. if (strncmp(scsi_scan_type, "async", 5) != 0)
  124. async_synchronize_full_domain(&scsi_sd_pm_domain);
  125. } else {
  126. pm_runtime_disable(dev);
  127. pm_runtime_set_active(dev);
  128. pm_runtime_enable(dev);
  129. }
  130. return 0;
  131. }
  132. static int scsi_bus_prepare(struct device *dev)
  133. {
  134. if (scsi_is_sdev_device(dev)) {
  135. /* sd probing uses async_schedule. Wait until it finishes. */
  136. async_synchronize_full_domain(&scsi_sd_probe_domain);
  137. } else if (scsi_is_host_device(dev)) {
  138. /* Wait until async scanning is finished */
  139. scsi_complete_async_scans();
  140. }
  141. return 0;
  142. }
  143. static int scsi_bus_suspend(struct device *dev)
  144. {
  145. return scsi_bus_suspend_common(dev, do_scsi_suspend);
  146. }
  147. static int scsi_bus_resume(struct device *dev)
  148. {
  149. return scsi_bus_resume_common(dev, do_scsi_resume);
  150. }
  151. static int scsi_bus_freeze(struct device *dev)
  152. {
  153. return scsi_bus_suspend_common(dev, do_scsi_freeze);
  154. }
  155. static int scsi_bus_thaw(struct device *dev)
  156. {
  157. return scsi_bus_resume_common(dev, do_scsi_thaw);
  158. }
  159. static int scsi_bus_poweroff(struct device *dev)
  160. {
  161. return scsi_bus_suspend_common(dev, do_scsi_poweroff);
  162. }
  163. static int scsi_bus_restore(struct device *dev)
  164. {
  165. return scsi_bus_resume_common(dev, do_scsi_restore);
  166. }
  167. #else /* CONFIG_PM_SLEEP */
  168. #define scsi_bus_prepare NULL
  169. #define scsi_bus_suspend NULL
  170. #define scsi_bus_resume NULL
  171. #define scsi_bus_freeze NULL
  172. #define scsi_bus_thaw NULL
  173. #define scsi_bus_poweroff NULL
  174. #define scsi_bus_restore NULL
  175. #endif /* CONFIG_PM_SLEEP */
  176. static int sdev_runtime_suspend(struct device *dev)
  177. {
  178. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  179. struct scsi_device *sdev = to_scsi_device(dev);
  180. int err = 0;
  181. err = blk_pre_runtime_suspend(sdev->request_queue);
  182. if (err)
  183. return err;
  184. if (pm && pm->runtime_suspend)
  185. err = pm->runtime_suspend(dev);
  186. blk_post_runtime_suspend(sdev->request_queue, err);
  187. return err;
  188. }
  189. static int scsi_runtime_suspend(struct device *dev)
  190. {
  191. int err = 0;
  192. dev_dbg(dev, "scsi_runtime_suspend\n");
  193. if (scsi_is_sdev_device(dev))
  194. err = sdev_runtime_suspend(dev);
  195. /* Insert hooks here for targets, hosts, and transport classes */
  196. return err;
  197. }
  198. static int sdev_runtime_resume(struct device *dev)
  199. {
  200. struct scsi_device *sdev = to_scsi_device(dev);
  201. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  202. int err = 0;
  203. blk_pre_runtime_resume(sdev->request_queue);
  204. if (pm && pm->runtime_resume)
  205. err = pm->runtime_resume(dev);
  206. blk_post_runtime_resume(sdev->request_queue, err);
  207. return err;
  208. }
  209. static int scsi_runtime_resume(struct device *dev)
  210. {
  211. int err = 0;
  212. dev_dbg(dev, "scsi_runtime_resume\n");
  213. if (scsi_is_sdev_device(dev))
  214. err = sdev_runtime_resume(dev);
  215. /* Insert hooks here for targets, hosts, and transport classes */
  216. return err;
  217. }
  218. static int scsi_runtime_idle(struct device *dev)
  219. {
  220. dev_dbg(dev, "scsi_runtime_idle\n");
  221. /* Insert hooks here for targets, hosts, and transport classes */
  222. if (scsi_is_sdev_device(dev)) {
  223. pm_runtime_mark_last_busy(dev);
  224. pm_runtime_autosuspend(dev);
  225. return -EBUSY;
  226. }
  227. return 0;
  228. }
  229. int scsi_autopm_get_device(struct scsi_device *sdev)
  230. {
  231. int err;
  232. err = pm_runtime_get_sync(&sdev->sdev_gendev);
  233. if (err < 0 && err !=-EACCES)
  234. pm_runtime_put_sync(&sdev->sdev_gendev);
  235. else
  236. err = 0;
  237. return err;
  238. }
  239. EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
  240. void scsi_autopm_put_device(struct scsi_device *sdev)
  241. {
  242. pm_runtime_put_sync(&sdev->sdev_gendev);
  243. }
  244. EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
  245. void scsi_autopm_get_target(struct scsi_target *starget)
  246. {
  247. pm_runtime_get_sync(&starget->dev);
  248. }
  249. void scsi_autopm_put_target(struct scsi_target *starget)
  250. {
  251. pm_runtime_put_sync(&starget->dev);
  252. }
  253. int scsi_autopm_get_host(struct Scsi_Host *shost)
  254. {
  255. int err;
  256. err = pm_runtime_get_sync(&shost->shost_gendev);
  257. if (err < 0 && err !=-EACCES)
  258. pm_runtime_put_sync(&shost->shost_gendev);
  259. else
  260. err = 0;
  261. return err;
  262. }
  263. void scsi_autopm_put_host(struct Scsi_Host *shost)
  264. {
  265. pm_runtime_put_sync(&shost->shost_gendev);
  266. }
  267. const struct dev_pm_ops scsi_bus_pm_ops = {
  268. .prepare = scsi_bus_prepare,
  269. .suspend = scsi_bus_suspend,
  270. .resume = scsi_bus_resume,
  271. .freeze = scsi_bus_freeze,
  272. .thaw = scsi_bus_thaw,
  273. .poweroff = scsi_bus_poweroff,
  274. .restore = scsi_bus_restore,
  275. .runtime_suspend = scsi_runtime_suspend,
  276. .runtime_resume = scsi_runtime_resume,
  277. .runtime_idle = scsi_runtime_idle,
  278. };