]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/ata/libata-acpi.c
libata-acpi: implement dev->gtf_cache and evaluate _GTF right after _STM during resume
[linux-2.6-omap-h63xx.git] / drivers / ata / libata-acpi.c
index 08a52dd45fb6a4bd686ee0ef05f1cc069f863713..e0dd132fd49071f598cd2e57f3b6c7f44fc69d64 100644 (file)
@@ -41,6 +41,12 @@ static int is_pci_dev(struct device *dev)
        return (dev->bus == &pci_bus_type);
 }
 
+static void ata_acpi_clear_gtf(struct ata_device *dev)
+{
+       kfree(dev->gtf_cache);
+       dev->gtf_cache = NULL;
+}
+
 /**
  * ata_acpi_associate_sata_port - associate SATA port with ACPI objects
  * @ap: target SATA port
@@ -94,6 +100,9 @@ static void ata_acpi_associate_ide_port(struct ata_port *ap)
 
                dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
        }
+
+       if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
+               ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
 }
 
 static void ata_acpi_handle_hotplug(struct ata_port *ap, struct kobject *kobj,
@@ -187,6 +196,32 @@ void ata_acpi_associate(struct ata_host *host)
        }
 }
 
+/**
+ * ata_acpi_dissociate - dissociate ATA host from ACPI objects
+ * @host: target ATA host
+ *
+ * This function is called during driver detach after the whole host
+ * is shut down.
+ *
+ * LOCKING:
+ * EH context.
+ */
+void ata_acpi_dissociate(struct ata_host *host)
+{
+       int i;
+
+       /* Restore initial _GTM values so that driver which attaches
+        * afterward can use them too.
+        */
+       for (i = 0; i < host->n_ports; i++) {
+               struct ata_port *ap = host->ports[i];
+               const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
+
+               if (ap->acpi_handle && gtm)
+                       ata_acpi_stm(ap, gtm);
+       }
+}
+
 /**
  * ata_acpi_gtm - execute _GTM
  * @ap: target ATA port
@@ -200,7 +235,7 @@ void ata_acpi_associate(struct ata_host *host)
  * 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)
+int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
 {
        struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
        union acpi_object *out_obj;
@@ -259,15 +294,16 @@ EXPORT_SYMBOL_GPL(ata_acpi_gtm);
  * 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)
+int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
 {
        acpi_status status;
+       struct ata_acpi_gtm             stm_buf = *stm;
        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;
+       in_params[0].buffer.pointer = (u8 *)&stm_buf;
        /* Buffers for id may need byteswapping ? */
        in_params[1].type = ACPI_TYPE_BUFFER;
        in_params[1].buffer.length = 512;
@@ -297,7 +333,6 @@ EXPORT_SYMBOL_GPL(ata_acpi_stm);
  * ata_dev_get_GTF - get the drive bootup default taskfile settings
  * @dev: target ATA device
  * @gtf: output parameter for buffer containing _GTF taskfile arrays
- * @ptr_to_free: pointer which should be freed
  *
  * This applies to both PATA and SATA drives.
  *
@@ -312,10 +347,9 @@ EXPORT_SYMBOL_GPL(ata_acpi_stm);
  *
  * RETURNS:
  * Number of taskfiles on success, 0 if _GTF doesn't exist or doesn't
- * contain valid data.  -errno on other errors.
+ * contain valid data.
  */
-static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
-                          void **ptr_to_free)
+static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
 {
        struct ata_port *ap = dev->link->ap;
        acpi_status status;
@@ -323,6 +357,12 @@ static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
        union acpi_object *out_obj;
        int rc = 0;
 
+       /* if _GTF is cached, use the cached value */
+       if (dev->gtf_cache) {
+               out_obj = dev->gtf_cache;
+               goto done;
+       }
+
        /* set up output buffer */
        output.length = ACPI_ALLOCATE_BUFFER;
        output.pointer = NULL;  /* ACPI-CA sets this; save/free it later */
@@ -333,13 +373,13 @@ static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
 
        /* _GTF has no input parameters */
        status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
+       out_obj = dev->gtf_cache = output.pointer;
 
        if (ACPI_FAILURE(status)) {
                if (status != AE_NOT_FOUND) {
                        ata_dev_printk(dev, KERN_WARNING,
                                       "_GTF evaluation failed (AE 0x%x)\n",
                                       status);
-                       rc = -EIO;
                }
                goto out_free;
        }
@@ -354,12 +394,10 @@ static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
                goto out_free;
        }
 
-       out_obj = output.pointer;
        if (out_obj->type != ACPI_TYPE_BUFFER) {
                ata_dev_printk(dev, KERN_WARNING,
                               "_GTF unexpected object type 0x%x\n",
                               out_obj->type);
-               rc = -EINVAL;
                goto out_free;
        }
 
@@ -367,22 +405,22 @@ static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
                ata_dev_printk(dev, KERN_WARNING,
                               "unexpected _GTF length (%d)\n",
                               out_obj->buffer.length);
-               rc = -EINVAL;
                goto out_free;
        }
 
-       *ptr_to_free = out_obj;
-       *gtf = (void *)out_obj->buffer.pointer;
+ done:
        rc = out_obj->buffer.length / REGS_PER_GTF;
-
-       if (ata_msg_probe(ap))
-               ata_dev_printk(dev, KERN_DEBUG, "%s: returning "
-                       "gtf=%p, gtf_count=%d, ptr_to_free=%p\n",
-                       __FUNCTION__, *gtf, rc, *ptr_to_free);
+       if (gtf) {
+               *gtf = (void *)out_obj->buffer.pointer;
+               if (ata_msg_probe(ap))
+                       ata_dev_printk(dev, KERN_DEBUG,
+                                      "%s: returning gtf=%p, gtf_count=%d\n",
+                                      __FUNCTION__, *gtf, rc);
+       }
        return rc;
 
  out_free:
-       kfree(output.pointer);
+       ata_acpi_clear_gtf(dev);
        return rc;
 }
 
@@ -396,22 +434,21 @@ static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
 
 int ata_acpi_cbl_80wire(struct ata_port *ap)
 {
-       struct ata_acpi_gtm gtm;
+       const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
        int valid = 0;
 
-       /* No _GTM data, no information */
-       if (ata_acpi_gtm(ap, &gtm) < 0)
+       if (!gtm)
                return 0;
 
        /* Split timing, DMA enabled */
-       if ((gtm.flags & 0x11) == 0x11 && gtm.drive[0].dma < 55)
+       if ((gtm->flags & 0x11) == 0x11 && gtm->drive[0].dma < 55)
                valid |= 1;
-       if ((gtm.flags & 0x14) == 0x14 && gtm.drive[1].dma < 55)
+       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)
+       if ((gtm->flags & 0x11) == 0x01 && gtm->drive[0].dma < 55)
                valid |= 1;
-       if ((gtm.flags & 0x14) == 0x04 && gtm.drive[0].dma < 55)
+       if ((gtm->flags & 0x14) == 0x04 && gtm->drive[0].dma < 55)
                valid |= 2;
 
        /* Drive check */
@@ -507,14 +544,10 @@ static int taskfile_load_raw(struct ata_device *dev,
 static int ata_acpi_exec_tfs(struct ata_device *dev)
 {
        struct ata_acpi_gtf *gtf = NULL;
-       void *ptr_to_free = NULL;
        int gtf_count, i, rc;
 
        /* get taskfiles */
-       rc = ata_dev_get_GTF(dev, &gtf, &ptr_to_free);
-       if (rc < 0)
-               return rc;
-       gtf_count = rc;
+       gtf_count = ata_dev_get_GTF(dev, &gtf);
 
        /* execute them */
        for (i = 0, rc = 0; i < gtf_count; i++) {
@@ -528,7 +561,7 @@ static int ata_acpi_exec_tfs(struct ata_device *dev)
                        rc = tmp;
        }
 
-       kfree(ptr_to_free);
+       ata_acpi_clear_gtf(dev);
 
        if (rc == 0)
                return gtf_count;
@@ -602,27 +635,8 @@ static int ata_acpi_push_id(struct ata_device *dev)
  */
 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;
+       /* nada */
+       return 0;
 }
 
 /**
@@ -637,18 +651,34 @@ int ata_acpi_on_suspend(struct ata_port *ap)
  */
 void ata_acpi_on_resume(struct ata_port *ap)
 {
+       const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
        struct ata_device *dev;
 
-       if (ap->acpi_handle && (ap->pflags & ATA_PFLAG_GTM_VALID)) {
-               BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
+       if (ap->acpi_handle && gtm) {
+               /* _GTM valid */
 
                /* restore timing parameters */
-               ata_acpi_stm(ap, &ap->acpi_gtm);
-       }
+               ata_acpi_stm(ap, gtm);
 
-       /* schedule _GTF */
-       ata_link_for_each_dev(dev, &ap->link)
-               dev->flags |= ATA_DFLAG_ACPI_PENDING;
+               /* _GTF should immediately follow _STM so that it can
+                * use values set by _STM.  Cache _GTF result and
+                * schedule _GTF.
+                */
+               ata_link_for_each_dev(dev, &ap->link) {
+                       ata_acpi_clear_gtf(dev);
+                       if (ata_dev_get_GTF(dev, NULL) >= 0)
+                               dev->flags |= ATA_DFLAG_ACPI_PENDING;
+               }
+       } else {
+               /* SATA _GTF needs to be evaulated after _SDD and
+                * there's no reason to evaluate IDE _GTF early
+                * without _STM.  Clear cache and schedule _GTF.
+                */
+               ata_link_for_each_dev(dev, &ap->link) {
+                       ata_acpi_clear_gtf(dev);
+                       dev->flags |= ATA_DFLAG_ACPI_PENDING;
+               }
+       }
 }
 
 /**
@@ -721,3 +751,17 @@ int ata_acpi_on_devcfg(struct ata_device *dev)
        dev->flags |= ATA_DFLAG_ACPI_FAILED;
        return rc;
 }
+
+/**
+ * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
+ * @dev: target ATA device
+ *
+ * This function is called when @dev is about to be disabled.
+ *
+ * LOCKING:
+ * EH context.
+ */
+void ata_acpi_on_disable(struct ata_device *dev)
+{
+       ata_acpi_clear_gtf(dev);
+}