blob: e0dd132fd49071f598cd2e57f3b6c7f44fc69d64 [file] [log] [blame]
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -07001/*
2 * libata-acpi.c
3 * Provides ACPI support for PATA/SATA.
4 *
5 * Copyright (C) 2006 Intel Corp.
6 * Copyright (C) 2006 Randy Dunlap
7 */
8
9#include <linux/ata.h>
10#include <linux/delay.h>
11#include <linux/device.h>
12#include <linux/errno.h>
13#include <linux/kernel.h>
14#include <linux/acpi.h>
15#include <linux/libata.h>
16#include <linux/pci.h>
Matthew Garrett237d8442007-10-03 01:24:16 +010017#include <scsi/scsi_device.h>
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070018#include "libata.h"
19
20#include <acpi/acpi_bus.h>
21#include <acpi/acnames.h>
22#include <acpi/acnamesp.h>
23#include <acpi/acparser.h>
24#include <acpi/acexcep.h>
25#include <acpi/acmacros.h>
26#include <acpi/actypes.h>
27
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070028#define NO_PORT_MULT 0xffff
Jeff Garzik2dcb4072007-10-19 06:42:56 -040029#define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070030
31#define REGS_PER_GTF 7
Tejun Heo4700c4b2007-05-15 03:28:16 +090032struct ata_acpi_gtf {
33 u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
34} __packed;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070035
Alan Coxca426632007-03-08 23:13:50 +000036/*
37 * Helper - belongs in the PCI layer somewhere eventually
38 */
39static int is_pci_dev(struct device *dev)
40{
41 return (dev->bus == &pci_bus_type);
42}
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070043
Tejun Heo398e0782007-12-15 15:05:03 +090044static void ata_acpi_clear_gtf(struct ata_device *dev)
45{
46 kfree(dev->gtf_cache);
47 dev->gtf_cache = NULL;
48}
49
Tejun Heod0df8b5d2007-09-23 13:19:54 +090050/**
51 * ata_acpi_associate_sata_port - associate SATA port with ACPI objects
52 * @ap: target SATA port
53 *
54 * Look up ACPI objects associated with @ap and initialize acpi_handle
55 * fields of @ap, the port and devices accordingly.
56 *
57 * LOCKING:
58 * EH context.
59 *
60 * RETURNS:
61 * 0 on success, -errno on failure.
62 */
63void ata_acpi_associate_sata_port(struct ata_port *ap)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070064{
Tejun Heod0df8b5d2007-09-23 13:19:54 +090065 WARN_ON(!(ap->flags & ATA_FLAG_ACPI_SATA));
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070066
Tejun Heod0df8b5d2007-09-23 13:19:54 +090067 if (!ap->nr_pmp_links) {
68 acpi_integer adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
69
70 ap->link.device->acpi_handle =
71 acpi_get_child(ap->host->acpi_handle, adr);
72 } else {
73 struct ata_link *link;
74
75 ap->link.device->acpi_handle = NULL;
76
77 ata_port_for_each_link(link, ap) {
78 acpi_integer adr = SATA_ADR(ap->port_no, link->pmp);
79
80 link->device->acpi_handle =
81 acpi_get_child(ap->host->acpi_handle, adr);
82 }
83 }
Tejun Heofafbae82007-05-15 03:28:16 +090084}
Alan Coxca426632007-03-08 23:13:50 +000085
Tejun Heofafbae82007-05-15 03:28:16 +090086static void ata_acpi_associate_ide_port(struct ata_port *ap)
87{
88 int max_devices, i;
89
90 ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no);
91 if (!ap->acpi_handle)
92 return;
93
94 max_devices = 1;
95 if (ap->flags & ATA_FLAG_SLAVE_POSS)
96 max_devices++;
97
98 for (i = 0; i < max_devices; i++) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +090099 struct ata_device *dev = &ap->link.device[i];
Tejun Heofafbae82007-05-15 03:28:16 +0900100
101 dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
102 }
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900103
104 if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
105 ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700106}
107
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400108static void ata_acpi_handle_hotplug(struct ata_port *ap, struct kobject *kobj,
109 u32 event)
Matthew Garrett237d8442007-10-03 01:24:16 +0100110{
111 char event_string[12];
112 char *envp[] = { event_string, NULL };
113 struct ata_eh_info *ehi = &ap->link.eh_info;
114
115 if (event == 0 || event == 1) {
116 unsigned long flags;
117 spin_lock_irqsave(ap->lock, flags);
118 ata_ehi_clear_desc(ehi);
119 ata_ehi_push_desc(ehi, "ACPI event");
120 ata_ehi_hotplugged(ehi);
121 ata_port_freeze(ap);
122 spin_unlock_irqrestore(ap->lock, flags);
123 }
124
125 if (kobj) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400126 sprintf(event_string, "BAY_EVENT=%d", event);
Matthew Garrett237d8442007-10-03 01:24:16 +0100127 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
128 }
129}
130
131static void ata_acpi_dev_notify(acpi_handle handle, u32 event, void *data)
132{
133 struct ata_device *dev = data;
134 struct kobject *kobj = NULL;
135
136 if (dev->sdev)
137 kobj = &dev->sdev->sdev_gendev.kobj;
138
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400139 ata_acpi_handle_hotplug(dev->link->ap, kobj, event);
Matthew Garrett237d8442007-10-03 01:24:16 +0100140}
141
142static void ata_acpi_ap_notify(acpi_handle handle, u32 event, void *data)
143{
144 struct ata_port *ap = data;
145
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400146 ata_acpi_handle_hotplug(ap, &ap->dev->kobj, event);
Matthew Garrett237d8442007-10-03 01:24:16 +0100147}
148
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700149/**
Tejun Heofafbae82007-05-15 03:28:16 +0900150 * ata_acpi_associate - associate ATA host with ACPI objects
151 * @host: target ATA host
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700152 *
Tejun Heofafbae82007-05-15 03:28:16 +0900153 * Look up ACPI objects associated with @host and initialize
154 * acpi_handle fields of @host, its ports and devices accordingly.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700155 *
Tejun Heofafbae82007-05-15 03:28:16 +0900156 * LOCKING:
157 * EH context.
158 *
159 * RETURNS:
160 * 0 on success, -errno on failure.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700161 */
Tejun Heofafbae82007-05-15 03:28:16 +0900162void ata_acpi_associate(struct ata_host *host)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700163{
Matthew Garrett237d8442007-10-03 01:24:16 +0100164 int i, j;
Alan Coxca426632007-03-08 23:13:50 +0000165
Tejun Heofafbae82007-05-15 03:28:16 +0900166 if (!is_pci_dev(host->dev) || libata_noacpi)
167 return;
Alan Coxca426632007-03-08 23:13:50 +0000168
Tejun Heofafbae82007-05-15 03:28:16 +0900169 host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev);
170 if (!host->acpi_handle)
171 return;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700172
Tejun Heofafbae82007-05-15 03:28:16 +0900173 for (i = 0; i < host->n_ports; i++) {
174 struct ata_port *ap = host->ports[i];
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700175
Tejun Heofafbae82007-05-15 03:28:16 +0900176 if (host->ports[0]->flags & ATA_FLAG_ACPI_SATA)
177 ata_acpi_associate_sata_port(ap);
178 else
179 ata_acpi_associate_ide_port(ap);
Matthew Garrett237d8442007-10-03 01:24:16 +0100180
181 if (ap->acpi_handle)
182 acpi_install_notify_handler (ap->acpi_handle,
183 ACPI_SYSTEM_NOTIFY,
184 ata_acpi_ap_notify,
185 ap);
186
187 for (j = 0; j < ata_link_max_devices(&ap->link); j++) {
188 struct ata_device *dev = &ap->link.device[j];
189
190 if (dev->acpi_handle)
191 acpi_install_notify_handler (dev->acpi_handle,
192 ACPI_SYSTEM_NOTIFY,
193 ata_acpi_dev_notify,
194 dev);
195 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700196 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700197}
198
199/**
Tejun Heo562f0c22007-12-15 15:05:01 +0900200 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
201 * @host: target ATA host
202 *
203 * This function is called during driver detach after the whole host
204 * is shut down.
205 *
206 * LOCKING:
207 * EH context.
208 */
209void ata_acpi_dissociate(struct ata_host *host)
210{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900211 int i;
212
213 /* Restore initial _GTM values so that driver which attaches
214 * afterward can use them too.
215 */
216 for (i = 0; i < host->n_ports; i++) {
217 struct ata_port *ap = host->ports[i];
218 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
219
220 if (ap->acpi_handle && gtm)
221 ata_acpi_stm(ap, gtm);
222 }
Tejun Heo562f0c22007-12-15 15:05:01 +0900223}
224
225/**
Tejun Heo64578a32007-05-15 03:28:16 +0900226 * ata_acpi_gtm - execute _GTM
227 * @ap: target ATA port
228 * @gtm: out parameter for _GTM result
229 *
230 * Evaluate _GTM and store the result in @gtm.
231 *
232 * LOCKING:
233 * EH context.
234 *
235 * RETURNS:
236 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
237 */
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900238int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
Tejun Heo64578a32007-05-15 03:28:16 +0900239{
240 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
241 union acpi_object *out_obj;
242 acpi_status status;
243 int rc = 0;
244
245 status = acpi_evaluate_object(ap->acpi_handle, "_GTM", NULL, &output);
246
247 rc = -ENOENT;
248 if (status == AE_NOT_FOUND)
249 goto out_free;
250
251 rc = -EINVAL;
252 if (ACPI_FAILURE(status)) {
253 ata_port_printk(ap, KERN_ERR,
254 "ACPI get timing mode failed (AE 0x%x)\n",
255 status);
256 goto out_free;
257 }
258
259 out_obj = output.pointer;
260 if (out_obj->type != ACPI_TYPE_BUFFER) {
261 ata_port_printk(ap, KERN_WARNING,
262 "_GTM returned unexpected object type 0x%x\n",
263 out_obj->type);
264
265 goto out_free;
266 }
267
268 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
269 ata_port_printk(ap, KERN_ERR,
270 "_GTM returned invalid length %d\n",
271 out_obj->buffer.length);
272 goto out_free;
273 }
274
275 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
276 rc = 0;
277 out_free:
278 kfree(output.pointer);
279 return rc;
280}
281
Alan Coxbadff032007-10-04 21:28:18 +0100282EXPORT_SYMBOL_GPL(ata_acpi_gtm);
283
Tejun Heo64578a32007-05-15 03:28:16 +0900284/**
285 * ata_acpi_stm - execute _STM
286 * @ap: target ATA port
287 * @stm: timing parameter to _STM
288 *
289 * Evaluate _STM with timing parameter @stm.
290 *
291 * LOCKING:
292 * EH context.
293 *
294 * RETURNS:
295 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
296 */
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900297int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
Tejun Heo64578a32007-05-15 03:28:16 +0900298{
299 acpi_status status;
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900300 struct ata_acpi_gtm stm_buf = *stm;
Tejun Heo64578a32007-05-15 03:28:16 +0900301 struct acpi_object_list input;
302 union acpi_object in_params[3];
303
304 in_params[0].type = ACPI_TYPE_BUFFER;
305 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900306 in_params[0].buffer.pointer = (u8 *)&stm_buf;
Tejun Heo64578a32007-05-15 03:28:16 +0900307 /* Buffers for id may need byteswapping ? */
308 in_params[1].type = ACPI_TYPE_BUFFER;
309 in_params[1].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900310 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
Tejun Heo64578a32007-05-15 03:28:16 +0900311 in_params[2].type = ACPI_TYPE_BUFFER;
312 in_params[2].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900313 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
Tejun Heo64578a32007-05-15 03:28:16 +0900314
315 input.count = 3;
316 input.pointer = in_params;
317
318 status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL);
319
320 if (status == AE_NOT_FOUND)
321 return -ENOENT;
322 if (ACPI_FAILURE(status)) {
323 ata_port_printk(ap, KERN_ERR,
324 "ACPI set timing mode failed (status=0x%x)\n", status);
325 return -EINVAL;
326 }
327 return 0;
328}
329
Alan Coxbadff032007-10-04 21:28:18 +0100330EXPORT_SYMBOL_GPL(ata_acpi_stm);
331
Tejun Heo64578a32007-05-15 03:28:16 +0900332/**
Tejun Heo4700c4b2007-05-15 03:28:16 +0900333 * ata_dev_get_GTF - get the drive bootup default taskfile settings
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900334 * @dev: target ATA device
Tejun Heo4700c4b2007-05-15 03:28:16 +0900335 * @gtf: output parameter for buffer containing _GTF taskfile arrays
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700336 *
337 * This applies to both PATA and SATA drives.
338 *
339 * The _GTF method has no input parameters.
340 * It returns a variable number of register set values (registers
341 * hex 1F1..1F7, taskfiles).
342 * The <variable number> is not known in advance, so have ACPI-CA
343 * allocate the buffer as needed and return it, then free it later.
344 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900345 * LOCKING:
346 * EH context.
347 *
348 * RETURNS:
349 * Number of taskfiles on success, 0 if _GTF doesn't exist or doesn't
Matthew Garrett037f6bb2007-11-08 18:37:07 +0000350 * contain valid data.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700351 */
Tejun Heo398e0782007-12-15 15:05:03 +0900352static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700353{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900354 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900355 acpi_status status;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900356 struct acpi_buffer output;
357 union acpi_object *out_obj;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900358 int rc = 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700359
Tejun Heo398e0782007-12-15 15:05:03 +0900360 /* if _GTF is cached, use the cached value */
361 if (dev->gtf_cache) {
362 out_obj = dev->gtf_cache;
363 goto done;
364 }
365
Tejun Heo4700c4b2007-05-15 03:28:16 +0900366 /* set up output buffer */
367 output.length = ACPI_ALLOCATE_BUFFER;
368 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700369
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700370 if (ata_msg_probe(ap))
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900371 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
Tejun Heo878d4fe2007-02-21 16:36:33 +0900372 __FUNCTION__, ap->port_no);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700373
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700374 /* _GTF has no input parameters */
Tejun Heo4700c4b2007-05-15 03:28:16 +0900375 status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
Tejun Heo398e0782007-12-15 15:05:03 +0900376 out_obj = dev->gtf_cache = output.pointer;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900377
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700378 if (ACPI_FAILURE(status)) {
Tejun Heo4700c4b2007-05-15 03:28:16 +0900379 if (status != AE_NOT_FOUND) {
380 ata_dev_printk(dev, KERN_WARNING,
381 "_GTF evaluation failed (AE 0x%x)\n",
382 status);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900383 }
384 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700385 }
386
387 if (!output.length || !output.pointer) {
388 if (ata_msg_probe(ap))
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900389 ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700390 "length or ptr is NULL (0x%llx, 0x%p)\n",
391 __FUNCTION__,
392 (unsigned long long)output.length,
393 output.pointer);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900394 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700395 }
396
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700397 if (out_obj->type != ACPI_TYPE_BUFFER) {
Tejun Heo69b16a52007-05-15 03:28:16 +0900398 ata_dev_printk(dev, KERN_WARNING,
399 "_GTF unexpected object type 0x%x\n",
400 out_obj->type);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900401 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700402 }
403
Tejun Heo4700c4b2007-05-15 03:28:16 +0900404 if (out_obj->buffer.length % REGS_PER_GTF) {
Tejun Heo69b16a52007-05-15 03:28:16 +0900405 ata_dev_printk(dev, KERN_WARNING,
406 "unexpected _GTF length (%d)\n",
407 out_obj->buffer.length);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900408 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700409 }
410
Tejun Heo398e0782007-12-15 15:05:03 +0900411 done:
Tejun Heo4700c4b2007-05-15 03:28:16 +0900412 rc = out_obj->buffer.length / REGS_PER_GTF;
Tejun Heo398e0782007-12-15 15:05:03 +0900413 if (gtf) {
414 *gtf = (void *)out_obj->buffer.pointer;
415 if (ata_msg_probe(ap))
416 ata_dev_printk(dev, KERN_DEBUG,
417 "%s: returning gtf=%p, gtf_count=%d\n",
418 __FUNCTION__, *gtf, rc);
419 }
Tejun Heo4700c4b2007-05-15 03:28:16 +0900420 return rc;
421
422 out_free:
Tejun Heo398e0782007-12-15 15:05:03 +0900423 ata_acpi_clear_gtf(dev);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900424 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700425}
426
427/**
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400428 * ata_acpi_cbl_80wire - Check for 80 wire cable
429 * @ap: Port to check
430 *
431 * Return 1 if the ACPI mode data for this port indicates the BIOS selected
432 * an 80wire mode.
433 */
434
435int ata_acpi_cbl_80wire(struct ata_port *ap)
436{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900437 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400438 int valid = 0;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400439
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900440 if (!gtm)
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400441 return 0;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400442
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400443 /* Split timing, DMA enabled */
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900444 if ((gtm->flags & 0x11) == 0x11 && gtm->drive[0].dma < 55)
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400445 valid |= 1;
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900446 if ((gtm->flags & 0x14) == 0x14 && gtm->drive[1].dma < 55)
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400447 valid |= 2;
448 /* Shared timing, DMA enabled */
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900449 if ((gtm->flags & 0x11) == 0x01 && gtm->drive[0].dma < 55)
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400450 valid |= 1;
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900451 if ((gtm->flags & 0x14) == 0x04 && gtm->drive[0].dma < 55)
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400452 valid |= 2;
453
454 /* Drive check */
455 if ((valid & 1) && ata_dev_enabled(&ap->link.device[0]))
456 return 1;
457 if ((valid & 2) && ata_dev_enabled(&ap->link.device[1]))
458 return 1;
459 return 0;
460}
461
462EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
463
464/**
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700465 * taskfile_load_raw - send taskfile registers to host controller
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900466 * @dev: target ATA device
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700467 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
468 *
469 * Outputs ATA taskfile to standard ATA host controller using MMIO
470 * or PIO as indicated by the ATA_FLAG_MMIO flag.
471 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
472 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
473 * hob_lbal, hob_lbam, and hob_lbah.
474 *
475 * This function waits for idle (!BUSY and !DRQ) after writing
476 * registers. If the control register has a new value, this
477 * function also waits for idle after writing control and before
478 * writing the remaining registers.
479 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900480 * LOCKING:
481 * EH context.
482 *
483 * RETURNS:
484 * 0 on success, -errno on failure.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700485 */
Tejun Heo4700c4b2007-05-15 03:28:16 +0900486static int taskfile_load_raw(struct ata_device *dev,
487 const struct ata_acpi_gtf *gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700488{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900489 struct ata_port *ap = dev->link->ap;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900490 struct ata_taskfile tf, rtf;
491 unsigned int err_mask;
Jeff Garzikfc16c252007-02-24 21:05:01 -0500492
Tejun Heo4700c4b2007-05-15 03:28:16 +0900493 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
494 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
495 && (gtf->tf[6] == 0))
496 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700497
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900498 ata_tf_init(dev, &tf);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700499
Jeff Garzikfc16c252007-02-24 21:05:01 -0500500 /* convert gtf to tf */
501 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
Tejun Heo48be6b12007-04-23 02:06:46 +0900502 tf.protocol = ATA_PROT_NODATA;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900503 tf.feature = gtf->tf[0]; /* 0x1f1 */
504 tf.nsect = gtf->tf[1]; /* 0x1f2 */
505 tf.lbal = gtf->tf[2]; /* 0x1f3 */
506 tf.lbam = gtf->tf[3]; /* 0x1f4 */
507 tf.lbah = gtf->tf[4]; /* 0x1f5 */
508 tf.device = gtf->tf[5]; /* 0x1f6 */
509 tf.command = gtf->tf[6]; /* 0x1f7 */
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700510
Tejun Heo4700c4b2007-05-15 03:28:16 +0900511 if (ata_msg_probe(ap))
512 ata_dev_printk(dev, KERN_DEBUG, "executing ACPI cmd "
513 "%02x/%02x:%02x:%02x:%02x:%02x:%02x\n",
514 tf.command, tf.feature, tf.nsect,
515 tf.lbal, tf.lbam, tf.lbah, tf.device);
516
517 rtf = tf;
Tejun Heo2b789102007-10-09 15:05:44 +0900518 err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900519 if (err_mask) {
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900520 ata_dev_printk(dev, KERN_ERR,
Tejun Heo4700c4b2007-05-15 03:28:16 +0900521 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x failed "
522 "(Emask=0x%x Stat=0x%02x Err=0x%02x)\n",
523 tf.command, tf.feature, tf.nsect, tf.lbal, tf.lbam,
524 tf.lbah, tf.device, err_mask, rtf.command, rtf.feature);
525 return -EIO;
526 }
527
528 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700529}
530
531/**
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700532 * ata_acpi_exec_tfs - get then write drive taskfile settings
Tejun Heo67465442007-05-15 03:28:16 +0900533 * @dev: target ATA device
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700534 *
Tejun Heo67465442007-05-15 03:28:16 +0900535 * Evaluate _GTF and excute returned taskfiles.
Tejun Heo69b16a52007-05-15 03:28:16 +0900536 *
537 * LOCKING:
538 * EH context.
539 *
540 * RETURNS:
Tejun Heo67465442007-05-15 03:28:16 +0900541 * Number of executed taskfiles on success, 0 if _GTF doesn't exist or
542 * doesn't contain valid data. -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700543 */
Tejun Heo67465442007-05-15 03:28:16 +0900544static int ata_acpi_exec_tfs(struct ata_device *dev)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700545{
Tejun Heo67465442007-05-15 03:28:16 +0900546 struct ata_acpi_gtf *gtf = NULL;
Tejun Heo67465442007-05-15 03:28:16 +0900547 int gtf_count, i, rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700548
Tejun Heo67465442007-05-15 03:28:16 +0900549 /* get taskfiles */
Tejun Heo398e0782007-12-15 15:05:03 +0900550 gtf_count = ata_dev_get_GTF(dev, &gtf);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700551
Tejun Heo67465442007-05-15 03:28:16 +0900552 /* execute them */
553 for (i = 0, rc = 0; i < gtf_count; i++) {
554 int tmp;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900555
Tejun Heo67465442007-05-15 03:28:16 +0900556 /* ACPI errors are eventually ignored. Run till the
557 * end even after errors.
558 */
559 tmp = taskfile_load_raw(dev, gtf++);
560 if (!rc)
561 rc = tmp;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700562 }
563
Tejun Heo398e0782007-12-15 15:05:03 +0900564 ata_acpi_clear_gtf(dev);
Tejun Heo67465442007-05-15 03:28:16 +0900565
566 if (rc == 0)
567 return gtf_count;
568 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700569}
570
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700571/**
572 * ata_acpi_push_id - send Identify data to drive
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900573 * @dev: target ATA device
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700574 *
575 * _SDD ACPI object: for SATA mode only
576 * Must be after Identify (Packet) Device -- uses its data
577 * ATM this function never returns a failure. It is an optional
578 * method and if it fails for whatever reason, we should still
579 * just keep going.
Tejun Heo69b16a52007-05-15 03:28:16 +0900580 *
581 * LOCKING:
582 * EH context.
583 *
584 * RETURNS:
585 * 0 on success, -errno on failure.
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700586 */
Tejun Heo67465442007-05-15 03:28:16 +0900587static int ata_acpi_push_id(struct ata_device *dev)
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700588{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900589 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900590 int err;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900591 acpi_status status;
592 struct acpi_object_list input;
593 union acpi_object in_params[1];
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700594
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700595 if (ata_msg_probe(ap))
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900596 ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
597 __FUNCTION__, dev->devno, ap->port_no);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700598
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700599 /* Give the drive Identify data to the drive via the _SDD method */
600 /* _SDD: set up input parameters */
601 input.count = 1;
602 input.pointer = in_params;
603 in_params[0].type = ACPI_TYPE_BUFFER;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900604 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
605 in_params[0].buffer.pointer = (u8 *)dev->id;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700606 /* Output buffer: _SDD has no output */
607
608 /* It's OK for _SDD to be missing too. */
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900609 swap_buf_le16(dev->id, ATA_ID_WORDS);
Tejun Heofafbae82007-05-15 03:28:16 +0900610 status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900611 swap_buf_le16(dev->id, ATA_ID_WORDS);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700612
613 err = ACPI_FAILURE(status) ? -EIO : 0;
Tejun Heo69b16a52007-05-15 03:28:16 +0900614 if (err < 0)
615 ata_dev_printk(dev, KERN_WARNING,
616 "ACPI _SDD failed (AE 0x%x)\n", status);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700617
Tejun Heo67465442007-05-15 03:28:16 +0900618 return err;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700619}
620
Tejun Heo67465442007-05-15 03:28:16 +0900621/**
Tejun Heo64578a32007-05-15 03:28:16 +0900622 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
623 * @ap: target ATA port
624 *
625 * This function is called when @ap is about to be suspended. All
626 * devices are already put to sleep but the port_suspend() callback
627 * hasn't been executed yet. Error return from this function aborts
628 * suspend.
629 *
630 * LOCKING:
631 * EH context.
632 *
633 * RETURNS:
634 * 0 on success, -errno on failure.
635 */
636int ata_acpi_on_suspend(struct ata_port *ap)
637{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900638 /* nada */
639 return 0;
Tejun Heo64578a32007-05-15 03:28:16 +0900640}
641
642/**
Tejun Heo67465442007-05-15 03:28:16 +0900643 * ata_acpi_on_resume - ATA ACPI hook called on resume
644 * @ap: target ATA port
645 *
646 * This function is called when @ap is resumed - right after port
647 * itself is resumed but before any EH action is taken.
648 *
649 * LOCKING:
650 * EH context.
651 */
652void ata_acpi_on_resume(struct ata_port *ap)
653{
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900654 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
Tejun Heof58229f2007-08-06 18:36:23 +0900655 struct ata_device *dev;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700656
Tejun Heo398e0782007-12-15 15:05:03 +0900657 if (ap->acpi_handle && gtm) {
658 /* _GTM valid */
659
660 /* restore timing parameters */
Tejun Heoc05e6ff2007-12-15 15:05:02 +0900661 ata_acpi_stm(ap, gtm);
Tejun Heo64578a32007-05-15 03:28:16 +0900662
Tejun Heo398e0782007-12-15 15:05:03 +0900663 /* _GTF should immediately follow _STM so that it can
664 * use values set by _STM. Cache _GTF result and
665 * schedule _GTF.
666 */
667 ata_link_for_each_dev(dev, &ap->link) {
668 ata_acpi_clear_gtf(dev);
669 if (ata_dev_get_GTF(dev, NULL) >= 0)
670 dev->flags |= ATA_DFLAG_ACPI_PENDING;
671 }
672 } else {
673 /* SATA _GTF needs to be evaulated after _SDD and
674 * there's no reason to evaluate IDE _GTF early
675 * without _STM. Clear cache and schedule _GTF.
676 */
677 ata_link_for_each_dev(dev, &ap->link) {
678 ata_acpi_clear_gtf(dev);
679 dev->flags |= ATA_DFLAG_ACPI_PENDING;
680 }
681 }
Tejun Heo67465442007-05-15 03:28:16 +0900682}
683
684/**
685 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
686 * @dev: target ATA device
687 *
688 * This function is called when @dev is about to be configured.
689 * IDENTIFY data might have been modified after this hook is run.
690 *
691 * LOCKING:
692 * EH context.
693 *
694 * RETURNS:
695 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
696 * -errno on failure.
697 */
698int ata_acpi_on_devcfg(struct ata_device *dev)
699{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900700 struct ata_port *ap = dev->link->ap;
701 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo67465442007-05-15 03:28:16 +0900702 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
703 int rc;
704
Tejun Heo67465442007-05-15 03:28:16 +0900705 if (!dev->acpi_handle)
706 return 0;
707
708 /* do we need to do _GTF? */
709 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
710 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
711 return 0;
712
713 /* do _SDD if SATA */
714 if (acpi_sata) {
715 rc = ata_acpi_push_id(dev);
716 if (rc)
717 goto acpi_err;
718 }
719
720 /* do _GTF */
721 rc = ata_acpi_exec_tfs(dev);
722 if (rc < 0)
723 goto acpi_err;
724
725 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
726
727 /* refresh IDENTIFY page if any _GTF command has been executed */
728 if (rc > 0) {
729 rc = ata_dev_reread_id(dev, 0);
730 if (rc < 0) {
731 ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
732 "after ACPI commands\n");
733 return rc;
734 }
735 }
736
737 return 0;
738
739 acpi_err:
740 /* let EH retry on the first failure, disable ACPI on the second */
741 if (dev->flags & ATA_DFLAG_ACPI_FAILED) {
742 ata_dev_printk(dev, KERN_WARNING, "ACPI on devcfg failed the "
743 "second time, disabling (errno=%d)\n", rc);
744
745 dev->acpi_handle = NULL;
746
747 /* if port is working, request IDENTIFY reload and continue */
748 if (!(ap->pflags & ATA_PFLAG_FROZEN))
749 rc = 1;
750 }
751 dev->flags |= ATA_DFLAG_ACPI_FAILED;
752 return rc;
753}
Tejun Heo562f0c22007-12-15 15:05:01 +0900754
755/**
756 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
757 * @dev: target ATA device
758 *
759 * This function is called when @dev is about to be disabled.
760 *
761 * LOCKING:
762 * EH context.
763 */
764void ata_acpi_on_disable(struct ata_device *dev)
765{
Tejun Heo398e0782007-12-15 15:05:03 +0900766 ata_acpi_clear_gtf(dev);
Tejun Heo562f0c22007-12-15 15:05:01 +0900767}