3 * Provides ACPI support for PATA/SATA.
5 * Copyright (C) 2006 Intel Corp.
6 * Copyright (C) 2006 Randy Dunlap
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>
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>
28 #define NO_PORT_MULT 0xffff
29 #define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
31 #define REGS_PER_GTF 7
33 u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
37 * Helper - belongs in the PCI layer somewhere eventually
39 static int is_pci_dev(struct device *dev)
41 return (dev->bus == &pci_bus_type);
44 static void ata_acpi_clear_gtf(struct ata_device *dev)
46 kfree(dev->gtf_cache);
47 dev->gtf_cache = NULL;
51 * ata_acpi_associate_sata_port - associate SATA port with ACPI objects
52 * @ap: target SATA port
54 * Look up ACPI objects associated with @ap and initialize acpi_handle
55 * fields of @ap, the port and devices accordingly.
61 * 0 on success, -errno on failure.
63 void ata_acpi_associate_sata_port(struct ata_port *ap)
65 WARN_ON(!(ap->flags & ATA_FLAG_ACPI_SATA));
67 if (!ap->nr_pmp_links) {
68 acpi_integer adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
70 ap->link.device->acpi_handle =
71 acpi_get_child(ap->host->acpi_handle, adr);
73 struct ata_link *link;
75 ap->link.device->acpi_handle = NULL;
77 ata_port_for_each_link(link, ap) {
78 acpi_integer adr = SATA_ADR(ap->port_no, link->pmp);
80 link->device->acpi_handle =
81 acpi_get_child(ap->host->acpi_handle, adr);
86 static void ata_acpi_associate_ide_port(struct ata_port *ap)
90 ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no);
95 if (ap->flags & ATA_FLAG_SLAVE_POSS)
98 for (i = 0; i < max_devices; i++) {
99 struct ata_device *dev = &ap->link.device[i];
101 dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
104 if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
105 ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
108 static void ata_acpi_handle_hotplug(struct ata_port *ap, struct kobject *kobj,
111 char event_string[12];
112 char *envp[] = { event_string, NULL };
113 struct ata_eh_info *ehi = &ap->link.eh_info;
115 if (event == 0 || event == 1) {
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);
122 spin_unlock_irqrestore(ap->lock, flags);
126 sprintf(event_string, "BAY_EVENT=%d", event);
127 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
131 static void ata_acpi_dev_notify(acpi_handle handle, u32 event, void *data)
133 struct ata_device *dev = data;
134 struct kobject *kobj = NULL;
137 kobj = &dev->sdev->sdev_gendev.kobj;
139 ata_acpi_handle_hotplug(dev->link->ap, kobj, event);
142 static void ata_acpi_ap_notify(acpi_handle handle, u32 event, void *data)
144 struct ata_port *ap = data;
146 ata_acpi_handle_hotplug(ap, &ap->dev->kobj, event);
150 * ata_acpi_associate - associate ATA host with ACPI objects
151 * @host: target ATA host
153 * Look up ACPI objects associated with @host and initialize
154 * acpi_handle fields of @host, its ports and devices accordingly.
160 * 0 on success, -errno on failure.
162 void ata_acpi_associate(struct ata_host *host)
166 if (!is_pci_dev(host->dev) || libata_noacpi)
169 host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev);
170 if (!host->acpi_handle)
173 for (i = 0; i < host->n_ports; i++) {
174 struct ata_port *ap = host->ports[i];
176 if (host->ports[0]->flags & ATA_FLAG_ACPI_SATA)
177 ata_acpi_associate_sata_port(ap);
179 ata_acpi_associate_ide_port(ap);
182 acpi_install_notify_handler (ap->acpi_handle,
187 for (j = 0; j < ata_link_max_devices(&ap->link); j++) {
188 struct ata_device *dev = &ap->link.device[j];
190 if (dev->acpi_handle)
191 acpi_install_notify_handler (dev->acpi_handle,
200 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
201 * @host: target ATA host
203 * This function is called during driver detach after the whole host
209 void ata_acpi_dissociate(struct ata_host *host)
213 /* Restore initial _GTM values so that driver which attaches
214 * afterward can use them too.
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);
220 if (ap->acpi_handle && gtm)
221 ata_acpi_stm(ap, gtm);
226 * ata_acpi_gtm - execute _GTM
227 * @ap: target ATA port
228 * @gtm: out parameter for _GTM result
230 * Evaluate _GTM and store the result in @gtm.
236 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
238 int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
240 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
241 union acpi_object *out_obj;
245 status = acpi_evaluate_object(ap->acpi_handle, "_GTM", NULL, &output);
248 if (status == AE_NOT_FOUND)
252 if (ACPI_FAILURE(status)) {
253 ata_port_printk(ap, KERN_ERR,
254 "ACPI get timing mode failed (AE 0x%x)\n",
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",
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);
275 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
278 kfree(output.pointer);
282 EXPORT_SYMBOL_GPL(ata_acpi_gtm);
285 * ata_acpi_stm - execute _STM
286 * @ap: target ATA port
287 * @stm: timing parameter to _STM
289 * Evaluate _STM with timing parameter @stm.
295 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
297 int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
300 struct ata_acpi_gtm stm_buf = *stm;
301 struct acpi_object_list input;
302 union acpi_object in_params[3];
304 in_params[0].type = ACPI_TYPE_BUFFER;
305 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
306 in_params[0].buffer.pointer = (u8 *)&stm_buf;
307 /* Buffers for id may need byteswapping ? */
308 in_params[1].type = ACPI_TYPE_BUFFER;
309 in_params[1].buffer.length = 512;
310 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
311 in_params[2].type = ACPI_TYPE_BUFFER;
312 in_params[2].buffer.length = 512;
313 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
316 input.pointer = in_params;
318 status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL);
320 if (status == AE_NOT_FOUND)
322 if (ACPI_FAILURE(status)) {
323 ata_port_printk(ap, KERN_ERR,
324 "ACPI set timing mode failed (status=0x%x)\n", status);
330 EXPORT_SYMBOL_GPL(ata_acpi_stm);
333 * ata_dev_get_GTF - get the drive bootup default taskfile settings
334 * @dev: target ATA device
335 * @gtf: output parameter for buffer containing _GTF taskfile arrays
337 * This applies to both PATA and SATA drives.
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.
349 * Number of taskfiles on success, 0 if _GTF doesn't exist or doesn't
350 * contain valid data.
352 static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
354 struct ata_port *ap = dev->link->ap;
356 struct acpi_buffer output;
357 union acpi_object *out_obj;
360 /* if _GTF is cached, use the cached value */
361 if (dev->gtf_cache) {
362 out_obj = dev->gtf_cache;
366 /* set up output buffer */
367 output.length = ACPI_ALLOCATE_BUFFER;
368 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
370 if (ata_msg_probe(ap))
371 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
372 __FUNCTION__, ap->port_no);
374 /* _GTF has no input parameters */
375 status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
376 out_obj = dev->gtf_cache = output.pointer;
378 if (ACPI_FAILURE(status)) {
379 if (status != AE_NOT_FOUND) {
380 ata_dev_printk(dev, KERN_WARNING,
381 "_GTF evaluation failed (AE 0x%x)\n",
387 if (!output.length || !output.pointer) {
388 if (ata_msg_probe(ap))
389 ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
390 "length or ptr is NULL (0x%llx, 0x%p)\n",
392 (unsigned long long)output.length,
397 if (out_obj->type != ACPI_TYPE_BUFFER) {
398 ata_dev_printk(dev, KERN_WARNING,
399 "_GTF unexpected object type 0x%x\n",
404 if (out_obj->buffer.length % REGS_PER_GTF) {
405 ata_dev_printk(dev, KERN_WARNING,
406 "unexpected _GTF length (%d)\n",
407 out_obj->buffer.length);
412 rc = out_obj->buffer.length / REGS_PER_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);
423 ata_acpi_clear_gtf(dev);
428 * ata_acpi_cbl_80wire - Check for 80 wire cable
431 * Return 1 if the ACPI mode data for this port indicates the BIOS selected
435 int ata_acpi_cbl_80wire(struct ata_port *ap)
437 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
443 /* Split timing, DMA enabled */
444 if ((gtm->flags & 0x11) == 0x11 && gtm->drive[0].dma < 55)
446 if ((gtm->flags & 0x14) == 0x14 && gtm->drive[1].dma < 55)
448 /* Shared timing, DMA enabled */
449 if ((gtm->flags & 0x11) == 0x01 && gtm->drive[0].dma < 55)
451 if ((gtm->flags & 0x14) == 0x04 && gtm->drive[0].dma < 55)
455 if ((valid & 1) && ata_dev_enabled(&ap->link.device[0]))
457 if ((valid & 2) && ata_dev_enabled(&ap->link.device[1]))
462 EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
465 * taskfile_load_raw - send taskfile registers to host controller
466 * @dev: target ATA device
467 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
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.
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.
484 * 0 on success, -errno on failure.
486 static int taskfile_load_raw(struct ata_device *dev,
487 const struct ata_acpi_gtf *gtf)
489 struct ata_port *ap = dev->link->ap;
490 struct ata_taskfile tf, rtf;
491 unsigned int err_mask;
493 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))
498 ata_tf_init(dev, &tf);
500 /* convert gtf to tf */
501 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
502 tf.protocol = ATA_PROT_NODATA;
503 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 */
511 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);
518 err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0, 0);
520 ata_dev_printk(dev, KERN_ERR,
521 "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);
532 * ata_acpi_exec_tfs - get then write drive taskfile settings
533 * @dev: target ATA device
535 * Evaluate _GTF and excute returned taskfiles.
541 * Number of executed taskfiles on success, 0 if _GTF doesn't exist or
542 * doesn't contain valid data. -errno on other errors.
544 static int ata_acpi_exec_tfs(struct ata_device *dev)
546 struct ata_acpi_gtf *gtf = NULL;
547 int gtf_count, i, rc;
550 gtf_count = ata_dev_get_GTF(dev, >f);
553 for (i = 0, rc = 0; i < gtf_count; i++) {
556 /* ACPI errors are eventually ignored. Run till the
557 * end even after errors.
559 tmp = taskfile_load_raw(dev, gtf++);
564 ata_acpi_clear_gtf(dev);
572 * ata_acpi_push_id - send Identify data to drive
573 * @dev: target ATA device
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
585 * 0 on success, -errno on failure.
587 static int ata_acpi_push_id(struct ata_device *dev)
589 struct ata_port *ap = dev->link->ap;
592 struct acpi_object_list input;
593 union acpi_object in_params[1];
595 if (ata_msg_probe(ap))
596 ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
597 __FUNCTION__, dev->devno, ap->port_no);
599 /* Give the drive Identify data to the drive via the _SDD method */
600 /* _SDD: set up input parameters */
602 input.pointer = in_params;
603 in_params[0].type = ACPI_TYPE_BUFFER;
604 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
605 in_params[0].buffer.pointer = (u8 *)dev->id;
606 /* Output buffer: _SDD has no output */
608 /* It's OK for _SDD to be missing too. */
609 swap_buf_le16(dev->id, ATA_ID_WORDS);
610 status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
611 swap_buf_le16(dev->id, ATA_ID_WORDS);
613 err = ACPI_FAILURE(status) ? -EIO : 0;
615 ata_dev_printk(dev, KERN_WARNING,
616 "ACPI _SDD failed (AE 0x%x)\n", status);
622 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
623 * @ap: target ATA port
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
634 * 0 on success, -errno on failure.
636 int ata_acpi_on_suspend(struct ata_port *ap)
643 * ata_acpi_on_resume - ATA ACPI hook called on resume
644 * @ap: target ATA port
646 * This function is called when @ap is resumed - right after port
647 * itself is resumed but before any EH action is taken.
652 void ata_acpi_on_resume(struct ata_port *ap)
654 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
655 struct ata_device *dev;
657 if (ap->acpi_handle && gtm) {
660 /* restore timing parameters */
661 ata_acpi_stm(ap, gtm);
663 /* _GTF should immediately follow _STM so that it can
664 * use values set by _STM. Cache _GTF result and
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;
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.
677 ata_link_for_each_dev(dev, &ap->link) {
678 ata_acpi_clear_gtf(dev);
679 dev->flags |= ATA_DFLAG_ACPI_PENDING;
685 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
686 * @dev: target ATA device
688 * This function is called when @dev is about to be configured.
689 * IDENTIFY data might have been modified after this hook is run.
695 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
698 int ata_acpi_on_devcfg(struct ata_device *dev)
700 struct ata_port *ap = dev->link->ap;
701 struct ata_eh_context *ehc = &ap->link.eh_context;
702 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
705 if (!dev->acpi_handle)
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)))
713 /* do _SDD if SATA */
715 rc = ata_acpi_push_id(dev);
721 rc = ata_acpi_exec_tfs(dev);
725 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
727 /* refresh IDENTIFY page if any _GTF command has been executed */
729 rc = ata_dev_reread_id(dev, 0);
731 ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
732 "after ACPI commands\n");
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);
745 dev->acpi_handle = NULL;
747 /* if port is working, request IDENTIFY reload and continue */
748 if (!(ap->pflags & ATA_PFLAG_FROZEN))
751 dev->flags |= ATA_DFLAG_ACPI_FAILED;
756 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
757 * @dev: target ATA device
759 * This function is called when @dev is about to be disabled.
764 void ata_acpi_on_disable(struct ata_device *dev)
766 ata_acpi_clear_gtf(dev);