| 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> |
| 9 | #include <linux/export.h> |
| 10 | #include <linux/async.h> |
| 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 | |
| 19 | static int scsi_dev_type_suspend(struct device *dev, pm_message_t msg) |
| 20 | { |
| 21 | struct device_driver *drv; |
| 22 | int err; |
| 23 | |
| 24 | err = scsi_device_quiesce(to_scsi_device(dev)); |
| 25 | if (err == 0) { |
| 26 | drv = dev->driver; |
| 27 | if (drv && drv->suspend) { |
| 28 | err = drv->suspend(dev, msg); |
| 29 | if (err) |
| 30 | scsi_device_resume(to_scsi_device(dev)); |
| 31 | } |
| 32 | } |
| 33 | dev_dbg(dev, "scsi suspend: %d\n", err); |
| 34 | return err; |
| 35 | } |
| 36 | |
| 37 | static int scsi_dev_type_resume(struct device *dev) |
| 38 | { |
| 39 | struct device_driver *drv; |
| 40 | int err = 0; |
| 41 | |
| 42 | drv = dev->driver; |
| 43 | if (drv && drv->resume) |
| 44 | err = drv->resume(dev); |
| 45 | scsi_device_resume(to_scsi_device(dev)); |
| 46 | dev_dbg(dev, "scsi resume: %d\n", err); |
| 47 | return err; |
| 48 | } |
| 49 | |
| 50 | #ifdef CONFIG_PM_SLEEP |
| 51 | |
| 52 | static int scsi_bus_suspend_common(struct device *dev, pm_message_t msg) |
| 53 | { |
| 54 | int err = 0; |
| 55 | |
| 56 | if (scsi_is_sdev_device(dev)) { |
| 57 | /* |
| 58 | * sd is the only high-level SCSI driver to implement runtime |
| 59 | * PM, and sd treats runtime suspend, system suspend, and |
| 60 | * system hibernate identically (but not system freeze). |
| 61 | */ |
| 62 | if (pm_runtime_suspended(dev)) { |
| 63 | if (msg.event == PM_EVENT_SUSPEND || |
| 64 | msg.event == PM_EVENT_HIBERNATE) |
| 65 | return 0; /* already suspended */ |
| 66 | |
| 67 | /* wake up device so that FREEZE will succeed */ |
| 68 | pm_runtime_resume(dev); |
| 69 | } |
| 70 | err = scsi_dev_type_suspend(dev, msg); |
| 71 | } |
| 72 | return err; |
| 73 | } |
| 74 | |
| 75 | static int scsi_bus_resume_common(struct device *dev) |
| 76 | { |
| 77 | int err = 0; |
| 78 | |
| 79 | if (scsi_is_sdev_device(dev)) |
| 80 | err = scsi_dev_type_resume(dev); |
| 81 | |
| 82 | if (err == 0) { |
| 83 | pm_runtime_disable(dev); |
| 84 | pm_runtime_set_active(dev); |
| 85 | pm_runtime_enable(dev); |
| 86 | } |
| 87 | return err; |
| 88 | } |
| 89 | |
| 90 | static int scsi_bus_prepare(struct device *dev) |
| 91 | { |
| 92 | if (scsi_is_sdev_device(dev)) { |
| 93 | /* sd probing uses async_schedule. Wait until it finishes. */ |
| 94 | async_synchronize_full_domain(&scsi_sd_probe_domain); |
| 95 | |
| 96 | } else if (scsi_is_host_device(dev)) { |
| 97 | /* Wait until async scanning is finished */ |
| 98 | scsi_complete_async_scans(); |
| 99 | } |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int scsi_bus_suspend(struct device *dev) |
| 104 | { |
| 105 | return scsi_bus_suspend_common(dev, PMSG_SUSPEND); |
| 106 | } |
| 107 | |
| 108 | static int scsi_bus_freeze(struct device *dev) |
| 109 | { |
| 110 | return scsi_bus_suspend_common(dev, PMSG_FREEZE); |
| 111 | } |
| 112 | |
| 113 | static int scsi_bus_poweroff(struct device *dev) |
| 114 | { |
| 115 | return scsi_bus_suspend_common(dev, PMSG_HIBERNATE); |
| 116 | } |
| 117 | |
| 118 | #else /* CONFIG_PM_SLEEP */ |
| 119 | |
| 120 | #define scsi_bus_resume_common NULL |
| 121 | #define scsi_bus_prepare NULL |
| 122 | #define scsi_bus_suspend NULL |
| 123 | #define scsi_bus_freeze NULL |
| 124 | #define scsi_bus_poweroff NULL |
| 125 | |
| 126 | #endif /* CONFIG_PM_SLEEP */ |
| 127 | |
| 128 | #ifdef CONFIG_PM_RUNTIME |
| 129 | |
| 130 | static int scsi_runtime_suspend(struct device *dev) |
| 131 | { |
| 132 | int err = 0; |
| 133 | |
| 134 | dev_dbg(dev, "scsi_runtime_suspend\n"); |
| 135 | if (scsi_is_sdev_device(dev)) { |
| 136 | err = scsi_dev_type_suspend(dev, PMSG_AUTO_SUSPEND); |
| 137 | if (err == -EAGAIN) |
| 138 | pm_schedule_suspend(dev, jiffies_to_msecs( |
| 139 | round_jiffies_up_relative(HZ/10))); |
| 140 | } |
| 141 | |
| 142 | /* Insert hooks here for targets, hosts, and transport classes */ |
| 143 | |
| 144 | return err; |
| 145 | } |
| 146 | |
| 147 | static int scsi_runtime_resume(struct device *dev) |
| 148 | { |
| 149 | int err = 0; |
| 150 | |
| 151 | dev_dbg(dev, "scsi_runtime_resume\n"); |
| 152 | if (scsi_is_sdev_device(dev)) |
| 153 | err = scsi_dev_type_resume(dev); |
| 154 | |
| 155 | /* Insert hooks here for targets, hosts, and transport classes */ |
| 156 | |
| 157 | return err; |
| 158 | } |
| 159 | |
| 160 | static int scsi_runtime_idle(struct device *dev) |
| 161 | { |
| 162 | int err; |
| 163 | |
| 164 | dev_dbg(dev, "scsi_runtime_idle\n"); |
| 165 | |
| 166 | /* Insert hooks here for targets, hosts, and transport classes */ |
| 167 | |
| 168 | if (scsi_is_sdev_device(dev)) |
| 169 | err = pm_schedule_suspend(dev, 100); |
| 170 | else |
| 171 | err = pm_runtime_suspend(dev); |
| 172 | return err; |
| 173 | } |
| 174 | |
| 175 | int scsi_autopm_get_device(struct scsi_device *sdev) |
| 176 | { |
| 177 | int err; |
| 178 | |
| 179 | err = pm_runtime_get_sync(&sdev->sdev_gendev); |
| 180 | if (err < 0 && err !=-EACCES) |
| 181 | pm_runtime_put_sync(&sdev->sdev_gendev); |
| 182 | else |
| 183 | err = 0; |
| 184 | return err; |
| 185 | } |
| 186 | EXPORT_SYMBOL_GPL(scsi_autopm_get_device); |
| 187 | |
| 188 | void scsi_autopm_put_device(struct scsi_device *sdev) |
| 189 | { |
| 190 | pm_runtime_put_sync(&sdev->sdev_gendev); |
| 191 | } |
| 192 | EXPORT_SYMBOL_GPL(scsi_autopm_put_device); |
| 193 | |
| 194 | void scsi_autopm_get_target(struct scsi_target *starget) |
| 195 | { |
| 196 | pm_runtime_get_sync(&starget->dev); |
| 197 | } |
| 198 | |
| 199 | void scsi_autopm_put_target(struct scsi_target *starget) |
| 200 | { |
| 201 | pm_runtime_put_sync(&starget->dev); |
| 202 | } |
| 203 | |
| 204 | int scsi_autopm_get_host(struct Scsi_Host *shost) |
| 205 | { |
| 206 | int err; |
| 207 | |
| 208 | err = pm_runtime_get_sync(&shost->shost_gendev); |
| 209 | if (err < 0 && err !=-EACCES) |
| 210 | pm_runtime_put_sync(&shost->shost_gendev); |
| 211 | else |
| 212 | err = 0; |
| 213 | return err; |
| 214 | } |
| 215 | |
| 216 | void scsi_autopm_put_host(struct Scsi_Host *shost) |
| 217 | { |
| 218 | pm_runtime_put_sync(&shost->shost_gendev); |
| 219 | } |
| 220 | |
| 221 | #else |
| 222 | |
| 223 | #define scsi_runtime_suspend NULL |
| 224 | #define scsi_runtime_resume NULL |
| 225 | #define scsi_runtime_idle NULL |
| 226 | |
| 227 | #endif /* CONFIG_PM_RUNTIME */ |
| 228 | |
| 229 | const struct dev_pm_ops scsi_bus_pm_ops = { |
| 230 | .prepare = scsi_bus_prepare, |
| 231 | .suspend = scsi_bus_suspend, |
| 232 | .resume = scsi_bus_resume_common, |
| 233 | .freeze = scsi_bus_freeze, |
| 234 | .thaw = scsi_bus_resume_common, |
| 235 | .poweroff = scsi_bus_poweroff, |
| 236 | .restore = scsi_bus_resume_common, |
| 237 | .runtime_suspend = scsi_runtime_suspend, |
| 238 | .runtime_resume = scsi_runtime_resume, |
| 239 | .runtime_idle = scsi_runtime_idle, |
| 240 | }; |