]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/ata/libata-acpi.c
libata-acpi: improve _GTF execution error handling and reporting
[mirror_ubuntu-artful-kernel.git] / drivers / ata / libata-acpi.c
CommitLineData
11ef697b
KCA
1/*
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>
237d8440 17#include <scsi/scsi_device.h>
11ef697b
KCA
18#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
11ef697b 28#define NO_PORT_MULT 0xffff
2dcb407e 29#define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
11ef697b
KCA
30
31#define REGS_PER_GTF 7
4700c4bc
TH
32struct ata_acpi_gtf {
33 u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
34} __packed;
11ef697b 35
ca426635
AC
36/*
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}
11ef697b 43
398e0782
TH
44static void ata_acpi_clear_gtf(struct ata_device *dev)
45{
46 kfree(dev->gtf_cache);
47 dev->gtf_cache = NULL;
48}
49
d0df8b5d
TH
50/**
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)
11ef697b 64{
d0df8b5d
TH
65 WARN_ON(!(ap->flags & ATA_FLAG_ACPI_SATA));
66
67 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);
fafbae87 79
d0df8b5d
TH
80 link->device->acpi_handle =
81 acpi_get_child(ap->host->acpi_handle, adr);
82 }
83 }
11ef697b
KCA
84}
85
fafbae87 86static void ata_acpi_associate_ide_port(struct ata_port *ap)
11ef697b 87{
fafbae87 88 int max_devices, i;
11ef697b 89
fafbae87
TH
90 ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no);
91 if (!ap->acpi_handle)
92 return;
11ef697b 93
fafbae87
TH
94 max_devices = 1;
95 if (ap->flags & ATA_FLAG_SLAVE_POSS)
96 max_devices++;
11ef697b 97
fafbae87 98 for (i = 0; i < max_devices; i++) {
9af5c9c9 99 struct ata_device *dev = &ap->link.device[i];
11ef697b 100
fafbae87 101 dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
11ef697b 102 }
c05e6ff0
TH
103
104 if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
105 ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
11ef697b
KCA
106}
107
2dcb407e
JG
108static void ata_acpi_handle_hotplug(struct ata_port *ap, struct kobject *kobj,
109 u32 event)
237d8440
MG
110{
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) {
2dcb407e 126 sprintf(event_string, "BAY_EVENT=%d", event);
237d8440
MG
127 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
2dcb407e 139 ata_acpi_handle_hotplug(dev->link->ap, kobj, event);
237d8440
MG
140}
141
142static void ata_acpi_ap_notify(acpi_handle handle, u32 event, void *data)
143{
144 struct ata_port *ap = data;
145
2dcb407e 146 ata_acpi_handle_hotplug(ap, &ap->dev->kobj, event);
237d8440
MG
147}
148
fafbae87
TH
149/**
150 * ata_acpi_associate - associate ATA host with ACPI objects
151 * @host: target ATA host
152 *
153 * Look up ACPI objects associated with @host and initialize
154 * acpi_handle fields of @host, its ports and devices accordingly.
155 *
156 * LOCKING:
157 * EH context.
158 *
159 * RETURNS:
160 * 0 on success, -errno on failure.
161 */
162void ata_acpi_associate(struct ata_host *host)
11ef697b 163{
237d8440 164 int i, j;
11ef697b 165
fafbae87
TH
166 if (!is_pci_dev(host->dev) || libata_noacpi)
167 return;
11ef697b 168
fafbae87
TH
169 host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev);
170 if (!host->acpi_handle)
171 return;
11ef697b 172
fafbae87
TH
173 for (i = 0; i < host->n_ports; i++) {
174 struct ata_port *ap = host->ports[i];
175
176 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);
237d8440
MG
180
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 }
11ef697b 196 }
11ef697b
KCA
197}
198
562f0c2d
TH
199/**
200 * 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{
c05e6ff0
TH
211 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 }
562f0c2d
TH
223}
224
64578a3d
TH
225/**
226 * 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 */
0d02f0b2 238int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
64578a3d
TH
239{
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
badff03d
AC
282EXPORT_SYMBOL_GPL(ata_acpi_gtm);
283
64578a3d
TH
284/**
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 */
0d02f0b2 297int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
64578a3d
TH
298{
299 acpi_status status;
0d02f0b2 300 struct ata_acpi_gtm stm_buf = *stm;
64578a3d
TH
301 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);
0d02f0b2 306 in_params[0].buffer.pointer = (u8 *)&stm_buf;
64578a3d
TH
307 /* Buffers for id may need byteswapping ? */
308 in_params[1].type = ACPI_TYPE_BUFFER;
309 in_params[1].buffer.length = 512;
9af5c9c9 310 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
64578a3d
TH
311 in_params[2].type = ACPI_TYPE_BUFFER;
312 in_params[2].buffer.length = 512;
9af5c9c9 313 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
64578a3d
TH
314
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
badff03d
AC
330EXPORT_SYMBOL_GPL(ata_acpi_stm);
331
11ef697b 332/**
4700c4bc 333 * ata_dev_get_GTF - get the drive bootup default taskfile settings
3a32a8e9 334 * @dev: target ATA device
4700c4bc 335 * @gtf: output parameter for buffer containing _GTF taskfile arrays
11ef697b
KCA
336 *
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 *
4700c4bc
TH
345 * LOCKING:
346 * EH context.
347 *
348 * RETURNS:
66fa7f21
TH
349 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
350 * if _GTF is invalid.
11ef697b 351 */
398e0782 352static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
11ef697b 353{
9af5c9c9 354 struct ata_port *ap = dev->link->ap;
3a32a8e9 355 acpi_status status;
3a32a8e9
TH
356 struct acpi_buffer output;
357 union acpi_object *out_obj;
4700c4bc 358 int rc = 0;
11ef697b 359
398e0782
TH
360 /* if _GTF is cached, use the cached value */
361 if (dev->gtf_cache) {
362 out_obj = dev->gtf_cache;
363 goto done;
364 }
365
4700c4bc
TH
366 /* set up output buffer */
367 output.length = ACPI_ALLOCATE_BUFFER;
368 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
11ef697b 369
11ef697b 370 if (ata_msg_probe(ap))
3a32a8e9 371 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
878d4fed 372 __FUNCTION__, ap->port_no);
11ef697b 373
11ef697b 374 /* _GTF has no input parameters */
4700c4bc 375 status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
398e0782 376 out_obj = dev->gtf_cache = output.pointer;
4700c4bc 377
11ef697b 378 if (ACPI_FAILURE(status)) {
4700c4bc
TH
379 if (status != AE_NOT_FOUND) {
380 ata_dev_printk(dev, KERN_WARNING,
381 "_GTF evaluation failed (AE 0x%x)\n",
382 status);
66fa7f21 383 rc = -EINVAL;
4700c4bc
TH
384 }
385 goto out_free;
11ef697b
KCA
386 }
387
388 if (!output.length || !output.pointer) {
389 if (ata_msg_probe(ap))
3a32a8e9 390 ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
11ef697b
KCA
391 "length or ptr is NULL (0x%llx, 0x%p)\n",
392 __FUNCTION__,
393 (unsigned long long)output.length,
394 output.pointer);
66fa7f21 395 rc = -EINVAL;
4700c4bc 396 goto out_free;
11ef697b
KCA
397 }
398
11ef697b 399 if (out_obj->type != ACPI_TYPE_BUFFER) {
69b16a5f
TH
400 ata_dev_printk(dev, KERN_WARNING,
401 "_GTF unexpected object type 0x%x\n",
402 out_obj->type);
66fa7f21 403 rc = -EINVAL;
4700c4bc 404 goto out_free;
11ef697b
KCA
405 }
406
4700c4bc 407 if (out_obj->buffer.length % REGS_PER_GTF) {
69b16a5f
TH
408 ata_dev_printk(dev, KERN_WARNING,
409 "unexpected _GTF length (%d)\n",
410 out_obj->buffer.length);
66fa7f21 411 rc = -EINVAL;
4700c4bc 412 goto out_free;
11ef697b
KCA
413 }
414
398e0782 415 done:
4700c4bc 416 rc = out_obj->buffer.length / REGS_PER_GTF;
398e0782
TH
417 if (gtf) {
418 *gtf = (void *)out_obj->buffer.pointer;
419 if (ata_msg_probe(ap))
420 ata_dev_printk(dev, KERN_DEBUG,
421 "%s: returning gtf=%p, gtf_count=%d\n",
422 __FUNCTION__, *gtf, rc);
423 }
4700c4bc
TH
424 return rc;
425
426 out_free:
398e0782 427 ata_acpi_clear_gtf(dev);
4700c4bc 428 return rc;
11ef697b
KCA
429}
430
e1ddb4b6
AC
431/**
432 * ata_acpi_cbl_80wire - Check for 80 wire cable
433 * @ap: Port to check
434 *
435 * Return 1 if the ACPI mode data for this port indicates the BIOS selected
436 * an 80wire mode.
437 */
438
439int ata_acpi_cbl_80wire(struct ata_port *ap)
440{
c05e6ff0 441 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
e1ddb4b6 442 int valid = 0;
2dcb407e 443
c05e6ff0 444 if (!gtm)
e1ddb4b6 445 return 0;
2dcb407e 446
e1ddb4b6 447 /* Split timing, DMA enabled */
c05e6ff0 448 if ((gtm->flags & 0x11) == 0x11 && gtm->drive[0].dma < 55)
e1ddb4b6 449 valid |= 1;
c05e6ff0 450 if ((gtm->flags & 0x14) == 0x14 && gtm->drive[1].dma < 55)
e1ddb4b6
AC
451 valid |= 2;
452 /* Shared timing, DMA enabled */
c05e6ff0 453 if ((gtm->flags & 0x11) == 0x01 && gtm->drive[0].dma < 55)
e1ddb4b6 454 valid |= 1;
c05e6ff0 455 if ((gtm->flags & 0x14) == 0x04 && gtm->drive[0].dma < 55)
e1ddb4b6
AC
456 valid |= 2;
457
458 /* Drive check */
459 if ((valid & 1) && ata_dev_enabled(&ap->link.device[0]))
460 return 1;
461 if ((valid & 2) && ata_dev_enabled(&ap->link.device[1]))
462 return 1;
463 return 0;
464}
465
466EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
467
11ef697b 468/**
0e8634bf 469 * ata_acpi_run_tf - send taskfile registers to host controller
3a32a8e9 470 * @dev: target ATA device
11ef697b
KCA
471 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
472 *
473 * Outputs ATA taskfile to standard ATA host controller using MMIO
474 * or PIO as indicated by the ATA_FLAG_MMIO flag.
475 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
476 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
477 * hob_lbal, hob_lbam, and hob_lbah.
478 *
479 * This function waits for idle (!BUSY and !DRQ) after writing
480 * registers. If the control register has a new value, this
481 * function also waits for idle after writing control and before
482 * writing the remaining registers.
483 *
4700c4bc
TH
484 * LOCKING:
485 * EH context.
486 *
487 * RETURNS:
0e8634bf
TH
488 * 1 if command is executed successfully. 0 if ignored or rejected,
489 * -errno on other errors.
11ef697b 490 */
0e8634bf
TH
491static int ata_acpi_run_tf(struct ata_device *dev,
492 const struct ata_acpi_gtf *gtf)
11ef697b 493{
4700c4bc
TH
494 struct ata_taskfile tf, rtf;
495 unsigned int err_mask;
0e8634bf
TH
496 const char *level;
497 char msg[60];
498 int rc;
fc16c25f 499
4700c4bc
TH
500 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
501 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
502 && (gtf->tf[6] == 0))
503 return 0;
11ef697b 504
3a32a8e9 505 ata_tf_init(dev, &tf);
fc16c25f
JG
506
507 /* convert gtf to tf */
508 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
48be6b18 509 tf.protocol = ATA_PROT_NODATA;
4700c4bc
TH
510 tf.feature = gtf->tf[0]; /* 0x1f1 */
511 tf.nsect = gtf->tf[1]; /* 0x1f2 */
512 tf.lbal = gtf->tf[2]; /* 0x1f3 */
513 tf.lbam = gtf->tf[3]; /* 0x1f4 */
514 tf.lbah = gtf->tf[4]; /* 0x1f5 */
515 tf.device = gtf->tf[5]; /* 0x1f6 */
516 tf.command = gtf->tf[6]; /* 0x1f7 */
517
4700c4bc 518 rtf = tf;
2b789108 519 err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0, 0);
0e8634bf
TH
520
521 switch (err_mask) {
522 case 0:
523 level = KERN_DEBUG;
524 snprintf(msg, sizeof(msg), "succeeded");
525 rc = 1;
526 break;
527
528 case AC_ERR_DEV:
529 level = KERN_INFO;
530 snprintf(msg, sizeof(msg),
531 "rejected by device (Stat=0x%02x Err=0x%02x)",
532 rtf.command, rtf.feature);
533 rc = 0;
534 break;
535
536 default:
537 level = KERN_ERR;
538 snprintf(msg, sizeof(msg),
539 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
540 err_mask, rtf.command, rtf.feature);
541 rc = -EIO;
542 break;
4700c4bc
TH
543 }
544
0e8634bf
TH
545 ata_dev_printk(dev, level,
546 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x %s\n",
547 tf.command, tf.feature, tf.nsect, tf.lbal,
548 tf.lbam, tf.lbah, tf.device, msg);
549
550 return rc;
11ef697b
KCA
551}
552
11ef697b
KCA
553/**
554 * ata_acpi_exec_tfs - get then write drive taskfile settings
6746544c 555 * @dev: target ATA device
66fa7f21 556 * @nr_executed: out paramter for the number of executed commands
11ef697b 557 *
6746544c 558 * Evaluate _GTF and excute returned taskfiles.
69b16a5f
TH
559 *
560 * LOCKING:
561 * EH context.
562 *
563 * RETURNS:
66fa7f21
TH
564 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
565 * -errno on other errors.
11ef697b 566 */
66fa7f21 567static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
11ef697b 568{
6746544c 569 struct ata_acpi_gtf *gtf = NULL;
6746544c
TH
570 int gtf_count, i, rc;
571
572 /* get taskfiles */
66fa7f21
TH
573 rc = ata_dev_get_GTF(dev, &gtf);
574 if (rc < 0)
575 return rc;
576 gtf_count = rc;
6746544c
TH
577
578 /* execute them */
0e8634bf
TH
579 for (i = 0; i < gtf_count; i++) {
580 rc = ata_acpi_run_tf(dev, gtf++);
581 if (rc < 0)
582 break;
583 if (rc)
584 (*nr_executed)++;
11ef697b
KCA
585 }
586
398e0782 587 ata_acpi_clear_gtf(dev);
6746544c 588
0e8634bf
TH
589 if (rc < 0)
590 return rc;
591 return 0;
11ef697b
KCA
592}
593
7ea1fbc2
KCA
594/**
595 * ata_acpi_push_id - send Identify data to drive
3a32a8e9 596 * @dev: target ATA device
7ea1fbc2
KCA
597 *
598 * _SDD ACPI object: for SATA mode only
599 * Must be after Identify (Packet) Device -- uses its data
600 * ATM this function never returns a failure. It is an optional
601 * method and if it fails for whatever reason, we should still
602 * just keep going.
69b16a5f
TH
603 *
604 * LOCKING:
605 * EH context.
606 *
607 * RETURNS:
608 * 0 on success, -errno on failure.
7ea1fbc2 609 */
6746544c 610static int ata_acpi_push_id(struct ata_device *dev)
7ea1fbc2 611{
9af5c9c9 612 struct ata_port *ap = dev->link->ap;
3a32a8e9 613 int err;
3a32a8e9
TH
614 acpi_status status;
615 struct acpi_object_list input;
616 union acpi_object in_params[1];
7ea1fbc2 617
7ea1fbc2 618 if (ata_msg_probe(ap))
3a32a8e9
TH
619 ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
620 __FUNCTION__, dev->devno, ap->port_no);
7ea1fbc2 621
7ea1fbc2
KCA
622 /* Give the drive Identify data to the drive via the _SDD method */
623 /* _SDD: set up input parameters */
624 input.count = 1;
625 input.pointer = in_params;
626 in_params[0].type = ACPI_TYPE_BUFFER;
3a32a8e9
TH
627 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
628 in_params[0].buffer.pointer = (u8 *)dev->id;
7ea1fbc2
KCA
629 /* Output buffer: _SDD has no output */
630
631 /* It's OK for _SDD to be missing too. */
3a32a8e9 632 swap_buf_le16(dev->id, ATA_ID_WORDS);
fafbae87 633 status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
3a32a8e9 634 swap_buf_le16(dev->id, ATA_ID_WORDS);
7ea1fbc2
KCA
635
636 err = ACPI_FAILURE(status) ? -EIO : 0;
69b16a5f
TH
637 if (err < 0)
638 ata_dev_printk(dev, KERN_WARNING,
639 "ACPI _SDD failed (AE 0x%x)\n", status);
7ea1fbc2 640
6746544c
TH
641 return err;
642}
643
64578a3d
TH
644/**
645 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
646 * @ap: target ATA port
647 *
648 * This function is called when @ap is about to be suspended. All
649 * devices are already put to sleep but the port_suspend() callback
650 * hasn't been executed yet. Error return from this function aborts
651 * suspend.
652 *
653 * LOCKING:
654 * EH context.
655 *
656 * RETURNS:
657 * 0 on success, -errno on failure.
658 */
659int ata_acpi_on_suspend(struct ata_port *ap)
660{
c05e6ff0
TH
661 /* nada */
662 return 0;
64578a3d
TH
663}
664
6746544c
TH
665/**
666 * ata_acpi_on_resume - ATA ACPI hook called on resume
667 * @ap: target ATA port
668 *
669 * This function is called when @ap is resumed - right after port
670 * itself is resumed but before any EH action is taken.
671 *
672 * LOCKING:
673 * EH context.
674 */
675void ata_acpi_on_resume(struct ata_port *ap)
676{
c05e6ff0 677 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
f58229f8 678 struct ata_device *dev;
6746544c 679
398e0782
TH
680 if (ap->acpi_handle && gtm) {
681 /* _GTM valid */
682
683 /* restore timing parameters */
c05e6ff0 684 ata_acpi_stm(ap, gtm);
64578a3d 685
398e0782
TH
686 /* _GTF should immediately follow _STM so that it can
687 * use values set by _STM. Cache _GTF result and
688 * schedule _GTF.
689 */
690 ata_link_for_each_dev(dev, &ap->link) {
691 ata_acpi_clear_gtf(dev);
692 if (ata_dev_get_GTF(dev, NULL) >= 0)
693 dev->flags |= ATA_DFLAG_ACPI_PENDING;
694 }
695 } else {
696 /* SATA _GTF needs to be evaulated after _SDD and
697 * there's no reason to evaluate IDE _GTF early
698 * without _STM. Clear cache and schedule _GTF.
699 */
700 ata_link_for_each_dev(dev, &ap->link) {
701 ata_acpi_clear_gtf(dev);
702 dev->flags |= ATA_DFLAG_ACPI_PENDING;
703 }
704 }
7ea1fbc2
KCA
705}
706
6746544c
TH
707/**
708 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
709 * @dev: target ATA device
710 *
711 * This function is called when @dev is about to be configured.
712 * IDENTIFY data might have been modified after this hook is run.
713 *
714 * LOCKING:
715 * EH context.
716 *
717 * RETURNS:
718 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
719 * -errno on failure.
720 */
721int ata_acpi_on_devcfg(struct ata_device *dev)
722{
9af5c9c9
TH
723 struct ata_port *ap = dev->link->ap;
724 struct ata_eh_context *ehc = &ap->link.eh_context;
6746544c 725 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
66fa7f21 726 int nr_executed = 0;
6746544c
TH
727 int rc;
728
6746544c
TH
729 if (!dev->acpi_handle)
730 return 0;
731
732 /* do we need to do _GTF? */
733 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
734 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
735 return 0;
736
737 /* do _SDD if SATA */
738 if (acpi_sata) {
739 rc = ata_acpi_push_id(dev);
740 if (rc)
741 goto acpi_err;
742 }
743
744 /* do _GTF */
66fa7f21
TH
745 rc = ata_acpi_exec_tfs(dev, &nr_executed);
746 if (rc)
6746544c
TH
747 goto acpi_err;
748
749 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
750
751 /* refresh IDENTIFY page if any _GTF command has been executed */
66fa7f21 752 if (nr_executed) {
6746544c
TH
753 rc = ata_dev_reread_id(dev, 0);
754 if (rc < 0) {
755 ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
756 "after ACPI commands\n");
757 return rc;
758 }
759 }
760
761 return 0;
762
763 acpi_err:
66fa7f21
TH
764 /* ignore evaluation failure if we can continue safely */
765 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
766 return 0;
6746544c 767
66fa7f21
TH
768 /* fail and let EH retry once more for unknown IO errors */
769 if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
770 dev->flags |= ATA_DFLAG_ACPI_FAILED;
771 return rc;
6746544c 772 }
66fa7f21
TH
773
774 ata_dev_printk(dev, KERN_WARNING,
775 "ACPI: failed the second time, disabled\n");
776 dev->acpi_handle = NULL;
777
778 /* We can safely continue if no _GTF command has been executed
779 * and port is not frozen.
780 */
781 if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
782 return 0;
783
6746544c
TH
784 return rc;
785}
562f0c2d
TH
786
787/**
788 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
789 * @dev: target ATA device
790 *
791 * This function is called when @dev is about to be disabled.
792 *
793 * LOCKING:
794 * EH context.
795 */
796void ata_acpi_on_disable(struct ata_device *dev)
797{
398e0782 798 ata_acpi_clear_gtf(dev);
562f0c2d 799}