Merge branch 'upstream'
authorJeff Garzik <jgarzik@pobox.com>
Wed, 19 Oct 2005 01:23:11 +0000 (21:23 -0400)
committerJeff Garzik <jgarzik@pobox.com>
Wed, 19 Oct 2005 01:23:11 +0000 (21:23 -0400)
1  2 
drivers/scsi/libata-core.c
include/linux/ata.h
include/linux/libata.h

@@@ -63,6 -63,7 +63,7 @@@
  static unsigned int ata_busy_sleep (struct ata_port *ap,
                                    unsigned long tmout_pat,
                                    unsigned long tmout);
+ 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 void ata_set_mode(struct ata_port *ap);
  static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
@@@ -72,7 -73,6 +73,7 @@@ static int ata_choose_xfer_mode(struct 
                                u8 *xfer_mode_out,
                                unsigned int *xfer_shift_out);
  static void __ata_qc_complete(struct ata_queued_cmd *qc);
 +static void ata_pio_error(struct ata_port *ap);
  
  static unsigned int ata_unique_id = 1;
  static struct workqueue_struct *ata_wq;
@@@ -617,79 -617,53 +618,53 @@@ void ata_tf_from_fis(u8 *fis, struct at
        tf->hob_nsect   = fis[13];
  }
  
- /**
-  *    ata_prot_to_cmd - determine which read/write opcodes to use
-  *    @protocol: ATA_PROT_xxx taskfile protocol
-  *    @lba48: true is lba48 is present
-  *
-  *    Given necessary input, determine which read/write commands
-  *    to use to transfer data.
-  *
-  *    LOCKING:
-  *    None.
-  */
- static int ata_prot_to_cmd(int protocol, int lba48)
- {
-       int rcmd = 0, wcmd = 0;
-       switch (protocol) {
-       case ATA_PROT_PIO:
-               if (lba48) {
-                       rcmd = ATA_CMD_PIO_READ_EXT;
-                       wcmd = ATA_CMD_PIO_WRITE_EXT;
-               } else {
-                       rcmd = ATA_CMD_PIO_READ;
-                       wcmd = ATA_CMD_PIO_WRITE;
-               }
-               break;
-       case ATA_PROT_DMA:
-               if (lba48) {
-                       rcmd = ATA_CMD_READ_EXT;
-                       wcmd = ATA_CMD_WRITE_EXT;
-               } else {
-                       rcmd = ATA_CMD_READ;
-                       wcmd = ATA_CMD_WRITE;
-               }
-               break;
-       default:
-               return -1;
-       }
-       return rcmd | (wcmd << 8);
- }
+ static const u8 ata_rw_cmds[] = {
+       /* pio multi */
+       ATA_CMD_READ_MULTI,
+       ATA_CMD_WRITE_MULTI,
+       ATA_CMD_READ_MULTI_EXT,
+       ATA_CMD_WRITE_MULTI_EXT,
+       /* pio */
+       ATA_CMD_PIO_READ,
+       ATA_CMD_PIO_WRITE,
+       ATA_CMD_PIO_READ_EXT,
+       ATA_CMD_PIO_WRITE_EXT,
+       /* dma */
+       ATA_CMD_READ,
+       ATA_CMD_WRITE,
+       ATA_CMD_READ_EXT,
+       ATA_CMD_WRITE_EXT
+ };
  
  /**
-  *    ata_dev_set_protocol - set taskfile protocol and r/w commands
-  *    @dev: device to examine and configure
+  *    ata_rwcmd_protocol - set taskfile r/w commands and protocol
+  *    @qc: command to examine and configure
   *
-  *    Examine the device configuration, after we have
-  *    read the identify-device page and configured the
-  *    data transfer mode.  Set internal state related to
-  *    the ATA taskfile protocol (pio, pio mult, dma, etc.)
-  *    and calculate the proper read/write commands to use.
+  *    Examine the device configuration and tf->flags to calculate 
+  *    the proper read/write commands and protocol to use.
   *
   *    LOCKING:
   *    caller.
   */
static void ata_dev_set_protocol(struct ata_device *dev)
void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
  {
-       int pio = (dev->flags & ATA_DFLAG_PIO);
-       int lba48 = (dev->flags & ATA_DFLAG_LBA48);
-       int proto, cmd;
+       struct ata_taskfile *tf = &qc->tf;
+       struct ata_device *dev = qc->dev;
  
-       if (pio)
-               proto = dev->xfer_protocol = ATA_PROT_PIO;
-       else
-               proto = dev->xfer_protocol = ATA_PROT_DMA;
+       int index, lba48, write;
+  
+       lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
+       write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
  
-       cmd = ata_prot_to_cmd(proto, lba48);
-       if (cmd < 0)
-               BUG();
+       if (dev->flags & ATA_DFLAG_PIO) {
+               tf->protocol = ATA_PROT_PIO;
+               index = dev->multi_count ? 0 : 4;
+       } else {
+               tf->protocol = ATA_PROT_DMA;
+               index = 8;
+       }
  
-       dev->read_cmd = cmd & 0xff;
-       dev->write_cmd = (cmd >> 8) & 0xff;
+       tf->command = ata_rw_cmds[index + lba48 + write];
  }
  
  static const char * xfer_mode_str[] = {
@@@ -1267,9 -1241,15 +1242,15 @@@ retry
                 * anything else..
                 * Some drives were very specific about that exact sequence.
                 */
-               if (major_version < 4 || (!ata_id_has_lba(dev->id)))
+               if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
                        ata_dev_init_params(ap, dev);
  
+                       /* current CHS translation info (id[53-58]) might be
+                        * changed. reread the identify device info.
+                        */
+                       ata_dev_reread_id(ap, dev);
+               }
                if (ata_id_has_lba(dev->id)) {
                        dev->flags |= ATA_DFLAG_LBA;
  
                ap->cdb_len = (unsigned int) rc;
                ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
  
 +              if (ata_id_cdb_intr(dev->id))
 +                      dev->flags |= ATA_DFLAG_CDB_INTR;
 +
                /* print device info to dmesg */
                printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
                       ap->id, device,
@@@ -1645,7 -1622,7 +1626,7 @@@ static void ata_host_set_dma(struct ata
   */
  static void ata_set_mode(struct ata_port *ap)
  {
-       unsigned int i, xfer_shift;
+       unsigned int xfer_shift;
        u8 xfer_mode;
        int rc;
  
        if (ap->ops->post_set_mode)
                ap->ops->post_set_mode(ap);
  
-       for (i = 0; i < 2; i++) {
-               struct ata_device *dev = &ap->device[i];
-               ata_dev_set_protocol(dev);
-       }
        return;
  
  err_out:
@@@ -2185,6 -2157,62 +2161,62 @@@ static void ata_dev_set_xfermode(struc
        DPRINTK("EXIT\n");
  }
  
+ /**
+  *    ata_dev_reread_id - Reread the device identify device info
+  *    @ap: port where the device is
+  *    @dev: device to reread the identify device info
+  *
+  *    LOCKING:
+  */
+ static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
+ {
+       DECLARE_COMPLETION(wait);
+       struct ata_queued_cmd *qc;
+       unsigned long flags;
+       int rc;
+       qc = ata_qc_new_init(ap, dev);
+       BUG_ON(qc == NULL);
+       ata_sg_init_one(qc, dev->id, sizeof(dev->id));
+       qc->dma_dir = DMA_FROM_DEVICE;
+       if (dev->class == ATA_DEV_ATA) {
+               qc->tf.command = ATA_CMD_ID_ATA;
+               DPRINTK("do ATA identify\n");
+       } else {
+               qc->tf.command = ATA_CMD_ID_ATAPI;
+               DPRINTK("do ATAPI identify\n");
+       }
+       qc->tf.flags |= ATA_TFLAG_DEVICE;
+       qc->tf.protocol = ATA_PROT_PIO;
+       qc->nsect = 1;
+       qc->waiting = &wait;
+       qc->complete_fn = ata_qc_complete_noop;
+       spin_lock_irqsave(&ap->host_set->lock, flags);
+       rc = ata_qc_issue(qc);
+       spin_unlock_irqrestore(&ap->host_set->lock, flags);
+       if (rc)
+               goto err_out;
+       wait_for_completion(&wait);
+       swap_buf_le16(dev->id, ATA_ID_WORDS);
+       ata_dump_id(dev);
+       DPRINTK("EXIT\n");
+       return;
+ err_out:
+       ata_port_disable(ap);
+ }
  /**
   *    ata_dev_init_params - Issue INIT DEV PARAMS command
   *    @ap: Port associated with device @dev
@@@ -2495,6 -2523,7 +2527,6 @@@ void ata_poll_qc_complete(struct ata_qu
        unsigned long flags;
  
        spin_lock_irqsave(&ap->host_set->lock, flags);
 -      ap->flags &= ~ATA_FLAG_NOINTR;
        ata_irq_on(ap);
        ata_qc_complete(qc, drv_stat);
        spin_unlock_irqrestore(&ap->host_set->lock, flags);
@@@ -2760,23 -2789,7 +2792,23 @@@ static void ata_pio_sector(struct ata_q
        page = nth_page(page, (offset >> PAGE_SHIFT));
        offset %= PAGE_SIZE;
  
 -      buf = kmap(page) + offset;
 +      DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
 +
 +      if (PageHighMem(page)) {
 +              unsigned long flags;
 +
 +              local_irq_save(flags);
 +              buf = kmap_atomic(page, KM_IRQ0);
 +
 +              /* do the actual data transfer */
 +              ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
 +
 +              kunmap_atomic(buf, KM_IRQ0);
 +              local_irq_restore(flags);
 +      } else {
 +              buf = page_address(page);
 +              ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
 +      }
  
        qc->cursect++;
        qc->cursg_ofs++;
                qc->cursg++;
                qc->cursg_ofs = 0;
        }
 +}
  
 -      DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
 +/**
 + *    atapi_send_cdb - Write CDB bytes to hardware
 + *    @ap: Port to which ATAPI device is attached.
 + *    @qc: Taskfile currently active
 + *
 + *    When device has indicated its readiness to accept
 + *    a CDB, this function is called.  Send the CDB.
 + *
 + *    LOCKING:
 + *    caller.
 + */
 +
 +static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
 +{
 +      /* send SCSI cdb */
 +      DPRINTK("send cdb\n");
 +      assert(ap->cdb_len >= 12);
 +
 +      ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
 +      ata_altstatus(ap); /* flush */
 +
 +      switch (qc->tf.protocol) {
 +      case ATA_PROT_ATAPI:
 +              ap->hsm_task_state = HSM_ST;
 +              break;
 +      case ATA_PROT_ATAPI_NODATA:
 +              ap->hsm_task_state = HSM_ST_LAST;
 +              break;
 +      case ATA_PROT_ATAPI_DMA:
 +              ap->hsm_task_state = HSM_ST_LAST;
 +              /* initiate bmdma */
 +              ap->ops->bmdma_start(qc);
 +              break;
 +      }
 +}
 +
 +/**
 + *    ata_dataout_task - Write first data block to hardware
 + *    @_data: Port to which ATA/ATAPI device is attached.
 + *
 + *    When device has indicated its readiness to accept
 + *    the data, this function sends out the CDB or 
 + *    the first data block by PIO.
 + *    After this, 
 + *      - If polling, ata_pio_task() handles the rest.
 + *      - Otherwise, interrupt handler takes over.
 + *
 + *    LOCKING:
 + *    Kernel thread context (may sleep)
 + */
 +
 +static void ata_dataout_task(void *_data)
 +{
 +      struct ata_port *ap = _data;
 +      struct ata_queued_cmd *qc;
 +      u8 status;
 +      unsigned long flags;
 +
 +      qc = ata_qc_from_tag(ap, ap->active_tag);
 +      assert(qc != NULL);
 +      assert(qc->flags & ATA_QCFLAG_ACTIVE);
 +
 +      /* sleep-wait for BSY to clear */
 +      DPRINTK("busy wait\n");
 +      if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT))
 +              goto err_out;
  
 -      /* do the actual data transfer */
 -      do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
 -      ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
 +      /* make sure DRQ is set */
 +      status = ata_chk_status(ap);
 +      if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
 +              goto err_out;
 +
 +      /* Send the CDB (atapi) or the first data block (ata pio out).
 +       * During the state transition, interrupt handler shouldn't
 +       * be invoked before the data transfer is complete and
 +       * hsm_task_state is changed. Hence, the following locking.
 +       */
 +      spin_lock_irqsave(&ap->host_set->lock, flags);
 +
 +      if (qc->tf.protocol == ATA_PROT_PIO) {
 +              /* PIO data out protocol.
 +               * send first data block.
 +               */
 +
 +              /* ata_pio_sector() might change the state to HSM_ST_LAST.
 +               * so, the state is changed here before ata_pio_sector().
 +               */
 +              ap->hsm_task_state = HSM_ST;
 +              ata_pio_sector(qc);
 +              ata_altstatus(ap); /* flush */
 +      } else
 +              /* send CDB */
 +              atapi_send_cdb(ap, qc);
 +
 +      /* if polling, ata_pio_task() handles the rest.
 +       * otherwise, interrupt handler takes over from here.
 +       */
 +      if (qc->tf.flags & ATA_TFLAG_POLLING)
 +              queue_work(ata_wq, &ap->pio_task);
 +
 +      spin_unlock_irqrestore(&ap->host_set->lock, flags);
  
 -      kunmap(page);
 +      return;
 +
 +err_out:
 +      ata_pio_error(ap);
  }
  
  /**
@@@ -2958,23 -2871,7 +2990,23 @@@ next_sg
        /* don't cross page boundaries */
        count = min(count, (unsigned int)PAGE_SIZE - offset);
  
 -      buf = kmap(page) + offset;
 +      DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
 +
 +      if (PageHighMem(page)) {
 +              unsigned long flags;
 +
 +              local_irq_save(flags);
 +              buf = kmap_atomic(page, KM_IRQ0);
 +
 +              /* do the actual data transfer */
 +              ata_data_xfer(ap, buf + offset, count, do_write);
 +
 +              kunmap_atomic(buf, KM_IRQ0);
 +              local_irq_restore(flags);
 +      } else {
 +              buf = page_address(page);
 +              ata_data_xfer(ap, buf + offset, count, do_write);
 +      }
  
        bytes -= count;
        qc->curbytes += count;
                qc->cursg_ofs = 0;
        }
  
 -      DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
 -
 -      /* do the actual data transfer */
 -      ata_data_xfer(ap, buf, count, do_write);
 -
 -      kunmap(page);
 -
        if (bytes)
                goto next_sg;
  }
@@@ -3022,8 -2926,6 +3054,8 @@@ static void atapi_pio_bytes(struct ata_
        if (do_write != i_write)
                goto err_out;
  
 +      VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
 +
        __atapi_pio_bytes(qc, bytes);
  
        return;
@@@ -3224,8 -3126,6 +3256,8 @@@ static void ata_qc_timeout(struct ata_q
                printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
                       ap->id, qc->tf.command, drv_stat, host_stat);
  
 +              ap->hsm_task_state = HSM_ST_IDLE;
 +
                /* complete taskfile transaction */
                ata_qc_complete(qc, drv_stat);
                break;
@@@ -3327,13 -3227,6 +3359,6 @@@ struct ata_queued_cmd *ata_qc_new_init(
                qc->nbytes = qc->curbytes = 0;
  
                ata_tf_init(ap, &qc->tf, dev->devno);
-               if (dev->flags & ATA_DFLAG_LBA) {
-                       qc->tf.flags |= ATA_TFLAG_LBA;
-                       if (dev->flags & ATA_DFLAG_LBA48)
-                               qc->tf.flags |= ATA_TFLAG_LBA48;
-               }
        }
  
        return qc;
@@@ -3520,103 -3413,43 +3545,103 @@@ int ata_qc_issue_prot(struct ata_queued
  {
        struct ata_port *ap = qc->ap;
  
 +      /* Use polling pio if the LLD doesn't handle
 +       * interrupt driven pio and atapi CDB interrupt.
 +       */
 +      if (ap->flags & ATA_FLAG_PIO_POLLING) {
 +              switch (qc->tf.protocol) {
 +              case ATA_PROT_PIO:
 +              case ATA_PROT_ATAPI:
 +              case ATA_PROT_ATAPI_NODATA:
 +                      qc->tf.flags |= ATA_TFLAG_POLLING;
 +                      break;
 +              case ATA_PROT_ATAPI_DMA:
 +                      if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
 +                              BUG();
 +                      break;
 +              default:
 +                      break;
 +              }
 +      }
 +
 +      /* select the device */
        ata_dev_select(ap, qc->dev->devno, 1, 0);
  
 +      /* start the command */
        switch (qc->tf.protocol) {
        case ATA_PROT_NODATA:
 +              if (qc->tf.flags & ATA_TFLAG_POLLING)
 +                      ata_qc_set_polling(qc);
 +
                ata_tf_to_host_nolock(ap, &qc->tf);
 +              ap->hsm_task_state = HSM_ST_LAST;
 +
 +              if (qc->tf.flags & ATA_TFLAG_POLLING)
 +                      queue_work(ata_wq, &ap->pio_task);
 +
                break;
  
        case ATA_PROT_DMA:
 +              assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
 +
                ap->ops->tf_load(ap, &qc->tf);   /* load tf registers */
                ap->ops->bmdma_setup(qc);           /* set up bmdma */
                ap->ops->bmdma_start(qc);           /* initiate bmdma */
 +              ap->hsm_task_state = HSM_ST_LAST;
                break;
  
 -      case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
 -              ata_qc_set_polling(qc);
 -              ata_tf_to_host_nolock(ap, &qc->tf);
 -              ap->hsm_task_state = HSM_ST;
 -              queue_work(ata_wq, &ap->pio_task);
 -              break;
 +      case ATA_PROT_PIO:
 +              if (qc->tf.flags & ATA_TFLAG_POLLING)
 +                      ata_qc_set_polling(qc);
  
 -      case ATA_PROT_ATAPI:
 -              ata_qc_set_polling(qc);
                ata_tf_to_host_nolock(ap, &qc->tf);
 -              queue_work(ata_wq, &ap->packet_task);
 +
 +              if (qc->tf.flags & ATA_TFLAG_WRITE) {
 +                      /* PIO data out protocol */
 +                      ap->hsm_task_state = HSM_ST_FIRST;
 +                      queue_work(ata_wq, &ap->dataout_task);
 +
 +                      /* always send first data block using
 +                       * the ata_dataout_task() codepath.
 +                       */
 +              } else {
 +                      /* PIO data in protocol */
 +                      ap->hsm_task_state = HSM_ST;
 +
 +                      if (qc->tf.flags & ATA_TFLAG_POLLING)
 +                              queue_work(ata_wq, &ap->pio_task);
 +
 +                      /* if polling, ata_pio_task() handles the rest.
 +                       * otherwise, interrupt handler takes over from here.
 +                       */
 +              }
 +
                break;
  
 +      case ATA_PROT_ATAPI:
        case ATA_PROT_ATAPI_NODATA:
 -              ap->flags |= ATA_FLAG_NOINTR;
 +              if (qc->tf.flags & ATA_TFLAG_POLLING)
 +                      ata_qc_set_polling(qc);
 +
                ata_tf_to_host_nolock(ap, &qc->tf);
 -              queue_work(ata_wq, &ap->packet_task);
 +              ap->hsm_task_state = HSM_ST_FIRST;
 +
 +              /* send cdb by polling if no cdb interrupt */
 +              if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
 +                  (qc->tf.flags & ATA_TFLAG_POLLING))
 +                      queue_work(ata_wq, &ap->dataout_task);
                break;
  
        case ATA_PROT_ATAPI_DMA:
 -              ap->flags |= ATA_FLAG_NOINTR;
 +              assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
 +
                ap->ops->tf_load(ap, &qc->tf);   /* load tf registers */
                ap->ops->bmdma_setup(qc);           /* set up bmdma */
 -              queue_work(ata_wq, &ap->packet_task);
 +              ap->hsm_task_state = HSM_ST_FIRST;
 +
 +              /* send cdb by polling if no cdb interrupt */
 +              if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
 +                      queue_work(ata_wq, &ap->dataout_task);
                break;
  
        default:
@@@ -3877,142 -3710,47 +3902,142 @@@ void ata_bmdma_stop(struct ata_queued_c
  inline unsigned int ata_host_intr (struct ata_port *ap,
                                   struct ata_queued_cmd *qc)
  {
 -      u8 status, host_stat;
 -
 -      switch (qc->tf.protocol) {
 +      u8 status, host_stat = 0;
  
 -      case ATA_PROT_DMA:
 -      case ATA_PROT_ATAPI_DMA:
 -      case ATA_PROT_ATAPI:
 -              /* check status of DMA engine */
 -              host_stat = ap->ops->bmdma_status(ap);
 -              VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
 +      VPRINTK("ata%u: protocol %d task_state %d\n",
 +              ap->id, qc->tf.protocol, ap->hsm_task_state);
  
 -              /* if it's not our irq... */
 -              if (!(host_stat & ATA_DMA_INTR))
 +      /* Check whether we are expecting interrupt in this state */
 +      switch (ap->hsm_task_state) {
 +      case HSM_ST_FIRST:
 +              /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
 +               * The flag was turned on only for atapi devices.
 +               * No need to check is_atapi_taskfile(&qc->tf) again.
 +               */
 +              if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
                        goto idle_irq;
 +              break;
 +      case HSM_ST_LAST:
 +              if (qc->tf.protocol == ATA_PROT_DMA ||
 +                  qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
 +                      /* check status of DMA engine */
 +                      host_stat = ap->ops->bmdma_status(ap);
 +                      VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
 +
 +                      /* if it's not our irq... */
 +                      if (!(host_stat & ATA_DMA_INTR))
 +                              goto idle_irq;
 +
 +                      /* before we do anything else, clear DMA-Start bit */
 +                      ap->ops->bmdma_stop(qc);
 +              }
 +              break;
 +      case HSM_ST:
 +              break;
 +      default:
 +              goto idle_irq;
 +      }
  
 -              /* before we do anything else, clear DMA-Start bit */
 -              ap->ops->bmdma_stop(qc);
 +      /* check altstatus */
 +      status = ata_altstatus(ap);
 +      if (status & ATA_BUSY)
 +              goto idle_irq;
  
 -              /* fall through */
 +      /* check main status, clearing INTRQ */
 +      status = ata_chk_status(ap);
 +      if (unlikely(status & ATA_BUSY))
 +              goto idle_irq;
  
 -      case ATA_PROT_ATAPI_NODATA:
 -      case ATA_PROT_NODATA:
 -              /* check altstatus */
 -              status = ata_altstatus(ap);
 -              if (status & ATA_BUSY)
 -                      goto idle_irq;
 +      DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
 +              ap->id, qc->tf.protocol, ap->hsm_task_state, status);
  
 -              /* check main status, clearing INTRQ */
 -              status = ata_chk_status(ap);
 -              if (unlikely(status & ATA_BUSY))
 -                      goto idle_irq;
 -              DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
 -                      ap->id, qc->tf.protocol, status);
 +      /* ack bmdma irq events */
 +      ap->ops->irq_clear(ap);
  
 -              /* ack bmdma irq events */
 -              ap->ops->irq_clear(ap);
 +      /* check error */
 +      if (unlikely((status & ATA_ERR) || (host_stat & ATA_DMA_ERR)))
 +              ap->hsm_task_state = HSM_ST_ERR;
 +
 +fsm_start:
 +      switch (ap->hsm_task_state) {
 +      case HSM_ST_FIRST:
 +              /* Some pre-ATAPI-4 devices assert INTRQ 
 +               * at this state when ready to receive CDB.
 +               */
 +
 +              /* check device status */
 +              if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
 +                      /* Wrong status. Let EH handle this */
 +                      ap->hsm_task_state = HSM_ST_ERR;
 +                      goto fsm_start;
 +              }
 +
 +              atapi_send_cdb(ap, qc);
 +
 +              break;
 +
 +      case HSM_ST:
 +              /* complete command or read/write the data register */
 +              if (qc->tf.protocol == ATA_PROT_ATAPI) {
 +                      /* ATAPI PIO protocol */
 +                      if ((status & ATA_DRQ) == 0) {
 +                              /* no more data to transfer */
 +                              ap->hsm_task_state = HSM_ST_LAST;
 +                              goto fsm_start;
 +                      }
 +                      
 +                      atapi_pio_bytes(qc);
 +
 +                      if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
 +                              /* bad ireason reported by device */
 +                              goto fsm_start;
 +
 +              } else {
 +                      /* ATA PIO protocol */
 +                      if (unlikely((status & ATA_DRQ) == 0)) {
 +                              /* handle BSY=0, DRQ=0 as error */
 +                              ap->hsm_task_state = HSM_ST_ERR;
 +                              goto fsm_start;
 +                      }
 +
 +                      ata_pio_sector(qc);
 +
 +                      if (ap->hsm_task_state == HSM_ST_LAST &&
 +                          (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
 +                              /* all data read */
 +                              ata_altstatus(ap);
 +                              status = ata_chk_status(ap);
 +                              goto fsm_start;
 +                      }
 +              }
 +
 +              ata_altstatus(ap); /* flush */
 +              break;
 +
 +      case HSM_ST_LAST:
 +              if (unlikely(status & ATA_DRQ)) {
 +                      /* handle DRQ=1 as error */
 +                      ap->hsm_task_state = HSM_ST_ERR;
 +                      goto fsm_start;
 +              }
 +
 +              /* no more data to transfer */
 +              DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
 +                      ap->id, status);
 +
 +              ap->hsm_task_state = HSM_ST_IDLE;
  
                /* complete taskfile transaction */
                ata_qc_complete(qc, status);
                break;
  
 +      case HSM_ST_ERR:
 +              printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
 +                     ap->id, status, host_stat);
 +
 +              ap->hsm_task_state = HSM_ST_IDLE;
 +              ata_qc_complete(qc, status | ATA_ERR);
 +              break;
        default:
                goto idle_irq;
        }
@@@ -4064,11 -3802,11 +4089,11 @@@ irqreturn_t ata_interrupt (int irq, voi
  
                ap = host_set->ports[i];
                if (ap &&
 -                  !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
 +                  !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
                        struct ata_queued_cmd *qc;
  
                        qc = ata_qc_from_tag(ap, ap->active_tag);
 -                      if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
 +                      if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
                            (qc->flags & ATA_QCFLAG_ACTIVE))
                                handled |= ata_host_intr(ap, qc);
                }
        return IRQ_RETVAL(handled);
  }
  
 -/**
 - *    atapi_packet_task - Write CDB bytes to hardware
 - *    @_data: Port to which ATAPI device is attached.
 - *
 - *    When device has indicated its readiness to accept
 - *    a CDB, this function is called.  Send the CDB.
 - *    If DMA is to be performed, exit immediately.
 - *    Otherwise, we are in polling mode, so poll
 - *    status under operation succeeds or fails.
 - *
 - *    LOCKING:
 - *    Kernel thread context (may sleep)
 - */
 -
 -static void atapi_packet_task(void *_data)
 -{
 -      struct ata_port *ap = _data;
 -      struct ata_queued_cmd *qc;
 -      u8 status;
 -
 -      qc = ata_qc_from_tag(ap, ap->active_tag);
 -      assert(qc != NULL);
 -      assert(qc->flags & ATA_QCFLAG_ACTIVE);
 -
 -      /* sleep-wait for BSY to clear */
 -      DPRINTK("busy wait\n");
 -      if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
 -              goto err_out;
 -
 -      /* make sure DRQ is set */
 -      status = ata_chk_status(ap);
 -      if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
 -              goto err_out;
 -
 -      /* send SCSI cdb */
 -      DPRINTK("send cdb\n");
 -      assert(ap->cdb_len >= 12);
 -
 -      if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
 -          qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
 -              unsigned long flags;
 -
 -              /* Once we're done issuing command and kicking bmdma,
 -               * irq handler takes over.  To not lose irq, we need
 -               * to clear NOINTR flag before sending cdb, but
 -               * interrupt handler shouldn't be invoked before we're
 -               * finished.  Hence, the following locking.
 -               */
 -              spin_lock_irqsave(&ap->host_set->lock, flags);
 -              ap->flags &= ~ATA_FLAG_NOINTR;
 -              ata_data_xfer(ap, qc->cdb, ap->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);
 -
 -              /* PIO commands are handled by polling */
 -              ap->hsm_task_state = HSM_ST;
 -              queue_work(ata_wq, &ap->pio_task);
 -      }
 -
 -      return;
 -
 -err_out:
 -      ata_poll_qc_complete(qc, ATA_ERR);
 -}
 -
 -
  /**
   *    ata_port_start - Set port up for dma.
   *    @ap: Port to initialize
@@@ -4197,7 -4004,7 +4222,7 @@@ static void ata_host_init(struct ata_po
        ap->active_tag = ATA_TAG_POISON;
        ap->last_ctl = 0xFF;
  
 -      INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
 +      INIT_WORK(&ap->dataout_task, ata_dataout_task, ap);
        INIT_WORK(&ap->pio_task, ata_pio_task, ap);
  
        for (i = 0; i < ATA_MAX_DEVICES; i++)
diff --combined include/linux/ata.h
@@@ -128,6 -128,10 +128,10 @@@ enum 
        ATA_CMD_PIO_READ_EXT    = 0x24,
        ATA_CMD_PIO_WRITE       = 0x30,
        ATA_CMD_PIO_WRITE_EXT   = 0x34,
+       ATA_CMD_READ_MULTI      = 0xC4,
+       ATA_CMD_READ_MULTI_EXT  = 0x29,
+       ATA_CMD_WRITE_MULTI     = 0xC5,
+       ATA_CMD_WRITE_MULTI_EXT = 0x39,
        ATA_CMD_SET_FEATURES    = 0xEF,
        ATA_CMD_PACKET          = 0xA0,
        ATA_CMD_VERIFY          = 0x40,
        ATA_TFLAG_DEVICE        = (1 << 2), /* enable r/w to device reg */
        ATA_TFLAG_WRITE         = (1 << 3), /* data dir: host->dev==1 (write) */
        ATA_TFLAG_LBA           = (1 << 4), /* enable LBA */
 +      ATA_TFLAG_POLLING       = (1 << 5), /* set nIEN to 1 and use polling */
  };
  
  enum ata_tf_protocols {
@@@ -253,8 -256,6 +257,8 @@@ struct ata_taskfile 
          ((u64) (id)[(n) + 1] << 16) | \
          ((u64) (id)[(n) + 0]) )
  
 +#define ata_id_cdb_intr(id)   (((id)[0] & 0x60) == 0x20)
 +
  static inline int ata_id_current_chs_valid(u16 *id)
  {
        /* For ATA-1 devices, if the INITIALIZE DEVICE PARAMETERS command 
diff --combined include/linux/libata.h
@@@ -98,7 -98,6 +98,7 @@@ enum 
        ATA_DFLAG_PIO           = (1 << 1), /* device currently in PIO mode */
        ATA_DFLAG_LOCK_SECTORS  = (1 << 2), /* don't adjust max_sectors */
        ATA_DFLAG_LBA           = (1 << 3), /* device supports LBA */
 +      ATA_DFLAG_CDB_INTR      = (1 << 4), /* device asserts INTRQ when ready for CDB */
  
        ATA_DEV_UNKNOWN         = 0,    /* unknown device */
        ATA_DEV_ATA             = 1,    /* ATA device */
        ATA_FLAG_MMIO           = (1 << 6), /* use MMIO, not PIO */
        ATA_FLAG_SATA_RESET     = (1 << 7), /* use COMRESET */
        ATA_FLAG_PIO_DMA        = (1 << 8), /* PIO cmds via DMA */
 -      ATA_FLAG_NOINTR         = (1 << 9), /* FIXME: Remove this once
 -                                           * proper HSM is in place. */
 +      ATA_FLAG_PIO_POLLING    = (1 << 9), /* use polling PIO if LLD
 +                                           * doesn't handle PIO interrupts */
  
        ATA_QCFLAG_ACTIVE       = (1 << 1), /* cmd not yet ack'd to scsi lyer */
        ATA_QCFLAG_SG           = (1 << 3), /* have s/g table? */
        ATA_TMOUT_PIO           = 30 * HZ,
        ATA_TMOUT_BOOT          = 30 * HZ,      /* hueristic */
        ATA_TMOUT_BOOT_QUICK    = 7 * HZ,       /* hueristic */
 -      ATA_TMOUT_CDB           = 30 * HZ,
 -      ATA_TMOUT_CDB_QUICK     = 5 * HZ,
 +      ATA_TMOUT_DATAOUT       = 30 * HZ,
 +      ATA_TMOUT_DATAOUT_QUICK = 5 * HZ,
  
        /* ATA bus states */
        BUS_UNKNOWN             = 0,
  };
  
  enum hsm_task_states {
 -      HSM_ST_UNKNOWN,
 -      HSM_ST_IDLE,
 -      HSM_ST_POLL,
 -      HSM_ST_TMOUT,
 -      HSM_ST,
 -      HSM_ST_LAST,
 -      HSM_ST_LAST_POLL,
 -      HSM_ST_ERR,
 +      HSM_ST_UNKNOWN,         /* state unknown */
 +      HSM_ST_IDLE,            /* no command on going */
 +      HSM_ST_POLL,            /* same as HSM_ST, waits longer */
 +      HSM_ST_TMOUT,           /* timeout */
 +      HSM_ST,                 /* (waiting the device to) transfer data */
 +      HSM_ST_LAST,            /* (waiting the device to) complete command */
 +      HSM_ST_LAST_POLL,       /* same as HSM_ST_LAST, waits longer */
 +      HSM_ST_ERR,             /* error */
 +      HSM_ST_FIRST,           /* (waiting the device to)
 +                                 write CDB or first data block */
  };
  
  /* forward declarations */
@@@ -286,10 -283,8 +286,8 @@@ struct ata_device 
        u8                      xfer_mode;
        unsigned int            xfer_shift;     /* ATA_SHIFT_xxx */
  
-       /* cache info about current transfer mode */
-       u8                      xfer_protocol;  /* taskfile xfer protocol */
-       u8                      read_cmd;       /* opcode to use on read */
-       u8                      write_cmd;      /* opcode to use on write */
+       unsigned int            multi_count;    /* sectors count for
+                                                  READ/WRITE MULTIPLE */
  
        /* for CHS addressing */
        u16                     cylinders;      /* Number of cylinders */
@@@ -329,7 -324,7 +327,7 @@@ struct ata_port 
        struct ata_host_stats   stats;
        struct ata_host_set     *host_set;
  
 -      struct work_struct      packet_task;
 +      struct work_struct      dataout_task;
  
        struct work_struct      pio_task;
        unsigned int            hsm_task_state;