Merge branch 'upstream-fixes'
[sfrench/cifs-2.6.git] / drivers / scsi / libata-core.c
index a0b6ca3d1a0c2f4dd7e5e8a094b4da9be78b25f5..70efde99f65220cb422a5689778587212e9cdcad 100644 (file)
@@ -62,7 +62,8 @@
 #include "libata.h"
 
 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
-static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
+static unsigned int ata_dev_init_params(struct ata_port *ap,
+                                       struct ata_device *dev);
 static void ata_set_mode(struct ata_port *ap);
 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
@@ -213,7 +214,7 @@ int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
        } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
                /* Unable to use DMA due to host limitation */
                tf->protocol = ATA_PROT_PIO;
-               index = dev->multi_count ? 0 : 4;
+               index = dev->multi_count ? 0 : 8;
        } else {
                tf->protocol = ATA_PROT_DMA;
                index = 16;
@@ -486,7 +487,7 @@ ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
 }
 
 /**
- *     ata_dev_id_string - Convert IDENTIFY DEVICE page into string
+ *     ata_id_string - Convert IDENTIFY DEVICE page into string
  *     @id: IDENTIFY DEVICE results we will examine
  *     @s: string into which data is output
  *     @ofs: offset into identify device page
@@ -500,8 +501,8 @@ ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
  *     caller.
  */
 
-void ata_dev_id_string(const u16 *id, unsigned char *s,
-                      unsigned int ofs, unsigned int len)
+void ata_id_string(const u16 *id, unsigned char *s,
+                  unsigned int ofs, unsigned int len)
 {
        unsigned int c;
 
@@ -520,27 +521,27 @@ void ata_dev_id_string(const u16 *id, unsigned char *s,
 }
 
 /**
- *     ata_dev_id_c_string - Convert IDENTIFY DEVICE page into C string
+ *     ata_id_c_string - Convert IDENTIFY DEVICE page into C string
  *     @id: IDENTIFY DEVICE results we will examine
  *     @s: string into which data is output
  *     @ofs: offset into identify device page
  *     @len: length of string to return. must be an odd number.
  *
- *     This function is identical to ata_dev_id_string except that it
+ *     This function is identical to ata_id_string except that it
  *     trims trailing spaces and terminates the resulting string with
  *     null.  @len must be actual maximum length (even number) + 1.
  *
  *     LOCKING:
  *     caller.
  */
-void ata_dev_id_c_string(const u16 *id, unsigned char *s,
-                        unsigned int ofs, unsigned int len)
+void ata_id_c_string(const u16 *id, unsigned char *s,
+                    unsigned int ofs, unsigned int len)
 {
        unsigned char *p;
 
        WARN_ON(!(len & 1));
 
-       ata_dev_id_string(id, s, ofs, len - 1);
+       ata_id_string(id, s, ofs, len - 1);
 
        p = s + strnlen(s, len - 1);
        while (p > s && p[-1] == ' ')
@@ -931,7 +932,7 @@ static void ata_dev_identify(struct ata_port *ap, unsigned int device)
        unsigned int using_edd;
        struct ata_taskfile tf;
        unsigned int err_mask;
-       int rc;
+       int i, rc;
 
        if (!ata_dev_present(dev)) {
                DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
@@ -939,7 +940,8 @@ static void ata_dev_identify(struct ata_port *ap, unsigned int device)
                return;
        }
 
-       if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
+       if (ap->ops->probe_reset ||
+           ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
                using_edd = 0;
        else
                using_edd = 1;
@@ -1040,7 +1042,12 @@ retry:
                 * Some drives were very specific about that exact sequence.
                 */
                if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
-                       ata_dev_init_params(ap, dev);
+                       err_mask = ata_dev_init_params(ap, dev);
+                       if (err_mask) {
+                               printk(KERN_ERR "ata%u: failed to init "
+                                      "parameters, disabled\n", ap->id);
+                               goto err_out;
+                       }
 
                        /* current CHS translation info (id[53-58]) might be
                         * changed. reread the identify device info.
@@ -1086,7 +1093,7 @@ retry:
 
                }
 
-               ap->host->max_cmd_len = 16;
+               dev->cdb_len = 16;
        }
 
        /* ATAPI-specific feature tests */
@@ -1099,8 +1106,7 @@ retry:
                        printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
                        goto err_out_nosup;
                }
-               ap->cdb_len = (unsigned int) rc;
-               ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
+               dev->cdb_len = (unsigned int) rc;
 
                /* print device info to dmesg */
                printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
@@ -1108,6 +1114,12 @@ retry:
                       ata_mode_string(xfer_modes));
        }
 
+       ap->host->max_cmd_len = 0;
+       for (i = 0; i < ATA_MAX_DEVICES; i++)
+               ap->host->max_cmd_len = max_t(unsigned int,
+                                             ap->host->max_cmd_len,
+                                             ap->device[i].cdb_len);
+
        DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
        return;
 
@@ -1120,9 +1132,10 @@ err_out:
 }
 
 
-static inline u8 ata_dev_knobble(const struct ata_port *ap)
+static inline u8 ata_dev_knobble(const struct ata_port *ap,
+                                struct ata_device *dev)
 {
-       return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
+       return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
 }
 
 /**
@@ -1136,13 +1149,11 @@ static inline u8 ata_dev_knobble(const struct ata_port *ap)
 void ata_dev_config(struct ata_port *ap, unsigned int i)
 {
        /* limit bridge transfers to udma5, 200 sectors */
-       if (ata_dev_knobble(ap)) {
+       if (ata_dev_knobble(ap, &ap->device[i])) {
                printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
-                       ap->id, ap->device->devno);
+                      ap->id, i);
                ap->udma_mask &= ATA_UDMA5;
-               ap->host->max_sectors = ATA_MAX_SECTORS;
-               ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
-               ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS;
+               ap->device[i].max_sectors = ATA_MAX_SECTORS;
        }
 
        if (ap->ops->dev_config)
@@ -2101,7 +2112,6 @@ int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
  *     This function is invoked after a successful reset.  Note that
  *     the device might have been reset more than once using
  *     different reset methods before postreset is invoked.
- *     postreset is also reponsible for setting cable type.
  *
  *     This function is to be used as standard callback for
  *     ata_drive_*_reset().
@@ -2113,7 +2123,7 @@ void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
 {
        DPRINTK("ENTER\n");
 
-       /* set cable type */
+       /* set cable type if it isn't already set */
        if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
                ap->cbl = ATA_CBL_SATA;
 
@@ -2310,8 +2320,7 @@ static int ata_dma_blacklisted(const struct ata_device *dev)
        unsigned char model_num[41];
        int i;
 
-       ata_dev_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS,
-                           sizeof(model_num));
+       ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
 
        for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
                if (!strcmp(ata_dma_blacklist[i], model_num))
@@ -2526,17 +2535,23 @@ err_out:
  *     @dev: Device to which command will be sent
  *
  *     LOCKING:
+ *     Kernel thread context (may sleep)
+ *
+ *     RETURNS:
+ *     0 on success, AC_ERR_* mask otherwise.
  */
 
-static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
+static unsigned int ata_dev_init_params(struct ata_port *ap,
+                                       struct ata_device *dev)
 {
        struct ata_taskfile tf;
+       unsigned int err_mask;
        u16 sectors = dev->id[6];
        u16 heads   = dev->id[3];
 
        /* Number of sectors per track 1-255. Number of heads 1-16 */
        if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
-               return;
+               return 0;
 
        /* set up init dev params taskfile */
        DPRINTK("init dev params \n");
@@ -2548,13 +2563,10 @@ static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
        tf.nsect = sectors;
        tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
 
-       if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
-               printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
-                      ap->id);
-               ata_port_disable(ap);
-       }
+       err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
 
-       DPRINTK("EXIT\n");
+       DPRINTK("EXIT, err_mask=%x\n", err_mask);
+       return err_mask;
 }
 
 /**
@@ -2578,7 +2590,7 @@ static void ata_sg_clean(struct ata_queued_cmd *qc)
        WARN_ON(sg == NULL);
 
        if (qc->flags & ATA_QCFLAG_SINGLE)
-               WARN_ON(qc->n_elem != 1);
+               WARN_ON(qc->n_elem > 1);
 
        VPRINTK("unmapping %u sg elements\n", qc->n_elem);
 
@@ -2601,7 +2613,7 @@ static void ata_sg_clean(struct ata_queued_cmd *qc)
                        kunmap_atomic(addr, KM_IRQ0);
                }
        } else {
-               if (sg_dma_len(&sg[0]) > 0)
+               if (qc->n_elem)
                        dma_unmap_single(ap->host_set->dev,
                                sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
                                dir);
@@ -2634,7 +2646,7 @@ static void ata_fill_sg(struct ata_queued_cmd *qc)
        unsigned int idx;
 
        WARN_ON(qc->__sg == NULL);
-       WARN_ON(qc->n_elem == 0);
+       WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
 
        idx = 0;
        ata_for_each_sg(sg, qc) {
@@ -2779,6 +2791,7 @@ static int ata_sg_setup_one(struct ata_queued_cmd *qc)
        int dir = qc->dma_dir;
        struct scatterlist *sg = qc->__sg;
        dma_addr_t dma_address;
+       int trim_sg = 0;
 
        /* we must lengthen transfers to end on a 32-bit boundary */
        qc->pad_len = sg->length & 3;
@@ -2798,13 +2811,15 @@ static int ata_sg_setup_one(struct ata_queued_cmd *qc)
                sg_dma_len(psg) = ATA_DMA_PAD_SZ;
                /* trim sg */
                sg->length -= qc->pad_len;
+               if (sg->length == 0)
+                       trim_sg = 1;
 
                DPRINTK("padding done, sg->length=%u pad_len=%u\n",
                        sg->length, qc->pad_len);
        }
 
-       if (!sg->length) {
-               sg_dma_address(sg) = 0;
+       if (trim_sg) {
+               qc->n_elem--;
                goto skip_map;
        }
 
@@ -2817,9 +2832,9 @@ static int ata_sg_setup_one(struct ata_queued_cmd *qc)
        }
 
        sg_dma_address(sg) = dma_address;
-skip_map:
        sg_dma_len(sg) = sg->length;
 
+skip_map:
        DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
                qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
 
@@ -3421,11 +3436,12 @@ static void ata_pio_error(struct ata_port *ap)
 {
        struct ata_queued_cmd *qc;
 
-       printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
-
        qc = ata_qc_from_tag(ap, ap->active_tag);
        WARN_ON(qc == NULL);
 
+       if (qc->tf.command != ATA_CMD_PACKET)
+               printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
+
        /* make sure qc->err_mask is available to 
         * know what's wrong and recover
         */
@@ -4201,7 +4217,7 @@ static void atapi_packet_task(void *_data)
 
        /* send SCSI cdb */
        DPRINTK("send cdb\n");
-       WARN_ON(ap->cdb_len < 12);
+       WARN_ON(qc->dev->cdb_len < 12);
 
        if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
            qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
@@ -4215,12 +4231,12 @@ static void atapi_packet_task(void *_data)
                 */
                spin_lock_irqsave(&ap->host_set->lock, flags);
                ap->flags &= ~ATA_FLAG_NOINTR;
-               ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
+               ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
                if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
                        ap->ops->bmdma_start(qc);       /* initiate bmdma */
                spin_unlock_irqrestore(&ap->host_set->lock, flags);
        } else {
-               ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
+               ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
 
                /* PIO commands are handled by polling */
                ap->hsm_task_state = HSM_ST;
@@ -4928,8 +4944,8 @@ EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
 EXPORT_SYMBOL_GPL(ata_scsi_release);
 EXPORT_SYMBOL_GPL(ata_host_intr);
 EXPORT_SYMBOL_GPL(ata_dev_classify);
-EXPORT_SYMBOL_GPL(ata_dev_id_string);
-EXPORT_SYMBOL_GPL(ata_dev_id_c_string);
+EXPORT_SYMBOL_GPL(ata_id_string);
+EXPORT_SYMBOL_GPL(ata_id_c_string);
 EXPORT_SYMBOL_GPL(ata_dev_config);
 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);