vserver 2.0 rc7
[linux-2.6.git] / drivers / scsi / qla2xxx / qla_isr.c
index 415242a..6792cfa 100644 (file)
@@ -16,9 +16,6 @@
  * General Public License for more details.
  *
  */
-
-#include "qla_os.h"
-
 #include "qla_def.h"
 
 static void qla2x00_mbx_completion(scsi_qla_host_t *, uint16_t);
@@ -30,10 +27,8 @@ static void qla2x00_status_cont_entry(scsi_qla_host_t *, sts_cont_entry_t *);
 static void qla2x00_error_entry(scsi_qla_host_t *, sts_entry_t *);
 static void qla2x00_ms_entry(scsi_qla_host_t *, ms_iocb_entry_t *);
 
-static int qla2x00_check_sense(struct scsi_cmnd *cp, os_lun_t *);
-
 /**
- * qla2x00_intr_handler() - Process interrupts for the ISP.
+ * qla2100_intr_handler() - Process interrupts for the ISP2100 and ISP2200.
  * @irq:
  * @dev_id: SCSI driver HA context
  * @regs:
@@ -43,20 +38,14 @@ static int qla2x00_check_sense(struct scsi_cmnd *cp, os_lun_t *);
  * Returns handled flag.
  */
 irqreturn_t
-qla2x00_intr_handler(int irq, void *dev_id, struct pt_regs *regs)
+qla2100_intr_handler(int irq, void *dev_id, struct pt_regs *regs)
 {
        scsi_qla_host_t *ha;
-       device_reg_t    *reg;
+       device_reg_t __iomem *reg;
+       int             status;
+       unsigned long   flags;
+       unsigned long   iter;
        uint32_t        mbx;
-       int             status = 0;
-       unsigned long   flags = 0;
-       unsigned long   mbx_flags = 0;
-       unsigned long   intr_iter;
-       uint32_t        stat;
-       uint16_t        hccr;
-
-       /* Don't loop forever, interrupt are OFF */
-       intr_iter = 50; 
 
        ha = (scsi_qla_host_t *) dev_id;
        if (!ha) {
@@ -66,157 +55,162 @@ qla2x00_intr_handler(int irq, void *dev_id, struct pt_regs *regs)
        }
 
        reg = ha->iobase;
+       status = 0;
 
        spin_lock_irqsave(&ha->hardware_lock, flags);
-
-       for (;;) {
-               /* Relax CPU! */
-               if (!(intr_iter--))
+       for (iter = 50; iter--; ) {
+               if ((RD_REG_WORD(&reg->istatus) & ISR_RISC_INT) == 0)
                        break;
 
-               if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
-                       if ((RD_REG_WORD(&reg->istatus) & ISR_RISC_INT) == 0)
-                               break;
-
-                       if (RD_REG_WORD(&reg->semaphore) & BIT_0) {
-                               WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
-                               RD_REG_WORD(&reg->hccr);
-
-                               /* Get mailbox data. */
-                               mbx = RD_MAILBOX_REG(ha, reg, 0);
-                               if (mbx > 0x3fff && mbx < 0x8000) {
-                                       qla2x00_mbx_completion(ha,
-                                           (uint16_t)mbx);
-                                       status |= MBX_INTERRUPT;
-                               } else if (mbx > 0x7fff && mbx < 0xc000) {
-                                       qla2x00_async_event(ha, mbx);
-                               } else {
-                                       /*EMPTY*/
-                                       DEBUG2(printk("scsi(%ld): Unrecognized "
-                                           "interrupt type (%d)\n",
-                                           ha->host_no, mbx));
-                               }
-                               /* Release mailbox registers. */
-                               WRT_REG_WORD(&reg->semaphore, 0);
-                               /* Workaround for ISP2100 chip. */
-                               if (IS_QLA2100(ha))
-                                       RD_REG_WORD(&reg->semaphore);
-                       } else {
-                               qla2x00_process_response_queue(ha);
-       
-                               WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
-                               RD_REG_WORD(&reg->hccr);
-                       }
-               } else /* IS_QLA23XX(ha) */ {
-                       stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
-                       if ((stat & HSR_RISC_INT) == 0)
-                               break;
+               if (RD_REG_WORD(&reg->semaphore) & BIT_0) {
+                       WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
+                       RD_REG_WORD(&reg->hccr);
 
-                       mbx = MSW(stat);
-                       switch (stat & 0xff) {
-                       case 0x13:
-                               qla2x00_process_response_queue(ha);
-                               break;
-                       case 0x1:
-                       case 0x2:
-                       case 0x10:
-                       case 0x11:
+                       /* Get mailbox data. */
+                       mbx = RD_MAILBOX_REG(ha, reg, 0);
+                       if (mbx > 0x3fff && mbx < 0x8000) {
                                qla2x00_mbx_completion(ha, (uint16_t)mbx);
                                status |= MBX_INTERRUPT;
-
-                               /* Release mailbox registers. */
-                               WRT_REG_WORD(&reg->semaphore, 0);
-                               break;
-                       case 0x12:
-                               qla2x00_async_event(ha, mbx);
-                               break;
-                       case 0x15:
-                               mbx = mbx << 16 | MBA_CMPLT_1_16BIT;
+                       } else if (mbx > 0x7fff && mbx < 0xc000) {
                                qla2x00_async_event(ha, mbx);
-                               break;
-                       case 0x16:
-                               mbx = mbx << 16 | MBA_SCSI_COMPLETION;
-                               qla2x00_async_event(ha, mbx);
-                               break;
-                       default:
-                               hccr = RD_REG_WORD(&reg->hccr);
-                               if (hccr & HCCR_RISC_PAUSE) {
-                                       qla_printk(KERN_INFO, ha,
-                                           "RISC paused, dumping HCCR=%x\n",
-                                           hccr);
-
-                                       /*
-                                        * Issue a "HARD" reset in order for
-                                        * the RISC interrupt bit to be
-                                        * cleared.  Schedule a big hammmer to
-                                        * get out of the RISC PAUSED state.
-                                        */
-                                       WRT_REG_WORD(&reg->hccr,
-                                           HCCR_RESET_RISC);
-                                       RD_REG_WORD(&reg->hccr);
-                                       set_bit(ISP_ABORT_NEEDED,
-                                           &ha->dpc_flags);
-                                       break;
-                               } else {
-                                       DEBUG2(printk("scsi(%ld): Unrecognized "
-                                           "interrupt type (%d)\n",
-                                           ha->host_no, stat & 0xff));
-                               }
-                               break;
+                       } else {
+                               /*EMPTY*/
+                               DEBUG2(printk("scsi(%ld): Unrecognized "
+                                   "interrupt type (%d)\n",
+                                   ha->host_no, mbx));
                        }
+                       /* Release mailbox registers. */
+                       WRT_REG_WORD(&reg->semaphore, 0);
+                       RD_REG_WORD(&reg->semaphore);
+               } else {
+                       qla2x00_process_response_queue(ha);
+
                        WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
                        RD_REG_WORD(&reg->hccr);
                }
        }
-
        spin_unlock_irqrestore(&ha->hardware_lock, flags);
 
-       qla2x00_next(ha);
-       ha->last_irq_cpu = smp_processor_id();
+       ha->last_irq_cpu = _smp_processor_id();
        ha->total_isr_cnt++;
 
        if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
            (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
+               spin_lock_irqsave(&ha->mbx_reg_lock, flags);
 
-               /* There was a mailbox completion */
-               DEBUG3(printk("%s(%ld): Going to get mbx reg lock.\n",
-                   __func__, ha->host_no));
+               set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
+               up(&ha->mbx_intr_sem);
 
-               spin_lock_irqsave(&ha->mbx_reg_lock, mbx_flags);
+               spin_unlock_irqrestore(&ha->mbx_reg_lock, flags);
+       }
 
-               if (ha->mcp == NULL) {
-                       DEBUG3(printk("%s(%ld): Error mbx pointer.\n",
-                           __func__, ha->host_no));
-               } else {
-                       DEBUG3(printk("%s(%ld): Going to set mbx intr flags. "
-                           "cmd=%x.\n",
-                           __func__, ha->host_no, ha->mcp->mb[0]));
-               }
-               set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
+       return (IRQ_HANDLED);
+}
 
-               DEBUG3(printk("%s(%ld): Going to wake up mbx function for "
-                   "completion.\n",
-                   __func__, ha->host_no));
+/**
+ * qla2300_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
+ * @irq:
+ * @dev_id: SCSI driver HA context
+ * @regs:
+ *
+ * Called by system whenever the host adapter generates an interrupt.
+ *
+ * Returns handled flag.
+ */
+irqreturn_t
+qla2300_intr_handler(int irq, void *dev_id, struct pt_regs *regs)
+{
+       scsi_qla_host_t *ha;
+       device_reg_t __iomem *reg;
+       int             status;
+       unsigned long   flags;
+       unsigned long   iter;
+       uint32_t        stat;
+       uint32_t        mbx;
+       uint16_t        hccr;
 
-               up(&ha->mbx_intr_sem);
+       ha = (scsi_qla_host_t *) dev_id;
+       if (!ha) {
+               printk(KERN_INFO
+                   "%s(): NULL host pointer\n", __func__);
+               return (IRQ_NONE);
+       }
 
-               DEBUG3(printk("%s(%ld): Going to release mbx reg lock.\n",
-                   __func__, ha->host_no));
+       reg = ha->iobase;
+       status = 0;
+
+       spin_lock_irqsave(&ha->hardware_lock, flags);
+       for (iter = 50; iter--; ) {
+               stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
+               if (stat & HSR_RISC_PAUSED) {
+                       hccr = RD_REG_WORD(&reg->hccr);
+                       if (hccr & (BIT_15 | BIT_13 | BIT_11 | BIT_8))
+                               qla_printk(KERN_INFO, ha,
+                                   "Parity error -- HCCR=%x.\n", hccr);
+                       else
+                               qla_printk(KERN_INFO, ha,
+                                   "RISC paused -- HCCR=%x\n", hccr);
+
+                       /*
+                        * Issue a "HARD" reset in order for the RISC
+                        * interrupt bit to be cleared.  Schedule a big
+                        * hammmer to get out of the RISC PAUSED state.
+                        */
+                       WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
+                       RD_REG_WORD(&reg->hccr);
+                       set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
+                       break;
+               } else if ((stat & HSR_RISC_INT) == 0)
+                       break;
 
-               spin_unlock_irqrestore(&ha->mbx_reg_lock, mbx_flags);
+               mbx = MSW(stat);
+               switch (stat & 0xff) {
+               case 0x13:
+                       qla2x00_process_response_queue(ha);
+                       break;
+               case 0x1:
+               case 0x2:
+               case 0x10:
+               case 0x11:
+                       qla2x00_mbx_completion(ha, (uint16_t)mbx);
+                       status |= MBX_INTERRUPT;
+
+                       /* Release mailbox registers. */
+                       WRT_REG_WORD(&reg->semaphore, 0);
+                       break;
+               case 0x12:
+                       qla2x00_async_event(ha, mbx);
+                       break;
+               case 0x15:
+                       mbx = mbx << 16 | MBA_CMPLT_1_16BIT;
+                       qla2x00_async_event(ha, mbx);
+                       break;
+               case 0x16:
+                       mbx = mbx << 16 | MBA_SCSI_COMPLETION;
+                       qla2x00_async_event(ha, mbx);
+                       break;
+               default:
+                       DEBUG2(printk("scsi(%ld): Unrecognized interrupt type "
+                           "(%d)\n",
+                           ha->host_no, stat & 0xff));
+                       break;
+               }
+               WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
+               RD_REG_WORD_RELAXED(&reg->hccr);
        }
+       spin_unlock_irqrestore(&ha->hardware_lock, flags);
 
-       if (!list_empty(&ha->done_queue))
-               qla2x00_done(ha);
+       ha->last_irq_cpu = _smp_processor_id();
+       ha->total_isr_cnt++;
 
-       /* Wakeup the DPC routine */
-       if ((!ha->flags.mbox_busy &&
-           (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
-               test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) ||
-               test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))) &&
-                   ha->dpc_wait && !ha->dpc_active) {
+       if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
+           (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
+               spin_lock_irqsave(&ha->mbx_reg_lock, flags);
+
+               set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
+               up(&ha->mbx_intr_sem);
 
-               up(ha->dpc_wait);
+               spin_unlock_irqrestore(&ha->mbx_reg_lock, flags);
        }
 
        return (IRQ_HANDLED);
@@ -231,17 +225,17 @@ static void
 qla2x00_mbx_completion(scsi_qla_host_t *ha, uint16_t mb0)
 {
        uint16_t        cnt;
-       uint16_t        *wptr;
-       device_reg_t    *reg = ha->iobase;
+       uint16_t __iomem *wptr;
+       device_reg_t __iomem *reg = ha->iobase;
 
        /* Load return mailbox registers. */
-       ha->flags.mbox_int = TRUE;
+       ha->flags.mbox_int = 1;
        ha->mailbox_out[0] = mb0;
-       wptr = (uint16_t *)MAILBOX_REG(ha, reg, 1);
+       wptr = (uint16_t __iomem *)MAILBOX_REG(ha, reg, 1);
 
        for (cnt = 1; cnt < ha->mbx_count; cnt++) {
                if (IS_QLA2200(ha) && cnt == 8) 
-                       wptr = (uint16_t *)MAILBOX_REG(ha, reg, 8);
+                       wptr = (uint16_t __iomem *)MAILBOX_REG(ha, reg, 8);
                if (cnt == 4 || cnt == 5)
                        ha->mailbox_out[cnt] = qla2x00_debounce_register(wptr);
                else
@@ -273,7 +267,7 @@ qla2x00_async_event(scsi_qla_host_t *ha, uint32_t mbx)
        uint16_t        handle_cnt;
        uint16_t        cnt;
        uint32_t        handles[5];
-       device_reg_t    *reg = ha->iobase;
+       device_reg_t __iomem *reg = ha->iobase;
        uint32_t        rscn_entry, host_pid;
        uint8_t         rscn_queue_index;
 
@@ -344,7 +338,6 @@ qla2x00_async_event(scsi_qla_host_t *ha, uint32_t mbx)
                break;
        }
 
-       mb[0] = LSW(mbx);
        switch (mb[0]) {
        case MBA_SCSI_COMPLETION:       /* Fast Post */
                if (!ha->flags.online)
@@ -373,7 +366,14 @@ qla2x00_async_event(scsi_qla_host_t *ha, uint32_t mbx)
                        qla2100_fw_dump(ha, 1);
                else
                        qla2300_fw_dump(ha, 1);
-               set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
+
+               if (mb[1] == 0) {
+                       qla_printk(KERN_INFO, ha,
+                           "Unrecoverable Hardware Error: adapter marked "
+                           "OFFLINE!\n");
+                       ha->flags.online = 0;
+               } else
+                       set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
                break;
 
        case MBA_REQ_TRANSFER_ERR:      /* Request Transfer Error */
@@ -558,8 +558,8 @@ qla2x00_async_event(scsi_qla_host_t *ha, uint32_t mbx)
                        rscn_fcport = qla2x00_alloc_rscn_fcport(ha, GFP_ATOMIC);
                        if (rscn_fcport) {
                                DEBUG14(printk("scsi(%ld): Port Update -- "
-                                   "creating RSCN fcport %p for login.\n",
-                                   ha->host_no, rscn_fcport));
+                                   "creating RSCN fcport %p for %x/%x.\n",
+                                   ha->host_no, rscn_fcport, mb[1], mb[2]));
 
                                rscn_fcport->loop_id = mb[1];
                                rscn_fcport->d_id.b24 = INVALID_PORT_ID;
@@ -584,7 +584,9 @@ qla2x00_async_event(scsi_qla_host_t *ha, uint32_t mbx)
                 * event etc. earlier indicating loop is down) then process
                 * it.  Otherwise ignore it and Wait for RSCN to come in.
                 */
-               if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
+               atomic_set(&ha->loop_down_timer, 0);
+               if (atomic_read(&ha->loop_state) != LOOP_DOWN &&
+                   atomic_read(&ha->loop_state) != LOOP_DEAD) {
                        DEBUG2(printk("scsi(%ld): Asynchronous PORT UPDATE "
                            "ignored.\n", ha->host_no));
                        break;
@@ -601,7 +603,6 @@ qla2x00_async_event(scsi_qla_host_t *ha, uint32_t mbx)
                 */
                atomic_set(&ha->loop_state, LOOP_UP);
 
-               atomic_set(&ha->loop_down_timer, 0);
                qla2x00_mark_all_devices_lost(ha);
 
                ha->flags.rscn_queue_overflow = 1;
@@ -692,18 +693,17 @@ qla2x00_process_completed_request(struct scsi_qla_host *ha, uint32_t index)
        sp = ha->outstanding_cmds[index];
        if (sp) {
                /* Free outstanding command slot. */
-               ha->outstanding_cmds[index] = 0;
+               ha->outstanding_cmds[index] = NULL;
 
                if (ha->actthreads)
                        ha->actthreads--;
-               sp->lun_queue->out_cnt--;
                CMD_COMPL_STATUS(sp->cmd) = 0L;
                CMD_SCSI_STATUS(sp->cmd) = 0L;
 
                /* Save ISP completion status */
                sp->cmd->result = DID_OK << 16;
                sp->fo_retry_cnt = 0;
-               add_to_done_queue(ha, sp);
+               qla2x00_sp_compl(ha, sp);
        } else {
                DEBUG2(printk("scsi(%ld): Invalid ISP SCSI completion handle\n",
                    ha->host_no));
@@ -721,7 +721,7 @@ qla2x00_process_completed_request(struct scsi_qla_host *ha, uint32_t index)
 void
 qla2x00_process_response_queue(struct scsi_qla_host *ha)
 {
-       device_reg_t    *reg = ha->iobase;
+       device_reg_t __iomem *reg = ha->iobase;
        sts_entry_t     *pkt;
        uint16_t        handle_cnt;
        uint16_t        cnt;
@@ -817,17 +817,14 @@ qla2x00_process_response_queue(struct scsi_qla_host *ha)
 static void
 qla2x00_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
 {
-       int             ret;
        unsigned        b, t, l;
        srb_t           *sp;
-       os_lun_t        *lq;
-       os_tgt_t        *tq;
        fc_port_t       *fcport;
        struct scsi_cmnd *cp;
        uint16_t        comp_status;
        uint16_t        scsi_status;
        uint8_t         lscsi_status;
-       uint32_t        resid;
+       int32_t         resid;
        uint8_t         sense_sz = 0;
        uint16_t        rsp_info_len;
 
@@ -842,7 +839,7 @@ qla2x00_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
        /* Validate handle. */
        if (pkt->handle < MAX_OUTSTANDING_COMMANDS) {
                sp = ha->outstanding_cmds[pkt->handle];
-               ha->outstanding_cmds[pkt->handle] = 0;
+               ha->outstanding_cmds[pkt->handle] = NULL;
        } else
                sp = NULL;
 
@@ -871,21 +868,6 @@ qla2x00_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
        if (ha->actthreads)
                ha->actthreads--;
 
-       if (sp->lun_queue == NULL) {
-               DEBUG2(printk("scsi(%ld): Status Entry invalid lun pointer.\n",
-                   ha->host_no));
-               qla_printk(KERN_WARNING, ha,
-                   "Status Entry invalid lun pointer.\n");
-
-               set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
-               if (ha->dpc_wait && !ha->dpc_active) 
-                       up(ha->dpc_wait);
-
-               return;
-       }
-
-       sp->lun_queue->out_cnt--;
-
        comp_status = le16_to_cpu(pkt->comp_status);
        /* Mask of reserved bits 12-15, before we examine the scsi status */
        scsi_status = le16_to_cpu(pkt->scsi_status) & SS_MASK;
@@ -900,26 +882,7 @@ qla2x00_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
        t = cp->device->id;
        l = cp->device->lun,
 
-       tq = sp->tgt_queue;
-       lq = sp->lun_queue;
-
-       /*
-        * If loop is in transient state Report DID_BUS_BUSY
-        */
-       if ((comp_status != CS_COMPLETE || scsi_status != 0)) {
-               if (!(sp->flags & SRB_IOCTL) &&
-                   (atomic_read(&ha->loop_down_timer) ||
-                       atomic_read(&ha->loop_state) != LOOP_READY)) {
-
-                       DEBUG2(printk("scsi(%ld:%d:%d:%d): Loop Not Ready - "
-                           "pid=%lx.\n",
-                           ha->host_no, b, t, l, cp->serial_number));
-
-                       qla2x00_extend_timeout(cp, EXTEND_CMD_TIMEOUT);
-                       add_to_retry_queue(ha, sp);
-                       return;
-               }
-       }
+       fcport = sp->fcport;
 
        /* Check for any FCP transport errors. */
        if (scsi_status & SS_RESPONSE_INFO_LEN_VALID) {
@@ -934,7 +897,7 @@ qla2x00_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
                            pkt->rsp_info[6], pkt->rsp_info[7]));
 
                        cp->result = DID_BUS_BUSY << 16;
-                       add_to_done_queue(ha, sp);
+                       qla2x00_sp_compl(ha, sp);
                        return;
                }
        }
@@ -948,11 +911,11 @@ qla2x00_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
                        cp->result = DID_OK << 16;
                        break;
                }
-               if (lscsi_status == SS_BUSY_CONDITION) {
-                       cp->result = DID_BUS_BUSY << 16 | lscsi_status;
-                       break;
+               if (scsi_status & (SS_RESIDUAL_UNDER | SS_RESIDUAL_OVER)) {
+                       resid = le32_to_cpu(pkt->residual_length);
+                       cp->resid = resid;
+                       CMD_RESID_LEN(cp) = resid;
                }
-
                cp->result = DID_OK << 16 | lscsi_status;
 
                if (lscsi_status != SS_CHECK_CONDITION)
@@ -970,7 +933,7 @@ qla2x00_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
                    sizeof(cp->sense_buffer))
                        sense_sz = le16_to_cpu(pkt->req_sense_length);
                else
-                       sense_sz = sizeof(cp->sense_buffer) - 1;
+                       sense_sz = sizeof(cp->sense_buffer);
 
                CMD_ACTUAL_SNSLEN(cp) = sense_sz;
                sp->request_sense_length = sense_sz;
@@ -986,14 +949,6 @@ qla2x00_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
                if (sp->request_sense_length != 0)
                        ha->status_srb = sp;
 
-               if (!(sp->flags & SRB_IOCTL) &&
-                   qla2x00_check_sense(cp, lq) == QLA_SUCCESS) {
-                       /* Throw away status_cont if any */
-                       ha->status_srb = NULL;
-                       add_to_scsi_retry_queue(ha, sp);
-                       return;
-               }
-
                DEBUG5(printk("%s(): Check condition Sense data, "
                    "scsi(%ld:%d:%d:%d) cmd=%p pid=%ld\n",
                    __func__, ha->host_no, b, t, l, cp,
@@ -1009,19 +964,16 @@ qla2x00_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
                    ha->host_no, t, l, comp_status, scsi_status));
 
                resid = le32_to_cpu(pkt->residual_length);
-               CMD_RESID_LEN(cp) = resid;
+               if (scsi_status & SS_RESIDUAL_UNDER) {
+                       cp->resid = resid;
+                       CMD_RESID_LEN(cp) = resid;
+               }
 
                /*
                 * Check to see if SCSI Status is non zero. If so report SCSI 
                 * Status.
                 */
                if (lscsi_status != 0) {
-                       if (lscsi_status == SS_BUSY_CONDITION) {
-                               cp->result = DID_BUS_BUSY << 16 |
-                                   lscsi_status;
-                               break;
-                       }
-
                        cp->result = DID_OK << 16 | lscsi_status;
 
                        if (lscsi_status != SS_CHECK_CONDITION)
@@ -1037,7 +989,7 @@ qla2x00_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
                            sizeof(cp->sense_buffer))
                                sense_sz = le16_to_cpu(pkt->req_sense_length);
                        else
-                               sense_sz = sizeof(cp->sense_buffer) - 1;
+                               sense_sz = sizeof(cp->sense_buffer);
 
                        CMD_ACTUAL_SNSLEN(cp) = sense_sz;
                        sp->request_sense_length = sense_sz;
@@ -1053,12 +1005,6 @@ qla2x00_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
                        if (sp->request_sense_length != 0)
                                ha->status_srb = sp;
 
-                       if (!(sp->flags & SRB_IOCTL) &&
-                           (qla2x00_check_sense(cp, lq) == QLA_SUCCESS)) {
-                               ha->status_srb = NULL;
-                               add_to_scsi_retry_queue(ha, sp);
-                               return;
-                       }
                        DEBUG5(printk("%s(): Check condition Sense data, "
                            "scsi(%ld:%d:%d:%d) cmd=%p pid=%ld\n",
                            __func__, ha->host_no, b, t, l, cp,
@@ -1085,7 +1031,6 @@ qla2x00_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
                        }
 
                        /* Handle mid-layer underflow */
-                       cp->resid = resid;
                        if ((unsigned)(cp->request_bufflen - resid) <
                            cp->underflow) {
                                qla_printk(KERN_INFO, ha,
@@ -1131,30 +1076,15 @@ qla2x00_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
                 * Target with DID_NO_CONNECT ELSE Queue the IOs in the
                 * retry_queue.
                 */
-               fcport = sp->fclun->fcport;
                DEBUG2(printk("scsi(%ld:%d:%d): status_entry: Port Down "
                    "pid=%ld, compl status=0x%x, port state=0x%x\n",
                    ha->host_no, t, l, cp->serial_number, comp_status,
                    atomic_read(&fcport->state)));
 
-               if ((sp->flags & SRB_IOCTL) ||
-                   atomic_read(&fcport->state) == FCS_DEVICE_DEAD) {
-                       cp->result = DID_NO_CONNECT << 16;
-                       if (atomic_read(&ha->loop_state) == LOOP_DOWN) 
-                               sp->err_id = SRB_ERR_LOOP;
-                       else
-                               sp->err_id = SRB_ERR_PORT;
-                       add_to_done_queue(ha, sp);
-               } else {
-                       qla2x00_extend_timeout(cp, EXTEND_CMD_TIMEOUT);
-                       add_to_retry_queue(ha, sp);
-               }
-
+               cp->result = DID_BUS_BUSY << 16;
                if (atomic_read(&fcport->state) == FCS_ONLINE) {
                        qla2x00_mark_device_lost(ha, fcport, 1);
                }
-
-               return;
                break;
 
        case CS_RESET:
@@ -1162,13 +1092,7 @@ qla2x00_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
                    "scsi(%ld): RESET status detected 0x%x-0x%x.\n",
                    ha->host_no, comp_status, scsi_status));
 
-               if (sp->flags & SRB_IOCTL) {
-                       cp->result = DID_RESET << 16;
-               } else {
-                       qla2x00_extend_timeout(cp, EXTEND_CMD_TIMEOUT);
-                       add_to_retry_queue(ha, sp);
-                       return;
-               }
+               cp->result = DID_RESET << 16;
                break;
 
        case CS_ABORTED:
@@ -1186,13 +1110,12 @@ qla2x00_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
 
        case CS_TIMEOUT:
                DEBUG2(printk(KERN_INFO
-                   "scsi(%ld:%d:%d:%d): TIMEOUT status detected 0x%x-0x%x.\n",
-                   ha->host_no, b, t, l, comp_status, scsi_status));
+                   "scsi(%ld:%d:%d:%d): TIMEOUT status detected 0x%x-0x%x "
+                   "sflags=%x.\n", ha->host_no, b, t, l, comp_status,
+                   scsi_status, le16_to_cpu(pkt->status_flags)));
 
                cp->result = DID_BUS_BUSY << 16;
 
-               fcport = lq->fclun->fcport;
-
                /* Check to see if logout occurred */
                if ((le16_to_cpu(pkt->status_flags) & SF_LOGOUT_SENT)) {
                        qla2x00_mark_device_lost(ha, fcport, 1);
@@ -1208,16 +1131,6 @@ qla2x00_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
 
                cp->result = DID_OK << 16 | lscsi_status; 
 
-               /* TODO: ??? */
-               /* Adjust queue depth */
-               ret = scsi_track_queue_full(cp->device,
-                   sp->lun_queue->out_cnt - 1);
-               if (ret) {
-                       qla_printk(KERN_INFO, ha,
-                           "scsi(%ld:%d:%d:%d): Queue depth adjusted to %d.\n",
-                           ha->host_no, cp->device->channel, cp->device->id,
-                           cp->device->lun, ret);
-               }
                break;
 
        default:
@@ -1234,7 +1147,7 @@ qla2x00_status_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
 
        /* Place command on done queue. */
        if (ha->status_srb == NULL)
-               add_to_done_queue(ha, sp);
+               qla2x00_sp_compl(ha, sp);
 }
 
 /**
@@ -1279,8 +1192,8 @@ qla2x00_status_cont_entry(scsi_qla_host_t *ha, sts_cont_entry_t *pkt)
 
                /* Place command on done queue. */
                if (sp->request_sense_length == 0) {
-                       add_to_done_queue(ha, sp);
                        ha->status_srb = NULL;
+                       qla2x00_sp_compl(ha, sp);
                }
        }
 }
@@ -1319,11 +1232,9 @@ qla2x00_error_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
 
        if (sp) {
                /* Free outstanding command slot. */
-               ha->outstanding_cmds[pkt->handle] = 0;
+               ha->outstanding_cmds[pkt->handle] = NULL;
                if (ha->actthreads)
                        ha->actthreads--;
-               sp->lun_queue->out_cnt--;
-
                /* Bad payload or header */
                if (pkt->entry_status &
                    (RF_INV_E_ORDER | RF_INV_E_COUNT |
@@ -1334,8 +1245,7 @@ qla2x00_error_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
                } else {
                        sp->cmd->result = DID_ERROR << 16;
                }
-               /* Place command on done queue. */
-               add_to_done_queue(ha, sp);
+               qla2x00_sp_compl(ha, sp);
 
        } else if (pkt->entry_type == COMMAND_A64_TYPE ||
            pkt->entry_type == COMMAND_TYPE) {
@@ -1382,64 +1292,7 @@ qla2x00_ms_entry(scsi_qla_host_t *ha, ms_iocb_entry_t *pkt)
        CMD_ENTRY_STATUS(sp->cmd) = pkt->entry_status;
 
        /* Free outstanding command slot. */
-       ha->outstanding_cmds[pkt->handle1] = 0;
-
-       add_to_done_queue(ha, sp);
-}
-
-/**
- * qla2x00_check_sense() - Perform any sense data interrogation.
- * @cp: SCSI Command
- * @lq: Lun queue
- *
- * Returns QLA_SUCCESS if the lun queue is suspended, else
- * QLA_FUNCTION_FAILED  (lun queue not suspended).
- */
-static int 
-qla2x00_check_sense(struct scsi_cmnd *cp, os_lun_t *lq)
-{
-       scsi_qla_host_t *ha;
-       srb_t           *sp;
-       fc_port_t       *fcport;
-
-       ha = (scsi_qla_host_t *) cp->device->host->hostdata;
-       if ((cp->sense_buffer[0] & 0x70) != 0x70) {
-               return (QLA_FUNCTION_FAILED);
-       }
-
-       sp = (srb_t * )CMD_SP(cp);
-       sp->flags |= SRB_GOT_SENSE;
-
-       switch (cp->sense_buffer[2] & 0xf) {
-       case RECOVERED_ERROR:
-               cp->result = DID_OK << 16;
-               cp->sense_buffer[0] = 0;
-               break;
-
-       case NOT_READY:
-               fcport = lq->fclun->fcport;
-
-               /*
-                * Suspend the lun only for hard disk device type.
-                */
-               if ((fcport->flags & FCF_TAPE_PRESENT) == 0 &&
-                   lq->q_state != LUN_STATE_TIMEOUT) {
-                       /*
-                        * If target is in process of being ready then suspend
-                        * lun for 6 secs and retry all the commands.
-                        */
-                       if (cp->sense_buffer[12] == 0x4 &&
-                           cp->sense_buffer[13] == 0x1) {
-
-                               /* Suspend the lun for 6 secs */
-                               qla2x00_suspend_lun(ha, lq, 6,
-                                   ql2xsuspendcount);
-
-                               return (QLA_SUCCESS);
-                       }
-               }
-               break;
-       }
+       ha->outstanding_cmds[pkt->handle1] = NULL;
 
-       return (QLA_FUNCTION_FAILED);
+       qla2x00_sp_compl(ha, sp);
 }