]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - drivers/scsi/53c700.c
[SCSI] megaraid_sas: Fix FastPath I/O to work on degraded raid 1
[linux-2.6.git] / drivers / scsi / 53c700.c
index e7ad269041a4bdc36b1c95c8a24996cae519baa6..f672491774eb61756c2a36f8af3a36496a7ea53c 100644 (file)
  * */
 #define NCR_700_VERSION "2.8"
 
-#include <linux/config.h>
 #include <linux/kernel.h>
 #include <linux/types.h>
 #include <linux/string.h>
+#include <linux/slab.h>
 #include <linux/ioport.h>
 #include <linux/delay.h>
 #include <linux/spinlock.h>
 #include <linux/completion.h>
-#include <linux/sched.h>
 #include <linux/init.h>
 #include <linux/proc_fs.h>
 #include <linux/blkdev.h>
@@ -168,15 +167,16 @@ MODULE_LICENSE("GPL");
 #include "53c700_d.h"
 
 
-STATIC int NCR_700_queuecommand(struct scsi_cmnd *, void (*done)(struct scsi_cmnd *));
+STATIC int NCR_700_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *);
 STATIC int NCR_700_abort(struct scsi_cmnd * SCpnt);
 STATIC int NCR_700_bus_reset(struct scsi_cmnd * SCpnt);
 STATIC int NCR_700_host_reset(struct scsi_cmnd * SCpnt);
 STATIC void NCR_700_chip_setup(struct Scsi_Host *host);
 STATIC void NCR_700_chip_reset(struct Scsi_Host *host);
+STATIC int NCR_700_slave_alloc(struct scsi_device *SDpnt);
 STATIC int NCR_700_slave_configure(struct scsi_device *SDpnt);
 STATIC void NCR_700_slave_destroy(struct scsi_device *SDpnt);
-static int NCR_700_change_queue_depth(struct scsi_device *SDpnt, int depth);
+static int NCR_700_change_queue_depth(struct scsi_device *SDpnt, int depth, int reason);
 static int NCR_700_change_queue_type(struct scsi_device *SDpnt, int depth);
 
 STATIC struct device_attribute *NCR_700_dev_attrs[];
@@ -238,14 +238,6 @@ static char *NCR_700_SBCL_to_phase[] = {
        "MSG IN",
 };
 
-static __u8 NCR_700_SDTR_msg[] = {
-       0x01,                   /* Extended message */
-       0x03,                   /* Extended message Length */
-       0x01,                   /* SDTR Extended message */
-       NCR_700_MIN_PERIOD,
-       NCR_700_MAX_OFFSET
-};
-
 /* This translates the SDTR message offset and period to a value
  * which can be loaded into the SXFER_REG.
  *
@@ -266,7 +258,7 @@ NCR_700_offset_period_to_sxfer(struct NCR_700_Host_Parameters *hostdata,
                return 0;
 
        if(period < hostdata->min_period) {
-               printk(KERN_WARNING "53c700: Period %dns is less than this chip's minimum, setting to %d\n", period*4, NCR_700_SDTR_msg[3]*4);
+               printk(KERN_WARNING "53c700: Period %dns is less than this chip's minimum, setting to %d\n", period*4, NCR_700_MIN_PERIOD*4);
                period = hostdata->min_period;
        }
        XFERP = (period*4 * hostdata->sync_clock)/1000 - 4;
@@ -276,8 +268,6 @@ NCR_700_offset_period_to_sxfer(struct NCR_700_Host_Parameters *hostdata,
                offset = max_offset;
        }
        if(XFERP < min_xferp) {
-               printk(KERN_WARNING "53c700: XFERP %d is less than minium, setting to %d\n",
-                      XFERP,  min_xferp);
                XFERP =  min_xferp;
        }
        return (offset & 0x0f) | (XFERP & 0x07)<<4;
@@ -319,12 +309,9 @@ NCR_700_detect(struct scsi_host_template *tpnt,
        hostdata->msgin = memory + MSGIN_OFFSET;
        hostdata->msgout = memory + MSGOUT_OFFSET;
        hostdata->status = memory + STATUS_OFFSET;
-       /* all of these offsets are L1_CACHE_BYTES separated.  It is fatal
-        * if this isn't sufficient separation to avoid dma flushing issues */
-       BUG_ON(!dma_is_consistent(pScript) && L1_CACHE_BYTES < dma_get_cache_alignment());
        hostdata->slots = (struct NCR_700_command_slot *)(memory + SLOTS_OFFSET);
        hostdata->dev = dev;
-               
+
        pSlots = pScript + SLOTS_OFFSET;
 
        /* Fill in the missing routines from the host template */
@@ -338,21 +325,21 @@ NCR_700_detect(struct scsi_host_template *tpnt,
        tpnt->use_clustering = ENABLE_CLUSTERING;
        tpnt->slave_configure = NCR_700_slave_configure;
        tpnt->slave_destroy = NCR_700_slave_destroy;
+       tpnt->slave_alloc = NCR_700_slave_alloc;
        tpnt->change_queue_depth = NCR_700_change_queue_depth;
        tpnt->change_queue_type = NCR_700_change_queue_type;
-       
+
        if(tpnt->name == NULL)
                tpnt->name = "53c700";
        if(tpnt->proc_name == NULL)
                tpnt->proc_name = "53c700";
-       
 
        host = scsi_host_alloc(tpnt, 4);
        if (!host)
                return NULL;
        memset(hostdata->slots, 0, sizeof(struct NCR_700_command_slot)
               * NCR_700_COMMAND_SLOTS_PER_HOST);
-       for(j = 0; j < NCR_700_COMMAND_SLOTS_PER_HOST; j++) {
+       for (j = 0; j < NCR_700_COMMAND_SLOTS_PER_HOST; j++) {
                dma_addr_t offset = (dma_addr_t)((unsigned long)&hostdata->slots[j].SG[0]
                                          - (unsigned long)&hostdata->slots[0].SG[0]);
                hostdata->slots[j].pSG = (struct NCR_700_SG_List *)((unsigned long)(pSlots + offset));
@@ -363,20 +350,18 @@ NCR_700_detect(struct scsi_host_template *tpnt,
                hostdata->slots[j].state = NCR_700_SLOT_FREE;
        }
 
-       for(j = 0; j < sizeof(SCRIPT)/sizeof(SCRIPT[0]); j++) {
+       for (j = 0; j < ARRAY_SIZE(SCRIPT); j++)
                script[j] = bS_to_host(SCRIPT[j]);
-       }
 
        /* adjust all labels to be bus physical */
-       for(j = 0; j < PATCHES; j++) {
+       for (j = 0; j < PATCHES; j++)
                script[LABELPATCHES[j]] = bS_to_host(pScript + SCRIPT[LABELPATCHES[j]]);
-       }
        /* now patch up fixed addresses. */
-       script_patch_32(script, MessageLocation,
+       script_patch_32(hostdata->dev, script, MessageLocation,
                        pScript + MSGOUT_OFFSET);
-       script_patch_32(script, StatusAddress,
+       script_patch_32(hostdata->dev, script, StatusAddress,
                        pScript + STATUS_OFFSET);
-       script_patch_32(script, ReceiveMsgAddress,
+       script_patch_32(hostdata->dev, script, ReceiveMsgAddress,
                        pScript + MSGIN_OFFSET);
 
        hostdata->script = script;
@@ -384,7 +369,7 @@ NCR_700_detect(struct scsi_host_template *tpnt,
        dma_sync_single_for_device(hostdata->dev, pScript, sizeof(SCRIPT), DMA_TO_DEVICE);
        hostdata->state = NCR_700_HOST_FREE;
        hostdata->cmd = NULL;
-       host->max_id = 7;
+       host->max_id = 8;
        host->max_lun = NCR_700_MAX_LUNS;
        BUG_ON(NCR_700_transport_template == NULL);
        host->transportt = NCR_700_transport_template;
@@ -393,17 +378,17 @@ NCR_700_detect(struct scsi_host_template *tpnt,
        host->hostdata[0] = (unsigned long)hostdata;
        /* kick the chip */
        NCR_700_writeb(0xff, host, CTEST9_REG);
-       if(hostdata->chip710) 
+       if (hostdata->chip710)
                hostdata->rev = (NCR_700_readb(host, CTEST8_REG)>>4) & 0x0f;
        else
                hostdata->rev = (NCR_700_readb(host, CTEST7_REG)>>4) & 0x0f;
        hostdata->fast = (NCR_700_readb(host, CTEST9_REG) == 0);
-       if(banner == 0) {
+       if (banner == 0) {
                printk(KERN_NOTICE "53c700: Version " NCR_700_VERSION " By James.Bottomley@HansenPartnership.com\n");
                banner = 1;
        }
        printk(KERN_NOTICE "scsi%d: %s rev %d %s\n", host->host_no,
-              hostdata->chip710 ? "53c710" : 
+              hostdata->chip710 ? "53c710" :
               (hostdata->fast ? "53c700-66" : "53c700"),
               hostdata->rev, hostdata->differential ?
               "(Differential)" : "");
@@ -548,6 +533,7 @@ find_empty_slot(struct NCR_700_Host_Parameters *hostdata)
         * finish routine.  If we cannot queue the command when it
         * is properly build, we then change to NCR_700_SLOT_QUEUED */
        slot->state = NCR_700_SLOT_BUSY;
+       slot->flags = 0;
        hostdata->command_slot_count++;
        
        return slot;
@@ -595,16 +581,8 @@ NCR_700_unmap(struct NCR_700_Host_Parameters *hostdata, struct scsi_cmnd *SCp,
              struct NCR_700_command_slot *slot)
 {
        if(SCp->sc_data_direction != DMA_NONE &&
-          SCp->sc_data_direction != DMA_BIDIRECTIONAL) {
-               if(SCp->use_sg) {
-                       dma_unmap_sg(hostdata->dev, SCp->buffer,
-                                    SCp->use_sg, SCp->sc_data_direction);
-               } else {
-                       dma_unmap_single(hostdata->dev, slot->dma_handle,
-                                        SCp->request_bufflen,
-                                        SCp->sc_data_direction);
-               }
-       }
+          SCp->sc_data_direction != DMA_BIDIRECTIONAL)
+               scsi_dma_unmap(SCp);
 }
 
 STATIC inline void
@@ -618,31 +596,27 @@ NCR_700_scsi_done(struct NCR_700_Host_Parameters *hostdata,
                struct NCR_700_command_slot *slot = 
                        (struct NCR_700_command_slot *)SCp->host_scribble;
                
-               NCR_700_unmap(hostdata, SCp, slot);
                dma_unmap_single(hostdata->dev, slot->pCmd,
-                                sizeof(SCp->cmnd), DMA_TO_DEVICE);
-               if(SCp->cmnd[0] == REQUEST_SENSE && SCp->cmnd[6] == NCR_700_INTERNAL_SENSE_MAGIC) {
+                                MAX_COMMAND_SIZE, DMA_TO_DEVICE);
+               if (slot->flags == NCR_700_FLAG_AUTOSENSE) {
+                       char *cmnd = NCR_700_get_sense_cmnd(SCp->device);
 #ifdef NCR_700_DEBUG
                        printk(" ORIGINAL CMD %p RETURNED %d, new return is %d sense is\n",
                               SCp, SCp->cmnd[7], result);
                        scsi_print_sense("53c700", SCp);
 
 #endif
+                       dma_unmap_single(hostdata->dev, slot->dma_handle,
+                                        SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
                        /* restore the old result if the request sense was
                         * successful */
-                       if(result == 0)
-                               result = SCp->cmnd[7];
-                       /* now restore the original command */
-                       memcpy((void *) SCp->cmnd, (void *) SCp->data_cmnd,
-                              sizeof(SCp->data_cmnd));
-                       SCp->request_buffer = SCp->buffer;
-                       SCp->request_bufflen = SCp->bufflen;
-                       SCp->use_sg = SCp->old_use_sg;
-                       SCp->cmd_len = SCp->old_cmd_len;
-                       SCp->sc_data_direction = SCp->sc_old_data_direction;
-                       SCp->underflow = SCp->old_underflow;
-                       
-               }
+                       if (result == 0)
+                               result = cmnd[7];
+                       /* restore the original length */
+                       SCp->cmd_len = cmnd[8];
+               } else
+                       NCR_700_unmap(hostdata, SCp, slot);
+
                free_slot(slot, hostdata);
 #ifdef NCR_700_DEBUG
                if(NCR_700_get_depth(SCp->device) == 0 ||
@@ -676,20 +650,38 @@ NCR_700_chip_setup(struct Scsi_Host *host)
 {
        struct NCR_700_Host_Parameters *hostdata = 
                (struct NCR_700_Host_Parameters *)host->hostdata[0];
-       __u32 dcntl_extra = 0;
        __u8 min_period;
        __u8 min_xferp = (hostdata->chip710 ? NCR_710_MIN_XFERP : NCR_700_MIN_XFERP);
 
        if(hostdata->chip710) {
-               __u8 burst_disable = hostdata->burst_disable
-                       ? BURST_DISABLE : 0;
-               dcntl_extra = COMPAT_700_MODE;
+               __u8 burst_disable = 0;
+               __u8 burst_length = 0;
+
+               switch (hostdata->burst_length) {
+                       case 1:
+                               burst_length = BURST_LENGTH_1;
+                               break;
+                       case 2:
+                               burst_length = BURST_LENGTH_2;
+                               break;
+                       case 4:
+                               burst_length = BURST_LENGTH_4;
+                               break;
+                       case 8:
+                               burst_length = BURST_LENGTH_8;
+                               break;
+                       default:
+                               burst_disable = BURST_DISABLE;
+                               break;
+               }
+               hostdata->dcntl_extra |= COMPAT_700_MODE;
 
-               NCR_700_writeb(dcntl_extra, host, DCNTL_REG);
-               NCR_700_writeb(BURST_LENGTH_8  | hostdata->dmode_extra,
+               NCR_700_writeb(hostdata->dcntl_extra, host, DCNTL_REG);
+               NCR_700_writeb(burst_length | hostdata->dmode_extra,
                               host, DMODE_710_REG);
-               NCR_700_writeb(burst_disable | (hostdata->differential ? 
-                                               DIFF : 0), host, CTEST7_REG);
+               NCR_700_writeb(burst_disable | hostdata->ctest7_extra |
+                              (hostdata->differential ? DIFF : 0),
+                              host, CTEST7_REG);
                NCR_700_writeb(BTB_TIMER_DISABLE, host, CTEST0_REG);
                NCR_700_writeb(FULL_ARBITRATION | ENABLE_PARITY | PARITY
                               | AUTO_ATN, host, SCNTL0_REG);
@@ -724,13 +716,13 @@ NCR_700_chip_setup(struct Scsi_Host *host)
                 * of spec: sync divider 2, async divider 3 */
                DEBUG(("53c700: sync 2 async 3\n"));
                NCR_700_writeb(SYNC_DIV_2_0, host, SBCL_REG);
-               NCR_700_writeb(ASYNC_DIV_3_0 | dcntl_extra, host, DCNTL_REG);
+               NCR_700_writeb(ASYNC_DIV_3_0 | hostdata->dcntl_extra, host, DCNTL_REG);
                hostdata->sync_clock = hostdata->clock/2;
        } else  if(hostdata->clock > 50  && hostdata->clock <= 75) {
                /* sync divider 1.5, async divider 3 */
                DEBUG(("53c700: sync 1.5 async 3\n"));
                NCR_700_writeb(SYNC_DIV_1_5, host, SBCL_REG);
-               NCR_700_writeb(ASYNC_DIV_3_0 | dcntl_extra, host, DCNTL_REG);
+               NCR_700_writeb(ASYNC_DIV_3_0 | hostdata->dcntl_extra, host, DCNTL_REG);
                hostdata->sync_clock = hostdata->clock*2;
                hostdata->sync_clock /= 3;
                
@@ -738,18 +730,18 @@ NCR_700_chip_setup(struct Scsi_Host *host)
                /* sync divider 1, async divider 2 */
                DEBUG(("53c700: sync 1 async 2\n"));
                NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
-               NCR_700_writeb(ASYNC_DIV_2_0 | dcntl_extra, host, DCNTL_REG);
+               NCR_700_writeb(ASYNC_DIV_2_0 | hostdata->dcntl_extra, host, DCNTL_REG);
                hostdata->sync_clock = hostdata->clock;
        } else if(hostdata->clock > 25 && hostdata->clock <=37) {
                /* sync divider 1, async divider 1.5 */
                DEBUG(("53c700: sync 1 async 1.5\n"));
                NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
-               NCR_700_writeb(ASYNC_DIV_1_5 | dcntl_extra, host, DCNTL_REG);
+               NCR_700_writeb(ASYNC_DIV_1_5 | hostdata->dcntl_extra, host, DCNTL_REG);
                hostdata->sync_clock = hostdata->clock;
        } else {
                DEBUG(("53c700: sync 1 async 1\n"));
                NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
-               NCR_700_writeb(ASYNC_DIV_1_0 | dcntl_extra, host, DCNTL_REG);
+               NCR_700_writeb(ASYNC_DIV_1_0 | hostdata->dcntl_extra, host, DCNTL_REG);
                /* sync divider 1, async divider 1 */
                hostdata->sync_clock = hostdata->clock;
        }
@@ -835,8 +827,9 @@ process_extended_message(struct Scsi_Host *host,
                        shost_printk(KERN_WARNING, host,
                                "Unexpected SDTR msg\n");
                        hostdata->msgout[0] = A_REJECT_MSG;
-                       dma_cache_sync(hostdata->msgout, 1, DMA_TO_DEVICE);
-                       script_patch_16(hostdata->script, MessageCount, 1);
+                       dma_cache_sync(hostdata->dev, hostdata->msgout, 1, DMA_TO_DEVICE);
+                       script_patch_16(hostdata->dev, hostdata->script,
+                                       MessageCount, 1);
                        /* SendMsgOut returns, so set up the return
                         * address */
                        resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
@@ -847,8 +840,9 @@ process_extended_message(struct Scsi_Host *host,
                printk(KERN_INFO "scsi%d: (%d:%d), Unsolicited WDTR after CMD, Rejecting\n",
                       host->host_no, pun, lun);
                hostdata->msgout[0] = A_REJECT_MSG;
-               dma_cache_sync(hostdata->msgout, 1, DMA_TO_DEVICE);
-               script_patch_16(hostdata->script, MessageCount, 1);
+               dma_cache_sync(hostdata->dev, hostdata->msgout, 1, DMA_TO_DEVICE);
+               script_patch_16(hostdata->dev, hostdata->script, MessageCount,
+                               1);
                resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
 
                break;
@@ -857,12 +851,13 @@ process_extended_message(struct Scsi_Host *host,
                printk(KERN_INFO "scsi%d (%d:%d): Unexpected message %s: ",
                       host->host_no, pun, lun,
                       NCR_700_phase[(dsps & 0xf00) >> 8]);
-               scsi_print_msg(hostdata->msgin);
+               spi_print_msg(hostdata->msgin);
                printk("\n");
                /* just reject it */
                hostdata->msgout[0] = A_REJECT_MSG;
-               dma_cache_sync(hostdata->msgout, 1, DMA_TO_DEVICE);
-               script_patch_16(hostdata->script, MessageCount, 1);
+               dma_cache_sync(hostdata->dev, hostdata->msgout, 1, DMA_TO_DEVICE);
+               script_patch_16(hostdata->dev, hostdata->script, MessageCount,
+                               1);
                /* SendMsgOut returns, so set up the return
                 * address */
                resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
@@ -887,7 +882,7 @@ process_message(struct Scsi_Host *host,     struct NCR_700_Host_Parameters *hostdata
 #ifdef NCR_700_DEBUG
        printk("scsi%d (%d:%d): message %s: ", host->host_no, pun, lun,
               NCR_700_phase[(dsps & 0xf00) >> 8]);
-       scsi_print_msg(hostdata->msgin);
+       spi_print_msg(hostdata->msgin);
        printk("\n");
 #endif
 
@@ -939,12 +934,13 @@ process_message(struct Scsi_Host *host,   struct NCR_700_Host_Parameters *hostdata
                       host->host_no, pun, lun,
                       NCR_700_phase[(dsps & 0xf00) >> 8]);
 
-               scsi_print_msg(hostdata->msgin);
+               spi_print_msg(hostdata->msgin);
                printk("\n");
                /* just reject it */
                hostdata->msgout[0] = A_REJECT_MSG;
-               dma_cache_sync(hostdata->msgout, 1, DMA_TO_DEVICE);
-               script_patch_16(hostdata->script, MessageCount, 1);
+               dma_cache_sync(hostdata->dev, hostdata->msgout, 1, DMA_TO_DEVICE);
+               script_patch_16(hostdata->dev, hostdata->script, MessageCount,
+                               1);
                /* SendMsgOut returns, so set up the return
                 * address */
                resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
@@ -953,7 +949,7 @@ process_message(struct Scsi_Host *host,     struct NCR_700_Host_Parameters *hostdata
        }
        NCR_700_writel(temp, host, TEMP_REG);
        /* set us up to receive another message */
-       dma_cache_sync(hostdata->msgin, MSG_ARRAY_SIZE, DMA_FROM_DEVICE);
+       dma_cache_sync(hostdata->dev, hostdata->msgin, MSG_ARRAY_SIZE, DMA_FROM_DEVICE);
        return resume_offset;
 }
 
@@ -983,13 +979,15 @@ process_script_interrupt(__u32 dsps, __u32 dsp, struct scsi_cmnd *SCp,
                   status_byte(hostdata->status[0]) == COMMAND_TERMINATED) {
                        struct NCR_700_command_slot *slot =
                                (struct NCR_700_command_slot *)SCp->host_scribble;
-                       if(SCp->cmnd[0] == REQUEST_SENSE) {
+                       if(slot->flags == NCR_700_FLAG_AUTOSENSE) {
                                /* OOPS: bad device, returning another
                                 * contingent allegiance condition */
                                scmd_printk(KERN_ERR, SCp,
                                        "broken device is looping in contingent allegiance: ignoring\n");
                                NCR_700_scsi_done(hostdata, SCp, hostdata->status[0]);
                        } else {
+                               char *cmnd =
+                                       NCR_700_get_sense_cmnd(SCp->device);
 #ifdef NCR_DEBUG
                                scsi_print_command(SCp);
                                printk("  cmd %p has status %d, requesting sense\n",
@@ -1003,38 +1001,40 @@ process_script_interrupt(__u32 dsps, __u32 dsp, struct scsi_cmnd *SCp,
                                 * data associated with the command
                                 * here */
                                NCR_700_unmap(hostdata, SCp, slot);
-
-                               SCp->cmnd[0] = REQUEST_SENSE;
-                               SCp->cmnd[1] = (SCp->device->lun & 0x7) << 5;
-                               SCp->cmnd[2] = 0;
-                               SCp->cmnd[3] = 0;
-                               SCp->cmnd[4] = sizeof(SCp->sense_buffer);
-                               SCp->cmnd[5] = 0;
-                               SCp->cmd_len = 6;
+                               dma_unmap_single(hostdata->dev, slot->pCmd,
+                                                MAX_COMMAND_SIZE,
+                                                DMA_TO_DEVICE);
+
+                               cmnd[0] = REQUEST_SENSE;
+                               cmnd[1] = (SCp->device->lun & 0x7) << 5;
+                               cmnd[2] = 0;
+                               cmnd[3] = 0;
+                               cmnd[4] = SCSI_SENSE_BUFFERSIZE;
+                               cmnd[5] = 0;
                                /* Here's a quiet hack: the
                                 * REQUEST_SENSE command is six bytes,
                                 * so store a flag indicating that
                                 * this was an internal sense request
                                 * and the original status at the end
                                 * of the command */
-                               SCp->cmnd[6] = NCR_700_INTERNAL_SENSE_MAGIC;
-                               SCp->cmnd[7] = hostdata->status[0];
-                               SCp->use_sg = 0;
-                               SCp->sc_data_direction = DMA_FROM_DEVICE;
-                               dma_sync_single_for_device(hostdata->dev, slot->pCmd,
-                                                          SCp->cmd_len, DMA_TO_DEVICE);
-                               SCp->request_bufflen = sizeof(SCp->sense_buffer);
-                               slot->dma_handle = dma_map_single(hostdata->dev, SCp->sense_buffer, sizeof(SCp->sense_buffer), DMA_FROM_DEVICE);
-                               slot->SG[0].ins = bS_to_host(SCRIPT_MOVE_DATA_IN | sizeof(SCp->sense_buffer));
+                               cmnd[6] = NCR_700_INTERNAL_SENSE_MAGIC;
+                               cmnd[7] = hostdata->status[0];
+                               cmnd[8] = SCp->cmd_len;
+                               SCp->cmd_len = 6; /* command length for
+                                                  * REQUEST_SENSE */
+                               slot->pCmd = dma_map_single(hostdata->dev, cmnd, MAX_COMMAND_SIZE, DMA_TO_DEVICE);
+                               slot->dma_handle = dma_map_single(hostdata->dev, SCp->sense_buffer, SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
+                               slot->SG[0].ins = bS_to_host(SCRIPT_MOVE_DATA_IN | SCSI_SENSE_BUFFERSIZE);
                                slot->SG[0].pAddr = bS_to_host(slot->dma_handle);
                                slot->SG[1].ins = bS_to_host(SCRIPT_RETURN);
                                slot->SG[1].pAddr = 0;
                                slot->resume_offset = hostdata->pScript;
-                               dma_cache_sync(slot->SG, sizeof(slot->SG[0])*2, DMA_TO_DEVICE);
-                               dma_cache_sync(SCp->sense_buffer, sizeof(SCp->sense_buffer), DMA_FROM_DEVICE);
-                               
+                               dma_cache_sync(hostdata->dev, slot->SG, sizeof(slot->SG[0])*2, DMA_TO_DEVICE);
+                               dma_cache_sync(hostdata->dev, SCp->sense_buffer, SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
+
                                /* queue the command for reissue */
                                slot->state = NCR_700_SLOT_QUEUED;
+                               slot->flags = NCR_700_FLAG_AUTOSENSE;
                                hostdata->state = NCR_700_HOST_FREE;
                                hostdata->cmd = NULL;
                        }
@@ -1146,11 +1146,12 @@ process_script_interrupt(__u32 dsps, __u32 dsp, struct scsi_cmnd *SCp,
                        hostdata->cmd = slot->cmnd;
 
                        /* re-patch for this command */
-                       script_patch_32_abs(hostdata->script, CommandAddress, 
-                                           slot->pCmd);
-                       script_patch_16(hostdata->script,
+                       script_patch_32_abs(hostdata->dev, hostdata->script,
+                                           CommandAddress, slot->pCmd);
+                       script_patch_16(hostdata->dev, hostdata->script,
                                        CommandCount, slot->cmnd->cmd_len);
-                       script_patch_32_abs(hostdata->script, SGScriptStartAddress,
+                       script_patch_32_abs(hostdata->dev, hostdata->script,
+                                           SGScriptStartAddress,
                                            to32bit(&slot->pSG[0].ins));
 
                        /* Note: setting SXFER only works if we're
@@ -1160,13 +1161,13 @@ process_script_interrupt(__u32 dsps, __u32 dsp, struct scsi_cmnd *SCp,
                         * should therefore always clear ACK */
                        NCR_700_writeb(NCR_700_get_SXFER(hostdata->cmd->device),
                                       host, SXFER_REG);
-                       dma_cache_sync(hostdata->msgin,
+                       dma_cache_sync(hostdata->dev, hostdata->msgin,
                                       MSG_ARRAY_SIZE, DMA_FROM_DEVICE);
-                       dma_cache_sync(hostdata->msgout,
+                       dma_cache_sync(hostdata->dev, hostdata->msgout,
                                       MSG_ARRAY_SIZE, DMA_TO_DEVICE);
                        /* I'm just being paranoid here, the command should
                         * already have been flushed from the cache */
-                       dma_cache_sync(slot->cmnd->cmnd,
+                       dma_cache_sync(hostdata->dev, slot->cmnd->cmnd,
                                       slot->cmnd->cmd_len, DMA_TO_DEVICE);
 
 
@@ -1230,7 +1231,7 @@ process_script_interrupt(__u32 dsps, __u32 dsp, struct scsi_cmnd *SCp,
                hostdata->reselection_id = reselection_id;
                /* just in case we have a stale simple tag message, clear it */
                hostdata->msgin[1] = 0;
-               dma_cache_sync(hostdata->msgin,
+               dma_cache_sync(hostdata->dev, hostdata->msgin,
                               MSG_ARRAY_SIZE, DMA_BIDIRECTIONAL);
                if(hostdata->tag_negotiated & (1<<reselection_id)) {
                        resume_offset = hostdata->pScript + Ent_GetReselectionWithTag;
@@ -1251,14 +1252,13 @@ process_script_interrupt(__u32 dsps, __u32 dsp, struct scsi_cmnd *SCp,
                       host->host_no, pun, lun, NCR_700_condition[i],
                       NCR_700_phase[j], dsp - hostdata->pScript);
                if(SCp != NULL) {
-                       scsi_print_command(SCp);
+                       struct scatterlist *sg;
 
-                       if(SCp->use_sg) {
-                               for(i = 0; i < SCp->use_sg + 1; i++) {
-                                       printk(KERN_INFO " SG[%d].length = %d, move_insn=%08x, addr %08x\n", i, ((struct scatterlist *)SCp->buffer)[i].length, ((struct NCR_700_command_slot *)SCp->host_scribble)->SG[i].ins, ((struct NCR_700_command_slot *)SCp->host_scribble)->SG[i].pAddr);
-                               }
+                       scsi_print_command(SCp);
+                       scsi_for_each_sg(SCp, sg, scsi_sg_count(SCp) + 1, i) {
+                               printk(KERN_INFO " SG[%d].length = %d, move_insn=%08x, addr %08x\n", i, sg->length, ((struct NCR_700_command_slot *)SCp->host_scribble)->SG[i].ins, ((struct NCR_700_command_slot *)SCp->host_scribble)->SG[i].pAddr);
                        }
-               }              
+               }
                NCR_700_internal_bus_reset(host);
        } else if((dsps & 0xfffff000) == A_DEBUG_INTERRUPT) {
                printk(KERN_NOTICE "scsi%d (%d:%d) DEBUG INTERRUPT %d AT %08x[%04x], continuing\n",
@@ -1346,7 +1346,7 @@ process_selection(struct Scsi_Host *host, __u32 dsp)
        hostdata->cmd = NULL;
        /* clear any stale simple tag message */
        hostdata->msgin[1] = 0;
-       dma_cache_sync(hostdata->msgin, MSG_ARRAY_SIZE,
+       dma_cache_sync(hostdata->dev, hostdata->msgin, MSG_ARRAY_SIZE,
                       DMA_BIDIRECTIONAL);
 
        if(id == 0xff) {
@@ -1414,12 +1414,14 @@ NCR_700_start_command(struct scsi_cmnd *SCp)
        /* keep interrupts disabled until we have the command correctly
         * set up so we cannot take a selection interrupt */
 
-       hostdata->msgout[0] = NCR_700_identify(SCp->cmnd[0] != REQUEST_SENSE,
+       hostdata->msgout[0] = NCR_700_identify((SCp->cmnd[0] != REQUEST_SENSE &&
+                                               slot->flags != NCR_700_FLAG_AUTOSENSE),
                                               SCp->device->lun);
        /* for INQUIRY or REQUEST_SENSE commands, we cannot be sure
         * if the negotiated transfer parameters still hold, so
         * always renegotiate them */
-       if(SCp->cmnd[0] == INQUIRY || SCp->cmnd[0] == REQUEST_SENSE) {
+       if(SCp->cmnd[0] == INQUIRY || SCp->cmnd[0] == REQUEST_SENSE ||
+          slot->flags == NCR_700_FLAG_AUTOSENSE) {
                NCR_700_clear_flag(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC);
        }
 
@@ -1428,43 +1430,43 @@ NCR_700_start_command(struct scsi_cmnd *SCp)
         * will refuse all tags, so send the request sense as untagged
         * */
        if((hostdata->tag_negotiated & (1<<scmd_id(SCp)))
-          && (slot->tag != SCSI_NO_TAG && SCp->cmnd[0] != REQUEST_SENSE)) {
+          && (slot->tag != SCSI_NO_TAG && SCp->cmnd[0] != REQUEST_SENSE &&
+              slot->flags != NCR_700_FLAG_AUTOSENSE)) {
                count += scsi_populate_tag_msg(SCp, &hostdata->msgout[count]);
        }
 
        if(hostdata->fast &&
           NCR_700_is_flag_clear(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC)) {
-               memcpy(&hostdata->msgout[count], NCR_700_SDTR_msg,
-                      sizeof(NCR_700_SDTR_msg));
-               hostdata->msgout[count+3] = spi_period(SCp->device->sdev_target);
-               hostdata->msgout[count+4] = spi_offset(SCp->device->sdev_target);
-               count += sizeof(NCR_700_SDTR_msg);
+               count += spi_populate_sync_msg(&hostdata->msgout[count],
+                               spi_period(SCp->device->sdev_target),
+                               spi_offset(SCp->device->sdev_target));
                NCR_700_set_flag(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
        }
 
-       script_patch_16(hostdata->script, MessageCount, count);
+       script_patch_16(hostdata->dev, hostdata->script, MessageCount, count);
 
 
-       script_patch_ID(hostdata->script,
+       script_patch_ID(hostdata->dev, hostdata->script,
                        Device_ID, 1<<scmd_id(SCp));
 
-       script_patch_32_abs(hostdata->script, CommandAddress, 
+       script_patch_32_abs(hostdata->dev, hostdata->script, CommandAddress,
                            slot->pCmd);
-       script_patch_16(hostdata->script, CommandCount, SCp->cmd_len);
+       script_patch_16(hostdata->dev, hostdata->script, CommandCount,
+                       SCp->cmd_len);
        /* finally plumb the beginning of the SG list into the script
         * */
-       script_patch_32_abs(hostdata->script, SGScriptStartAddress,
-                           to32bit(&slot->pSG[0].ins));
+       script_patch_32_abs(hostdata->dev, hostdata->script,
+                           SGScriptStartAddress, to32bit(&slot->pSG[0].ins));
        NCR_700_clear_fifo(SCp->device->host);
 
        if(slot->resume_offset == 0)
                slot->resume_offset = hostdata->pScript;
        /* now perform all the writebacks and invalidates */
-       dma_cache_sync(hostdata->msgout, count, DMA_TO_DEVICE);
-       dma_cache_sync(hostdata->msgin, MSG_ARRAY_SIZE,
+       dma_cache_sync(hostdata->dev, hostdata->msgout, count, DMA_TO_DEVICE);
+       dma_cache_sync(hostdata->dev, hostdata->msgin, MSG_ARRAY_SIZE,
                       DMA_FROM_DEVICE);
-       dma_cache_sync(SCp->cmnd, SCp->cmd_len, DMA_TO_DEVICE);
-       dma_cache_sync(hostdata->status, 1, DMA_FROM_DEVICE);
+       dma_cache_sync(hostdata->dev, SCp->cmnd, SCp->cmd_len, DMA_TO_DEVICE);
+       dma_cache_sync(hostdata->dev, hostdata->status, 1, DMA_FROM_DEVICE);
 
        /* set the synchronous period/offset */
        NCR_700_writeb(NCR_700_get_SXFER(SCp->device),
@@ -1476,7 +1478,7 @@ NCR_700_start_command(struct scsi_cmnd *SCp)
 }
 
 irqreturn_t
-NCR_700_intr(int irq, void *dev_id, struct pt_regs *regs)
+NCR_700_intr(int irq, void *dev_id)
 {
        struct Scsi_Host *host = (struct Scsi_Host *)dev_id;
        struct NCR_700_Host_Parameters *hostdata =
@@ -1487,7 +1489,7 @@ NCR_700_intr(int irq, void *dev_id, struct pt_regs *regs)
        unsigned long flags;
        int handled = 0;
 
-       /* Use the host lock to serialise acess to the 53c700
+       /* Use the host lock to serialise access to the 53c700
         * hardware.  Note: In future, we may need to take the queue
         * lock to enter the done routines.  When that happens, we
         * need to ensure that for this driver, the host lock and the
@@ -1543,7 +1545,7 @@ NCR_700_intr(int irq, void *dev_id, struct pt_regs *regs)
 
                        /* clear all the negotiated parameters */
                        __shost_for_each_device(SDp, host)
-                               SDp->hostdata = NULL;
+                               NCR_700_clear_flag(SDp, ~0);
                        
                        /* clear all the slots and their pending commands */
                        for(i = 0; i < NCR_700_COMMAND_SLOTS_PER_HOST; i++) {
@@ -1640,7 +1642,7 @@ NCR_700_intr(int irq, void *dev_id, struct pt_regs *regs)
                                        slot->SG[i].ins = bS_to_host(SCRIPT_NOP);
                                        slot->SG[i].pAddr = 0;
                                }
-                               dma_cache_sync(slot->SG, sizeof(slot->SG), DMA_TO_DEVICE);
+                               dma_cache_sync(hostdata->dev, slot->SG, sizeof(slot->SG), DMA_TO_DEVICE);
                                /* and pretend we disconnected after
                                 * the command phase */
                                resume_offset = hostdata->pScript + Ent_MsgInDuringData;
@@ -1747,8 +1749,8 @@ NCR_700_intr(int irq, void *dev_id, struct pt_regs *regs)
        return IRQ_RETVAL(handled);
 }
 
-STATIC int
-NCR_700_queuecommand(struct scsi_cmnd *SCp, void (*done)(struct scsi_cmnd *))
+static int
+NCR_700_queuecommand_lck(struct scsi_cmnd *SCp, void (*done)(struct scsi_cmnd *))
 {
        struct NCR_700_Host_Parameters *hostdata = 
                (struct NCR_700_Host_Parameters *)SCp->device->host->hostdata[0];
@@ -1830,8 +1832,8 @@ NCR_700_queuecommand(struct scsi_cmnd *SCp, void (*done)(struct scsi_cmnd *))
        }
        /* sanity check: some of the commands generated by the mid-layer
         * have an eccentric idea of their sc_data_direction */
-       if(!SCp->use_sg && !SCp->request_bufflen 
-          && SCp->sc_data_direction != DMA_NONE) {
+       if(!scsi_sg_count(SCp) && !scsi_bufflen(SCp) &&
+          SCp->sc_data_direction != DMA_NONE) {
 #ifdef NCR_700_DEBUG
                printk("53c700: Command");
                scsi_print_command(SCp);
@@ -1873,30 +1875,15 @@ NCR_700_queuecommand(struct scsi_cmnd *SCp, void (*done)(struct scsi_cmnd *))
                int i;
                int sg_count;
                dma_addr_t vPtr = 0;
+               struct scatterlist *sg;
                __u32 count = 0;
 
-               if(SCp->use_sg) {
-                       sg_count = dma_map_sg(hostdata->dev, SCp->buffer,
-                                             SCp->use_sg, direction);
-               } else {
-                       vPtr = dma_map_single(hostdata->dev,
-                                             SCp->request_buffer, 
-                                             SCp->request_bufflen,
-                                             direction);
-                       count = SCp->request_bufflen;
-                       slot->dma_handle = vPtr;
-                       sg_count = 1;
-               }
-                       
-
-               for(i = 0; i < sg_count; i++) {
-
-                       if(SCp->use_sg) {
-                               struct scatterlist *sg = SCp->buffer;
+               sg_count = scsi_dma_map(SCp);
+               BUG_ON(sg_count < 0);
 
-                               vPtr = sg_dma_address(&sg[i]);
-                               count = sg_dma_len(&sg[i]);
-                       }
+               scsi_for_each_sg(SCp, sg, sg_count, i) {
+                       vPtr = sg_dma_address(sg);
+                       count = sg_dma_len(sg);
 
                        slot->SG[i].ins = bS_to_host(move_ins | count);
                        DEBUG((" scatter block %d: move %d[%08x] from 0x%lx\n",
@@ -1905,18 +1892,20 @@ NCR_700_queuecommand(struct scsi_cmnd *SCp, void (*done)(struct scsi_cmnd *))
                }
                slot->SG[i].ins = bS_to_host(SCRIPT_RETURN);
                slot->SG[i].pAddr = 0;
-               dma_cache_sync(slot->SG, sizeof(slot->SG), DMA_TO_DEVICE);
+               dma_cache_sync(hostdata->dev, slot->SG, sizeof(slot->SG), DMA_TO_DEVICE);
                DEBUG((" SETTING %08lx to %x\n",
-                      (&slot->pSG[i].ins), 
+                      (&slot->pSG[i].ins),
                       slot->SG[i].ins));
        }
        slot->resume_offset = 0;
        slot->pCmd = dma_map_single(hostdata->dev, SCp->cmnd,
-                                   sizeof(SCp->cmnd), DMA_TO_DEVICE);
+                                   MAX_COMMAND_SIZE, DMA_TO_DEVICE);
        NCR_700_start_command(SCp);
        return 0;
 }
 
+STATIC DEF_SCSI_QCMD(NCR_700_queuecommand)
+
 STATIC int
 NCR_700_abort(struct scsi_cmnd * SCp)
 {
@@ -1952,7 +1941,7 @@ NCR_700_abort(struct scsi_cmnd * SCp)
 STATIC int
 NCR_700_bus_reset(struct scsi_cmnd * SCp)
 {
-       DECLARE_COMPLETION(complete);
+       DECLARE_COMPLETION_ONSTACK(complete);
        struct NCR_700_Host_Parameters *hostdata = 
                (struct NCR_700_Host_Parameters *)SCp->device->host->hostdata[0];
 
@@ -2047,7 +2036,17 @@ NCR_700_set_offset(struct scsi_target *STp, int offset)
        spi_flags(STp) |= NCR_700_DEV_PRINT_SYNC_NEGOTIATION;
 }
 
+STATIC int
+NCR_700_slave_alloc(struct scsi_device *SDp)
+{
+       SDp->hostdata = kzalloc(sizeof(struct NCR_700_Device_Parameters),
+                               GFP_KERNEL);
+
+       if (!SDp->hostdata)
+               return -ENOMEM;
 
+       return 0;
+}
 
 STATIC int
 NCR_700_slave_configure(struct scsi_device *SDp)
@@ -2078,12 +2077,16 @@ NCR_700_slave_configure(struct scsi_device *SDp)
 STATIC void
 NCR_700_slave_destroy(struct scsi_device *SDp)
 {
-       /* to do here: deallocate memory */
+       kfree(SDp->hostdata);
+       SDp->hostdata = NULL;
 }
 
 static int
-NCR_700_change_queue_depth(struct scsi_device *SDp, int depth)
+NCR_700_change_queue_depth(struct scsi_device *SDp, int depth, int reason)
 {
+       if (reason != SCSI_QDEPTH_DEFAULT)
+               return -EOPNOTSUPP;
+
        if (depth > NCR_700_MAX_TAGS)
                depth = NCR_700_MAX_TAGS;