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