[SCSI] mpt2sas: fix the incorrect scsi_dma_map error checking
[safe/jmp/linux-2.6] / drivers / scsi / mpt2sas / mpt2sas_scsih.c
index 471c3b6..aa67b75 100644 (file)
@@ -2,7 +2,7 @@
  * Scsi Host Layer for MPT (Message Passing Technology) based controllers
  *
  * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
- * Copyright (C) 2007-2008  LSI Corporation
+ * Copyright (C) 2007-2009  LSI Corporation
  *  (mailto:DL-MPTFusionLinux@lsi.com)
  *
  * This program is free software; you can redistribute it and/or
@@ -52,6 +52,8 @@
 #include <linux/delay.h>
 #include <linux/pci.h>
 #include <linux/interrupt.h>
+#include <linux/raid_class.h>
+#include <linux/slab.h>
 
 #include "mpt2sas_base.h"
 
@@ -76,9 +78,13 @@ static u8 tm_cb_idx = -1;
 static u8 ctl_cb_idx = -1;
 static u8 base_cb_idx = -1;
 static u8 transport_cb_idx = -1;
+static u8 scsih_cb_idx = -1;
 static u8 config_cb_idx = -1;
 static int mpt_ids;
 
+static u8 tm_tr_cb_idx = -1 ;
+static u8 tm_sas_control_cb_idx = -1;
+
 /* command line options */
 static u32 logging_level;
 MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
@@ -109,6 +115,7 @@ struct sense_info {
  * @work: work object (ioc->fault_reset_work_q)
  * @ioc: per adapter object
  * @VF_ID: virtual function id
+ * @VP_ID: virtual port id
  * @host_reset_handling: handling events during host reset
  * @ignore: flag meaning this event has been marked to ignore
  * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
@@ -121,12 +128,16 @@ struct fw_event_work {
        struct work_struct      work;
        struct MPT2SAS_ADAPTER *ioc;
        u8                      VF_ID;
+       u8                      VP_ID;
        u8                      host_reset_handling;
        u8                      ignore;
        u16                     event;
        void                    *event_data;
 };
 
+/* raid transport support */
+static struct raid_template *mpt2sas_raid_template;
+
 /**
  * struct _scsi_io_transfer - scsi io transfer
  * @handle: sas device handle (assigned by firmware)
@@ -138,8 +149,10 @@ struct fw_event_work {
  * @lun: lun number
  * @cdb_length: cdb length
  * @cdb: cdb contents
- * @valid_reply: flag set for reply message
  * @timeout: timeout for this command
+ * @VF_ID: virtual function id
+ * @VP_ID: virtual port id
+ * @valid_reply: flag set for reply message
  * @sense_length: sense length
  * @ioc_status: ioc status
  * @scsi_state: scsi state
@@ -161,6 +174,8 @@ struct _scsi_io_transfer {
        u8      cdb_length;
        u8      cdb[32];
        u8      timeout;
+       u8      VF_ID;
+       u8      VP_ID;
        u8      valid_reply;
   /* the following bits are only valid when 'valid_reply = 1' */
        u32     sense_length;
@@ -187,10 +202,28 @@ static struct pci_device_id scsih_pci_table[] = {
                PCI_ANY_ID, PCI_ANY_ID },
        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
                PCI_ANY_ID, PCI_ANY_ID },
+       /* Meteor ~ 2116 */
        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
                PCI_ANY_ID, PCI_ANY_ID },
        { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
                PCI_ANY_ID, PCI_ANY_ID },
+       /* Thunderbolt ~ 2208 */
+       { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
+               PCI_ANY_ID, PCI_ANY_ID },
+       { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
+               PCI_ANY_ID, PCI_ANY_ID },
+       { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
+               PCI_ANY_ID, PCI_ANY_ID },
+       { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
+               PCI_ANY_ID, PCI_ANY_ID },
+       { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
+               PCI_ANY_ID, PCI_ANY_ID },
+       { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
+               PCI_ANY_ID, PCI_ANY_ID },
+       { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_7,
+               PCI_ANY_ID, PCI_ANY_ID },
+       { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_8,
+               PCI_ANY_ID, PCI_ANY_ID },
        {0}     /* Terminating entry */
 };
 MODULE_DEVICE_TABLE(pci, scsih_pci_table);
@@ -308,6 +341,47 @@ _scsih_is_boot_device(u64 sas_address, u64 device_name,
 }
 
 /**
+ * _scsih_get_sas_address - set the sas_address for given device handle
+ * @handle: device handle
+ * @sas_address: sas address
+ *
+ * Returns 0 success, non-zero when failure
+ */
+static int
+_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
+    u64 *sas_address)
+{
+       Mpi2SasDevicePage0_t sas_device_pg0;
+       Mpi2ConfigReply_t mpi_reply;
+       u32 ioc_status;
+
+       if (handle <= ioc->sas_hba.num_phys) {
+               *sas_address = ioc->sas_hba.sas_address;
+               return 0;
+       } else
+               *sas_address = 0;
+
+       if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
+           MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
+               printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
+                   ioc->name, __FILE__, __LINE__, __func__);
+               return -ENXIO;
+       }
+
+       ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
+           MPI2_IOCSTATUS_MASK;
+       if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
+               printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
+                   "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
+                    __FILE__, __LINE__, __func__);
+               return -EIO;
+       }
+
+       *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
+       return 0;
+}
+
+/**
  * _scsih_determine_boot_device - determine boot device.
  * @ioc: per adapter object
  * @device: either sas_device or raid_device object
@@ -501,8 +575,6 @@ _scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
     struct _sas_device *sas_device)
 {
        unsigned long flags;
-       u16 handle, parent_handle;
-       u64 sas_address;
 
        dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
            "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
@@ -512,10 +584,8 @@ _scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
        list_add_tail(&sas_device->list, &ioc->sas_device_list);
        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 
-       handle = sas_device->handle;
-       parent_handle = sas_device->parent_handle;
-       sas_address = sas_device->sas_address;
-       if (!mpt2sas_transport_port_add(ioc, handle, parent_handle))
+       if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
+            sas_device->sas_address_parent))
                _scsih_sas_device_remove(ioc, sas_device);
 }
 
@@ -544,31 +614,6 @@ _scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
 }
 
 /**
- * mpt2sas_scsih_expander_find_by_handle - expander device search
- * @ioc: per adapter object
- * @handle: expander handle (assigned by firmware)
- * Context: Calling function should acquire ioc->sas_device_lock
- *
- * This searches for expander device based on handle, then returns the
- * sas_node object.
- */
-struct _sas_node *
-mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
-{
-       struct _sas_node *sas_expander, *r;
-
-       r = NULL;
-       list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
-               if (sas_expander->handle != handle)
-                       continue;
-               r = sas_expander;
-               goto out;
-       }
- out:
-       return r;
-}
-
-/**
  * _scsih_raid_device_find_by_id - raid device search
  * @ioc: per adapter object
  * @id: sas device target id
@@ -690,6 +735,31 @@ _scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
 }
 
 /**
+ * mpt2sas_scsih_expander_find_by_handle - expander device search
+ * @ioc: per adapter object
+ * @handle: expander handle (assigned by firmware)
+ * Context: Calling function should acquire ioc->sas_device_lock
+ *
+ * This searches for expander device based on handle, then returns the
+ * sas_node object.
+ */
+struct _sas_node *
+mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
+{
+       struct _sas_node *sas_expander, *r;
+
+       r = NULL;
+       list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
+               if (sas_expander->handle != handle)
+                       continue;
+               r = sas_expander;
+               goto out;
+       }
+ out:
+       return r;
+}
+
+/**
  * mpt2sas_scsih_expander_find_by_sas_address - expander device search
  * @ioc: per adapter object
  * @sas_address: sas address
@@ -756,66 +826,16 @@ _scsih_is_end_device(u32 device_info)
 }
 
 /**
- * _scsih_scsi_lookup_get - returns scmd entry
+ * mptscsih_get_scsi_lookup - returns scmd entry
  * @ioc: per adapter object
  * @smid: system request message index
- * Context: This function will acquire ioc->scsi_lookup_lock.
  *
  * Returns the smid stored scmd pointer.
  */
 static struct scsi_cmnd *
 _scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
 {
-       unsigned long   flags;
-       struct scsi_cmnd *scmd;
-
-       spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
-       scmd = ioc->scsi_lookup[smid - 1].scmd;
-       spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
-       return scmd;
-}
-
-/**
- * mptscsih_getclear_scsi_lookup - returns scmd entry
- * @ioc: per adapter object
- * @smid: system request message index
- * Context: This function will acquire ioc->scsi_lookup_lock.
- *
- * Returns the smid stored scmd pointer, as well as clearing the scmd pointer.
- */
-static struct scsi_cmnd *
-_scsih_scsi_lookup_getclear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
-{
-       unsigned long   flags;
-       struct scsi_cmnd *scmd;
-
-       spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
-       scmd = ioc->scsi_lookup[smid - 1].scmd;
-       ioc->scsi_lookup[smid - 1].scmd = NULL;
-       spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
-       return scmd;
-}
-
-/**
- * _scsih_scsi_lookup_set - updates scmd entry in lookup
- * @ioc: per adapter object
- * @smid: system request message index
- * @scmd: pointer to scsi command object
- * Context: This function will acquire ioc->scsi_lookup_lock.
- *
- * This will save scmd pointer in the scsi_lookup array.
- *
- * Return nothing.
- */
-static void
-_scsih_scsi_lookup_set(struct MPT2SAS_ADAPTER *ioc, u16 smid,
-    struct scsi_cmnd *scmd)
-{
-       unsigned long   flags;
-
-       spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
-       ioc->scsi_lookup[smid - 1].scmd = scmd;
-       spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
+       return ioc->scsi_lookup[smid - 1].scmd;
 }
 
 /**
@@ -838,9 +858,9 @@ _scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
 
        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
        smid = 0;
-       for (i = 0; i < ioc->request_depth; i++) {
+       for (i = 0; i < ioc->scsiio_depth; i++) {
                if (ioc->scsi_lookup[i].scmd == scmd) {
-                       smid = i + 1;
+                       smid = ioc->scsi_lookup[i].smid;
                        goto out;
                }
        }
@@ -869,7 +889,7 @@ _scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
 
        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
        found = 0;
-       for (i = 0 ; i < ioc->request_depth; i++) {
+       for (i = 0 ; i < ioc->scsiio_depth; i++) {
                if (ioc->scsi_lookup[i].scmd &&
                    (ioc->scsi_lookup[i].scmd->device->id == id &&
                    ioc->scsi_lookup[i].scmd->device->channel == channel)) {
@@ -903,7 +923,7 @@ _scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
 
        spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
        found = 0;
-       for (i = 0 ; i < ioc->request_depth; i++) {
+       for (i = 0 ; i < ioc->scsiio_depth; i++) {
                if (ioc->scsi_lookup[i].scmd &&
                    (ioc->scsi_lookup[i].scmd->device->id == id &&
                    ioc->scsi_lookup[i].scmd->device->channel == channel &&
@@ -968,7 +988,7 @@ _scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
        u32 chain_offset;
        u32 chain_length;
        u32 chain_flags;
-       u32 sges_left;
+       int sges_left;
        u32 sges_in_segment;
        u32 sgl_flags;
        u32 sgl_flags_last_element;
@@ -989,7 +1009,7 @@ _scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
 
        sg_scmd = scsi_sglist(scmd);
        sges_left = scsi_dma_map(scmd);
-       if (!sges_left) {
+       if (sges_left < 0) {
                sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
                " failed: request for %d bytes!\n", scsi_bufflen(scmd));
                return -ENOMEM;
@@ -1084,17 +1104,46 @@ _scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
  * _scsih_change_queue_depth - setting device queue depth
  * @sdev: scsi device struct
  * @qdepth: requested queue depth
+ * @reason: calling context
  *
  * Returns queue depth.
  */
 static int
-_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
+_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
 {
        struct Scsi_Host *shost = sdev->host;
        int max_depth;
        int tag_type;
+       struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
+       struct MPT2SAS_DEVICE *sas_device_priv_data;
+       struct MPT2SAS_TARGET *sas_target_priv_data;
+       struct _sas_device *sas_device;
+       unsigned long flags;
+
+       if (reason != SCSI_QDEPTH_DEFAULT)
+               return -EOPNOTSUPP;
 
        max_depth = shost->can_queue;
+
+       /* limit max device queue for SATA to 32 */
+       sas_device_priv_data = sdev->hostdata;
+       if (!sas_device_priv_data)
+               goto not_sata;
+       sas_target_priv_data = sas_device_priv_data->sas_target;
+       if (!sas_target_priv_data)
+               goto not_sata;
+       if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
+               goto not_sata;
+       spin_lock_irqsave(&ioc->sas_device_lock, flags);
+       sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
+          sas_device_priv_data->sas_target->sas_address);
+       spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
+       if (sas_device && sas_device->device_info &
+           MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
+               max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
+
+ not_sata:
+
        if (!sdev->tagged_supported)
                max_depth = 1;
        if (qdepth > max_depth)
@@ -1113,7 +1162,7 @@ _scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
 }
 
 /**
- * _scsih_change_queue_depth - changing device queue tag type
+ * _scsih_change_queue_type - changing device queue tag type
  * @sdev: scsi device struct
  * @tag_type: requested tag type
  *
@@ -1261,7 +1310,6 @@ _scsih_slave_alloc(struct scsi_device *sdev)
        struct MPT2SAS_DEVICE *sas_device_priv_data;
        struct scsi_target *starget;
        struct _raid_device *raid_device;
-       struct _sas_device *sas_device;
        unsigned long flags;
 
        sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
@@ -1288,21 +1336,8 @@ _scsih_slave_alloc(struct scsi_device *sdev)
                if (raid_device)
                        raid_device->sdev = sdev; /* raid is single lun */
                spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
-       } else {
-               /* set TLR bit for SSP devices */
-               if (!(ioc->facts.IOCCapabilities &
-                    MPI2_IOCFACTS_CAPABILITY_TLR))
-                       goto out;
-               spin_lock_irqsave(&ioc->sas_device_lock, flags);
-               sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
-                  sas_device_priv_data->sas_target->sas_address);
-               spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
-               if (sas_device && sas_device->device_info &
-                   MPI2_SAS_DEVICE_INFO_SSP_TARGET)
-                       sas_device_priv_data->flags |= MPT_DEVICE_TLR_ON;
        }
 
- out:
        return 0;
 }
 
@@ -1375,6 +1410,140 @@ _scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
 }
 
 /**
+ * _scsih_is_raid - return boolean indicating device is raid volume
+ * @dev the device struct object
+ */
+static int
+_scsih_is_raid(struct device *dev)
+{
+       struct scsi_device *sdev = to_scsi_device(dev);
+
+       return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
+}
+
+/**
+ * _scsih_get_resync - get raid volume resync percent complete
+ * @dev the device struct object
+ */
+static void
+_scsih_get_resync(struct device *dev)
+{
+       struct scsi_device *sdev = to_scsi_device(dev);
+       struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
+       static struct _raid_device *raid_device;
+       unsigned long flags;
+       Mpi2RaidVolPage0_t vol_pg0;
+       Mpi2ConfigReply_t mpi_reply;
+       u32 volume_status_flags;
+       u8 percent_complete = 0;
+
+       spin_lock_irqsave(&ioc->raid_device_lock, flags);
+       raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
+           sdev->channel);
+       spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
+
+       if (!raid_device)
+               goto out;
+
+       if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
+            MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
+            sizeof(Mpi2RaidVolPage0_t))) {
+               printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
+                   ioc->name, __FILE__, __LINE__, __func__);
+               goto out;
+       }
+
+       volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
+       if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
+               percent_complete = raid_device->percent_complete;
+ out:
+       raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
+}
+
+/**
+ * _scsih_get_state - get raid volume level
+ * @dev the device struct object
+ */
+static void
+_scsih_get_state(struct device *dev)
+{
+       struct scsi_device *sdev = to_scsi_device(dev);
+       struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
+       static struct _raid_device *raid_device;
+       unsigned long flags;
+       Mpi2RaidVolPage0_t vol_pg0;
+       Mpi2ConfigReply_t mpi_reply;
+       u32 volstate;
+       enum raid_state state = RAID_STATE_UNKNOWN;
+
+       spin_lock_irqsave(&ioc->raid_device_lock, flags);
+       raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
+           sdev->channel);
+       spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
+
+       if (!raid_device)
+               goto out;
+
+       if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
+            MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
+            sizeof(Mpi2RaidVolPage0_t))) {
+               printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
+                   ioc->name, __FILE__, __LINE__, __func__);
+               goto out;
+       }
+
+       volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
+       if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
+               state = RAID_STATE_RESYNCING;
+               goto out;
+       }
+
+       switch (vol_pg0.VolumeState) {
+       case MPI2_RAID_VOL_STATE_OPTIMAL:
+       case MPI2_RAID_VOL_STATE_ONLINE:
+               state = RAID_STATE_ACTIVE;
+               break;
+       case  MPI2_RAID_VOL_STATE_DEGRADED:
+               state = RAID_STATE_DEGRADED;
+               break;
+       case MPI2_RAID_VOL_STATE_FAILED:
+       case MPI2_RAID_VOL_STATE_MISSING:
+               state = RAID_STATE_OFFLINE;
+               break;
+       }
+ out:
+       raid_set_state(mpt2sas_raid_template, dev, state);
+}
+
+/**
+ * _scsih_set_level - set raid level
+ * @sdev: scsi device struct
+ * @raid_device: raid_device object
+ */
+static void
+_scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
+{
+       enum raid_level level = RAID_LEVEL_UNKNOWN;
+
+       switch (raid_device->volume_type) {
+       case MPI2_RAID_VOL_TYPE_RAID0:
+               level = RAID_LEVEL_0;
+               break;
+       case MPI2_RAID_VOL_TYPE_RAID10:
+               level = RAID_LEVEL_10;
+               break;
+       case MPI2_RAID_VOL_TYPE_RAID1E:
+               level = RAID_LEVEL_1E;
+               break;
+       case MPI2_RAID_VOL_TYPE_RAID1:
+               level = RAID_LEVEL_1;
+               break;
+       }
+
+       raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
+}
+
+/**
  * _scsih_get_volume_capabilities - volume capabilities
  * @ioc: per adapter object
  * @sas_device: the raid_device object
@@ -1435,6 +1604,32 @@ _scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
 }
 
 /**
+ * _scsih_enable_tlr - setting TLR flags
+ * @ioc: per adapter object
+ * @sdev: scsi device struct
+ *
+ * Enabling Transaction Layer Retries for tape devices when
+ * vpd page 0x90 is present
+ *
+ */
+static void
+_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
+{
+       /* only for TAPE */
+       if (sdev->type != TYPE_TAPE)
+               return;
+
+       if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
+               return;
+
+       sas_enable_tlr(sdev);
+       sdev_printk(KERN_INFO, sdev, "TLR %s\n",
+           sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
+       return;
+
+}
+
+/**
  * _scsih_slave_configure - device configure routine.
  * @sdev: scsi device struct
  *
@@ -1501,7 +1696,13 @@ _scsih_slave_configure(struct scsi_device *sdev)
                        break;
                case MPI2_RAID_VOL_TYPE_RAID1E:
                        qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
-                       r_level = "RAID1E";
+                       if (ioc->manu_pg10.OEMIdentifier &&
+                           (ioc->manu_pg10.GenericFlags0 &
+                           MFG10_GF0_R10_DISPLAY) &&
+                           !(raid_device->num_pds % 2))
+                               r_level = "RAID10";
+                       else
+                               r_level = "RAID1E";
                        break;
                case MPI2_RAID_VOL_TYPE_RAID1:
                        qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
@@ -1523,7 +1724,9 @@ _scsih_slave_configure(struct scsi_device *sdev)
                    r_level, raid_device->handle,
                    (unsigned long long)raid_device->wwid,
                    raid_device->num_pds, ds);
-               _scsih_change_queue_depth(sdev, qdepth);
+               _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
+               /* raid transport support */
+               _scsih_set_level(sdev, raid_device);
                return 0;
        }
 
@@ -1569,10 +1772,12 @@ _scsih_slave_configure(struct scsi_device *sdev)
                        _scsih_display_sata_capabilities(ioc, sas_device, sdev);
        }
 
-       _scsih_change_queue_depth(sdev, qdepth);
+       _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
 
-       if (ssp_target)
+       if (ssp_target) {
                sas_read_port_mode_page(sdev);
+               _scsih_enable_tlr(ioc, sdev);
+       }
        return 0;
 }
 
@@ -1673,23 +1878,24 @@ _scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
  * _scsih_tm_done - tm completion routine
  * @ioc: per adapter object
  * @smid: system request message index
- * @VF_ID: virtual function id
+ * @msix_index: MSIX table index supplied by the OS
  * @reply: reply message frame(lower 32bit addr)
  * Context: none.
  *
  * The callback handler when using scsih_issue_tm.
  *
- * Return nothing.
+ * Return 1 meaning mf should be freed from _base_interrupt
+ *        0 means the mf is freed from this function.
  */
-static void
-_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
+static u8
+_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
 {
        MPI2DefaultReply_t *mpi_reply;
 
        if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
-               return;
+               return 1;
        if (ioc->tm_cmds.smid != smid)
-               return;
+               return 1;
        ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
        mpi_reply =  mpt2sas_base_get_reply_virt_addr(ioc, reply);
        if (mpi_reply) {
@@ -1698,6 +1904,7 @@ _scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
        }
        ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
        complete(&ioc->tm_cmds.done);
+       return 1;
 }
 
 /**
@@ -1784,18 +1991,18 @@ mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
        u16 smid = 0;
        u32 ioc_state;
        unsigned long timeleft;
-       u8 VF_ID = 0;
-       unsigned long flags;
 
-       spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
-       if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED ||
-           ioc->shost_recovery) {
-               spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
+       if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
+               printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
+                   __func__, ioc->name);
+               return;
+       }
+
+       if (ioc->shost_recovery) {
                printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
                    __func__, ioc->name);
                return;
        }
-       spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
 
        ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
        if (ioc_state & MPI2_DOORBELL_USED) {
@@ -1810,7 +2017,7 @@ mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
                goto issue_host_reset;
        }
 
-       smid = mpt2sas_base_get_smid(ioc, ioc->tm_cb_idx);
+       smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
        if (!smid) {
                printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
                    ioc->name, __func__);
@@ -1818,7 +2025,8 @@ mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
        }
 
        dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
-           " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type, smid));
+           " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
+           smid_task));
        ioc->tm_cmds.status = MPT2_CMD_PENDING;
        mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
        ioc->tm_cmds.smid = smid;
@@ -1827,9 +2035,12 @@ mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
        mpi_request->DevHandle = cpu_to_le16(handle);
        mpi_request->TaskType = type;
        mpi_request->TaskMID = cpu_to_le16(smid_task);
+       mpi_request->VP_ID = 0;  /* TODO */
+       mpi_request->VF_ID = 0;
        int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
        mpt2sas_scsih_set_tm_flag(ioc, handle);
-       mpt2sas_base_put_smid_hi_priority(ioc, smid, VF_ID);
+       init_completion(&ioc->tm_cmds.done);
+       mpt2sas_base_put_smid_hi_priority(ioc, smid);
        timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
        mpt2sas_scsih_clear_tm_flag(ioc, handle);
        if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
@@ -1903,6 +2114,8 @@ _scsih_abort(struct scsi_cmnd *scmd)
                goto out;
        }
 
+       mpt2sas_halt_firmware(ioc);
+
        mutex_lock(&ioc->tm_cmds.mutex);
        handle = sas_device_priv_data->sas_target->handle;
        mpt2sas_scsih_issue_tm(ioc, handle, sas_device_priv_data->lun,
@@ -2067,7 +2280,7 @@ _scsih_target_reset(struct scsi_cmnd *scmd)
 }
 
 /**
- * _scsih_abort - eh threads main host reset routine
+ * _scsih_host_reset - eh threads main host reset routine
  * @sdev: scsi device struct
  *
  * Returns SUCCESS if command aborted else FAILED
@@ -2221,7 +2434,7 @@ _scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
                            MPT2SAS_INFO_FMT "SDEV_RUNNING: "
                            "handle(0x%04x)\n", ioc->name, handle));
                        sas_device_priv_data->block = 0;
-                       scsi_device_set_state(sdev, SDEV_RUNNING);
+                       scsi_internal_device_unblock(sdev);
                }
        }
 }
@@ -2250,7 +2463,7 @@ _scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
                            MPT2SAS_INFO_FMT "SDEV_BLOCK: "
                            "handle(0x%04x)\n", ioc->name, handle));
                        sas_device_priv_data->block = 1;
-                       scsi_device_set_state(sdev, SDEV_BLOCK);
+                       scsi_internal_device_block(sdev);
                }
        }
 }
@@ -2340,6 +2553,246 @@ _scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
 }
 
 /**
+ * _scsih_tm_tr_send - send task management request
+ * @ioc: per adapter object
+ * @handle: device handle
+ * Context: interrupt time.
+ *
+ * This code is to initiate the device removal handshake protocal
+ * with controller firmware.  This function will issue target reset
+ * using high priority request queue.  It will send a sas iounit
+ * controll request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
+ *
+ * This is designed to send muliple task management request at the same
+ * time to the fifo. If the fifo is full, we will append the request,
+ * and process it in a future completion.
+ */
+static void
+_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
+{
+       Mpi2SCSITaskManagementRequest_t *mpi_request;
+       struct MPT2SAS_TARGET *sas_target_priv_data;
+       u16 smid;
+       struct _sas_device *sas_device;
+       unsigned long flags;
+       struct _tr_list *delayed_tr;
+
+       if (ioc->shost_recovery) {
+               printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
+                   __func__, ioc->name);
+               return;
+       }
+
+       spin_lock_irqsave(&ioc->sas_device_lock, flags);
+       sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
+       spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
+
+       /* skip is hidden raid component */
+       if (sas_device && sas_device->hidden_raid_component)
+               return;
+
+       smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
+       if (!smid) {
+               delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
+               if (!delayed_tr)
+                       return;
+               INIT_LIST_HEAD(&delayed_tr->list);
+               delayed_tr->handle = handle;
+               delayed_tr->state = MPT2SAS_REQ_SAS_CNTRL;
+               list_add_tail(&delayed_tr->list,
+                   &ioc->delayed_tr_list);
+               if (sas_device && sas_device->starget) {
+                       dewtprintk(ioc, starget_printk(KERN_INFO,
+                           sas_device->starget, "DELAYED:tr:handle(0x%04x), "
+                           "(open)\n", handle));
+               } else {
+                       dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
+                           "DELAYED:tr:handle(0x%04x), (open)\n",
+                           ioc->name, handle));
+               }
+               return;
+       }
+
+       if (sas_device) {
+               sas_device->state |= MPTSAS_STATE_TR_SEND;
+               sas_device->state |= MPT2SAS_REQ_SAS_CNTRL;
+               if (sas_device->starget && sas_device->starget->hostdata) {
+                       sas_target_priv_data = sas_device->starget->hostdata;
+                       sas_target_priv_data->tm_busy = 1;
+                       dewtprintk(ioc, starget_printk(KERN_INFO,
+                           sas_device->starget, "tr:handle(0x%04x), (open)\n",
+                           handle));
+               }
+       } else {
+               dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
+                   "tr:handle(0x%04x), (open)\n", ioc->name, handle));
+       }
+
+       mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
+       memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
+       mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
+       mpi_request->DevHandle = cpu_to_le16(handle);
+       mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
+       mpt2sas_base_put_smid_hi_priority(ioc, smid);
+}
+
+
+
+/**
+ * _scsih_sas_control_complete - completion routine
+ * @ioc: per adapter object
+ * @smid: system request message index
+ * @msix_index: MSIX table index supplied by the OS
+ * @reply: reply message frame(lower 32bit addr)
+ * Context: interrupt time.
+ *
+ * This is the sas iounit controll completion routine.
+ * This code is part of the code to initiate the device removal
+ * handshake protocal with controller firmware.
+ *
+ * Return 1 meaning mf should be freed from _base_interrupt
+ *        0 means the mf is freed from this function.
+ */
+static u8
+_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
+    u8 msix_index, u32 reply)
+{
+       unsigned long flags;
+       u16 handle;
+       struct _sas_device *sas_device;
+       Mpi2SasIoUnitControlReply_t *mpi_reply =
+           mpt2sas_base_get_reply_virt_addr(ioc, reply);
+
+       handle = le16_to_cpu(mpi_reply->DevHandle);
+
+       spin_lock_irqsave(&ioc->sas_device_lock, flags);
+       sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
+       spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
+
+       if (sas_device) {
+               sas_device->state |= MPTSAS_STATE_CNTRL_COMPLETE;
+               if (sas_device->starget)
+                       dewtprintk(ioc, starget_printk(KERN_INFO,
+                           sas_device->starget,
+                           "sc_complete:handle(0x%04x), "
+                           "ioc_status(0x%04x), loginfo(0x%08x)\n",
+                           handle, le16_to_cpu(mpi_reply->IOCStatus),
+                           le32_to_cpu(mpi_reply->IOCLogInfo)));
+       } else {
+               dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
+                   "sc_complete:handle(0x%04x), "
+                   "ioc_status(0x%04x), loginfo(0x%08x)\n",
+                   ioc->name, handle, le16_to_cpu(mpi_reply->IOCStatus),
+                   le32_to_cpu(mpi_reply->IOCLogInfo)));
+       }
+
+       return 1;
+}
+
+/**
+ * _scsih_tm_tr_complete -
+ * @ioc: per adapter object
+ * @smid: system request message index
+ * @msix_index: MSIX table index supplied by the OS
+ * @reply: reply message frame(lower 32bit addr)
+ * Context: interrupt time.
+ *
+ * This is the target reset completion routine.
+ * This code is part of the code to initiate the device removal
+ * handshake protocal with controller firmware.
+ * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
+ *
+ * Return 1 meaning mf should be freed from _base_interrupt
+ *        0 means the mf is freed from this function.
+ */
+static u8
+_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
+    u32 reply)
+{
+       unsigned long flags;
+       u16 handle;
+       struct _sas_device *sas_device;
+       Mpi2SCSITaskManagementReply_t *mpi_reply =
+           mpt2sas_base_get_reply_virt_addr(ioc, reply);
+       Mpi2SasIoUnitControlRequest_t *mpi_request;
+       u16 smid_sas_ctrl;
+       struct MPT2SAS_TARGET *sas_target_priv_data;
+       struct _tr_list *delayed_tr;
+       u8 rc;
+
+       handle = le16_to_cpu(mpi_reply->DevHandle);
+       spin_lock_irqsave(&ioc->sas_device_lock, flags);
+       sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
+       spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
+
+       if (sas_device) {
+               sas_device->state |= MPTSAS_STATE_TR_COMPLETE;
+               if (sas_device->starget) {
+                       dewtprintk(ioc, starget_printk(KERN_INFO,
+                           sas_device->starget, "tr_complete:handle(0x%04x), "
+                           "(%s) ioc_status(0x%04x), loginfo(0x%08x), "
+                           "completed(%d)\n", sas_device->handle,
+                           (sas_device->state & MPT2SAS_REQ_SAS_CNTRL) ?
+                           "open" : "active",
+                           le16_to_cpu(mpi_reply->IOCStatus),
+                           le32_to_cpu(mpi_reply->IOCLogInfo),
+                           le32_to_cpu(mpi_reply->TerminationCount)));
+                       if (sas_device->starget->hostdata) {
+                               sas_target_priv_data =
+                                   sas_device->starget->hostdata;
+                               sas_target_priv_data->tm_busy = 0;
+                       }
+               }
+       } else {
+               dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
+                   "tr_complete:handle(0x%04x), (open) ioc_status(0x%04x), "
+                   "loginfo(0x%08x), completed(%d)\n", ioc->name,
+                   handle, le16_to_cpu(mpi_reply->IOCStatus),
+                   le32_to_cpu(mpi_reply->IOCLogInfo),
+                   le32_to_cpu(mpi_reply->TerminationCount)));
+       }
+
+       if (!list_empty(&ioc->delayed_tr_list)) {
+               delayed_tr = list_entry(ioc->delayed_tr_list.next,
+                   struct _tr_list, list);
+               mpt2sas_base_free_smid(ioc, smid);
+               if (delayed_tr->state & MPT2SAS_REQ_SAS_CNTRL)
+                       _scsih_tm_tr_send(ioc, delayed_tr->handle);
+               list_del(&delayed_tr->list);
+               kfree(delayed_tr);
+               rc = 0; /* tells base_interrupt not to free mf */
+       } else
+               rc = 1;
+
+       if (sas_device && !(sas_device->state & MPT2SAS_REQ_SAS_CNTRL))
+               return rc;
+
+       if (ioc->shost_recovery) {
+               printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
+                   __func__, ioc->name);
+               return rc;
+       }
+
+       smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
+       if (!smid_sas_ctrl) {
+               printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
+                   ioc->name, __func__);
+               return rc;
+       }
+
+       if (sas_device)
+               sas_device->state |= MPTSAS_STATE_CNTRL_SEND;
+
+       mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
+       memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
+       mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
+       mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
+       mpi_request->DevHandle = mpi_reply->DevHandle;
+       mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
+       return rc;
+}
+
+/**
  * _scsih_check_topo_delete_events - sanity check on topo events
  * @ioc: per adapter object
  * @event_data: the event data payload
@@ -2361,6 +2814,21 @@ _scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
        u16 expander_handle;
        struct _sas_node *sas_expander;
        unsigned long flags;
+       int i, reason_code;
+       u16 handle;
+
+       for (i = 0 ; i < event_data->NumEntries; i++) {
+               if (event_data->PHY[i].PhyStatus &
+                   MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
+                       continue;
+               handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
+               if (!handle)
+                       continue;
+               reason_code = event_data->PHY[i].PhyStatus &
+                   MPI2_EVENT_SAS_TOPO_RC_MASK;
+               if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
+                       _scsih_tm_tr_send(ioc, handle);
+       }
 
        expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
        if (expander_handle < ioc->sas_hba.num_phys) {
@@ -2419,8 +2887,8 @@ _scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
        u16 smid;
        u16 count = 0;
 
-       for (smid = 1; smid <= ioc->request_depth; smid++) {
-               scmd = _scsih_scsi_lookup_getclear(ioc, smid);
+       for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
+               scmd = _scsih_scsi_lookup_get(ioc, smid);
                if (!scmd)
                        continue;
                count++;
@@ -2461,8 +2929,6 @@ _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
        else
                return;
 
-       mpi_request->EEDPBlockSize = scmd->device->sector_size;
-
        switch (prot_type) {
        case SCSI_PROT_DIF_TYPE1:
 
@@ -2470,8 +2936,7 @@ _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
                * enable ref/guard checking
                * auto increment ref tag
                */
-               mpi_request->EEDPFlags = eedp_flags |
-                   MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
+               eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
                    MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
                    MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
                mpi_request->CDB.EEDP32.PrimaryReferenceTag =
@@ -2484,11 +2949,11 @@ _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
                /*
                * enable guard checking
                */
-               mpi_request->EEDPFlags = eedp_flags |
-                   MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
-
+               eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
                break;
        }
+       mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
+       mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
 }
 
 /**
@@ -2553,7 +3018,6 @@ _scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
        Mpi2SCSIIORequest_t *mpi_request;
        u32 mpi_control;
        u16 smid;
-       unsigned long flags;
 
        scmd->scsi_done = done;
        sas_device_priv_data = scmd->device->hostdata;
@@ -2572,13 +3036,10 @@ _scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
        }
 
        /* see if we are busy with task managment stuff */
-       spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
-       if (sas_target_priv_data->tm_busy ||
-           ioc->shost_recovery || ioc->ioc_link_reset_in_progress) {
-               spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
+       if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
+               return SCSI_MLQUEUE_DEVICE_BUSY;
+       else if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
                return SCSI_MLQUEUE_HOST_BUSY;
-       }
-       spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
 
        if (scmd->sc_data_direction == DMA_FROM_DEVICE)
                mpi_control = MPI2_SCSIIO_CONTROL_READ;
@@ -2602,11 +3063,12 @@ _scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
 
        } else
                mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
-
-       if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON))
+       /* Make sure Device is not raid volume */
+       if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
+           sas_is_tlr_enabled(scmd->device))
                mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
 
-       smid = mpt2sas_base_get_smid(ioc, ioc->scsi_io_cb_idx);
+       smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
        if (!smid) {
                printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
                    ioc->name, __func__);
@@ -2629,11 +3091,12 @@ _scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
        mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
        mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
        mpi_request->SenseBufferLowAddress =
-           (u32)mpt2sas_base_get_sense_buffer_dma(ioc, smid);
+           mpt2sas_base_get_sense_buffer_dma(ioc, smid);
        mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
        mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
            MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
-
+       mpi_request->VF_ID = 0; /* TODO */
+       mpi_request->VP_ID = 0;
        int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
            mpi_request->LUN);
        memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
@@ -2647,8 +3110,7 @@ _scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
                }
        }
 
-       _scsih_scsi_lookup_set(ioc, smid, scmd);
-       mpt2sas_base_put_smid_scsi_io(ioc, smid, 0,
+       mpt2sas_base_put_smid_scsi_io(ioc, smid,
            sas_device_priv_data->sas_target->handle);
        return 0;
 
@@ -2681,7 +3143,7 @@ _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
 
 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
 /**
- * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
+ * _scsih_scsi_ioc_info - translated non-successfull SCSI_IO request
  * @ioc: per adapter object
  * @scmd: pointer to scsi command object
  * @mpi_reply: reply mf payload returned from firmware
@@ -2846,7 +3308,7 @@ _scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
        if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
                response_info = le32_to_cpu(mpi_reply->ResponseInfo);
                response_bytes = (u8 *)&response_info;
-               _scsih_response_code(ioc, response_bytes[3]);
+               _scsih_response_code(ioc, response_bytes[0]);
        }
 }
 #endif
@@ -2944,15 +3406,16 @@ _scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  * _scsih_io_done - scsi request callback
  * @ioc: per adapter object
  * @smid: system request message index
- * @VF_ID: virtual function id
+ * @msix_index: MSIX table index supplied by the OS
  * @reply: reply message frame(lower 32bit addr)
  *
- * Callback handler when using scsih_qcmd.
+ * Callback handler when using _scsih_qcmd.
  *
- * Return nothing.
+ * Return 1 meaning mf should be freed from _base_interrupt
+ *        0 means the mf is freed from this function.
  */
-static void
-_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
+static u8
+_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
 {
        Mpi2SCSIIORequest_t *mpi_request;
        Mpi2SCSIIOReply_t *mpi_reply;
@@ -2963,12 +3426,12 @@ _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
        u8 scsi_status;
        u32 log_info;
        struct MPT2SAS_DEVICE *sas_device_priv_data;
-       u32 response_code;
+       u32 response_code = 0;
 
        mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
-       scmd = _scsih_scsi_lookup_getclear(ioc, smid);
+       scmd = _scsih_scsi_lookup_get(ioc, smid);
        if (scmd == NULL)
-               return;
+               return 1;
 
        mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
 
@@ -2985,15 +3448,17 @@ _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
        }
 
        /* turning off TLR */
+       scsi_state = mpi_reply->SCSIState;
+       if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
+               response_code =
+                   le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
        if (!sas_device_priv_data->tlr_snoop_check) {
                sas_device_priv_data->tlr_snoop_check++;
-               if (sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) {
-                       response_code = (le32_to_cpu(mpi_reply->ResponseInfo)
-                           >> 24);
-                       if (response_code ==
-                           MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
-                               sas_device_priv_data->flags &=
-                                   ~MPT_DEVICE_TLR_ON;
+       if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
+               sas_is_tlr_enabled(scmd->device) &&
+                   response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
+                       sas_disable_tlr(scmd->device);
+                       sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
                }
        }
 
@@ -3005,7 +3470,6 @@ _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
        else
                log_info = 0;
        ioc_status &= MPI2_IOCSTATUS_MASK;
-       scsi_state = mpi_reply->SCSIState;
        scsi_status = mpi_reply->SCSIStatus;
 
        if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
@@ -3041,10 +3505,9 @@ _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
 
        case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
                if (sas_device_priv_data->block) {
-                       scmd->result = (DID_BUS_BUSY << 16);
-                       break;
+                       scmd->result = DID_TRANSPORT_DISRUPTED << 16;
+                       goto out;
                }
-
        case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
        case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
                scmd->result = DID_RESET << 16;
@@ -3090,8 +3553,10 @@ _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
        case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
        case MPI2_IOCSTATUS_SUCCESS:
                scmd->result = (DID_OK << 16) | scsi_status;
-               if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
-                    MPI2_SCSI_STATE_NO_SCSI_STATUS))
+               if (response_code ==
+                   MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
+                   (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
+                    MPI2_SCSI_STATE_NO_SCSI_STATUS)))
                        scmd->result = DID_SOFT_ERROR << 16;
                else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
                        scmd->result = DID_RESET << 16;
@@ -3124,31 +3589,12 @@ _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
  out:
        scsi_dma_unmap(scmd);
        scmd->scsi_done(scmd);
-}
-
-/**
- * _scsih_link_change - process phy link changes
- * @ioc: per adapter object
- * @handle: phy handle
- * @attached_handle: valid for devices attached to link
- * @phy_number: phy number
- * @link_rate: new link rate
- * Context: user.
- *
- * Return nothing.
- */
-static void
-_scsih_link_change(struct MPT2SAS_ADAPTER *ioc, u16 handle, u16 attached_handle,
-   u8 phy_number, u8 link_rate)
-{
-       mpt2sas_transport_update_phy_link_change(ioc, handle, attached_handle,
-           phy_number, link_rate);
+       return 1;
 }
 
 /**
  * _scsih_sas_host_refresh - refreshing sas host object contents
  * @ioc: per adapter object
- * @update: update link information
  * Context: user
  *
  * During port enable, fw will send topology events for every device. Its
@@ -3158,13 +3604,14 @@ _scsih_link_change(struct MPT2SAS_ADAPTER *ioc, u16 handle, u16 attached_handle,
  * Return nothing.
  */
 static void
-_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc, u8 update)
+_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
 {
        u16 sz;
        u16 ioc_status;
        int i;
        Mpi2ConfigReply_t mpi_reply;
        Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
+       u16 attached_handle;
 
        dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
            "updating handles for sas_host(0x%016llx)\n",
@@ -3178,26 +3625,24 @@ _scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc, u8 update)
                    ioc->name, __FILE__, __LINE__, __func__);
                return;
        }
-       if (!(mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
-           sas_iounit_pg0, sz))) {
-               ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
-                   MPI2_IOCSTATUS_MASK;
-               if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
-                       goto out;
-               for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
-                       ioc->sas_hba.phy[i].handle =
-                           le16_to_cpu(sas_iounit_pg0->PhyData[i].
-                               ControllerDevHandle);
-                       if (update)
-                               _scsih_link_change(ioc,
-                                   ioc->sas_hba.phy[i].handle,
-                                   le16_to_cpu(sas_iounit_pg0->PhyData[i].
-                                   AttachedDevHandle), i,
-                                   sas_iounit_pg0->PhyData[i].
-                                   NegotiatedLinkRate >> 4);
-               }
-       }
 
+       if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
+           sas_iounit_pg0, sz)) != 0)
+               goto out;
+       ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
+       if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
+               goto out;
+       for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
+               if (i == 0)
+                       ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
+                           PhyData[0].ControllerDevHandle);
+               ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
+               attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
+                   AttachedDevHandle);
+               mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
+                   attached_handle, i, sas_iounit_pg0->PhyData[i].
+                   NegotiatedLinkRate >> 4);
+       }
  out:
        kfree(sas_iounit_pg0);
 }
@@ -3310,19 +3755,21 @@ _scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
                            ioc->name, __FILE__, __LINE__, __func__);
                        goto out;
                }
-               ioc->sas_hba.phy[i].handle =
-                   le16_to_cpu(sas_iounit_pg0->PhyData[i].ControllerDevHandle);
+
+               if (i == 0)
+                       ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
+                           PhyData[0].ControllerDevHandle);
+               ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
                ioc->sas_hba.phy[i].phy_id = i;
                mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
                    phy_pg0, ioc->sas_hba.parent_dev);
        }
        if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
-           MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.phy[0].handle))) {
+           MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
                    ioc->name, __FILE__, __LINE__, __func__);
                goto out;
        }
-       ioc->sas_hba.handle = le16_to_cpu(sas_device_pg0.DevHandle);
        ioc->sas_hba.enclosure_handle =
            le16_to_cpu(sas_device_pg0.EnclosureHandle);
        ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
@@ -3365,7 +3812,7 @@ _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
        Mpi2SasEnclosurePage0_t enclosure_pg0;
        u32 ioc_status;
        u16 parent_handle;
-       __le64 sas_address;
+       __le64 sas_address, sas_address_parent = 0;
        int i;
        unsigned long flags;
        struct _sas_port *mpt2sas_port = NULL;
@@ -3374,6 +3821,9 @@ _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
        if (!handle)
                return -1;
 
+       if (ioc->shost_recovery)
+               return -1;
+
        if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
            MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
@@ -3391,10 +3841,16 @@ _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
 
        /* handle out of order topology events */
        parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
-       if (parent_handle >= ioc->sas_hba.num_phys) {
+       if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
+           != 0) {
+               printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
+                   ioc->name, __FILE__, __LINE__, __func__);
+               return -1;
+       }
+       if (sas_address_parent != ioc->sas_hba.sas_address) {
                spin_lock_irqsave(&ioc->sas_node_lock, flags);
-               sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
-                   parent_handle);
+               sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
+                   sas_address_parent);
                spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
                if (!sas_expander) {
                        rc = _scsih_expander_add(ioc, parent_handle);
@@ -3403,9 +3859,8 @@ _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
                }
        }
 
-       sas_address = le64_to_cpu(expander_pg0.SASAddress);
-
        spin_lock_irqsave(&ioc->sas_node_lock, flags);
+       sas_address = le64_to_cpu(expander_pg0.SASAddress);
        sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
            sas_address);
        spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
@@ -3423,14 +3878,12 @@ _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
 
        sas_expander->handle = handle;
        sas_expander->num_phys = expander_pg0.NumPhys;
-       sas_expander->parent_handle = parent_handle;
-       sas_expander->enclosure_handle =
-           le16_to_cpu(expander_pg0.EnclosureHandle);
+       sas_expander->sas_address_parent = sas_address_parent;
        sas_expander->sas_address = sas_address;
 
        printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
            " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
-           handle, sas_expander->parent_handle, (unsigned long long)
+           handle, parent_handle, (unsigned long long)
            sas_expander->sas_address, sas_expander->num_phys);
 
        if (!sas_expander->num_phys)
@@ -3446,7 +3899,7 @@ _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
 
        INIT_LIST_HEAD(&sas_expander->sas_port_list);
        mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
-           sas_expander->parent_handle);
+           sas_address_parent);
        if (!mpt2sas_port) {
                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
                    ioc->name, __FILE__, __LINE__, __func__);
@@ -3492,26 +3945,64 @@ _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
 
        if (mpt2sas_port)
                mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
-                   sas_expander->parent_handle);
+                   sas_address_parent);
        kfree(sas_expander);
        return rc;
 }
 
 /**
+ * _scsih_done -  scsih callback handler.
+ * @ioc: per adapter object
+ * @smid: system request message index
+ * @msix_index: MSIX table index supplied by the OS
+ * @reply: reply message frame(lower 32bit addr)
+ *
+ * Callback handler when sending internal generated message frames.
+ * The callback index passed is `ioc->scsih_cb_idx`
+ *
+ * Return 1 meaning mf should be freed from _base_interrupt
+ *        0 means the mf is freed from this function.
+ */
+static u8
+_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
+{
+       MPI2DefaultReply_t *mpi_reply;
+
+       mpi_reply =  mpt2sas_base_get_reply_virt_addr(ioc, reply);
+       if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
+               return 1;
+       if (ioc->scsih_cmds.smid != smid)
+               return 1;
+       ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
+       if (mpi_reply) {
+               memcpy(ioc->scsih_cmds.reply, mpi_reply,
+                   mpi_reply->MsgLength*4);
+               ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
+       }
+       ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
+       complete(&ioc->scsih_cmds.done);
+       return 1;
+}
+
+/**
  * _scsih_expander_remove - removing expander object
  * @ioc: per adapter object
- * @handle: expander handle
+ * @sas_address: expander sas_address
  *
  * Return nothing.
  */
 static void
-_scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u16 handle)
+_scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
 {
        struct _sas_node *sas_expander;
        unsigned long flags;
 
+       if (ioc->shost_recovery)
+               return;
+
        spin_lock_irqsave(&ioc->sas_node_lock, flags);
-       sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc, handle);
+       sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
+           sas_address);
        spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
        _scsih_expander_node_remove(ioc, sas_expander);
 }
@@ -3603,8 +4094,11 @@ _scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
        }
 
        sas_device->handle = handle;
-       sas_device->parent_handle =
-           le16_to_cpu(sas_device_pg0.ParentDevHandle);
+       if (_scsih_get_sas_address(ioc, le16_to_cpu
+               (sas_device_pg0.ParentDevHandle),
+               &sas_device->sas_address_parent) != 0)
+               printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
+                   ioc->name, __FILE__, __LINE__, __func__);
        sas_device->enclosure_handle =
            le16_to_cpu(sas_device_pg0.EnclosureHandle);
        sas_device->slot =
@@ -3634,40 +4128,42 @@ _scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
 /**
  * _scsih_remove_device -  removing sas device object
  * @ioc: per adapter object
- * @handle: sas device handle
+ * @sas_device: the sas_device object
  *
  * Return nothing.
  */
 static void
-_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
+_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, struct _sas_device
+    *sas_device)
 {
        struct MPT2SAS_TARGET *sas_target_priv_data;
-       struct _sas_device *sas_device;
-       unsigned long flags;
        Mpi2SasIoUnitControlReply_t mpi_reply;
        Mpi2SasIoUnitControlRequest_t mpi_request;
-       u16 device_handle;
+       u16 device_handle, handle;
 
-       /* lookup sas_device */
-       spin_lock_irqsave(&ioc->sas_device_lock, flags);
-       sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
-       if (!sas_device) {
-               spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
+       if (!sas_device)
                return;
-       }
 
-       dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle"
-           "(0x%04x)\n", ioc->name, __func__, handle));
+       handle = sas_device->handle;
+       dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle(0x%04x),"
+           " sas_addr(0x%016llx)\n", ioc->name, __func__, handle,
+           (unsigned long long) sas_device->sas_address));
 
        if (sas_device->starget && sas_device->starget->hostdata) {
                sas_target_priv_data = sas_device->starget->hostdata;
                sas_target_priv_data->deleted = 1;
        }
-       spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 
-       if (ioc->remove_host)
+       if (ioc->remove_host || ioc->shost_recovery || !handle)
                goto out;
 
+       if ((sas_device->state & MPTSAS_STATE_TR_COMPLETE)) {
+               dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "\tskip "
+                  "target_reset handle(0x%04x)\n", ioc->name,
+                  handle));
+               goto skip_tr;
+       }
+
        /* Target Reset to flush out all the outstanding IO */
        device_handle = (sas_device->hidden_raid_component) ?
            sas_device->volume_handle : handle;
@@ -3681,6 +4177,15 @@ _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
                mutex_unlock(&ioc->tm_cmds.mutex);
                dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
                    "done: handle(0x%04x)\n", ioc->name, device_handle));
+               if (ioc->shost_recovery)
+                       goto out;
+       }
+ skip_tr:
+
+       if ((sas_device->state & MPTSAS_STATE_CNTRL_COMPLETE)) {
+               dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "\tskip "
+                  "sas_cntrl handle(0x%04x)\n", ioc->name, handle));
+               goto out;
        }
 
        /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
@@ -3690,7 +4195,8 @@ _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
        mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
        mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
        mpi_request.DevHandle = handle;
-       mpi_request.VF_ID = 0;
+       mpi_request.VF_ID = 0; /* TODO */
+       mpi_request.VP_ID = 0;
        if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
            &mpi_request)) != 0) {
                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
@@ -3703,11 +4209,14 @@ _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
            le32_to_cpu(mpi_reply.IOCLogInfo)));
 
  out:
+
+       _scsih_ublock_io_device(ioc, handle);
+
        mpt2sas_transport_port_remove(ioc, sas_device->sas_address,
-           sas_device->parent_handle);
+           sas_device->sas_address_parent);
 
        printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
-           "(0x%016llx)\n", ioc->name, sas_device->handle,
+           "(0x%016llx)\n", ioc->name, handle,
            (unsigned long long) sas_device->sas_address);
        _scsih_sas_device_remove(ioc, sas_device);
 
@@ -3731,7 +4240,7 @@ _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
        u16 reason_code;
        u8 phy_number;
        char *status_str = NULL;
-       char link_rate[25];
+       u8 link_rate, prev_link_rate;
 
        switch (event_data->ExpStatus) {
        case MPI2_EVENT_SAS_TOPO_ES_ADDED:
@@ -3741,6 +4250,7 @@ _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
                status_str = "remove";
                break;
        case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
+       case 0:
                status_str =  "responding";
                break;
        case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
@@ -3766,30 +4276,30 @@ _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
                    MPI2_EVENT_SAS_TOPO_RC_MASK;
                switch (reason_code) {
                case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
-                       snprintf(link_rate, 25, ": add, link(0x%02x)",
-                           (event_data->PHY[i].LinkRate >> 4));
-                       status_str = link_rate;
+                       status_str = "target add";
                        break;
                case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
-                       status_str = ": remove";
+                       status_str = "target remove";
                        break;
                case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
-                       status_str = ": remove_delay";
+                       status_str = "delay target remove";
                        break;
                case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
-                       snprintf(link_rate, 25, ": link(0x%02x)",
-                           (event_data->PHY[i].LinkRate >> 4));
-                       status_str = link_rate;
+                       status_str = "link rate change";
                        break;
                case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
-                       status_str = ": responding";
+                       status_str = "target responding";
                        break;
                default:
-                       status_str = "unknown";
+                       status_str = "unknown";
                        break;
                }
-               printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x)%s\n",
-                   phy_number, handle, status_str);
+               link_rate = event_data->PHY[i].LinkRate >> 4;
+               prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
+               printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x): %s:"
+                   " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
+                   handle, status_str, link_rate, prev_link_rate);
+
        }
 }
 #endif
@@ -3797,15 +4307,12 @@ _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
 /**
  * _scsih_sas_topology_change_event - handle topology changes
  * @ioc: per adapter object
- * @VF_ID:
- * @event_data: event data payload
- * fw_event:
+ * @fw_event: The fw_event_work object
  * Context: user.
  *
  */
 static void
-_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
-    Mpi2EventDataSasTopologyChangeList_t *event_data,
+_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
     struct fw_event_work *fw_event)
 {
        int i;
@@ -3813,18 +4320,24 @@ _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
        u16 reason_code;
        u8 phy_number;
        struct _sas_node *sas_expander;
+       struct _sas_device *sas_device;
+       u64 sas_address;
        unsigned long flags;
-       u8 link_rate_;
+       u8 link_rate, prev_link_rate;
+       Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
 
 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
        if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
                _scsih_sas_topology_change_event_debug(ioc, event_data);
 #endif
 
+       if (ioc->shost_recovery)
+               return;
+
        if (!ioc->sas_hba.num_phys)
                _scsih_sas_host_add(ioc);
        else
-               _scsih_sas_host_refresh(ioc, 0);
+               _scsih_sas_host_refresh(ioc);
 
        if (fw_event->ignore) {
                dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
@@ -3839,6 +4352,17 @@ _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
                if (_scsih_expander_add(ioc, parent_handle) != 0)
                        return;
 
+       spin_lock_irqsave(&ioc->sas_node_lock, flags);
+       sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
+           parent_handle);
+       spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
+       if (sas_expander)
+               sas_address = sas_expander->sas_address;
+       else if (parent_handle < ioc->sas_hba.num_phys)
+               sas_address = ioc->sas_hba.sas_address;
+       else
+               return;
+
        /* handle siblings events */
        for (i = 0; i < event_data->NumEntries; i++) {
                if (fw_event->ignore) {
@@ -3846,59 +4370,59 @@ _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
                            "expander event\n", ioc->name));
                        return;
                }
-               if (event_data->PHY[i].PhyStatus &
-                   MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
+               if (ioc->shost_recovery)
+                       return;
+               phy_number = event_data->StartPhyNum + i;
+               reason_code = event_data->PHY[i].PhyStatus &
+                   MPI2_EVENT_SAS_TOPO_RC_MASK;
+               if ((event_data->PHY[i].PhyStatus &
+                   MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
+                   MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
                        continue;
                handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
                if (!handle)
                        continue;
-               phy_number = event_data->StartPhyNum + i;
-               reason_code = event_data->PHY[i].PhyStatus &
-                   MPI2_EVENT_SAS_TOPO_RC_MASK;
-               link_rate_ = event_data->PHY[i].LinkRate >> 4;
+               link_rate = event_data->PHY[i].LinkRate >> 4;
+               prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
                switch (reason_code) {
                case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
+
+                       if (link_rate == prev_link_rate)
+                               break;
+
+                       mpt2sas_transport_update_links(ioc, sas_address,
+                           handle, phy_number, link_rate);
+
+                       if (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)
+                               _scsih_ublock_io_device(ioc, handle);
+                       break;
                case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
-                       if (!parent_handle) {
-                               if (phy_number < ioc->sas_hba.num_phys)
-                                       _scsih_link_change(ioc,
-                                          ioc->sas_hba.phy[phy_number].handle,
-                                          handle, phy_number, link_rate_);
-                       } else {
-                               spin_lock_irqsave(&ioc->sas_node_lock, flags);
-                               sas_expander =
-                                   mpt2sas_scsih_expander_find_by_handle(ioc,
-                                       parent_handle);
-                               spin_unlock_irqrestore(&ioc->sas_node_lock,
-                                   flags);
-                               if (sas_expander) {
-                                       if (phy_number < sas_expander->num_phys)
-                                               _scsih_link_change(ioc,
-                                                  sas_expander->
-                                                  phy[phy_number].handle,
-                                                  handle, phy_number,
-                                                  link_rate_);
-                               }
-                       }
-                       if (reason_code == MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED) {
-                               if (link_rate_ >= MPI2_SAS_NEG_LINK_RATE_1_5)
-                                       _scsih_ublock_io_device(ioc, handle);
-                       }
-                       if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED) {
-                               if (link_rate_ < MPI2_SAS_NEG_LINK_RATE_1_5)
-                                       break;
-                               _scsih_add_device(ioc, handle, phy_number, 0);
-                       }
+
+                       mpt2sas_transport_update_links(ioc, sas_address,
+                           handle, phy_number, link_rate);
+
+                       _scsih_add_device(ioc, handle, phy_number, 0);
                        break;
                case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
-                       _scsih_remove_device(ioc, handle);
+
+                       spin_lock_irqsave(&ioc->sas_device_lock, flags);
+                       sas_device = _scsih_sas_device_find_by_handle(ioc,
+                           handle);
+                       if (!sas_device) {
+                               spin_unlock_irqrestore(&ioc->sas_device_lock,
+                                   flags);
+                               break;
+                       }
+                       spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
+                       _scsih_remove_device(ioc, sas_device);
                        break;
                }
        }
 
        /* handle expander removal */
-       if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
-               _scsih_expander_remove(ioc, parent_handle);
+       if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
+           sas_expander)
+               _scsih_expander_remove(ioc, sas_address);
 
 }
 
@@ -3950,6 +4474,12 @@ _scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
        case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
                reason_str = "internal async notification";
                break;
+       case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
+               reason_str = "expander reduced functionality";
+               break;
+       case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
+               reason_str = "expander reduced functionality complete";
+               break;
        default:
                reason_str = "unknown reason";
                break;
@@ -3968,20 +4498,52 @@ _scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
 /**
  * _scsih_sas_device_status_change_event - handle device status change
  * @ioc: per adapter object
- * @VF_ID:
- * @event_data: event data payload
+ * @fw_event: The fw_event_work object
  * Context: user.
  *
  * Return nothing.
  */
 static void
-_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
-    Mpi2EventDataSasDeviceStatusChange_t *event_data)
+_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
+    struct fw_event_work *fw_event)
 {
+       struct MPT2SAS_TARGET *target_priv_data;
+       struct _sas_device *sas_device;
+       __le64 sas_address;
+       unsigned long flags;
+       Mpi2EventDataSasDeviceStatusChange_t *event_data =
+           fw_event->event_data;
+
 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
        if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
-               _scsih_sas_device_status_change_event_debug(ioc, event_data);
+               _scsih_sas_device_status_change_event_debug(ioc,
+                    event_data);
 #endif
+
+       if (!(event_data->ReasonCode ==
+           MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
+          event_data->ReasonCode ==
+           MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET))
+               return;
+
+       spin_lock_irqsave(&ioc->sas_device_lock, flags);
+       sas_address = le64_to_cpu(event_data->SASAddress);
+       sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
+           sas_address);
+       spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
+
+       if (!sas_device || !sas_device->starget)
+               return;
+
+       target_priv_data = sas_device->starget->hostdata;
+       if (!target_priv_data)
+               return;
+
+       if (event_data->ReasonCode ==
+           MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
+               target_priv_data->tm_busy = 1;
+       else
+               target_priv_data->tm_busy = 0;
 }
 
 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
@@ -4023,34 +4585,33 @@ _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
 /**
  * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
  * @ioc: per adapter object
- * @VF_ID:
- * @event_data: event data payload
+ * @fw_event: The fw_event_work object
  * Context: user.
  *
  * Return nothing.
  */
 static void
 _scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
-    u8 VF_ID, Mpi2EventDataSasEnclDevStatusChange_t *event_data)
+    struct fw_event_work *fw_event)
 {
 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
        if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
                _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
-                    event_data);
+                    fw_event->event_data);
 #endif
 }
 
 /**
  * _scsih_sas_broadcast_primative_event - handle broadcast events
  * @ioc: per adapter object
- * @event_data: event data payload
+ * @fw_event: The fw_event_work object
  * Context: user.
  *
  * Return nothing.
  */
 static void
-_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
-    Mpi2EventDataSasBroadcastPrimitive_t *event_data)
+_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
+    struct fw_event_work *fw_event)
 {
        struct scsi_cmnd *scmd;
        u16 smid, handle;
@@ -4059,11 +4620,13 @@ _scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
        u32 termination_count;
        u32 query_count;
        Mpi2SCSITaskManagementReply_t *mpi_reply;
-
+#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
+       Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
+#endif
+       u16 ioc_status;
        dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
            "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
            event_data->PortWidth));
-
        dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
            __func__));
 
@@ -4071,7 +4634,7 @@ _scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
        termination_count = 0;
        query_count = 0;
        mpi_reply = ioc->tm_cmds.reply;
-       for (smid = 1; smid <= ioc->request_depth; smid++) {
+       for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
                scmd = _scsih_scsi_lookup_get(ioc, smid);
                if (!scmd)
                        continue;
@@ -4094,8 +4657,9 @@ _scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
                mpt2sas_scsih_issue_tm(ioc, handle, lun,
                    MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
                ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
-
-               if ((mpi_reply->IOCStatus == MPI2_IOCSTATUS_SUCCESS) &&
+               ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
+                   & MPI2_IOCSTATUS_MASK;
+               if ((ioc_status == MPI2_IOCSTATUS_SUCCESS) &&
                    (mpi_reply->ResponseCode ==
                     MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
                     mpi_reply->ResponseCode ==
@@ -4118,23 +4682,25 @@ _scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
 /**
  * _scsih_sas_discovery_event - handle discovery events
  * @ioc: per adapter object
- * @event_data: event data payload
+ * @fw_event: The fw_event_work object
  * Context: user.
  *
  * Return nothing.
  */
 static void
-_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
-    Mpi2EventDataSasDiscovery_t *event_data)
+_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
+    struct fw_event_work *fw_event)
 {
+       Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
+
 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
        if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
                printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
                    (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
                    "start" : "stop");
        if (event_data->DiscoveryStatus)
-               printk(MPT2SAS_DEBUG_FMT ", discovery_status(0x%08x)",
-                   ioc->name, le32_to_cpu(event_data->DiscoveryStatus));
+               printk("discovery_status(0x%08x)",
+                   le32_to_cpu(event_data->DiscoveryStatus));
        printk("\n");
        }
 #endif
@@ -4348,7 +4914,7 @@ _scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
        if (!sas_device)
                return;
-       _scsih_remove_device(ioc, handle);
+       _scsih_remove_device(ioc, sas_device);
 }
 
 /**
@@ -4369,6 +4935,8 @@ _scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
        Mpi2ConfigReply_t mpi_reply;
        Mpi2SasDevicePage0_t sas_device_pg0;
        u32 ioc_status;
+       u64 sas_address;
+       u16 parent_handle;
 
        spin_lock_irqsave(&ioc->sas_device_lock, flags);
        sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
@@ -4393,9 +4961,10 @@ _scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
                return;
        }
 
-       _scsih_link_change(ioc,
-           le16_to_cpu(sas_device_pg0.ParentDevHandle),
-           handle, sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
+       parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
+       if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
+               mpt2sas_transport_update_links(ioc, sas_address, handle,
+                   sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
 
        _scsih_add_device(ioc, handle, 0, 1);
 }
@@ -4485,19 +5054,19 @@ _scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
 /**
  * _scsih_sas_ir_config_change_event - handle ir configuration change events
  * @ioc: per adapter object
- * @VF_ID:
- * @event_data: event data payload
+ * @fw_event: The fw_event_work object
  * Context: user.
  *
  * Return nothing.
  */
 static void
-_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
-    Mpi2EventDataIrConfigChangeList_t *event_data)
+_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
+    struct fw_event_work *fw_event)
 {
        Mpi2EventIrConfigElement_t *element;
        int i;
        u8 foreign_config;
+       Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
 
 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
        if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
@@ -4540,14 +5109,14 @@ _scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
 /**
  * _scsih_sas_ir_volume_event - IR volume event
  * @ioc: per adapter object
- * @event_data: event data payload
+ * @fw_event: The fw_event_work object
  * Context: user.
  *
  * Return nothing.
  */
 static void
-_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
-    Mpi2EventDataIrVolume_t *event_data)
+_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
+    struct fw_event_work *fw_event)
 {
        u64 wwid;
        unsigned long flags;
@@ -4556,6 +5125,7 @@ _scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
        u32 state;
        int rc;
        struct MPT2SAS_TARGET *sas_target_priv_data;
+       Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
 
        if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
                return;
@@ -4625,22 +5195,24 @@ _scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
 /**
  * _scsih_sas_ir_physical_disk_event - PD event
  * @ioc: per adapter object
- * @event_data: event data payload
+ * @fw_event: The fw_event_work object
  * Context: user.
  *
  * Return nothing.
  */
 static void
-_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
-   Mpi2EventDataIrPhysicalDisk_t *event_data)
+_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
+    struct fw_event_work *fw_event)
 {
-       u16 handle;
+       u16 handle, parent_handle;
        u32 state;
        struct _sas_device *sas_device;
        unsigned long flags;
        Mpi2ConfigReply_t mpi_reply;
        Mpi2SasDevicePage0_t sas_device_pg0;
        u32 ioc_status;
+       Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
+       u64 sas_address;
 
        if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
                return;
@@ -4682,9 +5254,10 @@ _scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
                        return;
                }
 
-               _scsih_link_change(ioc,
-                   le16_to_cpu(sas_device_pg0.ParentDevHandle),
-                   handle, sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
+               parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
+               if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
+                       mpt2sas_transport_update_links(ioc, sas_address, handle,
+                           sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
 
                _scsih_add_device(ioc, handle, 0, 1);
 
@@ -4724,11 +5297,17 @@ _scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
        case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
                reason_str = "consistency check";
                break;
-       default:
-               reason_str = "unknown reason";
+       case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
+               reason_str = "background init";
+               break;
+       case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
+               reason_str = "make data consistent";
                break;
        }
 
+       if (!reason_str)
+               return;
+
        printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
            "\thandle(0x%04x), percent complete(%d)\n",
            ioc->name, reason_str,
@@ -4740,33 +5319,55 @@ _scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
 /**
  * _scsih_sas_ir_operation_status_event - handle RAID operation events
  * @ioc: per adapter object
- * @VF_ID:
- * @event_data: event data payload
+ * @fw_event: The fw_event_work object
  * Context: user.
  *
  * Return nothing.
  */
 static void
-_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
-    Mpi2EventDataIrOperationStatus_t *event_data)
+_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
+    struct fw_event_work *fw_event)
 {
+       Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
+       static struct _raid_device *raid_device;
+       unsigned long flags;
+       u16 handle;
+
 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
        if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
-               _scsih_sas_ir_operation_status_event_debug(ioc, event_data);
+               _scsih_sas_ir_operation_status_event_debug(ioc,
+                    event_data);
 #endif
+
+       /* code added for raid transport support */
+       if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
+
+               handle = le16_to_cpu(event_data->VolDevHandle);
+
+               spin_lock_irqsave(&ioc->raid_device_lock, flags);
+               raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
+               spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
+
+               if (!raid_device)
+                       return;
+
+               if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
+                       raid_device->percent_complete =
+                           event_data->PercentComplete;
+       }
 }
 
 /**
  * _scsih_task_set_full - handle task set full
  * @ioc: per adapter object
- * @event_data: event data payload
+ * @fw_event: The fw_event_work object
  * Context: user.
  *
  * Throttle back qdepth.
  */
 static void
-_scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
-    Mpi2EventDataTaskSetFull_t *event_data)
+_scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
+       *fw_event)
 {
        unsigned long flags;
        struct _sas_device *sas_device;
@@ -4777,6 +5378,7 @@ _scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
        u16 handle;
        int id, channel;
        u64 sas_address;
+       Mpi2EventDataTaskSetFull_t *event_data = fw_event->event_data;
 
        current_depth = le16_to_cpu(event_data->CurrentDepth);
        handle = le16_to_cpu(event_data->DevHandle);
@@ -4865,6 +5467,10 @@ _scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
                if (sas_device->sas_address == sas_address &&
                    sas_device->slot == slot && sas_device->starget) {
                        sas_device->responding = 1;
+                       sas_device->state = 0;
+                       starget = sas_device->starget;
+                       sas_target_priv_data = starget->hostdata;
+                       sas_target_priv_data->tm_busy = 0;
                        starget_printk(KERN_INFO, sas_device->starget,
                            "handle(0x%04x), sas_addr(0x%016llx), enclosure "
                            "logical id(0x%016llx), slot(%d)\n", handle,
@@ -4877,8 +5483,6 @@ _scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
                        printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
                            sas_device->handle);
                        sas_device->handle = handle;
-                       starget = sas_device->starget;
-                       sas_target_priv_data = starget->hostdata;
                        sas_target_priv_data->handle = handle;
                        goto out;
                }
@@ -5025,18 +5629,23 @@ _scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
 {
        struct _sas_node *sas_expander;
        unsigned long flags;
+       int i;
 
        spin_lock_irqsave(&ioc->sas_node_lock, flags);
        list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
-               if (sas_expander->sas_address == sas_address) {
-                       sas_expander->responding = 1;
-                       if (sas_expander->handle != handle) {
-                               printk(KERN_INFO "old handle(0x%04x)\n",
-                                   sas_expander->handle);
-                               sas_expander->handle = handle;
-                       }
+               if (sas_expander->sas_address != sas_address)
+                       continue;
+               sas_expander->responding = 1;
+               if (sas_expander->handle == handle)
                        goto out;
-               }
+               printk(KERN_INFO "\texpander(0x%016llx): handle changed"
+                   " from(0x%04x) to (0x%04x)!!!\n",
+                   (unsigned long long)sas_expander->sas_address,
+                   sas_expander->handle, handle);
+               sas_expander->handle = handle;
+               for (i = 0 ; i < sas_expander->num_phys ; i++)
+                       sas_expander->phy[i].handle = handle;
+               goto out;
        }
  out:
        spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
@@ -5113,7 +5722,9 @@ _scsih_remove_unresponding_devices(struct MPT2SAS_ADAPTER *ioc)
                            (unsigned long long)
                            sas_device->enclosure_logical_id,
                            sas_device->slot);
-               _scsih_remove_device(ioc, sas_device->handle);
+               /* invalidate the device handle */
+               sas_device->handle = 0;
+               _scsih_remove_device(ioc, sas_device);
        }
 
        list_for_each_entry_safe(raid_device, raid_device_next,
@@ -5139,7 +5750,7 @@ _scsih_remove_unresponding_devices(struct MPT2SAS_ADAPTER *ioc)
                        sas_expander->responding = 0;
                        continue;
                }
-               _scsih_expander_remove(ioc, sas_expander->handle);
+               _scsih_expander_remove(ioc, sas_expander->sas_address);
                goto retry_expander_search;
        }
 }
@@ -5179,7 +5790,7 @@ mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
        case MPT2_IOC_DONE_RESET:
                dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
                    "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
-               _scsih_sas_host_refresh(ioc, 0);
+               _scsih_sas_host_refresh(ioc);
                _scsih_search_responding_sas_devices(ioc);
                _scsih_search_responding_raid_devices(ioc);
                _scsih_search_responding_expanders(ioc);
@@ -5217,54 +5828,45 @@ _firmware_event_work(struct work_struct *work)
        }
        spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
 
-       spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
        if (ioc->shost_recovery) {
-               spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
                _scsih_fw_event_requeue(ioc, fw_event, 1000);
                return;
        }
-       spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
 
        switch (fw_event->event) {
        case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
-               _scsih_sas_topology_change_event(ioc, fw_event->VF_ID,
-                   fw_event->event_data, fw_event);
+               _scsih_sas_topology_change_event(ioc, fw_event);
                break;
        case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
-               _scsih_sas_device_status_change_event(ioc, fw_event->VF_ID,
-                   fw_event->event_data);
+               _scsih_sas_device_status_change_event(ioc,
+                   fw_event);
                break;
        case MPI2_EVENT_SAS_DISCOVERY:
-               _scsih_sas_discovery_event(ioc, fw_event->VF_ID,
-                   fw_event->event_data);
+               _scsih_sas_discovery_event(ioc,
+                   fw_event);
                break;
        case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
-               _scsih_sas_broadcast_primative_event(ioc, fw_event->VF_ID,
-                   fw_event->event_data);
+               _scsih_sas_broadcast_primative_event(ioc,
+                   fw_event);
                break;
        case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
                _scsih_sas_enclosure_dev_status_change_event(ioc,
-                   fw_event->VF_ID, fw_event->event_data);
+                   fw_event);
                break;
        case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
-               _scsih_sas_ir_config_change_event(ioc, fw_event->VF_ID,
-                   fw_event->event_data);
+               _scsih_sas_ir_config_change_event(ioc, fw_event);
                break;
        case MPI2_EVENT_IR_VOLUME:
-               _scsih_sas_ir_volume_event(ioc, fw_event->VF_ID,
-                   fw_event->event_data);
+               _scsih_sas_ir_volume_event(ioc, fw_event);
                break;
        case MPI2_EVENT_IR_PHYSICAL_DISK:
-               _scsih_sas_ir_physical_disk_event(ioc, fw_event->VF_ID,
-                   fw_event->event_data);
+               _scsih_sas_ir_physical_disk_event(ioc, fw_event);
                break;
        case MPI2_EVENT_IR_OPERATION_STATUS:
-               _scsih_sas_ir_operation_status_event(ioc, fw_event->VF_ID,
-                   fw_event->event_data);
+               _scsih_sas_ir_operation_status_event(ioc, fw_event);
                break;
        case MPI2_EVENT_TASK_SET_FULL:
-               _scsih_task_set_full(ioc, fw_event->VF_ID,
-                   fw_event->event_data);
+               _scsih_task_set_full(ioc, fw_event);
                break;
        }
        _scsih_fw_event_free(ioc, fw_event);
@@ -5273,17 +5875,19 @@ _firmware_event_work(struct work_struct *work)
 /**
  * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
  * @ioc: per adapter object
- * @VF_ID: virtual function id
+ * @msix_index: MSIX table index supplied by the OS
  * @reply: reply message frame(lower 32bit addr)
  * Context: interrupt.
  *
  * This function merely adds a new work task into ioc->firmware_event_thread.
  * The tasks are worked from _firmware_event_work in user context.
  *
- * Return nothing.
+ * Return 1 meaning mf should be freed from _base_interrupt
+ *        0 means the mf is freed from this function.
  */
-void
-mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, u32 reply)
+u8
+mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
+       u32 reply)
 {
        struct fw_event_work *fw_event;
        Mpi2EventNotificationReply_t *mpi_reply;
@@ -5294,11 +5898,11 @@ mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, u32 reply)
        spin_lock_irqsave(&ioc->fw_event_lock, flags);
        if (ioc->fw_events_off || ioc->remove_host) {
                spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
-               return;
+               return 1;
        }
        spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
 
-       mpi_reply =  mpt2sas_base_get_reply_virt_addr(ioc, reply);
+       mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
        event = le16_to_cpu(mpi_reply->Event);
 
        switch (event) {
@@ -5312,7 +5916,7 @@ mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, u32 reply)
                if (baen_data->Primitive !=
                    MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
                    ioc->broadcast_aen_busy)
-                       return;
+                       return 1;
                ioc->broadcast_aen_busy = 1;
                break;
        }
@@ -5334,14 +5938,14 @@ mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, u32 reply)
                break;
 
        default: /* ignore the rest */
-               return;
+               return 1;
        }
 
        fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
        if (!fw_event) {
                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
                    ioc->name, __FILE__, __LINE__, __func__);
-               return;
+               return 1;
        }
        fw_event->event_data =
            kzalloc(mpi_reply->EventDataLength*4, GFP_ATOMIC);
@@ -5349,15 +5953,17 @@ mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, u32 reply)
                printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
                    ioc->name, __FILE__, __LINE__, __func__);
                kfree(fw_event);
-               return;
+               return 1;
        }
 
        memcpy(fw_event->event_data, mpi_reply->EventData,
            mpi_reply->EventDataLength*4);
        fw_event->ioc = ioc;
-       fw_event->VF_ID = VF_ID;
+       fw_event->VF_ID = mpi_reply->VF_ID;
+       fw_event->VP_ID = mpi_reply->VP_ID;
        fw_event->event = event;
        _scsih_fw_event_add(ioc, fw_event);
+       return 1;
 }
 
 /* shost template */
@@ -5424,7 +6030,9 @@ _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
                        spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
                        if (!sas_device)
                                continue;
-                       _scsih_remove_device(ioc, sas_device->handle);
+                       _scsih_remove_device(ioc, sas_device);
+                       if (ioc->shost_recovery)
+                               return;
                        goto retry_device_search;
                }
        }
@@ -5445,13 +6053,16 @@ _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
                        spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
                        if (!expander_sibling)
                                continue;
-                       _scsih_expander_remove(ioc, expander_sibling->handle);
+                       _scsih_expander_remove(ioc,
+                           expander_sibling->sas_address);
+                       if (ioc->shost_recovery)
+                               return;
                        goto retry_expander_search;
                }
        }
 
        mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
-           sas_expander->parent_handle);
+           sas_expander->sas_address_parent);
 
        printk(MPT2SAS_INFO_FMT "expander_remove: handle"
           "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
@@ -5464,9 +6075,99 @@ _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
 }
 
 /**
+ * _scsih_ir_shutdown - IR shutdown notification
+ * @ioc: per adapter object
+ *
+ * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
+ * the host system is shutting down.
+ *
+ * Return nothing.
+ */
+static void
+_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
+{
+       Mpi2RaidActionRequest_t *mpi_request;
+       Mpi2RaidActionReply_t *mpi_reply;
+       u16 smid;
+
+       /* is IR firmware build loaded ? */
+       if (!ioc->ir_firmware)
+               return;
+
+       /* are there any volumes ? */
+       if (list_empty(&ioc->raid_device_list))
+               return;
+
+       mutex_lock(&ioc->scsih_cmds.mutex);
+
+       if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
+               printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
+                   ioc->name, __func__);
+               goto out;
+       }
+       ioc->scsih_cmds.status = MPT2_CMD_PENDING;
+
+       smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
+       if (!smid) {
+               printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
+                   ioc->name, __func__);
+               ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
+               goto out;
+       }
+
+       mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
+       ioc->scsih_cmds.smid = smid;
+       memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
+
+       mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
+       mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
+
+       printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
+       init_completion(&ioc->scsih_cmds.done);
+       mpt2sas_base_put_smid_default(ioc, smid);
+       wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
+
+       if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
+               printk(MPT2SAS_ERR_FMT "%s: timeout\n",
+                   ioc->name, __func__);
+               goto out;
+       }
+
+       if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
+               mpi_reply = ioc->scsih_cmds.reply;
+
+               printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
+                   "ioc_status(0x%04x), loginfo(0x%08x)\n",
+                   ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
+                   le32_to_cpu(mpi_reply->IOCLogInfo));
+       }
+
+ out:
+       ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
+       mutex_unlock(&ioc->scsih_cmds.mutex);
+}
+
+/**
+ * _scsih_shutdown - routine call during system shutdown
+ * @pdev: PCI device struct
+ *
+ * Return nothing.
+ */
+static void
+_scsih_shutdown(struct pci_dev *pdev)
+{
+       struct Scsi_Host *shost = pci_get_drvdata(pdev);
+       struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
+
+       _scsih_ir_shutdown(ioc);
+       mpt2sas_base_detach(ioc);
+}
+
+/**
  * _scsih_remove - detach and remove add host
  * @pdev: PCI device struct
  *
+ * Routine called when unloading the driver.
  * Return nothing.
  */
 static void __devexit
@@ -5477,6 +6178,8 @@ _scsih_remove(struct pci_dev *pdev)
        struct _sas_port *mpt2sas_port;
        struct _sas_device *sas_device;
        struct _sas_node *expander_sibling;
+       struct _raid_device *raid_device, *next;
+       struct MPT2SAS_TARGET *sas_target_priv_data;
        struct workqueue_struct *wq;
        unsigned long flags;
 
@@ -5490,6 +6193,21 @@ _scsih_remove(struct pci_dev *pdev)
        if (wq)
                destroy_workqueue(wq);
 
+       /* release all the volumes */
+       list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
+           list) {
+               if (raid_device->starget) {
+                       sas_target_priv_data =
+                           raid_device->starget->hostdata;
+                       sas_target_priv_data->deleted = 1;
+                       scsi_remove_target(&raid_device->starget->dev);
+               }
+               printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
+                   "(0x%016llx)\n", ioc->name,  raid_device->handle,
+                   (unsigned long long) raid_device->wwid);
+               _scsih_raid_device_remove(ioc, raid_device);
+       }
+
        /* free ports attached to the sas_host */
  retry_again:
        list_for_each_entry(mpt2sas_port,
@@ -5500,7 +6218,7 @@ _scsih_remove(struct pci_dev *pdev)
                            mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
                           mpt2sas_port->remote_identify.sas_address);
                        if (sas_device) {
-                               _scsih_remove_device(ioc, sas_device->handle);
+                               _scsih_remove_device(ioc, sas_device);
                                goto retry_again;
                        }
                } else {
@@ -5509,7 +6227,7 @@ _scsih_remove(struct pci_dev *pdev)
                            mpt2sas_port->remote_identify.sas_address);
                        if (expander_sibling) {
                                _scsih_expander_remove(ioc,
-                                   expander_sibling->handle);
+                                   expander_sibling->sas_address);
                                goto retry_again;
                        }
                }
@@ -5523,7 +6241,7 @@ _scsih_remove(struct pci_dev *pdev)
        }
 
        sas_remove_host(shost);
-       mpt2sas_base_detach(ioc);
+       _scsih_shutdown(pdev);
        list_del(&ioc->list);
        scsi_remove_host(shost);
        scsi_host_put(shost);
@@ -5544,7 +6262,8 @@ _scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
        void *device;
        struct _sas_device *sas_device;
        struct _raid_device *raid_device;
-       u16 handle, parent_handle;
+       u16 handle;
+       u64 sas_address_parent;
        u64 sas_address;
        unsigned long flags;
        int rc;
@@ -5573,17 +6292,17 @@ _scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
        } else {
                sas_device = device;
                handle = sas_device->handle;
-               parent_handle = sas_device->parent_handle;
+               sas_address_parent = sas_device->sas_address_parent;
                sas_address = sas_device->sas_address;
                spin_lock_irqsave(&ioc->sas_device_lock, flags);
                list_move_tail(&sas_device->list, &ioc->sas_device_list);
                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
                if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
-                   sas_device->parent_handle)) {
+                   sas_device->sas_address_parent)) {
                        _scsih_sas_device_remove(ioc, sas_device);
                } else if (!sas_device->starget) {
                        mpt2sas_transport_port_remove(ioc, sas_address,
-                           parent_handle);
+                           sas_address_parent);
                        _scsih_sas_device_remove(ioc, sas_device);
                }
        }
@@ -5613,7 +6332,7 @@ _scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
 }
 
 /**
- * _scsih_probe_sas - reporting raid volumes to sas transport
+ * _scsih_probe_sas - reporting sas devices to sas transport
  * @ioc: per adapter object
  *
  * Called during initial loading of the driver.
@@ -5623,8 +6342,6 @@ _scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
 {
        struct _sas_device *sas_device, *next;
        unsigned long flags;
-       u16 handle, parent_handle;
-       u64 sas_address;
 
        /* SAS Device List */
        list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
@@ -5633,14 +6350,13 @@ _scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
                list_move_tail(&sas_device->list, &ioc->sas_device_list);
                spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 
-               handle = sas_device->handle;
-               parent_handle = sas_device->parent_handle;
-               sas_address = sas_device->sas_address;
-               if (!mpt2sas_transport_port_add(ioc, handle, parent_handle)) {
+               if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
+                   sas_device->sas_address_parent)) {
                        _scsih_sas_device_remove(ioc, sas_device);
                } else if (!sas_device->starget) {
-                       mpt2sas_transport_port_remove(ioc, sas_address,
-                           parent_handle);
+                       mpt2sas_transport_port_remove(ioc,
+                           sas_device->sas_address,
+                           sas_device->sas_address_parent);
                        _scsih_sas_device_remove(ioc, sas_device);
                }
        }
@@ -5709,7 +6425,10 @@ _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
        ioc->ctl_cb_idx = ctl_cb_idx;
        ioc->base_cb_idx = base_cb_idx;
        ioc->transport_cb_idx = transport_cb_idx;
+       ioc->scsih_cb_idx = scsih_cb_idx;
        ioc->config_cb_idx = config_cb_idx;
+       ioc->tm_tr_cb_idx = tm_tr_cb_idx;
+       ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
        ioc->logging_level = logging_level;
        /* misc semaphores and spin locks */
        spin_lock_init(&ioc->ioc_reset_in_progress_lock);
@@ -5725,6 +6444,7 @@ _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
        INIT_LIST_HEAD(&ioc->fw_event_list);
        INIT_LIST_HEAD(&ioc->raid_device_list);
        INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
+       INIT_LIST_HEAD(&ioc->delayed_tr_list);
 
        /* init shost parameters */
        shost->max_cmd_len = 16;
@@ -5741,6 +6461,7 @@ _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
        scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
            | SHOST_DIF_TYPE3_PROTECTION);
+       scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
 
        /* event thread */
        snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
@@ -5842,12 +6563,20 @@ static struct pci_driver scsih_driver = {
        .id_table       = scsih_pci_table,
        .probe          = _scsih_probe,
        .remove         = __devexit_p(_scsih_remove),
+       .shutdown       = _scsih_shutdown,
 #ifdef CONFIG_PM
        .suspend        = _scsih_suspend,
        .resume         = _scsih_resume,
 #endif
 };
 
+/* raid transport support */
+static struct raid_function_template mpt2sas_raid_functions = {
+       .cookie         = &scsih_driver_template,
+       .is_raid        = _scsih_is_raid,
+       .get_resync     = _scsih_get_resync,
+       .get_state      = _scsih_get_state,
+};
 
 /**
  * _scsih_init - main entry point for this driver.
@@ -5867,6 +6596,12 @@ _scsih_init(void)
            sas_attach_transport(&mpt2sas_transport_functions);
        if (!mpt2sas_transport_template)
                return -ENODEV;
+       /* raid transport support */
+       mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
+       if (!mpt2sas_raid_template) {
+               sas_release_transport(mpt2sas_transport_template);
+               return -ENODEV;
+       }
 
        mpt2sas_base_initialize_callback_handler();
 
@@ -5883,6 +6618,9 @@ _scsih_init(void)
        transport_cb_idx = mpt2sas_base_register_callback_handler(
            mpt2sas_transport_done);
 
+       /* scsih internal commands callback handler */
+       scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
+
        /* configuration page API internal commands callback handler */
        config_cb_idx = mpt2sas_base_register_callback_handler(
            mpt2sas_config_done);
@@ -5890,11 +6628,19 @@ _scsih_init(void)
        /* ctl module callback handler */
        ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
 
+       tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
+           _scsih_tm_tr_complete);
+       tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
+           _scsih_sas_control_complete);
+
        mpt2sas_ctl_init();
 
        error = pci_register_driver(&scsih_driver);
-       if (error)
+       if (error) {
+               /* raid transport support */
+               raid_class_release(mpt2sas_raid_template);
                sas_release_transport(mpt2sas_transport_template);
+       }
 
        return error;
 }
@@ -5912,15 +6658,23 @@ _scsih_exit(void)
 
        pci_unregister_driver(&scsih_driver);
 
-       sas_release_transport(mpt2sas_transport_template);
+       mpt2sas_ctl_exit();
+
        mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
        mpt2sas_base_release_callback_handler(tm_cb_idx);
        mpt2sas_base_release_callback_handler(base_cb_idx);
        mpt2sas_base_release_callback_handler(transport_cb_idx);
+       mpt2sas_base_release_callback_handler(scsih_cb_idx);
        mpt2sas_base_release_callback_handler(config_cb_idx);
        mpt2sas_base_release_callback_handler(ctl_cb_idx);
 
-       mpt2sas_ctl_exit();
+       mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
+       mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
+
+       /* raid transport support */
+       raid_class_release(mpt2sas_raid_template);
+       sas_release_transport(mpt2sas_transport_template);
+
 }
 
 module_init(_scsih_init);