blob: 3717eea37ecb386fd772e6dd90182b3674d41ceb [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) {
83 pm_runtime_disable(dev);
Stanley Chu3f7e62b2019-01-03 22:08:05 +080084 err = pm_runtime_set_active(dev);
Dan Williams3c31b522014-04-10 15:30:35 -070085 pm_runtime_enable(dev);
Stanley Chu3f7e62b2019-01-03 22:08:05 +080086
87 /*
88 * Forcibly set runtime PM status of request queue to "active"
89 * to make sure we can again get requests from the queue
90 * (see also blk_pm_peek_request()).
91 *
92 * The resume hook will correct runtime PM status of the disk.
93 */
94 if (!err && scsi_is_sdev_device(dev)) {
95 struct scsi_device *sdev = to_scsi_device(dev);
96
Stanley Chub8040492019-09-12 16:35:28 +080097 blk_set_runtime_active(sdev->request_queue);
Stanley Chu3f7e62b2019-01-03 22:08:05 +080098 }
Dan Williams3c31b522014-04-10 15:30:35 -070099 }
100
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400101 return err;
102}
103
Aaron Lu80d2fd42012-11-09 15:27:54 +0800104static int
Dan Williams3c31b522014-04-10 15:30:35 -0700105scsi_bus_suspend_common(struct device *dev,
106 int (*cb)(struct device *, const struct dev_pm_ops *))
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400107{
108 int err = 0;
109
Lin Ming28640512011-12-05 09:20:25 +0800110 if (scsi_is_sdev_device(dev)) {
111 /*
Aaron Lu80d2fd42012-11-09 15:27:54 +0800112 * All the high-level SCSI drivers that implement runtime
113 * PM treat runtime suspend, system suspend, and system
Oliver Neukum95897912013-09-16 13:28:15 +0200114 * hibernate nearly identically. In all cases the requirements
115 * for runtime suspension are stricter.
Lin Ming28640512011-12-05 09:20:25 +0800116 */
Aaron Lu80d2fd42012-11-09 15:27:54 +0800117 if (pm_runtime_suspended(dev))
118 return 0;
Lin Ming28640512011-12-05 09:20:25 +0800119
Aaron Lu80d2fd42012-11-09 15:27:54 +0800120 err = scsi_dev_type_suspend(dev, cb);
Lin Ming28640512011-12-05 09:20:25 +0800121 }
Aaron Lu80d2fd42012-11-09 15:27:54 +0800122
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400123 return err;
124}
125
Dan Williams3c31b522014-04-10 15:30:35 -0700126static void async_sdev_resume(void *dev, async_cookie_t cookie)
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400127{
Dan Williams3c31b522014-04-10 15:30:35 -0700128 scsi_dev_type_resume(dev, do_scsi_resume);
129}
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400130
Dan Williams3c31b522014-04-10 15:30:35 -0700131static void async_sdev_thaw(void *dev, async_cookie_t cookie)
132{
133 scsi_dev_type_resume(dev, do_scsi_thaw);
134}
Aaron Lu63347902012-11-09 15:27:52 +0800135
Dan Williams3c31b522014-04-10 15:30:35 -0700136static void async_sdev_restore(void *dev, async_cookie_t cookie)
137{
138 scsi_dev_type_resume(dev, do_scsi_restore);
139}
140
141static int scsi_bus_resume_common(struct device *dev,
142 int (*cb)(struct device *, const struct dev_pm_ops *))
143{
144 async_func_t fn;
145
146 if (!scsi_is_sdev_device(dev))
147 fn = NULL;
148 else if (cb == do_scsi_resume)
149 fn = async_sdev_resume;
150 else if (cb == do_scsi_thaw)
151 fn = async_sdev_thaw;
152 else if (cb == do_scsi_restore)
153 fn = async_sdev_restore;
154 else
155 fn = NULL;
156
157 if (fn) {
158 async_schedule_domain(fn, dev, &scsi_sd_pm_domain);
159
160 /*
161 * If a user has disabled async probing a likely reason
162 * is due to a storage enclosure that does not inject
163 * staggered spin-ups. For safety, make resume
164 * synchronous as well in that case.
165 */
166 if (strncmp(scsi_scan_type, "async", 5) != 0)
167 async_synchronize_full_domain(&scsi_sd_pm_domain);
168 } else {
Alan Sternbc4f2402010-06-17 10:41:42 -0400169 pm_runtime_disable(dev);
170 pm_runtime_set_active(dev);
171 pm_runtime_enable(dev);
172 }
Dan Williams3c31b522014-04-10 15:30:35 -0700173 return 0;
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400174}
175
Alan Sternfea6d602012-02-17 16:25:08 -0500176static int scsi_bus_prepare(struct device *dev)
177{
Bart Van Asschef049cf12019-04-30 14:39:18 -0700178 if (scsi_is_host_device(dev)) {
Alan Sternfea6d602012-02-17 16:25:08 -0500179 /* Wait until async scanning is finished */
180 scsi_complete_async_scans();
181 }
182 return 0;
183}
184
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400185static int scsi_bus_suspend(struct device *dev)
186{
Dan Williams3c31b522014-04-10 15:30:35 -0700187 return scsi_bus_suspend_common(dev, do_scsi_suspend);
Aaron Lu80d2fd42012-11-09 15:27:54 +0800188}
189
190static int scsi_bus_resume(struct device *dev)
191{
Dan Williams3c31b522014-04-10 15:30:35 -0700192 return scsi_bus_resume_common(dev, do_scsi_resume);
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400193}
194
195static int scsi_bus_freeze(struct device *dev)
196{
Dan Williams3c31b522014-04-10 15:30:35 -0700197 return scsi_bus_suspend_common(dev, do_scsi_freeze);
Aaron Lu80d2fd42012-11-09 15:27:54 +0800198}
199
200static int scsi_bus_thaw(struct device *dev)
201{
Dan Williams3c31b522014-04-10 15:30:35 -0700202 return scsi_bus_resume_common(dev, do_scsi_thaw);
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400203}
204
205static int scsi_bus_poweroff(struct device *dev)
206{
Dan Williams3c31b522014-04-10 15:30:35 -0700207 return scsi_bus_suspend_common(dev, do_scsi_poweroff);
Aaron Lu80d2fd42012-11-09 15:27:54 +0800208}
209
210static int scsi_bus_restore(struct device *dev)
211{
Dan Williams3c31b522014-04-10 15:30:35 -0700212 return scsi_bus_resume_common(dev, do_scsi_restore);
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400213}
214
215#else /* CONFIG_PM_SLEEP */
216
Alan Sternfea6d602012-02-17 16:25:08 -0500217#define scsi_bus_prepare NULL
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400218#define scsi_bus_suspend NULL
Aaron Lu80d2fd42012-11-09 15:27:54 +0800219#define scsi_bus_resume NULL
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400220#define scsi_bus_freeze NULL
Aaron Lu80d2fd42012-11-09 15:27:54 +0800221#define scsi_bus_thaw NULL
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400222#define scsi_bus_poweroff NULL
Aaron Lu80d2fd42012-11-09 15:27:54 +0800223#define scsi_bus_restore NULL
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400224
225#endif /* CONFIG_PM_SLEEP */
226
Aaron Lu6627b382013-10-28 15:27:49 +0800227static int sdev_runtime_suspend(struct device *dev)
Lin Ming6df339a2013-03-23 11:42:28 +0800228{
Aaron Lu6627b382013-10-28 15:27:49 +0800229 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
230 struct scsi_device *sdev = to_scsi_device(dev);
Alan Stern49718f02015-08-17 11:02:42 -0400231 int err = 0;
Lin Ming6df339a2013-03-23 11:42:28 +0800232
Ken Xue1c69d3b2015-12-01 14:45:23 +0800233 err = blk_pre_runtime_suspend(sdev->request_queue);
234 if (err)
235 return err;
236 if (pm && pm->runtime_suspend)
Aaron Lu6627b382013-10-28 15:27:49 +0800237 err = pm->runtime_suspend(dev);
Ken Xue1c69d3b2015-12-01 14:45:23 +0800238 blk_post_runtime_suspend(sdev->request_queue, err);
239
Lin Ming6df339a2013-03-23 11:42:28 +0800240 return err;
241}
242
Alan Sternbc4f2402010-06-17 10:41:42 -0400243static int scsi_runtime_suspend(struct device *dev)
244{
245 int err = 0;
246
247 dev_dbg(dev, "scsi_runtime_suspend\n");
Lin Ming6df339a2013-03-23 11:42:28 +0800248 if (scsi_is_sdev_device(dev))
249 err = sdev_runtime_suspend(dev);
Alan Sternbc4f2402010-06-17 10:41:42 -0400250
251 /* Insert hooks here for targets, hosts, and transport classes */
252
253 return err;
254}
255
Lin Ming6df339a2013-03-23 11:42:28 +0800256static int sdev_runtime_resume(struct device *dev)
257{
258 struct scsi_device *sdev = to_scsi_device(dev);
259 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Aaron Lu6627b382013-10-28 15:27:49 +0800260 int err = 0;
Lin Ming6df339a2013-03-23 11:42:28 +0800261
Ken Xue1c69d3b2015-12-01 14:45:23 +0800262 blk_pre_runtime_resume(sdev->request_queue);
263 if (pm && pm->runtime_resume)
Aaron Lu6627b382013-10-28 15:27:49 +0800264 err = pm->runtime_resume(dev);
Ken Xue1c69d3b2015-12-01 14:45:23 +0800265 blk_post_runtime_resume(sdev->request_queue, err);
266
Aaron Lu6627b382013-10-28 15:27:49 +0800267 return err;
Lin Ming6df339a2013-03-23 11:42:28 +0800268}
269
Alan Sternbc4f2402010-06-17 10:41:42 -0400270static int scsi_runtime_resume(struct device *dev)
271{
272 int err = 0;
273
274 dev_dbg(dev, "scsi_runtime_resume\n");
275 if (scsi_is_sdev_device(dev))
Lin Ming6df339a2013-03-23 11:42:28 +0800276 err = sdev_runtime_resume(dev);
Alan Sternbc4f2402010-06-17 10:41:42 -0400277
278 /* Insert hooks here for targets, hosts, and transport classes */
279
280 return err;
281}
282
283static int scsi_runtime_idle(struct device *dev)
284{
Alan Sternbc4f2402010-06-17 10:41:42 -0400285 dev_dbg(dev, "scsi_runtime_idle\n");
286
287 /* Insert hooks here for targets, hosts, and transport classes */
288
Lin Ming6df339a2013-03-23 11:42:28 +0800289 if (scsi_is_sdev_device(dev)) {
Aaron Lu6627b382013-10-28 15:27:49 +0800290 pm_runtime_mark_last_busy(dev);
291 pm_runtime_autosuspend(dev);
292 return -EBUSY;
Lin Ming6df339a2013-03-23 11:42:28 +0800293 }
Aaron Lu6627b382013-10-28 15:27:49 +0800294
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200295 return 0;
Alan Sternbc4f2402010-06-17 10:41:42 -0400296}
297
298int scsi_autopm_get_device(struct scsi_device *sdev)
299{
300 int err;
301
302 err = pm_runtime_get_sync(&sdev->sdev_gendev);
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200303 if (err < 0 && err !=-EACCES)
Alan Sternbc4f2402010-06-17 10:41:42 -0400304 pm_runtime_put_sync(&sdev->sdev_gendev);
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200305 else
Alan Sternbc4f2402010-06-17 10:41:42 -0400306 err = 0;
307 return err;
308}
309EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
310
311void scsi_autopm_put_device(struct scsi_device *sdev)
312{
313 pm_runtime_put_sync(&sdev->sdev_gendev);
314}
315EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
316
317void scsi_autopm_get_target(struct scsi_target *starget)
318{
319 pm_runtime_get_sync(&starget->dev);
320}
321
322void scsi_autopm_put_target(struct scsi_target *starget)
323{
324 pm_runtime_put_sync(&starget->dev);
325}
326
327int scsi_autopm_get_host(struct Scsi_Host *shost)
328{
329 int err;
330
331 err = pm_runtime_get_sync(&shost->shost_gendev);
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200332 if (err < 0 && err !=-EACCES)
Alan Sternbc4f2402010-06-17 10:41:42 -0400333 pm_runtime_put_sync(&shost->shost_gendev);
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200334 else
Alan Sternbc4f2402010-06-17 10:41:42 -0400335 err = 0;
336 return err;
337}
338
339void scsi_autopm_put_host(struct Scsi_Host *shost)
340{
341 pm_runtime_put_sync(&shost->shost_gendev);
342}
343
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400344const struct dev_pm_ops scsi_bus_pm_ops = {
Alan Sternfea6d602012-02-17 16:25:08 -0500345 .prepare = scsi_bus_prepare,
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400346 .suspend = scsi_bus_suspend,
Aaron Lu80d2fd42012-11-09 15:27:54 +0800347 .resume = scsi_bus_resume,
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400348 .freeze = scsi_bus_freeze,
Aaron Lu80d2fd42012-11-09 15:27:54 +0800349 .thaw = scsi_bus_thaw,
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400350 .poweroff = scsi_bus_poweroff,
Aaron Lu80d2fd42012-11-09 15:27:54 +0800351 .restore = scsi_bus_restore,
Alan Sternbc4f2402010-06-17 10:41:42 -0400352 .runtime_suspend = scsi_runtime_suspend,
353 .runtime_resume = scsi_runtime_resume,
354 .runtime_idle = scsi_runtime_idle,
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400355};