scsi_pm.c 8.9 KB

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