ata_piix: SATA 2port controller port map fix
[cascardo/linux.git] / drivers / ata / libata-core.c
index eb97098..d696999 100644 (file)
@@ -97,13 +97,17 @@ static int ata_ignore_hpa = 0;
 module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
 MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
 
+static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
+module_param_named(dma, libata_dma_mask, int, 0444);
+MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
+
 static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
 module_param(ata_probe_timeout, int, 0444);
 MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
 
-int libata_noacpi = 1;
+int libata_noacpi = 0;
 module_param_named(noacpi, libata_noacpi, int, 0444);
-MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in suspend/resume when set");
+MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in probe/suspend/resume when set");
 
 MODULE_AUTHOR("Jeff Garzik");
 MODULE_DESCRIPTION("Library module for ATA devices");
@@ -670,29 +674,49 @@ static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
  *     None.
  *
  *     RETURNS:
- *     Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
- *     the event of failure.
+ *     Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
+ *     %ATA_DEV_UNKNOWN the event of failure.
  */
-
 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
 {
        /* Apple's open source Darwin code hints that some devices only
         * put a proper signature into the LBA mid/high registers,
         * So, we only check those.  It's sufficient for uniqueness.
+        *
+        * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
+        * signatures for ATA and ATAPI devices attached on SerialATA,
+        * 0x3c/0xc3 and 0x69/0x96 respectively.  However, SerialATA
+        * spec has never mentioned about using different signatures
+        * for ATA/ATAPI devices.  Then, Serial ATA II: Port
+        * Multiplier specification began to use 0x69/0x96 to identify
+        * port multpliers and 0x3c/0xc3 to identify SEMB device.
+        * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
+        * 0x69/0x96 shortly and described them as reserved for
+        * SerialATA.
+        *
+        * We follow the current spec and consider that 0x69/0x96
+        * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
         */
-
-       if (((tf->lbam == 0) && (tf->lbah == 0)) ||
-           ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
+       if ((tf->lbam == 0) && (tf->lbah == 0)) {
                DPRINTK("found ATA device by sig\n");
                return ATA_DEV_ATA;
        }
 
-       if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
-           ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
+       if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
                DPRINTK("found ATAPI device by sig\n");
                return ATA_DEV_ATAPI;
        }
 
+       if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
+               DPRINTK("found PMP device by sig\n");
+               return ATA_DEV_PMP;
+       }
+
+       if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
+               printk("ata: SEMB device ignored\n");
+               return ATA_DEV_SEMB_UNSUP; /* not yet */
+       }
+
        DPRINTK("unknown device\n");
        return ATA_DEV_UNKNOWN;
 }
@@ -902,7 +926,7 @@ static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
        tf.protocol |= ATA_PROT_NODATA;
        tf.device |= ATA_LBA;
 
-       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
        if (err_mask) {
                ata_dev_printk(dev, KERN_WARNING, "failed to read native "
                               "max address (err_mask=0x%x)\n", err_mask);
@@ -964,7 +988,7 @@ static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
        tf.lbam = (new_sectors >> 8) & 0xff;
        tf.lbah = (new_sectors >> 16) & 0xff;
 
-       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
        if (err_mask) {
                ata_dev_printk(dev, KERN_WARNING, "failed to set "
                               "max address (err_mask=0x%x)\n", err_mask);
@@ -1370,6 +1394,7 @@ static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
  *     @dma_dir: Data tranfer direction of the command
  *     @sg: sg list for the data buffer of the command
  *     @n_elem: Number of sg entries
+ *     @timeout: Timeout in msecs (0 for default)
  *
  *     Executes libata internal command with timeout.  @tf contains
  *     command on entry and result on return.  Timeout and error
@@ -1386,7 +1411,7 @@ static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
 unsigned ata_exec_internal_sg(struct ata_device *dev,
                              struct ata_taskfile *tf, const u8 *cdb,
                              int dma_dir, struct scatterlist *sg,
-                             unsigned int n_elem)
+                             unsigned int n_elem, unsigned long timeout)
 {
        struct ata_link *link = dev->link;
        struct ata_port *ap = link->ap;
@@ -1462,7 +1487,10 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
 
        spin_unlock_irqrestore(ap->lock, flags);
 
-       rc = wait_for_completion_timeout(&wait, ata_probe_timeout);
+       if (!timeout)
+               timeout = ata_probe_timeout * 1000 / HZ;
+
+       rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
 
        ata_port_flush_task(ap);
 
@@ -1547,6 +1575,7 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
  *     @dma_dir: Data tranfer direction of the command
  *     @buf: Data buffer of the command
  *     @buflen: Length of data buffer
+ *     @timeout: Timeout in msecs (0 for default)
  *
  *     Wrapper around ata_exec_internal_sg() which takes simple
  *     buffer instead of sg list.
@@ -1559,7 +1588,8 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
  */
 unsigned ata_exec_internal(struct ata_device *dev,
                           struct ata_taskfile *tf, const u8 *cdb,
-                          int dma_dir, void *buf, unsigned int buflen)
+                          int dma_dir, void *buf, unsigned int buflen,
+                          unsigned long timeout)
 {
        struct scatterlist *psg = NULL, sg;
        unsigned int n_elem = 0;
@@ -1571,7 +1601,8 @@ unsigned ata_exec_internal(struct ata_device *dev,
                n_elem++;
        }
 
-       return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem);
+       return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
+                                   timeout);
 }
 
 /**
@@ -1598,7 +1629,7 @@ unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
        tf.flags |= ATA_TFLAG_DEVICE;
        tf.protocol = ATA_PROT_NODATA;
 
-       return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
 }
 
 /**
@@ -1713,7 +1744,7 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
        tf.flags |= ATA_TFLAG_POLLING;
 
        err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
-                                    id, sizeof(id[0]) * ATA_ID_WORDS);
+                                    id, sizeof(id[0]) * ATA_ID_WORDS, 0);
        if (err_mask) {
                if (err_mask & AC_ERR_NODEV_HINT) {
                        DPRINTK("ata%u.%d: NODEV after polling detection\n",
@@ -1772,7 +1803,8 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
                tf.feature = SETFEATURES_SPINUP;
                tf.protocol = ATA_PROT_NODATA;
                tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
-               err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+               err_mask = ata_exec_internal(dev, &tf, NULL,
+                                            DMA_NONE, NULL, 0, 0);
                if (err_mask && id[2] != 0x738c) {
                        rc = -EIO;
                        reason = "SPINUP failed";
@@ -2897,14 +2929,27 @@ int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
        /* step 1: calculate xfer_mask */
        ata_link_for_each_dev(dev, link) {
                unsigned int pio_mask, dma_mask;
+               unsigned int mode_mask;
 
                if (!ata_dev_enabled(dev))
                        continue;
 
+               mode_mask = ATA_DMA_MASK_ATA;
+               if (dev->class == ATA_DEV_ATAPI)
+                       mode_mask = ATA_DMA_MASK_ATAPI;
+               else if (ata_id_is_cfa(dev->id))
+                       mode_mask = ATA_DMA_MASK_CFA;
+
                ata_dev_xfermask(dev);
 
                pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
                dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
+
+               if (libata_dma_mask & mode_mask)
+                       dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
+               else
+                       dma_mask = 0;
+
                dev->pio_mode = ata_xfer_mask2mode(pio_mask);
                dev->dma_mode = ata_xfer_mask2mode(dma_mask);
 
@@ -3172,6 +3217,8 @@ static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
                             unsigned long deadline)
 {
        struct ata_ioports *ioaddr = &ap->ioaddr;
+       struct ata_device *dev;
+       int i = 0;
 
        DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
 
@@ -3182,6 +3229,25 @@ static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
        udelay(20);     /* FIXME: flush */
        iowrite8(ap->ctl, ioaddr->ctl_addr);
 
+       /* If we issued an SRST then an ATA drive (not ATAPI)
+        * may have changed configuration and be in PIO0 timing. If
+        * we did a hard reset (or are coming from power on) this is
+        * true for ATA or ATAPI. Until we've set a suitable controller
+        * mode we should not touch the bus as we may be talking too fast.
+        */
+
+       ata_link_for_each_dev(dev, &ap->link)
+               dev->pio_mode = XFER_PIO_0;
+
+       /* If the controller has a pio mode setup function then use
+          it to set the chipset to rights. Don't touch the DMA setup
+          as that will be dealt with when revalidating */
+       if (ap->ops->set_piomode) {
+               ata_link_for_each_dev(dev, &ap->link)
+                       if (devmask & (1 << i++))
+                               ap->ops->set_piomode(ap, dev);
+       }
+
        /* spec mandates ">= 2ms" before checking status.
         * We wait 150ms, because that was the magic delay used for
         * ATAPI devices in Hale Landis's ATADRVR, for the period of time
@@ -3426,6 +3492,12 @@ int ata_std_prereset(struct ata_link *link, unsigned long deadline)
            (link->flags & ATA_LFLAG_HRST_TO_RESUME))
                ehc->i.action |= ATA_EH_HARDRESET;
 
+       /* Some PMPs don't work with only SRST, force hardreset if PMP
+        * is supported.
+        */
+       if (ap->flags & ATA_FLAG_PMP)
+               ehc->i.action |= ATA_EH_HARDRESET;
+
        /* if we're about to do hardreset, nothing more to do */
        if (ehc->i.action & ATA_EH_HARDRESET)
                return 0;
@@ -3616,6 +3688,16 @@ int sata_std_hardreset(struct ata_link *link, unsigned int *class,
        /* wait a while before checking status, see SRST for more info */
        msleep(150);
 
+       /* If PMP is supported, we have to do follow-up SRST.  Note
+        * that some PMPs don't send D2H Reg FIS after hardreset at
+        * all if the first port is empty.  Wait for it just for a
+        * second and request follow-up SRST.
+        */
+       if (ap->flags & ATA_FLAG_PMP) {
+               ata_wait_ready(ap, jiffies + HZ);
+               return -EAGAIN;
+       }
+
        rc = ata_wait_ready(ap, deadline);
        /* link occupied, -ENODEV too is an error */
        if (rc) {
@@ -3865,6 +3947,9 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        { "IOMEGA  ZIP 250       ATAPI", NULL,  ATA_HORKAGE_NODMA }, /* temporary fix */
        { "IOMEGA  ZIP 250       ATAPI       Floppy",
                                NULL,           ATA_HORKAGE_NODMA },
+       /* Odd clown on sil3726/4726 PMPs */
+       { "Config  Disk",       NULL,           ATA_HORKAGE_NODMA |
+                                               ATA_HORKAGE_SKIP_PM },
 
        /* Weird ATAPI devices */
        { "TORiSAN DVD-ROM DRD-N216", NULL,     ATA_HORKAGE_MAX_SEC_128 },
@@ -3881,6 +3966,7 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        { "Maxtor 7V300F0",     "VA111630",     ATA_HORKAGE_NONCQ },
        { "HITACHI HDS7250SASUN500G*", NULL,    ATA_HORKAGE_NONCQ },
        { "HITACHI HDS7225SBSUN250G*", NULL,    ATA_HORKAGE_NONCQ },
+       { "ST380817AS",         "3.42",         ATA_HORKAGE_NONCQ },
 
        /* Blacklist entries taken from Silicon Image 3124/3132
           Windows driver .inf file - also several Linux problem reports */
@@ -3890,12 +3976,15 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        /* Drives which do spurious command completion */
        { "HTS541680J9SA00",    "SB2IC7EP",     ATA_HORKAGE_NONCQ, },
        { "HTS541612J9SA00",    "SBDIC7JP",     ATA_HORKAGE_NONCQ, },
+       { "HDT722516DLA380",    "V43OA96A",     ATA_HORKAGE_NONCQ, },
        { "Hitachi HTS541616J9SA00", "SB4OC70P", ATA_HORKAGE_NONCQ, },
        { "WDC WD740ADFD-00NLR1", NULL,         ATA_HORKAGE_NONCQ, },
        { "WDC WD3200AAJS-00RYA0", "12.01B01",  ATA_HORKAGE_NONCQ, },
        { "FUJITSU MHV2080BH",  "00840028",     ATA_HORKAGE_NONCQ, },
        { "ST9120822AS",        "3.CLF",        ATA_HORKAGE_NONCQ, },
        { "ST9160821AS",        "3.CLF",        ATA_HORKAGE_NONCQ, },
+       { "ST9160821AS",        "3.ALD",        ATA_HORKAGE_NONCQ, },
+       { "ST9160821AS",        "3.CCD",        ATA_HORKAGE_NONCQ, },
        { "ST3160812AS",        "3.ADJ",        ATA_HORKAGE_NONCQ, },
        { "ST980813AS",         "3.ADB",        ATA_HORKAGE_NONCQ, },
        { "SAMSUNG HD401LJ",    "ZZ100-15",     ATA_HORKAGE_NONCQ, },
@@ -4077,7 +4166,7 @@ static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
        tf.protocol = ATA_PROT_NODATA;
        tf.nsect = dev->xfer_mode;
 
-       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
 
        DPRINTK("EXIT, err_mask=%x\n", err_mask);
        return err_mask;
@@ -4113,7 +4202,7 @@ static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable)
        tf.protocol = ATA_PROT_NODATA;
        tf.nsect = SATA_AN;
 
-       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
 
        DPRINTK("EXIT, err_mask=%x\n", err_mask);
        return err_mask;
@@ -4151,7 +4240,7 @@ static unsigned int ata_dev_init_params(struct ata_device *dev,
        tf.nsect = sectors;
        tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
 
-       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
+       err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
        /* A clean abort indicates an original or just out of spec drive
           and we should continue as we issue the setup based on the
           drive reported working geometry */
@@ -5963,22 +6052,26 @@ int sata_scr_valid(struct ata_link *link)
  *     @val: Place to store read value
  *
  *     Read SCR register @reg of @link into *@val.  This function is
- *     guaranteed to succeed if the cable type of the port is SATA
- *     and the port implements ->scr_read.
+ *     guaranteed to succeed if @link is ap->link, the cable type of
+ *     the port is SATA and the port implements ->scr_read.
  *
  *     LOCKING:
- *     None.
+ *     None if @link is ap->link.  Kernel thread context otherwise.
  *
  *     RETURNS:
  *     0 on success, negative errno on failure.
  */
 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
 {
-       struct ata_port *ap = link->ap;
+       if (ata_is_host_link(link)) {
+               struct ata_port *ap = link->ap;
 
-       if (sata_scr_valid(link))
-               return ap->ops->scr_read(ap, reg, val);
-       return -EOPNOTSUPP;
+               if (sata_scr_valid(link))
+                       return ap->ops->scr_read(ap, reg, val);
+               return -EOPNOTSUPP;
+       }
+
+       return sata_pmp_scr_read(link, reg, val);
 }
 
 /**
@@ -5988,22 +6081,26 @@ int sata_scr_read(struct ata_link *link, int reg, u32 *val)
  *     @val: value to write
  *
  *     Write @val to SCR register @reg of @link.  This function is
- *     guaranteed to succeed if the cable type of the port is SATA
- *     and the port implements ->scr_read.
+ *     guaranteed to succeed if @link is ap->link, the cable type of
+ *     the port is SATA and the port implements ->scr_read.
  *
  *     LOCKING:
- *     None.
+ *     None if @link is ap->link.  Kernel thread context otherwise.
  *
  *     RETURNS:
  *     0 on success, negative errno on failure.
  */
 int sata_scr_write(struct ata_link *link, int reg, u32 val)
 {
-       struct ata_port *ap = link->ap;
+       if (ata_is_host_link(link)) {
+               struct ata_port *ap = link->ap;
+
+               if (sata_scr_valid(link))
+                       return ap->ops->scr_write(ap, reg, val);
+               return -EOPNOTSUPP;
+       }
 
-       if (sata_scr_valid(link))
-               return ap->ops->scr_write(ap, reg, val);
-       return -EOPNOTSUPP;
+       return sata_pmp_scr_write(link, reg, val);
 }
 
 /**
@@ -6016,23 +6113,27 @@ int sata_scr_write(struct ata_link *link, int reg, u32 val)
  *     function performs flush after writing to the register.
  *
  *     LOCKING:
- *     None.
+ *     None if @link is ap->link.  Kernel thread context otherwise.
  *
  *     RETURNS:
  *     0 on success, negative errno on failure.
  */
 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
 {
-       struct ata_port *ap = link->ap;
-       int rc;
+       if (ata_is_host_link(link)) {
+               struct ata_port *ap = link->ap;
+               int rc;
 
-       if (sata_scr_valid(link)) {
-               rc = ap->ops->scr_write(ap, reg, val);
-               if (rc == 0)
-                       rc = ap->ops->scr_read(ap, reg, &val);
-               return rc;
+               if (sata_scr_valid(link)) {
+                       rc = ap->ops->scr_write(ap, reg, val);
+                       if (rc == 0)
+                               rc = ap->ops->scr_read(ap, reg, &val);
+                       return rc;
+               }
+               return -EOPNOTSUPP;
        }
-       return -EOPNOTSUPP;
+
+       return sata_pmp_scr_write(link, reg, val);
 }
 
 /**
@@ -6421,6 +6522,7 @@ static void ata_host_release(struct device *gendev, void *res)
                if (ap->scsi_host)
                        scsi_host_put(ap->scsi_host);
 
+               kfree(ap->pmp_link);
                kfree(ap);
                host->ports[i] = NULL;
        }
@@ -6680,13 +6782,14 @@ int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
                xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
                                              ap->udma_mask);
 
-               if (!ata_port_is_dummy(ap))
+               if (!ata_port_is_dummy(ap)) {
                        ata_port_printk(ap, KERN_INFO,
                                        "%cATA max %s %s\n",
                                        (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
                                        ata_mode_string(xfer_mask),
                                        ap->link.eh_info.desc);
-               else
+                       ata_ehi_clear_desc(&ap->link.eh_info);
+               } else
                        ata_port_printk(ap, KERN_INFO, "DUMMY\n");
        }
 
@@ -6905,7 +7008,7 @@ void ata_std_ports(struct ata_ioports *ioaddr)
  */
 void ata_pci_remove_one(struct pci_dev *pdev)
 {
-       struct device *dev = pci_dev_to_dev(pdev);
+       struct device *dev = &pdev->dev;
        struct ata_host *host = dev_get_drvdata(dev);
 
        ata_host_detach(host);
@@ -7251,6 +7354,12 @@ EXPORT_SYMBOL_GPL(ata_pci_default_filter);
 EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
 #endif /* CONFIG_PCI */
 
+EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
+EXPORT_SYMBOL_GPL(sata_pmp_std_prereset);
+EXPORT_SYMBOL_GPL(sata_pmp_std_hardreset);
+EXPORT_SYMBOL_GPL(sata_pmp_std_postreset);
+EXPORT_SYMBOL_GPL(sata_pmp_do_eh);
+
 EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
 EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
 EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);