Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 1 | /* |
| 2 | * scsi_pm.c Copyright (C) 2010 Alan Stern |
| 3 | * |
| 4 | * SCSI dynamic Power Management |
| 5 | * Initial version: Alan Stern <stern@rowland.harvard.edu> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/pm_runtime.h> |
Paul Gortmaker | 0970366 | 2011-05-27 09:37:25 -0400 | [diff] [blame] | 9 | #include <linux/export.h> |
Alan Stern | fea6d60 | 2012-02-17 16:25:08 -0500 | [diff] [blame] | 10 | #include <linux/async.h> |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 11 | |
| 12 | #include <scsi/scsi.h> |
| 13 | #include <scsi/scsi_device.h> |
| 14 | #include <scsi/scsi_driver.h> |
| 15 | #include <scsi/scsi_host.h> |
| 16 | |
| 17 | #include "scsi_priv.h" |
| 18 | |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 19 | static int scsi_dev_type_suspend(struct device *dev, int (*cb)(struct device *)) |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 20 | { |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 21 | int err; |
| 22 | |
| 23 | err = scsi_device_quiesce(to_scsi_device(dev)); |
| 24 | if (err == 0) { |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 25 | if (cb) { |
| 26 | err = cb(dev); |
Aaron Lu | 845abae | 2012-05-15 14:43:00 +0800 | [diff] [blame] | 27 | if (err) |
| 28 | scsi_device_resume(to_scsi_device(dev)); |
| 29 | } |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 30 | } |
| 31 | dev_dbg(dev, "scsi suspend: %d\n", err); |
| 32 | return err; |
| 33 | } |
| 34 | |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 35 | static int scsi_dev_type_resume(struct device *dev, int (*cb)(struct device *)) |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 36 | { |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 37 | int err = 0; |
| 38 | |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 39 | if (cb) |
| 40 | err = cb(dev); |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 41 | scsi_device_resume(to_scsi_device(dev)); |
| 42 | dev_dbg(dev, "scsi resume: %d\n", err); |
| 43 | return err; |
| 44 | } |
| 45 | |
| 46 | #ifdef CONFIG_PM_SLEEP |
| 47 | |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 48 | static int |
| 49 | scsi_bus_suspend_common(struct device *dev, int (*cb)(struct device *)) |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 50 | { |
| 51 | int err = 0; |
| 52 | |
Lin Ming | 2864051 | 2011-12-05 09:20:25 +0800 | [diff] [blame] | 53 | if (scsi_is_sdev_device(dev)) { |
| 54 | /* |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 55 | * All the high-level SCSI drivers that implement runtime |
| 56 | * PM treat runtime suspend, system suspend, and system |
| 57 | * hibernate identically. |
Lin Ming | 2864051 | 2011-12-05 09:20:25 +0800 | [diff] [blame] | 58 | */ |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 59 | if (pm_runtime_suspended(dev)) |
| 60 | return 0; |
Lin Ming | 2864051 | 2011-12-05 09:20:25 +0800 | [diff] [blame] | 61 | |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 62 | err = scsi_dev_type_suspend(dev, cb); |
Lin Ming | 2864051 | 2011-12-05 09:20:25 +0800 | [diff] [blame] | 63 | } |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 64 | |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 65 | return err; |
| 66 | } |
| 67 | |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 68 | static int |
| 69 | scsi_bus_resume_common(struct device *dev, int (*cb)(struct device *)) |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 70 | { |
| 71 | int err = 0; |
| 72 | |
Lin Ming | 28fd00d | 2011-12-22 14:50:47 +0800 | [diff] [blame] | 73 | if (scsi_is_sdev_device(dev)) { |
| 74 | /* |
| 75 | * Parent device may have runtime suspended as soon as |
| 76 | * it is woken up during the system resume. |
| 77 | * |
| 78 | * Resume it on behalf of child. |
| 79 | */ |
| 80 | pm_runtime_get_sync(dev->parent); |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 81 | err = scsi_dev_type_resume(dev, cb); |
Lin Ming | 28fd00d | 2011-12-22 14:50:47 +0800 | [diff] [blame] | 82 | pm_runtime_put_sync(dev->parent); |
| 83 | } |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 84 | |
| 85 | if (err == 0) { |
| 86 | pm_runtime_disable(dev); |
| 87 | pm_runtime_set_active(dev); |
| 88 | pm_runtime_enable(dev); |
| 89 | } |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 90 | return err; |
| 91 | } |
| 92 | |
Alan Stern | fea6d60 | 2012-02-17 16:25:08 -0500 | [diff] [blame] | 93 | static int scsi_bus_prepare(struct device *dev) |
| 94 | { |
| 95 | if (scsi_is_sdev_device(dev)) { |
| 96 | /* sd probing uses async_schedule. Wait until it finishes. */ |
| 97 | async_synchronize_full(); |
| 98 | |
| 99 | } else if (scsi_is_host_device(dev)) { |
| 100 | /* Wait until async scanning is finished */ |
| 101 | scsi_complete_async_scans(); |
| 102 | } |
| 103 | return 0; |
| 104 | } |
| 105 | |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 106 | static int scsi_bus_suspend(struct device *dev) |
| 107 | { |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 108 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 109 | return scsi_bus_suspend_common(dev, pm ? pm->suspend : NULL); |
| 110 | } |
| 111 | |
| 112 | static int scsi_bus_resume(struct device *dev) |
| 113 | { |
| 114 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 115 | return scsi_bus_resume_common(dev, pm ? pm->resume : NULL); |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static int scsi_bus_freeze(struct device *dev) |
| 119 | { |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 120 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 121 | return scsi_bus_suspend_common(dev, pm ? pm->freeze : NULL); |
| 122 | } |
| 123 | |
| 124 | static int scsi_bus_thaw(struct device *dev) |
| 125 | { |
| 126 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 127 | return scsi_bus_resume_common(dev, pm ? pm->thaw : NULL); |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static int scsi_bus_poweroff(struct device *dev) |
| 131 | { |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 132 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 133 | return scsi_bus_suspend_common(dev, pm ? pm->poweroff : NULL); |
| 134 | } |
| 135 | |
| 136 | static int scsi_bus_restore(struct device *dev) |
| 137 | { |
| 138 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 139 | return scsi_bus_resume_common(dev, pm ? pm->restore : NULL); |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | #else /* CONFIG_PM_SLEEP */ |
| 143 | |
Alan Stern | fea6d60 | 2012-02-17 16:25:08 -0500 | [diff] [blame] | 144 | #define scsi_bus_prepare NULL |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 145 | #define scsi_bus_suspend NULL |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 146 | #define scsi_bus_resume NULL |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 147 | #define scsi_bus_freeze NULL |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 148 | #define scsi_bus_thaw NULL |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 149 | #define scsi_bus_poweroff NULL |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 150 | #define scsi_bus_restore NULL |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 151 | |
| 152 | #endif /* CONFIG_PM_SLEEP */ |
| 153 | |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 154 | #ifdef CONFIG_PM_RUNTIME |
| 155 | |
Lin Ming | 00fdcd7 | 2013-03-23 11:42:28 +0800 | [diff] [blame] | 156 | static int sdev_blk_runtime_suspend(struct scsi_device *sdev, |
| 157 | int (*cb)(struct device *)) |
| 158 | { |
| 159 | int err; |
| 160 | |
| 161 | err = blk_pre_runtime_suspend(sdev->request_queue); |
| 162 | if (err) |
| 163 | return err; |
| 164 | if (cb) |
| 165 | err = cb(&sdev->sdev_gendev); |
| 166 | blk_post_runtime_suspend(sdev->request_queue, err); |
| 167 | |
| 168 | return err; |
| 169 | } |
| 170 | |
| 171 | static int sdev_runtime_suspend(struct device *dev) |
| 172 | { |
| 173 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 174 | int (*cb)(struct device *) = pm ? pm->runtime_suspend : NULL; |
| 175 | struct scsi_device *sdev = to_scsi_device(dev); |
| 176 | int err; |
| 177 | |
| 178 | if (sdev->request_queue->dev) |
| 179 | return sdev_blk_runtime_suspend(sdev, cb); |
| 180 | |
| 181 | err = scsi_dev_type_suspend(dev, cb); |
| 182 | if (err == -EAGAIN) |
| 183 | pm_schedule_suspend(dev, jiffies_to_msecs( |
| 184 | round_jiffies_up_relative(HZ/10))); |
| 185 | return err; |
| 186 | } |
| 187 | |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 188 | static int scsi_runtime_suspend(struct device *dev) |
| 189 | { |
| 190 | int err = 0; |
| 191 | |
| 192 | dev_dbg(dev, "scsi_runtime_suspend\n"); |
Lin Ming | 00fdcd7 | 2013-03-23 11:42:28 +0800 | [diff] [blame] | 193 | if (scsi_is_sdev_device(dev)) |
| 194 | err = sdev_runtime_suspend(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 | |
Lin Ming | 00fdcd7 | 2013-03-23 11:42:28 +0800 | [diff] [blame] | 201 | static int sdev_blk_runtime_resume(struct scsi_device *sdev, |
| 202 | int (*cb)(struct device *)) |
| 203 | { |
| 204 | int err = 0; |
| 205 | |
| 206 | blk_pre_runtime_resume(sdev->request_queue); |
| 207 | if (cb) |
| 208 | err = cb(&sdev->sdev_gendev); |
| 209 | blk_post_runtime_resume(sdev->request_queue, err); |
| 210 | |
| 211 | return err; |
| 212 | } |
| 213 | |
| 214 | static int sdev_runtime_resume(struct device *dev) |
| 215 | { |
| 216 | struct scsi_device *sdev = to_scsi_device(dev); |
| 217 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 218 | int (*cb)(struct device *) = pm ? pm->runtime_resume : NULL; |
| 219 | |
| 220 | if (sdev->request_queue->dev) |
| 221 | return sdev_blk_runtime_resume(sdev, cb); |
| 222 | else |
| 223 | return scsi_dev_type_resume(dev, cb); |
| 224 | } |
| 225 | |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 226 | static int scsi_runtime_resume(struct device *dev) |
| 227 | { |
| 228 | int err = 0; |
| 229 | |
| 230 | dev_dbg(dev, "scsi_runtime_resume\n"); |
| 231 | if (scsi_is_sdev_device(dev)) |
Lin Ming | 00fdcd7 | 2013-03-23 11:42:28 +0800 | [diff] [blame] | 232 | err = sdev_runtime_resume(dev); |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 233 | |
| 234 | /* Insert hooks here for targets, hosts, and transport classes */ |
| 235 | |
| 236 | return err; |
| 237 | } |
| 238 | |
| 239 | static int scsi_runtime_idle(struct device *dev) |
| 240 | { |
| 241 | int err; |
| 242 | |
| 243 | dev_dbg(dev, "scsi_runtime_idle\n"); |
| 244 | |
| 245 | /* Insert hooks here for targets, hosts, and transport classes */ |
| 246 | |
Lin Ming | 00fdcd7 | 2013-03-23 11:42:28 +0800 | [diff] [blame] | 247 | if (scsi_is_sdev_device(dev)) { |
| 248 | struct scsi_device *sdev = to_scsi_device(dev); |
| 249 | |
| 250 | if (sdev->request_queue->dev) { |
| 251 | pm_runtime_mark_last_busy(dev); |
| 252 | err = pm_runtime_autosuspend(dev); |
| 253 | } else { |
| 254 | err = pm_runtime_suspend(dev); |
| 255 | } |
| 256 | } else { |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 257 | err = pm_runtime_suspend(dev); |
Lin Ming | 00fdcd7 | 2013-03-23 11:42:28 +0800 | [diff] [blame] | 258 | } |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 259 | return err; |
| 260 | } |
| 261 | |
| 262 | int scsi_autopm_get_device(struct scsi_device *sdev) |
| 263 | { |
| 264 | int err; |
| 265 | |
| 266 | err = pm_runtime_get_sync(&sdev->sdev_gendev); |
Rafael J. Wysocki | 632e270 | 2011-07-01 22:29:15 +0200 | [diff] [blame] | 267 | if (err < 0 && err !=-EACCES) |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 268 | pm_runtime_put_sync(&sdev->sdev_gendev); |
Rafael J. Wysocki | 632e270 | 2011-07-01 22:29:15 +0200 | [diff] [blame] | 269 | else |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 270 | err = 0; |
| 271 | return err; |
| 272 | } |
| 273 | EXPORT_SYMBOL_GPL(scsi_autopm_get_device); |
| 274 | |
| 275 | void scsi_autopm_put_device(struct scsi_device *sdev) |
| 276 | { |
| 277 | pm_runtime_put_sync(&sdev->sdev_gendev); |
| 278 | } |
| 279 | EXPORT_SYMBOL_GPL(scsi_autopm_put_device); |
| 280 | |
| 281 | void scsi_autopm_get_target(struct scsi_target *starget) |
| 282 | { |
| 283 | pm_runtime_get_sync(&starget->dev); |
| 284 | } |
| 285 | |
| 286 | void scsi_autopm_put_target(struct scsi_target *starget) |
| 287 | { |
| 288 | pm_runtime_put_sync(&starget->dev); |
| 289 | } |
| 290 | |
| 291 | int scsi_autopm_get_host(struct Scsi_Host *shost) |
| 292 | { |
| 293 | int err; |
| 294 | |
| 295 | err = pm_runtime_get_sync(&shost->shost_gendev); |
Rafael J. Wysocki | 632e270 | 2011-07-01 22:29:15 +0200 | [diff] [blame] | 296 | if (err < 0 && err !=-EACCES) |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 297 | pm_runtime_put_sync(&shost->shost_gendev); |
Rafael J. Wysocki | 632e270 | 2011-07-01 22:29:15 +0200 | [diff] [blame] | 298 | else |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 299 | err = 0; |
| 300 | return err; |
| 301 | } |
| 302 | |
| 303 | void scsi_autopm_put_host(struct Scsi_Host *shost) |
| 304 | { |
| 305 | pm_runtime_put_sync(&shost->shost_gendev); |
| 306 | } |
| 307 | |
| 308 | #else |
| 309 | |
| 310 | #define scsi_runtime_suspend NULL |
| 311 | #define scsi_runtime_resume NULL |
| 312 | #define scsi_runtime_idle NULL |
| 313 | |
| 314 | #endif /* CONFIG_PM_RUNTIME */ |
| 315 | |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 316 | const struct dev_pm_ops scsi_bus_pm_ops = { |
Alan Stern | fea6d60 | 2012-02-17 16:25:08 -0500 | [diff] [blame] | 317 | .prepare = scsi_bus_prepare, |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 318 | .suspend = scsi_bus_suspend, |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 319 | .resume = scsi_bus_resume, |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 320 | .freeze = scsi_bus_freeze, |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 321 | .thaw = scsi_bus_thaw, |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 322 | .poweroff = scsi_bus_poweroff, |
Aaron Lu | 0ed2aa6 | 2012-11-09 15:27:54 +0800 | [diff] [blame] | 323 | .restore = scsi_bus_restore, |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 324 | .runtime_suspend = scsi_runtime_suspend, |
| 325 | .runtime_resume = scsi_runtime_resume, |
| 326 | .runtime_idle = scsi_runtime_idle, |
Alan Stern | db5bd1e | 2010-06-17 10:36:49 -0400 | [diff] [blame] | 327 | }; |