Commit | Line | Data |
---|---|---|
db5bd1e0 AS |
1 | /* |
2 | * scsi_pm.c Copyright (C) 2010 Alan Stern | |
3 | * | |
4 | * SCSI dynamic Power Management | |
5 | * Initial version: Alan Stern <stern@rowland.harvard.edu> | |
6 | */ | |
7 | ||
8 | #include <linux/pm_runtime.h> | |
09703660 | 9 | #include <linux/export.h> |
db5bd1e0 AS |
10 | |
11 | #include <scsi/scsi.h> | |
12 | #include <scsi/scsi_device.h> | |
13 | #include <scsi/scsi_driver.h> | |
14 | #include <scsi/scsi_host.h> | |
15 | ||
16 | #include "scsi_priv.h" | |
17 | ||
18 | static int scsi_dev_type_suspend(struct device *dev, pm_message_t msg) | |
19 | { | |
20 | struct device_driver *drv; | |
21 | int err; | |
22 | ||
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); | |
28 | } | |
29 | dev_dbg(dev, "scsi suspend: %d\n", err); | |
30 | return err; | |
31 | } | |
32 | ||
33 | static int scsi_dev_type_resume(struct device *dev) | |
34 | { | |
35 | struct device_driver *drv; | |
36 | int err = 0; | |
37 | ||
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; | |
44 | } | |
45 | ||
46 | #ifdef CONFIG_PM_SLEEP | |
47 | ||
48 | static int scsi_bus_suspend_common(struct device *dev, pm_message_t msg) | |
49 | { | |
50 | int err = 0; | |
51 | ||
52 | if (scsi_is_sdev_device(dev)) | |
53 | err = scsi_dev_type_suspend(dev, msg); | |
54 | return err; | |
55 | } | |
56 | ||
57 | static int scsi_bus_resume_common(struct device *dev) | |
58 | { | |
59 | int err = 0; | |
60 | ||
61 | if (scsi_is_sdev_device(dev)) | |
62 | err = scsi_dev_type_resume(dev); | |
bc4f2401 AS |
63 | |
64 | if (err == 0) { | |
65 | pm_runtime_disable(dev); | |
66 | pm_runtime_set_active(dev); | |
67 | pm_runtime_enable(dev); | |
68 | } | |
db5bd1e0 AS |
69 | return err; |
70 | } | |
71 | ||
72 | static int scsi_bus_suspend(struct device *dev) | |
73 | { | |
74 | return scsi_bus_suspend_common(dev, PMSG_SUSPEND); | |
75 | } | |
76 | ||
77 | static int scsi_bus_freeze(struct device *dev) | |
78 | { | |
79 | return scsi_bus_suspend_common(dev, PMSG_FREEZE); | |
80 | } | |
81 | ||
82 | static int scsi_bus_poweroff(struct device *dev) | |
83 | { | |
84 | return scsi_bus_suspend_common(dev, PMSG_HIBERNATE); | |
85 | } | |
86 | ||
87 | #else /* CONFIG_PM_SLEEP */ | |
88 | ||
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 | |
93 | ||
94 | #endif /* CONFIG_PM_SLEEP */ | |
95 | ||
bc4f2401 AS |
96 | #ifdef CONFIG_PM_RUNTIME |
97 | ||
98 | static int scsi_runtime_suspend(struct device *dev) | |
99 | { | |
100 | int err = 0; | |
101 | ||
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))); | |
108 | } | |
109 | ||
110 | /* Insert hooks here for targets, hosts, and transport classes */ | |
111 | ||
112 | return err; | |
113 | } | |
114 | ||
115 | static int scsi_runtime_resume(struct device *dev) | |
116 | { | |
117 | int err = 0; | |
118 | ||
119 | dev_dbg(dev, "scsi_runtime_resume\n"); | |
120 | if (scsi_is_sdev_device(dev)) | |
121 | err = scsi_dev_type_resume(dev); | |
122 | ||
123 | /* Insert hooks here for targets, hosts, and transport classes */ | |
124 | ||
125 | return err; | |
126 | } | |
127 | ||
128 | static int scsi_runtime_idle(struct device *dev) | |
129 | { | |
130 | int err; | |
131 | ||
132 | dev_dbg(dev, "scsi_runtime_idle\n"); | |
133 | ||
134 | /* Insert hooks here for targets, hosts, and transport classes */ | |
135 | ||
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; | |
141 | } | |
142 | ||
143 | int scsi_autopm_get_device(struct scsi_device *sdev) | |
144 | { | |
145 | int err; | |
146 | ||
147 | err = pm_runtime_get_sync(&sdev->sdev_gendev); | |
632e270e | 148 | if (err < 0 && err !=-EACCES) |
bc4f2401 | 149 | pm_runtime_put_sync(&sdev->sdev_gendev); |
632e270e | 150 | else |
bc4f2401 AS |
151 | err = 0; |
152 | return err; | |
153 | } | |
154 | EXPORT_SYMBOL_GPL(scsi_autopm_get_device); | |
155 | ||
156 | void scsi_autopm_put_device(struct scsi_device *sdev) | |
157 | { | |
158 | pm_runtime_put_sync(&sdev->sdev_gendev); | |
159 | } | |
160 | EXPORT_SYMBOL_GPL(scsi_autopm_put_device); | |
161 | ||
162 | void scsi_autopm_get_target(struct scsi_target *starget) | |
163 | { | |
164 | pm_runtime_get_sync(&starget->dev); | |
165 | } | |
166 | ||
167 | void scsi_autopm_put_target(struct scsi_target *starget) | |
168 | { | |
169 | pm_runtime_put_sync(&starget->dev); | |
170 | } | |
171 | ||
172 | int scsi_autopm_get_host(struct Scsi_Host *shost) | |
173 | { | |
174 | int err; | |
175 | ||
176 | err = pm_runtime_get_sync(&shost->shost_gendev); | |
632e270e | 177 | if (err < 0 && err !=-EACCES) |
bc4f2401 | 178 | pm_runtime_put_sync(&shost->shost_gendev); |
632e270e | 179 | else |
bc4f2401 AS |
180 | err = 0; |
181 | return err; | |
182 | } | |
183 | ||
184 | void scsi_autopm_put_host(struct Scsi_Host *shost) | |
185 | { | |
186 | pm_runtime_put_sync(&shost->shost_gendev); | |
187 | } | |
188 | ||
189 | #else | |
190 | ||
191 | #define scsi_runtime_suspend NULL | |
192 | #define scsi_runtime_resume NULL | |
193 | #define scsi_runtime_idle NULL | |
194 | ||
195 | #endif /* CONFIG_PM_RUNTIME */ | |
196 | ||
db5bd1e0 AS |
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, | |
bc4f2401 AS |
204 | .runtime_suspend = scsi_runtime_suspend, |
205 | .runtime_resume = scsi_runtime_resume, | |
206 | .runtime_idle = scsi_runtime_idle, | |
db5bd1e0 | 207 | }; |