Pull battery into release branch
[pandora-kernel.git] / drivers / ata / libata-acpi.c
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>
17 #include <scsi/scsi_device.h>
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
28 #define NO_PORT_MULT            0xffff
29 #define SATA_ADR(root, pmp)     (((root) << 16) | (pmp))
30
31 #define REGS_PER_GTF            7
32 struct ata_acpi_gtf {
33         u8      tf[REGS_PER_GTF];       /* regs. 0x1f1 - 0x1f7 */
34 } __packed;
35
36 /*
37  *      Helper - belongs in the PCI layer somewhere eventually
38  */
39 static int is_pci_dev(struct device *dev)
40 {
41         return (dev->bus == &pci_bus_type);
42 }
43
44 /**
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  */
57 void ata_acpi_associate_sata_port(struct ata_port *ap)
58 {
59         WARN_ON(!(ap->flags & ATA_FLAG_ACPI_SATA));
60
61         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         }
78 }
79
80 static 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++) {
93                 struct ata_device *dev = &ap->link.device[i];
94
95                 dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
96         }
97 }
98
99 static void ata_acpi_handle_hotplug(struct ata_port *ap, struct kobject *kobj,
100                                     u32 event)
101 {
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) {
117                 sprintf(event_string, "BAY_EVENT=%d", event);
118                 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
119         }
120 }
121
122 static 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
130         ata_acpi_handle_hotplug(dev->link->ap, kobj, event);
131 }
132
133 static void ata_acpi_ap_notify(acpi_handle handle, u32 event, void *data)
134 {
135         struct ata_port *ap = data;
136
137         ata_acpi_handle_hotplug(ap, &ap->dev->kobj, event);
138 }
139
140 /**
141  * ata_acpi_associate - associate ATA host with ACPI objects
142  * @host: target ATA host
143  *
144  * Look up ACPI objects associated with @host and initialize
145  * acpi_handle fields of @host, its ports and devices accordingly.
146  *
147  * LOCKING:
148  * EH context.
149  *
150  * RETURNS:
151  * 0 on success, -errno on failure.
152  */
153 void ata_acpi_associate(struct ata_host *host)
154 {
155         int i, j;
156
157         if (!is_pci_dev(host->dev) || libata_noacpi)
158                 return;
159
160         host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev);
161         if (!host->acpi_handle)
162                 return;
163
164         for (i = 0; i < host->n_ports; i++) {
165                 struct ata_port *ap = host->ports[i];
166
167                 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);
171
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                 }
187         }
188 }
189
190 /**
191  * 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  */
203 int ata_acpi_gtm(const struct ata_port *ap, struct ata_acpi_gtm *gtm)
204 {
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
247 EXPORT_SYMBOL_GPL(ata_acpi_gtm);
248
249 /**
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  */
262 int ata_acpi_stm(const struct ata_port *ap, struct ata_acpi_gtm *stm)
263 {
264         acpi_status status;
265         struct acpi_object_list         input;
266         union acpi_object               in_params[3];
267
268         in_params[0].type = ACPI_TYPE_BUFFER;
269         in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
270         in_params[0].buffer.pointer = (u8 *)stm;
271         /* Buffers for id may need byteswapping ? */
272         in_params[1].type = ACPI_TYPE_BUFFER;
273         in_params[1].buffer.length = 512;
274         in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
275         in_params[2].type = ACPI_TYPE_BUFFER;
276         in_params[2].buffer.length = 512;
277         in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
278
279         input.count = 3;
280         input.pointer = in_params;
281
282         status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL);
283
284         if (status == AE_NOT_FOUND)
285                 return -ENOENT;
286         if (ACPI_FAILURE(status)) {
287                 ata_port_printk(ap, KERN_ERR,
288                         "ACPI set timing mode failed (status=0x%x)\n", status);
289                 return -EINVAL;
290         }
291         return 0;
292 }
293
294 EXPORT_SYMBOL_GPL(ata_acpi_stm);
295
296 /**
297  * ata_dev_get_GTF - get the drive bootup default taskfile settings
298  * @dev: target ATA device
299  * @gtf: output parameter for buffer containing _GTF taskfile arrays
300  * @ptr_to_free: pointer which should be freed
301  *
302  * This applies to both PATA and SATA drives.
303  *
304  * The _GTF method has no input parameters.
305  * It returns a variable number of register set values (registers
306  * hex 1F1..1F7, taskfiles).
307  * The <variable number> is not known in advance, so have ACPI-CA
308  * allocate the buffer as needed and return it, then free it later.
309  *
310  * LOCKING:
311  * EH context.
312  *
313  * RETURNS:
314  * Number of taskfiles on success, 0 if _GTF doesn't exist or doesn't
315  * contain valid data.
316  */
317 static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
318                            void **ptr_to_free)
319 {
320         struct ata_port *ap = dev->link->ap;
321         acpi_status status;
322         struct acpi_buffer output;
323         union acpi_object *out_obj;
324         int rc = 0;
325
326         /* set up output buffer */
327         output.length = ACPI_ALLOCATE_BUFFER;
328         output.pointer = NULL;  /* ACPI-CA sets this; save/free it later */
329
330         if (ata_msg_probe(ap))
331                 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
332                                __FUNCTION__, ap->port_no);
333
334         /* _GTF has no input parameters */
335         status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
336
337         if (ACPI_FAILURE(status)) {
338                 if (status != AE_NOT_FOUND) {
339                         ata_dev_printk(dev, KERN_WARNING,
340                                        "_GTF evaluation failed (AE 0x%x)\n",
341                                        status);
342                 }
343                 goto out_free;
344         }
345
346         if (!output.length || !output.pointer) {
347                 if (ata_msg_probe(ap))
348                         ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
349                                 "length or ptr is NULL (0x%llx, 0x%p)\n",
350                                 __FUNCTION__,
351                                 (unsigned long long)output.length,
352                                 output.pointer);
353                 goto out_free;
354         }
355
356         out_obj = output.pointer;
357         if (out_obj->type != ACPI_TYPE_BUFFER) {
358                 ata_dev_printk(dev, KERN_WARNING,
359                                "_GTF unexpected object type 0x%x\n",
360                                out_obj->type);
361                 goto out_free;
362         }
363
364         if (out_obj->buffer.length % REGS_PER_GTF) {
365                 ata_dev_printk(dev, KERN_WARNING,
366                                "unexpected _GTF length (%d)\n",
367                                out_obj->buffer.length);
368                 goto out_free;
369         }
370
371         *ptr_to_free = out_obj;
372         *gtf = (void *)out_obj->buffer.pointer;
373         rc = out_obj->buffer.length / REGS_PER_GTF;
374
375         if (ata_msg_probe(ap))
376                 ata_dev_printk(dev, KERN_DEBUG, "%s: returning "
377                         "gtf=%p, gtf_count=%d, ptr_to_free=%p\n",
378                         __FUNCTION__, *gtf, rc, *ptr_to_free);
379         return rc;
380
381  out_free:
382         kfree(output.pointer);
383         return rc;
384 }
385
386 /**
387  * ata_acpi_cbl_80wire          -       Check for 80 wire cable
388  * @ap: Port to check
389  *
390  * Return 1 if the ACPI mode data for this port indicates the BIOS selected
391  * an 80wire mode.
392  */
393
394 int ata_acpi_cbl_80wire(struct ata_port *ap)
395 {
396         struct ata_acpi_gtm gtm;
397         int valid = 0;
398
399         /* No _GTM data, no information */
400         if (ata_acpi_gtm(ap, &gtm) < 0)
401                 return 0;
402
403         /* Split timing, DMA enabled */
404         if ((gtm.flags & 0x11) == 0x11 && gtm.drive[0].dma < 55)
405                 valid |= 1;
406         if ((gtm.flags & 0x14) == 0x14 && gtm.drive[1].dma < 55)
407                 valid |= 2;
408         /* Shared timing, DMA enabled */
409         if ((gtm.flags & 0x11) == 0x01 && gtm.drive[0].dma < 55)
410                 valid |= 1;
411         if ((gtm.flags & 0x14) == 0x04 && gtm.drive[0].dma < 55)
412                 valid |= 2;
413
414         /* Drive check */
415         if ((valid & 1) && ata_dev_enabled(&ap->link.device[0]))
416                 return 1;
417         if ((valid & 2) && ata_dev_enabled(&ap->link.device[1]))
418                 return 1;
419         return 0;
420 }
421
422 EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
423
424 /**
425  * taskfile_load_raw - send taskfile registers to host controller
426  * @dev: target ATA device
427  * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
428  *
429  * Outputs ATA taskfile to standard ATA host controller using MMIO
430  * or PIO as indicated by the ATA_FLAG_MMIO flag.
431  * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
432  * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
433  * hob_lbal, hob_lbam, and hob_lbah.
434  *
435  * This function waits for idle (!BUSY and !DRQ) after writing
436  * registers.  If the control register has a new value, this
437  * function also waits for idle after writing control and before
438  * writing the remaining registers.
439  *
440  * LOCKING:
441  * EH context.
442  *
443  * RETURNS:
444  * 0 on success, -errno on failure.
445  */
446 static int taskfile_load_raw(struct ata_device *dev,
447                               const struct ata_acpi_gtf *gtf)
448 {
449         struct ata_port *ap = dev->link->ap;
450         struct ata_taskfile tf, rtf;
451         unsigned int err_mask;
452
453         if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
454             && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
455             && (gtf->tf[6] == 0))
456                 return 0;
457
458         ata_tf_init(dev, &tf);
459
460         /* convert gtf to tf */
461         tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
462         tf.protocol = ATA_PROT_NODATA;
463         tf.feature = gtf->tf[0];        /* 0x1f1 */
464         tf.nsect   = gtf->tf[1];        /* 0x1f2 */
465         tf.lbal    = gtf->tf[2];        /* 0x1f3 */
466         tf.lbam    = gtf->tf[3];        /* 0x1f4 */
467         tf.lbah    = gtf->tf[4];        /* 0x1f5 */
468         tf.device  = gtf->tf[5];        /* 0x1f6 */
469         tf.command = gtf->tf[6];        /* 0x1f7 */
470
471         if (ata_msg_probe(ap))
472                 ata_dev_printk(dev, KERN_DEBUG, "executing ACPI cmd "
473                                "%02x/%02x:%02x:%02x:%02x:%02x:%02x\n",
474                                tf.command, tf.feature, tf.nsect,
475                                tf.lbal, tf.lbam, tf.lbah, tf.device);
476
477         rtf = tf;
478         err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0, 0);
479         if (err_mask) {
480                 ata_dev_printk(dev, KERN_ERR,
481                         "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x failed "
482                         "(Emask=0x%x Stat=0x%02x Err=0x%02x)\n",
483                         tf.command, tf.feature, tf.nsect, tf.lbal, tf.lbam,
484                         tf.lbah, tf.device, err_mask, rtf.command, rtf.feature);
485                 return -EIO;
486         }
487
488         return 0;
489 }
490
491 /**
492  * ata_acpi_exec_tfs - get then write drive taskfile settings
493  * @dev: target ATA device
494  *
495  * Evaluate _GTF and excute returned taskfiles.
496  *
497  * LOCKING:
498  * EH context.
499  *
500  * RETURNS:
501  * Number of executed taskfiles on success, 0 if _GTF doesn't exist or
502  * doesn't contain valid data.  -errno on other errors.
503  */
504 static int ata_acpi_exec_tfs(struct ata_device *dev)
505 {
506         struct ata_acpi_gtf *gtf = NULL;
507         void *ptr_to_free = NULL;
508         int gtf_count, i, rc;
509
510         /* get taskfiles */
511         gtf_count = ata_dev_get_GTF(dev, &gtf, &ptr_to_free);
512
513         /* execute them */
514         for (i = 0, rc = 0; i < gtf_count; i++) {
515                 int tmp;
516
517                 /* ACPI errors are eventually ignored.  Run till the
518                  * end even after errors.
519                  */
520                 tmp = taskfile_load_raw(dev, gtf++);
521                 if (!rc)
522                         rc = tmp;
523         }
524
525         kfree(ptr_to_free);
526
527         if (rc == 0)
528                 return gtf_count;
529         return rc;
530 }
531
532 /**
533  * ata_acpi_push_id - send Identify data to drive
534  * @dev: target ATA device
535  *
536  * _SDD ACPI object: for SATA mode only
537  * Must be after Identify (Packet) Device -- uses its data
538  * ATM this function never returns a failure.  It is an optional
539  * method and if it fails for whatever reason, we should still
540  * just keep going.
541  *
542  * LOCKING:
543  * EH context.
544  *
545  * RETURNS:
546  * 0 on success, -errno on failure.
547  */
548 static int ata_acpi_push_id(struct ata_device *dev)
549 {
550         struct ata_port *ap = dev->link->ap;
551         int err;
552         acpi_status status;
553         struct acpi_object_list input;
554         union acpi_object in_params[1];
555
556         if (ata_msg_probe(ap))
557                 ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
558                                __FUNCTION__, dev->devno, ap->port_no);
559
560         /* Give the drive Identify data to the drive via the _SDD method */
561         /* _SDD: set up input parameters */
562         input.count = 1;
563         input.pointer = in_params;
564         in_params[0].type = ACPI_TYPE_BUFFER;
565         in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
566         in_params[0].buffer.pointer = (u8 *)dev->id;
567         /* Output buffer: _SDD has no output */
568
569         /* It's OK for _SDD to be missing too. */
570         swap_buf_le16(dev->id, ATA_ID_WORDS);
571         status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
572         swap_buf_le16(dev->id, ATA_ID_WORDS);
573
574         err = ACPI_FAILURE(status) ? -EIO : 0;
575         if (err < 0)
576                 ata_dev_printk(dev, KERN_WARNING,
577                                "ACPI _SDD failed (AE 0x%x)\n", status);
578
579         return err;
580 }
581
582 /**
583  * ata_acpi_on_suspend - ATA ACPI hook called on suspend
584  * @ap: target ATA port
585  *
586  * This function is called when @ap is about to be suspended.  All
587  * devices are already put to sleep but the port_suspend() callback
588  * hasn't been executed yet.  Error return from this function aborts
589  * suspend.
590  *
591  * LOCKING:
592  * EH context.
593  *
594  * RETURNS:
595  * 0 on success, -errno on failure.
596  */
597 int ata_acpi_on_suspend(struct ata_port *ap)
598 {
599         unsigned long flags;
600         int rc;
601
602         /* proceed iff per-port acpi_handle is valid */
603         if (!ap->acpi_handle)
604                 return 0;
605         BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
606
607         /* store timing parameters */
608         rc = ata_acpi_gtm(ap, &ap->acpi_gtm);
609
610         spin_lock_irqsave(ap->lock, flags);
611         if (rc == 0)
612                 ap->pflags |= ATA_PFLAG_GTM_VALID;
613         else
614                 ap->pflags &= ~ATA_PFLAG_GTM_VALID;
615         spin_unlock_irqrestore(ap->lock, flags);
616
617         if (rc == -ENOENT)
618                 rc = 0;
619         return rc;
620 }
621
622 /**
623  * ata_acpi_on_resume - ATA ACPI hook called on resume
624  * @ap: target ATA port
625  *
626  * This function is called when @ap is resumed - right after port
627  * itself is resumed but before any EH action is taken.
628  *
629  * LOCKING:
630  * EH context.
631  */
632 void ata_acpi_on_resume(struct ata_port *ap)
633 {
634         struct ata_device *dev;
635
636         if (ap->acpi_handle && (ap->pflags & ATA_PFLAG_GTM_VALID)) {
637                 BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
638
639                 /* restore timing parameters */
640                 ata_acpi_stm(ap, &ap->acpi_gtm);
641         }
642
643         /* schedule _GTF */
644         ata_link_for_each_dev(dev, &ap->link)
645                 dev->flags |= ATA_DFLAG_ACPI_PENDING;
646 }
647
648 /**
649  * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
650  * @dev: target ATA device
651  *
652  * This function is called when @dev is about to be configured.
653  * IDENTIFY data might have been modified after this hook is run.
654  *
655  * LOCKING:
656  * EH context.
657  *
658  * RETURNS:
659  * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
660  * -errno on failure.
661  */
662 int ata_acpi_on_devcfg(struct ata_device *dev)
663 {
664         struct ata_port *ap = dev->link->ap;
665         struct ata_eh_context *ehc = &ap->link.eh_context;
666         int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
667         int rc;
668
669         if (!dev->acpi_handle)
670                 return 0;
671
672         /* do we need to do _GTF? */
673         if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
674             !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
675                 return 0;
676
677         /* do _SDD if SATA */
678         if (acpi_sata) {
679                 rc = ata_acpi_push_id(dev);
680                 if (rc)
681                         goto acpi_err;
682         }
683
684         /* do _GTF */
685         rc = ata_acpi_exec_tfs(dev);
686         if (rc < 0)
687                 goto acpi_err;
688
689         dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
690
691         /* refresh IDENTIFY page if any _GTF command has been executed */
692         if (rc > 0) {
693                 rc = ata_dev_reread_id(dev, 0);
694                 if (rc < 0) {
695                         ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
696                                        "after ACPI commands\n");
697                         return rc;
698                 }
699         }
700
701         return 0;
702
703  acpi_err:
704         /* let EH retry on the first failure, disable ACPI on the second */
705         if (dev->flags & ATA_DFLAG_ACPI_FAILED) {
706                 ata_dev_printk(dev, KERN_WARNING, "ACPI on devcfg failed the "
707                                "second time, disabling (errno=%d)\n", rc);
708
709                 dev->acpi_handle = NULL;
710
711                 /* if port is working, request IDENTIFY reload and continue */
712                 if (!(ap->pflags & ATA_PFLAG_FROZEN))
713                         rc = 1;
714         }
715         dev->flags |= ATA_DFLAG_ACPI_FAILED;
716         return rc;
717 }