blob: eefda51f97d351bda5d8437e2d83aaeae16b30f6 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Aaron Luafe75952013-01-15 17:20:58 +08002#include <linux/libata.h>
3#include <linux/cdrom.h>
Aaron Luf064a202013-01-15 17:20:59 +08004#include <linux/pm_runtime.h>
Aaron Lu3dc67442013-01-15 17:21:00 +08005#include <linux/module.h>
Aaron Luf1bc1e42013-08-23 10:17:54 +08006#include <linux/pm_qos.h>
Aaron Luf064a202013-01-15 17:20:59 +08007#include <scsi/scsi_device.h>
Aaron Luafe75952013-01-15 17:20:58 +08008
9#include "libata.h"
10
Aaron Lu3dc67442013-01-15 17:21:00 +080011static int zpodd_poweroff_delay = 30; /* 30 seconds for power off delay */
12module_param(zpodd_poweroff_delay, int, 0644);
13MODULE_PARM_DESC(zpodd_poweroff_delay, "Poweroff delay for ZPODD in seconds");
14
Aaron Luafe75952013-01-15 17:20:58 +080015enum odd_mech_type {
16 ODD_MECH_TYPE_SLOT,
17 ODD_MECH_TYPE_DRAWER,
18 ODD_MECH_TYPE_UNSUPPORTED,
19};
20
21struct zpodd {
22 enum odd_mech_type mech_type; /* init during probe, RO afterwards */
23 struct ata_device *dev;
Aaron Luf064a202013-01-15 17:20:59 +080024
25 /* The following fields are synchronized by PM core. */
26 bool from_notify; /* resumed as a result of
27 * acpi wake notification */
Aaron Lu3dc67442013-01-15 17:21:00 +080028 bool zp_ready; /* ZP ready state */
29 unsigned long last_ready; /* last ZP ready timestamp */
30 bool zp_sampled; /* ZP ready state sampled */
Aaron Lu21334202013-01-15 17:21:01 +080031 bool powered_off; /* ODD is powered off
32 * during suspend */
Aaron Luafe75952013-01-15 17:20:58 +080033};
34
Aaron Lu21334202013-01-15 17:21:01 +080035static int eject_tray(struct ata_device *dev)
36{
Sergei Shtylyovd0887c42013-06-23 23:25:04 +040037 struct ata_taskfile tf;
Dan Carpenter18c9a992018-05-29 12:13:24 +030038 static const char cdb[ATAPI_CDB_LEN] = { GPCMD_START_STOP_UNIT,
Aaron Lu21334202013-01-15 17:21:01 +080039 0, 0, 0,
40 0x02, /* LoEj */
41 0, 0, 0, 0, 0, 0, 0,
42 };
43
Sergei Shtylyovd0887c42013-06-23 23:25:04 +040044 ata_tf_init(dev, &tf);
Aaron Lu21334202013-01-15 17:21:01 +080045 tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
46 tf.command = ATA_CMD_PACKET;
47 tf.protocol = ATAPI_PROT_NODATA;
48
49 return ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
50}
51
Aaron Luafe75952013-01-15 17:20:58 +080052/* Per the spec, only slot type and drawer type ODD can be supported */
53static enum odd_mech_type zpodd_get_mech_type(struct ata_device *dev)
54{
raymond pangdd08a8d2019-03-28 12:19:25 +000055 char *buf;
Aaron Luafe75952013-01-15 17:20:58 +080056 unsigned int ret;
raymond pangdd08a8d2019-03-28 12:19:25 +000057 struct rm_feature_desc *desc;
Sergei Shtylyovd0887c42013-06-23 23:25:04 +040058 struct ata_taskfile tf;
Kees Cook71d6c502019-07-29 14:47:22 -070059 static const char cdb[ATAPI_CDB_LEN] = { GPCMD_GET_CONFIGURATION,
Aaron Luafe75952013-01-15 17:20:58 +080060 2, /* only 1 feature descriptor requested */
61 0, 3, /* 3, removable medium feature */
62 0, 0, 0,/* reserved */
raymond pangdd08a8d2019-03-28 12:19:25 +000063 0, 16,
Aaron Luafe75952013-01-15 17:20:58 +080064 0, 0, 0,
65 };
66
raymond pangdd08a8d2019-03-28 12:19:25 +000067 buf = kzalloc(16, GFP_KERNEL);
68 if (!buf)
69 return ODD_MECH_TYPE_UNSUPPORTED;
70 desc = (void *)(buf + 8);
71
Sergei Shtylyovd0887c42013-06-23 23:25:04 +040072 ata_tf_init(dev, &tf);
Aaron Luafe75952013-01-15 17:20:58 +080073 tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
74 tf.command = ATA_CMD_PACKET;
75 tf.protocol = ATAPI_PROT_PIO;
raymond pangdd08a8d2019-03-28 12:19:25 +000076 tf.lbam = 16;
Aaron Luafe75952013-01-15 17:20:58 +080077
78 ret = ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
raymond pangdd08a8d2019-03-28 12:19:25 +000079 buf, 16, 0);
80 if (ret) {
81 kfree(buf);
Aaron Luafe75952013-01-15 17:20:58 +080082 return ODD_MECH_TYPE_UNSUPPORTED;
raymond pangdd08a8d2019-03-28 12:19:25 +000083 }
Aaron Luafe75952013-01-15 17:20:58 +080084
raymond pangdd08a8d2019-03-28 12:19:25 +000085 if (be16_to_cpu(desc->feature_code) != 3) {
86 kfree(buf);
Aaron Luafe75952013-01-15 17:20:58 +080087 return ODD_MECH_TYPE_UNSUPPORTED;
raymond pangdd08a8d2019-03-28 12:19:25 +000088 }
Aaron Luafe75952013-01-15 17:20:58 +080089
raymond pangdd08a8d2019-03-28 12:19:25 +000090 if (desc->mech_type == 0 && desc->load == 0 && desc->eject == 1) {
91 kfree(buf);
Aaron Luafe75952013-01-15 17:20:58 +080092 return ODD_MECH_TYPE_SLOT;
raymond pangdd08a8d2019-03-28 12:19:25 +000093 } else if (desc->mech_type == 1 && desc->load == 0 &&
94 desc->eject == 1) {
95 kfree(buf);
Aaron Luafe75952013-01-15 17:20:58 +080096 return ODD_MECH_TYPE_DRAWER;
raymond pangdd08a8d2019-03-28 12:19:25 +000097 } else {
98 kfree(buf);
Aaron Luafe75952013-01-15 17:20:58 +080099 return ODD_MECH_TYPE_UNSUPPORTED;
raymond pangdd08a8d2019-03-28 12:19:25 +0000100 }
Aaron Luafe75952013-01-15 17:20:58 +0800101}
102
Aaron Lu3dc67442013-01-15 17:21:00 +0800103/* Test if ODD is zero power ready by sense code */
104static bool zpready(struct ata_device *dev)
105{
106 u8 sense_key, *sense_buf;
107 unsigned int ret, asc, ascq, add_len;
108 struct zpodd *zpodd = dev->zpodd;
109
110 ret = atapi_eh_tur(dev, &sense_key);
111
112 if (!ret || sense_key != NOT_READY)
113 return false;
114
115 sense_buf = dev->link->ap->sector_buf;
116 ret = atapi_eh_request_sense(dev, sense_buf, sense_key);
117 if (ret)
118 return false;
119
120 /* sense valid */
121 if ((sense_buf[0] & 0x7f) != 0x70)
122 return false;
123
124 add_len = sense_buf[7];
125 /* has asc and ascq */
126 if (add_len < 6)
127 return false;
128
129 asc = sense_buf[12];
130 ascq = sense_buf[13];
131
132 if (zpodd->mech_type == ODD_MECH_TYPE_SLOT)
133 /* no media inside */
134 return asc == 0x3a;
135 else
136 /* no media inside and door closed */
137 return asc == 0x3a && ascq == 0x01;
138}
139
140/*
141 * Update the zpodd->zp_ready field. This field will only be set
142 * if the ODD has stayed in ZP ready state for zpodd_poweroff_delay
143 * time, and will be used to decide if power off is allowed. If it
144 * is set, it will be cleared during resume from powered off state.
145 */
146void zpodd_on_suspend(struct ata_device *dev)
147{
148 struct zpodd *zpodd = dev->zpodd;
149 unsigned long expires;
150
151 if (!zpready(dev)) {
152 zpodd->zp_sampled = false;
Aaron Lua59b9aa2013-01-15 17:21:02 +0800153 zpodd->zp_ready = false;
Aaron Lu3dc67442013-01-15 17:21:00 +0800154 return;
155 }
156
157 if (!zpodd->zp_sampled) {
158 zpodd->zp_sampled = true;
159 zpodd->last_ready = jiffies;
160 return;
161 }
162
163 expires = zpodd->last_ready +
164 msecs_to_jiffies(zpodd_poweroff_delay * 1000);
165 if (time_before(jiffies, expires))
166 return;
167
168 zpodd->zp_ready = true;
169}
170
Aaron Lu21334202013-01-15 17:21:01 +0800171bool zpodd_zpready(struct ata_device *dev)
172{
173 struct zpodd *zpodd = dev->zpodd;
174 return zpodd->zp_ready;
175}
176
177/*
178 * Enable runtime wake capability through ACPI and set the powered_off flag,
179 * this flag will be used during resume to decide what operations are needed
180 * to take.
Aaron Lu6f4c8272013-01-23 15:09:32 +0800181 *
182 * Also, media poll needs to be silenced, so that it doesn't bring the ODD
183 * back to full power state every few seconds.
Aaron Lu21334202013-01-15 17:21:01 +0800184 */
185void zpodd_enable_run_wake(struct ata_device *dev)
186{
187 struct zpodd *zpodd = dev->zpodd;
188
Aaron Lu6f4c8272013-01-23 15:09:32 +0800189 sdev_disable_disk_events(dev->sdev);
190
Aaron Lu21334202013-01-15 17:21:01 +0800191 zpodd->powered_off = true;
Rafael J. Wysocki4d183d02017-06-24 01:54:39 +0200192 acpi_pm_set_device_wakeup(&dev->tdev, true);
Aaron Lu21334202013-01-15 17:21:01 +0800193}
194
195/* Disable runtime wake capability if it is enabled */
196void zpodd_disable_run_wake(struct ata_device *dev)
197{
198 struct zpodd *zpodd = dev->zpodd;
199
Rafael J. Wysocki4d183d02017-06-24 01:54:39 +0200200 if (zpodd->powered_off)
201 acpi_pm_set_device_wakeup(&dev->tdev, false);
Aaron Lu21334202013-01-15 17:21:01 +0800202}
203
204/*
205 * Post power on processing after the ODD has been recovered. If the
206 * ODD wasn't powered off during suspend, it doesn't do anything.
207 *
208 * For drawer type ODD, if it is powered on due to user pressed the
209 * eject button, the tray needs to be ejected. This can only be done
210 * after the ODD has been recovered, i.e. link is initialized and
211 * device is able to process NON_DATA PIO command, as eject needs to
212 * send command for the ODD to process.
213 *
214 * The from_notify flag set in wake notification handler function
215 * zpodd_wake_dev represents if power on is due to user's action.
216 *
217 * For both types of ODD, several fields need to be reset.
218 */
219void zpodd_post_poweron(struct ata_device *dev)
220{
221 struct zpodd *zpodd = dev->zpodd;
222
223 if (!zpodd->powered_off)
224 return;
225
226 zpodd->powered_off = false;
227
228 if (zpodd->from_notify) {
229 zpodd->from_notify = false;
230 if (zpodd->mech_type == ODD_MECH_TYPE_DRAWER)
231 eject_tray(dev);
232 }
233
234 zpodd->zp_sampled = false;
235 zpodd->zp_ready = false;
Aaron Lu6f4c8272013-01-23 15:09:32 +0800236
237 sdev_enable_disk_events(dev->sdev);
Aaron Lu21334202013-01-15 17:21:01 +0800238}
239
Aaron Luf064a202013-01-15 17:20:59 +0800240static void zpodd_wake_dev(acpi_handle handle, u32 event, void *context)
241{
242 struct ata_device *ata_dev = context;
243 struct zpodd *zpodd = ata_dev->zpodd;
244 struct device *dev = &ata_dev->sdev->sdev_gendev;
245
Aaron Lu53637e02013-01-28 13:08:02 +0800246 if (event == ACPI_NOTIFY_DEVICE_WAKE && pm_runtime_suspended(dev)) {
Aaron Luf064a202013-01-15 17:20:59 +0800247 zpodd->from_notify = true;
248 pm_runtime_resume(dev);
249 }
250}
251
252static void ata_acpi_add_pm_notifier(struct ata_device *dev)
253{
254 acpi_handle handle = ata_dev_acpi_handle(dev);
255 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
256 zpodd_wake_dev, dev);
257}
258
259static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
260{
Aaron Luf1bc1e42013-08-23 10:17:54 +0800261 acpi_handle handle = ata_dev_acpi_handle(dev);
Aaron Luf064a202013-01-15 17:20:59 +0800262 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, zpodd_wake_dev);
263}
264
Aaron Luafe75952013-01-15 17:20:58 +0800265void zpodd_init(struct ata_device *dev)
266{
Aaron Lud9202032014-03-14 13:46:10 +0800267 struct acpi_device *adev = ACPI_COMPANION(&dev->tdev);
Aaron Luafe75952013-01-15 17:20:58 +0800268 enum odd_mech_type mech_type;
269 struct zpodd *zpodd;
270
Aaron Lud9202032014-03-14 13:46:10 +0800271 if (dev->zpodd || !adev || !acpi_device_can_poweroff(adev))
Aaron Luafe75952013-01-15 17:20:58 +0800272 return;
273
274 mech_type = zpodd_get_mech_type(dev);
275 if (mech_type == ODD_MECH_TYPE_UNSUPPORTED)
276 return;
277
278 zpodd = kzalloc(sizeof(struct zpodd), GFP_KERNEL);
279 if (!zpodd)
280 return;
281
282 zpodd->mech_type = mech_type;
283
Aaron Luf064a202013-01-15 17:20:59 +0800284 ata_acpi_add_pm_notifier(dev);
Aaron Luafe75952013-01-15 17:20:58 +0800285 zpodd->dev = dev;
286 dev->zpodd = zpodd;
Aaron Luf1bc1e42013-08-23 10:17:54 +0800287 dev_pm_qos_expose_flags(&dev->tdev, 0);
Aaron Luafe75952013-01-15 17:20:58 +0800288}
289
290void zpodd_exit(struct ata_device *dev)
291{
Aaron Luf064a202013-01-15 17:20:59 +0800292 ata_acpi_remove_pm_notifier(dev);
Aaron Luafe75952013-01-15 17:20:58 +0800293 kfree(dev->zpodd);
294 dev->zpodd = NULL;
295}