blob: d581613d87c7dfff1b7c1ac6800a88ff16c1fa70 [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>
Bart Van Asschebca6b062018-09-26 14:01:03 -070011#include <linux/blk-pm.h>
Alan Sterndb5bd1e2010-06-17 10:36:49 -040012
13#include <scsi/scsi.h>
14#include <scsi/scsi_device.h>
15#include <scsi/scsi_driver.h>
16#include <scsi/scsi_host.h>
17
18#include "scsi_priv.h"
19
Aaron Lu6627b382013-10-28 15:27:49 +080020#ifdef CONFIG_PM_SLEEP
21
Dan Williams3c31b522014-04-10 15:30:35 -070022static int do_scsi_suspend(struct device *dev, const struct dev_pm_ops *pm)
Alan Sterndb5bd1e2010-06-17 10:36:49 -040023{
Dan Williams3c31b522014-04-10 15:30:35 -070024 return pm && pm->suspend ? pm->suspend(dev) : 0;
25}
26
27static int do_scsi_freeze(struct device *dev, const struct dev_pm_ops *pm)
28{
29 return pm && pm->freeze ? pm->freeze(dev) : 0;
30}
31
32static int do_scsi_poweroff(struct device *dev, const struct dev_pm_ops *pm)
33{
34 return pm && pm->poweroff ? pm->poweroff(dev) : 0;
35}
36
37static int do_scsi_resume(struct device *dev, const struct dev_pm_ops *pm)
38{
39 return pm && pm->resume ? pm->resume(dev) : 0;
40}
41
42static int do_scsi_thaw(struct device *dev, const struct dev_pm_ops *pm)
43{
44 return pm && pm->thaw ? pm->thaw(dev) : 0;
45}
46
47static int do_scsi_restore(struct device *dev, const struct dev_pm_ops *pm)
48{
49 return pm && pm->restore ? pm->restore(dev) : 0;
50}
51
52static int scsi_dev_type_suspend(struct device *dev,
53 int (*cb)(struct device *, const struct dev_pm_ops *))
54{
55 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Alan Sterndb5bd1e2010-06-17 10:36:49 -040056 int err;
57
58 err = scsi_device_quiesce(to_scsi_device(dev));
59 if (err == 0) {
Dan Williams3c31b522014-04-10 15:30:35 -070060 err = cb(dev, pm);
61 if (err)
62 scsi_device_resume(to_scsi_device(dev));
Alan Sterndb5bd1e2010-06-17 10:36:49 -040063 }
64 dev_dbg(dev, "scsi suspend: %d\n", err);
65 return err;
66}
67
Aaron Lu80d2fd42012-11-09 15:27:54 +080068static int
Dan Williams3c31b522014-04-10 15:30:35 -070069scsi_bus_suspend_common(struct device *dev,
70 int (*cb)(struct device *, const struct dev_pm_ops *))
Alan Sterndb5bd1e2010-06-17 10:36:49 -040071{
Bart Van Assche9131bff2021-10-06 14:54:53 -070072 if (!scsi_is_sdev_device(dev))
73 return 0;
Alan Sterndb5bd1e2010-06-17 10:36:49 -040074
Bart Van Assche9131bff2021-10-06 14:54:53 -070075 return scsi_dev_type_suspend(dev, cb);
Alan Sterndb5bd1e2010-06-17 10:36:49 -040076}
77
Dan Williams3c31b522014-04-10 15:30:35 -070078static int scsi_bus_resume_common(struct device *dev,
79 int (*cb)(struct device *, const struct dev_pm_ops *))
80{
Bart Van Assche9131bff2021-10-06 14:54:53 -070081 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
82 int err;
83
84 if (!scsi_is_sdev_device(dev))
85 return 0;
86
87 err = cb(dev, pm);
88 scsi_device_resume(to_scsi_device(dev));
89 dev_dbg(dev, "scsi resume: %d\n", err);
90
91 return err;
Alan Sterndb5bd1e2010-06-17 10:36:49 -040092}
93
Alan Sternfea6d602012-02-17 16:25:08 -050094static int scsi_bus_prepare(struct device *dev)
95{
Bart Van Asschef049cf12019-04-30 14:39:18 -070096 if (scsi_is_host_device(dev)) {
Alan Sternfea6d602012-02-17 16:25:08 -050097 /* Wait until async scanning is finished */
98 scsi_complete_async_scans();
99 }
100 return 0;
101}
102
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400103static int scsi_bus_suspend(struct device *dev)
104{
Dan Williams3c31b522014-04-10 15:30:35 -0700105 return scsi_bus_suspend_common(dev, do_scsi_suspend);
Aaron Lu80d2fd42012-11-09 15:27:54 +0800106}
107
108static int scsi_bus_resume(struct device *dev)
109{
Dan Williams3c31b522014-04-10 15:30:35 -0700110 return scsi_bus_resume_common(dev, do_scsi_resume);
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400111}
112
113static int scsi_bus_freeze(struct device *dev)
114{
Dan Williams3c31b522014-04-10 15:30:35 -0700115 return scsi_bus_suspend_common(dev, do_scsi_freeze);
Aaron Lu80d2fd42012-11-09 15:27:54 +0800116}
117
118static int scsi_bus_thaw(struct device *dev)
119{
Dan Williams3c31b522014-04-10 15:30:35 -0700120 return scsi_bus_resume_common(dev, do_scsi_thaw);
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400121}
122
123static int scsi_bus_poweroff(struct device *dev)
124{
Dan Williams3c31b522014-04-10 15:30:35 -0700125 return scsi_bus_suspend_common(dev, do_scsi_poweroff);
Aaron Lu80d2fd42012-11-09 15:27:54 +0800126}
127
128static int scsi_bus_restore(struct device *dev)
129{
Dan Williams3c31b522014-04-10 15:30:35 -0700130 return scsi_bus_resume_common(dev, do_scsi_restore);
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400131}
132
133#else /* CONFIG_PM_SLEEP */
134
Alan Sternfea6d602012-02-17 16:25:08 -0500135#define scsi_bus_prepare NULL
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400136#define scsi_bus_suspend NULL
Aaron Lu80d2fd42012-11-09 15:27:54 +0800137#define scsi_bus_resume NULL
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400138#define scsi_bus_freeze NULL
Aaron Lu80d2fd42012-11-09 15:27:54 +0800139#define scsi_bus_thaw NULL
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400140#define scsi_bus_poweroff NULL
Aaron Lu80d2fd42012-11-09 15:27:54 +0800141#define scsi_bus_restore NULL
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400142
143#endif /* CONFIG_PM_SLEEP */
144
Aaron Lu6627b382013-10-28 15:27:49 +0800145static int sdev_runtime_suspend(struct device *dev)
Lin Ming6df339a2013-03-23 11:42:28 +0800146{
Aaron Lu6627b382013-10-28 15:27:49 +0800147 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
148 struct scsi_device *sdev = to_scsi_device(dev);
Alan Stern49718f02015-08-17 11:02:42 -0400149 int err = 0;
Lin Ming6df339a2013-03-23 11:42:28 +0800150
Ken Xue1c69d3b2015-12-01 14:45:23 +0800151 err = blk_pre_runtime_suspend(sdev->request_queue);
152 if (err)
153 return err;
154 if (pm && pm->runtime_suspend)
Aaron Lu6627b382013-10-28 15:27:49 +0800155 err = pm->runtime_suspend(dev);
Ken Xue1c69d3b2015-12-01 14:45:23 +0800156 blk_post_runtime_suspend(sdev->request_queue, err);
157
Lin Ming6df339a2013-03-23 11:42:28 +0800158 return err;
159}
160
Alan Sternbc4f2402010-06-17 10:41:42 -0400161static int scsi_runtime_suspend(struct device *dev)
162{
163 int err = 0;
164
165 dev_dbg(dev, "scsi_runtime_suspend\n");
Lin Ming6df339a2013-03-23 11:42:28 +0800166 if (scsi_is_sdev_device(dev))
167 err = sdev_runtime_suspend(dev);
Alan Sternbc4f2402010-06-17 10:41:42 -0400168
169 /* Insert hooks here for targets, hosts, and transport classes */
170
171 return err;
172}
173
Lin Ming6df339a2013-03-23 11:42:28 +0800174static int sdev_runtime_resume(struct device *dev)
175{
176 struct scsi_device *sdev = to_scsi_device(dev);
177 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Aaron Lu6627b382013-10-28 15:27:49 +0800178 int err = 0;
Lin Ming6df339a2013-03-23 11:42:28 +0800179
Ken Xue1c69d3b2015-12-01 14:45:23 +0800180 blk_pre_runtime_resume(sdev->request_queue);
181 if (pm && pm->runtime_resume)
Aaron Lu6627b382013-10-28 15:27:49 +0800182 err = pm->runtime_resume(dev);
Alan Stern6e1fcab2021-12-20 19:21:26 +0800183 blk_post_runtime_resume(sdev->request_queue);
Ken Xue1c69d3b2015-12-01 14:45:23 +0800184
Aaron Lu6627b382013-10-28 15:27:49 +0800185 return err;
Lin Ming6df339a2013-03-23 11:42:28 +0800186}
187
Alan Sternbc4f2402010-06-17 10:41:42 -0400188static int scsi_runtime_resume(struct device *dev)
189{
190 int err = 0;
191
192 dev_dbg(dev, "scsi_runtime_resume\n");
193 if (scsi_is_sdev_device(dev))
Lin Ming6df339a2013-03-23 11:42:28 +0800194 err = sdev_runtime_resume(dev);
Alan Sternbc4f2402010-06-17 10:41:42 -0400195
196 /* Insert hooks here for targets, hosts, and transport classes */
197
198 return err;
199}
200
201static int scsi_runtime_idle(struct device *dev)
202{
Alan Sternbc4f2402010-06-17 10:41:42 -0400203 dev_dbg(dev, "scsi_runtime_idle\n");
204
205 /* Insert hooks here for targets, hosts, and transport classes */
206
Lin Ming6df339a2013-03-23 11:42:28 +0800207 if (scsi_is_sdev_device(dev)) {
Aaron Lu6627b382013-10-28 15:27:49 +0800208 pm_runtime_mark_last_busy(dev);
209 pm_runtime_autosuspend(dev);
210 return -EBUSY;
Lin Ming6df339a2013-03-23 11:42:28 +0800211 }
Aaron Lu6627b382013-10-28 15:27:49 +0800212
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200213 return 0;
Alan Sternbc4f2402010-06-17 10:41:42 -0400214}
215
216int scsi_autopm_get_device(struct scsi_device *sdev)
217{
218 int err;
219
220 err = pm_runtime_get_sync(&sdev->sdev_gendev);
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200221 if (err < 0 && err !=-EACCES)
Alan Sternbc4f2402010-06-17 10:41:42 -0400222 pm_runtime_put_sync(&sdev->sdev_gendev);
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200223 else
Alan Sternbc4f2402010-06-17 10:41:42 -0400224 err = 0;
225 return err;
226}
227EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
228
229void scsi_autopm_put_device(struct scsi_device *sdev)
230{
231 pm_runtime_put_sync(&sdev->sdev_gendev);
232}
233EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
234
235void scsi_autopm_get_target(struct scsi_target *starget)
236{
237 pm_runtime_get_sync(&starget->dev);
238}
239
240void scsi_autopm_put_target(struct scsi_target *starget)
241{
242 pm_runtime_put_sync(&starget->dev);
243}
244
245int scsi_autopm_get_host(struct Scsi_Host *shost)
246{
247 int err;
248
249 err = pm_runtime_get_sync(&shost->shost_gendev);
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200250 if (err < 0 && err !=-EACCES)
Alan Sternbc4f2402010-06-17 10:41:42 -0400251 pm_runtime_put_sync(&shost->shost_gendev);
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200252 else
Alan Sternbc4f2402010-06-17 10:41:42 -0400253 err = 0;
254 return err;
255}
256
257void scsi_autopm_put_host(struct Scsi_Host *shost)
258{
259 pm_runtime_put_sync(&shost->shost_gendev);
260}
261
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400262const struct dev_pm_ops scsi_bus_pm_ops = {
Alan Sternfea6d602012-02-17 16:25:08 -0500263 .prepare = scsi_bus_prepare,
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400264 .suspend = scsi_bus_suspend,
Aaron Lu80d2fd42012-11-09 15:27:54 +0800265 .resume = scsi_bus_resume,
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400266 .freeze = scsi_bus_freeze,
Aaron Lu80d2fd42012-11-09 15:27:54 +0800267 .thaw = scsi_bus_thaw,
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400268 .poweroff = scsi_bus_poweroff,
Aaron Lu80d2fd42012-11-09 15:27:54 +0800269 .restore = scsi_bus_restore,
Alan Sternbc4f2402010-06-17 10:41:42 -0400270 .runtime_suspend = scsi_runtime_suspend,
271 .runtime_resume = scsi_runtime_resume,
272 .runtime_idle = scsi_runtime_idle,
Alan Sterndb5bd1e2010-06-17 10:36:49 -0400273};