blob: 8ae36ad029c599f9bd3f79b83968c64e5ed9bafc [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 Heod0df8b5d2007-09-23 13:19:54 +090044/**
45 * ata_acpi_associate_sata_port - associate SATA port with ACPI objects
46 * @ap: target SATA port
47 *
48 * Look up ACPI objects associated with @ap and initialize acpi_handle
49 * fields of @ap, the port and devices accordingly.
50 *
51 * LOCKING:
52 * EH context.
53 *
54 * RETURNS:
55 * 0 on success, -errno on failure.
56 */
57void ata_acpi_associate_sata_port(struct ata_port *ap)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070058{
Tejun Heod0df8b5d2007-09-23 13:19:54 +090059 WARN_ON(!(ap->flags & ATA_FLAG_ACPI_SATA));
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070060
Tejun Heod0df8b5d2007-09-23 13:19:54 +090061 if (!ap->nr_pmp_links) {
62 acpi_integer adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
63
64 ap->link.device->acpi_handle =
65 acpi_get_child(ap->host->acpi_handle, adr);
66 } else {
67 struct ata_link *link;
68
69 ap->link.device->acpi_handle = NULL;
70
71 ata_port_for_each_link(link, ap) {
72 acpi_integer adr = SATA_ADR(ap->port_no, link->pmp);
73
74 link->device->acpi_handle =
75 acpi_get_child(ap->host->acpi_handle, adr);
76 }
77 }
Tejun Heofafbae82007-05-15 03:28:16 +090078}
Alan Coxca426632007-03-08 23:13:50 +000079
Tejun Heofafbae82007-05-15 03:28:16 +090080static void ata_acpi_associate_ide_port(struct ata_port *ap)
81{
82 int max_devices, i;
83
84 ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no);
85 if (!ap->acpi_handle)
86 return;
87
88 max_devices = 1;
89 if (ap->flags & ATA_FLAG_SLAVE_POSS)
90 max_devices++;
91
92 for (i = 0; i < max_devices; i++) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +090093 struct ata_device *dev = &ap->link.device[i];
Tejun Heofafbae82007-05-15 03:28:16 +090094
95 dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
96 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -070097}
98
Jeff Garzik2dcb4072007-10-19 06:42:56 -040099static void ata_acpi_handle_hotplug(struct ata_port *ap, struct kobject *kobj,
100 u32 event)
Matthew Garrett237d8442007-10-03 01:24:16 +0100101{
102 char event_string[12];
103 char *envp[] = { event_string, NULL };
104 struct ata_eh_info *ehi = &ap->link.eh_info;
105
106 if (event == 0 || event == 1) {
107 unsigned long flags;
108 spin_lock_irqsave(ap->lock, flags);
109 ata_ehi_clear_desc(ehi);
110 ata_ehi_push_desc(ehi, "ACPI event");
111 ata_ehi_hotplugged(ehi);
112 ata_port_freeze(ap);
113 spin_unlock_irqrestore(ap->lock, flags);
114 }
115
116 if (kobj) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400117 sprintf(event_string, "BAY_EVENT=%d", event);
Matthew Garrett237d8442007-10-03 01:24:16 +0100118 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
119 }
120}
121
122static void ata_acpi_dev_notify(acpi_handle handle, u32 event, void *data)
123{
124 struct ata_device *dev = data;
125 struct kobject *kobj = NULL;
126
127 if (dev->sdev)
128 kobj = &dev->sdev->sdev_gendev.kobj;
129
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400130 ata_acpi_handle_hotplug(dev->link->ap, kobj, event);
Matthew Garrett237d8442007-10-03 01:24:16 +0100131}
132
133static void ata_acpi_ap_notify(acpi_handle handle, u32 event, void *data)
134{
135 struct ata_port *ap = data;
136
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400137 ata_acpi_handle_hotplug(ap, &ap->dev->kobj, event);
Matthew Garrett237d8442007-10-03 01:24:16 +0100138}
139
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700140/**
Tejun Heofafbae82007-05-15 03:28:16 +0900141 * ata_acpi_associate - associate ATA host with ACPI objects
142 * @host: target ATA host
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700143 *
Tejun Heofafbae82007-05-15 03:28:16 +0900144 * Look up ACPI objects associated with @host and initialize
145 * acpi_handle fields of @host, its ports and devices accordingly.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700146 *
Tejun Heofafbae82007-05-15 03:28:16 +0900147 * LOCKING:
148 * EH context.
149 *
150 * RETURNS:
151 * 0 on success, -errno on failure.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700152 */
Tejun Heofafbae82007-05-15 03:28:16 +0900153void ata_acpi_associate(struct ata_host *host)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700154{
Matthew Garrett237d8442007-10-03 01:24:16 +0100155 int i, j;
Alan Coxca426632007-03-08 23:13:50 +0000156
Tejun Heofafbae82007-05-15 03:28:16 +0900157 if (!is_pci_dev(host->dev) || libata_noacpi)
158 return;
Alan Coxca426632007-03-08 23:13:50 +0000159
Tejun Heofafbae82007-05-15 03:28:16 +0900160 host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev);
161 if (!host->acpi_handle)
162 return;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700163
Tejun Heofafbae82007-05-15 03:28:16 +0900164 for (i = 0; i < host->n_ports; i++) {
165 struct ata_port *ap = host->ports[i];
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700166
Tejun Heofafbae82007-05-15 03:28:16 +0900167 if (host->ports[0]->flags & ATA_FLAG_ACPI_SATA)
168 ata_acpi_associate_sata_port(ap);
169 else
170 ata_acpi_associate_ide_port(ap);
Matthew Garrett237d8442007-10-03 01:24:16 +0100171
172 if (ap->acpi_handle)
173 acpi_install_notify_handler (ap->acpi_handle,
174 ACPI_SYSTEM_NOTIFY,
175 ata_acpi_ap_notify,
176 ap);
177
178 for (j = 0; j < ata_link_max_devices(&ap->link); j++) {
179 struct ata_device *dev = &ap->link.device[j];
180
181 if (dev->acpi_handle)
182 acpi_install_notify_handler (dev->acpi_handle,
183 ACPI_SYSTEM_NOTIFY,
184 ata_acpi_dev_notify,
185 dev);
186 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700187 }
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700188}
189
190/**
Tejun Heo64578a3de2007-05-15 03:28:16 +0900191 * ata_acpi_gtm - execute _GTM
192 * @ap: target ATA port
193 * @gtm: out parameter for _GTM result
194 *
195 * Evaluate _GTM and store the result in @gtm.
196 *
197 * LOCKING:
198 * EH context.
199 *
200 * RETURNS:
201 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
202 */
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900203int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
Tejun Heo64578a3de2007-05-15 03:28:16 +0900204{
205 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
206 union acpi_object *out_obj;
207 acpi_status status;
208 int rc = 0;
209
210 status = acpi_evaluate_object(ap->acpi_handle, "_GTM", NULL, &output);
211
212 rc = -ENOENT;
213 if (status == AE_NOT_FOUND)
214 goto out_free;
215
216 rc = -EINVAL;
217 if (ACPI_FAILURE(status)) {
218 ata_port_printk(ap, KERN_ERR,
219 "ACPI get timing mode failed (AE 0x%x)\n",
220 status);
221 goto out_free;
222 }
223
224 out_obj = output.pointer;
225 if (out_obj->type != ACPI_TYPE_BUFFER) {
226 ata_port_printk(ap, KERN_WARNING,
227 "_GTM returned unexpected object type 0x%x\n",
228 out_obj->type);
229
230 goto out_free;
231 }
232
233 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
234 ata_port_printk(ap, KERN_ERR,
235 "_GTM returned invalid length %d\n",
236 out_obj->buffer.length);
237 goto out_free;
238 }
239
240 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
241 rc = 0;
242 out_free:
243 kfree(output.pointer);
244 return rc;
245}
246
Alan Coxbadff032007-10-04 21:28:18 +0100247EXPORT_SYMBOL_GPL(ata_acpi_gtm);
248
Tejun Heo64578a3de2007-05-15 03:28:16 +0900249/**
250 * ata_acpi_stm - execute _STM
251 * @ap: target ATA port
252 * @stm: timing parameter to _STM
253 *
254 * Evaluate _STM with timing parameter @stm.
255 *
256 * LOCKING:
257 * EH context.
258 *
259 * RETURNS:
260 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
261 */
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900262int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
Tejun Heo64578a3de2007-05-15 03:28:16 +0900263{
264 acpi_status status;
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900265 struct ata_acpi_gtm stm_buf = *stm;
Tejun Heo64578a3de2007-05-15 03:28:16 +0900266 struct acpi_object_list input;
267 union acpi_object in_params[3];
268
269 in_params[0].type = ACPI_TYPE_BUFFER;
270 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
Tejun Heo0d02f0b2007-12-15 15:04:57 +0900271 in_params[0].buffer.pointer = (u8 *)&stm_buf;
Tejun Heo64578a3de2007-05-15 03:28:16 +0900272 /* Buffers for id may need byteswapping ? */
273 in_params[1].type = ACPI_TYPE_BUFFER;
274 in_params[1].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900275 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
Tejun Heo64578a3de2007-05-15 03:28:16 +0900276 in_params[2].type = ACPI_TYPE_BUFFER;
277 in_params[2].buffer.length = 512;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900278 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
Tejun Heo64578a3de2007-05-15 03:28:16 +0900279
280 input.count = 3;
281 input.pointer = in_params;
282
283 status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL);
284
285 if (status == AE_NOT_FOUND)
286 return -ENOENT;
287 if (ACPI_FAILURE(status)) {
288 ata_port_printk(ap, KERN_ERR,
289 "ACPI set timing mode failed (status=0x%x)\n", status);
290 return -EINVAL;
291 }
292 return 0;
293}
294
Alan Coxbadff032007-10-04 21:28:18 +0100295EXPORT_SYMBOL_GPL(ata_acpi_stm);
296
Tejun Heo64578a3de2007-05-15 03:28:16 +0900297/**
Tejun Heo4700c4b2007-05-15 03:28:16 +0900298 * ata_dev_get_GTF - get the drive bootup default taskfile settings
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900299 * @dev: target ATA device
Tejun Heo4700c4b2007-05-15 03:28:16 +0900300 * @gtf: output parameter for buffer containing _GTF taskfile arrays
301 * @ptr_to_free: pointer which should be freed
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700302 *
303 * This applies to both PATA and SATA drives.
304 *
305 * The _GTF method has no input parameters.
306 * It returns a variable number of register set values (registers
307 * hex 1F1..1F7, taskfiles).
308 * The <variable number> is not known in advance, so have ACPI-CA
309 * allocate the buffer as needed and return it, then free it later.
310 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900311 * LOCKING:
312 * EH context.
313 *
314 * RETURNS:
315 * Number of taskfiles on success, 0 if _GTF doesn't exist or doesn't
Matthew Garrett037f6bb2007-11-08 18:37:07 +0000316 * contain valid data.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700317 */
Tejun Heo4700c4b2007-05-15 03:28:16 +0900318static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
319 void **ptr_to_free)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700320{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900321 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900322 acpi_status status;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900323 struct acpi_buffer output;
324 union acpi_object *out_obj;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900325 int rc = 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700326
Tejun Heo4700c4b2007-05-15 03:28:16 +0900327 /* set up output buffer */
328 output.length = ACPI_ALLOCATE_BUFFER;
329 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700330
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700331 if (ata_msg_probe(ap))
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900332 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
Tejun Heo878d4fe2007-02-21 16:36:33 +0900333 __FUNCTION__, ap->port_no);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700334
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700335 /* _GTF has no input parameters */
Tejun Heo4700c4b2007-05-15 03:28:16 +0900336 status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
337
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700338 if (ACPI_FAILURE(status)) {
Tejun Heo4700c4b2007-05-15 03:28:16 +0900339 if (status != AE_NOT_FOUND) {
340 ata_dev_printk(dev, KERN_WARNING,
341 "_GTF evaluation failed (AE 0x%x)\n",
342 status);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900343 }
344 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700345 }
346
347 if (!output.length || !output.pointer) {
348 if (ata_msg_probe(ap))
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900349 ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700350 "length or ptr is NULL (0x%llx, 0x%p)\n",
351 __FUNCTION__,
352 (unsigned long long)output.length,
353 output.pointer);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900354 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700355 }
356
357 out_obj = output.pointer;
358 if (out_obj->type != ACPI_TYPE_BUFFER) {
Tejun Heo69b16a52007-05-15 03:28:16 +0900359 ata_dev_printk(dev, KERN_WARNING,
360 "_GTF unexpected object type 0x%x\n",
361 out_obj->type);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900362 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700363 }
364
Tejun Heo4700c4b2007-05-15 03:28:16 +0900365 if (out_obj->buffer.length % REGS_PER_GTF) {
Tejun Heo69b16a52007-05-15 03:28:16 +0900366 ata_dev_printk(dev, KERN_WARNING,
367 "unexpected _GTF length (%d)\n",
368 out_obj->buffer.length);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900369 goto out_free;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700370 }
371
Tejun Heo4700c4b2007-05-15 03:28:16 +0900372 *ptr_to_free = out_obj;
373 *gtf = (void *)out_obj->buffer.pointer;
374 rc = out_obj->buffer.length / REGS_PER_GTF;
375
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700376 if (ata_msg_probe(ap))
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900377 ata_dev_printk(dev, KERN_DEBUG, "%s: returning "
Tejun Heo4700c4b2007-05-15 03:28:16 +0900378 "gtf=%p, gtf_count=%d, ptr_to_free=%p\n",
379 __FUNCTION__, *gtf, rc, *ptr_to_free);
380 return rc;
381
382 out_free:
383 kfree(output.pointer);
384 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700385}
386
387/**
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400388 * ata_acpi_cbl_80wire - Check for 80 wire cable
389 * @ap: Port to check
390 *
391 * Return 1 if the ACPI mode data for this port indicates the BIOS selected
392 * an 80wire mode.
393 */
394
395int ata_acpi_cbl_80wire(struct ata_port *ap)
396{
397 struct ata_acpi_gtm gtm;
398 int valid = 0;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400399
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400400 /* No _GTM data, no information */
401 if (ata_acpi_gtm(ap, &gtm) < 0)
402 return 0;
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400403
Alan Coxe1ddb4b2007-08-16 02:33:36 -0400404 /* Split timing, DMA enabled */
405 if ((gtm.flags & 0x11) == 0x11 && gtm.drive[0].dma < 55)
406 valid |= 1;
407 if ((gtm.flags & 0x14) == 0x14 && gtm.drive[1].dma < 55)
408 valid |= 2;
409 /* Shared timing, DMA enabled */
410 if ((gtm.flags & 0x11) == 0x01 && gtm.drive[0].dma < 55)
411 valid |= 1;
412 if ((gtm.flags & 0x14) == 0x04 && gtm.drive[0].dma < 55)
413 valid |= 2;
414
415 /* Drive check */
416 if ((valid & 1) && ata_dev_enabled(&ap->link.device[0]))
417 return 1;
418 if ((valid & 2) && ata_dev_enabled(&ap->link.device[1]))
419 return 1;
420 return 0;
421}
422
423EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
424
425/**
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700426 * taskfile_load_raw - send taskfile registers to host controller
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900427 * @dev: target ATA device
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700428 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
429 *
430 * Outputs ATA taskfile to standard ATA host controller using MMIO
431 * or PIO as indicated by the ATA_FLAG_MMIO flag.
432 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
433 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
434 * hob_lbal, hob_lbam, and hob_lbah.
435 *
436 * This function waits for idle (!BUSY and !DRQ) after writing
437 * registers. If the control register has a new value, this
438 * function also waits for idle after writing control and before
439 * writing the remaining registers.
440 *
Tejun Heo4700c4b2007-05-15 03:28:16 +0900441 * LOCKING:
442 * EH context.
443 *
444 * RETURNS:
445 * 0 on success, -errno on failure.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700446 */
Tejun Heo4700c4b2007-05-15 03:28:16 +0900447static int taskfile_load_raw(struct ata_device *dev,
448 const struct ata_acpi_gtf *gtf)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700449{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900450 struct ata_port *ap = dev->link->ap;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900451 struct ata_taskfile tf, rtf;
452 unsigned int err_mask;
Jeff Garzikfc16c252007-02-24 21:05:01 -0500453
Tejun Heo4700c4b2007-05-15 03:28:16 +0900454 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
455 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
456 && (gtf->tf[6] == 0))
457 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700458
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900459 ata_tf_init(dev, &tf);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700460
Jeff Garzikfc16c252007-02-24 21:05:01 -0500461 /* convert gtf to tf */
462 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
Tejun Heo48be6b12007-04-23 02:06:46 +0900463 tf.protocol = ATA_PROT_NODATA;
Tejun Heo4700c4b2007-05-15 03:28:16 +0900464 tf.feature = gtf->tf[0]; /* 0x1f1 */
465 tf.nsect = gtf->tf[1]; /* 0x1f2 */
466 tf.lbal = gtf->tf[2]; /* 0x1f3 */
467 tf.lbam = gtf->tf[3]; /* 0x1f4 */
468 tf.lbah = gtf->tf[4]; /* 0x1f5 */
469 tf.device = gtf->tf[5]; /* 0x1f6 */
470 tf.command = gtf->tf[6]; /* 0x1f7 */
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700471
Tejun Heo4700c4b2007-05-15 03:28:16 +0900472 if (ata_msg_probe(ap))
473 ata_dev_printk(dev, KERN_DEBUG, "executing ACPI cmd "
474 "%02x/%02x:%02x:%02x:%02x:%02x:%02x\n",
475 tf.command, tf.feature, tf.nsect,
476 tf.lbal, tf.lbam, tf.lbah, tf.device);
477
478 rtf = tf;
Tejun Heo2b789102007-10-09 15:05:44 +0900479 err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heo4700c4b2007-05-15 03:28:16 +0900480 if (err_mask) {
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900481 ata_dev_printk(dev, KERN_ERR,
Tejun Heo4700c4b2007-05-15 03:28:16 +0900482 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x failed "
483 "(Emask=0x%x Stat=0x%02x Err=0x%02x)\n",
484 tf.command, tf.feature, tf.nsect, tf.lbal, tf.lbam,
485 tf.lbah, tf.device, err_mask, rtf.command, rtf.feature);
486 return -EIO;
487 }
488
489 return 0;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700490}
491
492/**
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700493 * ata_acpi_exec_tfs - get then write drive taskfile settings
Tejun Heo67465442007-05-15 03:28:16 +0900494 * @dev: target ATA device
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700495 *
Tejun Heo67465442007-05-15 03:28:16 +0900496 * Evaluate _GTF and excute returned taskfiles.
Tejun Heo69b16a52007-05-15 03:28:16 +0900497 *
498 * LOCKING:
499 * EH context.
500 *
501 * RETURNS:
Tejun Heo67465442007-05-15 03:28:16 +0900502 * Number of executed taskfiles on success, 0 if _GTF doesn't exist or
503 * doesn't contain valid data. -errno on other errors.
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700504 */
Tejun Heo67465442007-05-15 03:28:16 +0900505static int ata_acpi_exec_tfs(struct ata_device *dev)
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700506{
Tejun Heo67465442007-05-15 03:28:16 +0900507 struct ata_acpi_gtf *gtf = NULL;
508 void *ptr_to_free = NULL;
509 int gtf_count, i, rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700510
Tejun Heo67465442007-05-15 03:28:16 +0900511 /* get taskfiles */
Matthew Garrett037f6bb2007-11-08 18:37:07 +0000512 gtf_count = ata_dev_get_GTF(dev, &gtf, &ptr_to_free);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700513
Tejun Heo67465442007-05-15 03:28:16 +0900514 /* execute them */
515 for (i = 0, rc = 0; i < gtf_count; i++) {
516 int tmp;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900517
Tejun Heo67465442007-05-15 03:28:16 +0900518 /* ACPI errors are eventually ignored. Run till the
519 * end even after errors.
520 */
521 tmp = taskfile_load_raw(dev, gtf++);
522 if (!rc)
523 rc = tmp;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700524 }
525
Tejun Heo67465442007-05-15 03:28:16 +0900526 kfree(ptr_to_free);
527
528 if (rc == 0)
529 return gtf_count;
530 return rc;
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700531}
532
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700533/**
534 * ata_acpi_push_id - send Identify data to drive
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900535 * @dev: target ATA device
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700536 *
537 * _SDD ACPI object: for SATA mode only
538 * Must be after Identify (Packet) Device -- uses its data
539 * ATM this function never returns a failure. It is an optional
540 * method and if it fails for whatever reason, we should still
541 * just keep going.
Tejun Heo69b16a52007-05-15 03:28:16 +0900542 *
543 * LOCKING:
544 * EH context.
545 *
546 * RETURNS:
547 * 0 on success, -errno on failure.
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700548 */
Tejun Heo67465442007-05-15 03:28:16 +0900549static int ata_acpi_push_id(struct ata_device *dev)
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700550{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900551 struct ata_port *ap = dev->link->ap;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900552 int err;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900553 acpi_status status;
554 struct acpi_object_list input;
555 union acpi_object in_params[1];
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700556
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700557 if (ata_msg_probe(ap))
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900558 ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
559 __FUNCTION__, dev->devno, ap->port_no);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700560
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700561 /* Give the drive Identify data to the drive via the _SDD method */
562 /* _SDD: set up input parameters */
563 input.count = 1;
564 input.pointer = in_params;
565 in_params[0].type = ACPI_TYPE_BUFFER;
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900566 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
567 in_params[0].buffer.pointer = (u8 *)dev->id;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700568 /* Output buffer: _SDD has no output */
569
570 /* It's OK for _SDD to be missing too. */
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900571 swap_buf_le16(dev->id, ATA_ID_WORDS);
Tejun Heofafbae82007-05-15 03:28:16 +0900572 status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
Tejun Heo3a32a8e2007-05-05 23:50:38 +0900573 swap_buf_le16(dev->id, ATA_ID_WORDS);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700574
575 err = ACPI_FAILURE(status) ? -EIO : 0;
Tejun Heo69b16a52007-05-15 03:28:16 +0900576 if (err < 0)
577 ata_dev_printk(dev, KERN_WARNING,
578 "ACPI _SDD failed (AE 0x%x)\n", status);
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700579
Tejun Heo67465442007-05-15 03:28:16 +0900580 return err;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700581}
582
Tejun Heo67465442007-05-15 03:28:16 +0900583/**
Tejun Heo64578a3de2007-05-15 03:28:16 +0900584 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
585 * @ap: target ATA port
586 *
587 * This function is called when @ap is about to be suspended. All
588 * devices are already put to sleep but the port_suspend() callback
589 * hasn't been executed yet. Error return from this function aborts
590 * suspend.
591 *
592 * LOCKING:
593 * EH context.
594 *
595 * RETURNS:
596 * 0 on success, -errno on failure.
597 */
598int ata_acpi_on_suspend(struct ata_port *ap)
599{
600 unsigned long flags;
601 int rc;
602
603 /* proceed iff per-port acpi_handle is valid */
604 if (!ap->acpi_handle)
605 return 0;
606 BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
607
608 /* store timing parameters */
609 rc = ata_acpi_gtm(ap, &ap->acpi_gtm);
610
611 spin_lock_irqsave(ap->lock, flags);
612 if (rc == 0)
613 ap->pflags |= ATA_PFLAG_GTM_VALID;
614 else
615 ap->pflags &= ~ATA_PFLAG_GTM_VALID;
616 spin_unlock_irqrestore(ap->lock, flags);
617
618 if (rc == -ENOENT)
619 rc = 0;
620 return rc;
621}
622
623/**
Tejun Heo67465442007-05-15 03:28:16 +0900624 * ata_acpi_on_resume - ATA ACPI hook called on resume
625 * @ap: target ATA port
626 *
627 * This function is called when @ap is resumed - right after port
628 * itself is resumed but before any EH action is taken.
629 *
630 * LOCKING:
631 * EH context.
632 */
633void ata_acpi_on_resume(struct ata_port *ap)
634{
Tejun Heof58229f2007-08-06 18:36:23 +0900635 struct ata_device *dev;
Kristen Carlson Accardi7ea1fbc2006-09-28 11:29:12 -0700636
Tejun Heo64578a3de2007-05-15 03:28:16 +0900637 if (ap->acpi_handle && (ap->pflags & ATA_PFLAG_GTM_VALID)) {
638 BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
639
640 /* restore timing parameters */
641 ata_acpi_stm(ap, &ap->acpi_gtm);
642 }
643
Tejun Heo67465442007-05-15 03:28:16 +0900644 /* schedule _GTF */
Tejun Heof58229f2007-08-06 18:36:23 +0900645 ata_link_for_each_dev(dev, &ap->link)
646 dev->flags |= ATA_DFLAG_ACPI_PENDING;
Tejun Heo67465442007-05-15 03:28:16 +0900647}
648
649/**
650 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
651 * @dev: target ATA device
652 *
653 * This function is called when @dev is about to be configured.
654 * IDENTIFY data might have been modified after this hook is run.
655 *
656 * LOCKING:
657 * EH context.
658 *
659 * RETURNS:
660 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
661 * -errno on failure.
662 */
663int ata_acpi_on_devcfg(struct ata_device *dev)
664{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900665 struct ata_port *ap = dev->link->ap;
666 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo67465442007-05-15 03:28:16 +0900667 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
668 int rc;
669
Tejun Heo67465442007-05-15 03:28:16 +0900670 if (!dev->acpi_handle)
671 return 0;
672
673 /* do we need to do _GTF? */
674 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
675 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
676 return 0;
677
678 /* do _SDD if SATA */
679 if (acpi_sata) {
680 rc = ata_acpi_push_id(dev);
681 if (rc)
682 goto acpi_err;
683 }
684
685 /* do _GTF */
686 rc = ata_acpi_exec_tfs(dev);
687 if (rc < 0)
688 goto acpi_err;
689
690 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
691
692 /* refresh IDENTIFY page if any _GTF command has been executed */
693 if (rc > 0) {
694 rc = ata_dev_reread_id(dev, 0);
695 if (rc < 0) {
696 ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
697 "after ACPI commands\n");
698 return rc;
699 }
700 }
701
702 return 0;
703
704 acpi_err:
705 /* let EH retry on the first failure, disable ACPI on the second */
706 if (dev->flags & ATA_DFLAG_ACPI_FAILED) {
707 ata_dev_printk(dev, KERN_WARNING, "ACPI on devcfg failed the "
708 "second time, disabling (errno=%d)\n", rc);
709
710 dev->acpi_handle = NULL;
711
712 /* if port is working, request IDENTIFY reload and continue */
713 if (!(ap->pflags & ATA_PFLAG_FROZEN))
714 rc = 1;
715 }
716 dev->flags |= ATA_DFLAG_ACPI_FAILED;
717 return rc;
718}