blob: ff78095a85747e82d6d62c488002deb76da34592 [file] [log] [blame]
Alan Sterndb5bd1e2010-06-17 10:36:49 -04001/*
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
8#include <linux/pm_runtime.h>
Paul Gortmaker09703662011-05-27 09:37:25 -04009#include <linux/export.h>
Alan Sternfea6d602012-02-17 16:25:08 -050010#include <linux/async.h>
Alan Sterndb5bd1e2010-06-17 10:36:49 -040011
12#include <scsi/scsi.h>
13#include <scsi/scsi_device.h>
14#include <scsi/scsi_driver.h>
15#include <scsi/scsi_host.h>
16
17#include "scsi_priv.h"
18
Aaron Lu0ed2aa62012-11-09 15:27:54 +080019static int scsi_dev_type_suspend(struct device *dev, int (*cb)(struct device *))
Alan Sterndb5bd1e2010-06-17 10:36:49 -040020{
Alan Sterndb5bd1e2010-06-17 10:36:49 -040021 int err;
22
23 err = scsi_device_quiesce(to_scsi_device(dev));
24 if (err == 0) {
Aaron Lu0ed2aa62012-11-09 15:27:54 +080025 if (cb) {
26 err = cb(dev);
Aaron Lu845abae2012-05-15 14:43:00 +080027 if (err)
28 scsi_device_resume(to_scsi_device(dev));
29 }
Alan Sterndb5bd1e2010-06-17 10:36:49 -040030 }
31 dev_dbg(dev, "scsi suspend: %d\n", err);
32 return err;
33}
34
Aaron Lu0ed2aa62012-11-09 15:27:54 +080035static int scsi_dev_type_resume(struct device *dev, int (*cb)(struct device *))
Alan Sterndb5bd1e2010-06-17 10:36:49 -040036{
Alan Sterndb5bd1e2010-06-17 10:36:49 -040037 int err = 0;
38
Aaron Lu0ed2aa62012-11-09 15:27:54 +080039 if (cb)
40 err = cb(dev);
Alan Sterndb5bd1e2010-06-17 10:36:49 -040041 scsi_device_resume(to_scsi_device(dev));
42 dev_dbg(dev, "scsi resume: %d\n", err);
43 return err;
44}
45
46#ifdef CONFIG_PM_SLEEP
47
Aaron Lu0ed2aa62012-11-09 15:27:54 +080048static int
49scsi_bus_suspend_common(struct device *dev, int (*cb)(struct device *))
Alan Sterndb5bd1e2010-06-17 10:36:49 -040050{
51 int err = 0;
52
Lin Ming28640512011-12-05 09:20:25 +080053 if (scsi_is_sdev_device(dev)) {
54 /*
Aaron Lu0ed2aa62012-11-09 15:27:54 +080055 * All the high-level SCSI drivers that implement runtime
56 * PM treat runtime suspend, system suspend, and system
57 * hibernate identically.
Lin Ming28640512011-12-05 09:20:25 +080058 */
Aaron Lu0ed2aa62012-11-09 15:27:54 +080059 if (pm_runtime_suspended(dev))
60 return 0;
Lin Ming28640512011-12-05 09:20:25 +080061
Aaron Lu0ed2aa62012-11-09 15:27:54 +080062 err = scsi_dev_type_suspend(dev, cb);
Lin Ming28640512011-12-05 09:20:25 +080063 }
Aaron Lu0ed2aa62012-11-09 15:27:54 +080064
Alan Sterndb5bd1e2010-06-17 10:36:49 -040065 return err;
66}
67
Aaron Lu0ed2aa62012-11-09 15:27:54 +080068static int
69scsi_bus_resume_common(struct device *dev, int (*cb)(struct device *))
Alan Sterndb5bd1e2010-06-17 10:36:49 -040070{
71 int err = 0;
72
Lin Ming28fd00d2011-12-22 14:50:47 +080073 if (scsi_is_sdev_device(dev)) {
74 /*
75 * Parent device may have runtime suspended as soon as
76 * it is woken up during the system resume.
77 *
78 * Resume it on behalf of child.
79 */
80 pm_runtime_get_sync(dev->parent);
Aaron Lu0ed2aa62012-11-09 15:27:54 +080081 err = scsi_dev_type_resume(dev, cb);
Lin Ming28fd00d2011-12-22 14:50:47 +080082 pm_runtime_put_sync(dev->parent);
83 }
Alan Sternbc4f2402010-06-17 10:41:42 -040084
85 if (err == 0) {
86 pm_runtime_disable(dev);
87 pm_runtime_set_active(dev);
88 pm_runtime_enable(dev);
89 }
Alan Sterndb5bd1e2010-06-17 10:36:49 -040090 return err;
91}
92
Alan Sternfea6d602012-02-17 16:25:08 -050093static int scsi_bus_prepare(struct device *dev)
94{
95 if (scsi_is_sdev_device(dev)) {
96 /* sd probing uses async_schedule. Wait until it finishes. */
97 async_synchronize_full();
98
99 } else if (scsi_is_host_device(dev)) {
100 /* Wait until async scanning is finished */
101 scsi_complete_async_scans();
102 }
103 return 0;
104}
105
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400106static int scsi_bus_suspend(struct device *dev)
107{
Aaron Lu0ed2aa62012-11-09 15:27:54 +0800108 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
109 return scsi_bus_suspend_common(dev, pm ? pm->suspend : NULL);
110}
111
112static int scsi_bus_resume(struct device *dev)
113{
114 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
115 return scsi_bus_resume_common(dev, pm ? pm->resume : NULL);
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400116}
117
118static int scsi_bus_freeze(struct device *dev)
119{
Aaron Lu0ed2aa62012-11-09 15:27:54 +0800120 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
121 return scsi_bus_suspend_common(dev, pm ? pm->freeze : NULL);
122}
123
124static int scsi_bus_thaw(struct device *dev)
125{
126 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
127 return scsi_bus_resume_common(dev, pm ? pm->thaw : NULL);
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400128}
129
130static int scsi_bus_poweroff(struct device *dev)
131{
Aaron Lu0ed2aa62012-11-09 15:27:54 +0800132 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
133 return scsi_bus_suspend_common(dev, pm ? pm->poweroff : NULL);
134}
135
136static int scsi_bus_restore(struct device *dev)
137{
138 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
139 return scsi_bus_resume_common(dev, pm ? pm->restore : NULL);
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400140}
141
142#else /* CONFIG_PM_SLEEP */
143
Alan Sternfea6d602012-02-17 16:25:08 -0500144#define scsi_bus_prepare NULL
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400145#define scsi_bus_suspend NULL
Aaron Lu0ed2aa62012-11-09 15:27:54 +0800146#define scsi_bus_resume NULL
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400147#define scsi_bus_freeze NULL
Aaron Lu0ed2aa62012-11-09 15:27:54 +0800148#define scsi_bus_thaw NULL
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400149#define scsi_bus_poweroff NULL
Aaron Lu0ed2aa62012-11-09 15:27:54 +0800150#define scsi_bus_restore NULL
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400151
152#endif /* CONFIG_PM_SLEEP */
153
Alan Sternbc4f2402010-06-17 10:41:42 -0400154#ifdef CONFIG_PM_RUNTIME
155
Lin Ming00fdcd72013-03-23 11:42:28 +0800156static int sdev_blk_runtime_suspend(struct scsi_device *sdev,
157 int (*cb)(struct device *))
158{
159 int err;
160
161 err = blk_pre_runtime_suspend(sdev->request_queue);
162 if (err)
163 return err;
164 if (cb)
165 err = cb(&sdev->sdev_gendev);
166 blk_post_runtime_suspend(sdev->request_queue, err);
167
168 return err;
169}
170
171static int sdev_runtime_suspend(struct device *dev)
172{
173 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
174 int (*cb)(struct device *) = pm ? pm->runtime_suspend : NULL;
175 struct scsi_device *sdev = to_scsi_device(dev);
176 int err;
177
178 if (sdev->request_queue->dev)
179 return sdev_blk_runtime_suspend(sdev, cb);
180
181 err = scsi_dev_type_suspend(dev, cb);
182 if (err == -EAGAIN)
183 pm_schedule_suspend(dev, jiffies_to_msecs(
184 round_jiffies_up_relative(HZ/10)));
185 return err;
186}
187
Alan Sternbc4f2402010-06-17 10:41:42 -0400188static int scsi_runtime_suspend(struct device *dev)
189{
190 int err = 0;
191
192 dev_dbg(dev, "scsi_runtime_suspend\n");
Lin Ming00fdcd72013-03-23 11:42:28 +0800193 if (scsi_is_sdev_device(dev))
194 err = sdev_runtime_suspend(dev);
Alan Sternbc4f2402010-06-17 10:41:42 -0400195
196 /* Insert hooks here for targets, hosts, and transport classes */
197
198 return err;
199}
200
Lin Ming00fdcd72013-03-23 11:42:28 +0800201static int sdev_blk_runtime_resume(struct scsi_device *sdev,
202 int (*cb)(struct device *))
203{
204 int err = 0;
205
206 blk_pre_runtime_resume(sdev->request_queue);
207 if (cb)
208 err = cb(&sdev->sdev_gendev);
209 blk_post_runtime_resume(sdev->request_queue, err);
210
211 return err;
212}
213
214static int sdev_runtime_resume(struct device *dev)
215{
216 struct scsi_device *sdev = to_scsi_device(dev);
217 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
218 int (*cb)(struct device *) = pm ? pm->runtime_resume : NULL;
219
220 if (sdev->request_queue->dev)
221 return sdev_blk_runtime_resume(sdev, cb);
222 else
223 return scsi_dev_type_resume(dev, cb);
224}
225
Alan Sternbc4f2402010-06-17 10:41:42 -0400226static int scsi_runtime_resume(struct device *dev)
227{
228 int err = 0;
229
230 dev_dbg(dev, "scsi_runtime_resume\n");
231 if (scsi_is_sdev_device(dev))
Lin Ming00fdcd72013-03-23 11:42:28 +0800232 err = sdev_runtime_resume(dev);
Alan Sternbc4f2402010-06-17 10:41:42 -0400233
234 /* Insert hooks here for targets, hosts, and transport classes */
235
236 return err;
237}
238
239static int scsi_runtime_idle(struct device *dev)
240{
241 int err;
242
243 dev_dbg(dev, "scsi_runtime_idle\n");
244
245 /* Insert hooks here for targets, hosts, and transport classes */
246
Lin Ming00fdcd72013-03-23 11:42:28 +0800247 if (scsi_is_sdev_device(dev)) {
248 struct scsi_device *sdev = to_scsi_device(dev);
249
250 if (sdev->request_queue->dev) {
251 pm_runtime_mark_last_busy(dev);
252 err = pm_runtime_autosuspend(dev);
253 } else {
254 err = pm_runtime_suspend(dev);
255 }
256 } else {
Alan Sternbc4f2402010-06-17 10:41:42 -0400257 err = pm_runtime_suspend(dev);
Lin Ming00fdcd72013-03-23 11:42:28 +0800258 }
Alan Sternbc4f2402010-06-17 10:41:42 -0400259 return err;
260}
261
262int scsi_autopm_get_device(struct scsi_device *sdev)
263{
264 int err;
265
266 err = pm_runtime_get_sync(&sdev->sdev_gendev);
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200267 if (err < 0 && err !=-EACCES)
Alan Sternbc4f2402010-06-17 10:41:42 -0400268 pm_runtime_put_sync(&sdev->sdev_gendev);
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200269 else
Alan Sternbc4f2402010-06-17 10:41:42 -0400270 err = 0;
271 return err;
272}
273EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
274
275void scsi_autopm_put_device(struct scsi_device *sdev)
276{
277 pm_runtime_put_sync(&sdev->sdev_gendev);
278}
279EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
280
281void scsi_autopm_get_target(struct scsi_target *starget)
282{
283 pm_runtime_get_sync(&starget->dev);
284}
285
286void scsi_autopm_put_target(struct scsi_target *starget)
287{
288 pm_runtime_put_sync(&starget->dev);
289}
290
291int scsi_autopm_get_host(struct Scsi_Host *shost)
292{
293 int err;
294
295 err = pm_runtime_get_sync(&shost->shost_gendev);
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200296 if (err < 0 && err !=-EACCES)
Alan Sternbc4f2402010-06-17 10:41:42 -0400297 pm_runtime_put_sync(&shost->shost_gendev);
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200298 else
Alan Sternbc4f2402010-06-17 10:41:42 -0400299 err = 0;
300 return err;
301}
302
303void scsi_autopm_put_host(struct Scsi_Host *shost)
304{
305 pm_runtime_put_sync(&shost->shost_gendev);
306}
307
308#else
309
310#define scsi_runtime_suspend NULL
311#define scsi_runtime_resume NULL
312#define scsi_runtime_idle NULL
313
314#endif /* CONFIG_PM_RUNTIME */
315
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400316const struct dev_pm_ops scsi_bus_pm_ops = {
Alan Sternfea6d602012-02-17 16:25:08 -0500317 .prepare = scsi_bus_prepare,
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400318 .suspend = scsi_bus_suspend,
Aaron Lu0ed2aa62012-11-09 15:27:54 +0800319 .resume = scsi_bus_resume,
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400320 .freeze = scsi_bus_freeze,
Aaron Lu0ed2aa62012-11-09 15:27:54 +0800321 .thaw = scsi_bus_thaw,
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400322 .poweroff = scsi_bus_poweroff,
Aaron Lu0ed2aa62012-11-09 15:27:54 +0800323 .restore = scsi_bus_restore,
Alan Sternbc4f2402010-06-17 10:41:42 -0400324 .runtime_suspend = scsi_runtime_suspend,
325 .runtime_resume = scsi_runtime_resume,
326 .runtime_idle = scsi_runtime_idle,
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400327};