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