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 dev_dbg(dev, "scsi suspend: %d\n", err);
34 static int scsi_dev_type_resume(struct device *dev)
36 struct device_driver *drv;
40 if (drv && drv->resume)
41 err = drv->resume(dev);
42 scsi_device_resume(to_scsi_device(dev));
43 dev_dbg(dev, "scsi resume: %d\n", err);
47 #ifdef CONFIG_PM_SLEEP
49 static int scsi_bus_suspend_common(struct device *dev, pm_message_t msg)
53 if (scsi_is_sdev_device(dev)) {
55 * sd is the only high-level SCSI driver to implement runtime
56 * PM, and sd treats runtime suspend, system suspend, and
57 * system hibernate identically (but not system freeze).
59 if (pm_runtime_suspended(dev)) {
60 if (msg.event == PM_EVENT_SUSPEND ||
61 msg.event == PM_EVENT_HIBERNATE)
62 return 0; /* already suspended */
64 /* wake up device so that FREEZE will succeed */
65 pm_runtime_resume(dev);
67 err = scsi_dev_type_suspend(dev, msg);
72 static int scsi_bus_resume_common(struct device *dev)
76 if (scsi_is_sdev_device(dev)) {
78 * Parent device may have runtime suspended as soon as
79 * it is woken up during the system resume.
81 * Resume it on behalf of child.
83 pm_runtime_get_sync(dev->parent);
84 err = scsi_dev_type_resume(dev);
85 pm_runtime_put_sync(dev->parent);
89 pm_runtime_disable(dev);
90 pm_runtime_set_active(dev);
91 pm_runtime_enable(dev);
96 static int scsi_bus_prepare(struct device *dev)
98 if (scsi_is_sdev_device(dev)) {
99 /* sd probing uses async_schedule. Wait until it finishes. */
100 async_synchronize_full_domain(&scsi_sd_probe_domain);
102 } else if (scsi_is_host_device(dev)) {
103 /* Wait until async scanning is finished */
104 scsi_complete_async_scans();
109 static int scsi_bus_suspend(struct device *dev)
111 return scsi_bus_suspend_common(dev, PMSG_SUSPEND);
114 static int scsi_bus_freeze(struct device *dev)
116 return scsi_bus_suspend_common(dev, PMSG_FREEZE);
119 static int scsi_bus_poweroff(struct device *dev)
121 return scsi_bus_suspend_common(dev, PMSG_HIBERNATE);
124 #else /* CONFIG_PM_SLEEP */
126 #define scsi_bus_resume_common NULL
127 #define scsi_bus_prepare NULL
128 #define scsi_bus_suspend NULL
129 #define scsi_bus_freeze NULL
130 #define scsi_bus_poweroff NULL
132 #endif /* CONFIG_PM_SLEEP */
134 #ifdef CONFIG_PM_RUNTIME
136 static int scsi_runtime_suspend(struct device *dev)
140 dev_dbg(dev, "scsi_runtime_suspend\n");
141 if (scsi_is_sdev_device(dev)) {
142 err = scsi_dev_type_suspend(dev, PMSG_AUTO_SUSPEND);
144 pm_schedule_suspend(dev, jiffies_to_msecs(
145 round_jiffies_up_relative(HZ/10)));
148 /* Insert hooks here for targets, hosts, and transport classes */
153 static int scsi_runtime_resume(struct device *dev)
157 dev_dbg(dev, "scsi_runtime_resume\n");
158 if (scsi_is_sdev_device(dev))
159 err = scsi_dev_type_resume(dev);
161 /* Insert hooks here for targets, hosts, and transport classes */
166 static int scsi_runtime_idle(struct device *dev)
170 dev_dbg(dev, "scsi_runtime_idle\n");
172 /* Insert hooks here for targets, hosts, and transport classes */
174 if (scsi_is_sdev_device(dev))
175 err = pm_schedule_suspend(dev, 100);
177 err = pm_runtime_suspend(dev);
181 int scsi_autopm_get_device(struct scsi_device *sdev)
185 err = pm_runtime_get_sync(&sdev->sdev_gendev);
186 if (err < 0 && err !=-EACCES)
187 pm_runtime_put_sync(&sdev->sdev_gendev);
192 EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
194 void scsi_autopm_put_device(struct scsi_device *sdev)
196 pm_runtime_put_sync(&sdev->sdev_gendev);
198 EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
200 void scsi_autopm_get_target(struct scsi_target *starget)
202 pm_runtime_get_sync(&starget->dev);
205 void scsi_autopm_put_target(struct scsi_target *starget)
207 pm_runtime_put_sync(&starget->dev);
210 int scsi_autopm_get_host(struct Scsi_Host *shost)
214 err = pm_runtime_get_sync(&shost->shost_gendev);
215 if (err < 0 && err !=-EACCES)
216 pm_runtime_put_sync(&shost->shost_gendev);
222 void scsi_autopm_put_host(struct Scsi_Host *shost)
224 pm_runtime_put_sync(&shost->shost_gendev);
229 #define scsi_runtime_suspend NULL
230 #define scsi_runtime_resume NULL
231 #define scsi_runtime_idle NULL
233 #endif /* CONFIG_PM_RUNTIME */
235 const struct dev_pm_ops scsi_bus_pm_ops = {
236 .prepare = scsi_bus_prepare,
237 .suspend = scsi_bus_suspend,
238 .resume = scsi_bus_resume_common,
239 .freeze = scsi_bus_freeze,
240 .thaw = scsi_bus_resume_common,
241 .poweroff = scsi_bus_poweroff,
242 .restore = scsi_bus_resume_common,
243 .runtime_suspend = scsi_runtime_suspend,
244 .runtime_resume = scsi_runtime_resume,
245 .runtime_idle = scsi_runtime_idle,