scsi_pm.c 8.0 KB

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