]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/ata/libata-core.c
libata: eliminate the home grown dma padding in favour of
[linux-2.6-omap-h63xx.git] / drivers / ata / libata-core.c
index 3011919f3ec88f0860d39445d596d5f454f604d6..3587ac3fe3f394bb2e9113672f1d165876ce6a6a 100644 (file)
@@ -3048,6 +3048,8 @@ int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
 static int ata_dev_set_mode(struct ata_device *dev)
 {
        struct ata_eh_context *ehc = &dev->link->eh_context;
+       const char *dev_err_whine = "";
+       int ign_dev_err = 0;
        unsigned int err_mask;
        int rc;
 
@@ -3057,41 +3059,57 @@ static int ata_dev_set_mode(struct ata_device *dev)
 
        err_mask = ata_dev_set_xfermode(dev);
 
+       if (err_mask & ~AC_ERR_DEV)
+               goto fail;
+
+       /* revalidate */
+       ehc->i.flags |= ATA_EHI_POST_SETMODE;
+       rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
+       ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
+       if (rc)
+               return rc;
+
        /* Old CFA may refuse this command, which is just fine */
        if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
-               err_mask &= ~AC_ERR_DEV;
+               ign_dev_err = 1;
 
        /* Some very old devices and some bad newer ones fail any kind of
           SET_XFERMODE request but support PIO0-2 timings and no IORDY */
        if (dev->xfer_shift == ATA_SHIFT_PIO && !ata_id_has_iordy(dev->id) &&
                        dev->pio_mode <= XFER_PIO_2)
-               err_mask &= ~AC_ERR_DEV;
+               ign_dev_err = 1;
 
        /* Early MWDMA devices do DMA but don't allow DMA mode setting.
           Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
        if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
            dev->dma_mode == XFER_MW_DMA_0 &&
            (dev->id[63] >> 8) & 1)
-               err_mask &= ~AC_ERR_DEV;
+               ign_dev_err = 1;
 
-       if (err_mask) {
-               ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
-                              "(err_mask=0x%x)\n", err_mask);
-               return -EIO;
-       }
+       /* if the device is actually configured correctly, ignore dev err */
+       if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
+               ign_dev_err = 1;
 
-       ehc->i.flags |= ATA_EHI_POST_SETMODE;
-       rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
-       ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
-       if (rc)
-               return rc;
+       if (err_mask & AC_ERR_DEV) {
+               if (!ign_dev_err)
+                       goto fail;
+               else
+                       dev_err_whine = " (device error ignored)";
+       }
 
        DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
                dev->xfer_shift, (int)dev->xfer_mode);
 
-       ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
-                      ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
+       ata_dev_printk(dev, KERN_INFO, "configured for %s%s\n",
+                      ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
+                      dev_err_whine);
+
        return 0;
+
+ fail:
+       ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
+                      "(err_mask=0x%x)\n", err_mask);
+       return -EIO;
 }
 
 /**
@@ -4172,6 +4190,7 @@ static const struct ata_blacklist_entry ata_device_blacklist [] = {
        /* Devices which report 1 sector over size HPA */
        { "ST340823A",          NULL,           ATA_HORKAGE_HPA_SIZE, },
        { "ST320413A",          NULL,           ATA_HORKAGE_HPA_SIZE, },
+       { "ST310211A",          NULL,           ATA_HORKAGE_HPA_SIZE, },
 
        /* Devices which get the IVB wrong */
        { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
@@ -4474,30 +4493,13 @@ void ata_sg_clean(struct ata_queued_cmd *qc)
        struct ata_port *ap = qc->ap;
        struct scatterlist *sg = qc->sg;
        int dir = qc->dma_dir;
-       void *pad_buf = NULL;
 
        WARN_ON(sg == NULL);
 
-       VPRINTK("unmapping %u sg elements\n", qc->mapped_n_elem);
+       VPRINTK("unmapping %u sg elements\n", qc->n_elem);
 
-       /* if we padded the buffer out to 32-bit bound, and data
-        * xfer direction is from-device, we must copy from the
-        * pad buffer back into the supplied buffer
-        */
-       if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
-               pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
-
-       if (qc->mapped_n_elem)
-               dma_unmap_sg(ap->dev, sg, qc->mapped_n_elem, dir);
-       /* restore last sg */
-       if (qc->last_sg)
-               *qc->last_sg = qc->saved_last_sg;
-       if (pad_buf) {
-               struct scatterlist *psg = &qc->extra_sg[1];
-               void *addr = kmap_atomic(sg_page(psg), KM_IRQ0);
-               memcpy(addr + psg->offset, pad_buf, qc->pad_len);
-               kunmap_atomic(addr, KM_IRQ0);
-       }
+       if (qc->n_elem)
+               dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
 
        qc->flags &= ~ATA_QCFLAG_DMAMAP;
        qc->sg = NULL;
@@ -4656,24 +4658,9 @@ int ata_check_atapi_dma(struct ata_queued_cmd *qc)
  */
 static int atapi_qc_may_overflow(struct ata_queued_cmd *qc)
 {
-       if (qc->tf.protocol != ATAPI_PROT_PIO &&
-           qc->tf.protocol != ATAPI_PROT_DMA)
-               return 0;
-
-       if (qc->tf.flags & ATA_TFLAG_WRITE)
-               return 0;
-
-       switch (qc->cdb[0]) {
-       case READ_10:
-       case READ_12:
-       case WRITE_10:
-       case WRITE_12:
-       case GPCMD_READ_CD:
-       case GPCMD_READ_CD_MSF:
-               return 0;
-       }
-
-       return 1;
+       return ata_is_atapi(qc->tf.protocol) && ata_is_data(qc->tf.protocol) &&
+               atapi_cmd_type(qc->cdb[0]) == ATAPI_MISC &&
+               !(qc->tf.flags & ATA_TFLAG_WRITE);
 }
 
 /**
@@ -4763,97 +4750,6 @@ void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
        qc->cursg = qc->sg;
 }
 
-static unsigned int ata_sg_setup_extra(struct ata_queued_cmd *qc,
-                                      unsigned int *n_elem_extra,
-                                      unsigned int *nbytes_extra)
-{
-       struct ata_port *ap = qc->ap;
-       unsigned int n_elem = qc->n_elem;
-       struct scatterlist *lsg, *copy_lsg = NULL, *tsg = NULL, *esg = NULL;
-
-       *n_elem_extra = 0;
-       *nbytes_extra = 0;
-
-       /* needs padding? */
-       qc->pad_len = qc->nbytes & 3;
-
-       if (likely(!qc->pad_len))
-               return n_elem;
-
-       /* locate last sg and save it */
-       lsg = sg_last(qc->sg, n_elem);
-       qc->last_sg = lsg;
-       qc->saved_last_sg = *lsg;
-
-       sg_init_table(qc->extra_sg, ARRAY_SIZE(qc->extra_sg));
-
-       if (qc->pad_len) {
-               struct scatterlist *psg = &qc->extra_sg[1];
-               void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
-               unsigned int offset;
-
-               WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
-
-               memset(pad_buf, 0, ATA_DMA_PAD_SZ);
-
-               /* psg->page/offset are used to copy to-be-written
-                * data in this function or read data in ata_sg_clean.
-                */
-               offset = lsg->offset + lsg->length - qc->pad_len;
-               sg_set_page(psg, nth_page(sg_page(lsg), offset >> PAGE_SHIFT),
-                           qc->pad_len, offset_in_page(offset));
-
-               if (qc->tf.flags & ATA_TFLAG_WRITE) {
-                       void *addr = kmap_atomic(sg_page(psg), KM_IRQ0);
-                       memcpy(pad_buf, addr + psg->offset, qc->pad_len);
-                       kunmap_atomic(addr, KM_IRQ0);
-               }
-
-               sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
-               sg_dma_len(psg) = ATA_DMA_PAD_SZ;
-
-               /* Trim the last sg entry and chain the original and
-                * padding sg lists.
-                *
-                * Because chaining consumes one sg entry, one extra
-                * sg entry is allocated and the last sg entry is
-                * copied to it if the length isn't zero after padded
-                * amount is removed.
-                *
-                * If the last sg entry is completely replaced by
-                * padding sg entry, the first sg entry is skipped
-                * while chaining.
-                */
-               lsg->length -= qc->pad_len;
-               if (lsg->length) {
-                       copy_lsg = &qc->extra_sg[0];
-                       tsg = &qc->extra_sg[0];
-               } else {
-                       n_elem--;
-                       tsg = &qc->extra_sg[1];
-               }
-
-               esg = &qc->extra_sg[1];
-
-               (*n_elem_extra)++;
-               (*nbytes_extra) += 4 - qc->pad_len;
-       }
-
-       if (copy_lsg)
-               sg_set_page(copy_lsg, sg_page(lsg), lsg->length, lsg->offset);
-
-       sg_chain(lsg, 1, tsg);
-       sg_mark_end(esg);
-
-       /* sglist can't start with chaining sg entry, fast forward */
-       if (qc->sg == lsg) {
-               qc->sg = tsg;
-               qc->cursg = tsg;
-       }
-
-       return n_elem;
-}
-
 /**
  *     ata_sg_setup - DMA-map the scatter-gather table associated with a command.
  *     @qc: Command with scatter-gather table to be mapped.
@@ -4870,26 +4766,17 @@ static unsigned int ata_sg_setup_extra(struct ata_queued_cmd *qc,
 static int ata_sg_setup(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
-       unsigned int n_elem, n_elem_extra, nbytes_extra;
+       unsigned int n_elem;
 
        VPRINTK("ENTER, ata%u\n", ap->print_id);
 
-       n_elem = ata_sg_setup_extra(qc, &n_elem_extra, &nbytes_extra);
+       n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
+       if (n_elem < 1)
+               return -1;
 
-       if (n_elem) {
-               n_elem = dma_map_sg(ap->dev, qc->sg, n_elem, qc->dma_dir);
-               if (n_elem < 1) {
-                       /* restore last sg */
-                       if (qc->last_sg)
-                               *qc->last_sg = qc->saved_last_sg;
-                       return -1;
-               }
-               DPRINTK("%d sg elements mapped\n", n_elem);
-       }
+       DPRINTK("%d sg elements mapped\n", n_elem);
 
-       qc->n_elem = qc->mapped_n_elem = n_elem;
-       qc->n_elem += n_elem_extra;
-       qc->nbytes += nbytes_extra;
+       qc->n_elem = n_elem;
        qc->flags |= ATA_QCFLAG_DMAMAP;
 
        return 0;
@@ -5127,13 +5014,14 @@ static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
  */
 static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
 {
-       int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
+       int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
        struct ata_port *ap = qc->ap;
-       struct ata_eh_info *ehi = &qc->dev->link->eh_info;
+       struct ata_device *dev = qc->dev;
+       struct ata_eh_info *ehi = &dev->link->eh_info;
        struct scatterlist *sg;
        struct page *page;
        unsigned char *buf;
-       unsigned int offset, count;
+       unsigned int offset, count, consumed;
 
 next_sg:
        sg = qc->cursg;
@@ -5146,26 +5034,27 @@ next_sg:
                 *    - for write case, padding zero data to the device
                 */
                u16 pad_buf[1] = { 0 };
-               unsigned int i;
 
-               if (bytes > qc->curbytes - qc->nbytes + ATAPI_MAX_DRAIN) {
+               if (qc->curbytes + bytes > qc->nbytes + ATAPI_MAX_DRAIN) {
                        ata_ehi_push_desc(ehi, "too much trailing data "
                                          "buf=%u cur=%u bytes=%u",
                                          qc->nbytes, qc->curbytes, bytes);
                        return -1;
                }
 
-                /* overflow is exptected for misc ATAPI commands */
-               if (bytes && !atapi_qc_may_overflow(qc))
-                       ata_dev_printk(qc->dev, KERN_WARNING, "ATAPI %u bytes "
-                                      "trailing data (cdb=%02x nbytes=%u)\n",
-                                      bytes, qc->cdb[0], qc->nbytes);
+               /* allow overflow only for misc ATAPI commands */
+               if (!atapi_qc_may_overflow(qc)) {
+                       ata_ehi_push_desc(ehi, "unexpected trailing data "
+                                         "%u bytes", bytes);
+                       return -1;
+               }
 
-               for (i = 0; i < (bytes + 1) / 2; i++)
-                       ap->ops->data_xfer(qc->dev, (unsigned char *)pad_buf, 2, do_write);
+               consumed = 0;
+               while (consumed < bytes)
+                       consumed += ap->ops->data_xfer(dev,
+                                       (unsigned char *)pad_buf, 2, rw);
 
                qc->curbytes += bytes;
-
                return 0;
        }
 
@@ -5192,18 +5081,16 @@ next_sg:
                buf = kmap_atomic(page, KM_IRQ0);
 
                /* do the actual data transfer */
-               ap->ops->data_xfer(qc->dev,  buf + offset, count, do_write);
+               consumed = ap->ops->data_xfer(dev,  buf + offset, count, rw);
 
                kunmap_atomic(buf, KM_IRQ0);
                local_irq_restore(flags);
        } else {
                buf = page_address(page);
-               ap->ops->data_xfer(qc->dev,  buf + offset, count, do_write);
+               consumed = ap->ops->data_xfer(dev,  buf + offset, count, rw);
        }
 
-       bytes -= count;
-       if ((count & 1) && bytes)
-               bytes--;
+       bytes -= min(bytes, consumed);
        qc->curbytes += count;
        qc->cursg_ofs += count;
 
@@ -5212,9 +5099,11 @@ next_sg:
                qc->cursg_ofs = 0;
        }
 
+       /* consumed can be larger than count only for the last transfer */
+       WARN_ON(qc->cursg && count != consumed);
+
        if (bytes)
                goto next_sg;
-
        return 0;
 }
 
@@ -5232,6 +5121,7 @@ static void atapi_pio_bytes(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
        struct ata_device *dev = qc->dev;
+       struct ata_eh_info *ehi = &dev->link->eh_info;
        unsigned int ireason, bc_lo, bc_hi, bytes;
        int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
 
@@ -5249,26 +5139,28 @@ static void atapi_pio_bytes(struct ata_queued_cmd *qc)
 
        /* shall be cleared to zero, indicating xfer of data */
        if (unlikely(ireason & (1 << 0)))
-               goto err_out;
+               goto atapi_check;
 
        /* make sure transfer direction matches expected */
        i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
        if (unlikely(do_write != i_write))
-               goto err_out;
+               goto atapi_check;
 
        if (unlikely(!bytes))
-               goto err_out;
+               goto atapi_check;
 
        VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
 
-       if (__atapi_pio_bytes(qc, bytes))
+       if (unlikely(__atapi_pio_bytes(qc, bytes)))
                goto err_out;
        ata_altstatus(ap); /* flush */
 
        return;
 
-err_out:
-       ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
+ atapi_check:
+       ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
+                         ireason, bytes);
+ err_out:
        qc->err_mask |= AC_ERR_HSM;
        ap->hsm_task_state = HSM_ST_ERR;
 }
@@ -5953,9 +5845,6 @@ void ata_qc_issue(struct ata_queued_cmd *qc)
         */
        BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
 
-       /* ata_sg_setup() may update nbytes */
-       qc->raw_nbytes = qc->nbytes;
-
        if (ata_is_dma(prot) || (ata_is_pio(prot) &&
                                 (ap->flags & ATA_FLAG_PIO_DMA)))
                if (ata_sg_setup(qc))
@@ -6564,19 +6453,12 @@ void ata_host_resume(struct ata_host *host)
 int ata_port_start(struct ata_port *ap)
 {
        struct device *dev = ap->dev;
-       int rc;
 
        ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
                                      GFP_KERNEL);
        if (!ap->prd)
                return -ENOMEM;
 
-       rc = ata_pad_alloc(ap, dev);
-       if (rc)
-               return rc;
-
-       DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd,
-               (unsigned long long)ap->prd_dma);
        return 0;
 }
 
@@ -7068,7 +6950,6 @@ int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
        DPRINTK("probe begin\n");
        for (i = 0; i < host->n_ports; i++) {
                struct ata_port *ap = host->ports[i];
-               int rc;
 
                /* probe */
                if (ap->ops->error_handler) {