Merge branch 'for-linus' of git://oss.sgi.com:8090/xfs/xfs-2.6
[sfrench/cifs-2.6.git] / drivers / ata / libata-acpi.c
index d4cb557a727db258766cadc9c85fd3e109218e87..9e8ec19260afa71ae5426b943fb4c17db6a6da19 100644 (file)
@@ -6,6 +6,7 @@
  * Copyright (C) 2006 Randy Dunlap
  */
 
+#include <linux/module.h>
 #include <linux/ata.h>
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <acpi/acmacros.h>
 #include <acpi/actypes.h>
 
+enum {
+       ATA_ACPI_FILTER_SETXFER = 1 << 0,
+       ATA_ACPI_FILTER_LOCK    = 1 << 1,
+
+       ATA_ACPI_FILTER_DEFAULT = ATA_ACPI_FILTER_SETXFER |
+                                 ATA_ACPI_FILTER_LOCK,
+};
+
+static unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT;
+module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644);
+MODULE_PARM_DESC(acpi_gtf_filter, "filter mask for ACPI _GTF commands, set to filter out (0x1=set xfermode, 0x2=lock/freeze lock)");
+
 #define NO_PORT_MULT           0xffff
 #define SATA_ADR(root, pmp)    (((root) << 16) | (pmp))
 
@@ -428,43 +441,134 @@ static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
        return rc;
 }
 
+/**
+ * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
+ * @dev: target device
+ * @gtm: GTM parameter to use
+ *
+ * Determine xfermask for @dev from @gtm.
+ *
+ * LOCKING:
+ * None.
+ *
+ * RETURNS:
+ * Determined xfermask.
+ */
+unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
+                                   const struct ata_acpi_gtm *gtm)
+{
+       unsigned long xfer_mask = 0;
+       unsigned int type;
+       int unit;
+       u8 mode;
+
+       /* we always use the 0 slot for crap hardware */
+       unit = dev->devno;
+       if (!(gtm->flags & 0x10))
+               unit = 0;
+
+       /* PIO */
+       mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
+       xfer_mask |= ata_xfer_mode2mask(mode);
+
+       /* See if we have MWDMA or UDMA data. We don't bother with
+        * MWDMA if UDMA is available as this means the BIOS set UDMA
+        * and our error changedown if it works is UDMA to PIO anyway.
+        */
+       if (!(gtm->flags & (1 << (2 * unit))))
+               type = ATA_SHIFT_MWDMA;
+       else
+               type = ATA_SHIFT_UDMA;
+
+       mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
+       xfer_mask |= ata_xfer_mode2mask(mode);
+
+       return xfer_mask;
+}
+EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
+
 /**
  * ata_acpi_cbl_80wire         -       Check for 80 wire cable
  * @ap: Port to check
+ * @gtm: GTM data to use
  *
- * Return 1 if the ACPI mode data for this port indicates the BIOS selected
- * an 80wire mode.
+ * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
  */
-
-int ata_acpi_cbl_80wire(struct ata_port *ap)
+int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
 {
-       const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
-       int valid = 0;
+       struct ata_device *dev;
 
-       if (!gtm)
-               return 0;
+       ata_link_for_each_dev(dev, &ap->link) {
+               unsigned long xfer_mask, udma_mask;
+
+               if (!ata_dev_enabled(dev))
+                       continue;
+
+               xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
+               ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
+
+               if (udma_mask & ~ATA_UDMA_MASK_40C)
+                       return 1;
+       }
 
-       /* 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);
 
+static void ata_acpi_gtf_to_tf(struct ata_device *dev,
+                              const struct ata_acpi_gtf *gtf,
+                              struct ata_taskfile *tf)
+{
+       ata_tf_init(dev, tf);
+
+       tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
+       tf->protocol = ATA_PROT_NODATA;
+       tf->feature = gtf->tf[0];       /* 0x1f1 */
+       tf->nsect   = gtf->tf[1];       /* 0x1f2 */
+       tf->lbal    = gtf->tf[2];       /* 0x1f3 */
+       tf->lbam    = gtf->tf[3];       /* 0x1f4 */
+       tf->lbah    = gtf->tf[4];       /* 0x1f5 */
+       tf->device  = gtf->tf[5];       /* 0x1f6 */
+       tf->command = gtf->tf[6];       /* 0x1f7 */
+}
+
+static int ata_acpi_filter_tf(const struct ata_taskfile *tf,
+                             const struct ata_taskfile *ptf)
+{
+       if (ata_acpi_gtf_filter & ATA_ACPI_FILTER_SETXFER) {
+               /* libata doesn't use ACPI to configure transfer mode.
+                * It will only confuse device configuration.  Skip.
+                */
+               if (tf->command == ATA_CMD_SET_FEATURES &&
+                   tf->feature == SETFEATURES_XFER)
+                       return 1;
+       }
+
+       if (ata_acpi_gtf_filter & ATA_ACPI_FILTER_LOCK) {
+               /* BIOS writers, sorry but we don't wanna lock
+                * features unless the user explicitly said so.
+                */
+
+               /* DEVICE CONFIGURATION FREEZE LOCK */
+               if (tf->command == ATA_CMD_CONF_OVERLAY &&
+                   tf->feature == ATA_DCO_FREEZE_LOCK)
+                       return 1;
+
+               /* SECURITY FREEZE LOCK */
+               if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
+                       return 1;
+
+               /* SET MAX LOCK and SET MAX FREEZE LOCK */
+               if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
+                   tf->command == ATA_CMD_SET_MAX &&
+                   (tf->feature == ATA_SET_MAX_LOCK ||
+                    tf->feature == ATA_SET_MAX_FREEZE_LOCK))
+                       return 1;
+       }
+
+       return 0;
+}
+
 /**
  * ata_acpi_run_tf - send taskfile registers to host controller
  * @dev: target ATA device
@@ -485,13 +589,15 @@ EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
  * EH context.
  *
  * RETURNS:
- * 1 if command is executed successfully.  0 if ignored or rejected,
- * -errno on other errors.
+ * 1 if command is executed successfully.  0 if ignored, rejected or
+ * filtered out, -errno on other errors.
  */
 static int ata_acpi_run_tf(struct ata_device *dev,
-                          const struct ata_acpi_gtf *gtf)
+                          const struct ata_acpi_gtf *gtf,
+                          const struct ata_acpi_gtf *prev_gtf)
 {
-       struct ata_taskfile tf, rtf;
+       struct ata_taskfile *pptf = NULL;
+       struct ata_taskfile tf, ptf, rtf;
        unsigned int err_mask;
        const char *level;
        char msg[60];
@@ -502,44 +608,44 @@ static int ata_acpi_run_tf(struct ata_device *dev,
            && (gtf->tf[6] == 0))
                return 0;
 
-       ata_tf_init(dev, &tf);
-
-       /* convert gtf to tf */
-       tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
-       tf.protocol = ATA_PROT_NODATA;
-       tf.feature = gtf->tf[0];        /* 0x1f1 */
-       tf.nsect   = gtf->tf[1];        /* 0x1f2 */
-       tf.lbal    = gtf->tf[2];        /* 0x1f3 */
-       tf.lbam    = gtf->tf[3];        /* 0x1f4 */
-       tf.lbah    = gtf->tf[4];        /* 0x1f5 */
-       tf.device  = gtf->tf[5];        /* 0x1f6 */
-       tf.command = gtf->tf[6];        /* 0x1f7 */
-
-       rtf = tf;
-       err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0, 0);
-
-       switch (err_mask) {
-       case 0:
-               level = KERN_DEBUG;
-               snprintf(msg, sizeof(msg), "succeeded");
-               rc = 1;
-               break;
-
-       case AC_ERR_DEV:
+       ata_acpi_gtf_to_tf(dev, gtf, &tf);
+       if (prev_gtf) {
+               ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
+               pptf = &ptf;
+       }
+
+       if (!ata_acpi_filter_tf(&tf, pptf)) {
+               rtf = tf;
+               err_mask = ata_exec_internal(dev, &rtf, NULL,
+                                            DMA_NONE, NULL, 0, 0);
+
+               switch (err_mask) {
+               case 0:
+                       level = KERN_DEBUG;
+                       snprintf(msg, sizeof(msg), "succeeded");
+                       rc = 1;
+                       break;
+
+               case AC_ERR_DEV:
+                       level = KERN_INFO;
+                       snprintf(msg, sizeof(msg),
+                                "rejected by device (Stat=0x%02x Err=0x%02x)",
+                                rtf.command, rtf.feature);
+                       rc = 0;
+                       break;
+
+               default:
+                       level = KERN_ERR;
+                       snprintf(msg, sizeof(msg),
+                                "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
+                                err_mask, rtf.command, rtf.feature);
+                       rc = -EIO;
+                       break;
+               }
+       } else {
                level = KERN_INFO;
-               snprintf(msg, sizeof(msg),
-                        "rejected by device (Stat=0x%02x Err=0x%02x)",
-                        rtf.command, rtf.feature);
+               snprintf(msg, sizeof(msg), "filtered out");
                rc = 0;
-               break;
-
-       default:
-               level = KERN_ERR;
-               snprintf(msg, sizeof(msg),
-                        "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
-                        err_mask, rtf.command, rtf.feature);
-               rc = -EIO;
-               break;
        }
 
        ata_dev_printk(dev, level,
@@ -566,7 +672,7 @@ static int ata_acpi_run_tf(struct ata_device *dev,
  */
 static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
 {
-       struct ata_acpi_gtf *gtf = NULL;
+       struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
        int gtf_count, i, rc;
 
        /* get taskfiles */
@@ -576,12 +682,14 @@ static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
        gtf_count = rc;
 
        /* execute them */
-       for (i = 0; i < gtf_count; i++) {
-               rc = ata_acpi_run_tf(dev, gtf++);
+       for (i = 0; i < gtf_count; i++, gtf++) {
+               rc = ata_acpi_run_tf(dev, gtf, pgtf);
                if (rc < 0)
                        break;
-               if (rc)
+               if (rc) {
                        (*nr_executed)++;
+                       pgtf = gtf;
+               }
        }
 
        ata_acpi_clear_gtf(dev);
@@ -704,6 +812,36 @@ void ata_acpi_on_resume(struct ata_port *ap)
        }
 }
 
+/**
+ * ata_acpi_set_state - set the port power state
+ * @ap: target ATA port
+ * @state: state, on/off
+ *
+ * This function executes the _PS0/_PS3 ACPI method to set the power state.
+ * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
+ */
+void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
+{
+       struct ata_device *dev;
+
+       if (!ap->acpi_handle || (ap->flags & ATA_FLAG_ACPI_SATA))
+               return;
+
+       /* channel first and then drives for power on and vica versa
+          for power off */
+       if (state.event == PM_EVENT_ON)
+               acpi_bus_set_power(ap->acpi_handle, ACPI_STATE_D0);
+
+       ata_link_for_each_dev(dev, &ap->link) {
+               if (dev->acpi_handle && ata_dev_enabled(dev))
+                       acpi_bus_set_power(dev->acpi_handle,
+                               state.event == PM_EVENT_ON ?
+                                       ACPI_STATE_D0 : ACPI_STATE_D3);
+       }
+       if (state.event != PM_EVENT_ON)
+               acpi_bus_set_power(ap->acpi_handle, ACPI_STATE_D3);
+}
+
 /**
  * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
  * @dev: target ATA device