]> err.no Git - linux-2.6/blobdiff - drivers/ata/libata-acpi.c
Merge branch 'fixes-davem' of master.kernel.org:/pub/scm/linux/kernel/git/linville...
[linux-2.6] / drivers / ata / libata-acpi.c
index 78db2e67d7c7c591d6a245262546c5ec5540e2b3..3f7533589041b7bd9f11b77cb6d875d864f50add 100644 (file)
@@ -14,6 +14,7 @@
 #include <linux/acpi.h>
 #include <linux/libata.h>
 #include <linux/pci.h>
+#include <scsi/scsi_device.h>
 #include "libata.h"
 
 #include <acpi/acpi_bus.h>
@@ -40,11 +41,40 @@ static int is_pci_dev(struct device *dev)
        return (dev->bus == &pci_bus_type);
 }
 
-static void ata_acpi_associate_sata_port(struct ata_port *ap)
+/**
+ * ata_acpi_associate_sata_port - associate SATA port with ACPI objects
+ * @ap: target SATA port
+ *
+ * Look up ACPI objects associated with @ap and initialize acpi_handle
+ * fields of @ap, the port and devices accordingly.
+ *
+ * LOCKING:
+ * EH context.
+ *
+ * RETURNS:
+ * 0 on success, -errno on failure.
+ */
+void ata_acpi_associate_sata_port(struct ata_port *ap)
 {
-       acpi_integer adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
+       WARN_ON(!(ap->flags & ATA_FLAG_ACPI_SATA));
 
-       ap->device->acpi_handle = acpi_get_child(ap->host->acpi_handle, adr);
+       if (!ap->nr_pmp_links) {
+               acpi_integer adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
+
+               ap->link.device->acpi_handle =
+                       acpi_get_child(ap->host->acpi_handle, adr);
+       } else {
+               struct ata_link *link;
+
+               ap->link.device->acpi_handle = NULL;
+
+               ata_port_for_each_link(link, ap) {
+                       acpi_integer adr = SATA_ADR(ap->port_no, link->pmp);
+
+                       link->device->acpi_handle =
+                               acpi_get_child(ap->host->acpi_handle, adr);
+               }
+       }
 }
 
 static void ata_acpi_associate_ide_port(struct ata_port *ap)
@@ -60,12 +90,53 @@ static void ata_acpi_associate_ide_port(struct ata_port *ap)
                max_devices++;
 
        for (i = 0; i < max_devices; i++) {
-               struct ata_device *dev = &ap->device[i];
+               struct ata_device *dev = &ap->link.device[i];
 
                dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
        }
 }
 
+static void ata_acpi_handle_hotplug (struct ata_port *ap, struct kobject *kobj,
+                                    u32 event)
+{
+       char event_string[12];
+       char *envp[] = { event_string, NULL };
+       struct ata_eh_info *ehi = &ap->link.eh_info;
+
+       if (event == 0 || event == 1) {
+              unsigned long flags;
+              spin_lock_irqsave(ap->lock, flags);
+              ata_ehi_clear_desc(ehi);
+              ata_ehi_push_desc(ehi, "ACPI event");
+              ata_ehi_hotplugged(ehi);
+              ata_port_freeze(ap);
+              spin_unlock_irqrestore(ap->lock, flags);
+       }
+
+       if (kobj) {
+               sprintf(event_string, "BAY_EVENT=%d", event);
+               kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
+       }
+}
+
+static void ata_acpi_dev_notify(acpi_handle handle, u32 event, void *data)
+{
+       struct ata_device *dev = data;
+       struct kobject *kobj = NULL;
+
+       if (dev->sdev)
+               kobj = &dev->sdev->sdev_gendev.kobj;
+
+       ata_acpi_handle_hotplug (dev->link->ap, kobj, event);
+}
+
+static void ata_acpi_ap_notify(acpi_handle handle, u32 event, void *data)
+{
+       struct ata_port *ap = data;
+
+       ata_acpi_handle_hotplug (ap, &ap->dev->kobj, event);
+}
+
 /**
  * ata_acpi_associate - associate ATA host with ACPI objects
  * @host: target ATA host
@@ -81,7 +152,7 @@ static void ata_acpi_associate_ide_port(struct ata_port *ap)
  */
 void ata_acpi_associate(struct ata_host *host)
 {
-       int i;
+       int i, j;
 
        if (!is_pci_dev(host->dev) || libata_noacpi)
                return;
@@ -97,9 +168,131 @@ void ata_acpi_associate(struct ata_host *host)
                        ata_acpi_associate_sata_port(ap);
                else
                        ata_acpi_associate_ide_port(ap);
+
+               if (ap->acpi_handle)
+                       acpi_install_notify_handler (ap->acpi_handle,
+                                                    ACPI_SYSTEM_NOTIFY,
+                                                    ata_acpi_ap_notify,
+                                                    ap);
+
+               for (j = 0; j < ata_link_max_devices(&ap->link); j++) {
+                       struct ata_device *dev = &ap->link.device[j];
+
+                       if (dev->acpi_handle)
+                               acpi_install_notify_handler (dev->acpi_handle,
+                                                            ACPI_SYSTEM_NOTIFY,
+                                                            ata_acpi_dev_notify,
+                                                            dev);
+               }
        }
 }
 
+/**
+ * ata_acpi_gtm - execute _GTM
+ * @ap: target ATA port
+ * @gtm: out parameter for _GTM result
+ *
+ * Evaluate _GTM and store the result in @gtm.
+ *
+ * LOCKING:
+ * EH context.
+ *
+ * RETURNS:
+ * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
+ */
+int ata_acpi_gtm(const struct ata_port *ap, struct ata_acpi_gtm *gtm)
+{
+       struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
+       union acpi_object *out_obj;
+       acpi_status status;
+       int rc = 0;
+
+       status = acpi_evaluate_object(ap->acpi_handle, "_GTM", NULL, &output);
+
+       rc = -ENOENT;
+       if (status == AE_NOT_FOUND)
+               goto out_free;
+
+       rc = -EINVAL;
+       if (ACPI_FAILURE(status)) {
+               ata_port_printk(ap, KERN_ERR,
+                               "ACPI get timing mode failed (AE 0x%x)\n",
+                               status);
+               goto out_free;
+       }
+
+       out_obj = output.pointer;
+       if (out_obj->type != ACPI_TYPE_BUFFER) {
+               ata_port_printk(ap, KERN_WARNING,
+                               "_GTM returned unexpected object type 0x%x\n",
+                               out_obj->type);
+
+               goto out_free;
+       }
+
+       if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
+               ata_port_printk(ap, KERN_ERR,
+                               "_GTM returned invalid length %d\n",
+                               out_obj->buffer.length);
+               goto out_free;
+       }
+
+       memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
+       rc = 0;
+ out_free:
+       kfree(output.pointer);
+       return rc;
+}
+
+EXPORT_SYMBOL_GPL(ata_acpi_gtm);
+
+/**
+ * ata_acpi_stm - execute _STM
+ * @ap: target ATA port
+ * @stm: timing parameter to _STM
+ *
+ * Evaluate _STM with timing parameter @stm.
+ *
+ * LOCKING:
+ * EH context.
+ *
+ * RETURNS:
+ * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
+ */
+int ata_acpi_stm(const struct ata_port *ap, struct ata_acpi_gtm *stm)
+{
+       acpi_status status;
+       struct acpi_object_list         input;
+       union acpi_object               in_params[3];
+
+       in_params[0].type = ACPI_TYPE_BUFFER;
+       in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
+       in_params[0].buffer.pointer = (u8 *)stm;
+       /* Buffers for id may need byteswapping ? */
+       in_params[1].type = ACPI_TYPE_BUFFER;
+       in_params[1].buffer.length = 512;
+       in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
+       in_params[2].type = ACPI_TYPE_BUFFER;
+       in_params[2].buffer.length = 512;
+       in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
+
+       input.count = 3;
+       input.pointer = in_params;
+
+       status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL);
+
+       if (status == AE_NOT_FOUND)
+               return -ENOENT;
+       if (ACPI_FAILURE(status)) {
+               ata_port_printk(ap, KERN_ERR,
+                       "ACPI set timing mode failed (status=0x%x)\n", status);
+               return -EINVAL;
+       }
+       return 0;
+}
+
+EXPORT_SYMBOL_GPL(ata_acpi_stm);
+
 /**
  * ata_dev_get_GTF - get the drive bootup default taskfile settings
  * @dev: target ATA device
@@ -124,7 +317,7 @@ void ata_acpi_associate(struct ata_host *host)
 static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
                           void **ptr_to_free)
 {
-       struct ata_port *ap = dev->ap;
+       struct ata_port *ap = dev->link->ap;
        acpi_status status;
        struct acpi_buffer output;
        union acpi_object *out_obj;
@@ -193,6 +386,44 @@ static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
        return rc;
 }
 
+/**
+ * ata_acpi_cbl_80wire         -       Check for 80 wire cable
+ * @ap: Port to check
+ *
+ * Return 1 if the ACPI mode data for this port indicates the BIOS selected
+ * an 80wire mode.
+ */
+
+int ata_acpi_cbl_80wire(struct ata_port *ap)
+{
+       struct ata_acpi_gtm gtm;
+       int valid = 0;
+       
+       /* No _GTM data, no information */
+       if (ata_acpi_gtm(ap, &gtm) < 0)
+               return 0;
+               
+       /* Split timing, DMA enabled */
+       if ((gtm.flags & 0x11) == 0x11 && gtm.drive[0].dma < 55)
+               valid |= 1;
+       if ((gtm.flags & 0x14) == 0x14 && gtm.drive[1].dma < 55)
+               valid |= 2;
+       /* Shared timing, DMA enabled */
+       if ((gtm.flags & 0x11) == 0x01 && gtm.drive[0].dma < 55)
+               valid |= 1;
+       if ((gtm.flags & 0x14) == 0x04 && gtm.drive[0].dma < 55)
+               valid |= 2;
+
+       /* Drive check */
+       if ((valid & 1) && ata_dev_enabled(&ap->link.device[0]))
+               return 1;
+       if ((valid & 2) && ata_dev_enabled(&ap->link.device[1]))
+               return 1;
+       return 0;
+}
+
+EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
+
 /**
  * taskfile_load_raw - send taskfile registers to host controller
  * @dev: target ATA device
@@ -218,7 +449,7 @@ static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
 static int taskfile_load_raw(struct ata_device *dev,
                              const struct ata_acpi_gtf *gtf)
 {
-       struct ata_port *ap = dev->ap;
+       struct ata_port *ap = dev->link->ap;
        struct ata_taskfile tf, rtf;
        unsigned int err_mask;
 
@@ -247,7 +478,7 @@ static int taskfile_load_raw(struct ata_device *dev,
                               tf.lbal, tf.lbam, tf.lbah, tf.device);
 
        rtf = tf;
-       err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0);
+       err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0, 0);
        if (err_mask) {
                ata_dev_printk(dev, KERN_ERR,
                        "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x failed "
@@ -322,7 +553,7 @@ static int ata_acpi_exec_tfs(struct ata_device *dev)
  */
 static int ata_acpi_push_id(struct ata_device *dev)
 {
-       struct ata_port *ap = dev->ap;
+       struct ata_port *ap = dev->link->ap;
        int err;
        acpi_status status;
        struct acpi_object_list input;
@@ -354,6 +585,46 @@ static int ata_acpi_push_id(struct ata_device *dev)
        return err;
 }
 
+/**
+ * ata_acpi_on_suspend - ATA ACPI hook called on suspend
+ * @ap: target ATA port
+ *
+ * This function is called when @ap is about to be suspended.  All
+ * devices are already put to sleep but the port_suspend() callback
+ * hasn't been executed yet.  Error return from this function aborts
+ * suspend.
+ *
+ * LOCKING:
+ * EH context.
+ *
+ * RETURNS:
+ * 0 on success, -errno on failure.
+ */
+int ata_acpi_on_suspend(struct ata_port *ap)
+{
+       unsigned long flags;
+       int rc;
+
+       /* proceed iff per-port acpi_handle is valid */
+       if (!ap->acpi_handle)
+               return 0;
+       BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
+
+       /* store timing parameters */
+       rc = ata_acpi_gtm(ap, &ap->acpi_gtm);
+
+       spin_lock_irqsave(ap->lock, flags);
+       if (rc == 0)
+               ap->pflags |= ATA_PFLAG_GTM_VALID;
+       else
+               ap->pflags &= ~ATA_PFLAG_GTM_VALID;
+       spin_unlock_irqrestore(ap->lock, flags);
+
+       if (rc == -ENOENT)
+               rc = 0;
+       return rc;
+}
+
 /**
  * ata_acpi_on_resume - ATA ACPI hook called on resume
  * @ap: target ATA port
@@ -366,11 +637,18 @@ static int ata_acpi_push_id(struct ata_device *dev)
  */
 void ata_acpi_on_resume(struct ata_port *ap)
 {
-       int i;
+       struct ata_device *dev;
+
+       if (ap->acpi_handle && (ap->pflags & ATA_PFLAG_GTM_VALID)) {
+               BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
+
+               /* restore timing parameters */
+               ata_acpi_stm(ap, &ap->acpi_gtm);
+       }
 
        /* schedule _GTF */
-       for (i = 0; i < ATA_MAX_DEVICES; i++)
-               ap->device[i].flags |= ATA_DFLAG_ACPI_PENDING;
+       ata_link_for_each_dev(dev, &ap->link)
+               dev->flags |= ATA_DFLAG_ACPI_PENDING;
 }
 
 /**
@@ -389,15 +667,11 @@ void ata_acpi_on_resume(struct ata_port *ap)
  */
 int ata_acpi_on_devcfg(struct ata_device *dev)
 {
-       struct ata_port *ap = dev->ap;
-       struct ata_eh_context *ehc = &ap->eh_context;
+       struct ata_port *ap = dev->link->ap;
+       struct ata_eh_context *ehc = &ap->link.eh_context;
        int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
        int rc;
 
-       /* XXX: _STM isn't implemented yet, skip if IDE for now */
-       if (!acpi_sata)
-               return 0;
-
        if (!dev->acpi_handle)
                return 0;