Blame view

drivers/ata/libata-zpodd.c 7.26 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
afe759511   Aaron Lu   libata: identify ...
2
3
  #include <linux/libata.h>
  #include <linux/cdrom.h>
f064a20dd   Aaron Lu   libata: move acpi...
4
  #include <linux/pm_runtime.h>
3dc67440d   Aaron Lu   libata: check zer...
5
  #include <linux/module.h>
f1bc1e4c4   Aaron Lu   ata: acpi: rework...
6
  #include <linux/pm_qos.h>
f064a20dd   Aaron Lu   libata: move acpi...
7
  #include <scsi/scsi_device.h>
afe759511   Aaron Lu   libata: identify ...
8
9
  
  #include "libata.h"
3dc67440d   Aaron Lu   libata: check zer...
10
11
12
  static int zpodd_poweroff_delay = 30; /* 30 seconds for power off delay */
  module_param(zpodd_poweroff_delay, int, 0644);
  MODULE_PARM_DESC(zpodd_poweroff_delay, "Poweroff delay for ZPODD in seconds");
afe759511   Aaron Lu   libata: identify ...
13
14
15
16
17
18
19
20
21
  enum odd_mech_type {
  	ODD_MECH_TYPE_SLOT,
  	ODD_MECH_TYPE_DRAWER,
  	ODD_MECH_TYPE_UNSUPPORTED,
  };
  
  struct zpodd {
  	enum odd_mech_type	mech_type; /* init during probe, RO afterwards */
  	struct ata_device	*dev;
f064a20dd   Aaron Lu   libata: move acpi...
22
23
24
25
  
  	/* The following fields are synchronized by PM core. */
  	bool			from_notify; /* resumed as a result of
  					      * acpi wake notification */
3dc67440d   Aaron Lu   libata: check zer...
26
27
28
  	bool			zp_ready; /* ZP ready state */
  	unsigned long		last_ready; /* last ZP ready timestamp */
  	bool			zp_sampled; /* ZP ready state sampled */
213342053   Aaron Lu   libata: handle po...
29
30
  	bool			powered_off; /* ODD is powered off
  					      *	during suspend */
afe759511   Aaron Lu   libata: identify ...
31
  };
213342053   Aaron Lu   libata: handle po...
32
33
  static int eject_tray(struct ata_device *dev)
  {
d0887c43f   Sergei Shtylyov   libata-zpodd: mus...
34
  	struct ata_taskfile tf;
18c9a99bc   Dan Carpenter   libata: zpodd: sm...
35
  	static const char cdb[ATAPI_CDB_LEN] = {  GPCMD_START_STOP_UNIT,
213342053   Aaron Lu   libata: handle po...
36
37
38
39
  		0, 0, 0,
  		0x02,     /* LoEj */
  		0, 0, 0, 0, 0, 0, 0,
  	};
d0887c43f   Sergei Shtylyov   libata-zpodd: mus...
40
  	ata_tf_init(dev, &tf);
213342053   Aaron Lu   libata: handle po...
41
42
43
44
45
46
  	tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  	tf.command = ATA_CMD_PACKET;
  	tf.protocol = ATAPI_PROT_NODATA;
  
  	return ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
  }
afe759511   Aaron Lu   libata: identify ...
47
48
49
  /* Per the spec, only slot type and drawer type ODD can be supported */
  static enum odd_mech_type zpodd_get_mech_type(struct ata_device *dev)
  {
dd08a8d9a   raymond pang   libata: fix using...
50
  	char *buf;
afe759511   Aaron Lu   libata: identify ...
51
  	unsigned int ret;
dd08a8d9a   raymond pang   libata: fix using...
52
  	struct rm_feature_desc *desc;
d0887c43f   Sergei Shtylyov   libata-zpodd: mus...
53
  	struct ata_taskfile tf;
71d6c505b   Kees Cook   libata: zpodd: Fi...
54
  	static const char cdb[ATAPI_CDB_LEN] = {  GPCMD_GET_CONFIGURATION,
afe759511   Aaron Lu   libata: identify ...
55
56
57
  			2,      /* only 1 feature descriptor requested */
  			0, 3,   /* 3, removable medium feature */
  			0, 0, 0,/* reserved */
dd08a8d9a   raymond pang   libata: fix using...
58
  			0, 16,
afe759511   Aaron Lu   libata: identify ...
59
60
  			0, 0, 0,
  	};
dd08a8d9a   raymond pang   libata: fix using...
61
62
63
64
  	buf = kzalloc(16, GFP_KERNEL);
  	if (!buf)
  		return ODD_MECH_TYPE_UNSUPPORTED;
  	desc = (void *)(buf + 8);
d0887c43f   Sergei Shtylyov   libata-zpodd: mus...
65
  	ata_tf_init(dev, &tf);
afe759511   Aaron Lu   libata: identify ...
66
67
68
  	tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  	tf.command = ATA_CMD_PACKET;
  	tf.protocol = ATAPI_PROT_PIO;
dd08a8d9a   raymond pang   libata: fix using...
69
  	tf.lbam = 16;
afe759511   Aaron Lu   libata: identify ...
70
71
  
  	ret = ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
dd08a8d9a   raymond pang   libata: fix using...
72
73
74
  				buf, 16, 0);
  	if (ret) {
  		kfree(buf);
afe759511   Aaron Lu   libata: identify ...
75
  		return ODD_MECH_TYPE_UNSUPPORTED;
dd08a8d9a   raymond pang   libata: fix using...
76
  	}
afe759511   Aaron Lu   libata: identify ...
77

dd08a8d9a   raymond pang   libata: fix using...
78
79
  	if (be16_to_cpu(desc->feature_code) != 3) {
  		kfree(buf);
afe759511   Aaron Lu   libata: identify ...
80
  		return ODD_MECH_TYPE_UNSUPPORTED;
dd08a8d9a   raymond pang   libata: fix using...
81
  	}
afe759511   Aaron Lu   libata: identify ...
82

dd08a8d9a   raymond pang   libata: fix using...
83
84
  	if (desc->mech_type == 0 && desc->load == 0 && desc->eject == 1) {
  		kfree(buf);
afe759511   Aaron Lu   libata: identify ...
85
  		return ODD_MECH_TYPE_SLOT;
dd08a8d9a   raymond pang   libata: fix using...
86
87
88
  	} else if (desc->mech_type == 1 && desc->load == 0 &&
  		   desc->eject == 1) {
  		kfree(buf);
afe759511   Aaron Lu   libata: identify ...
89
  		return ODD_MECH_TYPE_DRAWER;
dd08a8d9a   raymond pang   libata: fix using...
90
91
  	} else {
  		kfree(buf);
afe759511   Aaron Lu   libata: identify ...
92
  		return ODD_MECH_TYPE_UNSUPPORTED;
dd08a8d9a   raymond pang   libata: fix using...
93
  	}
afe759511   Aaron Lu   libata: identify ...
94
  }
3dc67440d   Aaron Lu   libata: check zer...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  /* Test if ODD is zero power ready by sense code */
  static bool zpready(struct ata_device *dev)
  {
  	u8 sense_key, *sense_buf;
  	unsigned int ret, asc, ascq, add_len;
  	struct zpodd *zpodd = dev->zpodd;
  
  	ret = atapi_eh_tur(dev, &sense_key);
  
  	if (!ret || sense_key != NOT_READY)
  		return false;
  
  	sense_buf = dev->link->ap->sector_buf;
  	ret = atapi_eh_request_sense(dev, sense_buf, sense_key);
  	if (ret)
  		return false;
  
  	/* sense valid */
  	if ((sense_buf[0] & 0x7f) != 0x70)
  		return false;
  
  	add_len = sense_buf[7];
  	/* has asc and ascq */
  	if (add_len < 6)
  		return false;
  
  	asc = sense_buf[12];
  	ascq = sense_buf[13];
  
  	if (zpodd->mech_type == ODD_MECH_TYPE_SLOT)
  		/* no media inside */
  		return asc == 0x3a;
  	else
  		/* no media inside and door closed */
  		return asc == 0x3a && ascq == 0x01;
  }
  
  /*
   * Update the zpodd->zp_ready field. This field will only be set
   * if the ODD has stayed in ZP ready state for zpodd_poweroff_delay
   * time, and will be used to decide if power off is allowed. If it
   * is set, it will be cleared during resume from powered off state.
   */
  void zpodd_on_suspend(struct ata_device *dev)
  {
  	struct zpodd *zpodd = dev->zpodd;
  	unsigned long expires;
  
  	if (!zpready(dev)) {
  		zpodd->zp_sampled = false;
a59b9aae2   Aaron Lu   libata: expose pm...
145
  		zpodd->zp_ready = false;
3dc67440d   Aaron Lu   libata: check zer...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  		return;
  	}
  
  	if (!zpodd->zp_sampled) {
  		zpodd->zp_sampled = true;
  		zpodd->last_ready = jiffies;
  		return;
  	}
  
  	expires = zpodd->last_ready +
  		  msecs_to_jiffies(zpodd_poweroff_delay * 1000);
  	if (time_before(jiffies, expires))
  		return;
  
  	zpodd->zp_ready = true;
  }
213342053   Aaron Lu   libata: handle po...
162
163
164
165
166
167
168
169
170
171
  bool zpodd_zpready(struct ata_device *dev)
  {
  	struct zpodd *zpodd = dev->zpodd;
  	return zpodd->zp_ready;
  }
  
  /*
   * Enable runtime wake capability through ACPI and set the powered_off flag,
   * this flag will be used during resume to decide what operations are needed
   * to take.
6f4c827e6   Aaron Lu   [libata] scsi: no...
172
173
174
   *
   * Also, media poll needs to be silenced, so that it doesn't bring the ODD
   * back to full power state every few seconds.
213342053   Aaron Lu   libata: handle po...
175
176
177
178
   */
  void zpodd_enable_run_wake(struct ata_device *dev)
  {
  	struct zpodd *zpodd = dev->zpodd;
6f4c827e6   Aaron Lu   [libata] scsi: no...
179
  	sdev_disable_disk_events(dev->sdev);
213342053   Aaron Lu   libata: handle po...
180
  	zpodd->powered_off = true;
4d183d041   Rafael J. Wysocki   ACPI / PM: Consol...
181
  	acpi_pm_set_device_wakeup(&dev->tdev, true);
213342053   Aaron Lu   libata: handle po...
182
183
184
185
186
187
  }
  
  /* Disable runtime wake capability if it is enabled */
  void zpodd_disable_run_wake(struct ata_device *dev)
  {
  	struct zpodd *zpodd = dev->zpodd;
4d183d041   Rafael J. Wysocki   ACPI / PM: Consol...
188
189
  	if (zpodd->powered_off)
  		acpi_pm_set_device_wakeup(&dev->tdev, false);
213342053   Aaron Lu   libata: handle po...
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  }
  
  /*
   * Post power on processing after the ODD has been recovered. If the
   * ODD wasn't powered off during suspend, it doesn't do anything.
   *
   * For drawer type ODD, if it is powered on due to user pressed the
   * eject button, the tray needs to be ejected. This can only be done
   * after the ODD has been recovered, i.e. link is initialized and
   * device is able to process NON_DATA PIO command, as eject needs to
   * send command for the ODD to process.
   *
   * The from_notify flag set in wake notification handler function
   * zpodd_wake_dev represents if power on is due to user's action.
   *
   * For both types of ODD, several fields need to be reset.
   */
  void zpodd_post_poweron(struct ata_device *dev)
  {
  	struct zpodd *zpodd = dev->zpodd;
  
  	if (!zpodd->powered_off)
  		return;
  
  	zpodd->powered_off = false;
  
  	if (zpodd->from_notify) {
  		zpodd->from_notify = false;
  		if (zpodd->mech_type == ODD_MECH_TYPE_DRAWER)
  			eject_tray(dev);
  	}
  
  	zpodd->zp_sampled = false;
  	zpodd->zp_ready = false;
6f4c827e6   Aaron Lu   [libata] scsi: no...
224
225
  
  	sdev_enable_disk_events(dev->sdev);
213342053   Aaron Lu   libata: handle po...
226
  }
f064a20dd   Aaron Lu   libata: move acpi...
227
228
229
230
231
  static void zpodd_wake_dev(acpi_handle handle, u32 event, void *context)
  {
  	struct ata_device *ata_dev = context;
  	struct zpodd *zpodd = ata_dev->zpodd;
  	struct device *dev = &ata_dev->sdev->sdev_gendev;
53637e076   Aaron Lu   [libata] fix smat...
232
  	if (event == ACPI_NOTIFY_DEVICE_WAKE && pm_runtime_suspended(dev)) {
f064a20dd   Aaron Lu   libata: move acpi...
233
234
235
236
237
238
239
240
241
242
243
244
245
246
  		zpodd->from_notify = true;
  		pm_runtime_resume(dev);
  	}
  }
  
  static void ata_acpi_add_pm_notifier(struct ata_device *dev)
  {
  	acpi_handle handle = ata_dev_acpi_handle(dev);
  	acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  				    zpodd_wake_dev, dev);
  }
  
  static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
  {
f1bc1e4c4   Aaron Lu   ata: acpi: rework...
247
  	acpi_handle handle = ata_dev_acpi_handle(dev);
f064a20dd   Aaron Lu   libata: move acpi...
248
249
  	acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, zpodd_wake_dev);
  }
afe759511   Aaron Lu   libata: identify ...
250
251
  void zpodd_init(struct ata_device *dev)
  {
d92020369   Aaron Lu   libata: zpodd: el...
252
  	struct acpi_device *adev = ACPI_COMPANION(&dev->tdev);
afe759511   Aaron Lu   libata: identify ...
253
254
  	enum odd_mech_type mech_type;
  	struct zpodd *zpodd;
d92020369   Aaron Lu   libata: zpodd: el...
255
  	if (dev->zpodd || !adev || !acpi_device_can_poweroff(adev))
afe759511   Aaron Lu   libata: identify ...
256
257
258
259
260
261
262
263
264
265
266
  		return;
  
  	mech_type = zpodd_get_mech_type(dev);
  	if (mech_type == ODD_MECH_TYPE_UNSUPPORTED)
  		return;
  
  	zpodd = kzalloc(sizeof(struct zpodd), GFP_KERNEL);
  	if (!zpodd)
  		return;
  
  	zpodd->mech_type = mech_type;
f064a20dd   Aaron Lu   libata: move acpi...
267
  	ata_acpi_add_pm_notifier(dev);
afe759511   Aaron Lu   libata: identify ...
268
269
  	zpodd->dev = dev;
  	dev->zpodd = zpodd;
f1bc1e4c4   Aaron Lu   ata: acpi: rework...
270
  	dev_pm_qos_expose_flags(&dev->tdev, 0);
afe759511   Aaron Lu   libata: identify ...
271
272
273
274
  }
  
  void zpodd_exit(struct ata_device *dev)
  {
f064a20dd   Aaron Lu   libata: move acpi...
275
  	ata_acpi_remove_pm_notifier(dev);
afe759511   Aaron Lu   libata: identify ...
276
277
278
  	kfree(dev->zpodd);
  	dev->zpodd = NULL;
  }