blob: ebc193f7f7ddbc626e98e36f9fe622211a52be25 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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>
9#include <linux/export.h>
10#include <linux/async.h>
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
19#ifdef CONFIG_PM_SLEEP
20
21static int do_scsi_suspend(struct device *dev, const struct dev_pm_ops *pm)
22{
23 return pm && pm->suspend ? pm->suspend(dev) : 0;
24}
25
26static 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
31static 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
36static 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
41static 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
46static 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
51static 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;
55 int err;
56
57 /* flush pending in-flight resume operations, suspend is synchronous */
58 async_synchronize_full_domain(&scsi_sd_pm_domain);
59
60 err = scsi_device_quiesce(to_scsi_device(dev));
61 if (err == 0) {
62 err = cb(dev, pm);
63 if (err)
64 scsi_device_resume(to_scsi_device(dev));
65 }
66 dev_dbg(dev, "scsi suspend: %d\n", err);
67 return err;
68}
69
70static int scsi_dev_type_resume(struct device *dev,
71 int (*cb)(struct device *, const struct dev_pm_ops *))
72{
73 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
74 int err = 0;
75
76 err = cb(dev, pm);
77 scsi_device_resume(to_scsi_device(dev));
78 dev_dbg(dev, "scsi resume: %d\n", err);
79
80 if (err == 0) {
81 pm_runtime_disable(dev);
82 err = pm_runtime_set_active(dev);
83 pm_runtime_enable(dev);
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 }
98 }
99
100 return err;
101}
102
103static int
104scsi_bus_suspend_common(struct device *dev,
105 int (*cb)(struct device *, const struct dev_pm_ops *))
106{
107 int err = 0;
108
109 if (scsi_is_sdev_device(dev)) {
110 /*
111 * All the high-level SCSI drivers that implement runtime
112 * PM treat runtime suspend, system suspend, and system
113 * hibernate nearly identically. In all cases the requirements
114 * for runtime suspension are stricter.
115 */
116 if (pm_runtime_suspended(dev))
117 return 0;
118
119 err = scsi_dev_type_suspend(dev, cb);
120 }
121
122 return err;
123}
124
125static void async_sdev_resume(void *dev, async_cookie_t cookie)
126{
127 scsi_dev_type_resume(dev, do_scsi_resume);
128}
129
130static void async_sdev_thaw(void *dev, async_cookie_t cookie)
131{
132 scsi_dev_type_resume(dev, do_scsi_thaw);
133}
134
135static void async_sdev_restore(void *dev, async_cookie_t cookie)
136{
137 scsi_dev_type_resume(dev, do_scsi_restore);
138}
139
140static 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 {
168 pm_runtime_disable(dev);
169 pm_runtime_set_active(dev);
170 pm_runtime_enable(dev);
171 }
172 return 0;
173}
174
175static 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. */
179 async_synchronize_full_domain(&scsi_sd_probe_domain);
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
188static int scsi_bus_suspend(struct device *dev)
189{
190 return scsi_bus_suspend_common(dev, do_scsi_suspend);
191}
192
193static int scsi_bus_resume(struct device *dev)
194{
195 return scsi_bus_resume_common(dev, do_scsi_resume);
196}
197
198static int scsi_bus_freeze(struct device *dev)
199{
200 return scsi_bus_suspend_common(dev, do_scsi_freeze);
201}
202
203static int scsi_bus_thaw(struct device *dev)
204{
205 return scsi_bus_resume_common(dev, do_scsi_thaw);
206}
207
208static int scsi_bus_poweroff(struct device *dev)
209{
210 return scsi_bus_suspend_common(dev, do_scsi_poweroff);
211}
212
213static int scsi_bus_restore(struct device *dev)
214{
215 return scsi_bus_resume_common(dev, do_scsi_restore);
216}
217
218#else /* CONFIG_PM_SLEEP */
219
220#define scsi_bus_prepare NULL
221#define scsi_bus_suspend NULL
222#define scsi_bus_resume NULL
223#define scsi_bus_freeze NULL
224#define scsi_bus_thaw NULL
225#define scsi_bus_poweroff NULL
226#define scsi_bus_restore NULL
227
228#endif /* CONFIG_PM_SLEEP */
229
230static int sdev_runtime_suspend(struct device *dev)
231{
232 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
233 struct scsi_device *sdev = to_scsi_device(dev);
234 int err = 0;
235
236 err = blk_pre_runtime_suspend(sdev->request_queue);
237 if (err)
238 return err;
239 if (pm && pm->runtime_suspend)
240 err = pm->runtime_suspend(dev);
241 blk_post_runtime_suspend(sdev->request_queue, err);
242
243 return err;
244}
245
246static int scsi_runtime_suspend(struct device *dev)
247{
248 int err = 0;
249
250 dev_dbg(dev, "scsi_runtime_suspend\n");
251 if (scsi_is_sdev_device(dev))
252 err = sdev_runtime_suspend(dev);
253
254 /* Insert hooks here for targets, hosts, and transport classes */
255
256 return err;
257}
258
259static int sdev_runtime_resume(struct device *dev)
260{
261 struct scsi_device *sdev = to_scsi_device(dev);
262 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
263 int err = 0;
264
265 blk_pre_runtime_resume(sdev->request_queue);
266 if (pm && pm->runtime_resume)
267 err = pm->runtime_resume(dev);
268 blk_post_runtime_resume(sdev->request_queue, err);
269
270 return err;
271}
272
273static int scsi_runtime_resume(struct device *dev)
274{
275 int err = 0;
276
277 dev_dbg(dev, "scsi_runtime_resume\n");
278 if (scsi_is_sdev_device(dev))
279 err = sdev_runtime_resume(dev);
280
281 /* Insert hooks here for targets, hosts, and transport classes */
282
283 return err;
284}
285
286static int scsi_runtime_idle(struct device *dev)
287{
288 dev_dbg(dev, "scsi_runtime_idle\n");
289
290 /* Insert hooks here for targets, hosts, and transport classes */
291
292 if (scsi_is_sdev_device(dev)) {
293 pm_runtime_mark_last_busy(dev);
294 pm_runtime_autosuspend(dev);
295 return -EBUSY;
296 }
297
298 return 0;
299}
300
301int scsi_autopm_get_device(struct scsi_device *sdev)
302{
303 int err;
304
305 err = pm_runtime_get_sync(&sdev->sdev_gendev);
306 if (err < 0 && err !=-EACCES)
307 pm_runtime_put_sync(&sdev->sdev_gendev);
308 else
309 err = 0;
310 return err;
311}
312EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
313
314void scsi_autopm_put_device(struct scsi_device *sdev)
315{
316 pm_runtime_put_sync(&sdev->sdev_gendev);
317}
318EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
319
320void scsi_autopm_get_target(struct scsi_target *starget)
321{
322 pm_runtime_get_sync(&starget->dev);
323}
324
325void scsi_autopm_put_target(struct scsi_target *starget)
326{
327 pm_runtime_put_sync(&starget->dev);
328}
329
330int scsi_autopm_get_host(struct Scsi_Host *shost)
331{
332 int err;
333
334 err = pm_runtime_get_sync(&shost->shost_gendev);
335 if (err < 0 && err !=-EACCES)
336 pm_runtime_put_sync(&shost->shost_gendev);
337 else
338 err = 0;
339 return err;
340}
341
342void scsi_autopm_put_host(struct Scsi_Host *shost)
343{
344 pm_runtime_put_sync(&shost->shost_gendev);
345}
346
347const struct dev_pm_ops scsi_bus_pm_ops = {
348 .prepare = scsi_bus_prepare,
349 .suspend = scsi_bus_suspend,
350 .resume = scsi_bus_resume,
351 .freeze = scsi_bus_freeze,
352 .thaw = scsi_bus_thaw,
353 .poweroff = scsi_bus_poweroff,
354 .restore = scsi_bus_restore,
355 .runtime_suspend = scsi_runtime_suspend,
356 .runtime_resume = scsi_runtime_resume,
357 .runtime_idle = scsi_runtime_idle,
358};