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>
11 #include <scsi/scsi.h>
12 #include <scsi/scsi_device.h>
13 #include <scsi/scsi_driver.h>
14 #include <scsi/scsi_host.h>
16 #include "scsi_priv.h"
18 static int scsi_dev_type_suspend(struct device
*dev
, pm_message_t msg
)
20 struct device_driver
*drv
;
23 err
= scsi_device_quiesce(to_scsi_device(dev
));
26 if (drv
&& drv
->suspend
)
27 err
= drv
->suspend(dev
, msg
);
29 dev_dbg(dev
, "scsi suspend: %d\n", err
);
33 static int scsi_dev_type_resume(struct device
*dev
)
35 struct device_driver
*drv
;
39 if (drv
&& drv
->resume
)
40 err
= drv
->resume(dev
);
41 scsi_device_resume(to_scsi_device(dev
));
42 dev_dbg(dev
, "scsi resume: %d\n", err
);
46 #ifdef CONFIG_PM_SLEEP
48 static int scsi_bus_suspend_common(struct device
*dev
, pm_message_t msg
)
52 if (scsi_is_sdev_device(dev
)) {
54 * sd is the only high-level SCSI driver to implement runtime
55 * PM, and sd treats runtime suspend, system suspend, and
56 * system hibernate identically (but not system freeze).
58 if (pm_runtime_suspended(dev
)) {
59 if (msg
.event
== PM_EVENT_SUSPEND
||
60 msg
.event
== PM_EVENT_HIBERNATE
)
61 return 0; /* already suspended */
63 /* wake up device so that FREEZE will succeed */
64 pm_runtime_resume(dev
);
66 err
= scsi_dev_type_suspend(dev
, msg
);
71 static int scsi_bus_resume_common(struct device
*dev
)
75 if (scsi_is_sdev_device(dev
)) {
77 * Parent device may have runtime suspended as soon as
78 * it is woken up during the system resume.
80 * Resume it on behalf of child.
82 pm_runtime_get_sync(dev
->parent
);
83 err
= scsi_dev_type_resume(dev
);
84 pm_runtime_put_sync(dev
->parent
);
88 pm_runtime_disable(dev
);
89 pm_runtime_set_active(dev
);
90 pm_runtime_enable(dev
);
95 static int scsi_bus_suspend(struct device
*dev
)
97 return scsi_bus_suspend_common(dev
, PMSG_SUSPEND
);
100 static int scsi_bus_freeze(struct device
*dev
)
102 return scsi_bus_suspend_common(dev
, PMSG_FREEZE
);
105 static int scsi_bus_poweroff(struct device
*dev
)
107 return scsi_bus_suspend_common(dev
, PMSG_HIBERNATE
);
110 #else /* CONFIG_PM_SLEEP */
112 #define scsi_bus_resume_common NULL
113 #define scsi_bus_suspend NULL
114 #define scsi_bus_freeze NULL
115 #define scsi_bus_poweroff NULL
117 #endif /* CONFIG_PM_SLEEP */
119 #ifdef CONFIG_PM_RUNTIME
121 static int scsi_runtime_suspend(struct device
*dev
)
125 dev_dbg(dev
, "scsi_runtime_suspend\n");
126 if (scsi_is_sdev_device(dev
)) {
127 err
= scsi_dev_type_suspend(dev
, PMSG_AUTO_SUSPEND
);
129 pm_schedule_suspend(dev
, jiffies_to_msecs(
130 round_jiffies_up_relative(HZ
/10)));
133 /* Insert hooks here for targets, hosts, and transport classes */
138 static int scsi_runtime_resume(struct device
*dev
)
142 dev_dbg(dev
, "scsi_runtime_resume\n");
143 if (scsi_is_sdev_device(dev
))
144 err
= scsi_dev_type_resume(dev
);
146 /* Insert hooks here for targets, hosts, and transport classes */
151 static int scsi_runtime_idle(struct device
*dev
)
155 dev_dbg(dev
, "scsi_runtime_idle\n");
157 /* Insert hooks here for targets, hosts, and transport classes */
159 if (scsi_is_sdev_device(dev
))
160 err
= pm_schedule_suspend(dev
, 100);
162 err
= pm_runtime_suspend(dev
);
166 int scsi_autopm_get_device(struct scsi_device
*sdev
)
170 err
= pm_runtime_get_sync(&sdev
->sdev_gendev
);
171 if (err
< 0 && err
!=-EACCES
)
172 pm_runtime_put_sync(&sdev
->sdev_gendev
);
177 EXPORT_SYMBOL_GPL(scsi_autopm_get_device
);
179 void scsi_autopm_put_device(struct scsi_device
*sdev
)
181 pm_runtime_put_sync(&sdev
->sdev_gendev
);
183 EXPORT_SYMBOL_GPL(scsi_autopm_put_device
);
185 void scsi_autopm_get_target(struct scsi_target
*starget
)
187 pm_runtime_get_sync(&starget
->dev
);
190 void scsi_autopm_put_target(struct scsi_target
*starget
)
192 pm_runtime_put_sync(&starget
->dev
);
195 int scsi_autopm_get_host(struct Scsi_Host
*shost
)
199 err
= pm_runtime_get_sync(&shost
->shost_gendev
);
200 if (err
< 0 && err
!=-EACCES
)
201 pm_runtime_put_sync(&shost
->shost_gendev
);
207 void scsi_autopm_put_host(struct Scsi_Host
*shost
)
209 pm_runtime_put_sync(&shost
->shost_gendev
);
214 #define scsi_runtime_suspend NULL
215 #define scsi_runtime_resume NULL
216 #define scsi_runtime_idle NULL
218 #endif /* CONFIG_PM_RUNTIME */
220 const struct dev_pm_ops scsi_bus_pm_ops
= {
221 .suspend
= scsi_bus_suspend
,
222 .resume
= scsi_bus_resume_common
,
223 .freeze
= scsi_bus_freeze
,
224 .thaw
= scsi_bus_resume_common
,
225 .poweroff
= scsi_bus_poweroff
,
226 .restore
= scsi_bus_resume_common
,
227 .runtime_suspend
= scsi_runtime_suspend
,
228 .runtime_resume
= scsi_runtime_resume
,
229 .runtime_idle
= scsi_runtime_idle
,