]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blobdiff - drivers/scsi/aacraid/aachba.c
[SCSI] aacraid: add user initiated reset
[mirror_ubuntu-jammy-kernel.git] / drivers / scsi / aacraid / aachba.c
index ddb33b06e0efffcbaf27b43b13c5102c34b1e7f2..b3081b10d0a382ca4103e1de132a6a040e6cafbe 100644 (file)
@@ -5,7 +5,7 @@
  * based on the old aacraid driver that is..
  * Adaptec aacraid device driver for Linux.
  *
- * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
+ * Copyright (c) 2000-2007 Adaptec, Inc. (aacraid@adaptec.com)
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -26,7 +26,6 @@
 #include <linux/kernel.h>
 #include <linux/init.h>
 #include <linux/types.h>
-#include <linux/sched.h>
 #include <linux/pci.h>
 #include <linux/spinlock.h>
 #include <linux/slab.h>
@@ -147,7 +146,7 @@ static char *aac_get_status_string(u32 status);
 static int nondasd = -1;
 static int dacmode = -1;
 
-static int commit = -1;
+int aac_commit = -1;
 int startup_timeout = 180;
 int aif_timeout = 120;
 
@@ -155,7 +154,7 @@ module_param(nondasd, int, S_IRUGO|S_IWUSR);
 MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices. 0=off, 1=on");
 module_param(dacmode, int, S_IRUGO|S_IWUSR);
 MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC. 0=off, 1=on");
-module_param(commit, int, S_IRUGO|S_IWUSR);
+module_param_named(commit, aac_commit, int, S_IRUGO|S_IWUSR);
 MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the adapter for foreign arrays.\nThis is typically needed in systems that do not have a BIOS. 0=off, 1=on");
 module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
 MODULE_PARM_DESC(startup_timeout, "The duration of time in seconds to wait for adapter to have it's kernel up and\nrunning. This is typically adjusted for large systems that do not have a BIOS.");
@@ -170,9 +169,48 @@ int acbsize = -1;
 module_param(acbsize, int, S_IRUGO|S_IWUSR);
 MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB) size. Valid values are 512, 2048, 4096 and 8192. Default is to use suggestion from Firmware.");
 
+int update_interval = 30 * 60;
+module_param(update_interval, int, S_IRUGO|S_IWUSR);
+MODULE_PARM_DESC(update_interval, "Interval in seconds between time sync updates issued to adapter.");
+
+int check_interval = 24 * 60 * 60;
+module_param(check_interval, int, S_IRUGO|S_IWUSR);
+MODULE_PARM_DESC(check_interval, "Interval in seconds between adapter health checks.");
+
+int check_reset = 1;
+module_param(check_reset, int, S_IRUGO|S_IWUSR);
+MODULE_PARM_DESC(check_reset, "If adapter fails health check, reset the adapter.");
+
 int expose_physicals = -1;
 module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
 MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays. -1=protect 0=off, 1=on");
+
+int aac_reset_devices = 0;
+module_param_named(reset_devices, aac_reset_devices, int, S_IRUGO|S_IWUSR);
+MODULE_PARM_DESC(reset_devices, "Force an adapter reset at initialization.");
+
+static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
+               struct fib *fibptr) {
+       struct scsi_device *device;
+
+       if (unlikely(!scsicmd || !scsicmd->scsi_done )) {
+               dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"))
+;
+                aac_fib_complete(fibptr);
+                aac_fib_free(fibptr);
+                return 0;
+        }
+       scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
+       device = scsicmd->device;
+       if (unlikely(!device || !scsi_device_online(device))) {
+               dprintk((KERN_WARNING "aac_valid_context: scsi device corrupt\n"));
+               aac_fib_complete(fibptr);
+               aac_fib_free(fibptr);
+               return 0;
+       }
+       return 1;
+}
+
 /**
  *     aac_get_config_status   -       check the adapter configuration
  *     @common: adapter to query
@@ -223,7 +261,7 @@ int aac_get_config_status(struct aac_dev *dev, int commit_flag)
        aac_fib_complete(fibptr);
        /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
        if (status >= 0) {
-               if ((commit == 1) || commit_flag) {
+               if ((aac_commit == 1) || commit_flag) {
                        struct aac_commit_config * dinfo;
                        aac_fib_init(fibptr);
                        dinfo = (struct aac_commit_config *) fib_data(fibptr);
@@ -238,7 +276,7 @@ int aac_get_config_status(struct aac_dev *dev, int commit_flag)
                                    1, 1,
                                    NULL, NULL);
                        aac_fib_complete(fibptr);
-               } else if (commit == 0) {
+               } else if (aac_commit == 0) {
                        printk(KERN_WARNING
                          "aac_get_config_status: Foreign device configurations are being ignored\n");
                }
@@ -259,13 +297,10 @@ int aac_get_containers(struct aac_dev *dev)
        u32 index; 
        int status = 0;
        struct fib * fibptr;
-       unsigned instance;
        struct aac_get_container_count *dinfo;
        struct aac_get_container_count_resp *dresp;
        int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
 
-       instance = dev->scsi_host_ptr->unique_id;
-
        if (!(fibptr = aac_fib_alloc(dev)))
                return -ENOMEM;
 
@@ -285,109 +320,51 @@ int aac_get_containers(struct aac_dev *dev)
                maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
                aac_fib_complete(fibptr);
        }
+       aac_fib_free(fibptr);
 
        if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
                maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
-       fsa_dev_ptr = kmalloc(
-         sizeof(*fsa_dev_ptr) * maximum_num_containers, GFP_KERNEL);
-       if (!fsa_dev_ptr) {
-               aac_fib_free(fibptr);
+       fsa_dev_ptr = kzalloc(sizeof(*fsa_dev_ptr) * maximum_num_containers,
+                       GFP_KERNEL);
+       if (!fsa_dev_ptr)
                return -ENOMEM;
-       }
-       memset(fsa_dev_ptr, 0, sizeof(*fsa_dev_ptr) * maximum_num_containers);
 
        dev->fsa_dev = fsa_dev_ptr;
        dev->maximum_num_containers = maximum_num_containers;
 
-       for (index = 0; index < dev->maximum_num_containers; index++) {
-               struct aac_query_mount *dinfo;
-               struct aac_mount *dresp;
-
+       for (index = 0; index < dev->maximum_num_containers; ) {
                fsa_dev_ptr[index].devname[0] = '\0';
 
-               aac_fib_init(fibptr);
-               dinfo = (struct aac_query_mount *) fib_data(fibptr);
-
-               dinfo->command = cpu_to_le32(VM_NameServe);
-               dinfo->count = cpu_to_le32(index);
-               dinfo->type = cpu_to_le32(FT_FILESYS);
+               status = aac_probe_container(dev, index);
 
-               status = aac_fib_send(ContainerCommand,
-                                   fibptr,
-                                   sizeof (struct aac_query_mount),
-                                   FsaNormal,
-                                   1, 1,
-                                   NULL, NULL);
-               if (status < 0 ) {
+               if (status < 0) {
                        printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
                        break;
                }
-               dresp = (struct aac_mount *)fib_data(fibptr);
 
-               if ((le32_to_cpu(dresp->status) == ST_OK) &&
-                   (le32_to_cpu(dresp->mnt[0].vol) == CT_NONE)) {
-                       dinfo->command = cpu_to_le32(VM_NameServe64);
-                       dinfo->count = cpu_to_le32(index);
-                       dinfo->type = cpu_to_le32(FT_FILESYS);
-
-                       if (aac_fib_send(ContainerCommand,
-                                   fibptr,
-                                   sizeof(struct aac_query_mount),
-                                   FsaNormal,
-                                   1, 1,
-                                   NULL, NULL) < 0)
-                               continue;
-               } else
-                       dresp->mnt[0].capacityhigh = 0;
-
-               dprintk ((KERN_DEBUG
-                 "VM_NameServe cid=%d status=%d vol=%d state=%d cap=%llu\n",
-                 (int)index, (int)le32_to_cpu(dresp->status),
-                 (int)le32_to_cpu(dresp->mnt[0].vol),
-                 (int)le32_to_cpu(dresp->mnt[0].state),
-                 ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
-                   (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32)));
-               if ((le32_to_cpu(dresp->status) == ST_OK) &&
-                   (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
-                   (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
-                       fsa_dev_ptr[index].valid = 1;
-                       fsa_dev_ptr[index].type = le32_to_cpu(dresp->mnt[0].vol);
-                       fsa_dev_ptr[index].size
-                         = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
-                           (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
-                       if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
-                                   fsa_dev_ptr[index].ro = 1;
-               }
-               aac_fib_complete(fibptr);
                /*
                 *      If there are no more containers, then stop asking.
                 */
-               if ((index + 1) >= le32_to_cpu(dresp->count)){
+               if (++index >= status)
                        break;
-               }
        }
-       aac_fib_free(fibptr);
        return status;
 }
 
 static void aac_internal_transfer(struct scsi_cmnd *scsicmd, void *data, unsigned int offset, unsigned int len)
 {
        void *buf;
-       unsigned int transfer_len;
-       struct scatterlist *sg = scsicmd->request_buffer;
+       int transfer_len;
+       struct scatterlist *sg = scsi_sglist(scsicmd);
 
-       if (scsicmd->use_sg) {
-               buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
-               transfer_len = min(sg->length, len + offset);
-       } else {
-               buf = scsicmd->request_buffer;
-               transfer_len = min(scsicmd->request_bufflen, len + offset);
-       }
+       buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
+       transfer_len = min(sg->length, len + offset);
 
-       memcpy(buf + offset, data, transfer_len - offset);
+       transfer_len -= offset;
+       if (buf && transfer_len > 0)
+               memcpy(buf + offset, data, transfer_len);
 
-       if (scsicmd->use_sg) 
-               kunmap_atomic(buf - sg->offset, KM_IRQ0);
+       kunmap_atomic(buf - sg->offset, KM_IRQ0);
 
 }
 
@@ -397,7 +374,9 @@ static void get_container_name_callback(void *context, struct fib * fibptr)
        struct scsi_cmnd * scsicmd;
 
        scsicmd = (struct scsi_cmnd *) context;
-       scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
+
+       if (!aac_valid_context(scsicmd, fibptr))
+               return;
 
        dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
        BUG_ON(fibptr == NULL);
@@ -432,7 +411,7 @@ static void get_container_name_callback(void *context, struct fib * fibptr)
 /**
  *     aac_get_container_name  -       get container name, none blocking.
  */
-static int aac_get_container_name(struct scsi_cmnd * scsicmd, int cid)
+static int aac_get_container_name(struct scsi_cmnd * scsicmd)
 {
        int status;
        struct aac_get_name *dinfo;
@@ -449,7 +428,7 @@ static int aac_get_container_name(struct scsi_cmnd * scsicmd, int cid)
 
        dinfo->command = cpu_to_le32(VM_ContainerConfig);
        dinfo->type = cpu_to_le32(CT_READ_NAME);
-       dinfo->cid = cpu_to_le32(cid);
+       dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
        dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
 
        status = aac_fib_send(ContainerCommand,
@@ -474,85 +453,191 @@ static int aac_get_container_name(struct scsi_cmnd * scsicmd, int cid)
        return -1;
 }
 
-/**
- *     aac_probe_container             -       query a logical volume
- *     @dev: device to query
- *     @cid: container identifier
- *
- *     Queries the controller about the given volume. The volume information
- *     is updated in the struct fsa_dev_info structure rather than returned.
- */
-int aac_probe_container(struct aac_dev *dev, int cid)
+static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
+{
+       struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
+
+       if ((fsa_dev_ptr[scmd_id(scsicmd)].valid & 1))
+               return aac_scsi_cmd(scsicmd);
+
+       scsicmd->result = DID_NO_CONNECT << 16;
+       scsicmd->scsi_done(scsicmd);
+       return 0;
+}
+
+static void _aac_probe_container2(void * context, struct fib * fibptr)
 {
        struct fsa_dev_info *fsa_dev_ptr;
-       int status;
+       int (*callback)(struct scsi_cmnd *);
+       struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
+
+
+       if (!aac_valid_context(scsicmd, fibptr))
+               return;
+
+       scsicmd->SCp.Status = 0;
+       fsa_dev_ptr = fibptr->dev->fsa_dev;
+       if (fsa_dev_ptr) {
+               struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
+               fsa_dev_ptr += scmd_id(scsicmd);
+
+               if ((le32_to_cpu(dresp->status) == ST_OK) &&
+                   (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
+                   (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
+                       fsa_dev_ptr->valid = 1;
+                       fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
+                       fsa_dev_ptr->size
+                         = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
+                           (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
+                       fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
+               }
+               if ((fsa_dev_ptr->valid & 1) == 0)
+                       fsa_dev_ptr->valid = 0;
+               scsicmd->SCp.Status = le32_to_cpu(dresp->count);
+       }
+       aac_fib_complete(fibptr);
+       aac_fib_free(fibptr);
+       callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
+       scsicmd->SCp.ptr = NULL;
+       (*callback)(scsicmd);
+       return;
+}
+
+static void _aac_probe_container1(void * context, struct fib * fibptr)
+{
+       struct scsi_cmnd * scsicmd;
+       struct aac_mount * dresp;
        struct aac_query_mount *dinfo;
-       struct aac_mount *dresp;
-       struct fib * fibptr;
-       unsigned instance;
+       int status;
 
-       fsa_dev_ptr = dev->fsa_dev;
-       if (!fsa_dev_ptr)
-               return -ENOMEM;
-       instance = dev->scsi_host_ptr->unique_id;
+       dresp = (struct aac_mount *) fib_data(fibptr);
+       dresp->mnt[0].capacityhigh = 0;
+       if ((le32_to_cpu(dresp->status) != ST_OK) ||
+           (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE)) {
+               _aac_probe_container2(context, fibptr);
+               return;
+       }
+       scsicmd = (struct scsi_cmnd *) context;
 
-       if (!(fibptr = aac_fib_alloc(dev)))
-               return -ENOMEM;
+       if (!aac_valid_context(scsicmd, fibptr))
+               return;
 
        aac_fib_init(fibptr);
 
        dinfo = (struct aac_query_mount *)fib_data(fibptr);
 
-       dinfo->command = cpu_to_le32(VM_NameServe);
-       dinfo->count = cpu_to_le32(cid);
+       dinfo->command = cpu_to_le32(VM_NameServe64);
+       dinfo->count = cpu_to_le32(scmd_id(scsicmd));
        dinfo->type = cpu_to_le32(FT_FILESYS);
 
        status = aac_fib_send(ContainerCommand,
-                           fibptr,
-                           sizeof(struct aac_query_mount),
-                           FsaNormal,
-                           1, 1,
-                           NULL, NULL);
-       if (status < 0) {
-               printk(KERN_WARNING "aacraid: aac_probe_container query failed.\n");
-               goto error;
+                         fibptr,
+                         sizeof(struct aac_query_mount),
+                         FsaNormal,
+                         0, 1,
+                         _aac_probe_container2,
+                         (void *) scsicmd);
+       /*
+        *      Check that the command queued to the controller
+        */
+       if (status == -EINPROGRESS)
+               scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
+       else if (status < 0) {
+               /* Inherit results from VM_NameServe, if any */
+               dresp->status = cpu_to_le32(ST_OK);
+               _aac_probe_container2(context, fibptr);
        }
+}
 
-       dresp = (struct aac_mount *) fib_data(fibptr);
+static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
+{
+       struct fib * fibptr;
+       int status = -ENOMEM;
 
-       if ((le32_to_cpu(dresp->status) == ST_OK) &&
-           (le32_to_cpu(dresp->mnt[0].vol) == CT_NONE)) {
-               dinfo->command = cpu_to_le32(VM_NameServe64);
-               dinfo->count = cpu_to_le32(cid);
+       if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
+               struct aac_query_mount *dinfo;
+
+               aac_fib_init(fibptr);
+
+               dinfo = (struct aac_query_mount *)fib_data(fibptr);
+
+               dinfo->command = cpu_to_le32(VM_NameServe);
+               dinfo->count = cpu_to_le32(scmd_id(scsicmd));
                dinfo->type = cpu_to_le32(FT_FILESYS);
+               scsicmd->SCp.ptr = (char *)callback;
 
-               if (aac_fib_send(ContainerCommand,
-                           fibptr,
-                           sizeof(struct aac_query_mount),
-                           FsaNormal,
-                           1, 1,
-                           NULL, NULL) < 0)
-                       goto error;
-       } else
-               dresp->mnt[0].capacityhigh = 0;
-
-       if ((le32_to_cpu(dresp->status) == ST_OK) &&
-           (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
-           (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
-               fsa_dev_ptr[cid].valid = 1;
-               fsa_dev_ptr[cid].type = le32_to_cpu(dresp->mnt[0].vol);
-               fsa_dev_ptr[cid].size
-                 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
-                   (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
-               if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
-                       fsa_dev_ptr[cid].ro = 1;
+               status = aac_fib_send(ContainerCommand,
+                         fibptr,
+                         sizeof(struct aac_query_mount),
+                         FsaNormal,
+                         0, 1,
+                         _aac_probe_container1,
+                         (void *) scsicmd);
+               /*
+                *      Check that the command queued to the controller
+                */
+               if (status == -EINPROGRESS) {
+                       scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
+                       return 0;
+               }
+               if (status < 0) {
+                       scsicmd->SCp.ptr = NULL;
+                       aac_fib_complete(fibptr);
+                       aac_fib_free(fibptr);
+               }
+       }
+       if (status < 0) {
+               struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
+               if (fsa_dev_ptr) {
+                       fsa_dev_ptr += scmd_id(scsicmd);
+                       if ((fsa_dev_ptr->valid & 1) == 0) {
+                               fsa_dev_ptr->valid = 0;
+                               return (*callback)(scsicmd);
+                       }
+               }
        }
+       return status;
+}
 
-error:
-       aac_fib_complete(fibptr);
-       aac_fib_free(fibptr);
+/**
+ *     aac_probe_container             -       query a logical volume
+ *     @dev: device to query
+ *     @cid: container identifier
+ *
+ *     Queries the controller about the given volume. The volume information
+ *     is updated in the struct fsa_dev_info structure rather than returned.
+ */
+static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
+{
+       scsicmd->device = NULL;
+       return 0;
+}
+
+int aac_probe_container(struct aac_dev *dev, int cid)
+{
+       struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
+       struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
+       int status;
 
+       if (!scsicmd || !scsidev) {
+               kfree(scsicmd);
+               kfree(scsidev);
+               return -ENOMEM;
+       }
+       scsicmd->list.next = NULL;
+       scsicmd->scsi_done = (void (*)(struct scsi_cmnd*))aac_probe_container_callback1;
+
+       scsicmd->device = scsidev;
+       scsidev->sdev_state = 0;
+       scsidev->id = cid;
+       scsidev->host = dev->scsi_host_ptr;
+
+       if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
+               while (scsicmd->device == scsidev)
+                       schedule();
+       kfree(scsidev);
+       status = scsicmd->SCp.Status;
+       kfree(scsicmd);
        return status;
 }
 
@@ -745,7 +830,7 @@ static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u3
        readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
        readcmd->count = cpu_to_le32(count<<9);
        readcmd->cid = cpu_to_le16(scmd_id(cmd));
-       readcmd->flags = cpu_to_le16(1);
+       readcmd->flags = cpu_to_le16(IO_TYPE_READ);
        readcmd->bpTotal = 0;
        readcmd->bpComplete = 0;
 
@@ -824,7 +909,7 @@ static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32
                          (void *) cmd);
 }
 
-static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
+static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
 {
        u16 fibsize;
        struct aac_raw_io *writecmd;
@@ -834,7 +919,9 @@ static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u
        writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
        writecmd->count = cpu_to_le32(count<<9);
        writecmd->cid = cpu_to_le16(scmd_id(cmd));
-       writecmd->flags = 0;
+       writecmd->flags = fua ?
+               cpu_to_le16(IO_TYPE_WRITE|IO_SUREWRITE) :
+               cpu_to_le16(IO_TYPE_WRITE);
        writecmd->bpTotal = 0;
        writecmd->bpComplete = 0;
 
@@ -853,7 +940,7 @@ static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u
                          (void *) cmd);
 }
 
-static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
+static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
 {
        u16 fibsize;
        struct aac_write64 *writecmd;
@@ -884,7 +971,7 @@ static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba,
                          (void *) cmd);
 }
 
-static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
+static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
 {
        u16 fibsize;
        struct aac_write *writecmd;
@@ -961,7 +1048,7 @@ static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
        struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
 
        aac_build_sg64(cmd, (struct sgmap64*) &srbcmd->sg);
-       srbcmd->count = cpu_to_le32(cmd->request_bufflen);
+       srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
 
        memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
        memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
@@ -989,7 +1076,7 @@ static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
        struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
 
        aac_build_sg(cmd, (struct sgmap*)&srbcmd->sg);
-       srbcmd->count = cpu_to_le32(cmd->request_bufflen);
+       srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
 
        memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
        memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
@@ -1116,6 +1203,18 @@ int aac_get_adapter_info(struct aac_dev* dev)
                        printk(KERN_INFO "%s%d: serial %x\n",
                                dev->name, dev->id,
                                le32_to_cpu(dev->adapter_info.serial[0]));
+               if (dev->supplement_adapter_info.VpdInfo.Tsid[0]) {
+                       printk(KERN_INFO "%s%d: TSID %.*s\n",
+                         dev->name, dev->id,
+                         (int)sizeof(dev->supplement_adapter_info.VpdInfo.Tsid),
+                         dev->supplement_adapter_info.VpdInfo.Tsid);
+               }
+               if (!check_reset ||
+                 (dev->supplement_adapter_info.SupportedOptions2 &
+                 le32_to_cpu(AAC_OPTION_IGNORE_RESET))) {
+                       printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
+                         dev->name, dev->id);
+               }
        }
 
        dev->nondasd_support = 0;
@@ -1242,9 +1341,11 @@ static void io_callback(void *context, struct fib * fibptr)
        u32 cid;
 
        scsicmd = (struct scsi_cmnd *) context;
-       scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
 
-       dev = (struct aac_dev *)scsicmd->device->host->hostdata;
+       if (!aac_valid_context(scsicmd, fibptr))
+               return;
+
+       dev = fibptr->dev;
        cid = scmd_id(scsicmd);
 
        if (nblank(dprintk(x))) {
@@ -1283,16 +1384,9 @@ static void io_callback(void *context, struct fib * fibptr)
        }
 
        BUG_ON(fibptr == NULL);
-               
-       if(scsicmd->use_sg)
-               pci_unmap_sg(dev->pdev, 
-                       (struct scatterlist *)scsicmd->request_buffer,
-                       scsicmd->use_sg,
-                       scsicmd->sc_data_direction);
-       else if(scsicmd->request_bufflen)
-               pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
-                                scsicmd->request_bufflen,
-                                scsicmd->sc_data_direction);
+
+       scsi_dma_unmap(scsicmd);
+
        readreply = (struct aac_read_reply *)fib_data(fibptr);
        if (le32_to_cpu(readreply->status) == ST_OK)
                scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
@@ -1318,7 +1412,7 @@ static void io_callback(void *context, struct fib * fibptr)
        scsicmd->scsi_done(scsicmd);
 }
 
-static int aac_read(struct scsi_cmnd * scsicmd, int cid)
+static int aac_read(struct scsi_cmnd * scsicmd)
 {
        u64 lba;
        u32 count;
@@ -1332,7 +1426,7 @@ static int aac_read(struct scsi_cmnd * scsicmd, int cid)
         */
        switch (scsicmd->cmnd[0]) {
        case READ_6:
-               dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", cid));
+               dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
 
                lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | 
                        (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
@@ -1342,7 +1436,7 @@ static int aac_read(struct scsi_cmnd * scsicmd, int cid)
                        count = 256;
                break;
        case READ_16:
-               dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", cid));
+               dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
 
                lba =   ((u64)scsicmd->cmnd[2] << 56) |
                        ((u64)scsicmd->cmnd[3] << 48) |
@@ -1356,7 +1450,7 @@ static int aac_read(struct scsi_cmnd * scsicmd, int cid)
                        (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
                break;
        case READ_12:
-               dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", cid));
+               dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
 
                lba = ((u64)scsicmd->cmnd[2] << 24) | 
                        (scsicmd->cmnd[3] << 16) |
@@ -1366,7 +1460,7 @@ static int aac_read(struct scsi_cmnd * scsicmd, int cid)
                        (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
                break;
        default:
-               dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", cid));
+               dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
 
                lba = ((u64)scsicmd->cmnd[2] << 24) | 
                        (scsicmd->cmnd[3] << 16) | 
@@ -1406,10 +1500,11 @@ static int aac_read(struct scsi_cmnd * scsicmd, int cid)
        return 0;
 }
 
-static int aac_write(struct scsi_cmnd * scsicmd, int cid)
+static int aac_write(struct scsi_cmnd * scsicmd)
 {
        u64 lba;
        u32 count;
+       int fua;
        int status;
        struct aac_dev *dev;
        struct fib * cmd_fibcontext;
@@ -1424,8 +1519,9 @@ static int aac_write(struct scsi_cmnd * scsicmd, int cid)
                count = scsicmd->cmnd[4];
                if (count == 0)
                        count = 256;
+               fua = 0;
        } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
-               dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", cid));
+               dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
 
                lba =   ((u64)scsicmd->cmnd[2] << 56) |
                        ((u64)scsicmd->cmnd[3] << 48) |
@@ -1436,17 +1532,20 @@ static int aac_write(struct scsi_cmnd * scsicmd, int cid)
                        (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
                count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
                        (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
+               fua = scsicmd->cmnd[1] & 0x8;
        } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
-               dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", cid));
+               dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
 
                lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
                    | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
                count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
                      | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
+               fua = scsicmd->cmnd[1] & 0x8;
        } else {
-               dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", cid));
+               dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
                lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
                count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
+               fua = scsicmd->cmnd[1] & 0x8;
        }
        dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
          smp_processor_id(), (unsigned long long)lba, jiffies));
@@ -1461,7 +1560,7 @@ static int aac_write(struct scsi_cmnd * scsicmd, int cid)
                return 0;
        }
 
-       status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count);
+       status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
 
        /*
         *      Check that the command queued to the controller
@@ -1489,7 +1588,9 @@ static void synchronize_callback(void *context, struct fib *fibptr)
        struct scsi_cmnd *cmd;
 
        cmd = context;
-       cmd->SCp.phase = AAC_OWNER_MIDLEVEL;
+
+       if (!aac_valid_context(cmd, fibptr))
+               return;
 
        dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n", 
                                smp_processor_id(), jiffies));
@@ -1502,7 +1603,7 @@ static void synchronize_callback(void *context, struct fib *fibptr)
                        COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
        else {
                struct scsi_device *sdev = cmd->device;
-               struct aac_dev *dev = (struct aac_dev *)sdev->host->hostdata;
+               struct aac_dev *dev = fibptr->dev;
                u32 cid = sdev_id(sdev);
                printk(KERN_WARNING 
                     "synchronize_callback: synchronize failed, status = %d\n",
@@ -1524,7 +1625,7 @@ static void synchronize_callback(void *context, struct fib *fibptr)
        cmd->scsi_done(cmd);
 }
 
-static int aac_synchronize(struct scsi_cmnd *scsicmd, int cid)
+static int aac_synchronize(struct scsi_cmnd *scsicmd)
 {
        int status;
        struct fib *cmd_fibcontext;
@@ -1569,7 +1670,7 @@ static int aac_synchronize(struct scsi_cmnd *scsicmd, int cid)
        synchronizecmd = fib_data(cmd_fibcontext);
        synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
        synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
-       synchronizecmd->cid = cpu_to_le32(cid);
+       synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
        synchronizecmd->count = 
             cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
 
@@ -1609,7 +1710,7 @@ static int aac_synchronize(struct scsi_cmnd *scsicmd, int cid)
  
 int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
 {
-       u32 cid = 0;
+       u32 cid;
        struct Scsi_Host *host = scsicmd->device->host;
        struct aac_dev *dev = (struct aac_dev *)host->hostdata;
        struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
@@ -1621,15 +1722,15 @@ int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
         *      Test does not apply to ID 16, the pseudo id for the controller
         *      itself.
         */
-       if (scmd_id(scsicmd) != host->this_id) {
-               if ((scmd_channel(scsicmd) == CONTAINER_CHANNEL)) {
-                       if((scmd_id(scsicmd) >= dev->maximum_num_containers) ||
+       cid = scmd_id(scsicmd);
+       if (cid != host->this_id) {
+               if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
+                       if((cid >= dev->maximum_num_containers) ||
                                        (scsicmd->device->lun != 0)) {
                                scsicmd->result = DID_NO_CONNECT << 16;
                                scsicmd->scsi_done(scsicmd);
                                return 0;
                        }
-                       cid = scmd_id(scsicmd);
 
                        /*
                         *      If the target container doesn't exist, it may have
@@ -1647,29 +1748,12 @@ int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
                                case TEST_UNIT_READY:
                                        if (dev->in_reset)
                                                return -1;
-                                       spin_unlock_irq(host->host_lock);
-                                       aac_probe_container(dev, cid);
-                                       if ((fsa_dev_ptr[cid].valid & 1) == 0)
-                                               fsa_dev_ptr[cid].valid = 0;
-                                       spin_lock_irq(host->host_lock);
-                                       if (fsa_dev_ptr[cid].valid == 0) {
-                                               scsicmd->result = DID_NO_CONNECT << 16;
-                                               scsicmd->scsi_done(scsicmd);
-                                               return 0;
-                                       }
+                                       return _aac_probe_container(scsicmd,
+                                                       aac_probe_container_callback2);
                                default:
                                        break;
                                }
                        }
-                       /*
-                        *      If the target container still doesn't exist, 
-                        *      return failure
-                        */
-                       if (fsa_dev_ptr[cid].valid == 0) {
-                               scsicmd->result = DID_BAD_TARGET << 16;
-                               scsicmd->scsi_done(scsicmd);
-                               return 0;
-                       }
                } else {  /* check for physical non-dasd devices */
                        if ((dev->nondasd_support == 1) || expose_physicals) {
                                if (dev->in_reset)
@@ -1709,7 +1793,7 @@ int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
        {
                struct inquiry_data inq_data;
 
-               dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scmd_id(scsicmd)));
+               dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
                memset(&inq_data, 0, sizeof (struct inquiry_data));
 
                inq_data.inqd_ver = 2;  /* claim compliance to SCSI-2 */
@@ -1721,7 +1805,7 @@ int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
                 *      Set the Vendor, Product, and Revision Level
                 *      see: <vendor>.c i.e. aac.c
                 */
-               if (scmd_id(scsicmd) == host->this_id) {
+               if (cid == host->this_id) {
                        setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
                        inq_data.inqd_pdt = INQD_PDT_PROC;      /* Processor device */
                        aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
@@ -1734,7 +1818,7 @@ int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
                setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
                inq_data.inqd_pdt = INQD_PDT_DA;        /* Direct/random access device */
                aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
-               return aac_get_container_name(scsicmd, cid);
+               return aac_get_container_name(scsicmd);
        }
        case SERVICE_ACTION_IN:
                if (!(dev->raw_io_interface) ||
@@ -1813,15 +1897,29 @@ int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
 
        case MODE_SENSE:
        {
-               char mode_buf[4];
+               char mode_buf[7];
+               int mode_buf_length = 4;
 
                dprintk((KERN_DEBUG "MODE SENSE command.\n"));
                mode_buf[0] = 3;        /* Mode data length */
                mode_buf[1] = 0;        /* Medium type - default */
-               mode_buf[2] = 0;        /* Device-specific param, bit 8: 0/1 = write enabled/protected */
+               mode_buf[2] = 0;        /* Device-specific param,
+                                          bit 8: 0/1 = write enabled/protected
+                                          bit 4: 0/1 = FUA enabled */
+               if (dev->raw_io_interface)
+                       mode_buf[2] = 0x10;
                mode_buf[3] = 0;        /* Block descriptor length */
-
-               aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
+               if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
+                 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
+                       mode_buf[0] = 6;
+                       mode_buf[4] = 8;
+                       mode_buf[5] = 1;
+                       mode_buf[6] = 0x04; /* WCE */
+                       mode_buf_length = 7;
+                       if (mode_buf_length > scsicmd->cmnd[4])
+                               mode_buf_length = scsicmd->cmnd[4];
+               }
+               aac_internal_transfer(scsicmd, mode_buf, 0, mode_buf_length);
                scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
                scsicmd->scsi_done(scsicmd);
 
@@ -1829,18 +1927,33 @@ int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
        }
        case MODE_SENSE_10:
        {
-               char mode_buf[8];
+               char mode_buf[11];
+               int mode_buf_length = 8;
 
                dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
                mode_buf[0] = 0;        /* Mode data length (MSB) */
                mode_buf[1] = 6;        /* Mode data length (LSB) */
                mode_buf[2] = 0;        /* Medium type - default */
-               mode_buf[3] = 0;        /* Device-specific param, bit 8: 0/1 = write enabled/protected */
+               mode_buf[3] = 0;        /* Device-specific param,
+                                          bit 8: 0/1 = write enabled/protected
+                                          bit 4: 0/1 = FUA enabled */
+               if (dev->raw_io_interface)
+                       mode_buf[3] = 0x10;
                mode_buf[4] = 0;        /* reserved */
                mode_buf[5] = 0;        /* reserved */
                mode_buf[6] = 0;        /* Block descriptor length (MSB) */
                mode_buf[7] = 0;        /* Block descriptor length (LSB) */
-               aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
+               if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
+                 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
+                       mode_buf[1] = 9;
+                       mode_buf[8] = 8;
+                       mode_buf[9] = 1;
+                       mode_buf[10] = 0x04; /* WCE */
+                       mode_buf_length = 11;
+                       if (mode_buf_length > scsicmd->cmnd[8])
+                               mode_buf_length = scsicmd->cmnd[8];
+               }
+               aac_internal_transfer(scsicmd, mode_buf, 0, mode_buf_length);
 
                scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
                scsicmd->scsi_done(scsicmd);
@@ -1900,7 +2013,7 @@ int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
                                min(sizeof(fsa_dev_ptr[cid].devname),
                                sizeof(scsicmd->request->rq_disk->disk_name) + 1));
 
-                       return aac_read(scsicmd, cid);
+                       return aac_read(scsicmd);
 
                case WRITE_6:
                case WRITE_10:
@@ -1908,11 +2021,11 @@ int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
                case WRITE_16:
                        if (dev->in_reset)
                                return -1;
-                       return aac_write(scsicmd, cid);
+                       return aac_write(scsicmd);
 
                case SYNCHRONIZE_CACHE:
                        /* Issue FIB to tell Firmware to flush it's cache */
-                       return aac_synchronize(scsicmd, cid);
+                       return aac_synchronize(scsicmd);
                        
                default:
                        /*
@@ -2059,29 +2172,25 @@ static void aac_srb_callback(void *context, struct fib * fibptr)
        struct scsi_cmnd *scsicmd;
 
        scsicmd = (struct scsi_cmnd *) context;
-       scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
-       dev = (struct aac_dev *)scsicmd->device->host->hostdata;
+
+       if (!aac_valid_context(scsicmd, fibptr))
+               return;
 
        BUG_ON(fibptr == NULL);
 
+       dev = fibptr->dev;
+
        srbreply = (struct aac_srb_reply *) fib_data(fibptr);
 
        scsicmd->sense_buffer[0] = '\0';  /* Initialize sense valid flag to false */
        /*
         *      Calculate resid for sg 
         */
-        
-       scsicmd->resid = scsicmd->request_bufflen - 
-               le32_to_cpu(srbreply->data_xfer_length);
-
-       if(scsicmd->use_sg)
-               pci_unmap_sg(dev->pdev, 
-                       (struct scatterlist *)scsicmd->request_buffer,
-                       scsicmd->use_sg,
-                       scsicmd->sc_data_direction);
-       else if(scsicmd->request_bufflen)
-               pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle, scsicmd->request_bufflen,
-                       scsicmd->sc_data_direction);
+
+       scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
+                      - le32_to_cpu(srbreply->data_xfer_length));
+
+       scsi_dma_unmap(scsicmd);
 
        /*
         * First check the fib status
@@ -2267,34 +2376,33 @@ static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
 {
        struct aac_dev *dev;
        unsigned long byte_count = 0;
+       int nseg;
 
        dev = (struct aac_dev *)scsicmd->device->host->hostdata;
        // Get rid of old data
        psg->count = 0;
        psg->sg[0].addr = 0;
-       psg->sg[0].count = 0;  
-       if (scsicmd->use_sg) {
+       psg->sg[0].count = 0;
+
+       nseg = scsi_dma_map(scsicmd);
+       BUG_ON(nseg < 0);
+       if (nseg) {
                struct scatterlist *sg;
                int i;
-               int sg_count;
-               sg = (struct scatterlist *) scsicmd->request_buffer;
 
-               sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
-                       scsicmd->sc_data_direction);
-               psg->count = cpu_to_le32(sg_count);
+               psg->count = cpu_to_le32(nseg);
 
-               for (i = 0; i < sg_count; i++) {
+               scsi_for_each_sg(scsicmd, sg, nseg, i) {
                        psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
                        psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
                        byte_count += sg_dma_len(sg);
-                       sg++;
                }
                /* hba wants the size to be exact */
-               if(byte_count > scsicmd->request_bufflen){
-                       u32 temp = le32_to_cpu(psg->sg[i-1].count) - 
-                               (byte_count - scsicmd->request_bufflen);
+               if (byte_count > scsi_bufflen(scsicmd)) {
+                       u32 temp = le32_to_cpu(psg->sg[i-1].count) -
+                               (byte_count - scsi_bufflen(scsicmd));
                        psg->sg[i-1].count = cpu_to_le32(temp);
-                       byte_count = scsicmd->request_bufflen;
+                       byte_count = scsi_bufflen(scsicmd);
                }
                /* Check for command underflow */
                if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
@@ -2302,18 +2410,6 @@ static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
                                        byte_count, scsicmd->underflow);
                }
        }
-       else if(scsicmd->request_bufflen) {
-               u32 addr;
-               scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
-                               scsicmd->request_buffer,
-                               scsicmd->request_bufflen,
-                               scsicmd->sc_data_direction);
-               addr = scsicmd->SCp.dma_handle;
-               psg->count = cpu_to_le32(1);
-               psg->sg[0].addr = cpu_to_le32(addr);
-               psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);  
-               byte_count = scsicmd->request_bufflen;
-       }
        return byte_count;
 }
 
@@ -2323,6 +2419,7 @@ static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* p
        struct aac_dev *dev;
        unsigned long byte_count = 0;
        u64 addr;
+       int nseg;
 
        dev = (struct aac_dev *)scsicmd->device->host->hostdata;
        // Get rid of old data
@@ -2330,31 +2427,28 @@ static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* p
        psg->sg[0].addr[0] = 0;
        psg->sg[0].addr[1] = 0;
        psg->sg[0].count = 0;
-       if (scsicmd->use_sg) {
+
+       nseg = scsi_dma_map(scsicmd);
+       BUG_ON(nseg < 0);
+       if (nseg) {
                struct scatterlist *sg;
                int i;
-               int sg_count;
-               sg = (struct scatterlist *) scsicmd->request_buffer;
-
-               sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
-                       scsicmd->sc_data_direction);
 
-               for (i = 0; i < sg_count; i++) {
+               scsi_for_each_sg(scsicmd, sg, nseg, i) {
                        int count = sg_dma_len(sg);
                        addr = sg_dma_address(sg);
                        psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
                        psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
                        psg->sg[i].count = cpu_to_le32(count);
                        byte_count += count;
-                       sg++;
                }
-               psg->count = cpu_to_le32(sg_count);
+               psg->count = cpu_to_le32(nseg);
                /* hba wants the size to be exact */
-               if(byte_count > scsicmd->request_bufflen){
-                       u32 temp = le32_to_cpu(psg->sg[i-1].count) - 
-                               (byte_count - scsicmd->request_bufflen);
+               if (byte_count > scsi_bufflen(scsicmd)) {
+                       u32 temp = le32_to_cpu(psg->sg[i-1].count) -
+                               (byte_count - scsi_bufflen(scsicmd));
                        psg->sg[i-1].count = cpu_to_le32(temp);
-                       byte_count = scsicmd->request_bufflen;
+                       byte_count = scsi_bufflen(scsicmd);
                }
                /* Check for command underflow */
                if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
@@ -2362,26 +2456,13 @@ static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* p
                                        byte_count, scsicmd->underflow);
                }
        }
-       else if(scsicmd->request_bufflen) {
-               scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
-                               scsicmd->request_buffer,
-                               scsicmd->request_bufflen,
-                               scsicmd->sc_data_direction);
-               addr = scsicmd->SCp.dma_handle;
-               psg->count = cpu_to_le32(1);
-               psg->sg[0].addr[0] = cpu_to_le32(addr & 0xffffffff);
-               psg->sg[0].addr[1] = cpu_to_le32(addr >> 32);
-               psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);  
-               byte_count = scsicmd->request_bufflen;
-       }
        return byte_count;
 }
 
 static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
 {
-       struct Scsi_Host *host = scsicmd->device->host;
-       struct aac_dev *dev = (struct aac_dev *)host->hostdata;
        unsigned long byte_count = 0;
+       int nseg;
 
        // Get rid of old data
        psg->count = 0;
@@ -2391,16 +2472,14 @@ static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw*
        psg->sg[0].addr[1] = 0;
        psg->sg[0].count = 0;
        psg->sg[0].flags = 0;
-       if (scsicmd->use_sg) {
+
+       nseg = scsi_dma_map(scsicmd);
+       BUG_ON(nseg < 0);
+       if (nseg) {
                struct scatterlist *sg;
                int i;
-               int sg_count;
-               sg = (struct scatterlist *) scsicmd->request_buffer;
 
-               sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
-                       scsicmd->sc_data_direction);
-
-               for (i = 0; i < sg_count; i++) {
+               scsi_for_each_sg(scsicmd, sg, nseg, i) {
                        int count = sg_dma_len(sg);
                        u64 addr = sg_dma_address(sg);
                        psg->sg[i].next = 0;
@@ -2410,15 +2489,14 @@ static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw*
                        psg->sg[i].count = cpu_to_le32(count);
                        psg->sg[i].flags = 0;
                        byte_count += count;
-                       sg++;
                }
-               psg->count = cpu_to_le32(sg_count);
+               psg->count = cpu_to_le32(nseg);
                /* hba wants the size to be exact */
-               if(byte_count > scsicmd->request_bufflen){
-                       u32 temp = le32_to_cpu(psg->sg[i-1].count) - 
-                               (byte_count - scsicmd->request_bufflen);
+               if (byte_count > scsi_bufflen(scsicmd)) {
+                       u32 temp = le32_to_cpu(psg->sg[i-1].count) -
+                               (byte_count - scsi_bufflen(scsicmd));
                        psg->sg[i-1].count = cpu_to_le32(temp);
-                       byte_count = scsicmd->request_bufflen;
+                       byte_count = scsi_bufflen(scsicmd);
                }
                /* Check for command underflow */
                if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
@@ -2426,24 +2504,6 @@ static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw*
                                        byte_count, scsicmd->underflow);
                }
        }
-       else if(scsicmd->request_bufflen) {
-               int count;
-               u64 addr;
-               scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
-                               scsicmd->request_buffer,
-                               scsicmd->request_bufflen,
-                               scsicmd->sc_data_direction);
-               addr = scsicmd->SCp.dma_handle;
-               count = scsicmd->request_bufflen;
-               psg->count = cpu_to_le32(1);
-               psg->sg[0].next = 0;
-               psg->sg[0].prev = 0;
-               psg->sg[0].addr[1] = cpu_to_le32((u32)(addr>>32));
-               psg->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
-               psg->sg[0].count = cpu_to_le32(count);
-               psg->sg[0].flags = 0;
-               byte_count = scsicmd->request_bufflen;
-       }
        return byte_count;
 }