[PATCH] libata-hp-prep: add flags and eh_info/context fields for hotplug
[deliverable/linux.git] / drivers / scsi / libata-core.c
index eea1fe9c8b792b87fee35344c9cab7aa9ce15583..66df895c9617c54eb9363e2f14f04c1af7f0fa7c 100644 (file)
@@ -875,6 +875,9 @@ static unsigned int ata_id_xfermask(const u16 *id)
 /**
  *     ata_port_queue_task - Queue port_task
  *     @ap: The ata_port to queue port_task for
+ *     @fn: workqueue function to be scheduled
+ *     @data: data value to pass to workqueue function
+ *     @delay: delay time for workqueue function
  *
  *     Schedule @fn(@data) for execution after @delay jiffies using
  *     port_task.  There is one port_task per port and it's the
@@ -1251,6 +1254,28 @@ static inline u8 ata_dev_knobble(struct ata_device *dev)
        return ((dev->ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
 }
 
+static void ata_dev_config_ncq(struct ata_device *dev,
+                              char *desc, size_t desc_sz)
+{
+       struct ata_port *ap = dev->ap;
+       int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
+
+       if (!ata_id_has_ncq(dev->id)) {
+               desc[0] = '\0';
+               return;
+       }
+
+       if (ap->flags & ATA_FLAG_NCQ) {
+               hdepth = min(ap->host->can_queue, ATA_MAX_QUEUE - 1);
+               dev->flags |= ATA_DFLAG_NCQ;
+       }
+
+       if (hdepth >= ddepth)
+               snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
+       else
+               snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
+}
+
 /**
  *     ata_dev_configure - Configure the specified ATA/ATAPI device
  *     @dev: Target device to configure
@@ -1311,6 +1336,7 @@ static int ata_dev_configure(struct ata_device *dev, int print_info)
 
                if (ata_id_has_lba(id)) {
                        const char *lba_desc;
+                       char ncq_desc[20];
 
                        lba_desc = "LBA";
                        dev->flags |= ATA_DFLAG_LBA;
@@ -1319,14 +1345,17 @@ static int ata_dev_configure(struct ata_device *dev, int print_info)
                                lba_desc = "LBA48";
                        }
 
+                       /* config NCQ */
+                       ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
+
                        /* print device info to dmesg */
                        if (print_info)
                                ata_dev_printk(dev, KERN_INFO, "ATA-%d, "
-                                       "max %s, %Lu sectors: %s\n",
+                                       "max %s, %Lu sectors: %s %s\n",
                                        ata_id_major_version(id),
                                        ata_mode_string(xfer_mask),
                                        (unsigned long long)dev->n_sectors,
-                                       lba_desc);
+                                       lba_desc, ncq_desc);
                } else {
                        /* CHS */
 
@@ -1469,6 +1498,12 @@ static int ata_bus_probe(struct ata_port *ap)
                if (classes[i] == ATA_DEV_UNKNOWN)
                        classes[i] = ATA_DEV_NONE;
 
+       /* after the reset the device state is PIO 0 and the controller
+          state is undefined. Record the mode */
+
+       for (i = 0; i < ATA_MAX_DEVICES; i++)
+               ap->device[i].pio_mode = XFER_PIO_0;
+
        /* read IDENTIFY page and configure devices */
        for (i = 0; i < ATA_MAX_DEVICES; i++) {
                dev = &ap->device[i];
@@ -3065,8 +3100,8 @@ static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
 /**
  *     ata_dev_init_params - Issue INIT DEV PARAMS command
  *     @dev: Device to which command will be sent
- *     @heads: Number of heads
- *     @sectors: Number of sectors
+ *     @heads: Number of heads (taskfile parameter)
+ *     @sectors: Number of sectors (taskfile parameter)
  *
  *     LOCKING:
  *     Kernel thread context (may sleep)
@@ -3492,7 +3527,7 @@ void swap_buf_le16(u16 *buf, unsigned int buf_words)
 
 /**
  *     ata_mmio_data_xfer - Transfer data by MMIO
- *     @ap: port to read/write
+ *     @dev: device for this I/O
  *     @buf: data buffer
  *     @buflen: buffer length
  *     @write_data: read/write
@@ -3503,9 +3538,10 @@ void swap_buf_le16(u16 *buf, unsigned int buf_words)
  *     Inherited from caller.
  */
 
-static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
-                              unsigned int buflen, int write_data)
+void ata_mmio_data_xfer(struct ata_device *adev, unsigned char *buf, 
+                       unsigned int buflen, int write_data)
 {
+       struct ata_port *ap = adev->ap;
        unsigned int i;
        unsigned int words = buflen >> 1;
        u16 *buf16 = (u16 *) buf;
@@ -3537,7 +3573,7 @@ static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
 
 /**
  *     ata_pio_data_xfer - Transfer data by PIO
- *     @ap: port to read/write
+ *     @adev: device to target
  *     @buf: data buffer
  *     @buflen: buffer length
  *     @write_data: read/write
@@ -3548,9 +3584,10 @@ static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
  *     Inherited from caller.
  */
 
-static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
-                             unsigned int buflen, int write_data)
+void ata_pio_data_xfer(struct ata_device *adev, unsigned char *buf, 
+                      unsigned int buflen, int write_data)
 {
+       struct ata_port *ap = adev->ap;
        unsigned int words = buflen >> 1;
 
        /* Transfer multiple of 2 bytes */
@@ -3575,38 +3612,29 @@ static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
 }
 
 /**
- *     ata_data_xfer - Transfer data from/to the data register.
- *     @ap: port to read/write
+ *     ata_pio_data_xfer_noirq - Transfer data by PIO
+ *     @adev: device to target
  *     @buf: data buffer
  *     @buflen: buffer length
- *     @do_write: read/write
+ *     @write_data: read/write
  *
- *     Transfer data from/to the device data register.
+ *     Transfer data from/to the device data register by PIO. Do the 
+ *     transfer with interrupts disabled.
  *
  *     LOCKING:
  *     Inherited from caller.
  */
 
-static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
-                         unsigned int buflen, int do_write)
+void ata_pio_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
+                                   unsigned int buflen, int write_data)
 {
-       /* Make the crap hardware pay the costs not the good stuff */
-       if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
-               unsigned long flags;
-               local_irq_save(flags);
-               if (ap->flags & ATA_FLAG_MMIO)
-                       ata_mmio_data_xfer(ap, buf, buflen, do_write);
-               else
-                       ata_pio_data_xfer(ap, buf, buflen, do_write);
-               local_irq_restore(flags);
-       } else {
-               if (ap->flags & ATA_FLAG_MMIO)
-                       ata_mmio_data_xfer(ap, buf, buflen, do_write);
-               else
-                       ata_pio_data_xfer(ap, buf, buflen, do_write);
-       }
+       unsigned long flags;
+       local_irq_save(flags);
+       ata_pio_data_xfer(adev, buf, buflen, write_data);
+       local_irq_restore(flags);
 }
 
+
 /**
  *     ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
  *     @qc: Command on going
@@ -3641,17 +3669,18 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
        if (PageHighMem(page)) {
                unsigned long flags;
 
+               /* FIXME: use a bounce buffer */
                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);
+               ap->ops->data_xfer(qc->dev, 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);
+               ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
        }
 
        qc->cursect++;
@@ -3667,7 +3696,7 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
  *     ata_pio_sectors - Transfer one or many 512-byte sectors.
  *     @qc: Command on going
  *
- *     Transfer one or many ATA_SECT_SIZE of data from/to the 
+ *     Transfer one or many ATA_SECT_SIZE of data from/to the
  *     ATA device for the DRQ request.
  *
  *     LOCKING:
@@ -3707,7 +3736,7 @@ static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
        DPRINTK("send cdb\n");
        WARN_ON(qc->dev->cdb_len < 12);
 
-       ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
+       ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
        ata_altstatus(ap); /* flush */
 
        switch (qc->tf.protocol) {
@@ -3767,7 +3796,7 @@ next_sg:
                                       "%u bytes trailing data\n", bytes);
 
                for (i = 0; i < words; i++)
-                       ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
+                       ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
 
                ap->hsm_task_state = HSM_ST_LAST;
                return;
@@ -3793,17 +3822,18 @@ next_sg:
        if (PageHighMem(page)) {
                unsigned long flags;
 
+               /* FIXME: use bounce buffer */
                local_irq_save(flags);
                buf = kmap_atomic(page, KM_IRQ0);
 
                /* do the actual data transfer */
-               ata_data_xfer(ap, buf + offset, count, do_write);
+               ap->ops->data_xfer(qc->dev,  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);
+               ap->ops->data_xfer(qc->dev,  buf + offset, count, do_write);
        }
 
        bytes -= count;
@@ -3836,10 +3866,16 @@ static void atapi_pio_bytes(struct ata_queued_cmd *qc)
        unsigned int ireason, bc_lo, bc_hi, bytes;
        int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
 
-       ap->ops->tf_read(ap, &qc->tf);
-       ireason = qc->tf.nsect;
-       bc_lo = qc->tf.lbam;
-       bc_hi = qc->tf.lbah;
+       /* Abuse qc->result_tf for temp storage of intermediate TF
+        * here to save some kernel stack usage.
+        * For normal completion, qc->result_tf is not relevant. For
+        * error, qc->result_tf is later overwritten by ata_qc_complete().
+        * So, the correctness of qc->result_tf is not affected.
+        */
+       ap->ops->tf_read(ap, &qc->result_tf);
+       ireason = qc->result_tf.nsect;
+       bc_lo = qc->result_tf.lbam;
+       bc_hi = qc->result_tf.lbah;
        bytes = (bc_hi << 8) | bc_lo;
 
        /* shall be cleared to zero, indicating xfer of data */
@@ -3938,6 +3974,8 @@ static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
                } else
                        ata_qc_complete(qc);
        }
+
+       ata_altstatus(ap); /* flush */
 }
 
 /**
@@ -3980,9 +4018,15 @@ fsm_start:
                poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
 
                /* check device status */
-               if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
-                       /* Wrong status. Let EH handle this */
-                       qc->err_mask |= AC_ERR_HSM;
+               if (unlikely((status & ATA_DRQ) == 0)) {
+                       /* handle BSY=0, DRQ=0 as error */
+                       if (likely(status & (ATA_ERR | ATA_DF)))
+                               /* device stops HSM for abort/error */
+                               qc->err_mask |= AC_ERR_DEV;
+                       else
+                               /* HSM violation. Let EH handle this */
+                               qc->err_mask |= AC_ERR_HSM;
+
                        ap->hsm_task_state = HSM_ST_ERR;
                        goto fsm_start;
                }
@@ -3996,7 +4040,7 @@ fsm_start:
                if (unlikely(status & (ATA_ERR | ATA_DF))) {
                        printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
                               ap->id, status);
-                       qc->err_mask |= AC_ERR_DEV;
+                       qc->err_mask |= AC_ERR_HSM;
                        ap->hsm_task_state = HSM_ST_ERR;
                        goto fsm_start;
                }
@@ -4038,7 +4082,9 @@ fsm_start:
                if (qc->tf.protocol == ATA_PROT_ATAPI) {
                        /* ATAPI PIO protocol */
                        if ((status & ATA_DRQ) == 0) {
-                               /* no more data to transfer */
+                               /* No more data to transfer or device error.
+                                * Device error will be tagged in HSM_ST_LAST.
+                                */
                                ap->hsm_task_state = HSM_ST_LAST;
                                goto fsm_start;
                        }
@@ -4052,7 +4098,7 @@ fsm_start:
                        if (unlikely(status & (ATA_ERR | ATA_DF))) {
                                printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
                                       ap->id, status);
-                               qc->err_mask |= AC_ERR_DEV;
+                               qc->err_mask |= AC_ERR_HSM;
                                ap->hsm_task_state = HSM_ST_ERR;
                                goto fsm_start;
                        }
@@ -4067,7 +4113,13 @@ fsm_start:
                        /* ATA PIO protocol */
                        if (unlikely((status & ATA_DRQ) == 0)) {
                                /* handle BSY=0, DRQ=0 as error */
-                               qc->err_mask |= AC_ERR_HSM;
+                               if (likely(status & (ATA_ERR | ATA_DF)))
+                                       /* device stops HSM for abort/error */
+                                       qc->err_mask |= AC_ERR_DEV;
+                               else
+                                       /* HSM violation. Let EH handle this */
+                                       qc->err_mask |= AC_ERR_HSM;
+
                                ap->hsm_task_state = HSM_ST_ERR;
                                goto fsm_start;
                        }
@@ -4092,6 +4144,9 @@ fsm_start:
                                        status = ata_wait_idle(ap);
                                }
 
+                               if (status & (ATA_BUSY | ATA_DRQ))
+                                       qc->err_mask |= AC_ERR_HSM;
+
                                /* ata_pio_sectors() might change the
                                 * state to HSM_ST_LAST. so, the state
                                 * is changed after ata_pio_sectors().
@@ -4416,6 +4471,7 @@ static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
        struct ata_port *ap = qc->ap;
 
        switch (qc->tf.protocol) {
+       case ATA_PROT_NCQ:
        case ATA_PROT_DMA:
        case ATA_PROT_ATAPI_DMA:
                return 1;
@@ -4965,6 +5021,9 @@ int ata_device_resume(struct ata_device *dev)
 
        if (ap->flags & ATA_FLAG_SUSPENDED) {
                struct ata_device *failed_dev;
+
+               ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 200000);
+
                ap->flags &= ~ATA_FLAG_SUSPENDED;
                while (ata_set_mode(ap, &failed_dev))
                        ata_dev_disable(failed_dev);
@@ -4980,6 +5039,7 @@ int ata_device_resume(struct ata_device *dev)
 /**
  *     ata_device_suspend - prepare a device for suspend
  *     @dev: the device to suspend
+ *     @state: target power management state
  *
  *     Flush the cache on the drive, if appropriate, then issue a
  *     standbynow command.
@@ -5125,9 +5185,11 @@ static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
        ap->sata_spd_limit = UINT_MAX;
        ap->active_tag = ATA_TAG_POISON;
        ap->last_ctl = 0xFF;
+       ap->msg_enable = ATA_MSG_DRV;
 
        INIT_WORK(&ap->port_task, NULL, NULL);
        INIT_LIST_HEAD(&ap->eh_done_q);
+       init_waitqueue_head(&ap->eh_wait_q);
 
        /* set cable type */
        ap->cbl = ATA_CBL_NONE;
@@ -5640,6 +5702,9 @@ EXPORT_SYMBOL_GPL(ata_port_start);
 EXPORT_SYMBOL_GPL(ata_port_stop);
 EXPORT_SYMBOL_GPL(ata_host_stop);
 EXPORT_SYMBOL_GPL(ata_interrupt);
+EXPORT_SYMBOL_GPL(ata_mmio_data_xfer);
+EXPORT_SYMBOL_GPL(ata_pio_data_xfer);
+EXPORT_SYMBOL_GPL(ata_pio_data_xfer_noirq);
 EXPORT_SYMBOL_GPL(ata_qc_prep);
 EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
@@ -5674,6 +5739,7 @@ EXPORT_SYMBOL_GPL(ata_port_queue_task);
 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
+EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
 EXPORT_SYMBOL_GPL(ata_scsi_release);
 EXPORT_SYMBOL_GPL(ata_host_intr);
 EXPORT_SYMBOL_GPL(sata_scr_valid);
This page took 0.080162 seconds and 5 git commands to generate.