Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 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> |
Paul Gortmaker | 0970366 | 2011-05-27 09:37:25 -0400 | [diff] [blame] | 10 | #include <linux/export.h> |
Alan Stern | fea6d60 | 2012-02-17 16:25:08 -0500 | [diff] [blame] | 11 | #include <linux/async.h> |
Bart Van Assche | bca6b06 | 2018-09-26 14:01:03 -0700 | [diff] [blame] | 12 | #include <linux/blk-pm.h> |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 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 | |
Aaron Lu | 6627b38 | 2013-10-28 15:27:49 +0800 | [diff] [blame] | 21 | #ifdef CONFIG_PM_SLEEP |
| 22 | |
Dan Williams | 3c31b52 | 2014-04-10 15:30:35 -0700 | [diff] [blame] | 23 | static int do_scsi_suspend(struct device *dev, const struct dev_pm_ops *pm) |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 24 | { |
Dan Williams | 3c31b52 | 2014-04-10 15:30:35 -0700 | [diff] [blame] | 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; |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 57 | int err; |
| 58 | |
| 59 | err = scsi_device_quiesce(to_scsi_device(dev)); |
| 60 | if (err == 0) { |
Dan Williams | 3c31b52 | 2014-04-10 15:30:35 -0700 | [diff] [blame] | 61 | err = cb(dev, pm); |
| 62 | if (err) |
| 63 | scsi_device_resume(to_scsi_device(dev)); |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 64 | } |
| 65 | dev_dbg(dev, "scsi suspend: %d\n", err); |
| 66 | return err; |
| 67 | } |
| 68 | |
Aaron Lu | 80d2fd4 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 69 | static int |
Dan Williams | 3c31b52 | 2014-04-10 15:30:35 -0700 | [diff] [blame] | 70 | scsi_bus_suspend_common(struct device *dev, |
| 71 | int (*cb)(struct device *, const struct dev_pm_ops *)) |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 72 | { |
Bart Van Assche | 9131bff | 2021-10-06 14:54:53 -0700 | [diff] [blame^] | 73 | if (!scsi_is_sdev_device(dev)) |
| 74 | return 0; |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 75 | |
Bart Van Assche | 9131bff | 2021-10-06 14:54:53 -0700 | [diff] [blame^] | 76 | return scsi_dev_type_suspend(dev, cb); |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Dan Williams | 3c31b52 | 2014-04-10 15:30:35 -0700 | [diff] [blame] | 79 | static int scsi_bus_resume_common(struct device *dev, |
| 80 | int (*cb)(struct device *, const struct dev_pm_ops *)) |
| 81 | { |
Bart Van Assche | 9131bff | 2021-10-06 14:54:53 -0700 | [diff] [blame^] | 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; |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Alan Stern | fea6d60 | 2012-02-17 16:25:08 -0500 | [diff] [blame] | 95 | static int scsi_bus_prepare(struct device *dev) |
| 96 | { |
Bart Van Assche | f049cf1 | 2019-04-30 14:39:18 -0700 | [diff] [blame] | 97 | if (scsi_is_host_device(dev)) { |
Alan Stern | fea6d60 | 2012-02-17 16:25:08 -0500 | [diff] [blame] | 98 | /* Wait until async scanning is finished */ |
| 99 | scsi_complete_async_scans(); |
| 100 | } |
| 101 | return 0; |
| 102 | } |
| 103 | |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 104 | static int scsi_bus_suspend(struct device *dev) |
| 105 | { |
Dan Williams | 3c31b52 | 2014-04-10 15:30:35 -0700 | [diff] [blame] | 106 | return scsi_bus_suspend_common(dev, do_scsi_suspend); |
Aaron Lu | 80d2fd4 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static int scsi_bus_resume(struct device *dev) |
| 110 | { |
Dan Williams | 3c31b52 | 2014-04-10 15:30:35 -0700 | [diff] [blame] | 111 | return scsi_bus_resume_common(dev, do_scsi_resume); |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static int scsi_bus_freeze(struct device *dev) |
| 115 | { |
Dan Williams | 3c31b52 | 2014-04-10 15:30:35 -0700 | [diff] [blame] | 116 | return scsi_bus_suspend_common(dev, do_scsi_freeze); |
Aaron Lu | 80d2fd4 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static int scsi_bus_thaw(struct device *dev) |
| 120 | { |
Dan Williams | 3c31b52 | 2014-04-10 15:30:35 -0700 | [diff] [blame] | 121 | return scsi_bus_resume_common(dev, do_scsi_thaw); |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static int scsi_bus_poweroff(struct device *dev) |
| 125 | { |
Dan Williams | 3c31b52 | 2014-04-10 15:30:35 -0700 | [diff] [blame] | 126 | return scsi_bus_suspend_common(dev, do_scsi_poweroff); |
Aaron Lu | 80d2fd4 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static int scsi_bus_restore(struct device *dev) |
| 130 | { |
Dan Williams | 3c31b52 | 2014-04-10 15:30:35 -0700 | [diff] [blame] | 131 | return scsi_bus_resume_common(dev, do_scsi_restore); |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | #else /* CONFIG_PM_SLEEP */ |
| 135 | |
Alan Stern | fea6d60 | 2012-02-17 16:25:08 -0500 | [diff] [blame] | 136 | #define scsi_bus_prepare NULL |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 137 | #define scsi_bus_suspend NULL |
Aaron Lu | 80d2fd4 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 138 | #define scsi_bus_resume NULL |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 139 | #define scsi_bus_freeze NULL |
Aaron Lu | 80d2fd4 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 140 | #define scsi_bus_thaw NULL |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 141 | #define scsi_bus_poweroff NULL |
Aaron Lu | 80d2fd4 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 142 | #define scsi_bus_restore NULL |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 143 | |
| 144 | #endif /* CONFIG_PM_SLEEP */ |
| 145 | |
Aaron Lu | 6627b38 | 2013-10-28 15:27:49 +0800 | [diff] [blame] | 146 | static int sdev_runtime_suspend(struct device *dev) |
Lin Ming | 6df339a | 2013-03-23 11:42:28 +0800 | [diff] [blame] | 147 | { |
Aaron Lu | 6627b38 | 2013-10-28 15:27:49 +0800 | [diff] [blame] | 148 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 149 | struct scsi_device *sdev = to_scsi_device(dev); |
Alan Stern | 49718f0 | 2015-08-17 11:02:42 -0400 | [diff] [blame] | 150 | int err = 0; |
Lin Ming | 6df339a | 2013-03-23 11:42:28 +0800 | [diff] [blame] | 151 | |
Ken Xue | 1c69d3b | 2015-12-01 14:45:23 +0800 | [diff] [blame] | 152 | err = blk_pre_runtime_suspend(sdev->request_queue); |
| 153 | if (err) |
| 154 | return err; |
| 155 | if (pm && pm->runtime_suspend) |
Aaron Lu | 6627b38 | 2013-10-28 15:27:49 +0800 | [diff] [blame] | 156 | err = pm->runtime_suspend(dev); |
Ken Xue | 1c69d3b | 2015-12-01 14:45:23 +0800 | [diff] [blame] | 157 | blk_post_runtime_suspend(sdev->request_queue, err); |
| 158 | |
Lin Ming | 6df339a | 2013-03-23 11:42:28 +0800 | [diff] [blame] | 159 | return err; |
| 160 | } |
| 161 | |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 162 | static int scsi_runtime_suspend(struct device *dev) |
| 163 | { |
| 164 | int err = 0; |
| 165 | |
| 166 | dev_dbg(dev, "scsi_runtime_suspend\n"); |
Lin Ming | 6df339a | 2013-03-23 11:42:28 +0800 | [diff] [blame] | 167 | if (scsi_is_sdev_device(dev)) |
| 168 | err = sdev_runtime_suspend(dev); |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 169 | |
| 170 | /* Insert hooks here for targets, hosts, and transport classes */ |
| 171 | |
| 172 | return err; |
| 173 | } |
| 174 | |
Lin Ming | 6df339a | 2013-03-23 11:42:28 +0800 | [diff] [blame] | 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; |
Aaron Lu | 6627b38 | 2013-10-28 15:27:49 +0800 | [diff] [blame] | 179 | int err = 0; |
Lin Ming | 6df339a | 2013-03-23 11:42:28 +0800 | [diff] [blame] | 180 | |
Ken Xue | 1c69d3b | 2015-12-01 14:45:23 +0800 | [diff] [blame] | 181 | blk_pre_runtime_resume(sdev->request_queue); |
| 182 | if (pm && pm->runtime_resume) |
Aaron Lu | 6627b38 | 2013-10-28 15:27:49 +0800 | [diff] [blame] | 183 | err = pm->runtime_resume(dev); |
Ken Xue | 1c69d3b | 2015-12-01 14:45:23 +0800 | [diff] [blame] | 184 | blk_post_runtime_resume(sdev->request_queue, err); |
| 185 | |
Aaron Lu | 6627b38 | 2013-10-28 15:27:49 +0800 | [diff] [blame] | 186 | return err; |
Lin Ming | 6df339a | 2013-03-23 11:42:28 +0800 | [diff] [blame] | 187 | } |
| 188 | |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 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)) |
Lin Ming | 6df339a | 2013-03-23 11:42:28 +0800 | [diff] [blame] | 195 | err = sdev_runtime_resume(dev); |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 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 | { |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 204 | dev_dbg(dev, "scsi_runtime_idle\n"); |
| 205 | |
| 206 | /* Insert hooks here for targets, hosts, and transport classes */ |
| 207 | |
Lin Ming | 6df339a | 2013-03-23 11:42:28 +0800 | [diff] [blame] | 208 | if (scsi_is_sdev_device(dev)) { |
Aaron Lu | 6627b38 | 2013-10-28 15:27:49 +0800 | [diff] [blame] | 209 | pm_runtime_mark_last_busy(dev); |
| 210 | pm_runtime_autosuspend(dev); |
| 211 | return -EBUSY; |
Lin Ming | 6df339a | 2013-03-23 11:42:28 +0800 | [diff] [blame] | 212 | } |
Aaron Lu | 6627b38 | 2013-10-28 15:27:49 +0800 | [diff] [blame] | 213 | |
Rafael J. Wysocki | 45f0a85 | 2013-06-03 21:49:52 +0200 | [diff] [blame] | 214 | return 0; |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 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); |
Rafael J. Wysocki | 632e270 | 2011-07-01 22:29:15 +0200 | [diff] [blame] | 222 | if (err < 0 && err !=-EACCES) |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 223 | pm_runtime_put_sync(&sdev->sdev_gendev); |
Rafael J. Wysocki | 632e270 | 2011-07-01 22:29:15 +0200 | [diff] [blame] | 224 | else |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 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); |
Rafael J. Wysocki | 632e270 | 2011-07-01 22:29:15 +0200 | [diff] [blame] | 251 | if (err < 0 && err !=-EACCES) |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 252 | pm_runtime_put_sync(&shost->shost_gendev); |
Rafael J. Wysocki | 632e270 | 2011-07-01 22:29:15 +0200 | [diff] [blame] | 253 | else |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 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 | |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 263 | const struct dev_pm_ops scsi_bus_pm_ops = { |
Alan Stern | fea6d60 | 2012-02-17 16:25:08 -0500 | [diff] [blame] | 264 | .prepare = scsi_bus_prepare, |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 265 | .suspend = scsi_bus_suspend, |
Aaron Lu | 80d2fd4 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 266 | .resume = scsi_bus_resume, |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 267 | .freeze = scsi_bus_freeze, |
Aaron Lu | 80d2fd4 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 268 | .thaw = scsi_bus_thaw, |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 269 | .poweroff = scsi_bus_poweroff, |
Aaron Lu | 80d2fd4 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 270 | .restore = scsi_bus_restore, |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 271 | .runtime_suspend = scsi_runtime_suspend, |
| 272 | .runtime_resume = scsi_runtime_resume, |
| 273 | .runtime_idle = scsi_runtime_idle, |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 274 | }; |