blob: 5f0ad8b32e3af5900b571ae3ffa828f5e85eb6fb [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Alan Sterndb5bd1e2010-06-17 10:36:49 -04002/*
3 * scsi_pm.c Copyright (C) 2010 Alan Stern
4 *
5 * SCSI dynamic Power Management
6 * Initial version: Alan Stern <stern@rowland.harvard.edu>
7 */
8
9#include <linux/pm_runtime.h>
Paul Gortmaker09703662011-05-27 09:37:25 -040010#include <linux/export.h>
Alan Sternfea6d602012-02-17 16:25:08 -050011#include <linux/async.h>
Bart Van Asschebca6b062018-09-26 14:01:03 -070012#include <linux/blk-pm.h>
Alan Sterndb5bd1e2010-06-17 10:36:49 -040013
14#include <scsi/scsi.h>
15#include <scsi/scsi_device.h>
16#include <scsi/scsi_driver.h>
17#include <scsi/scsi_host.h>
18
19#include "scsi_priv.h"
20
Aaron Lu6627b382013-10-28 15:27:49 +080021#ifdef CONFIG_PM_SLEEP
22
Dan Williams3c31b522014-04-10 15:30:35 -070023static int do_scsi_suspend(struct device *dev, const struct dev_pm_ops *pm)
Alan Sterndb5bd1e2010-06-17 10:36:49 -040024{
Dan Williams3c31b522014-04-10 15:30:35 -070025 return pm && pm->suspend ? pm->suspend(dev) : 0;
26}
27
28static int do_scsi_freeze(struct device *dev, const struct dev_pm_ops *pm)
29{
30 return pm && pm->freeze ? pm->freeze(dev) : 0;
31}
32
33static int do_scsi_poweroff(struct device *dev, const struct dev_pm_ops *pm)
34{
35 return pm && pm->poweroff ? pm->poweroff(dev) : 0;
36}
37
38static int do_scsi_resume(struct device *dev, const struct dev_pm_ops *pm)
39{
40 return pm && pm->resume ? pm->resume(dev) : 0;
41}
42
43static int do_scsi_thaw(struct device *dev, const struct dev_pm_ops *pm)
44{
45 return pm && pm->thaw ? pm->thaw(dev) : 0;
46}
47
48static int do_scsi_restore(struct device *dev, const struct dev_pm_ops *pm)
49{
50 return pm && pm->restore ? pm->restore(dev) : 0;
51}
52
53static int scsi_dev_type_suspend(struct device *dev,
54 int (*cb)(struct device *, const struct dev_pm_ops *))
55{
56 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Alan Sterndb5bd1e2010-06-17 10:36:49 -040057 int err;
58
Dan Williams3c31b522014-04-10 15:30:35 -070059 /* flush pending in-flight resume operations, suspend is synchronous */
60 async_synchronize_full_domain(&scsi_sd_pm_domain);
61
Alan Sterndb5bd1e2010-06-17 10:36:49 -040062 err = scsi_device_quiesce(to_scsi_device(dev));
63 if (err == 0) {
Dan Williams3c31b522014-04-10 15:30:35 -070064 err = cb(dev, pm);
65 if (err)
66 scsi_device_resume(to_scsi_device(dev));
Alan Sterndb5bd1e2010-06-17 10:36:49 -040067 }
68 dev_dbg(dev, "scsi suspend: %d\n", err);
69 return err;
70}
71
Dan Williams3c31b522014-04-10 15:30:35 -070072static int scsi_dev_type_resume(struct device *dev,
73 int (*cb)(struct device *, const struct dev_pm_ops *))
Alan Sterndb5bd1e2010-06-17 10:36:49 -040074{
Dan Williams3c31b522014-04-10 15:30:35 -070075 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Alan Sterndb5bd1e2010-06-17 10:36:49 -040076 int err = 0;
77
Dan Williams3c31b522014-04-10 15:30:35 -070078 err = cb(dev, pm);
Alan Sterndb5bd1e2010-06-17 10:36:49 -040079 scsi_device_resume(to_scsi_device(dev));
80 dev_dbg(dev, "scsi resume: %d\n", err);
Dan Williams3c31b522014-04-10 15:30:35 -070081
82 if (err == 0) {
Can Guo05d18ae2020-05-05 21:55:35 -070083 bool was_runtime_suspended;
84
85 was_runtime_suspended = pm_runtime_suspended(dev);
86
Dan Williams3c31b522014-04-10 15:30:35 -070087 pm_runtime_disable(dev);
Stanley Chu3f7e62b2019-01-03 22:08:05 +080088 err = pm_runtime_set_active(dev);
Dan Williams3c31b522014-04-10 15:30:35 -070089 pm_runtime_enable(dev);
Stanley Chu3f7e62b2019-01-03 22:08:05 +080090
91 /*
92 * Forcibly set runtime PM status of request queue to "active"
93 * to make sure we can again get requests from the queue
94 * (see also blk_pm_peek_request()).
95 *
96 * The resume hook will correct runtime PM status of the disk.
97 */
98 if (!err && scsi_is_sdev_device(dev)) {
99 struct scsi_device *sdev = to_scsi_device(dev);
Can Guo05d18ae2020-05-05 21:55:35 -0700100 if (was_runtime_suspended)
101 blk_post_runtime_resume(sdev->request_queue, 0);
102 else
103 blk_set_runtime_active(sdev->request_queue);
Stanley Chu3f7e62b2019-01-03 22:08:05 +0800104 }
Dan Williams3c31b522014-04-10 15:30:35 -0700105 }
106
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400107 return err;
108}
109
Aaron Lu80d2fd42012-11-09 15:27:54 +0800110static int
Dan Williams3c31b522014-04-10 15:30:35 -0700111scsi_bus_suspend_common(struct device *dev,
112 int (*cb)(struct device *, const struct dev_pm_ops *))
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400113{
114 int err = 0;
115
Lin Ming28640512011-12-05 09:20:25 +0800116 if (scsi_is_sdev_device(dev)) {
117 /*
Aaron Lu80d2fd42012-11-09 15:27:54 +0800118 * All the high-level SCSI drivers that implement runtime
119 * PM treat runtime suspend, system suspend, and system
Oliver Neukum95897912013-09-16 13:28:15 +0200120 * hibernate nearly identically. In all cases the requirements
121 * for runtime suspension are stricter.
Lin Ming28640512011-12-05 09:20:25 +0800122 */
Aaron Lu80d2fd42012-11-09 15:27:54 +0800123 if (pm_runtime_suspended(dev))
124 return 0;
Lin Ming28640512011-12-05 09:20:25 +0800125
Aaron Lu80d2fd42012-11-09 15:27:54 +0800126 err = scsi_dev_type_suspend(dev, cb);
Lin Ming28640512011-12-05 09:20:25 +0800127 }
Aaron Lu80d2fd42012-11-09 15:27:54 +0800128
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400129 return err;
130}
131
Dan Williams3c31b522014-04-10 15:30:35 -0700132static void async_sdev_resume(void *dev, async_cookie_t cookie)
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400133{
Dan Williams3c31b522014-04-10 15:30:35 -0700134 scsi_dev_type_resume(dev, do_scsi_resume);
135}
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400136
Dan Williams3c31b522014-04-10 15:30:35 -0700137static void async_sdev_thaw(void *dev, async_cookie_t cookie)
138{
139 scsi_dev_type_resume(dev, do_scsi_thaw);
140}
Aaron Lu63347902012-11-09 15:27:52 +0800141
Dan Williams3c31b522014-04-10 15:30:35 -0700142static void async_sdev_restore(void *dev, async_cookie_t cookie)
143{
144 scsi_dev_type_resume(dev, do_scsi_restore);
145}
146
147static int scsi_bus_resume_common(struct device *dev,
148 int (*cb)(struct device *, const struct dev_pm_ops *))
149{
150 async_func_t fn;
151
152 if (!scsi_is_sdev_device(dev))
153 fn = NULL;
154 else if (cb == do_scsi_resume)
155 fn = async_sdev_resume;
156 else if (cb == do_scsi_thaw)
157 fn = async_sdev_thaw;
158 else if (cb == do_scsi_restore)
159 fn = async_sdev_restore;
160 else
161 fn = NULL;
162
163 if (fn) {
164 async_schedule_domain(fn, dev, &scsi_sd_pm_domain);
165
166 /*
167 * If a user has disabled async probing a likely reason
168 * is due to a storage enclosure that does not inject
169 * staggered spin-ups. For safety, make resume
170 * synchronous as well in that case.
171 */
172 if (strncmp(scsi_scan_type, "async", 5) != 0)
173 async_synchronize_full_domain(&scsi_sd_pm_domain);
174 } else {
Alan Sternbc4f2402010-06-17 10:41:42 -0400175 pm_runtime_disable(dev);
176 pm_runtime_set_active(dev);
177 pm_runtime_enable(dev);
178 }
Dan Williams3c31b522014-04-10 15:30:35 -0700179 return 0;
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400180}
181
Alan Sternfea6d602012-02-17 16:25:08 -0500182static int scsi_bus_prepare(struct device *dev)
183{
Bart Van Asschef049cf12019-04-30 14:39:18 -0700184 if (scsi_is_host_device(dev)) {
Alan Sternfea6d602012-02-17 16:25:08 -0500185 /* Wait until async scanning is finished */
186 scsi_complete_async_scans();
187 }
188 return 0;
189}
190
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400191static int scsi_bus_suspend(struct device *dev)
192{
Dan Williams3c31b522014-04-10 15:30:35 -0700193 return scsi_bus_suspend_common(dev, do_scsi_suspend);
Aaron Lu80d2fd42012-11-09 15:27:54 +0800194}
195
196static int scsi_bus_resume(struct device *dev)
197{
Dan Williams3c31b522014-04-10 15:30:35 -0700198 return scsi_bus_resume_common(dev, do_scsi_resume);
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400199}
200
201static int scsi_bus_freeze(struct device *dev)
202{
Dan Williams3c31b522014-04-10 15:30:35 -0700203 return scsi_bus_suspend_common(dev, do_scsi_freeze);
Aaron Lu80d2fd42012-11-09 15:27:54 +0800204}
205
206static int scsi_bus_thaw(struct device *dev)
207{
Dan Williams3c31b522014-04-10 15:30:35 -0700208 return scsi_bus_resume_common(dev, do_scsi_thaw);
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400209}
210
211static int scsi_bus_poweroff(struct device *dev)
212{
Dan Williams3c31b522014-04-10 15:30:35 -0700213 return scsi_bus_suspend_common(dev, do_scsi_poweroff);
Aaron Lu80d2fd42012-11-09 15:27:54 +0800214}
215
216static int scsi_bus_restore(struct device *dev)
217{
Dan Williams3c31b522014-04-10 15:30:35 -0700218 return scsi_bus_resume_common(dev, do_scsi_restore);
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400219}
220
221#else /* CONFIG_PM_SLEEP */
222
Alan Sternfea6d602012-02-17 16:25:08 -0500223#define scsi_bus_prepare NULL
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400224#define scsi_bus_suspend NULL
Aaron Lu80d2fd42012-11-09 15:27:54 +0800225#define scsi_bus_resume NULL
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400226#define scsi_bus_freeze NULL
Aaron Lu80d2fd42012-11-09 15:27:54 +0800227#define scsi_bus_thaw NULL
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400228#define scsi_bus_poweroff NULL
Aaron Lu80d2fd42012-11-09 15:27:54 +0800229#define scsi_bus_restore NULL
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400230
231#endif /* CONFIG_PM_SLEEP */
232
Aaron Lu6627b382013-10-28 15:27:49 +0800233static int sdev_runtime_suspend(struct device *dev)
Lin Ming6df339a2013-03-23 11:42:28 +0800234{
Aaron Lu6627b382013-10-28 15:27:49 +0800235 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
236 struct scsi_device *sdev = to_scsi_device(dev);
Alan Stern49718f02015-08-17 11:02:42 -0400237 int err = 0;
Lin Ming6df339a2013-03-23 11:42:28 +0800238
Ken Xue1c69d3b2015-12-01 14:45:23 +0800239 err = blk_pre_runtime_suspend(sdev->request_queue);
240 if (err)
241 return err;
242 if (pm && pm->runtime_suspend)
Aaron Lu6627b382013-10-28 15:27:49 +0800243 err = pm->runtime_suspend(dev);
Ken Xue1c69d3b2015-12-01 14:45:23 +0800244 blk_post_runtime_suspend(sdev->request_queue, err);
245
Lin Ming6df339a2013-03-23 11:42:28 +0800246 return err;
247}
248
Alan Sternbc4f2402010-06-17 10:41:42 -0400249static int scsi_runtime_suspend(struct device *dev)
250{
251 int err = 0;
252
253 dev_dbg(dev, "scsi_runtime_suspend\n");
Lin Ming6df339a2013-03-23 11:42:28 +0800254 if (scsi_is_sdev_device(dev))
255 err = sdev_runtime_suspend(dev);
Alan Sternbc4f2402010-06-17 10:41:42 -0400256
257 /* Insert hooks here for targets, hosts, and transport classes */
258
259 return err;
260}
261
Lin Ming6df339a2013-03-23 11:42:28 +0800262static int sdev_runtime_resume(struct device *dev)
263{
264 struct scsi_device *sdev = to_scsi_device(dev);
265 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Aaron Lu6627b382013-10-28 15:27:49 +0800266 int err = 0;
Lin Ming6df339a2013-03-23 11:42:28 +0800267
Ken Xue1c69d3b2015-12-01 14:45:23 +0800268 blk_pre_runtime_resume(sdev->request_queue);
269 if (pm && pm->runtime_resume)
Aaron Lu6627b382013-10-28 15:27:49 +0800270 err = pm->runtime_resume(dev);
Ken Xue1c69d3b2015-12-01 14:45:23 +0800271 blk_post_runtime_resume(sdev->request_queue, err);
272
Aaron Lu6627b382013-10-28 15:27:49 +0800273 return err;
Lin Ming6df339a2013-03-23 11:42:28 +0800274}
275
Alan Sternbc4f2402010-06-17 10:41:42 -0400276static int scsi_runtime_resume(struct device *dev)
277{
278 int err = 0;
279
280 dev_dbg(dev, "scsi_runtime_resume\n");
281 if (scsi_is_sdev_device(dev))
Lin Ming6df339a2013-03-23 11:42:28 +0800282 err = sdev_runtime_resume(dev);
Alan Sternbc4f2402010-06-17 10:41:42 -0400283
284 /* Insert hooks here for targets, hosts, and transport classes */
285
286 return err;
287}
288
289static int scsi_runtime_idle(struct device *dev)
290{
Alan Sternbc4f2402010-06-17 10:41:42 -0400291 dev_dbg(dev, "scsi_runtime_idle\n");
292
293 /* Insert hooks here for targets, hosts, and transport classes */
294
Lin Ming6df339a2013-03-23 11:42:28 +0800295 if (scsi_is_sdev_device(dev)) {
Aaron Lu6627b382013-10-28 15:27:49 +0800296 pm_runtime_mark_last_busy(dev);
297 pm_runtime_autosuspend(dev);
298 return -EBUSY;
Lin Ming6df339a2013-03-23 11:42:28 +0800299 }
Aaron Lu6627b382013-10-28 15:27:49 +0800300
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200301 return 0;
Alan Sternbc4f2402010-06-17 10:41:42 -0400302}
303
304int scsi_autopm_get_device(struct scsi_device *sdev)
305{
306 int err;
307
308 err = pm_runtime_get_sync(&sdev->sdev_gendev);
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200309 if (err < 0 && err !=-EACCES)
Alan Sternbc4f2402010-06-17 10:41:42 -0400310 pm_runtime_put_sync(&sdev->sdev_gendev);
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200311 else
Alan Sternbc4f2402010-06-17 10:41:42 -0400312 err = 0;
313 return err;
314}
315EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
316
317void scsi_autopm_put_device(struct scsi_device *sdev)
318{
319 pm_runtime_put_sync(&sdev->sdev_gendev);
320}
321EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
322
323void scsi_autopm_get_target(struct scsi_target *starget)
324{
325 pm_runtime_get_sync(&starget->dev);
326}
327
328void scsi_autopm_put_target(struct scsi_target *starget)
329{
330 pm_runtime_put_sync(&starget->dev);
331}
332
333int scsi_autopm_get_host(struct Scsi_Host *shost)
334{
335 int err;
336
337 err = pm_runtime_get_sync(&shost->shost_gendev);
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200338 if (err < 0 && err !=-EACCES)
Alan Sternbc4f2402010-06-17 10:41:42 -0400339 pm_runtime_put_sync(&shost->shost_gendev);
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200340 else
Alan Sternbc4f2402010-06-17 10:41:42 -0400341 err = 0;
342 return err;
343}
344
345void scsi_autopm_put_host(struct Scsi_Host *shost)
346{
347 pm_runtime_put_sync(&shost->shost_gendev);
348}
349
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400350const struct dev_pm_ops scsi_bus_pm_ops = {
Alan Sternfea6d602012-02-17 16:25:08 -0500351 .prepare = scsi_bus_prepare,
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400352 .suspend = scsi_bus_suspend,
Aaron Lu80d2fd42012-11-09 15:27:54 +0800353 .resume = scsi_bus_resume,
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400354 .freeze = scsi_bus_freeze,
Aaron Lu80d2fd42012-11-09 15:27:54 +0800355 .thaw = scsi_bus_thaw,
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400356 .poweroff = scsi_bus_poweroff,
Aaron Lu80d2fd42012-11-09 15:27:54 +0800357 .restore = scsi_bus_restore,
Alan Sternbc4f2402010-06-17 10:41:42 -0400358 .runtime_suspend = scsi_runtime_suspend,
359 .runtime_resume = scsi_runtime_resume,
360 .runtime_idle = scsi_runtime_idle,
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400361};