2 * scsi_pm.c Copyright (C) 2010 Alan Stern
4 * SCSI dynamic Power Management
5 * Initial version: Alan Stern <stern@rowland.harvard.edu>
8 #include <linux/pm_runtime.h>
9 #include <linux/export.h>
10 #include <linux/async.h>
12 #include <scsi/scsi.h>
13 #include <scsi/scsi_device.h>
14 #include <scsi/scsi_driver.h>
15 #include <scsi/scsi_host.h>
17 #include "scsi_priv.h"
19 #ifdef CONFIG_PM_SLEEP
21 static int do_scsi_suspend(struct device
*dev
, const struct dev_pm_ops
*pm
)
23 return pm
&& pm
->suspend
? pm
->suspend(dev
) : 0;
26 static int do_scsi_freeze(struct device
*dev
, const struct dev_pm_ops
*pm
)
28 return pm
&& pm
->freeze
? pm
->freeze(dev
) : 0;
31 static int do_scsi_poweroff(struct device
*dev
, const struct dev_pm_ops
*pm
)
33 return pm
&& pm
->poweroff
? pm
->poweroff(dev
) : 0;
36 static int do_scsi_resume(struct device
*dev
, const struct dev_pm_ops
*pm
)
38 return pm
&& pm
->resume
? pm
->resume(dev
) : 0;
41 static int do_scsi_thaw(struct device
*dev
, const struct dev_pm_ops
*pm
)
43 return pm
&& pm
->thaw
? pm
->thaw(dev
) : 0;
46 static int do_scsi_restore(struct device
*dev
, const struct dev_pm_ops
*pm
)
48 return pm
&& pm
->restore
? pm
->restore(dev
) : 0;
51 static int scsi_dev_type_suspend(struct device
*dev
,
52 int (*cb
)(struct device
*, const struct dev_pm_ops
*))
54 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
57 /* flush pending in-flight resume operations, suspend is synchronous */
58 async_synchronize_full_domain(&scsi_sd_pm_domain
);
60 err
= scsi_device_quiesce(to_scsi_device(dev
));
64 scsi_device_resume(to_scsi_device(dev
));
66 dev_dbg(dev
, "scsi suspend: %d\n", err
);
70 static int scsi_dev_type_resume(struct device
*dev
,
71 int (*cb
)(struct device
*, const struct dev_pm_ops
*))
73 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
77 scsi_device_resume(to_scsi_device(dev
));
78 dev_dbg(dev
, "scsi resume: %d\n", err
);
81 pm_runtime_disable(dev
);
82 err
= pm_runtime_set_active(dev
);
83 pm_runtime_enable(dev
);
86 * Forcibly set runtime PM status of request queue to "active"
87 * to make sure we can again get requests from the queue
88 * (see also blk_pm_peek_request()).
90 * The resume hook will correct runtime PM status of the disk.
92 if (!err
&& scsi_is_sdev_device(dev
)) {
93 struct scsi_device
*sdev
= to_scsi_device(dev
);
95 if (sdev
->request_queue
->dev
)
96 blk_set_runtime_active(sdev
->request_queue
);
104 scsi_bus_suspend_common(struct device
*dev
,
105 int (*cb
)(struct device
*, const struct dev_pm_ops
*))
109 if (scsi_is_sdev_device(dev
)) {
111 * All the high-level SCSI drivers that implement runtime
112 * PM treat runtime suspend, system suspend, and system
113 * hibernate nearly identically. In all cases the requirements
114 * for runtime suspension are stricter.
116 if (pm_runtime_suspended(dev
))
119 err
= scsi_dev_type_suspend(dev
, cb
);
125 static void async_sdev_resume(void *dev
, async_cookie_t cookie
)
127 scsi_dev_type_resume(dev
, do_scsi_resume
);
130 static void async_sdev_thaw(void *dev
, async_cookie_t cookie
)
132 scsi_dev_type_resume(dev
, do_scsi_thaw
);
135 static void async_sdev_restore(void *dev
, async_cookie_t cookie
)
137 scsi_dev_type_resume(dev
, do_scsi_restore
);
140 static int scsi_bus_resume_common(struct device
*dev
,
141 int (*cb
)(struct device
*, const struct dev_pm_ops
*))
145 if (!scsi_is_sdev_device(dev
))
147 else if (cb
== do_scsi_resume
)
148 fn
= async_sdev_resume
;
149 else if (cb
== do_scsi_thaw
)
150 fn
= async_sdev_thaw
;
151 else if (cb
== do_scsi_restore
)
152 fn
= async_sdev_restore
;
157 async_schedule_domain(fn
, dev
, &scsi_sd_pm_domain
);
160 * If a user has disabled async probing a likely reason
161 * is due to a storage enclosure that does not inject
162 * staggered spin-ups. For safety, make resume
163 * synchronous as well in that case.
165 if (strncmp(scsi_scan_type
, "async", 5) != 0)
166 async_synchronize_full_domain(&scsi_sd_pm_domain
);
168 pm_runtime_disable(dev
);
169 pm_runtime_set_active(dev
);
170 pm_runtime_enable(dev
);
175 static int scsi_bus_prepare(struct device
*dev
)
177 if (scsi_is_sdev_device(dev
)) {
178 /* sd probing uses async_schedule. Wait until it finishes. */
179 async_synchronize_full_domain(&scsi_sd_probe_domain
);
181 } else if (scsi_is_host_device(dev
)) {
182 /* Wait until async scanning is finished */
183 scsi_complete_async_scans();
188 static int scsi_bus_suspend(struct device
*dev
)
190 return scsi_bus_suspend_common(dev
, do_scsi_suspend
);
193 static int scsi_bus_resume(struct device
*dev
)
195 return scsi_bus_resume_common(dev
, do_scsi_resume
);
198 static int scsi_bus_freeze(struct device
*dev
)
200 return scsi_bus_suspend_common(dev
, do_scsi_freeze
);
203 static int scsi_bus_thaw(struct device
*dev
)
205 return scsi_bus_resume_common(dev
, do_scsi_thaw
);
208 static int scsi_bus_poweroff(struct device
*dev
)
210 return scsi_bus_suspend_common(dev
, do_scsi_poweroff
);
213 static int scsi_bus_restore(struct device
*dev
)
215 return scsi_bus_resume_common(dev
, do_scsi_restore
);
218 #else /* CONFIG_PM_SLEEP */
220 #define scsi_bus_prepare NULL
221 #define scsi_bus_suspend NULL
222 #define scsi_bus_resume NULL
223 #define scsi_bus_freeze NULL
224 #define scsi_bus_thaw NULL
225 #define scsi_bus_poweroff NULL
226 #define scsi_bus_restore NULL
228 #endif /* CONFIG_PM_SLEEP */
230 static int sdev_runtime_suspend(struct device
*dev
)
232 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
233 struct scsi_device
*sdev
= to_scsi_device(dev
);
236 err
= blk_pre_runtime_suspend(sdev
->request_queue
);
239 if (pm
&& pm
->runtime_suspend
)
240 err
= pm
->runtime_suspend(dev
);
241 blk_post_runtime_suspend(sdev
->request_queue
, err
);
246 static int scsi_runtime_suspend(struct device
*dev
)
250 dev_dbg(dev
, "scsi_runtime_suspend\n");
251 if (scsi_is_sdev_device(dev
))
252 err
= sdev_runtime_suspend(dev
);
254 /* Insert hooks here for targets, hosts, and transport classes */
259 static int sdev_runtime_resume(struct device
*dev
)
261 struct scsi_device
*sdev
= to_scsi_device(dev
);
262 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
265 blk_pre_runtime_resume(sdev
->request_queue
);
266 if (pm
&& pm
->runtime_resume
)
267 err
= pm
->runtime_resume(dev
);
268 blk_post_runtime_resume(sdev
->request_queue
, err
);
273 static int scsi_runtime_resume(struct device
*dev
)
277 dev_dbg(dev
, "scsi_runtime_resume\n");
278 if (scsi_is_sdev_device(dev
))
279 err
= sdev_runtime_resume(dev
);
281 /* Insert hooks here for targets, hosts, and transport classes */
286 static int scsi_runtime_idle(struct device
*dev
)
288 dev_dbg(dev
, "scsi_runtime_idle\n");
290 /* Insert hooks here for targets, hosts, and transport classes */
292 if (scsi_is_sdev_device(dev
)) {
293 pm_runtime_mark_last_busy(dev
);
294 pm_runtime_autosuspend(dev
);
301 int scsi_autopm_get_device(struct scsi_device
*sdev
)
305 err
= pm_runtime_get_sync(&sdev
->sdev_gendev
);
306 if (err
< 0 && err
!=-EACCES
)
307 pm_runtime_put_sync(&sdev
->sdev_gendev
);
312 EXPORT_SYMBOL_GPL(scsi_autopm_get_device
);
314 void scsi_autopm_put_device(struct scsi_device
*sdev
)
316 pm_runtime_put_sync(&sdev
->sdev_gendev
);
318 EXPORT_SYMBOL_GPL(scsi_autopm_put_device
);
320 void scsi_autopm_get_target(struct scsi_target
*starget
)
322 pm_runtime_get_sync(&starget
->dev
);
325 void scsi_autopm_put_target(struct scsi_target
*starget
)
327 pm_runtime_put_sync(&starget
->dev
);
330 int scsi_autopm_get_host(struct Scsi_Host
*shost
)
334 err
= pm_runtime_get_sync(&shost
->shost_gendev
);
335 if (err
< 0 && err
!=-EACCES
)
336 pm_runtime_put_sync(&shost
->shost_gendev
);
342 void scsi_autopm_put_host(struct Scsi_Host
*shost
)
344 pm_runtime_put_sync(&shost
->shost_gendev
);
347 const struct dev_pm_ops scsi_bus_pm_ops
= {
348 .prepare
= scsi_bus_prepare
,
349 .suspend
= scsi_bus_suspend
,
350 .resume
= scsi_bus_resume
,
351 .freeze
= scsi_bus_freeze
,
352 .thaw
= scsi_bus_thaw
,
353 .poweroff
= scsi_bus_poweroff
,
354 .restore
= scsi_bus_restore
,
355 .runtime_suspend
= scsi_runtime_suspend
,
356 .runtime_resume
= scsi_runtime_resume
,
357 .runtime_idle
= scsi_runtime_idle
,