scsi_pm.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. #ifdef CONFIG_PM_RUNTIME
  177. static int sdev_runtime_suspend(struct device *dev)
  178. {
  179. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  180. struct scsi_device *sdev = to_scsi_device(dev);
  181. int err;
  182. err = blk_pre_runtime_suspend(sdev->request_queue);
  183. if (err)
  184. return err;
  185. if (pm && pm->runtime_suspend)
  186. err = pm->runtime_suspend(dev);
  187. blk_post_runtime_suspend(sdev->request_queue, err);
  188. return err;
  189. }
  190. static int scsi_runtime_suspend(struct device *dev)
  191. {
  192. int err = 0;
  193. dev_dbg(dev, "scsi_runtime_suspend\n");
  194. if (scsi_is_sdev_device(dev))
  195. err = sdev_runtime_suspend(dev);
  196. /* Insert hooks here for targets, hosts, and transport classes */
  197. return err;
  198. }
  199. static int sdev_runtime_resume(struct device *dev)
  200. {
  201. struct scsi_device *sdev = to_scsi_device(dev);
  202. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  203. int err = 0;
  204. blk_pre_runtime_resume(sdev->request_queue);
  205. if (pm && pm->runtime_resume)
  206. err = pm->runtime_resume(dev);
  207. blk_post_runtime_resume(sdev->request_queue, err);
  208. return err;
  209. }
  210. static int scsi_runtime_resume(struct device *dev)
  211. {
  212. int err = 0;
  213. dev_dbg(dev, "scsi_runtime_resume\n");
  214. if (scsi_is_sdev_device(dev))
  215. err = sdev_runtime_resume(dev);
  216. /* Insert hooks here for targets, hosts, and transport classes */
  217. return err;
  218. }
  219. static int scsi_runtime_idle(struct device *dev)
  220. {
  221. dev_dbg(dev, "scsi_runtime_idle\n");
  222. /* Insert hooks here for targets, hosts, and transport classes */
  223. if (scsi_is_sdev_device(dev)) {
  224. pm_runtime_mark_last_busy(dev);
  225. pm_runtime_autosuspend(dev);
  226. return -EBUSY;
  227. }
  228. return 0;
  229. }
  230. int scsi_autopm_get_device(struct scsi_device *sdev)
  231. {
  232. int err;
  233. err = pm_runtime_get_sync(&sdev->sdev_gendev);
  234. if (err < 0 && err !=-EACCES)
  235. pm_runtime_put_sync(&sdev->sdev_gendev);
  236. else
  237. err = 0;
  238. return err;
  239. }
  240. EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
  241. void scsi_autopm_put_device(struct scsi_device *sdev)
  242. {
  243. pm_runtime_put_sync(&sdev->sdev_gendev);
  244. }
  245. EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
  246. void scsi_autopm_get_target(struct scsi_target *starget)
  247. {
  248. pm_runtime_get_sync(&starget->dev);
  249. }
  250. void scsi_autopm_put_target(struct scsi_target *starget)
  251. {
  252. pm_runtime_put_sync(&starget->dev);
  253. }
  254. int scsi_autopm_get_host(struct Scsi_Host *shost)
  255. {
  256. int err;
  257. err = pm_runtime_get_sync(&shost->shost_gendev);
  258. if (err < 0 && err !=-EACCES)
  259. pm_runtime_put_sync(&shost->shost_gendev);
  260. else
  261. err = 0;
  262. return err;
  263. }
  264. void scsi_autopm_put_host(struct Scsi_Host *shost)
  265. {
  266. pm_runtime_put_sync(&shost->shost_gendev);
  267. }
  268. #else
  269. #define scsi_runtime_suspend NULL
  270. #define scsi_runtime_resume NULL
  271. #define scsi_runtime_idle NULL
  272. #endif /* CONFIG_PM_RUNTIME */
  273. const struct dev_pm_ops scsi_bus_pm_ops = {
  274. .prepare = scsi_bus_prepare,
  275. .suspend = scsi_bus_suspend,
  276. .resume = scsi_bus_resume,
  277. .freeze = scsi_bus_freeze,
  278. .thaw = scsi_bus_thaw,
  279. .poweroff = scsi_bus_poweroff,
  280. .restore = scsi_bus_restore,
  281. .runtime_suspend = scsi_runtime_suspend,
  282. .runtime_resume = scsi_runtime_resume,
  283. .runtime_idle = scsi_runtime_idle,
  284. };