Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
db5bd1e0 AS |
2 | /* |
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> | |
09703660 | 10 | #include <linux/export.h> |
bca6b067 | 11 | #include <linux/blk-pm.h> |
db5bd1e0 AS |
12 | |
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 | ||
6627b38f AL |
20 | #ifdef CONFIG_PM_SLEEP |
21 | ||
3c31b52f | 22 | static int do_scsi_suspend(struct device *dev, const struct dev_pm_ops *pm) |
db5bd1e0 | 23 | { |
3c31b52f DW |
24 | return pm && pm->suspend ? pm->suspend(dev) : 0; |
25 | } | |
26 | ||
27 | static 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 | ||
32 | static 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 | ||
37 | static 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 | ||
42 | static 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 | ||
47 | static 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 | ||
52 | static 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; | |
db5bd1e0 AS |
56 | int err; |
57 | ||
58 | err = scsi_device_quiesce(to_scsi_device(dev)); | |
59 | if (err == 0) { | |
3c31b52f DW |
60 | err = cb(dev, pm); |
61 | if (err) | |
62 | scsi_device_resume(to_scsi_device(dev)); | |
db5bd1e0 AS |
63 | } |
64 | dev_dbg(dev, "scsi suspend: %d\n", err); | |
65 | return err; | |
66 | } | |
67 | ||
9131bff6 BVA |
68 | static int |
69 | scsi_bus_suspend_common(struct device *dev, | |
3c31b52f | 70 | int (*cb)(struct device *, const struct dev_pm_ops *)) |
db5bd1e0 | 71 | { |
9131bff6 BVA |
72 | if (!scsi_is_sdev_device(dev)) |
73 | return 0; | |
3c31b52f | 74 | |
9131bff6 | 75 | return scsi_dev_type_suspend(dev, cb); |
db5bd1e0 AS |
76 | } |
77 | ||
9131bff6 | 78 | static int scsi_bus_resume_common(struct device *dev, |
3c31b52f | 79 | int (*cb)(struct device *, const struct dev_pm_ops *)) |
db5bd1e0 | 80 | { |
9131bff6 BVA |
81 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
82 | int err; | |
db5bd1e0 | 83 | |
9131bff6 BVA |
84 | if (!scsi_is_sdev_device(dev)) |
85 | return 0; | |
28640516 | 86 | |
9131bff6 BVA |
87 | err = cb(dev, pm); |
88 | scsi_device_resume(to_scsi_device(dev)); | |
89 | dev_dbg(dev, "scsi resume: %d\n", err); | |
80d2fd48 | 90 | |
db5bd1e0 AS |
91 | return err; |
92 | } | |
93 | ||
fea6d607 AS |
94 | static int scsi_bus_prepare(struct device *dev) |
95 | { | |
f049cf1a | 96 | if (scsi_is_host_device(dev)) { |
fea6d607 AS |
97 | /* Wait until async scanning is finished */ |
98 | scsi_complete_async_scans(); | |
99 | } | |
100 | return 0; | |
101 | } | |
102 | ||
db5bd1e0 AS |
103 | static int scsi_bus_suspend(struct device *dev) |
104 | { | |
3c31b52f | 105 | return scsi_bus_suspend_common(dev, do_scsi_suspend); |
80d2fd48 AL |
106 | } |
107 | ||
108 | static int scsi_bus_resume(struct device *dev) | |
109 | { | |
3c31b52f | 110 | return scsi_bus_resume_common(dev, do_scsi_resume); |
db5bd1e0 AS |
111 | } |
112 | ||
113 | static int scsi_bus_freeze(struct device *dev) | |
114 | { | |
3c31b52f | 115 | return scsi_bus_suspend_common(dev, do_scsi_freeze); |
80d2fd48 AL |
116 | } |
117 | ||
118 | static int scsi_bus_thaw(struct device *dev) | |
119 | { | |
3c31b52f | 120 | return scsi_bus_resume_common(dev, do_scsi_thaw); |
db5bd1e0 AS |
121 | } |
122 | ||
123 | static int scsi_bus_poweroff(struct device *dev) | |
124 | { | |
3c31b52f | 125 | return scsi_bus_suspend_common(dev, do_scsi_poweroff); |
80d2fd48 AL |
126 | } |
127 | ||
128 | static int scsi_bus_restore(struct device *dev) | |
129 | { | |
3c31b52f | 130 | return scsi_bus_resume_common(dev, do_scsi_restore); |
db5bd1e0 AS |
131 | } |
132 | ||
133 | #else /* CONFIG_PM_SLEEP */ | |
134 | ||
fea6d607 | 135 | #define scsi_bus_prepare NULL |
db5bd1e0 | 136 | #define scsi_bus_suspend NULL |
80d2fd48 | 137 | #define scsi_bus_resume NULL |
db5bd1e0 | 138 | #define scsi_bus_freeze NULL |
80d2fd48 | 139 | #define scsi_bus_thaw NULL |
db5bd1e0 | 140 | #define scsi_bus_poweroff NULL |
80d2fd48 | 141 | #define scsi_bus_restore NULL |
db5bd1e0 AS |
142 | |
143 | #endif /* CONFIG_PM_SLEEP */ | |
144 | ||
6627b38f | 145 | static int sdev_runtime_suspend(struct device *dev) |
6df339a5 | 146 | { |
6627b38f AL |
147 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
148 | struct scsi_device *sdev = to_scsi_device(dev); | |
49718f0f | 149 | int err = 0; |
6df339a5 | 150 | |
1c69d3b6 KX |
151 | err = blk_pre_runtime_suspend(sdev->request_queue); |
152 | if (err) | |
153 | return err; | |
154 | if (pm && pm->runtime_suspend) | |
6627b38f | 155 | err = pm->runtime_suspend(dev); |
1c69d3b6 KX |
156 | blk_post_runtime_suspend(sdev->request_queue, err); |
157 | ||
6df339a5 LM |
158 | return err; |
159 | } | |
160 | ||
bc4f2401 AS |
161 | static int scsi_runtime_suspend(struct device *dev) |
162 | { | |
163 | int err = 0; | |
164 | ||
165 | dev_dbg(dev, "scsi_runtime_suspend\n"); | |
6df339a5 LM |
166 | if (scsi_is_sdev_device(dev)) |
167 | err = sdev_runtime_suspend(dev); | |
bc4f2401 AS |
168 | |
169 | /* Insert hooks here for targets, hosts, and transport classes */ | |
170 | ||
171 | return err; | |
172 | } | |
173 | ||
6627b38f | 174 | static int sdev_runtime_resume(struct device *dev) |
bc4f2401 | 175 | { |
6627b38f AL |
176 | struct scsi_device *sdev = to_scsi_device(dev); |
177 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; | |
bc4f2401 | 178 | int err = 0; |
6df339a5 | 179 | |
1c69d3b6 KX |
180 | blk_pre_runtime_resume(sdev->request_queue); |
181 | if (pm && pm->runtime_resume) | |
6627b38f | 182 | err = pm->runtime_resume(dev); |
6e1fcab0 | 183 | blk_post_runtime_resume(sdev->request_queue); |
1c69d3b6 | 184 | |
6df339a5 LM |
185 | return err; |
186 | } | |
187 | ||
6df339a5 LM |
188 | static int scsi_runtime_resume(struct device *dev) |
189 | { | |
190 | int err = 0; | |
bc4f2401 AS |
191 | |
192 | dev_dbg(dev, "scsi_runtime_resume\n"); | |
193 | if (scsi_is_sdev_device(dev)) | |
6df339a5 | 194 | err = sdev_runtime_resume(dev); |
bc4f2401 AS |
195 | |
196 | /* Insert hooks here for targets, hosts, and transport classes */ | |
197 | ||
198 | return err; | |
199 | } | |
200 | ||
201 | static int scsi_runtime_idle(struct device *dev) | |
202 | { | |
bc4f2401 AS |
203 | dev_dbg(dev, "scsi_runtime_idle\n"); |
204 | ||
205 | /* Insert hooks here for targets, hosts, and transport classes */ | |
206 | ||
6df339a5 | 207 | if (scsi_is_sdev_device(dev)) { |
6627b38f AL |
208 | pm_runtime_mark_last_busy(dev); |
209 | pm_runtime_autosuspend(dev); | |
210 | return -EBUSY; | |
6df339a5 | 211 | } |
6627b38f | 212 | |
45f0a85c | 213 | return 0; |
bc4f2401 AS |
214 | } |
215 | ||
216 | int scsi_autopm_get_device(struct scsi_device *sdev) | |
217 | { | |
218 | int err; | |
219 | ||
220 | err = pm_runtime_get_sync(&sdev->sdev_gendev); | |
632e270e | 221 | if (err < 0 && err !=-EACCES) |
bc4f2401 | 222 | pm_runtime_put_sync(&sdev->sdev_gendev); |
632e270e | 223 | else |
bc4f2401 AS |
224 | err = 0; |
225 | return err; | |
226 | } | |
227 | EXPORT_SYMBOL_GPL(scsi_autopm_get_device); | |
228 | ||
229 | void scsi_autopm_put_device(struct scsi_device *sdev) | |
230 | { | |
231 | pm_runtime_put_sync(&sdev->sdev_gendev); | |
232 | } | |
233 | EXPORT_SYMBOL_GPL(scsi_autopm_put_device); | |
234 | ||
235 | void scsi_autopm_get_target(struct scsi_target *starget) | |
236 | { | |
237 | pm_runtime_get_sync(&starget->dev); | |
238 | } | |
239 | ||
240 | void scsi_autopm_put_target(struct scsi_target *starget) | |
241 | { | |
242 | pm_runtime_put_sync(&starget->dev); | |
243 | } | |
244 | ||
245 | int scsi_autopm_get_host(struct Scsi_Host *shost) | |
246 | { | |
247 | int err; | |
248 | ||
249 | err = pm_runtime_get_sync(&shost->shost_gendev); | |
632e270e | 250 | if (err < 0 && err !=-EACCES) |
bc4f2401 | 251 | pm_runtime_put_sync(&shost->shost_gendev); |
632e270e | 252 | else |
bc4f2401 AS |
253 | err = 0; |
254 | return err; | |
255 | } | |
256 | ||
257 | void scsi_autopm_put_host(struct Scsi_Host *shost) | |
258 | { | |
259 | pm_runtime_put_sync(&shost->shost_gendev); | |
260 | } | |
261 | ||
db5bd1e0 | 262 | const struct dev_pm_ops scsi_bus_pm_ops = { |
fea6d607 | 263 | .prepare = scsi_bus_prepare, |
db5bd1e0 | 264 | .suspend = scsi_bus_suspend, |
80d2fd48 | 265 | .resume = scsi_bus_resume, |
db5bd1e0 | 266 | .freeze = scsi_bus_freeze, |
80d2fd48 | 267 | .thaw = scsi_bus_thaw, |
db5bd1e0 | 268 | .poweroff = scsi_bus_poweroff, |
80d2fd48 | 269 | .restore = scsi_bus_restore, |
bc4f2401 AS |
270 | .runtime_suspend = scsi_runtime_suspend, |
271 | .runtime_resume = scsi_runtime_resume, | |
272 | .runtime_idle = scsi_runtime_idle, | |
db5bd1e0 | 273 | }; |