scsi_pm.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. /*
  116. * Forcibly set runtime PM status of request queue to "active" to
  117. * make sure we can again get requests from the queue (see also
  118. * blk_pm_peek_request()).
  119. *
  120. * The resume hook will correct runtime PM status of the disk.
  121. */
  122. if (scsi_is_sdev_device(dev) && pm_runtime_suspended(dev))
  123. blk_set_runtime_active(to_scsi_device(dev)->request_queue);
  124. if (fn) {
  125. async_schedule_domain(fn, dev, &scsi_sd_pm_domain);
  126. /*
  127. * If a user has disabled async probing a likely reason
  128. * is due to a storage enclosure that does not inject
  129. * staggered spin-ups. For safety, make resume
  130. * synchronous as well in that case.
  131. */
  132. if (strncmp(scsi_scan_type, "async", 5) != 0)
  133. async_synchronize_full_domain(&scsi_sd_pm_domain);
  134. } else {
  135. pm_runtime_disable(dev);
  136. pm_runtime_set_active(dev);
  137. pm_runtime_enable(dev);
  138. }
  139. return 0;
  140. }
  141. static int scsi_bus_prepare(struct device *dev)
  142. {
  143. if (scsi_is_sdev_device(dev)) {
  144. /* sd probing uses async_schedule. Wait until it finishes. */
  145. async_synchronize_full_domain(&scsi_sd_probe_domain);
  146. } else if (scsi_is_host_device(dev)) {
  147. /* Wait until async scanning is finished */
  148. scsi_complete_async_scans();
  149. }
  150. return 0;
  151. }
  152. static int scsi_bus_suspend(struct device *dev)
  153. {
  154. return scsi_bus_suspend_common(dev, do_scsi_suspend);
  155. }
  156. static int scsi_bus_resume(struct device *dev)
  157. {
  158. return scsi_bus_resume_common(dev, do_scsi_resume);
  159. }
  160. static int scsi_bus_freeze(struct device *dev)
  161. {
  162. return scsi_bus_suspend_common(dev, do_scsi_freeze);
  163. }
  164. static int scsi_bus_thaw(struct device *dev)
  165. {
  166. return scsi_bus_resume_common(dev, do_scsi_thaw);
  167. }
  168. static int scsi_bus_poweroff(struct device *dev)
  169. {
  170. return scsi_bus_suspend_common(dev, do_scsi_poweroff);
  171. }
  172. static int scsi_bus_restore(struct device *dev)
  173. {
  174. return scsi_bus_resume_common(dev, do_scsi_restore);
  175. }
  176. #else /* CONFIG_PM_SLEEP */
  177. #define scsi_bus_prepare NULL
  178. #define scsi_bus_suspend NULL
  179. #define scsi_bus_resume NULL
  180. #define scsi_bus_freeze NULL
  181. #define scsi_bus_thaw NULL
  182. #define scsi_bus_poweroff NULL
  183. #define scsi_bus_restore NULL
  184. #endif /* CONFIG_PM_SLEEP */
  185. static int sdev_runtime_suspend(struct device *dev)
  186. {
  187. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  188. struct scsi_device *sdev = to_scsi_device(dev);
  189. int err = 0;
  190. err = blk_pre_runtime_suspend(sdev->request_queue);
  191. if (err)
  192. return err;
  193. if (pm && pm->runtime_suspend)
  194. err = pm->runtime_suspend(dev);
  195. blk_post_runtime_suspend(sdev->request_queue, err);
  196. return err;
  197. }
  198. static int scsi_runtime_suspend(struct device *dev)
  199. {
  200. int err = 0;
  201. dev_dbg(dev, "scsi_runtime_suspend\n");
  202. if (scsi_is_sdev_device(dev))
  203. err = sdev_runtime_suspend(dev);
  204. /* Insert hooks here for targets, hosts, and transport classes */
  205. return err;
  206. }
  207. static int sdev_runtime_resume(struct device *dev)
  208. {
  209. struct scsi_device *sdev = to_scsi_device(dev);
  210. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  211. int err = 0;
  212. blk_pre_runtime_resume(sdev->request_queue);
  213. if (pm && pm->runtime_resume)
  214. err = pm->runtime_resume(dev);
  215. blk_post_runtime_resume(sdev->request_queue, err);
  216. return err;
  217. }
  218. static int scsi_runtime_resume(struct device *dev)
  219. {
  220. int err = 0;
  221. dev_dbg(dev, "scsi_runtime_resume\n");
  222. if (scsi_is_sdev_device(dev))
  223. err = sdev_runtime_resume(dev);
  224. /* Insert hooks here for targets, hosts, and transport classes */
  225. return err;
  226. }
  227. static int scsi_runtime_idle(struct device *dev)
  228. {
  229. dev_dbg(dev, "scsi_runtime_idle\n");
  230. /* Insert hooks here for targets, hosts, and transport classes */
  231. if (scsi_is_sdev_device(dev)) {
  232. pm_runtime_mark_last_busy(dev);
  233. pm_runtime_autosuspend(dev);
  234. return -EBUSY;
  235. }
  236. return 0;
  237. }
  238. int scsi_autopm_get_device(struct scsi_device *sdev)
  239. {
  240. int err;
  241. err = pm_runtime_get_sync(&sdev->sdev_gendev);
  242. if (err < 0 && err !=-EACCES)
  243. pm_runtime_put_sync(&sdev->sdev_gendev);
  244. else
  245. err = 0;
  246. return err;
  247. }
  248. EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
  249. void scsi_autopm_put_device(struct scsi_device *sdev)
  250. {
  251. pm_runtime_put_sync(&sdev->sdev_gendev);
  252. }
  253. EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
  254. void scsi_autopm_get_target(struct scsi_target *starget)
  255. {
  256. pm_runtime_get_sync(&starget->dev);
  257. }
  258. void scsi_autopm_put_target(struct scsi_target *starget)
  259. {
  260. pm_runtime_put_sync(&starget->dev);
  261. }
  262. int scsi_autopm_get_host(struct Scsi_Host *shost)
  263. {
  264. int err;
  265. err = pm_runtime_get_sync(&shost->shost_gendev);
  266. if (err < 0 && err !=-EACCES)
  267. pm_runtime_put_sync(&shost->shost_gendev);
  268. else
  269. err = 0;
  270. return err;
  271. }
  272. void scsi_autopm_put_host(struct Scsi_Host *shost)
  273. {
  274. pm_runtime_put_sync(&shost->shost_gendev);
  275. }
  276. const struct dev_pm_ops scsi_bus_pm_ops = {
  277. .prepare = scsi_bus_prepare,
  278. .suspend = scsi_bus_suspend,
  279. .resume = scsi_bus_resume,
  280. .freeze = scsi_bus_freeze,
  281. .thaw = scsi_bus_thaw,
  282. .poweroff = scsi_bus_poweroff,
  283. .restore = scsi_bus_restore,
  284. .runtime_suspend = scsi_runtime_suspend,
  285. .runtime_resume = scsi_runtime_resume,
  286. .runtime_idle = scsi_runtime_idle,
  287. };