scsi_pm.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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/blk-pm.h>
  11. #include <scsi/scsi.h>
  12. #include <scsi/scsi_device.h>
  13. #include <scsi/scsi_driver.h>
  14. #include <scsi/scsi_host.h>
  15. #include "scsi_priv.h"
  16. #ifdef CONFIG_PM_SLEEP
  17. static int do_scsi_suspend(struct device *dev, const struct dev_pm_ops *pm)
  18. {
  19. return pm && pm->suspend ? pm->suspend(dev) : 0;
  20. }
  21. static int do_scsi_freeze(struct device *dev, const struct dev_pm_ops *pm)
  22. {
  23. return pm && pm->freeze ? pm->freeze(dev) : 0;
  24. }
  25. static int do_scsi_poweroff(struct device *dev, const struct dev_pm_ops *pm)
  26. {
  27. return pm && pm->poweroff ? pm->poweroff(dev) : 0;
  28. }
  29. static int do_scsi_resume(struct device *dev, const struct dev_pm_ops *pm)
  30. {
  31. return pm && pm->resume ? pm->resume(dev) : 0;
  32. }
  33. static int do_scsi_thaw(struct device *dev, const struct dev_pm_ops *pm)
  34. {
  35. return pm && pm->thaw ? pm->thaw(dev) : 0;
  36. }
  37. static int do_scsi_restore(struct device *dev, const struct dev_pm_ops *pm)
  38. {
  39. return pm && pm->restore ? pm->restore(dev) : 0;
  40. }
  41. static int scsi_dev_type_suspend(struct device *dev,
  42. int (*cb)(struct device *, const struct dev_pm_ops *))
  43. {
  44. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  45. int err;
  46. err = scsi_device_quiesce(to_scsi_device(dev));
  47. if (err == 0) {
  48. err = cb(dev, pm);
  49. if (err)
  50. scsi_device_resume(to_scsi_device(dev));
  51. }
  52. dev_dbg(dev, "scsi suspend: %d\n", err);
  53. return err;
  54. }
  55. static int
  56. scsi_bus_suspend_common(struct device *dev,
  57. int (*cb)(struct device *, const struct dev_pm_ops *))
  58. {
  59. if (!scsi_is_sdev_device(dev))
  60. return 0;
  61. return scsi_dev_type_suspend(dev, cb);
  62. }
  63. static int scsi_bus_resume_common(struct device *dev,
  64. int (*cb)(struct device *, const struct dev_pm_ops *))
  65. {
  66. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  67. int err;
  68. if (!scsi_is_sdev_device(dev))
  69. return 0;
  70. err = cb(dev, pm);
  71. scsi_device_resume(to_scsi_device(dev));
  72. dev_dbg(dev, "scsi resume: %d\n", err);
  73. return err;
  74. }
  75. static int scsi_bus_prepare(struct device *dev)
  76. {
  77. if (scsi_is_host_device(dev)) {
  78. /* Wait until async scanning is finished */
  79. scsi_complete_async_scans();
  80. }
  81. return 0;
  82. }
  83. static int scsi_bus_suspend(struct device *dev)
  84. {
  85. return scsi_bus_suspend_common(dev, do_scsi_suspend);
  86. }
  87. static int scsi_bus_resume(struct device *dev)
  88. {
  89. return scsi_bus_resume_common(dev, do_scsi_resume);
  90. }
  91. static int scsi_bus_freeze(struct device *dev)
  92. {
  93. return scsi_bus_suspend_common(dev, do_scsi_freeze);
  94. }
  95. static int scsi_bus_thaw(struct device *dev)
  96. {
  97. return scsi_bus_resume_common(dev, do_scsi_thaw);
  98. }
  99. static int scsi_bus_poweroff(struct device *dev)
  100. {
  101. return scsi_bus_suspend_common(dev, do_scsi_poweroff);
  102. }
  103. static int scsi_bus_restore(struct device *dev)
  104. {
  105. return scsi_bus_resume_common(dev, do_scsi_restore);
  106. }
  107. #else /* CONFIG_PM_SLEEP */
  108. #define scsi_bus_prepare NULL
  109. #define scsi_bus_suspend NULL
  110. #define scsi_bus_resume NULL
  111. #define scsi_bus_freeze NULL
  112. #define scsi_bus_thaw NULL
  113. #define scsi_bus_poweroff NULL
  114. #define scsi_bus_restore NULL
  115. #endif /* CONFIG_PM_SLEEP */
  116. static int sdev_runtime_suspend(struct device *dev)
  117. {
  118. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  119. struct scsi_device *sdev = to_scsi_device(dev);
  120. int err = 0;
  121. err = blk_pre_runtime_suspend(sdev->request_queue);
  122. if (err)
  123. return err;
  124. if (pm && pm->runtime_suspend)
  125. err = pm->runtime_suspend(dev);
  126. blk_post_runtime_suspend(sdev->request_queue, err);
  127. return err;
  128. }
  129. static int scsi_runtime_suspend(struct device *dev)
  130. {
  131. int err = 0;
  132. dev_dbg(dev, "scsi_runtime_suspend\n");
  133. if (scsi_is_sdev_device(dev))
  134. err = sdev_runtime_suspend(dev);
  135. /* Insert hooks here for targets, hosts, and transport classes */
  136. return err;
  137. }
  138. static int sdev_runtime_resume(struct device *dev)
  139. {
  140. struct scsi_device *sdev = to_scsi_device(dev);
  141. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  142. int err = 0;
  143. blk_pre_runtime_resume(sdev->request_queue);
  144. if (pm && pm->runtime_resume)
  145. err = pm->runtime_resume(dev);
  146. blk_post_runtime_resume(sdev->request_queue);
  147. return err;
  148. }
  149. static int scsi_runtime_resume(struct device *dev)
  150. {
  151. int err = 0;
  152. dev_dbg(dev, "scsi_runtime_resume\n");
  153. if (scsi_is_sdev_device(dev))
  154. err = sdev_runtime_resume(dev);
  155. /* Insert hooks here for targets, hosts, and transport classes */
  156. return err;
  157. }
  158. static int scsi_runtime_idle(struct device *dev)
  159. {
  160. dev_dbg(dev, "scsi_runtime_idle\n");
  161. /* Insert hooks here for targets, hosts, and transport classes */
  162. if (scsi_is_sdev_device(dev)) {
  163. pm_runtime_autosuspend(dev);
  164. return -EBUSY;
  165. }
  166. return 0;
  167. }
  168. int scsi_autopm_get_device(struct scsi_device *sdev)
  169. {
  170. int err;
  171. err = pm_runtime_get_sync(&sdev->sdev_gendev);
  172. if (err < 0 && err !=-EACCES)
  173. pm_runtime_put_sync(&sdev->sdev_gendev);
  174. else
  175. err = 0;
  176. return err;
  177. }
  178. EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
  179. void scsi_autopm_put_device(struct scsi_device *sdev)
  180. {
  181. pm_runtime_put_sync(&sdev->sdev_gendev);
  182. }
  183. EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
  184. void scsi_autopm_get_target(struct scsi_target *starget)
  185. {
  186. pm_runtime_get_sync(&starget->dev);
  187. }
  188. void scsi_autopm_put_target(struct scsi_target *starget)
  189. {
  190. pm_runtime_put_sync(&starget->dev);
  191. }
  192. int scsi_autopm_get_host(struct Scsi_Host *shost)
  193. {
  194. int err;
  195. err = pm_runtime_get_sync(&shost->shost_gendev);
  196. if (err < 0 && err !=-EACCES)
  197. pm_runtime_put_sync(&shost->shost_gendev);
  198. else
  199. err = 0;
  200. return err;
  201. }
  202. void scsi_autopm_put_host(struct Scsi_Host *shost)
  203. {
  204. pm_runtime_put_sync(&shost->shost_gendev);
  205. }
  206. const struct dev_pm_ops scsi_bus_pm_ops = {
  207. .prepare = scsi_bus_prepare,
  208. .suspend = scsi_bus_suspend,
  209. .resume = scsi_bus_resume,
  210. .freeze = scsi_bus_freeze,
  211. .thaw = scsi_bus_thaw,
  212. .poweroff = scsi_bus_poweroff,
  213. .restore = scsi_bus_restore,
  214. .runtime_suspend = scsi_runtime_suspend,
  215. .runtime_resume = scsi_runtime_resume,
  216. .runtime_idle = scsi_runtime_idle,
  217. };