]>
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> |
fea6d607 | 10 | #include <linux/async.h> |
db5bd1e0 AS |
11 | |
12 | #include <scsi/scsi.h> | |
13 | #include <scsi/scsi_device.h> | |
14 | #include <scsi/scsi_driver.h> | |
15 | #include <scsi/scsi_host.h> | |
16 | ||
17 | #include "scsi_priv.h" | |
18 | ||
6627b38f AL |
19 | #ifdef CONFIG_PM_SLEEP |
20 | ||
3c31b52f | 21 | static int do_scsi_suspend(struct device *dev, const struct dev_pm_ops *pm) |
db5bd1e0 | 22 | { |
3c31b52f DW |
23 | return pm && pm->suspend ? pm->suspend(dev) : 0; |
24 | } | |
25 | ||
26 | static int do_scsi_freeze(struct device *dev, const struct dev_pm_ops *pm) | |
27 | { | |
28 | return pm && pm->freeze ? pm->freeze(dev) : 0; | |
29 | } | |
30 | ||
31 | static int do_scsi_poweroff(struct device *dev, const struct dev_pm_ops *pm) | |
32 | { | |
33 | return pm && pm->poweroff ? pm->poweroff(dev) : 0; | |
34 | } | |
35 | ||
36 | static int do_scsi_resume(struct device *dev, const struct dev_pm_ops *pm) | |
37 | { | |
38 | return pm && pm->resume ? pm->resume(dev) : 0; | |
39 | } | |
40 | ||
41 | static int do_scsi_thaw(struct device *dev, const struct dev_pm_ops *pm) | |
42 | { | |
43 | return pm && pm->thaw ? pm->thaw(dev) : 0; | |
44 | } | |
45 | ||
46 | static int do_scsi_restore(struct device *dev, const struct dev_pm_ops *pm) | |
47 | { | |
48 | return pm && pm->restore ? pm->restore(dev) : 0; | |
49 | } | |
50 | ||
51 | static int scsi_dev_type_suspend(struct device *dev, | |
52 | int (*cb)(struct device *, const struct dev_pm_ops *)) | |
53 | { | |
54 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; | |
db5bd1e0 AS |
55 | int err; |
56 | ||
3c31b52f DW |
57 | /* flush pending in-flight resume operations, suspend is synchronous */ |
58 | async_synchronize_full_domain(&scsi_sd_pm_domain); | |
59 | ||
db5bd1e0 AS |
60 | err = scsi_device_quiesce(to_scsi_device(dev)); |
61 | if (err == 0) { | |
3c31b52f DW |
62 | err = cb(dev, pm); |
63 | if (err) | |
64 | scsi_device_resume(to_scsi_device(dev)); | |
db5bd1e0 AS |
65 | } |
66 | dev_dbg(dev, "scsi suspend: %d\n", err); | |
67 | return err; | |
68 | } | |
69 | ||
3c31b52f DW |
70 | static int scsi_dev_type_resume(struct device *dev, |
71 | int (*cb)(struct device *, const struct dev_pm_ops *)) | |
db5bd1e0 | 72 | { |
3c31b52f | 73 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
db5bd1e0 AS |
74 | int err = 0; |
75 | ||
3c31b52f | 76 | err = cb(dev, pm); |
db5bd1e0 AS |
77 | scsi_device_resume(to_scsi_device(dev)); |
78 | dev_dbg(dev, "scsi resume: %d\n", err); | |
3c31b52f DW |
79 | |
80 | if (err == 0) { | |
81 | pm_runtime_disable(dev); | |
71add444 | 82 | err = pm_runtime_set_active(dev); |
3c31b52f | 83 | pm_runtime_enable(dev); |
71add444 SC |
84 | |
85 | /* | |
86 | * Forcibly set runtime PM status of request queue to "active" | |
87 | * to make sure we can again get requests from the queue | |
88 | * (see also blk_pm_peek_request()). | |
89 | * | |
90 | * The resume hook will correct runtime PM status of the disk. | |
91 | */ | |
92 | if (!err && scsi_is_sdev_device(dev)) { | |
93 | struct scsi_device *sdev = to_scsi_device(dev); | |
94 | ||
95 | if (sdev->request_queue->dev) | |
96 | blk_set_runtime_active(sdev->request_queue); | |
97 | } | |
3c31b52f DW |
98 | } |
99 | ||
db5bd1e0 AS |
100 | return err; |
101 | } | |
102 | ||
80d2fd48 | 103 | static int |
3c31b52f DW |
104 | scsi_bus_suspend_common(struct device *dev, |
105 | int (*cb)(struct device *, const struct dev_pm_ops *)) | |
db5bd1e0 AS |
106 | { |
107 | int err = 0; | |
108 | ||
28640516 LM |
109 | if (scsi_is_sdev_device(dev)) { |
110 | /* | |
80d2fd48 AL |
111 | * All the high-level SCSI drivers that implement runtime |
112 | * PM treat runtime suspend, system suspend, and system | |
95897910 ON |
113 | * hibernate nearly identically. In all cases the requirements |
114 | * for runtime suspension are stricter. | |
28640516 | 115 | */ |
80d2fd48 AL |
116 | if (pm_runtime_suspended(dev)) |
117 | return 0; | |
28640516 | 118 | |
80d2fd48 | 119 | err = scsi_dev_type_suspend(dev, cb); |
28640516 | 120 | } |
80d2fd48 | 121 | |
db5bd1e0 AS |
122 | return err; |
123 | } | |
124 | ||
3c31b52f | 125 | static void async_sdev_resume(void *dev, async_cookie_t cookie) |
db5bd1e0 | 126 | { |
3c31b52f DW |
127 | scsi_dev_type_resume(dev, do_scsi_resume); |
128 | } | |
db5bd1e0 | 129 | |
3c31b52f DW |
130 | static void async_sdev_thaw(void *dev, async_cookie_t cookie) |
131 | { | |
132 | scsi_dev_type_resume(dev, do_scsi_thaw); | |
133 | } | |
63347905 | 134 | |
3c31b52f DW |
135 | static void async_sdev_restore(void *dev, async_cookie_t cookie) |
136 | { | |
137 | scsi_dev_type_resume(dev, do_scsi_restore); | |
138 | } | |
139 | ||
140 | static int scsi_bus_resume_common(struct device *dev, | |
141 | int (*cb)(struct device *, const struct dev_pm_ops *)) | |
142 | { | |
143 | async_func_t fn; | |
144 | ||
145 | if (!scsi_is_sdev_device(dev)) | |
146 | fn = NULL; | |
147 | else if (cb == do_scsi_resume) | |
148 | fn = async_sdev_resume; | |
149 | else if (cb == do_scsi_thaw) | |
150 | fn = async_sdev_thaw; | |
151 | else if (cb == do_scsi_restore) | |
152 | fn = async_sdev_restore; | |
153 | else | |
154 | fn = NULL; | |
155 | ||
156 | if (fn) { | |
157 | async_schedule_domain(fn, dev, &scsi_sd_pm_domain); | |
158 | ||
159 | /* | |
160 | * If a user has disabled async probing a likely reason | |
161 | * is due to a storage enclosure that does not inject | |
162 | * staggered spin-ups. For safety, make resume | |
163 | * synchronous as well in that case. | |
164 | */ | |
165 | if (strncmp(scsi_scan_type, "async", 5) != 0) | |
166 | async_synchronize_full_domain(&scsi_sd_pm_domain); | |
167 | } else { | |
bc4f2401 AS |
168 | pm_runtime_disable(dev); |
169 | pm_runtime_set_active(dev); | |
170 | pm_runtime_enable(dev); | |
171 | } | |
3c31b52f | 172 | return 0; |
db5bd1e0 AS |
173 | } |
174 | ||
fea6d607 AS |
175 | static int scsi_bus_prepare(struct device *dev) |
176 | { | |
177 | if (scsi_is_sdev_device(dev)) { | |
178 | /* sd probing uses async_schedule. Wait until it finishes. */ | |
a7a20d10 | 179 | async_synchronize_full_domain(&scsi_sd_probe_domain); |
fea6d607 AS |
180 | |
181 | } else if (scsi_is_host_device(dev)) { | |
182 | /* Wait until async scanning is finished */ | |
183 | scsi_complete_async_scans(); | |
184 | } | |
185 | return 0; | |
186 | } | |
187 | ||
db5bd1e0 AS |
188 | static int scsi_bus_suspend(struct device *dev) |
189 | { | |
3c31b52f | 190 | return scsi_bus_suspend_common(dev, do_scsi_suspend); |
80d2fd48 AL |
191 | } |
192 | ||
193 | static int scsi_bus_resume(struct device *dev) | |
194 | { | |
3c31b52f | 195 | return scsi_bus_resume_common(dev, do_scsi_resume); |
db5bd1e0 AS |
196 | } |
197 | ||
198 | static int scsi_bus_freeze(struct device *dev) | |
199 | { | |
3c31b52f | 200 | return scsi_bus_suspend_common(dev, do_scsi_freeze); |
80d2fd48 AL |
201 | } |
202 | ||
203 | static int scsi_bus_thaw(struct device *dev) | |
204 | { | |
3c31b52f | 205 | return scsi_bus_resume_common(dev, do_scsi_thaw); |
db5bd1e0 AS |
206 | } |
207 | ||
208 | static int scsi_bus_poweroff(struct device *dev) | |
209 | { | |
3c31b52f | 210 | return scsi_bus_suspend_common(dev, do_scsi_poweroff); |
80d2fd48 AL |
211 | } |
212 | ||
213 | static int scsi_bus_restore(struct device *dev) | |
214 | { | |
3c31b52f | 215 | return scsi_bus_resume_common(dev, do_scsi_restore); |
db5bd1e0 AS |
216 | } |
217 | ||
218 | #else /* CONFIG_PM_SLEEP */ | |
219 | ||
fea6d607 | 220 | #define scsi_bus_prepare NULL |
db5bd1e0 | 221 | #define scsi_bus_suspend NULL |
80d2fd48 | 222 | #define scsi_bus_resume NULL |
db5bd1e0 | 223 | #define scsi_bus_freeze NULL |
80d2fd48 | 224 | #define scsi_bus_thaw NULL |
db5bd1e0 | 225 | #define scsi_bus_poweroff NULL |
80d2fd48 | 226 | #define scsi_bus_restore NULL |
db5bd1e0 AS |
227 | |
228 | #endif /* CONFIG_PM_SLEEP */ | |
229 | ||
6627b38f | 230 | static int sdev_runtime_suspend(struct device *dev) |
6df339a5 | 231 | { |
6627b38f AL |
232 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
233 | struct scsi_device *sdev = to_scsi_device(dev); | |
49718f0f | 234 | int err = 0; |
6df339a5 | 235 | |
1c69d3b6 KX |
236 | err = blk_pre_runtime_suspend(sdev->request_queue); |
237 | if (err) | |
238 | return err; | |
239 | if (pm && pm->runtime_suspend) | |
6627b38f | 240 | err = pm->runtime_suspend(dev); |
1c69d3b6 KX |
241 | blk_post_runtime_suspend(sdev->request_queue, err); |
242 | ||
6df339a5 LM |
243 | return err; |
244 | } | |
245 | ||
bc4f2401 AS |
246 | static int scsi_runtime_suspend(struct device *dev) |
247 | { | |
248 | int err = 0; | |
249 | ||
250 | dev_dbg(dev, "scsi_runtime_suspend\n"); | |
6df339a5 LM |
251 | if (scsi_is_sdev_device(dev)) |
252 | err = sdev_runtime_suspend(dev); | |
bc4f2401 AS |
253 | |
254 | /* Insert hooks here for targets, hosts, and transport classes */ | |
255 | ||
256 | return err; | |
257 | } | |
258 | ||
6627b38f | 259 | static int sdev_runtime_resume(struct device *dev) |
bc4f2401 | 260 | { |
6627b38f AL |
261 | struct scsi_device *sdev = to_scsi_device(dev); |
262 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; | |
bc4f2401 | 263 | int err = 0; |
6df339a5 | 264 | |
1c69d3b6 KX |
265 | blk_pre_runtime_resume(sdev->request_queue); |
266 | if (pm && pm->runtime_resume) | |
6627b38f | 267 | err = pm->runtime_resume(dev); |
1c69d3b6 KX |
268 | blk_post_runtime_resume(sdev->request_queue, err); |
269 | ||
6df339a5 LM |
270 | return err; |
271 | } | |
272 | ||
6df339a5 LM |
273 | static int scsi_runtime_resume(struct device *dev) |
274 | { | |
275 | int err = 0; | |
bc4f2401 AS |
276 | |
277 | dev_dbg(dev, "scsi_runtime_resume\n"); | |
278 | if (scsi_is_sdev_device(dev)) | |
6df339a5 | 279 | err = sdev_runtime_resume(dev); |
bc4f2401 AS |
280 | |
281 | /* Insert hooks here for targets, hosts, and transport classes */ | |
282 | ||
283 | return err; | |
284 | } | |
285 | ||
286 | static int scsi_runtime_idle(struct device *dev) | |
287 | { | |
bc4f2401 AS |
288 | dev_dbg(dev, "scsi_runtime_idle\n"); |
289 | ||
290 | /* Insert hooks here for targets, hosts, and transport classes */ | |
291 | ||
6df339a5 | 292 | if (scsi_is_sdev_device(dev)) { |
6627b38f AL |
293 | pm_runtime_mark_last_busy(dev); |
294 | pm_runtime_autosuspend(dev); | |
295 | return -EBUSY; | |
6df339a5 | 296 | } |
6627b38f | 297 | |
45f0a85c | 298 | return 0; |
bc4f2401 AS |
299 | } |
300 | ||
301 | int scsi_autopm_get_device(struct scsi_device *sdev) | |
302 | { | |
303 | int err; | |
304 | ||
305 | err = pm_runtime_get_sync(&sdev->sdev_gendev); | |
632e270e | 306 | if (err < 0 && err !=-EACCES) |
bc4f2401 | 307 | pm_runtime_put_sync(&sdev->sdev_gendev); |
632e270e | 308 | else |
bc4f2401 AS |
309 | err = 0; |
310 | return err; | |
311 | } | |
312 | EXPORT_SYMBOL_GPL(scsi_autopm_get_device); | |
313 | ||
314 | void scsi_autopm_put_device(struct scsi_device *sdev) | |
315 | { | |
316 | pm_runtime_put_sync(&sdev->sdev_gendev); | |
317 | } | |
318 | EXPORT_SYMBOL_GPL(scsi_autopm_put_device); | |
319 | ||
320 | void scsi_autopm_get_target(struct scsi_target *starget) | |
321 | { | |
322 | pm_runtime_get_sync(&starget->dev); | |
323 | } | |
324 | ||
325 | void scsi_autopm_put_target(struct scsi_target *starget) | |
326 | { | |
327 | pm_runtime_put_sync(&starget->dev); | |
328 | } | |
329 | ||
330 | int scsi_autopm_get_host(struct Scsi_Host *shost) | |
331 | { | |
332 | int err; | |
333 | ||
334 | err = pm_runtime_get_sync(&shost->shost_gendev); | |
632e270e | 335 | if (err < 0 && err !=-EACCES) |
bc4f2401 | 336 | pm_runtime_put_sync(&shost->shost_gendev); |
632e270e | 337 | else |
bc4f2401 AS |
338 | err = 0; |
339 | return err; | |
340 | } | |
341 | ||
342 | void scsi_autopm_put_host(struct Scsi_Host *shost) | |
343 | { | |
344 | pm_runtime_put_sync(&shost->shost_gendev); | |
345 | } | |
346 | ||
db5bd1e0 | 347 | const struct dev_pm_ops scsi_bus_pm_ops = { |
fea6d607 | 348 | .prepare = scsi_bus_prepare, |
db5bd1e0 | 349 | .suspend = scsi_bus_suspend, |
80d2fd48 | 350 | .resume = scsi_bus_resume, |
db5bd1e0 | 351 | .freeze = scsi_bus_freeze, |
80d2fd48 | 352 | .thaw = scsi_bus_thaw, |
db5bd1e0 | 353 | .poweroff = scsi_bus_poweroff, |
80d2fd48 | 354 | .restore = scsi_bus_restore, |
bc4f2401 AS |
355 | .runtime_suspend = scsi_runtime_suspend, |
356 | .runtime_resume = scsi_runtime_resume, | |
357 | .runtime_idle = scsi_runtime_idle, | |
db5bd1e0 | 358 | }; |