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/async.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
))
53 err
= scsi_dev_type_suspend(dev
, msg
);
57 static int scsi_bus_resume_common(struct device
*dev
)
61 if (scsi_is_sdev_device(dev
))
62 err
= scsi_dev_type_resume(dev
);
65 pm_runtime_disable(dev
);
66 pm_runtime_set_active(dev
);
67 pm_runtime_enable(dev
);
72 static int scsi_bus_prepare(struct device
*dev
)
74 if (scsi_is_sdev_device(dev
)) {
75 /* sd probing uses async_schedule. Wait until it finishes. */
76 async_synchronize_full();
78 } else if (scsi_is_host_device(dev
)) {
79 /* Wait until async scanning is finished */
80 scsi_complete_async_scans();
85 static int scsi_bus_suspend(struct device
*dev
)
87 return scsi_bus_suspend_common(dev
, PMSG_SUSPEND
);
90 static int scsi_bus_freeze(struct device
*dev
)
92 return scsi_bus_suspend_common(dev
, PMSG_FREEZE
);
95 static int scsi_bus_poweroff(struct device
*dev
)
97 return scsi_bus_suspend_common(dev
, PMSG_HIBERNATE
);
100 #else /* CONFIG_PM_SLEEP */
102 #define scsi_bus_resume_common NULL
103 #define scsi_bus_prepare NULL
104 #define scsi_bus_suspend NULL
105 #define scsi_bus_freeze NULL
106 #define scsi_bus_poweroff NULL
108 #endif /* CONFIG_PM_SLEEP */
110 #ifdef CONFIG_PM_RUNTIME
112 static int scsi_runtime_suspend(struct device
*dev
)
116 dev_dbg(dev
, "scsi_runtime_suspend\n");
117 if (scsi_is_sdev_device(dev
)) {
118 err
= scsi_dev_type_suspend(dev
, PMSG_AUTO_SUSPEND
);
120 pm_schedule_suspend(dev
, jiffies_to_msecs(
121 round_jiffies_up_relative(HZ
/10)));
124 /* Insert hooks here for targets, hosts, and transport classes */
129 static int scsi_runtime_resume(struct device
*dev
)
133 dev_dbg(dev
, "scsi_runtime_resume\n");
134 if (scsi_is_sdev_device(dev
))
135 err
= scsi_dev_type_resume(dev
);
137 /* Insert hooks here for targets, hosts, and transport classes */
142 static int scsi_runtime_idle(struct device
*dev
)
146 dev_dbg(dev
, "scsi_runtime_idle\n");
148 /* Insert hooks here for targets, hosts, and transport classes */
150 if (scsi_is_sdev_device(dev
))
151 err
= pm_schedule_suspend(dev
, 100);
153 err
= pm_runtime_suspend(dev
);
157 int scsi_autopm_get_device(struct scsi_device
*sdev
)
161 err
= pm_runtime_get_sync(&sdev
->sdev_gendev
);
163 pm_runtime_put_sync(&sdev
->sdev_gendev
);
168 EXPORT_SYMBOL_GPL(scsi_autopm_get_device
);
170 void scsi_autopm_put_device(struct scsi_device
*sdev
)
172 pm_runtime_put_sync(&sdev
->sdev_gendev
);
174 EXPORT_SYMBOL_GPL(scsi_autopm_put_device
);
176 void scsi_autopm_get_target(struct scsi_target
*starget
)
178 pm_runtime_get_sync(&starget
->dev
);
181 void scsi_autopm_put_target(struct scsi_target
*starget
)
183 pm_runtime_put_sync(&starget
->dev
);
186 int scsi_autopm_get_host(struct Scsi_Host
*shost
)
190 err
= pm_runtime_get_sync(&shost
->shost_gendev
);
192 pm_runtime_put_sync(&shost
->shost_gendev
);
198 void scsi_autopm_put_host(struct Scsi_Host
*shost
)
200 pm_runtime_put_sync(&shost
->shost_gendev
);
205 #define scsi_runtime_suspend NULL
206 #define scsi_runtime_resume NULL
207 #define scsi_runtime_idle NULL
209 #endif /* CONFIG_PM_RUNTIME */
211 const struct dev_pm_ops scsi_bus_pm_ops
= {
212 .prepare
= scsi_bus_prepare
,
213 .suspend
= scsi_bus_suspend
,
214 .resume
= scsi_bus_resume_common
,
215 .freeze
= scsi_bus_freeze
,
216 .thaw
= scsi_bus_resume_common
,
217 .poweroff
= scsi_bus_poweroff
,
218 .restore
= scsi_bus_resume_common
,
219 .runtime_suspend
= scsi_runtime_suspend
,
220 .runtime_resume
= scsi_runtime_resume
,
221 .runtime_idle
= scsi_runtime_idle
,