libata: use PIO for non-16 byte aligned ATAPI commands
[sfrench/cifs-2.6.git] / drivers / ata / libata-core.c
index d3ea7f55283c596e4f897168a00a5d9d3d1ee59b..094b51891dbfef9245fddc353166768d7270405b 100644 (file)
@@ -59,7 +59,7 @@
 
 #include "libata.h"
 
-#define DRV_VERSION    "2.20"  /* must be exactly four chars */
+#define DRV_VERSION    "2.21"  /* must be exactly four chars */
 
 
 /* debounce timing parameters in msecs { interval, duration, timeout } */
@@ -600,8 +600,9 @@ static const char *sata_spd_string(unsigned int spd)
 
 void ata_dev_disable(struct ata_device *dev)
 {
-       if (ata_dev_enabled(dev) && ata_msg_drv(dev->ap)) {
-               ata_dev_printk(dev, KERN_WARNING, "disabled\n");
+       if (ata_dev_enabled(dev)) {
+               if (ata_msg_drv(dev->ap))
+                       ata_dev_printk(dev, KERN_WARNING, "disabled\n");
                ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
                                             ATA_DNXFER_QUIET);
                dev->class++;
@@ -977,17 +978,12 @@ static u64 ata_hpa_resize(struct ata_device *dev)
 {
        u64 sectors = dev->n_sectors;
        u64 hpa_sectors;
-       
+
        if (ata_id_has_lba48(dev->id))
                hpa_sectors = ata_read_native_max_address_ext(dev);
        else
                hpa_sectors = ata_read_native_max_address(dev);
 
-       /* if no hpa, both should be equal */
-       ata_dev_printk(dev, KERN_INFO, "%s 1: sectors = %lld, "
-                               "hpa_sectors = %lld\n",
-               __FUNCTION__, (long long)sectors, (long long)hpa_sectors);
-
        if (hpa_sectors > sectors) {
                ata_dev_printk(dev, KERN_INFO,
                        "Host Protected Area detected:\n"
@@ -1009,7 +1005,11 @@ static u64 ata_hpa_resize(struct ata_device *dev)
                                return hpa_sectors;
                        }
                }
-       }
+       } else if (hpa_sectors < sectors)
+               ata_dev_printk(dev, KERN_WARNING, "%s 1: hpa sectors (%lld) "
+                              "is smaller than sectors (%lld)\n", __FUNCTION__,
+                              (long long)hpa_sectors, (long long)sectors);
+
        return sectors;
 }
 
@@ -1588,7 +1588,7 @@ unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
  *     Check if the current speed of the device requires IORDY. Used
  *     by various controllers for chip configuration.
  */
+
 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
 {
        /* Controller doesn't support  IORDY. Probably a pointless check
@@ -1611,7 +1611,7 @@ unsigned int ata_pio_need_iordy(const struct ata_device *adev)
  *     Compute the highest mode possible if we are not using iordy. Return
  *     -1 if no iordy mode is available.
  */
+
 static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
 {
        /* If we have no drive specific rule, then PIO 2 is non IORDY */
@@ -1727,7 +1727,7 @@ int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
 
        /* sanity check */
        rc = -EINVAL;
-       reason = "device reports illegal type";
+       reason = "device reports invalid type";
 
        if (class == ATA_DEV_ATA) {
                if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
@@ -1900,6 +1900,13 @@ int ata_dev_configure(struct ata_device *dev)
        if (ata_msg_probe(ap))
                ata_dump_id(id);
 
+       /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
+       ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
+                       sizeof(fwrevbuf));
+
+       ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
+                       sizeof(modelbuf));
+
        /* ATA-specific feature tests */
        if (dev->class == ATA_DEV_ATA) {
                if (ata_id_is_cfa(id)) {
@@ -1914,13 +1921,6 @@ int ata_dev_configure(struct ata_device *dev)
 
                dev->n_sectors = ata_id_n_sectors(id);
 
-               /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
-               ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
-                               sizeof(fwrevbuf));
-
-               ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
-                               sizeof(modelbuf));
-
                if (dev->id[59] & 0x100)
                        dev->multi_count = dev->id[59] & 0xff;
 
@@ -2009,7 +2009,9 @@ int ata_dev_configure(struct ata_device *dev)
 
                /* print device info to dmesg */
                if (ata_msg_drv(ap) && print_info)
-                       ata_dev_printk(dev, KERN_INFO, "ATAPI, max %s%s\n",
+                       ata_dev_printk(dev, KERN_INFO,
+                                      "ATAPI: %s, %s, max %s%s\n",
+                                      modelbuf, fwrevbuf,
                                       ata_mode_string(xfer_mask),
                                       cdb_intr_string);
        }
@@ -2663,7 +2665,7 @@ int ata_timing_compute(struct ata_device *adev, unsigned short speed,
                t->active += (t->cycle - (t->active + t->recover)) / 2;
                t->recover = t->cycle - t->active;
        }
-       
+
        /* In a few cases quantisation may produce enough errors to
           leave t->cycle too low for the sum of active and recovery
           if so we must correct this */
@@ -2893,9 +2895,6 @@ int ata_do_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
        if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
                ap->host->simplex_claimed = ap;
 
-       /* step5: chip specific finalisation */
-       if (ap->ops->post_set_mode)
-               ap->ops->post_set_mode(ap);
  out:
        if (rc)
                *r_failed_dev = dev;
@@ -3025,7 +3024,7 @@ int ata_wait_ready(struct ata_port *ap, unsigned long deadline)
 
                if (!(status & ATA_BUSY))
                        return 0;
-               if (status == 0xff)
+               if (!ata_port_online(ap) && status == 0xff)
                        return -ENODEV;
                if (time_after(now, deadline))
                        return -EBUSY;
@@ -3062,22 +3061,28 @@ static int ata_bus_post_reset(struct ata_port *ap, unsigned int devmask,
                }
        }
 
-       /* if device 1 was found in ata_devchk, wait for
-        * register access, then wait for BSY to clear
+       /* if device 1 was found in ata_devchk, wait for register
+        * access briefly, then wait for BSY to clear.
         */
-       while (dev1) {
-               u8 nsect, lbal;
+       if (dev1) {
+               int i;
 
                ap->ops->dev_select(ap, 1);
-               nsect = ioread8(ioaddr->nsect_addr);
-               lbal = ioread8(ioaddr->lbal_addr);
-               if ((nsect == 1) && (lbal == 1))
-                       break;
-               if (time_after(jiffies, deadline))
-                       return -EBUSY;
-               msleep(50);     /* give drive a breather */
-       }
-       if (dev1) {
+
+               /* Wait for register access.  Some ATAPI devices fail
+                * to set nsect/lbal after reset, so don't waste too
+                * much time on it.  We're gonna wait for !BSY anyway.
+                */
+               for (i = 0; i < 2; i++) {
+                       u8 nsect, lbal;
+
+                       nsect = ioread8(ioaddr->nsect_addr);
+                       lbal = ioread8(ioaddr->lbal_addr);
+                       if ((nsect == 1) && (lbal == 1))
+                               break;
+                       msleep(50);     /* give drive a breather */
+               }
+
                rc = ata_wait_ready(ap, deadline);
                if (rc) {
                        if (rc != -ENODEV)
@@ -3371,7 +3376,7 @@ int ata_std_prereset(struct ata_port *ap, unsigned long deadline)
         */
        if (!(ap->flags & ATA_FLAG_SKIP_D2H_BSY) && !ata_port_offline(ap)) {
                rc = ata_wait_ready(ap, deadline);
-               if (rc) {
+               if (rc && rc != -ENODEV) {
                        ata_port_printk(ap, KERN_WARNING, "device not ready "
                                        "(errno=%d), forcing hardreset\n", rc);
                        ehc->i.action |= ATA_EH_HARDRESET;
@@ -3654,7 +3659,7 @@ static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
 
 /**
  *     ata_dev_reread_id - Re-read IDENTIFY data
- *     @adev: target ATA device
+ *     @dev: target ATA device
  *     @readid_flags: read ID flags
  *
  *     Re-read IDENTIFY page and make sure @dev is still attached to
@@ -3771,6 +3776,8 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
        { "_NEC DV5800A",       NULL,           ATA_HORKAGE_NODMA },
        { "SAMSUNG CD-ROM SN-124","N001",       ATA_HORKAGE_NODMA },
+       { "Seagate STT20000A", NULL,            ATA_HORKAGE_NODMA },
+       { "IOMEGA  ZIP 250       ATAPI", NULL,  ATA_HORKAGE_NODMA }, /* temporary fix */
 
        /* Weird ATAPI devices */
        { "TORiSAN DVD-ROM DRD-N216", NULL,     ATA_HORKAGE_MAX_SEC_128 |
@@ -3785,6 +3792,7 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        { "FUJITSU MHT2060BH",  NULL,           ATA_HORKAGE_NONCQ },
        /* NCQ is broken */
        { "Maxtor 6L250S0",     "BANC1G10",     ATA_HORKAGE_NONCQ },
+       { "Maxtor 6B200M0",     "BANC1B10",     ATA_HORKAGE_NONCQ },
        /* NCQ hard hangs device under heavier load, needs hard power cycle */
        { "Maxtor 6B250S0",     "BANC1B70",     ATA_HORKAGE_NONCQ },
        /* Blacklist entries taken from Silicon Image 3124/3132
@@ -3792,6 +3800,10 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        { "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
        { "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
        { "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
+       /* Drives which do spurious command completion */
+       { "HTS541680J9SA00",    "SB2IC7EP",     ATA_HORKAGE_NONCQ, },
+       { "HTS541612J9SA00",    "SBDIC7JP",     ATA_HORKAGE_NONCQ, },
+       { "WDC WD740ADFD-00NLR1", NULL,         ATA_HORKAGE_NONCQ, },
 
        /* Devices with NCQ limits */
 
@@ -3934,10 +3946,13 @@ static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
        /* set up set-features taskfile */
        DPRINTK("set features - xfer mode\n");
 
+       /* Some controllers and ATAPI devices show flaky interrupt
+        * behavior after setting xfer mode.  Use polling instead.
+        */
        ata_tf_init(dev, &tf);
        tf.command = ATA_CMD_SET_FEATURES;
        tf.feature = SETFEATURES_XFER;
-       tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
+       tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
        tf.protocol = ATA_PROT_NODATA;
        tf.nsect = dev->xfer_mode;
 
@@ -4094,6 +4109,7 @@ static void ata_fill_sg(struct ata_queued_cmd *qc)
        if (idx)
                ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
 }
+
 /**
  *     ata_check_atapi_dma - Check whether ATAPI DMA can be supported
  *     @qc: Metadata associated with taskfile to check
@@ -4111,33 +4127,19 @@ static void ata_fill_sg(struct ata_queued_cmd *qc)
 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
-       int rc = 0; /* Assume ATAPI DMA is OK by default */
-
-       /* some drives can only do ATAPI DMA on read/write */
-       if (unlikely(qc->dev->horkage & ATA_HORKAGE_DMA_RW_ONLY)) {
-               struct scsi_cmnd *cmd = qc->scsicmd;
-               u8 *scsicmd = cmd->cmnd;
-
-               switch (scsicmd[0]) {
-               case READ_10:
-               case WRITE_10:
-               case READ_12:
-               case WRITE_12:
-               case READ_6:
-               case WRITE_6:
-                       /* atapi dma maybe ok */
-                       break;
-               default:
-                       /* turn off atapi dma */
-                       return 1;
-               }
-       }
+
+       /* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
+        * few ATAPI devices choke on such DMA requests.
+        */
+       if (unlikely(qc->nbytes & 15))
+               return 1;
 
        if (ap->ops->check_atapi_dma)
-               rc = ap->ops->check_atapi_dma(qc);
+               return ap->ops->check_atapi_dma(qc);
 
-       return rc;
+       return 0;
 }
+
 /**
  *     ata_qc_prep - Prepare taskfile for submission
  *     @qc: Metadata associated with taskfile to be prepared
@@ -5415,14 +5417,6 @@ unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
                }
        }
 
-       /* Some controllers show flaky interrupt behavior after
-        * setting xfer mode.  Use polling instead.
-        */
-       if (unlikely(qc->tf.command == ATA_CMD_SET_FEATURES &&
-                    qc->tf.feature == SETFEATURES_XFER) &&
-           (ap->flags & ATA_FLAG_SETXFER_POLLING))
-               qc->tf.flags |= ATA_TFLAG_POLLING;
-
        /* select the device */
        ata_dev_select(ap, qc->dev->devno, 1, 0);
 
@@ -6319,7 +6313,8 @@ int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
                /* init sata_spd_limit to the current value */
                if (sata_scr_read(ap, SCR_CONTROL, &scontrol) == 0) {
                        int spd = (scontrol >> 4) & 0xf;
-                       ap->hw_sata_spd_limit &= (1 << spd) - 1;
+                       if (spd)
+                               ap->hw_sata_spd_limit &= (1 << spd) - 1;
                }
                ap->sata_spd_limit = ap->hw_sata_spd_limit;
 
@@ -6439,6 +6434,9 @@ int ata_host_activate(struct ata_host *host, int irq,
        if (rc)
                devm_free_irq(host->dev, irq, host);
 
+       /* Used to print device info at probe */
+       host->irq = irq;
+
        return rc;
 }
 
@@ -6824,6 +6822,7 @@ EXPORT_SYMBOL_GPL(ata_check_status);
 EXPORT_SYMBOL_GPL(ata_altstatus);
 EXPORT_SYMBOL_GPL(ata_exec_command);
 EXPORT_SYMBOL_GPL(ata_port_start);
+EXPORT_SYMBOL_GPL(ata_sff_port_start);
 EXPORT_SYMBOL_GPL(ata_interrupt);
 EXPORT_SYMBOL_GPL(ata_do_set_mode);
 EXPORT_SYMBOL_GPL(ata_data_xfer);