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 static int scsi_dev_type_suspend(struct device
*dev
, pm_message_t msg
)
21 struct device_driver
*drv
;
24 err
= scsi_device_quiesce(to_scsi_device(dev
));
27 if (drv
&& drv
->suspend
) {
28 err
= drv
->suspend(dev
, msg
);
30 scsi_device_resume(to_scsi_device(dev
));
33 dev_dbg(dev
, "scsi suspend: %d\n", err
);
37 static int scsi_dev_type_resume(struct device
*dev
)
39 struct device_driver
*drv
;
43 if (drv
&& drv
->resume
)
44 err
= drv
->resume(dev
);
45 scsi_device_resume(to_scsi_device(dev
));
46 dev_dbg(dev
, "scsi resume: %d\n", err
);
50 #ifdef CONFIG_PM_SLEEP
52 static int scsi_bus_suspend_common(struct device
*dev
, pm_message_t msg
)
56 if (scsi_is_sdev_device(dev
)) {
58 * sd is the only high-level SCSI driver to implement runtime
59 * PM, and sd treats runtime suspend, system suspend, and
60 * system hibernate identically (but not system freeze).
62 if (pm_runtime_suspended(dev
)) {
63 if (msg
.event
== PM_EVENT_SUSPEND
||
64 msg
.event
== PM_EVENT_HIBERNATE
)
65 return 0; /* already suspended */
67 /* wake up device so that FREEZE will succeed */
68 pm_runtime_resume(dev
);
70 err
= scsi_dev_type_suspend(dev
, msg
);
75 static int scsi_bus_resume_common(struct device
*dev
)
79 if (scsi_is_sdev_device(dev
))
80 err
= scsi_dev_type_resume(dev
);
83 pm_runtime_disable(dev
);
84 pm_runtime_set_active(dev
);
85 pm_runtime_enable(dev
);
90 static int scsi_bus_prepare(struct device
*dev
)
92 if (scsi_is_sdev_device(dev
)) {
93 /* sd probing uses async_schedule. Wait until it finishes. */
94 async_synchronize_full_domain(&scsi_sd_probe_domain
);
96 } else if (scsi_is_host_device(dev
)) {
97 /* Wait until async scanning is finished */
98 scsi_complete_async_scans();
103 static int scsi_bus_suspend(struct device
*dev
)
105 return scsi_bus_suspend_common(dev
, PMSG_SUSPEND
);
108 static int scsi_bus_freeze(struct device
*dev
)
110 return scsi_bus_suspend_common(dev
, PMSG_FREEZE
);
113 static int scsi_bus_poweroff(struct device
*dev
)
115 return scsi_bus_suspend_common(dev
, PMSG_HIBERNATE
);
118 #else /* CONFIG_PM_SLEEP */
120 #define scsi_bus_resume_common NULL
121 #define scsi_bus_prepare NULL
122 #define scsi_bus_suspend NULL
123 #define scsi_bus_freeze NULL
124 #define scsi_bus_poweroff NULL
126 #endif /* CONFIG_PM_SLEEP */
128 #ifdef CONFIG_PM_RUNTIME
130 static int scsi_runtime_suspend(struct device
*dev
)
134 dev_dbg(dev
, "scsi_runtime_suspend\n");
135 if (scsi_is_sdev_device(dev
)) {
136 err
= scsi_dev_type_suspend(dev
, PMSG_AUTO_SUSPEND
);
138 pm_schedule_suspend(dev
, jiffies_to_msecs(
139 round_jiffies_up_relative(HZ
/10)));
142 /* Insert hooks here for targets, hosts, and transport classes */
147 static int scsi_runtime_resume(struct device
*dev
)
151 dev_dbg(dev
, "scsi_runtime_resume\n");
152 if (scsi_is_sdev_device(dev
))
153 err
= scsi_dev_type_resume(dev
);
155 /* Insert hooks here for targets, hosts, and transport classes */
160 static int scsi_runtime_idle(struct device
*dev
)
164 dev_dbg(dev
, "scsi_runtime_idle\n");
166 /* Insert hooks here for targets, hosts, and transport classes */
168 if (scsi_is_sdev_device(dev
))
169 err
= pm_schedule_suspend(dev
, 100);
171 err
= pm_runtime_suspend(dev
);
175 int scsi_autopm_get_device(struct scsi_device
*sdev
)
179 err
= pm_runtime_get_sync(&sdev
->sdev_gendev
);
180 if (err
< 0 && err
!=-EACCES
)
181 pm_runtime_put_sync(&sdev
->sdev_gendev
);
186 EXPORT_SYMBOL_GPL(scsi_autopm_get_device
);
188 void scsi_autopm_put_device(struct scsi_device
*sdev
)
190 pm_runtime_put_sync(&sdev
->sdev_gendev
);
192 EXPORT_SYMBOL_GPL(scsi_autopm_put_device
);
194 void scsi_autopm_get_target(struct scsi_target
*starget
)
196 pm_runtime_get_sync(&starget
->dev
);
199 void scsi_autopm_put_target(struct scsi_target
*starget
)
201 pm_runtime_put_sync(&starget
->dev
);
204 int scsi_autopm_get_host(struct Scsi_Host
*shost
)
208 err
= pm_runtime_get_sync(&shost
->shost_gendev
);
209 if (err
< 0 && err
!=-EACCES
)
210 pm_runtime_put_sync(&shost
->shost_gendev
);
216 void scsi_autopm_put_host(struct Scsi_Host
*shost
)
218 pm_runtime_put_sync(&shost
->shost_gendev
);
223 #define scsi_runtime_suspend NULL
224 #define scsi_runtime_resume NULL
225 #define scsi_runtime_idle NULL
227 #endif /* CONFIG_PM_RUNTIME */
229 const struct dev_pm_ops scsi_bus_pm_ops
= {
230 .prepare
= scsi_bus_prepare
,
231 .suspend
= scsi_bus_suspend
,
232 .resume
= scsi_bus_resume_common
,
233 .freeze
= scsi_bus_freeze
,
234 .thaw
= scsi_bus_resume_common
,
235 .poweroff
= scsi_bus_poweroff
,
236 .restore
= scsi_bus_resume_common
,
237 .runtime_suspend
= scsi_runtime_suspend
,
238 .runtime_resume
= scsi_runtime_resume
,
239 .runtime_idle
= scsi_runtime_idle
,