1 // SPDX-License-Identifier: GPL-2.0-only
3 * scsi_pm.c Copyright (C) 2010 Alan Stern
5 * SCSI dynamic Power Management
9 #include <linux/pm_runtime.h>
10 #include <linux/export.h>
11 #include <linux/async.h>
12 #include <linux/blk-pm.h>
14 #include <scsi/scsi.h>
15 #include <scsi/scsi_device.h>
16 #include <scsi/scsi_driver.h>
17 #include <scsi/scsi_host.h>
19 #include "scsi_priv.h"
21 #ifdef CONFIG_PM_SLEEP
23 static int do_scsi_suspend(struct device *dev, const struct dev_pm_ops *pm)
25 return pm && pm->suspend ? pm->suspend(dev) : 0;
28 static int do_scsi_freeze(struct device *dev, const struct dev_pm_ops *pm)
30 return pm && pm->freeze ? pm->freeze(dev) : 0;
33 static int do_scsi_poweroff(struct device *dev, const struct dev_pm_ops *pm)
35 return pm && pm->poweroff ? pm->poweroff(dev) : 0;
38 static int do_scsi_resume(struct device *dev, const struct dev_pm_ops *pm)
40 return pm && pm->resume ? pm->resume(dev) : 0;
43 static int do_scsi_thaw(struct device *dev, const struct dev_pm_ops *pm)
45 return pm && pm->thaw ? pm->thaw(dev) : 0;
48 static int do_scsi_restore(struct device *dev, const struct dev_pm_ops *pm)
50 return pm && pm->restore ? pm->restore(dev) : 0;
53 static int scsi_dev_type_suspend(struct device *dev,
54 int (*cb)(struct device *, const struct dev_pm_ops *))
56 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
59 err = scsi_device_quiesce(to_scsi_device(dev));
63 scsi_device_resume(to_scsi_device(dev));
65 dev_dbg(dev, "scsi suspend: %d\n", err);
70 scsi_bus_suspend_common(struct device *dev,
71 int (*cb)(struct device *, const struct dev_pm_ops *))
73 if (!scsi_is_sdev_device(dev))
76 return scsi_dev_type_suspend(dev, cb);
79 static int scsi_bus_resume_common(struct device *dev,
80 int (*cb)(struct device *, const struct dev_pm_ops *))
82 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
85 if (!scsi_is_sdev_device(dev))
89 scsi_device_resume(to_scsi_device(dev));
90 dev_dbg(dev, "scsi resume: %d\n", err);
95 static int scsi_bus_prepare(struct device *dev)
97 if (scsi_is_host_device(dev)) {
98 /* Wait until async scanning is finished */
99 scsi_complete_async_scans();
104 static int scsi_bus_suspend(struct device *dev)
106 return scsi_bus_suspend_common(dev, do_scsi_suspend);
109 static int scsi_bus_resume(struct device *dev)
111 return scsi_bus_resume_common(dev, do_scsi_resume);
114 static int scsi_bus_freeze(struct device *dev)
116 return scsi_bus_suspend_common(dev, do_scsi_freeze);
119 static int scsi_bus_thaw(struct device *dev)
121 return scsi_bus_resume_common(dev, do_scsi_thaw);
124 static int scsi_bus_poweroff(struct device *dev)
126 return scsi_bus_suspend_common(dev, do_scsi_poweroff);
129 static int scsi_bus_restore(struct device *dev)
131 return scsi_bus_resume_common(dev, do_scsi_restore);
134 #else /* CONFIG_PM_SLEEP */
136 #define scsi_bus_prepare NULL
137 #define scsi_bus_suspend NULL
138 #define scsi_bus_resume NULL
139 #define scsi_bus_freeze NULL
140 #define scsi_bus_thaw NULL
141 #define scsi_bus_poweroff NULL
142 #define scsi_bus_restore NULL
144 #endif /* CONFIG_PM_SLEEP */
146 static int sdev_runtime_suspend(struct device *dev)
148 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
149 struct scsi_device *sdev = to_scsi_device(dev);
152 err = blk_pre_runtime_suspend(sdev->request_queue);
155 if (pm && pm->runtime_suspend)
156 err = pm->runtime_suspend(dev);
157 blk_post_runtime_suspend(sdev->request_queue, err);
162 static int scsi_runtime_suspend(struct device *dev)
166 dev_dbg(dev, "scsi_runtime_suspend\n");
167 if (scsi_is_sdev_device(dev))
168 err = sdev_runtime_suspend(dev);
170 /* Insert hooks here for targets, hosts, and transport classes */
175 static int sdev_runtime_resume(struct device *dev)
177 struct scsi_device *sdev = to_scsi_device(dev);
178 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
181 blk_pre_runtime_resume(sdev->request_queue);
182 if (pm && pm->runtime_resume)
183 err = pm->runtime_resume(dev);
184 blk_post_runtime_resume(sdev->request_queue, err);
189 static int scsi_runtime_resume(struct device *dev)
193 dev_dbg(dev, "scsi_runtime_resume\n");
194 if (scsi_is_sdev_device(dev))
195 err = sdev_runtime_resume(dev);
197 /* Insert hooks here for targets, hosts, and transport classes */
202 static int scsi_runtime_idle(struct device *dev)
204 dev_dbg(dev, "scsi_runtime_idle\n");
206 /* Insert hooks here for targets, hosts, and transport classes */
208 if (scsi_is_sdev_device(dev)) {
209 pm_runtime_mark_last_busy(dev);
210 pm_runtime_autosuspend(dev);
217 int scsi_autopm_get_device(struct scsi_device *sdev)
221 err = pm_runtime_get_sync(&sdev->sdev_gendev);
222 if (err < 0 && err !=-EACCES)
223 pm_runtime_put_sync(&sdev->sdev_gendev);
228 EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
230 void scsi_autopm_put_device(struct scsi_device *sdev)
232 pm_runtime_put_sync(&sdev->sdev_gendev);
234 EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
236 void scsi_autopm_get_target(struct scsi_target *starget)
238 pm_runtime_get_sync(&starget->dev);
241 void scsi_autopm_put_target(struct scsi_target *starget)
243 pm_runtime_put_sync(&starget->dev);
246 int scsi_autopm_get_host(struct Scsi_Host *shost)
250 err = pm_runtime_get_sync(&shost->shost_gendev);
251 if (err < 0 && err !=-EACCES)
252 pm_runtime_put_sync(&shost->shost_gendev);
258 void scsi_autopm_put_host(struct Scsi_Host *shost)
260 pm_runtime_put_sync(&shost->shost_gendev);
263 const struct dev_pm_ops scsi_bus_pm_ops = {
264 .prepare = scsi_bus_prepare,
265 .suspend = scsi_bus_suspend,
266 .resume = scsi_bus_resume,
267 .freeze = scsi_bus_freeze,
268 .thaw = scsi_bus_thaw,
269 .poweroff = scsi_bus_poweroff,
270 .restore = scsi_bus_restore,
271 .runtime_suspend = scsi_runtime_suspend,
272 .runtime_resume = scsi_runtime_resume,
273 .runtime_idle = scsi_runtime_idle,