]> err.no Git - linux-2.6/blobdiff - drivers/scsi/tmscsim.c
Merge master.kernel.org:/pub/scm/linux/kernel/git/lethal/sh-2.6.23
[linux-2.6] / drivers / scsi / tmscsim.c
index 12d8fea3de933f44334cad708b0995daf632ca49..5db1520f8ba968a48a9bcc783d2e100cc5d0c98f 100644 (file)
@@ -351,6 +351,27 @@ static u8  dc390_clock_speed[] = {100,80,67,57,50, 40, 31, 20};
  * (DCBs, SRBs, Queueing)
  *
  **********************************************************************/
+static void inline dc390_start_segment(struct dc390_srb* pSRB)
+{
+       struct scatterlist *psgl = pSRB->pSegmentList;
+
+       /* start new sg segment */
+       pSRB->SGBusAddr = sg_dma_address(psgl);
+       pSRB->SGToBeXferLen = sg_dma_len(psgl);
+}
+
+static unsigned long inline dc390_advance_segment(struct dc390_srb* pSRB, u32 residue)
+{
+       unsigned long xfer = pSRB->SGToBeXferLen - residue;
+
+       /* xfer more bytes transferred */
+       pSRB->SGBusAddr += xfer;
+       pSRB->TotalXferredLen += xfer;
+       pSRB->SGToBeXferLen = residue;
+
+       return xfer;
+}
+
 static struct dc390_dcb __inline__ *dc390_findDCB ( struct dc390_acb* pACB, u8 id, u8 lun)
 {
    struct dc390_dcb* pDCB = pACB->pLinkDCB; if (!pDCB) return NULL;
@@ -436,28 +457,21 @@ static int dc390_pci_map (struct dc390_srb* pSRB)
                        error = 1;
                DEBUG1(printk("%s(): Mapped sense buffer %p at %x\n", __FUNCTION__, pcmd->sense_buffer, cmdp->saved_dma_handle));
        /* Map SG list */
-       } else if (pcmd->use_sg) {
-               pSRB->pSegmentList      = (struct scatterlist *) pcmd->request_buffer;
-               pSRB->SGcount           = pci_map_sg(pdev, pSRB->pSegmentList, pcmd->use_sg,
-                                                    pcmd->sc_data_direction);
+       } else if (scsi_sg_count(pcmd)) {
+               int nseg;
+
+               nseg = scsi_dma_map(pcmd);
+
+               pSRB->pSegmentList      = scsi_sglist(pcmd);
+               pSRB->SGcount           = nseg;
+
                /* TODO: error handling */
-               if (!pSRB->SGcount)
+               if (nseg < 0)
                        error = 1;
                DEBUG1(printk("%s(): Mapped SG %p with %d (%d) elements\n",\
-                             __FUNCTION__, pcmd->request_buffer, pSRB->SGcount, pcmd->use_sg));
+                             __FUNCTION__, scsi_sglist(pcmd), nseg, scsi_sg_count(pcmd)));
        /* Map single segment */
-       } else if (pcmd->request_buffer && pcmd->request_bufflen) {
-               pSRB->pSegmentList      = dc390_sg_build_single(&pSRB->Segmentx, pcmd->request_buffer, pcmd->request_bufflen);
-               pSRB->SGcount           = pci_map_sg(pdev, pSRB->pSegmentList, 1,
-                                                    pcmd->sc_data_direction);
-               cmdp->saved_dma_handle  = sg_dma_address(pSRB->pSegmentList);
-
-               /* TODO: error handling */
-               if (pSRB->SGcount != 1)
-                       error = 1;
-               DEBUG1(printk("%s(): Mapped request buffer %p at %x\n", __FUNCTION__, pcmd->request_buffer, cmdp->saved_dma_handle));
-       /* No mapping !? */     
-       } else
+       } else
                pSRB->SGcount = 0;
 
        return error;
@@ -473,12 +487,10 @@ static void dc390_pci_unmap (struct dc390_srb* pSRB)
        if (pSRB->SRBFlag) {
                pci_unmap_sg(pdev, &pSRB->Segmentx, 1, DMA_FROM_DEVICE);
                DEBUG1(printk("%s(): Unmapped sense buffer at %x\n", __FUNCTION__, cmdp->saved_dma_handle));
-       } else if (pcmd->use_sg) {
-               pci_unmap_sg(pdev, pcmd->request_buffer, pcmd->use_sg, pcmd->sc_data_direction);
-               DEBUG1(printk("%s(): Unmapped SG at %p with %d elements\n", __FUNCTION__, pcmd->request_buffer, pcmd->use_sg));
-       } else if (pcmd->request_buffer && pcmd->request_bufflen) {
-               pci_unmap_sg(pdev, &pSRB->Segmentx, 1, pcmd->sc_data_direction);
-               DEBUG1(printk("%s(): Unmapped request buffer at %x\n", __FUNCTION__, cmdp->saved_dma_handle));
+       } else {
+               scsi_dma_unmap(pcmd);
+               DEBUG1(printk("%s(): Unmapped SG at %p with %d elements\n",
+                             __FUNCTION__, scsi_sglist(pcmd), scsi_sg_count(pcmd)));
        }
 }
 
@@ -741,11 +753,10 @@ static irqreturn_t do_DC390_Interrupt(int irq, void *dev_id)
 }
 
 static void
-dc390_DataOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
+dc390_DataOut_0(struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
 {
     u8   sstatus;
-    struct scatterlist *psgl;
-    u32    ResidCnt, xferCnt;
+    u32  ResidCnt;
     u8   dstate = 0;
 
     sstatus = *psstatus;
@@ -776,25 +787,20 @@ dc390_DataOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
            if( pSRB->SGIndex < pSRB->SGcount )
            {
                pSRB->pSegmentList++;
-               psgl = pSRB->pSegmentList;
 
-               pSRB->SGBusAddr = sg_dma_address(psgl);
-               pSRB->SGToBeXferLen = sg_dma_len(psgl);
+               dc390_start_segment(pSRB);
            }
            else
                pSRB->SGToBeXferLen = 0;
        }
        else
        {
-           ResidCnt  = (u32) DC390_read8 (Current_Fifo) & 0x1f;
-           ResidCnt |= (u32) DC390_read8 (CtcReg_High) << 16;
-           ResidCnt |= (u32) DC390_read8 (CtcReg_Mid) << 8; 
-           ResidCnt += (u32) DC390_read8 (CtcReg_Low);
-
-           xferCnt = pSRB->SGToBeXferLen - ResidCnt;
-           pSRB->SGBusAddr += xferCnt;
-           pSRB->TotalXferredLen += xferCnt;
-           pSRB->SGToBeXferLen = ResidCnt;
+           ResidCnt = ((u32) DC390_read8 (Current_Fifo) & 0x1f) +
+                   (((u32) DC390_read8 (CtcReg_High) << 16) |
+                    ((u32) DC390_read8 (CtcReg_Mid) << 8) |
+                    (u32) DC390_read8 (CtcReg_Low));
+
+           dc390_advance_segment(pSRB, ResidCnt);
        }
     }
     if ((*psstatus & 7) != SCSI_DATA_OUT)
@@ -805,13 +811,11 @@ dc390_DataOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
 }
 
 static void
-dc390_DataIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
+dc390_DataIn_0(struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
 {
     u8   sstatus, residual, bval;
-    struct scatterlist *psgl;
-    u32    ResidCnt, i;
+    u32  ResidCnt, i;
     unsigned long   xferCnt;
-    u8      *ptr;
 
     sstatus = *psstatus;
 
@@ -851,10 +855,8 @@ dc390_DataIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
            if( pSRB->SGIndex < pSRB->SGcount )
            {
                pSRB->pSegmentList++;
-               psgl = pSRB->pSegmentList;
 
-               pSRB->SGBusAddr = sg_dma_address(psgl);
-               pSRB->SGToBeXferLen = sg_dma_len(psgl);
+               dc390_start_segment(pSRB);
            }
            else
                pSRB->SGToBeXferLen = 0;
@@ -894,41 +896,38 @@ din_1:
            /* It seems a DMA Blast abort isn't that bad ... */
            if (!i) printk (KERN_ERR "DC390: DMA Blast aborted unfinished!\n");
            //DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_IDLE_CMD);
-           dc390_laststatus &= ~0xff000000; dc390_laststatus |= bval << 24;
+           dc390_laststatus &= ~0xff000000;
+           dc390_laststatus |= bval << 24;
 
            DEBUG1(printk (KERN_DEBUG "Blast: Read %i times DMA_Status %02x", 0xa000-i, bval));
-           ResidCnt = (u32) DC390_read8 (CtcReg_High);
-           ResidCnt <<= 8;
-           ResidCnt |= (u32) DC390_read8 (CtcReg_Mid);
-           ResidCnt <<= 8;
-           ResidCnt |= (u32) DC390_read8 (CtcReg_Low);
-
-           xferCnt = pSRB->SGToBeXferLen - ResidCnt;
-           pSRB->SGBusAddr += xferCnt;
-           pSRB->TotalXferredLen += xferCnt;
-           pSRB->SGToBeXferLen = ResidCnt;
-
-           if( residual )
-           {
-               static int feedback_requested;
+           ResidCnt = (((u32) DC390_read8 (CtcReg_High) << 16) |
+                       ((u32) DC390_read8 (CtcReg_Mid) << 8)) |
+                   (u32) DC390_read8 (CtcReg_Low);
+
+           xferCnt = dc390_advance_segment(pSRB, ResidCnt);
+
+           if (residual) {
+               size_t count = 1;
+               size_t offset = pSRB->SGBusAddr - sg_dma_address(pSRB->pSegmentList);
+               unsigned long flags;
+               u8 *ptr;
+
                bval = DC390_read8 (ScsiFifo);      /* get one residual byte */
 
-               if (!feedback_requested) {
-                       feedback_requested = 1;
-                       printk(KERN_WARNING "%s: Please, contact <linux-scsi@vger.kernel.org> "
-                              "to help improve support for your system.\n", __FILE__);
+               local_irq_save(flags);
+               ptr = scsi_kmap_atomic_sg(pSRB->pSegmentList, pSRB->SGcount, &offset, &count);
+               if (likely(ptr)) {
+                       *(ptr + offset) = bval;
+                       scsi_kunmap_atomic_sg(ptr);
                }
+               local_irq_restore(flags);
+               WARN_ON(!ptr);
 
-               ptr = (u8 *) bus_to_virt( pSRB->SGBusAddr );
-               *ptr = bval;
-               pSRB->SGBusAddr++;
-               xferCnt++;
-               pSRB->TotalXferredLen++;
-               pSRB->SGToBeXferLen--;
+               /* 1 more byte read */
+               xferCnt += dc390_advance_segment(pSRB, pSRB->SGToBeXferLen - 1);
            }
            DEBUG1(printk (KERN_DEBUG "Xfered: %lu, Total: %lu, Remaining: %lu\n", xferCnt,\
                           pSRB->TotalXferredLen, pSRB->SGToBeXferLen));
-
        }
     }
     if ((*psstatus & 7) != SCSI_DATA_IN)
@@ -1137,7 +1136,7 @@ dc390_MsgIn_set_sync (struct dc390_acb* pACB, struct dc390_srb* pSRB)
 
 
 /* handle RESTORE_PTR */
-/* I presume, this command is already mapped, so, have to remap. */
+/* This doesn't look very healthy... to-be-fixed */
 static void 
 dc390_restore_ptr (struct dc390_acb* pACB, struct dc390_srb* pSRB)
 {
@@ -1145,8 +1144,9 @@ dc390_restore_ptr (struct dc390_acb* pACB, struct dc390_srb* pSRB)
     struct scatterlist *psgl;
     pSRB->TotalXferredLen = 0;
     pSRB->SGIndex = 0;
-    if (pcmd->use_sg) {
-       pSRB->pSegmentList = (struct scatterlist *)pcmd->request_buffer;
+    if (scsi_sg_count(pcmd)) {
+       size_t saved;
+       pSRB->pSegmentList = scsi_sglist(pcmd);
        psgl = pSRB->pSegmentList;
        //dc390_pci_sync(pSRB);
 
@@ -1157,24 +1157,19 @@ dc390_restore_ptr (struct dc390_acb* pACB, struct dc390_srb* pSRB)
            if( pSRB->SGIndex < pSRB->SGcount )
            {
                pSRB->pSegmentList++;
-               psgl = pSRB->pSegmentList;
-               pSRB->SGBusAddr = sg_dma_address(psgl);
-               pSRB->SGToBeXferLen = sg_dma_len(psgl);
+
+               dc390_start_segment(pSRB);
            }
            else
                pSRB->SGToBeXferLen = 0;
        }
-       pSRB->SGToBeXferLen -= pSRB->Saved_Ptr - pSRB->TotalXferredLen;
-       pSRB->SGBusAddr += pSRB->Saved_Ptr - pSRB->TotalXferredLen;
+
+       saved = pSRB->Saved_Ptr - pSRB->TotalXferredLen;
+       pSRB->SGToBeXferLen -= saved;
+       pSRB->SGBusAddr += saved;
        printk (KERN_INFO "DC390: Pointer restored. Segment %i, Total %li, Bus %08lx\n",
                pSRB->SGIndex, pSRB->Saved_Ptr, pSRB->SGBusAddr);
 
-    } else if(pcmd->request_buffer) {
-       //dc390_pci_sync(pSRB);
-
-       sg_dma_len(&pSRB->Segmentx) = pcmd->request_bufflen - pSRB->Saved_Ptr;
-       pSRB->SGcount = 1;
-       pSRB->pSegmentList = (struct scatterlist *) &pSRB->Segmentx;
     } else {
         pSRB->SGcount = 0;
         printk (KERN_INFO "DC390: RESTORE_PTR message for Transfer without Scatter-Gather ??\n");
@@ -1286,7 +1281,6 @@ dc390_MsgIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
 static void
 dc390_DataIO_Comm( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 ioDir)
 {
-    struct scatterlist *psgl;
     unsigned long  lval;
     struct dc390_dcb*   pDCB = pACB->pActiveDCB;
 
@@ -1315,9 +1309,8 @@ dc390_DataIO_Comm( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 ioDir)
        DC390_write8 (DMA_Cmd, DMA_IDLE_CMD | ioDir);
        if( !pSRB->SGToBeXferLen )
        {
-           psgl = pSRB->pSegmentList;
-           pSRB->SGBusAddr = sg_dma_address(psgl);
-           pSRB->SGToBeXferLen = sg_dma_len(psgl);
+           dc390_start_segment(pSRB);
+
            DEBUG1(printk (KERN_DEBUG " DC390: Next SG segment."));
        }
        lval = pSRB->SGToBeXferLen;
@@ -1571,7 +1564,8 @@ dc390_Disconnect( struct dc390_acb* pACB )
        if( (pSRB->SRBState & (SRB_START_+SRB_MSGOUT)) ||
           !(pSRB->SRBState & (SRB_DISCONNECT+SRB_COMPLETED)) )
        {       /* Selection time out */
-               pSRB->TargetStatus = SCSI_STAT_SEL_TIMEOUT;
+               pSRB->AdaptStatus = H_SEL_TIMEOUT;
+               pSRB->TargetStatus = 0;
                goto  disc1;
        }
        else if (!(pSRB->SRBState & SRB_DISCONNECT) && (pSRB->SRBState & SRB_COMPLETED))
@@ -1604,7 +1598,7 @@ dc390_Reselect( struct dc390_acb* pACB )
        if( !( pACB->scan_devices ) )
        {
            struct scsi_cmnd *pcmd = pSRB->pcmd;
-           pcmd->resid = pcmd->request_bufflen;
+           scsi_set_resid(pcmd, scsi_bufflen(pcmd));
            SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
            dc390_Going_remove(pDCB, pSRB);
            dc390_Free_insert(pACB, pSRB);
@@ -1687,7 +1681,6 @@ dc390_RequestSense(struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_
                              pcmd->cmnd[0], pDCB->TargetID, pDCB->TargetLUN));
 
        pSRB->SRBFlag |= AUTO_REQSENSE;
-       pSRB->SavedSGCount = pcmd->use_sg;
        pSRB->SavedTotXLen = pSRB->TotalXferredLen;
        pSRB->AdaptStatus = 0;
        pSRB->TargetStatus = 0; /* CHECK_CONDITION<<1; */
@@ -1720,22 +1713,21 @@ dc390_SRBdone( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb*
     {  /* Last command was a Request Sense */
        pSRB->SRBFlag &= ~AUTO_REQSENSE;
        pSRB->AdaptStatus = 0;
-       pSRB->TargetStatus = CHECK_CONDITION << 1;
+       pSRB->TargetStatus = SAM_STAT_CHECK_CONDITION;
 
        //pcmd->result = MK_RES(DRIVER_SENSE,DID_OK,0,status);
-       if (status == (CHECK_CONDITION << 1))
+       if (status == SAM_STAT_CHECK_CONDITION)
            pcmd->result = MK_RES_LNX(0, DID_BAD_TARGET, 0, /*CHECK_CONDITION*/0);
        else /* Retry */
        {
            if( pSRB->pcmd->cmnd[0] == TEST_UNIT_READY /* || pSRB->pcmd->cmnd[0] == START_STOP */)
            {
                /* Don't retry on TEST_UNIT_READY */
-               pcmd->result = MK_RES_LNX(DRIVER_SENSE,DID_OK,0,CHECK_CONDITION);
+               pcmd->result = MK_RES_LNX(DRIVER_SENSE, DID_OK, 0, SAM_STAT_CHECK_CONDITION);
                REMOVABLEDEBUG(printk(KERN_INFO "Cmd=%02x, Result=%08x, XferL=%08x\n",pSRB->pcmd->cmnd[0],\
                       (u32) pcmd->result, (u32) pSRB->TotalXferredLen));
            } else {
                SET_RES_DRV(pcmd->result, DRIVER_SENSE);
-               pcmd->use_sg = pSRB->SavedSGCount;
                //pSRB->ScsiCmdLen       = (u8) (pSRB->Segment1[0] >> 8);
                DEBUG0 (printk ("DC390: RETRY pid %li (%02x), target %02i-%02i\n", pcmd->pid, pcmd->cmnd[0], pcmd->device->id, pcmd->device->lun));
                pSRB->TotalXferredLen = 0;
@@ -1746,7 +1738,7 @@ dc390_SRBdone( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb*
     }
     if( status )
     {
-       if( status_byte(status) == CHECK_CONDITION )
+       if (status == SAM_STAT_CHECK_CONDITION)
        {
            if (dc390_RequestSense(pACB, pDCB, pSRB)) {
                SET_RES_DID(pcmd->result, DID_ERROR);
@@ -1754,22 +1746,14 @@ dc390_SRBdone( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb*
            }
            return;
        }
-       else if( status_byte(status) == QUEUE_FULL )
+       else if (status == SAM_STAT_TASK_SET_FULL)
        {
            scsi_track_queue_full(pcmd->device, pDCB->GoingSRBCnt - 1);
-           pcmd->use_sg = pSRB->SavedSGCount;
            DEBUG0 (printk ("DC390: RETRY pid %li (%02x), target %02i-%02i\n", pcmd->pid, pcmd->cmnd[0], pcmd->device->id, pcmd->device->lun));
            pSRB->TotalXferredLen = 0;
            SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
        }
-       else if(status == SCSI_STAT_SEL_TIMEOUT)
-       {
-           pSRB->AdaptStatus = H_SEL_TIMEOUT;
-           pSRB->TargetStatus = 0;
-           pcmd->result = MK_RES(0,DID_NO_CONNECT,0,0);
-           /* Devices are removed below ... */
-       }
-       else if (status_byte(status) == BUSY && 
+       else if (status == SAM_STAT_BUSY &&
                 (pcmd->cmnd[0] == TEST_UNIT_READY || pcmd->cmnd[0] == INQUIRY) &&
                 pACB->scan_devices)
        {
@@ -1787,12 +1771,17 @@ dc390_SRBdone( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb*
     else
     {  /*  Target status == 0 */
        status = pSRB->AdaptStatus;
-       if(status & H_OVER_UNDER_RUN)
+       if (status == H_OVER_UNDER_RUN)
        {
            pSRB->TargetStatus = 0;
            SET_RES_DID(pcmd->result,DID_OK);
            SET_RES_MSG(pcmd->result,pSRB->EndMessage);
        }
+       else if (status == H_SEL_TIMEOUT)
+       {
+           pcmd->result = MK_RES(0, DID_NO_CONNECT, 0, 0);
+           /* Devices are removed below ... */
+       }
        else if( pSRB->SRBStatus & PARITY_ERROR)
        {
            //pcmd->result = MK_RES(0,DID_PARITY,pSRB->EndMessage,0);
@@ -1808,7 +1797,7 @@ dc390_SRBdone( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb*
     }
 
 cmd_done:
-    pcmd->resid = pcmd->request_bufflen - pSRB->TotalXferredLen;
+    scsi_set_resid(pcmd, scsi_bufflen(pcmd) - pSRB->TotalXferredLen);
 
     dc390_Going_remove (pDCB, pSRB);
     /* Add to free list */
@@ -2093,10 +2082,9 @@ static int dc390_slave_alloc(struct scsi_device *scsi_device)
        uint id = scsi_device->id;
        uint lun = scsi_device->lun;
 
-       pDCB = kmalloc(sizeof(struct dc390_dcb), GFP_KERNEL);
+       pDCB = kzalloc(sizeof(struct dc390_dcb), GFP_KERNEL);
        if (!pDCB)
                return -ENOMEM;
-       memset(pDCB, 0, sizeof(struct dc390_dcb));
 
        if (!pACB->DCBCnt++) {
                pACB->pLinkDCB = pDCB;