Merge /spare/repo/linux-2.6/
[safe/jmp/linux-2.6] / drivers / scsi / ahci.c
index 8b468a6..179c95c 100644 (file)
@@ -1,26 +1,34 @@
 /*
  *  ahci.c - AHCI SATA support
  *
- *  Copyright 2004 Red Hat, Inc.
+ *  Maintained by:  Jeff Garzik <jgarzik@pobox.com>
+ *                 Please ALWAYS copy linux-ide@vger.kernel.org
+ *                 on emails.
  *
- *  The contents of this file are subject to the Open
- *  Software License version 1.1 that can be found at
- *  http://www.opensource.org/licenses/osl-1.1.txt and is included herein
- *  by reference.
+ *  Copyright 2004-2005 Red Hat, Inc.
  *
- *  Alternatively, the contents of this file may be used under the terms
- *  of the GNU General Public License version 2 (the "GPL") as distributed
- *  in the kernel source COPYING file, in which case the provisions of
- *  the GPL are applicable instead of the above.  If you wish to allow
- *  the use of your version of this file only under the terms of the
- *  GPL and not to allow others to use your version of this file under
- *  the OSL, indicate your decision by deleting the provisions above and
- *  replace them with the notice and other provisions required by the GPL.
- *  If you do not delete the provisions above, a recipient may use your
- *  version of this file under either the OSL or the GPL.
  *
- * Version 1.0 of the AHCI specification:
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2, or (at your option)
+ *  any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; see the file COPYING.  If not, write to
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ *
+ * libata documentation is available via 'make {ps|pdf}docs',
+ * as Documentation/DocBook/libata.*
+ *
+ * AHCI hardware documentation:
  * http://www.intel.com/technology/serialata/pdf/rev1_0.pdf
+ * http://www.intel.com/technology/serialata/pdf/rev1_1.pdf
  *
  */
 
 #include <linux/delay.h>
 #include <linux/interrupt.h>
 #include <linux/sched.h>
+#include <linux/dma-mapping.h>
 #include "scsi.h"
 #include <scsi/scsi_host.h>
 #include <linux/libata.h>
 #include <asm/io.h>
 
 #define DRV_NAME       "ahci"
-#define DRV_VERSION    "1.00"
+#define DRV_VERSION    "1.01"
 
 
 enum {
@@ -49,6 +58,7 @@ enum {
        AHCI_CMD_SLOT_SZ        = 32 * 32,
        AHCI_RX_FIS_SZ          = 256,
        AHCI_CMD_TBL_HDR        = 0x80,
+       AHCI_CMD_TBL_CDB        = 0x40,
        AHCI_CMD_TBL_SZ         = AHCI_CMD_TBL_HDR + (AHCI_MAX_SG * 16),
        AHCI_PORT_PRIV_DMA_SZ   = AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_SZ +
                                  AHCI_RX_FIS_SZ,
@@ -133,6 +143,9 @@ enum {
        PORT_CMD_ICC_ACTIVE     = (0x1 << 28), /* Put i/f in active state */
        PORT_CMD_ICC_PARTIAL    = (0x2 << 28), /* Put i/f in partial state */
        PORT_CMD_ICC_SLUMBER    = (0x6 << 28), /* Put i/f in slumber state */
+
+       /* hpriv->flags bits */
+       AHCI_FLAG_MSI           = (1 << 0),
 };
 
 struct ahci_cmd_hdr {
@@ -182,6 +195,7 @@ static void ahci_qc_prep(struct ata_queued_cmd *qc);
 static u8 ahci_check_status(struct ata_port *ap);
 static u8 ahci_check_err(struct ata_port *ap);
 static inline int ahci_host_intr(struct ata_port *ap, struct ata_queued_cmd *qc);
+static void ahci_remove_one (struct pci_dev *pdev);
 
 static Scsi_Host_Template ahci_sht = {
        .module                 = THIS_MODULE,
@@ -263,6 +277,8 @@ static struct pci_device_id ahci_pci_tbl[] = {
          board_ahci }, /* ESB2 */
        { PCI_VENDOR_ID_INTEL, 0x2683, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
          board_ahci }, /* ESB2 */
+       { PCI_VENDOR_ID_INTEL, 0x27c6, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
+         board_ahci }, /* ICH7-M DH */
        { }     /* terminate list */
 };
 
@@ -271,7 +287,7 @@ static struct pci_driver ahci_pci_driver = {
        .name                   = DRV_NAME,
        .id_table               = ahci_pci_tbl,
        .probe                  = ahci_init_one,
-       .remove                 = ata_pci_remove_one,
+       .remove                 = ahci_remove_one,
 };
 
 
@@ -298,26 +314,19 @@ static int ahci_port_start(struct ata_port *ap)
        struct device *dev = ap->host_set->dev;
        struct ahci_host_priv *hpriv = ap->host_set->private_data;
        struct ahci_port_priv *pp;
-       int rc;
        void *mem, *mmio = ap->host_set->mmio_base;
        void *port_mmio = ahci_port_base(mmio, ap->port_no);
        dma_addr_t mem_dma;
 
-       rc = ata_port_start(ap);
-       if (rc)
-               return rc;
-
        pp = kmalloc(sizeof(*pp), GFP_KERNEL);
-       if (!pp) {
-               rc = -ENOMEM;
-               goto err_out;
-       }
+       if (!pp)
+               return -ENOMEM;
        memset(pp, 0, sizeof(*pp));
 
        mem = dma_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma, GFP_KERNEL);
        if (!mem) {
-               rc = -ENOMEM;
-               goto err_out_kfree;
+               kfree(pp);
+               return -ENOMEM;
        }
        memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
 
@@ -367,12 +376,6 @@ static int ahci_port_start(struct ata_port *ap)
        readl(port_mmio + PORT_CMD); /* flush */
 
        return 0;
-
-err_out_kfree:
-       kfree(pp);
-err_out:
-       ata_port_stop(ap);
-       return rc;
 }
 
 
@@ -398,7 +401,6 @@ static void ahci_port_stop(struct ata_port *ap)
        dma_free_coherent(dev, AHCI_PORT_PRIV_DMA_SZ,
                          pp->cmd_slot, pp->cmd_slot_dma);
        kfree(pp);
-       ata_port_stop(ap);
 }
 
 static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg_in)
@@ -505,7 +507,8 @@ static void ahci_fill_sg(struct ata_queued_cmd *qc)
 
 static void ahci_qc_prep(struct ata_queued_cmd *qc)
 {
-       struct ahci_port_priv *pp = qc->ap->private_data;
+       struct ata_port *ap = qc->ap;
+       struct ahci_port_priv *pp = ap->private_data;
        u32 opts;
        const u32 cmd_fis_len = 5; /* five dwords */
 
@@ -517,18 +520,8 @@ static void ahci_qc_prep(struct ata_queued_cmd *qc)
        opts = (qc->n_elem << 16) | cmd_fis_len;
        if (qc->tf.flags & ATA_TFLAG_WRITE)
                opts |= AHCI_CMD_WRITE;
-
-       switch (qc->tf.protocol) {
-       case ATA_PROT_ATAPI:
-       case ATA_PROT_ATAPI_NODATA:
-       case ATA_PROT_ATAPI_DMA:
+       if (is_atapi_taskfile(&qc->tf))
                opts |= AHCI_CMD_ATAPI;
-               break;
-
-       default:
-               /* do nothing */
-               break;
-       }
 
        pp->cmd_slot[0].opts = cpu_to_le32(opts);
        pp->cmd_slot[0].status = 0;
@@ -540,6 +533,10 @@ static void ahci_qc_prep(struct ata_queued_cmd *qc)
         * a SATA Register - Host to Device command FIS.
         */
        ata_tf_to_fis(&qc->tf, pp->cmd_tbl, 0);
+       if (opts & AHCI_CMD_ATAPI) {
+               memset(pp->cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
+               memcpy(pp->cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, ap->cdb_len);
+       }
 
        if (!(qc->flags & ATA_QCFLAG_DMAMAP))
                return;
@@ -597,12 +594,16 @@ static void ahci_intr_error(struct ata_port *ap, u32 irq_stat)
 
 static void ahci_eng_timeout(struct ata_port *ap)
 {
-       void *mmio = ap->host_set->mmio_base;
+       struct ata_host_set *host_set = ap->host_set;
+       void *mmio = host_set->mmio_base;
        void *port_mmio = ahci_port_base(mmio, ap->port_no);
        struct ata_queued_cmd *qc;
+       unsigned long flags;
 
        DPRINTK("ENTER\n");
 
+       spin_lock_irqsave(&host_set->lock, flags);
+
        ahci_intr_error(ap, readl(port_mmio + PORT_IRQ_STAT));
 
        qc = ata_qc_from_tag(ap, ap->active_tag);
@@ -620,6 +621,7 @@ static void ahci_eng_timeout(struct ata_port *ap)
                ata_qc_complete(qc, ATA_ERR);
        }
 
+       spin_unlock_irqrestore(&host_set->lock, flags);
 }
 
 static inline int ahci_host_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
@@ -709,9 +711,6 @@ static int ahci_qc_issue(struct ata_queued_cmd *qc)
        struct ata_port *ap = qc->ap;
        void *port_mmio = (void *) ap->ioaddr.cmd_addr;
 
-       writel(1, port_mmio + PORT_SCR_ACT);
-       readl(port_mmio + PORT_SCR_ACT);        /* flush */
-
        writel(1, port_mmio + PORT_CMD_ISSUE);
        readl(port_mmio + PORT_CMD_ISSUE);      /* flush */
 
@@ -794,8 +793,6 @@ static int ahci_host_init(struct ata_probe_ent *probe_ent)
                                return rc;
                        }
                }
-
-               hpriv->flags |= HOST_CAP_64;
        } else {
                rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
                if (rc) {
@@ -878,15 +875,19 @@ static int ahci_host_init(struct ata_probe_ent *probe_ent)
 }
 
 /* move to PCI layer, integrate w/ MSI stuff */
-static void pci_enable_intx(struct pci_dev *pdev)
+static void pci_intx(struct pci_dev *pdev, int enable)
 {
-       u16 pci_command;
+       u16 pci_command, new;
 
        pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
-       if (pci_command & PCI_COMMAND_INTX_DISABLE) {
-               pci_command &= ~PCI_COMMAND_INTX_DISABLE;
+
+       if (enable)
+               new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
+       else
+               new = pci_command | PCI_COMMAND_INTX_DISABLE;
+
+       if (new != pci_command)
                pci_write_config_word(pdev, PCI_COMMAND, pci_command);
-       }
 }
 
 static void ahci_print_info(struct ata_probe_ent *probe_ent)
@@ -968,7 +969,7 @@ static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
        unsigned long base;
        void *mmio_base;
        unsigned int board_idx = (unsigned int) ent->driver_data;
-       int pci_dev_busy = 0;
+       int have_msi, pci_dev_busy = 0;
        int rc;
 
        VPRINTK("ENTER\n");
@@ -986,12 +987,17 @@ static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
                goto err_out;
        }
 
-       pci_enable_intx(pdev);
+       if (pci_enable_msi(pdev) == 0)
+               have_msi = 1;
+       else {
+               pci_intx(pdev, 1);
+               have_msi = 0;
+       }
 
        probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
        if (probe_ent == NULL) {
                rc = -ENOMEM;
-               goto err_out_regions;
+               goto err_out_msi;
        }
 
        memset(probe_ent, 0, sizeof(*probe_ent));
@@ -1024,6 +1030,9 @@ static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
        probe_ent->mmio_base = mmio_base;
        probe_ent->private_data = hpriv;
 
+       if (have_msi)
+               hpriv->flags |= AHCI_FLAG_MSI;
+
        /* initialize adapter */
        rc = ahci_host_init(probe_ent);
        if (rc)
@@ -1043,7 +1052,11 @@ err_out_iounmap:
        iounmap(mmio_base);
 err_out_free_ent:
        kfree(probe_ent);
-err_out_regions:
+err_out_msi:
+       if (have_msi)
+               pci_disable_msi(pdev);
+       else
+               pci_intx(pdev, 0);
        pci_release_regions(pdev);
 err_out:
        if (!pci_dev_busy)
@@ -1051,6 +1064,42 @@ err_out:
        return rc;
 }
 
+static void ahci_remove_one (struct pci_dev *pdev)
+{
+       struct device *dev = pci_dev_to_dev(pdev);
+       struct ata_host_set *host_set = dev_get_drvdata(dev);
+       struct ahci_host_priv *hpriv = host_set->private_data;
+       struct ata_port *ap;
+       unsigned int i;
+       int have_msi;
+
+       for (i = 0; i < host_set->n_ports; i++) {
+               ap = host_set->ports[i];
+
+               scsi_remove_host(ap->host);
+       }
+
+       have_msi = hpriv->flags & AHCI_FLAG_MSI;
+       free_irq(host_set->irq, host_set);
+
+       for (i = 0; i < host_set->n_ports; i++) {
+               ap = host_set->ports[i];
+
+               ata_scsi_release(ap->host);
+               scsi_host_put(ap->host);
+       }
+
+       host_set->ops->host_stop(host_set);
+       kfree(host_set);
+
+       if (have_msi)
+               pci_disable_msi(pdev);
+       else
+               pci_intx(pdev, 0);
+       pci_release_regions(pdev);
+       pci_disable_device(pdev);
+       dev_set_drvdata(dev, NULL);
+}
 
 static int __init ahci_init(void)
 {
@@ -1068,6 +1117,7 @@ MODULE_AUTHOR("Jeff Garzik");
 MODULE_DESCRIPTION("AHCI SATA low-level driver");
 MODULE_LICENSE("GPL");
 MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
+MODULE_VERSION(DRV_VERSION);
 
 module_init(ahci_init);
 module_exit(ahci_exit);