xref: /openbmc/linux/drivers/scsi/scsi_pm.c (revision 09717af7)
1 // SPDX-License-Identifier: GPL-2.0-only
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>
10 #include <linux/export.h>
11 #include <linux/async.h>
12 #include <linux/blk-pm.h>
13 
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 
21 #ifdef CONFIG_PM_SLEEP
22 
23 static int do_scsi_suspend(struct device *dev, const struct dev_pm_ops *pm)
24 {
25 	return pm && pm->suspend ? pm->suspend(dev) : 0;
26 }
27 
28 static 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 
33 static 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 
38 static 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 
43 static 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 
48 static 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 
53 static 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;
57 	int err;
58 
59 	err = scsi_device_quiesce(to_scsi_device(dev));
60 	if (err == 0) {
61 		err = cb(dev, pm);
62 		if (err)
63 			scsi_device_resume(to_scsi_device(dev));
64 	}
65 	dev_dbg(dev, "scsi suspend: %d\n", err);
66 	return err;
67 }
68 
69 static int
70 scsi_bus_suspend_common(struct device *dev,
71 		int (*cb)(struct device *, const struct dev_pm_ops *))
72 {
73 	if (!scsi_is_sdev_device(dev))
74 		return 0;
75 
76 	return scsi_dev_type_suspend(dev, cb);
77 }
78 
79 static int scsi_bus_resume_common(struct device *dev,
80 		int (*cb)(struct device *, const struct dev_pm_ops *))
81 {
82 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
83 	int err;
84 
85 	if (!scsi_is_sdev_device(dev))
86 		return 0;
87 
88 	err = cb(dev, pm);
89 	scsi_device_resume(to_scsi_device(dev));
90 	dev_dbg(dev, "scsi resume: %d\n", err);
91 
92 	return err;
93 }
94 
95 static int scsi_bus_prepare(struct device *dev)
96 {
97 	if (scsi_is_host_device(dev)) {
98 		/* Wait until async scanning is finished */
99 		scsi_complete_async_scans();
100 	}
101 	return 0;
102 }
103 
104 static int scsi_bus_suspend(struct device *dev)
105 {
106 	return scsi_bus_suspend_common(dev, do_scsi_suspend);
107 }
108 
109 static int scsi_bus_resume(struct device *dev)
110 {
111 	return scsi_bus_resume_common(dev, do_scsi_resume);
112 }
113 
114 static int scsi_bus_freeze(struct device *dev)
115 {
116 	return scsi_bus_suspend_common(dev, do_scsi_freeze);
117 }
118 
119 static int scsi_bus_thaw(struct device *dev)
120 {
121 	return scsi_bus_resume_common(dev, do_scsi_thaw);
122 }
123 
124 static int scsi_bus_poweroff(struct device *dev)
125 {
126 	return scsi_bus_suspend_common(dev, do_scsi_poweroff);
127 }
128 
129 static int scsi_bus_restore(struct device *dev)
130 {
131 	return scsi_bus_resume_common(dev, do_scsi_restore);
132 }
133 
134 #else /* CONFIG_PM_SLEEP */
135 
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
143 
144 #endif /* CONFIG_PM_SLEEP */
145 
146 static int sdev_runtime_suspend(struct device *dev)
147 {
148 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
149 	struct scsi_device *sdev = to_scsi_device(dev);
150 	int err = 0;
151 
152 	err = blk_pre_runtime_suspend(sdev->request_queue);
153 	if (err)
154 		return err;
155 	if (pm && pm->runtime_suspend)
156 		err = pm->runtime_suspend(dev);
157 	blk_post_runtime_suspend(sdev->request_queue, err);
158 
159 	return err;
160 }
161 
162 static int scsi_runtime_suspend(struct device *dev)
163 {
164 	int err = 0;
165 
166 	dev_dbg(dev, "scsi_runtime_suspend\n");
167 	if (scsi_is_sdev_device(dev))
168 		err = sdev_runtime_suspend(dev);
169 
170 	/* Insert hooks here for targets, hosts, and transport classes */
171 
172 	return err;
173 }
174 
175 static int sdev_runtime_resume(struct device *dev)
176 {
177 	struct scsi_device *sdev = to_scsi_device(dev);
178 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
179 	int err = 0;
180 
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);
185 
186 	return err;
187 }
188 
189 static int scsi_runtime_resume(struct device *dev)
190 {
191 	int err = 0;
192 
193 	dev_dbg(dev, "scsi_runtime_resume\n");
194 	if (scsi_is_sdev_device(dev))
195 		err = sdev_runtime_resume(dev);
196 
197 	/* Insert hooks here for targets, hosts, and transport classes */
198 
199 	return err;
200 }
201 
202 static int scsi_runtime_idle(struct device *dev)
203 {
204 	dev_dbg(dev, "scsi_runtime_idle\n");
205 
206 	/* Insert hooks here for targets, hosts, and transport classes */
207 
208 	if (scsi_is_sdev_device(dev)) {
209 		pm_runtime_mark_last_busy(dev);
210 		pm_runtime_autosuspend(dev);
211 		return -EBUSY;
212 	}
213 
214 	return 0;
215 }
216 
217 int scsi_autopm_get_device(struct scsi_device *sdev)
218 {
219 	int	err;
220 
221 	err = pm_runtime_get_sync(&sdev->sdev_gendev);
222 	if (err < 0 && err !=-EACCES)
223 		pm_runtime_put_sync(&sdev->sdev_gendev);
224 	else
225 		err = 0;
226 	return err;
227 }
228 EXPORT_SYMBOL_GPL(scsi_autopm_get_device);
229 
230 void scsi_autopm_put_device(struct scsi_device *sdev)
231 {
232 	pm_runtime_put_sync(&sdev->sdev_gendev);
233 }
234 EXPORT_SYMBOL_GPL(scsi_autopm_put_device);
235 
236 void scsi_autopm_get_target(struct scsi_target *starget)
237 {
238 	pm_runtime_get_sync(&starget->dev);
239 }
240 
241 void scsi_autopm_put_target(struct scsi_target *starget)
242 {
243 	pm_runtime_put_sync(&starget->dev);
244 }
245 
246 int scsi_autopm_get_host(struct Scsi_Host *shost)
247 {
248 	int	err;
249 
250 	err = pm_runtime_get_sync(&shost->shost_gendev);
251 	if (err < 0 && err !=-EACCES)
252 		pm_runtime_put_sync(&shost->shost_gendev);
253 	else
254 		err = 0;
255 	return err;
256 }
257 
258 void scsi_autopm_put_host(struct Scsi_Host *shost)
259 {
260 	pm_runtime_put_sync(&shost->shost_gendev);
261 }
262 
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,
274 };
275