scsi_pm.c 8.2 KB

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