Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / scsi / scsi_pm.c
blobd329f8b12e2b11503bd52b391bc198798aa1f297
1 /*
2 * scsi_pm.c Copyright (C) 2010 Alan Stern
4 * SCSI dynamic Power Management
5 * Initial version: Alan Stern <stern@rowland.harvard.edu>
6 */
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;
21 int err;
23 err = scsi_device_quiesce(to_scsi_device(dev));
24 if (err == 0) {
25 drv = dev->driver;
26 if (drv && drv->suspend)
27 err = drv->suspend(dev, msg);
29 dev_dbg(dev, "scsi suspend: %d\n", err);
30 return err;
33 static int scsi_dev_type_resume(struct device *dev)
35 struct device_driver *drv;
36 int err = 0;
38 drv = dev->driver;
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);
43 return err;
46 #ifdef CONFIG_PM_SLEEP
48 static int scsi_bus_suspend_common(struct device *dev, pm_message_t msg)
50 int err = 0;
52 if (scsi_is_sdev_device(dev))
53 err = scsi_dev_type_suspend(dev, msg);
54 return err;
57 static int scsi_bus_resume_common(struct device *dev)
59 int err = 0;
61 if (scsi_is_sdev_device(dev))
62 err = scsi_dev_type_resume(dev);
64 if (err == 0) {
65 pm_runtime_disable(dev);
66 pm_runtime_set_active(dev);
67 pm_runtime_enable(dev);
69 return err;
72 static int scsi_bus_suspend(struct device *dev)
74 return scsi_bus_suspend_common(dev, PMSG_SUSPEND);
77 static int scsi_bus_freeze(struct device *dev)
79 return scsi_bus_suspend_common(dev, PMSG_FREEZE);
82 static int scsi_bus_poweroff(struct device *dev)
84 return scsi_bus_suspend_common(dev, PMSG_HIBERNATE);
87 #else /* CONFIG_PM_SLEEP */
89 #define scsi_bus_resume_common NULL
90 #define scsi_bus_suspend NULL
91 #define scsi_bus_freeze NULL
92 #define scsi_bus_poweroff NULL
94 #endif /* CONFIG_PM_SLEEP */
96 #ifdef CONFIG_PM_RUNTIME
98 static int scsi_runtime_suspend(struct device *dev)
100 int err = 0;
102 dev_dbg(dev, "scsi_runtime_suspend\n");
103 if (scsi_is_sdev_device(dev)) {
104 err = scsi_dev_type_suspend(dev, PMSG_AUTO_SUSPEND);
105 if (err == -EAGAIN)
106 pm_schedule_suspend(dev, jiffies_to_msecs(
107 round_jiffies_up_relative(HZ/10)));
110 /* Insert hooks here for targets, hosts, and transport classes */
112 return err;
115 static int scsi_runtime_resume(struct device *dev)
117 int err = 0;
119 dev_dbg(dev, "scsi_runtime_resume\n");
120 if (scsi_is_sdev_device(dev))
121 err = scsi_dev_type_resume(dev);
123 /* Insert hooks here for targets, hosts, and transport classes */
125 return err;
128 static int scsi_runtime_idle(struct device *dev)
130 int err;
132 dev_dbg(dev, "scsi_runtime_idle\n");
134 /* Insert hooks here for targets, hosts, and transport classes */
136 if (scsi_is_sdev_device(dev))
137 err = pm_schedule_suspend(dev, 100);
138 else
139 err = pm_runtime_suspend(dev);
140 return err;
143 int scsi_autopm_get_device(struct scsi_device *sdev)
145 int err;
147 err = pm_runtime_get_sync(&sdev->sdev_gendev);
148 if (err < 0 && err !=-EACCES)
149 pm_runtime_put_sync(&sdev->sdev_gendev);
150 else
151 err = 0;
152 return err;
154 EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
156 void scsi_autopm_put_device(struct scsi_device *sdev)
158 pm_runtime_put_sync(&sdev->sdev_gendev);
160 EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
162 void scsi_autopm_get_target(struct scsi_target *starget)
164 pm_runtime_get_sync(&starget->dev);
167 void scsi_autopm_put_target(struct scsi_target *starget)
169 pm_runtime_put_sync(&starget->dev);
172 int scsi_autopm_get_host(struct Scsi_Host *shost)
174 int err;
176 err = pm_runtime_get_sync(&shost->shost_gendev);
177 if (err < 0 && err !=-EACCES)
178 pm_runtime_put_sync(&shost->shost_gendev);
179 else
180 err = 0;
181 return err;
184 void scsi_autopm_put_host(struct Scsi_Host *shost)
186 pm_runtime_put_sync(&shost->shost_gendev);
189 #else
191 #define scsi_runtime_suspend NULL
192 #define scsi_runtime_resume NULL
193 #define scsi_runtime_idle NULL
195 #endif /* CONFIG_PM_RUNTIME */
197 const struct dev_pm_ops scsi_bus_pm_ops = {
198 .suspend = scsi_bus_suspend,
199 .resume = scsi_bus_resume_common,
200 .freeze = scsi_bus_freeze,
201 .thaw = scsi_bus_resume_common,
202 .poweroff = scsi_bus_poweroff,
203 .restore = scsi_bus_resume_common,
204 .runtime_suspend = scsi_runtime_suspend,
205 .runtime_resume = scsi_runtime_resume,
206 .runtime_idle = scsi_runtime_idle,