Merge branch 'for-linus' of git://git.kernel.dk/linux-block
[linux-flexiantxendom0-3.2.10.git] / drivers / block / mtip32xx / mtip32xx.c
index 847b8ff..304000c 100644 (file)
 #include <linux/smp.h>
 #include <linux/compat.h>
 #include <linux/fs.h>
+#include <linux/module.h>
 #include <linux/genhd.h>
 #include <linux/blkdev.h>
 #include <linux/bio.h>
 #include <linux/dma-mapping.h>
 #include <linux/idr.h>
+#include <linux/kthread.h>
 #include <../drivers/ata/ahci.h>
+#include <linux/export.h>
 #include "mtip32xx.h"
 
 #define HW_CMD_SLOT_SZ         (MTIP_MAX_COMMAND_SLOTS * 32)
@@ -42,6 +45,7 @@
 #define HW_PORT_PRIV_DMA_SZ \
                (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
 
+#define HOST_CAP_NZDMA         (1 << 19)
 #define HOST_HSORG             0xFC
 #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
 #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
@@ -80,11 +84,14 @@ static int instance;
  * Global variable used to hold the major block device number
  * allocated in mtip_init().
  */
-int mtip_major;
+static int mtip_major;
 
 static DEFINE_SPINLOCK(rssd_index_lock);
 static DEFINE_IDA(rssd_index_ida);
 
+static int mtip_block_initialize(struct driver_data *dd);
+
+#ifdef CONFIG_COMPAT
 struct mtip_compat_ide_task_request_s {
        __u8            io_ports[8];
        __u8            hob_ports[8];
@@ -95,15 +102,80 @@ struct mtip_compat_ide_task_request_s {
        compat_ulong_t  out_size;
        compat_ulong_t  in_size;
 };
+#endif
 
-static int mtip_exec_internal_command(struct mtip_port *port,
-                               void *fis,
-                               int fisLen,
-                               dma_addr_t buffer,
-                               int bufLen,
-                               u32 opts,
-                               gfp_t atomic,
-                               unsigned long timeout);
+/*
+ * This function check_for_surprise_removal is called
+ * while card is removed from the system and it will
+ * read the vendor id from the configration space
+ *
+ * @pdev Pointer to the pci_dev structure.
+ *
+ * return value
+ *      true if device removed, else false
+ */
+static bool mtip_check_surprise_removal(struct pci_dev *pdev)
+{
+       u16 vendor_id = 0;
+
+       /* Read the vendorID from the configuration space */
+       pci_read_config_word(pdev, 0x00, &vendor_id);
+       if (vendor_id == 0xFFFF)
+               return true; /* device removed */
+
+       return false; /* device present */
+}
+
+/*
+ * This function is called for clean the pending command in the
+ * command slot during the surprise removal of device and return
+ * error to the upper layer.
+ *
+ * @dd Pointer to the DRIVER_DATA structure.
+ *
+ * return value
+ *     None
+ */
+static void mtip_command_cleanup(struct driver_data *dd)
+{
+       int group = 0, commandslot = 0, commandindex = 0;
+       struct mtip_cmd *command;
+       struct mtip_port *port = dd->port;
+       static int in_progress;
+
+       if (in_progress)
+               return;
+
+       in_progress = 1;
+
+       for (group = 0; group < 4; group++) {
+               for (commandslot = 0; commandslot < 32; commandslot++) {
+                       if (!(port->allocated[group] & (1 << commandslot)))
+                               continue;
+
+                       commandindex = group << 5 | commandslot;
+                       command = &port->commands[commandindex];
+
+                       if (atomic_read(&command->active)
+                           && (command->async_callback)) {
+                               command->async_callback(command->async_data,
+                                       -ENODEV);
+                               command->async_callback = NULL;
+                               command->async_data = NULL;
+                       }
+
+                       dma_unmap_sg(&port->dd->pdev->dev,
+                               command->sg,
+                               command->scatter_ents,
+                               command->direction);
+               }
+       }
+
+       up(&port->cmd_slot);
+
+       set_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag);
+       in_progress = 0;
+}
 
 /*
  * Obtain an empty command slot.
@@ -165,214 +237,79 @@ static inline void release_slot(struct mtip_port *port, int tag)
 }
 
 /*
- * Issue a command to the hardware.
- *
- * Set the appropriate bit in the s_active and Command Issue hardware
- * registers, causing hardware command processing to begin.
- *
- * @port Pointer to the port structure.
- * @tag  The tag of the command to be issued.
- *
- * return value
- *      None
- */
-static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
-{
-       unsigned long flags = 0;
-
-       atomic_set(&port->commands[tag].active, 1);
-
-       spin_lock_irqsave(&port->cmd_issue_lock, flags);
-
-       writel((1 << MTIP_TAG_BIT(tag)),
-                       port->s_active[MTIP_TAG_INDEX(tag)]);
-       writel((1 << MTIP_TAG_BIT(tag)),
-                       port->cmd_issue[MTIP_TAG_INDEX(tag)]);
-
-       spin_unlock_irqrestore(&port->cmd_issue_lock, flags);
-}
-
-/*
- * Called periodically to see if any read/write commands are
- * taking too long to complete.
- *
- * @data Pointer to the PORT data structure.
- *
- * return value
- *     None
- */
-void mtip_timeout_function(unsigned long int data)
-{
-       struct mtip_port *port = (struct mtip_port *) data;
-       struct host_to_dev_fis *fis;
-       struct mtip_cmd *command;
-       int tag, cmdto_cnt = 0;
-       unsigned int bit, group;
-       unsigned int num_command_slots = port->dd->slot_groups * 32;
-
-       if (unlikely(!port))
-               return;
-
-       if (atomic_read(&port->dd->resumeflag) == true) {
-               mod_timer(&port->cmd_timer,
-                       jiffies + msecs_to_jiffies(30000));
-               return;
-       }
-
-       for (tag = 0; tag < num_command_slots; tag++) {
-               /*
-                * Skip internal command slot as it has
-                * its own timeout mechanism
-                */
-               if (tag == MTIP_TAG_INTERNAL)
-                       continue;
-
-               if (atomic_read(&port->commands[tag].active) &&
-                  (time_after(jiffies, port->commands[tag].comp_time))) {
-                       group = tag >> 5;
-                       bit = tag & 0x1f;
-
-                       command = &port->commands[tag];
-                       fis = (struct host_to_dev_fis *) command->command;
-
-                       dev_warn(&port->dd->pdev->dev,
-                               "Timeout for command tag %d\n", tag);
-
-                       cmdto_cnt++;
-                       if (cmdto_cnt == 1)
-                               atomic_inc(&port->dd->eh_active);
-
-                       /*
-                        * Clear the completed bit. This should prevent
-                        *  any interrupt handlers from trying to retire
-                        *  the command.
-                        */
-                       writel(1 << bit, port->completed[group]);
-
-                       /* Call the async completion callback. */
-                       if (likely(command->async_callback))
-                               command->async_callback(command->async_data,
-                                                        -EIO);
-                       command->async_callback = NULL;
-                       command->comp_func = NULL;
-
-                       /* Unmap the DMA scatter list entries */
-                       dma_unmap_sg(&port->dd->pdev->dev,
-                                       command->sg,
-                                       command->scatter_ents,
-                                       command->direction);
-
-                       /*
-                        * Clear the allocated bit and active tag for the
-                        * command.
-                        */
-                       atomic_set(&port->commands[tag].active, 0);
-                       release_slot(port, tag);
-
-                       up(&port->cmd_slot);
-               }
-       }
-
-       if (cmdto_cnt) {
-               dev_warn(&port->dd->pdev->dev,
-                       "%d commands timed out: restarting port",
-                       cmdto_cnt);
-               mtip_restart_port(port);
-               atomic_dec(&port->dd->eh_active);
-       }
-
-       /* Restart the timer */
-       mod_timer(&port->cmd_timer,
-               jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
-}
-
-/*
- * IO completion function.
+ * Reset the HBA (without sleeping)
  *
- * This completion function is called by the driver ISR when a
- * command that was issued by the kernel completes. It first calls the
- * asynchronous completion function which normally calls back into the block
- * layer passing the asynchronous callback data, then unmaps the
- * scatter list associated with the completed command, and finally
- * clears the allocated bit associated with the completed command.
+ * Just like hba_reset, except does not call sleep, so can be
+ * run from interrupt/tasklet context.
  *
- * @port   Pointer to the port data structure.
- * @tag    Tag of the command.
- * @data   Pointer to driver_data.
- * @status Completion status.
+ * @dd Pointer to the driver data structure.
  *
  * return value
- *     None
+ *     0       The reset was successful.
+ *     -1      The HBA Reset bit did not clear.
  */
-static void mtip_async_complete(struct mtip_port *port,
-                               int tag,
-                               void *data,
-                               int status)
+static int hba_reset_nosleep(struct driver_data *dd)
 {
-       struct mtip_cmd *command;
-       struct driver_data *dd = data;
-       int cb_status = status ? -EIO : 0;
-
-       if (unlikely(!dd) || unlikely(!port))
-               return;
+       unsigned long timeout;
 
-       command = &port->commands[tag];
+       /* Chip quirk: quiesce any chip function */
+       mdelay(10);
 
-       if (unlikely(status == PORT_IRQ_TF_ERR)) {
-               dev_warn(&port->dd->pdev->dev,
-                       "Command tag %d failed due to TFE\n", tag);
-       }
+       /* Set the reset bit */
+       writel(HOST_RESET, dd->mmio + HOST_CTL);
 
-       /* Upper layer callback */
-       if (likely(command->async_callback))
-               command->async_callback(command->async_data, cb_status);
+       /* Flush */
+       readl(dd->mmio + HOST_CTL);
 
-       command->async_callback = NULL;
-       command->comp_func = NULL;
+       /*
+        * Wait 10ms then spin for up to 1 second
+        * waiting for reset acknowledgement
+        */
+       timeout = jiffies + msecs_to_jiffies(1000);
+       mdelay(10);
+       while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
+                && time_before(jiffies, timeout))
+               mdelay(1);
 
-       /* Unmap the DMA scatter list entries */
-       dma_unmap_sg(&dd->pdev->dev,
-               command->sg,
-               command->scatter_ents,
-               command->direction);
+       if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
+               return -1;
 
-       /* Clear the allocated and active bits for the command */
-       atomic_set(&port->commands[tag].active, 0);
-       release_slot(port, tag);
+       if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
+               return -1;
 
-       up(&port->cmd_slot);
+       return 0;
 }
 
 /*
- * Internal command completion callback function.
+ * Issue a command to the hardware.
  *
- * This function is normally called by the driver ISR when an internal
- * command completed. This function signals the command completion by
- * calling complete().
+ * Set the appropriate bit in the s_active and Command Issue hardware
+ * registers, causing hardware command processing to begin.
  *
- * @port   Pointer to the port data structure.
- * @tag    Tag of the command that has completed.
- * @data   Pointer to a completion structure.
- * @status Completion status.
+ * @port Pointer to the port structure.
+ * @tag  The tag of the command to be issued.
  *
  * return value
- *     None
- */
-static void mtip_completion(struct mtip_port *port,
-                           int tag,
-                           void *data,
-                           int status)
-{
-       struct mtip_cmd *command = &port->commands[tag];
-       struct completion *waiting = data;
-       if (unlikely(status == PORT_IRQ_TF_ERR))
-               dev_warn(&port->dd->pdev->dev,
-                       "Internal command %d completed with TFE\n", tag);
+ *      None
+ */
+static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
+{
+       unsigned long flags = 0;
 
-       command->async_callback = NULL;
-       command->comp_func = NULL;
+       atomic_set(&port->commands[tag].active, 1);
 
-       complete(waiting);
+       spin_lock_irqsave(&port->cmd_issue_lock, flags);
+
+       writel((1 << MTIP_TAG_BIT(tag)),
+                       port->s_active[MTIP_TAG_INDEX(tag)]);
+       writel((1 << MTIP_TAG_BIT(tag)),
+                       port->cmd_issue[MTIP_TAG_INDEX(tag)]);
+
+       spin_unlock_irqrestore(&port->cmd_issue_lock, flags);
+
+       /* Set the command's timeout value.*/
+       port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
+                                       MTIP_NCQ_COMMAND_TIMEOUT_MS);
 }
 
 /*
@@ -487,9 +424,9 @@ static void mtip_init_port(struct mtip_port *port)
                         port->mmio + PORT_FIS_ADDR_HI);
        }
 
-       writel(port->command_list_dma & 0xffffffff,
+       writel(port->command_list_dma & 0xFFFFFFFF,
                        port->mmio + PORT_LST_ADDR);
-       writel(port->rxfis_dma & 0xffffffff, port->mmio + PORT_FIS_ADDR);
+       writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
 
        /* Clear SError */
        writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
@@ -499,54 +436,18 @@ static void mtip_init_port(struct mtip_port *port)
                writel(0xFFFFFFFF, port->completed[i]);
 
        /* Clear any pending interrupts for this port */
-       writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
+       writel(readl(port->dd->mmio + PORT_IRQ_STAT),
+                                       port->dd->mmio + PORT_IRQ_STAT);
+
+       /* Clear any pending interrupts on the HBA. */
+       writel(readl(port->dd->mmio + HOST_IRQ_STAT),
+                                       port->dd->mmio + HOST_IRQ_STAT);
 
        /* Enable port interrupts */
        writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
 }
 
 /*
- * Reset the HBA (without sleeping)
- *
- * Just like hba_reset, except does not call sleep, so can be
- * run from interrupt/tasklet context.
- *
- * @dd Pointer to the driver data structure.
- *
- * return value
- *     0       The reset was successful.
- *     -1      The HBA Reset bit did not clear.
- */
-int hba_reset_nosleep(struct driver_data *dd)
-{
-       unsigned long timeout;
-
-       /* Chip quirk: quiesce any chip function */
-       mdelay(10);
-
-       /* Set the reset bit */
-       writel(HOST_RESET, dd->mmio + HOST_CTL);
-
-       /* Flush */
-       readl(dd->mmio + HOST_CTL);
-
-       /*
-        * Wait 10ms then spin for up to 1 second
-        * waiting for reset acknowledgement
-        */
-       timeout = jiffies + msecs_to_jiffies(1000);
-       mdelay(10);
-       while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
-                && time_before(jiffies, timeout))
-               mdelay(1);
-
-       if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
-               return -1;
-
-       return 0;
-}
-
-/*
  * Restart a port
  *
  * @port Pointer to the port data structure.
@@ -554,7 +455,7 @@ int hba_reset_nosleep(struct driver_data *dd)
  * return value
  *     None
  */
-void mtip_restart_port(struct mtip_port *port)
+static void mtip_restart_port(struct mtip_port *port)
 {
        unsigned long timeout;
 
@@ -567,6 +468,9 @@ void mtip_restart_port(struct mtip_port *port)
                 && time_before(jiffies, timeout))
                ;
 
+       if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
+               return;
+
        /*
         * Chip quirk: escalate to hba reset if
         * PxCMD.CR not clear after 500 ms
@@ -595,6 +499,9 @@ void mtip_restart_port(struct mtip_port *port)
        while (time_before(jiffies, timeout))
                ;
 
+       if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
+               return;
+
        /* Clear PxSCTL.DET */
        writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
                         port->mmio + PORT_SCR_CTL);
@@ -606,34 +513,248 @@ void mtip_restart_port(struct mtip_port *port)
                         && time_before(jiffies, timeout))
                ;
 
+       if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
+               return;
+
        if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
                dev_warn(&port->dd->pdev->dev,
                        "COM reset failed\n");
 
-       /* Clear SError, the PxSERR.DIAG.x should be set so clear it */
-       writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
+       mtip_init_port(port);
+       mtip_start_port(port);
+
+}
+
+/*
+ * Helper function for tag logging
+ */
+static void print_tags(struct driver_data *dd,
+                       char *msg,
+                       unsigned long *tagbits,
+                       int cnt)
+{
+       unsigned char tagmap[128];
+       int group, tagmap_len = 0;
+
+       memset(tagmap, 0, sizeof(tagmap));
+       for (group = SLOTBITS_IN_LONGS; group > 0; group--)
+               tagmap_len = sprintf(tagmap + tagmap_len, "%016lX ",
+                                               tagbits[group-1]);
+       dev_warn(&dd->pdev->dev,
+                       "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
+}
+
+/*
+ * Called periodically to see if any read/write commands are
+ * taking too long to complete.
+ *
+ * @data Pointer to the PORT data structure.
+ *
+ * return value
+ *     None
+ */
+static void mtip_timeout_function(unsigned long int data)
+{
+       struct mtip_port *port = (struct mtip_port *) data;
+       struct host_to_dev_fis *fis;
+       struct mtip_cmd *command;
+       int tag, cmdto_cnt = 0;
+       unsigned int bit, group;
+       unsigned int num_command_slots = port->dd->slot_groups * 32;
+       unsigned long to, tagaccum[SLOTBITS_IN_LONGS];
+
+       if (unlikely(!port))
+               return;
+
+       if (test_bit(MTIP_DDF_RESUME_BIT, &port->dd->dd_flag)) {
+               mod_timer(&port->cmd_timer,
+                       jiffies + msecs_to_jiffies(30000));
+               return;
+       }
+       /* clear the tag accumulator */
+       memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
+
+       for (tag = 0; tag < num_command_slots; tag++) {
+               /*
+                * Skip internal command slot as it has
+                * its own timeout mechanism
+                */
+               if (tag == MTIP_TAG_INTERNAL)
+                       continue;
+
+               if (atomic_read(&port->commands[tag].active) &&
+                  (time_after(jiffies, port->commands[tag].comp_time))) {
+                       group = tag >> 5;
+                       bit = tag & 0x1F;
+
+                       command = &port->commands[tag];
+                       fis = (struct host_to_dev_fis *) command->command;
+
+                       set_bit(tag, tagaccum);
+                       cmdto_cnt++;
+                       if (cmdto_cnt == 1)
+                               set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
+
+                       /*
+                        * Clear the completed bit. This should prevent
+                        *  any interrupt handlers from trying to retire
+                        *  the command.
+                        */
+                       writel(1 << bit, port->completed[group]);
+
+                       /* Call the async completion callback. */
+                       if (likely(command->async_callback))
+                               command->async_callback(command->async_data,
+                                                        -EIO);
+                       command->async_callback = NULL;
+                       command->comp_func = NULL;
+
+                       /* Unmap the DMA scatter list entries */
+                       dma_unmap_sg(&port->dd->pdev->dev,
+                                       command->sg,
+                                       command->scatter_ents,
+                                       command->direction);
+
+                       /*
+                        * Clear the allocated bit and active tag for the
+                        * command.
+                        */
+                       atomic_set(&port->commands[tag].active, 0);
+                       release_slot(port, tag);
+
+                       up(&port->cmd_slot);
+               }
+       }
+
+       if (cmdto_cnt && !test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
+               print_tags(port->dd, "timed out", tagaccum, cmdto_cnt);
+
+               mtip_restart_port(port);
+               clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
+               wake_up_interruptible(&port->svc_wait);
+       }
+
+       if (port->ic_pause_timer) {
+               to  = port->ic_pause_timer + msecs_to_jiffies(1000);
+               if (time_after(jiffies, to)) {
+                       if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
+                               port->ic_pause_timer = 0;
+                               clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
+                               clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
+                               clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
+                               wake_up_interruptible(&port->svc_wait);
+                       }
+
+
+               }
+       }
+
+       /* Restart the timer */
+       mod_timer(&port->cmd_timer,
+               jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
+}
+
+/*
+ * IO completion function.
+ *
+ * This completion function is called by the driver ISR when a
+ * command that was issued by the kernel completes. It first calls the
+ * asynchronous completion function which normally calls back into the block
+ * layer passing the asynchronous callback data, then unmaps the
+ * scatter list associated with the completed command, and finally
+ * clears the allocated bit associated with the completed command.
+ *
+ * @port   Pointer to the port data structure.
+ * @tag    Tag of the command.
+ * @data   Pointer to driver_data.
+ * @status Completion status.
+ *
+ * return value
+ *     None
+ */
+static void mtip_async_complete(struct mtip_port *port,
+                               int tag,
+                               void *data,
+                               int status)
+{
+       struct mtip_cmd *command;
+       struct driver_data *dd = data;
+       int cb_status = status ? -EIO : 0;
+
+       if (unlikely(!dd) || unlikely(!port))
+               return;
+
+       command = &port->commands[tag];
+
+       if (unlikely(status == PORT_IRQ_TF_ERR)) {
+               dev_warn(&port->dd->pdev->dev,
+                       "Command tag %d failed due to TFE\n", tag);
+       }
+
+       /* Upper layer callback */
+       if (likely(command->async_callback))
+               command->async_callback(command->async_data, cb_status);
+
+       command->async_callback = NULL;
+       command->comp_func = NULL;
+
+       /* Unmap the DMA scatter list entries */
+       dma_unmap_sg(&dd->pdev->dev,
+               command->sg,
+               command->scatter_ents,
+               command->direction);
+
+       /* Clear the allocated and active bits for the command */
+       atomic_set(&port->commands[tag].active, 0);
+       release_slot(port, tag);
+
+       up(&port->cmd_slot);
+}
+
+/*
+ * Internal command completion callback function.
+ *
+ * This function is normally called by the driver ISR when an internal
+ * command completed. This function signals the command completion by
+ * calling complete().
+ *
+ * @port   Pointer to the port data structure.
+ * @tag    Tag of the command that has completed.
+ * @data   Pointer to a completion structure.
+ * @status Completion status.
+ *
+ * return value
+ *     None
+ */
+static void mtip_completion(struct mtip_port *port,
+                           int tag,
+                           void *data,
+                           int status)
+{
+       struct mtip_cmd *command = &port->commands[tag];
+       struct completion *waiting = data;
+       if (unlikely(status == PORT_IRQ_TF_ERR))
+               dev_warn(&port->dd->pdev->dev,
+                       "Internal command %d completed with TFE\n", tag);
+
+       command->async_callback = NULL;
+       command->comp_func = NULL;
 
-       /* Enable the DMA engine */
-       mtip_enable_engine(port, 1);
+       complete(waiting);
 }
 
-/*
- * Helper function for tag logging
- */
-static void print_tags(struct driver_data *dd,
-                       char *msg,
-                       unsigned long *tagbits)
+static void mtip_null_completion(struct mtip_port *port,
+                           int tag,
+                           void *data,
+                           int status)
 {
-       unsigned int tag, count = 0;
-
-       for (tag = 0; tag < (dd->slot_groups) * 32; tag++) {
-               if (test_bit(tag, tagbits))
-                       count++;
-       }
-       if (count)
-               dev_info(&dd->pdev->dev, "%s [%i tags]\n", msg, count);
+       return;
 }
 
+static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
+                               dma_addr_t buffer_dma, unsigned int sectors);
+static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
+                                               struct smart_attr *attrib);
 /*
  * Handle an error.
  *
@@ -644,12 +765,16 @@ static void print_tags(struct driver_data *dd,
  */
 static void mtip_handle_tfe(struct driver_data *dd)
 {
-       int group, tag, bit, reissue;
+       int group, tag, bit, reissue, rv;
        struct mtip_port *port;
-       struct mtip_cmd  *command;
+       struct mtip_cmd  *cmd;
        u32 completed;
        struct host_to_dev_fis *fis;
        unsigned long tagaccum[SLOTBITS_IN_LONGS];
+       unsigned int cmd_cnt = 0;
+       unsigned char *buf;
+       char *fail_reason = NULL;
+       int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
 
        dev_warn(&dd->pdev->dev, "Taskfile error\n");
 
@@ -658,8 +783,11 @@ static void mtip_handle_tfe(struct driver_data *dd)
        /* Stop the timer to prevent command timeouts. */
        del_timer(&port->cmd_timer);
 
+       /* clear the tag accumulator */
+       memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
+
        /* Set eh_active */
-       atomic_inc(&dd->eh_active);
+       set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
 
        /* Loop through all the groups */
        for (group = 0; group < dd->slot_groups; group++) {
@@ -668,9 +796,6 @@ static void mtip_handle_tfe(struct driver_data *dd)
                /* clear completed status register in the hardware.*/
                writel(completed, port->completed[group]);
 
-               /* clear the tag accumulator */
-               memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
-
                /* Process successfully completed commands */
                for (bit = 0; bit < 32 && completed; bit++) {
                        if (!(completed & (1<<bit)))
@@ -681,13 +806,14 @@ static void mtip_handle_tfe(struct driver_data *dd)
                        if (tag == MTIP_TAG_INTERNAL)
                                continue;
 
-                       command = &port->commands[tag];
-                       if (likely(command->comp_func)) {
+                       cmd = &port->commands[tag];
+                       if (likely(cmd->comp_func)) {
                                set_bit(tag, tagaccum);
-                               atomic_set(&port->commands[tag].active, 0);
-                               command->comp_func(port,
+                               cmd_cnt++;
+                               atomic_set(&cmd->active, 0);
+                               cmd->comp_func(port,
                                         tag,
-                                        command->comp_data,
+                                        cmd->comp_data,
                                         0);
                        } else {
                                dev_err(&port->dd->pdev->dev,
@@ -701,12 +827,45 @@ static void mtip_handle_tfe(struct driver_data *dd)
                        }
                }
        }
-       print_tags(dd, "TFE tags completed:", tagaccum);
+
+       print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
 
        /* Restart the port */
        mdelay(20);
        mtip_restart_port(port);
 
+       /* Trying to determine the cause of the error */
+       rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
+                               dd->port->log_buf,
+                               dd->port->log_buf_dma, 1);
+       if (rv) {
+               dev_warn(&dd->pdev->dev,
+                       "Error in READ LOG EXT (10h) command\n");
+               /* non-critical error, don't fail the load */
+       } else {
+               buf = (unsigned char *)dd->port->log_buf;
+               if (buf[259] & 0x1) {
+                       dev_info(&dd->pdev->dev,
+                               "Write protect bit is set.\n");
+                       set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
+                       fail_all_ncq_write = 1;
+                       fail_reason = "write protect";
+               }
+               if (buf[288] == 0xF7) {
+                       dev_info(&dd->pdev->dev,
+                               "Exceeded Tmax, drive in thermal shutdown.\n");
+                       set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
+                       fail_all_ncq_cmds = 1;
+                       fail_reason = "thermal shutdown";
+               }
+               if (buf[288] == 0xBF) {
+                       dev_info(&dd->pdev->dev,
+                               "Drive indicates rebuild has failed.\n");
+                       fail_all_ncq_cmds = 1;
+                       fail_reason = "rebuild failed";
+               }
+       }
+
        /* clear the tag accumulator */
        memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
 
@@ -715,32 +874,47 @@ static void mtip_handle_tfe(struct driver_data *dd)
                for (bit = 0; bit < 32; bit++) {
                        reissue = 1;
                        tag = (group << 5) + bit;
+                       cmd = &port->commands[tag];
 
                        /* If the active bit is set re-issue the command */
-                       if (atomic_read(&port->commands[tag].active) == 0)
+                       if (atomic_read(&cmd->active) == 0)
                                continue;
 
-                       fis = (struct host_to_dev_fis *)
-                               port->commands[tag].command;
+                       fis = (struct host_to_dev_fis *)cmd->command;
 
                        /* Should re-issue? */
                        if (tag == MTIP_TAG_INTERNAL ||
                            fis->command == ATA_CMD_SET_FEATURES)
                                reissue = 0;
+                       else {
+                               if (fail_all_ncq_cmds ||
+                                       (fail_all_ncq_write &&
+                                       fis->command == ATA_CMD_FPDMA_WRITE)) {
+                                       dev_warn(&dd->pdev->dev,
+                                       "  Fail: %s w/tag %d [%s].\n",
+                                       fis->command == ATA_CMD_FPDMA_WRITE ?
+                                               "write" : "read",
+                                       tag,
+                                       fail_reason != NULL ?
+                                               fail_reason : "unknown");
+                                       atomic_set(&cmd->active, 0);
+                                       if (cmd->comp_func) {
+                                               cmd->comp_func(port, tag,
+                                                       cmd->comp_data,
+                                                       -ENODATA);
+                                       }
+                                       continue;
+                               }
+                       }
 
                        /*
                         * First check if this command has
                         *  exceeded its retries.
                         */
-                       if (reissue &&
-                           (port->commands[tag].retries-- > 0)) {
+                       if (reissue && (cmd->retries-- > 0)) {
 
                                set_bit(tag, tagaccum);
 
-                               /* Update the timeout value. */
-                               port->commands[tag].comp_time =
-                                       jiffies + msecs_to_jiffies(
-                                       MTIP_NCQ_COMMAND_TIMEOUT_MS);
                                /* Re-issue the command. */
                                mtip_issue_ncq_command(port, tag);
 
@@ -750,13 +924,13 @@ static void mtip_handle_tfe(struct driver_data *dd)
                        /* Retire a command that will not be reissued */
                        dev_warn(&port->dd->pdev->dev,
                                "retiring tag %d\n", tag);
-                       atomic_set(&port->commands[tag].active, 0);
+                       atomic_set(&cmd->active, 0);
 
-                       if (port->commands[tag].comp_func)
-                               port->commands[tag].comp_func(
+                       if (cmd->comp_func)
+                               cmd->comp_func(
                                        port,
                                        tag,
-                                       port->commands[tag].comp_data,
+                                       cmd->comp_data,
                                        PORT_IRQ_TF_ERR);
                        else
                                dev_warn(&port->dd->pdev->dev,
@@ -764,10 +938,11 @@ static void mtip_handle_tfe(struct driver_data *dd)
                                        tag);
                }
        }
-       print_tags(dd, "TFE tags reissued:", tagaccum);
+       print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
 
-       /* Decrement eh_active */
-       atomic_dec(&dd->eh_active);
+       /* clear eh_active */
+       clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
+       wake_up_interruptible(&port->svc_wait);
 
        mod_timer(&port->cmd_timer,
                 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
@@ -802,7 +977,6 @@ static inline void mtip_process_sdbf(struct driver_data *dd)
                                        continue;
 
                                command = &port->commands[tag];
-
                                /* make internal callback */
                                if (likely(command->comp_func)) {
                                        command->comp_func(
@@ -835,9 +1009,8 @@ static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
        struct mtip_port *port = dd->port;
        struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
 
-       if (port->internal_cmd_in_progress &&
-           cmd != NULL &&
-           !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
+       if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
+           (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
                & (1 << MTIP_TAG_INTERNAL))) {
                if (cmd->comp_func) {
                        cmd->comp_func(port,
@@ -848,8 +1021,6 @@ static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
                }
        }
 
-       dev_warn(&dd->pdev->dev, "IRQ status 0x%x ignored.\n", port_stat);
-
        return;
 }
 
@@ -905,6 +1076,9 @@ static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
                                /* don't proceed further */
                                return IRQ_HANDLED;
                        }
+                       if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
+                                                       &dd->dd_flag))
+                               return rv;
 
                        mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
                }
@@ -952,6 +1126,39 @@ static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
                port->cmd_issue[MTIP_TAG_INDEX(tag)]);
 }
 
+static bool mtip_pause_ncq(struct mtip_port *port,
+                               struct host_to_dev_fis *fis)
+{
+       struct host_to_dev_fis *reply;
+       unsigned long task_file_data;
+
+       reply = port->rxfis + RX_FIS_D2H_REG;
+       task_file_data = readl(port->mmio+PORT_TFDATA);
+
+       if ((task_file_data & 1) || (fis->command == ATA_CMD_SEC_ERASE_UNIT))
+               return false;
+
+       if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
+               set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
+               port->ic_pause_timer = jiffies;
+               return true;
+       } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
+                                       (fis->features == 0x03)) {
+               set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
+               port->ic_pause_timer = jiffies;
+               return true;
+       } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
+               ((fis->command == 0xFC) &&
+                       (fis->features == 0x27 || fis->features == 0x72 ||
+                        fis->features == 0x62 || fis->features == 0x26))) {
+               /* Com reset after secure erase or lowlevel format */
+               mtip_restart_port(port);
+               return false;
+       }
+
+       return false;
+}
+
 /*
  * Wait for port to quiesce
  *
@@ -965,15 +1172,23 @@ static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
 static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
 {
        unsigned long to;
-       unsigned int n, active;
+       unsigned int n;
+       unsigned int active = 1;
 
        to = jiffies + msecs_to_jiffies(timeout);
        do {
+               if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
+                       test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
+                       msleep(20);
+                       continue; /* svc thd is actively issuing commands */
+               }
+               if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
+                       return -EFAULT;
                /*
                 * Ignore s_active bit 0 of array element 0.
                 * This bit will always be set
                 */
-               active = readl(port->s_active[0]) & 0xfffffffe;
+               active = readl(port->s_active[0]) & 0xFFFFFFFE;
                for (n = 1; n < port->dd->slot_groups; n++)
                        active |= readl(port->s_active[n]);
 
@@ -991,9 +1206,9 @@ static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
  *
  * @port    Pointer to the port data structure.
  * @fis     Pointer to the FIS that describes the command.
- * @fisLen  Length in WORDS of the FIS.
+ * @fis_len  Length in WORDS of the FIS.
  * @buffer  DMA accessible for command data.
- * @bufLen  Length, in bytes, of the data buffer.
+ * @buf_len  Length, in bytes, of the data buffer.
  * @opts    Command header options, excluding the FIS length
  *             and the number of PRD entries.
  * @timeout Time in ms to wait for the command to complete.
@@ -1005,18 +1220,19 @@ static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
  *     -EAGAIN  Time out waiting for command to complete.
  */
 static int mtip_exec_internal_command(struct mtip_port *port,
-                                       void *fis,
-                                       int fisLen,
+                                       struct host_to_dev_fis *fis,
+                                       int fis_len,
                                        dma_addr_t buffer,
-                                       int bufLen,
+                                       int buf_len,
                                        u32 opts,
                                        gfp_t atomic,
                                        unsigned long timeout)
 {
        struct mtip_cmd_sg *command_sg;
        DECLARE_COMPLETION_ONSTACK(wait);
-       int rv = 0;
+       int rv = 0, ready2go = 1;
        struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
+       unsigned long to;
 
        /* Make sure the buffer is 8 byte aligned. This is asic specific. */
        if (buffer & 0x00000007) {
@@ -1025,22 +1241,38 @@ static int mtip_exec_internal_command(struct mtip_port *port,
                return -EFAULT;
        }
 
-       /* Only one internal command should be running at a time */
-       if (test_and_set_bit(MTIP_TAG_INTERNAL, port->allocated)) {
+       to = jiffies + msecs_to_jiffies(timeout);
+       do {
+               ready2go = !test_and_set_bit(MTIP_TAG_INTERNAL,
+                                               port->allocated);
+               if (ready2go)
+                       break;
+               mdelay(100);
+       } while (time_before(jiffies, to));
+       if (!ready2go) {
                dev_warn(&port->dd->pdev->dev,
-                       "Internal command already active\n");
+                       "Internal cmd active. new cmd [%02X]\n", fis->command);
                return -EBUSY;
        }
-       port->internal_cmd_in_progress = 1;
+       set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
+       port->ic_pause_timer = 0;
+
+       if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
+               clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
+       else if (fis->command == ATA_CMD_DOWNLOAD_MICRO)
+               clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
 
        if (atomic == GFP_KERNEL) {
-               /* wait for io to complete if non atomic */
-               if (mtip_quiesce_io(port, 5000) < 0) {
-                       dev_warn(&port->dd->pdev->dev,
-                               "Failed to quiesce IO\n");
-                       release_slot(port, MTIP_TAG_INTERNAL);
-                       port->internal_cmd_in_progress = 0;
-                       return -EBUSY;
+               if (fis->command != ATA_CMD_STANDBYNOW1) {
+                       /* wait for io to complete if non atomic */
+                       if (mtip_quiesce_io(port, 5000) < 0) {
+                               dev_warn(&port->dd->pdev->dev,
+                                       "Failed to quiesce IO\n");
+                               release_slot(port, MTIP_TAG_INTERNAL);
+                               clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
+                               wake_up_interruptible(&port->svc_wait);
+                               return -EBUSY;
+                       }
                }
 
                /* Set the completion function and data for the command. */
@@ -1050,23 +1282,27 @@ static int mtip_exec_internal_command(struct mtip_port *port,
        } else {
                /* Clear completion - we're going to poll */
                int_cmd->comp_data = NULL;
-               int_cmd->comp_func = NULL;
+               int_cmd->comp_func = mtip_null_completion;
        }
 
        /* Copy the command to the command table */
-       memcpy(int_cmd->command, fis, fisLen*4);
+       memcpy(int_cmd->command, fis, fis_len*4);
 
        /* Populate the SG list */
        int_cmd->command_header->opts =
-                cpu_to_le32(opts | fisLen);
-       if (bufLen) {
+                __force_bit2int cpu_to_le32(opts | fis_len);
+       if (buf_len) {
                command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
 
-               command_sg->info = cpu_to_le32((bufLen-1) & 0x3fffff);
-               command_sg->dba = cpu_to_le32(buffer & 0xffffffff);
-               command_sg->dba_upper = cpu_to_le32((buffer >> 16) >> 16);
+               command_sg->info =
+                       __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
+               command_sg->dba =
+                       __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
+               command_sg->dba_upper =
+                       __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
 
-               int_cmd->command_header->opts |= cpu_to_le32((1 << 16));
+               int_cmd->command_header->opts |=
+                       __force_bit2int cpu_to_le32((1 << 16));
        }
 
        /* Populate the command header */
@@ -1082,8 +1318,15 @@ static int mtip_exec_internal_command(struct mtip_port *port,
                                &wait,
                                msecs_to_jiffies(timeout)) == 0) {
                        dev_err(&port->dd->pdev->dev,
-                               "Internal command did not complete [%d]\n",
-                               atomic);
+                               "Internal command did not complete [%d] "
+                               "within timeout of  %lu ms\n",
+                               atomic, timeout);
+                       if (mtip_check_surprise_removal(port->dd->pdev) ||
+                               test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
+                                               &port->dd->dd_flag)) {
+                               rv = -ENXIO;
+                               goto exec_ic_exit;
+                       }
                        rv = -EAGAIN;
                }
 
@@ -1091,31 +1334,60 @@ static int mtip_exec_internal_command(struct mtip_port *port,
                        & (1 << MTIP_TAG_INTERNAL)) {
                        dev_warn(&port->dd->pdev->dev,
                                "Retiring internal command but CI is 1.\n");
+                       if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
+                                               &port->dd->dd_flag)) {
+                               hba_reset_nosleep(port->dd);
+                               rv = -ENXIO;
+                       } else {
+                               mtip_restart_port(port);
+                               rv = -EAGAIN;
+                       }
+                       goto exec_ic_exit;
                }
 
        } else {
                /* Spin for <timeout> checking if command still outstanding */
                timeout = jiffies + msecs_to_jiffies(timeout);
-
-               while ((readl(
-                       port->cmd_issue[MTIP_TAG_INTERNAL])
-                       & (1 << MTIP_TAG_INTERNAL))
-                       && time_before(jiffies, timeout))
-                       ;
+               while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
+                               & (1 << MTIP_TAG_INTERNAL))
+                               && time_before(jiffies, timeout)) {
+                       if (mtip_check_surprise_removal(port->dd->pdev)) {
+                               rv = -ENXIO;
+                               goto exec_ic_exit;
+                       }
+                       if ((fis->command != ATA_CMD_STANDBYNOW1) &&
+                               test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
+                                               &port->dd->dd_flag)) {
+                               rv = -ENXIO;
+                               goto exec_ic_exit;
+                       }
+               }
 
                if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
                        & (1 << MTIP_TAG_INTERNAL)) {
                        dev_err(&port->dd->pdev->dev,
-                               "Internal command did not complete [%d]\n",
-                               atomic);
+                               "Internal command did not complete [atomic]\n");
                        rv = -EAGAIN;
+                       if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
+                                               &port->dd->dd_flag)) {
+                               hba_reset_nosleep(port->dd);
+                               rv = -ENXIO;
+                       } else {
+                               mtip_restart_port(port);
+                               rv = -EAGAIN;
+                       }
                }
        }
-
+exec_ic_exit:
        /* Clear the allocated and active bits for the internal command. */
        atomic_set(&int_cmd->active, 0);
        release_slot(port, MTIP_TAG_INTERNAL);
-       port->internal_cmd_in_progress = 0;
+       if (rv >= 0 && mtip_pause_ncq(port, fis)) {
+               /* NCQ paused */
+               return rv;
+       }
+       clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
+       wake_up_interruptible(&port->svc_wait);
 
        return rv;
 }
@@ -1164,7 +1436,8 @@ static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
        int rv = 0;
        struct host_to_dev_fis fis;
 
-       down_write(&port->dd->internal_sem);
+       if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
+               return -EFAULT;
 
        /* Build the FIS. */
        memset(&fis, 0, sizeof(struct host_to_dev_fis));
@@ -1223,7 +1496,6 @@ static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
        }
 
 out:
-       up_write(&port->dd->internal_sem);
        return rv;
 }
 
@@ -1240,8 +1512,7 @@ static int mtip_standby_immediate(struct mtip_port *port)
 {
        int rv;
        struct host_to_dev_fis  fis;
-
-       down_write(&port->dd->internal_sem);
+       unsigned long start;
 
        /* Build the FIS. */
        memset(&fis, 0, sizeof(struct host_to_dev_fis));
@@ -1249,17 +1520,150 @@ static int mtip_standby_immediate(struct mtip_port *port)
        fis.opts        = 1 << 7;
        fis.command     = ATA_CMD_STANDBYNOW1;
 
-       /* Execute the command.  Use a 15-second timeout for large drives. */
+       start = jiffies;
        rv = mtip_exec_internal_command(port,
                                        &fis,
                                        5,
                                        0,
                                        0,
                                        0,
-                                       GFP_KERNEL,
+                                       GFP_ATOMIC,
+                                       15000);
+       dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
+                       jiffies_to_msecs(jiffies - start));
+       if (rv)
+               dev_warn(&port->dd->pdev->dev,
+                       "STANDBY IMMEDIATE command failed.\n");
+
+       return rv;
+}
+
+/*
+ * Issue a READ LOG EXT command to the device.
+ *
+ * @port       pointer to the port structure.
+ * @page       page number to fetch
+ * @buffer     pointer to buffer
+ * @buffer_dma dma address corresponding to @buffer
+ * @sectors    page length to fetch, in sectors
+ *
+ * return value
+ *     @rv     return value from mtip_exec_internal_command()
+ */
+static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
+                               dma_addr_t buffer_dma, unsigned int sectors)
+{
+       struct host_to_dev_fis fis;
+
+       memset(&fis, 0, sizeof(struct host_to_dev_fis));
+       fis.type        = 0x27;
+       fis.opts        = 1 << 7;
+       fis.command     = ATA_CMD_READ_LOG_EXT;
+       fis.sect_count  = sectors & 0xFF;
+       fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
+       fis.lba_low     = page;
+       fis.lba_mid     = 0;
+       fis.device      = ATA_DEVICE_OBS;
+
+       memset(buffer, 0, sectors * ATA_SECT_SIZE);
+
+       return mtip_exec_internal_command(port,
+                                       &fis,
+                                       5,
+                                       buffer_dma,
+                                       sectors * ATA_SECT_SIZE,
+                                       0,
+                                       GFP_ATOMIC,
+                                       MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
+}
+
+/*
+ * Issue a SMART READ DATA command to the device.
+ *
+ * @port       pointer to the port structure.
+ * @buffer     pointer to buffer
+ * @buffer_dma dma address corresponding to @buffer
+ *
+ * return value
+ *     @rv     return value from mtip_exec_internal_command()
+ */
+static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
+                                       dma_addr_t buffer_dma)
+{
+       struct host_to_dev_fis fis;
+
+       memset(&fis, 0, sizeof(struct host_to_dev_fis));
+       fis.type        = 0x27;
+       fis.opts        = 1 << 7;
+       fis.command     = ATA_CMD_SMART;
+       fis.features    = 0xD0;
+       fis.sect_count  = 1;
+       fis.lba_mid     = 0x4F;
+       fis.lba_hi      = 0xC2;
+       fis.device      = ATA_DEVICE_OBS;
+
+       return mtip_exec_internal_command(port,
+                                       &fis,
+                                       5,
+                                       buffer_dma,
+                                       ATA_SECT_SIZE,
+                                       0,
+                                       GFP_ATOMIC,
                                        15000);
+}
+
+/*
+ * Get the value of a smart attribute
+ *
+ * @port       pointer to the port structure
+ * @id         attribute number
+ * @attrib     pointer to return attrib information corresponding to @id
+ *
+ * return value
+ *     -EINVAL NULL buffer passed or unsupported attribute @id.
+ *     -EPERM  Identify data not valid, SMART not supported or not enabled
+ */
+static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
+                                               struct smart_attr *attrib)
+{
+       int rv, i;
+       struct smart_attr *pattr;
+
+       if (!attrib)
+               return -EINVAL;
+
+       if (!port->identify_valid) {
+               dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
+               return -EPERM;
+       }
+       if (!(port->identify[82] & 0x1)) {
+               dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
+               return -EPERM;
+       }
+       if (!(port->identify[85] & 0x1)) {
+               dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
+               return -EPERM;
+       }
+
+       memset(port->smart_buf, 0, ATA_SECT_SIZE);
+       rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
+       if (rv) {
+               dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
+               return rv;
+       }
+
+       pattr = (struct smart_attr *)(port->smart_buf + 2);
+       for (i = 0; i < 29; i++, pattr++)
+               if (pattr->attr_id == id) {
+                       memcpy(attrib, pattr, sizeof(struct smart_attr));
+                       break;
+               }
 
-       up_write(&port->dd->internal_sem);
+       if (i == 29) {
+               dev_warn(&port->dd->pdev->dev,
+                       "Query for invalid SMART attribute ID\n");
+               rv = -EINVAL;
+       }
 
        return rv;
 }
@@ -1274,7 +1678,7 @@ static int mtip_standby_immediate(struct mtip_port *port)
  *     1 Capacity was returned successfully.
  *     0 The identify information is invalid.
  */
-bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
+static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
 {
        struct mtip_port *port = dd->port;
        u64 total, raw0, raw1, raw2, raw3;
@@ -1361,7 +1765,7 @@ static void mtip_dump_identify(struct mtip_port *port)
                         ((u64)sectors) * ATA_SECT_SIZE >> 20);
 
        pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
-       switch (revid & 0xff) {
+       switch (revid & 0xFF) {
        case 0x1:
                strlcpy(cbuf, "A0", 3);
                break;
@@ -1401,15 +1805,12 @@ static inline void fill_command_sg(struct driver_data *dd,
                if (dma_len > 0x400000)
                        dev_err(&dd->pdev->dev,
                                "DMA segment length truncated\n");
-               command_sg->info = cpu_to_le32((dma_len-1) & 0x3fffff);
-#if (BITS_PER_LONG == 64)
-               *((unsigned long *) &command_sg->dba) =
-                        cpu_to_le64(sg_dma_address(sg));
-#else
-               command_sg->dba = cpu_to_le32(sg_dma_address(sg));
-               command_sg->dba_upper   =
-                        cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
-#endif
+               command_sg->info = __force_bit2int
+                       cpu_to_le32((dma_len-1) & 0x3FFFFF);
+               command_sg->dba = __force_bit2int
+                       cpu_to_le32(sg_dma_address(sg));
+               command_sg->dba_upper = __force_bit2int
+                       cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
                command_sg++;
                sg++;
        }
@@ -1421,14 +1822,11 @@ static inline void fill_command_sg(struct driver_data *dd,
  * return value 0 The command completed successfully.
  * return value -1 An error occurred while executing the command.
  */
-int exec_drive_task(struct mtip_port *port, u8 *command)
+static int exec_drive_task(struct mtip_port *port, u8 *command)
 {
        struct host_to_dev_fis  fis;
        struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
 
-       /* Lock the internal command semaphore. */
-       down_write(&port->dd->internal_sem);
-
        /* Build the FIS. */
        memset(&fis, 0, sizeof(struct host_to_dev_fis));
        fis.type        = 0x27;
@@ -1441,10 +1839,7 @@ int exec_drive_task(struct mtip_port *port, u8 *command)
        fis.cyl_hi      = command[5];
        fis.device      = command[6] & ~0x10; /* Clear the dev bit*/
 
-
-       dbg_printk(MTIP_DRV_NAME "%s: User Command: cmd %x, feat %x, "
-               "nsect %x, sect %x, lcyl %x, "
-               "hcyl %x, sel %x\n",
+       dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
                __func__,
                command[0],
                command[1],
@@ -1463,7 +1858,6 @@ int exec_drive_task(struct mtip_port *port, u8 *command)
                                 0,
                                 GFP_KERNEL,
                                 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
-               up_write(&port->dd->internal_sem);
                return -1;
        }
 
@@ -1472,15 +1866,13 @@ int exec_drive_task(struct mtip_port *port, u8 *command)
        command[4] = reply->cyl_low;
        command[5] = reply->cyl_hi;
 
-       dbg_printk(MTIP_DRV_NAME "%s: Completion Status: stat %x, "
-               "err %x , cyl_lo %x cyl_hi %x\n",
+       dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
                __func__,
                command[0],
                command[1],
                command[4],
                command[5]);
 
-       up_write(&port->dd->internal_sem);
        return 0;
 }
 
@@ -1497,15 +1889,12 @@ int exec_drive_task(struct mtip_port *port, u8 *command)
  *                 data to the user space buffer.
  * return value -1 An error occurred while executing the command.
  */
-int exec_drive_command(struct mtip_port *port, u8 *command,
-                       void __user *user_buffer)
+static int exec_drive_command(struct mtip_port *port, u8 *command,
+                               void __user *user_buffer)
 {
        struct host_to_dev_fis  fis;
        struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
 
-       /* Lock the internal command semaphore. */
-       down_write(&port->dd->internal_sem);
-
        /* Build the FIS. */
        memset(&fis, 0, sizeof(struct host_to_dev_fis));
        fis.type                = 0x27;
@@ -1515,12 +1904,12 @@ int exec_drive_command(struct mtip_port *port, u8 *command,
        fis.sect_count  = command[3];
        if (fis.command == ATA_CMD_SMART) {
                fis.sector      = command[1];
-               fis.cyl_low     = 0x4f;
-               fis.cyl_hi      = 0xc2;
+               fis.cyl_low     = 0x4F;
+               fis.cyl_hi      = 0xC2;
        }
 
        dbg_printk(MTIP_DRV_NAME
-               "%s: User Command: cmd %x, sect %x, "
+               " %s: User Command: cmd %x, sect %x, "
                "feat %x, sectcnt %x\n",
                __func__,
                command[0],
@@ -1540,7 +1929,6 @@ int exec_drive_command(struct mtip_port *port, u8 *command,
                                 GFP_KERNEL,
                                 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
                                 < 0) {
-               up_write(&port->dd->internal_sem);
                return -1;
        }
 
@@ -1550,7 +1938,7 @@ int exec_drive_command(struct mtip_port *port, u8 *command,
        command[2] = command[3];
 
        dbg_printk(MTIP_DRV_NAME
-               "%s: Completion Status: stat %x, "
+               " %s: Completion Status: stat %x, "
                "err %x, cmd %x\n",
                __func__,
                command[0],
@@ -1561,12 +1949,10 @@ int exec_drive_command(struct mtip_port *port, u8 *command,
                if (copy_to_user(user_buffer,
                                 port->sector_buffer,
                                 ATA_SECT_SIZE * command[3])) {
-                       up_write(&port->dd->internal_sem);
                        return -EFAULT;
                }
        }
 
-       up_write(&port->dd->internal_sem);
        return 0;
 }
 
@@ -1589,26 +1975,28 @@ static unsigned int implicit_sector(unsigned char command,
 
        /* list of commands that have an implicit sector count of 1 */
        switch (command) {
-       case 0xF1:
-       case 0xF2:
-       case 0xF3:
-       case 0xF4:
-       case 0xF5:
-       case 0xF6:
-       case 0xE4:
-       case 0xE8:
+       case ATA_CMD_SEC_SET_PASS:
+       case ATA_CMD_SEC_UNLOCK:
+       case ATA_CMD_SEC_ERASE_PREP:
+       case ATA_CMD_SEC_ERASE_UNIT:
+       case ATA_CMD_SEC_FREEZE_LOCK:
+       case ATA_CMD_SEC_DISABLE_PASS:
+       case ATA_CMD_PMP_READ:
+       case ATA_CMD_PMP_WRITE:
                rv = 1;
                break;
-       case 0xF9:
-               if (features == 0x03)
+       case ATA_CMD_SET_MAX:
+               if (features == ATA_SET_MAX_UNLOCK)
                        rv = 1;
                break;
-       case 0xB0:
-               if ((features == 0xD0) || (features == 0xD1))
+       case ATA_CMD_SMART:
+               if ((features == ATA_SMART_READ_VALUES) ||
+                               (features == ATA_SMART_READ_THRESHOLDS))
                        rv = 1;
                break;
-       case 0xB1:
-               if ((features == 0xC2) || (features == 0xC3))
+       case ATA_CMD_CONF_OVERLAY:
+               if ((features == ATA_DCO_IDENTIFY) ||
+                               (features == ATA_DCO_SET))
                        rv = 1;
                break;
        }
@@ -1620,68 +2008,26 @@ static unsigned int implicit_sector(unsigned char command,
  * See ide_taskfile_ioctl() for derivation
  */
 static int exec_drive_taskfile(struct driver_data *dd,
-                               unsigned long arg,
-                               unsigned char compat)
+                              void __user *buf,
+                              ide_task_request_t *req_task,
+                              int outtotal)
 {
        struct host_to_dev_fis  fis;
        struct host_to_dev_fis *reply;
-       ide_task_request_t *req_task;
        u8 *outbuf = NULL;
        u8 *inbuf = NULL;
-       dma_addr_t outbuf_dma = (dma_addr_t)NULL;
-       dma_addr_t inbuf_dma = (dma_addr_t)NULL;
-       dma_addr_t dma_buffer = (dma_addr_t)NULL;
+       dma_addr_t outbuf_dma = 0;
+       dma_addr_t inbuf_dma = 0;
+       dma_addr_t dma_buffer = 0;
        int err = 0;
-       int tasksize = sizeof(struct ide_task_request_s);
        unsigned int taskin = 0;
        unsigned int taskout = 0;
        u8 nsect = 0;
-       char __user *buf = (char __user *)arg;
        unsigned int timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
        unsigned int force_single_sector;
        unsigned int transfer_size;
        unsigned long task_file_data;
-       int intotal, outtotal;
-       struct mtip_compat_ide_task_request_s *compat_req_task = NULL;
-       int compat_tasksize = sizeof(struct mtip_compat_ide_task_request_s);
-
-       req_task = kzalloc(tasksize, GFP_KERNEL);
-       if (req_task == NULL)
-               return -ENOMEM;
-
-       if (compat == 1) {
-               compat_req_task =
-                       (struct mtip_compat_ide_task_request_s __user *) arg;
-
-               if (copy_from_user(req_task, buf,
-                               compat_tasksize -
-                               (2 * sizeof(compat_long_t)))) {
-                       err = -EFAULT;
-                       goto abort;
-               }
-
-               if (get_user(req_task->out_size, &compat_req_task->out_size)) {
-                       err = -EFAULT;
-                       goto abort;
-               }
-
-               if (get_user(req_task->in_size, &compat_req_task->in_size)) {
-                       err = -EFAULT;
-                       goto abort;
-               }
-
-               outtotal = compat_tasksize;
-               intotal = compat_tasksize + req_task->out_size;
-       } else {
-               if (copy_from_user(req_task, buf, tasksize)) {
-                       kfree(req_task);
-                       err = -EFAULT;
-                       goto abort;
-               }
-
-               outtotal = tasksize;
-               intotal = tasksize + req_task->out_size;
-       }
+       int intotal = outtotal + req_task->out_size;
 
        taskout = req_task->out_size;
        taskin = req_task->in_size;
@@ -1705,7 +2051,7 @@ static int exec_drive_taskfile(struct driver_data *dd,
                                         outbuf,
                                         taskout,
                                         DMA_TO_DEVICE);
-               if (outbuf_dma == (dma_addr_t)NULL) {
+               if (outbuf_dma == 0) {
                        err = -ENOMEM;
                        goto abort;
                }
@@ -1726,7 +2072,7 @@ static int exec_drive_taskfile(struct driver_data *dd,
                inbuf_dma = pci_map_single(dd->pdev,
                                         inbuf,
                                         taskin, DMA_FROM_DEVICE);
-               if (inbuf_dma == (dma_addr_t)NULL) {
+               if (inbuf_dma == 0) {
                        err = -ENOMEM;
                        goto abort;
                }
@@ -1750,9 +2096,6 @@ static int exec_drive_taskfile(struct driver_data *dd,
                goto abort;
        }
 
-       /* Lock the internal command semaphore. */
-       down_write(&dd->internal_sem);
-
        /* Build the FIS. */
        memset(&fis, 0, sizeof(struct host_to_dev_fis));
 
@@ -1791,7 +2134,6 @@ static int exec_drive_taskfile(struct driver_data *dd,
                                dev_warn(&dd->pdev->dev,
                                        "data movement but "
                                        "sect_count is 0\n");
-                                       up_write(&dd->internal_sem);
                                        err = -EINVAL;
                                        goto abort;
                        }
@@ -1799,9 +2141,10 @@ static int exec_drive_taskfile(struct driver_data *dd,
        }
 
        dbg_printk(MTIP_DRV_NAME
-               "taskfile: cmd %x, feat %x, nsect %x,"
+               " %s: cmd %x, feat %x, nsect %x,"
                " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
                " head/dev %x\n",
+               __func__,
                fis.command,
                fis.features,
                fis.sect_count,
@@ -1811,20 +2154,26 @@ static int exec_drive_taskfile(struct driver_data *dd,
                fis.device);
 
        switch (fis.command) {
-       case 0x92: /* Change timeout for Download Microcode to 60 seconds.*/
-               timeout = 60000;
+       case ATA_CMD_DOWNLOAD_MICRO:
+               /* Change timeout for Download Microcode to 2 minutes */
+               timeout = 120000;
                break;
-       case 0xf4: /* Change timeout for Security Erase Unit to 4 minutes.*/
+       case ATA_CMD_SEC_ERASE_UNIT:
+               /* Change timeout for Security Erase Unit to 4 minutes.*/
                timeout = 240000;
                break;
-       case 0xe0: /* Change timeout for standby immediate to 10 seconds.*/
+       case ATA_CMD_STANDBYNOW1:
+               /* Change timeout for standby immediate to 10 seconds.*/
                timeout = 10000;
                break;
-       case 0xf7: /* Change timeout for vendor unique command to 10 secs */
+       case 0xF7:
+       case 0xFA:
+               /* Change timeout for vendor unique command to 10 secs */
                timeout = 10000;
                break;
-       case 0xfa: /* Change timeout for vendor unique command to 10 secs */
-               timeout = 10000;
+       case ATA_CMD_SMART:
+               /* Change timeout for vendor unique command to 15 secs */
+               timeout = 15000;
                break;
        default:
                timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
@@ -1846,7 +2195,6 @@ static int exec_drive_taskfile(struct driver_data *dd,
                                 0,
                                 GFP_KERNEL,
                                 timeout) < 0) {
-               up_write(&dd->internal_sem);
                err = -EIO;
                goto abort;
        }
@@ -1868,8 +2216,8 @@ static int exec_drive_taskfile(struct driver_data *dd,
        if (outbuf_dma)
                pci_unmap_single(dd->pdev, outbuf_dma,
                        taskout, DMA_TO_DEVICE);
-       inbuf_dma  = (dma_addr_t) NULL;
-       outbuf_dma = (dma_addr_t) NULL;
+       inbuf_dma  = 0;
+       outbuf_dma = 0;
 
        /* return the ATA registers to the caller.*/
        req_task->io_ports[1] = reply->features;
@@ -1887,18 +2235,8 @@ static int exec_drive_taskfile(struct driver_data *dd,
                req_task->hob_ports[1] = reply->features_ex;
                req_task->hob_ports[2] = reply->sect_cnt_ex;
        }
-
-       /* Com rest after secure erase or lowlevel format */
-       if (((fis.command == 0xF4) ||
-               ((fis.command == 0xFC) &&
-                       (fis.features == 0x27 || fis.features == 0x72 ||
-                        fis.features == 0x62 || fis.features == 0x26))) &&
-                        !(reply->command & 1)) {
-               mtip_restart_port(dd->port);
-       }
-
        dbg_printk(MTIP_DRV_NAME
-               "%s: Completion: stat %x,"
+               " %s: Completion: stat %x,"
                "err %x, sect_cnt %x, lbalo %x,"
                "lbamid %x, lbahi %x, dev %x\n",
                __func__,
@@ -1910,30 +2248,6 @@ static int exec_drive_taskfile(struct driver_data *dd,
                req_task->io_ports[5],
                req_task->io_ports[6]);
 
-       up_write(&dd->internal_sem);
-
-       if (compat == 1) {
-               if (copy_to_user(buf, req_task,
-                               compat_tasksize -
-                               (2 * sizeof(compat_long_t)))) {
-                       err = -EFAULT;
-                       goto abort;
-               }
-               if (put_user(req_task->out_size,
-                               &compat_req_task->out_size)) {
-                       err = -EFAULT;
-                       goto abort;
-               }
-               if (put_user(req_task->in_size, &compat_req_task->in_size)) {
-                       err = -EFAULT;
-                       goto abort;
-               }
-       } else {
-               if (copy_to_user(buf, req_task, tasksize)) {
-                       err = -EFAULT;
-                       goto abort;
-               }
-       }
        if (taskout) {
                if (copy_to_user(buf + outtotal, outbuf, taskout)) {
                        err = -EFAULT;
@@ -1953,7 +2267,6 @@ abort:
        if (outbuf_dma)
                pci_unmap_single(dd->pdev, outbuf_dma,
                                        taskout, DMA_TO_DEVICE);
-       kfree(req_task);
        kfree(outbuf);
        kfree(inbuf);
 
@@ -1977,10 +2290,8 @@ abort:
  *     -EFAULT An error occurred copying data to a user space buffer.
  *     -EIO    An error occurred while executing the command.
  */
-int mtip_hw_ioctl(struct driver_data *dd,
-                 unsigned int cmd,
-                 unsigned long arg,
-                 unsigned char compat)
+static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
+                        unsigned long arg)
 {
        switch (cmd) {
        case HDIO_GET_IDENTITY:
@@ -2037,8 +2348,25 @@ int mtip_hw_ioctl(struct driver_data *dd,
 
                break;
        }
-       case HDIO_DRIVE_TASKFILE:
-               return exec_drive_taskfile(dd, arg, compat);
+       case HDIO_DRIVE_TASKFILE: {
+               ide_task_request_t req_task;
+               int ret, outtotal;
+
+               if (copy_from_user(&req_task, (void __user *) arg,
+                                       sizeof(req_task)))
+                       return -EFAULT;
+
+               outtotal = sizeof(req_task);
+
+               ret = exec_drive_taskfile(dd, (void __user *) arg,
+                                               &req_task, outtotal);
+
+               if (copy_to_user((void __user *) arg, &req_task,
+                                                       sizeof(req_task)))
+                       return -EFAULT;
+
+               return ret;
+       }
 
        default:
                return -EINVAL;
@@ -2062,34 +2390,22 @@ int mtip_hw_ioctl(struct driver_data *dd,
  *          when the read completes.
  * @data     Callback data passed to the callback function
  *          when the read completes.
- * @barrier  If non-zero, this command must be completed before
- *          issuing any other commands.
  * @dir      Direction (read or write)
  *
  * return value
  *     None
  */
-void mtip_hw_submit_io(struct driver_data *dd,
-                       sector_t start,
-                       int nsect,
-                       int nents,
-                       int tag,
-                       void *callback,
-                       void *data,
-                       int barrier,
-                       int dir)
+static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
+                             int nsect, int nents, int tag, void *callback,
+                             void *data, int dir)
 {
        struct host_to_dev_fis  *fis;
        struct mtip_port *port = dd->port;
        struct mtip_cmd *command = &port->commands[tag];
+       int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
 
        /* Map the scatter list for DMA access */
-       if (dir == READ)
-               nents = dma_map_sg(&dd->pdev->dev, command->sg,
-                                       nents, DMA_FROM_DEVICE);
-       else
-               nents = dma_map_sg(&dd->pdev->dev, command->sg,
-                                       nents, DMA_TO_DEVICE);
+       nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
 
        command->scatter_ents = nents;
 
@@ -2105,13 +2421,11 @@ void mtip_hw_submit_io(struct driver_data *dd,
        fis->opts        = 1 << 7;
        fis->command     =
                (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
-       *((unsigned int *) &fis->lba_low) = (start & 0xffffff);
-       *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xffffff);
+       *((unsigned int *) &fis->lba_low) = (start & 0xFFFFFF);
+       *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xFFFFFF);
        fis->device      = 1 << 6;
-       if (barrier)
-               fis->device |= FUA_BIT;
-       fis->features    = nsect & 0xff;
-       fis->features_ex = (nsect >> 8) & 0xff;
+       fis->features    = nsect & 0xFF;
+       fis->features_ex = (nsect >> 8) & 0xFF;
        fis->sect_count  = ((tag << 3) | (tag >> 5));
        fis->sect_cnt_ex = 0;
        fis->control     = 0;
@@ -2120,8 +2434,9 @@ void mtip_hw_submit_io(struct driver_data *dd,
        fill_command_sg(dd, command, nents);
 
        /* Populate the command header */
-       command->command_header->opts = cpu_to_le32(
-                       (nents << 16) | 5 | AHCI_CMD_PREFETCH);
+       command->command_header->opts =
+                       __force_bit2int cpu_to_le32(
+                               (nents << 16) | 5 | AHCI_CMD_PREFETCH);
        command->command_header->byte_count = 0;
 
        /*
@@ -2130,7 +2445,7 @@ void mtip_hw_submit_io(struct driver_data *dd,
         */
        command->comp_data = dd;
        command->comp_func = mtip_async_complete;
-       command->direction = (dir == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
+       command->direction = dma_dir;
 
        /*
         * Set the completion function and data for the command passed
@@ -2140,19 +2455,19 @@ void mtip_hw_submit_io(struct driver_data *dd,
        command->async_callback = callback;
 
        /*
-        * Lock used to prevent this command from being issued
-        * if an internal command is in progress.
+        * To prevent this command from being issued
+        * if an internal command is in progress or error handling is active.
         */
-       down_read(&port->dd->internal_sem);
+       if (port->flags & MTIP_PF_PAUSE_IO) {
+               set_bit(tag, port->cmds_to_issue);
+               set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
+               return;
+       }
 
        /* Issue the command to the hardware */
        mtip_issue_ncq_command(port, tag);
 
-       /* Set the command's timeout value.*/
-       port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
-                                       MTIP_NCQ_COMMAND_TIMEOUT_MS);
-
-       up_read(&port->dd->internal_sem);
+       return;
 }
 
 /*
@@ -2164,7 +2479,7 @@ void mtip_hw_submit_io(struct driver_data *dd,
  * return value
  *      None
  */
-void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
+static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
 {
        release_slot(dd->port, tag);
 }
@@ -2180,8 +2495,8 @@ void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
  *     Pointer to the scatter list for the allocated command slot
  *     or NULL if no command slots are available.
  */
-struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
-                                               int *tag)
+static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
+                                                  int *tag)
 {
        /*
         * It is possible that, even with this semaphore, a thread
@@ -2191,8 +2506,14 @@ struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
        down(&dd->port->cmd_slot);
        *tag = get_slot(dd->port);
 
-       if (unlikely(*tag < 0))
+       if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
+               up(&dd->port->cmd_slot);
+               return NULL;
+       }
+       if (unlikely(*tag < 0)) {
+               up(&dd->port->cmd_slot);
                return NULL;
+       }
 
        return dd->port->commands[*tag].sg;
 }
@@ -2207,7 +2528,7 @@ struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
  * return value
  *     The size, in bytes, of the data copied into buf.
  */
-static ssize_t hw_show_registers(struct device *dev,
+static ssize_t mtip_hw_show_registers(struct device *dev,
                                struct device_attribute *attr,
                                char *buf)
 {
@@ -2216,7 +2537,7 @@ static ssize_t hw_show_registers(struct device *dev,
        int size = 0;
        int n;
 
-       size += sprintf(&buf[size], "%s:\ns_active:\n", __func__);
+       size += sprintf(&buf[size], "S ACTive:\n");
 
        for (n = 0; n < dd->slot_groups; n++)
                size += sprintf(&buf[size], "0x%08x\n",
@@ -2240,20 +2561,39 @@ static ssize_t hw_show_registers(struct device *dev,
                                 group_allocated);
        }
 
-       size += sprintf(&buf[size], "completed:\n");
+       size += sprintf(&buf[size], "Completed:\n");
 
        for (n = 0; n < dd->slot_groups; n++)
                size += sprintf(&buf[size], "0x%08x\n",
                                readl(dd->port->completed[n]));
 
-       size += sprintf(&buf[size], "PORT_IRQ_STAT 0x%08x\n",
+       size += sprintf(&buf[size], "PORT IRQ STAT : 0x%08x\n",
                                readl(dd->port->mmio + PORT_IRQ_STAT));
-       size += sprintf(&buf[size], "HOST_IRQ_STAT 0x%08x\n",
+       size += sprintf(&buf[size], "HOST IRQ STAT : 0x%08x\n",
                                readl(dd->mmio + HOST_IRQ_STAT));
 
        return size;
 }
-static DEVICE_ATTR(registers, S_IRUGO, hw_show_registers, NULL);
+
+static ssize_t mtip_hw_show_status(struct device *dev,
+                               struct device_attribute *attr,
+                               char *buf)
+{
+       struct driver_data *dd = dev_to_disk(dev)->private_data;
+       int size = 0;
+
+       if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
+               size += sprintf(buf, "%s", "thermal_shutdown\n");
+       else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
+               size += sprintf(buf, "%s", "write_protect\n");
+       else
+               size += sprintf(buf, "%s", "online\n");
+
+       return size;
+}
+
+static DEVICE_ATTR(registers, S_IRUGO, mtip_hw_show_registers, NULL);
+static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
 
 /*
  * Create the sysfs related attributes.
@@ -2265,14 +2605,17 @@ static DEVICE_ATTR(registers, S_IRUGO, hw_show_registers, NULL);
  *     0       Operation completed successfully.
  *     -EINVAL Invalid parameter.
  */
-int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
+static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
 {
        if (!kobj || !dd)
                return -EINVAL;
 
        if (sysfs_create_file(kobj, &dev_attr_registers.attr))
                dev_warn(&dd->pdev->dev,
-                       "Error creating registers sysfs entry\n");
+                       "Error creating 'registers' sysfs entry\n");
+       if (sysfs_create_file(kobj, &dev_attr_status.attr))
+               dev_warn(&dd->pdev->dev,
+                       "Error creating 'status' sysfs entry\n");
        return 0;
 }
 
@@ -2286,12 +2629,13 @@ int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
  *     0       Operation completed successfully.
  *     -EINVAL Invalid parameter.
  */
-int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
+static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
 {
        if (!kobj || !dd)
                return -EINVAL;
 
        sysfs_remove_file(kobj, &dev_attr_registers.attr);
+       sysfs_remove_file(kobj, &dev_attr_status.attr);
 
        return 0;
 }
@@ -2384,14 +2728,15 @@ static int mtip_ftl_rebuild_poll(struct driver_data *dd)
                "FTL rebuild in progress. Polling for completion.\n");
 
        start = jiffies;
-       dd->ftlrebuildflag = 1;
        timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
 
        do {
-#ifdef CONFIG_HOTPLUG
+               if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
+                               &dd->dd_flag)))
+                       return -EFAULT;
                if (mtip_check_surprise_removal(dd->pdev))
                        return -EFAULT;
-#endif
+
                if (mtip_get_identify(dd->port, NULL) < 0)
                        return -EFAULT;
 
@@ -2409,20 +2754,91 @@ static int mtip_ftl_rebuild_poll(struct driver_data *dd)
                        dev_warn(&dd->pdev->dev,
                                "FTL rebuild complete (%d secs).\n",
                        jiffies_to_msecs(jiffies - start) / 1000);
-                       dd->ftlrebuildflag = 0;
-                       break;
+                       mtip_block_initialize(dd);
+                       return 0;
                }
                ssleep(10);
        } while (time_before(jiffies, timeout));
 
        /* Check for timeout */
-       if (dd->ftlrebuildflag) {
-               dev_err(&dd->pdev->dev,
+       dev_err(&dd->pdev->dev,
                "Timed out waiting for FTL rebuild to complete (%d secs).\n",
                jiffies_to_msecs(jiffies - start) / 1000);
-               return -EFAULT;
-       }
+       return -EFAULT;
+}
+
+/*
+ * service thread to issue queued commands
+ *
+ * @data Pointer to the driver data structure.
+ *
+ * return value
+ *     0
+ */
+
+static int mtip_service_thread(void *data)
+{
+       struct driver_data *dd = (struct driver_data *)data;
+       unsigned long slot, slot_start, slot_wrap;
+       unsigned int num_cmd_slots = dd->slot_groups * 32;
+       struct mtip_port *port = dd->port;
+
+       while (1) {
+               /*
+                * the condition is to check neither an internal command is
+                * is in progress nor error handling is active
+                */
+               wait_event_interruptible(port->svc_wait, (port->flags) &&
+                       !(port->flags & MTIP_PF_PAUSE_IO));
+
+               if (kthread_should_stop())
+                       break;
+
+               if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
+                               &dd->dd_flag)))
+                       break;
+
+               set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
+               if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
+                       slot = 1;
+                       /* used to restrict the loop to one iteration */
+                       slot_start = num_cmd_slots;
+                       slot_wrap = 0;
+                       while (1) {
+                               slot = find_next_bit(port->cmds_to_issue,
+                                               num_cmd_slots, slot);
+                               if (slot_wrap == 1) {
+                                       if ((slot_start >= slot) ||
+                                               (slot >= num_cmd_slots))
+                                               break;
+                               }
+                               if (unlikely(slot_start == num_cmd_slots))
+                                       slot_start = slot;
+
+                               if (unlikely(slot == num_cmd_slots)) {
+                                       slot = 1;
+                                       slot_wrap = 1;
+                                       continue;
+                               }
+
+                               /* Issue the command to the hardware */
+                               mtip_issue_ncq_command(port, slot);
+
+                               clear_bit(slot, port->cmds_to_issue);
+                       }
+
+                       clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
+               } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
+                       if (!mtip_ftl_rebuild_poll(dd))
+                               set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
+                                                       &dd->dd_flag);
+                       clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
+               }
+               clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
 
+               if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
+                       break;
+       }
        return 0;
 }
 
@@ -2434,11 +2850,14 @@ static int mtip_ftl_rebuild_poll(struct driver_data *dd)
  * return value
  *     0 on success, else an error code.
  */
-int mtip_hw_init(struct driver_data *dd)
+static int mtip_hw_init(struct driver_data *dd)
 {
        int i;
        int rv;
        unsigned int num_command_slots;
+       unsigned long timeout, timetaken;
+       unsigned char *buf;
+       struct smart_attr attr242;
 
        dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
 
@@ -2451,13 +2870,6 @@ int mtip_hw_init(struct driver_data *dd)
 
        hba_setup(dd);
 
-       /*
-        * Initialize the internal semaphore
-        * Use a rw semaphore to enable prioritization of
-        * mgmnt ioctl traffic during heavy IO load
-        */
-       init_rwsem(&dd->internal_sem);
-
        tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
 
        dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
@@ -2480,7 +2892,7 @@ int mtip_hw_init(struct driver_data *dd)
        /* Allocate memory for the command list. */
        dd->port->command_list =
                dmam_alloc_coherent(&dd->pdev->dev,
-                       HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
+                       HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
                        &dd->port->command_list_dma,
                        GFP_KERNEL);
        if (!dd->port->command_list) {
@@ -2493,7 +2905,7 @@ int mtip_hw_init(struct driver_data *dd)
        /* Clear the memory we have allocated. */
        memset(dd->port->command_list,
                0,
-               HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2));
+               HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
 
        /* Setup the addresse of the RX FIS. */
        dd->port->rxfis     = dd->port->command_list + HW_CMD_SLOT_SZ;
@@ -2509,10 +2921,19 @@ int mtip_hw_init(struct driver_data *dd)
        dd->port->identify_dma = dd->port->command_tbl_dma +
                                        HW_CMD_TBL_AR_SZ;
 
-       /* Setup the address of the sector buffer. */
+       /* Setup the address of the sector buffer - for some non-ncq cmds */
        dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
        dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
 
+       /* Setup the address of the log buf - for read log command */
+       dd->port->log_buf = (void *)dd->port->sector_buffer  + ATA_SECT_SIZE;
+       dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
+
+       /* Setup the address of the smart buf - for smart read data command */
+       dd->port->smart_buf = (void *)dd->port->log_buf  + ATA_SECT_SIZE;
+       dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
+
+
        /* Point the command headers at the command tables. */
        for (i = 0; i < num_command_slots; i++) {
                dd->port->commands[i].command_header =
@@ -2529,10 +2950,11 @@ int mtip_hw_init(struct driver_data *dd)
 
                if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
                        dd->port->commands[i].command_header->ctbau =
-                       cpu_to_le32(
+                       __force_bit2int cpu_to_le32(
                        (dd->port->commands[i].command_dma >> 16) >> 16);
-               dd->port->commands[i].command_header->ctba = cpu_to_le32(
-                       dd->port->commands[i].command_dma & 0xffffffff);
+               dd->port->commands[i].command_header->ctba =
+                       __force_bit2int cpu_to_le32(
+                       dd->port->commands[i].command_dma & 0xFFFFFFFF);
 
                /*
                 * If this is not done, a bug is reported by the stock
@@ -2555,14 +2977,43 @@ int mtip_hw_init(struct driver_data *dd)
                        dd->port->mmio + i*0x80 + PORT_SDBV;
        }
 
-       /* Reset the HBA. */
-       if (mtip_hba_reset(dd) < 0) {
-               dev_err(&dd->pdev->dev,
-                       "Card did not reset within timeout\n");
-               rv = -EIO;
+       timetaken = jiffies;
+       timeout = jiffies + msecs_to_jiffies(30000);
+       while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
+                time_before(jiffies, timeout)) {
+               mdelay(100);
+       }
+       if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
+               timetaken = jiffies - timetaken;
+               dev_warn(&dd->pdev->dev,
+                       "Surprise removal detected at %u ms\n",
+                       jiffies_to_msecs(timetaken));
+               rv = -ENODEV;
+               goto out2 ;
+       }
+       if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
+               timetaken = jiffies - timetaken;
+               dev_warn(&dd->pdev->dev,
+                       "Removal detected at %u ms\n",
+                       jiffies_to_msecs(timetaken));
+               rv = -EFAULT;
                goto out2;
        }
 
+       /* Conditionally reset the HBA. */
+       if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
+               if (mtip_hba_reset(dd) < 0) {
+                       dev_err(&dd->pdev->dev,
+                               "Card did not reset within timeout\n");
+                       rv = -EIO;
+                       goto out2;
+               }
+       } else {
+               /* Clear any pending interrupts on the HBA */
+               writel(readl(dd->mmio + HOST_IRQ_STAT),
+                       dd->mmio + HOST_IRQ_STAT);
+       }
+
        mtip_init_port(dd->port);
        mtip_start_port(dd->port);
 
@@ -2585,21 +3036,67 @@ int mtip_hw_init(struct driver_data *dd)
                                        dd->mmio + HOST_CTL);
 
        init_timer(&dd->port->cmd_timer);
+       init_waitqueue_head(&dd->port->svc_wait);
+
        dd->port->cmd_timer.data = (unsigned long int) dd->port;
        dd->port->cmd_timer.function = mtip_timeout_function;
        mod_timer(&dd->port->cmd_timer,
                jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
 
+
+       if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
+               rv = -EFAULT;
+               goto out3;
+       }
+
        if (mtip_get_identify(dd->port, NULL) < 0) {
                rv = -EFAULT;
                goto out3;
        }
-       mtip_dump_identify(dd->port);
 
        if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
                MTIP_FTL_REBUILD_MAGIC) {
-               return mtip_ftl_rebuild_poll(dd);
+               set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
+               return MTIP_FTL_REBUILD_MAGIC;
+       }
+       mtip_dump_identify(dd->port);
+
+       /* check write protect, over temp and rebuild statuses */
+       rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
+                               dd->port->log_buf,
+                               dd->port->log_buf_dma, 1);
+       if (rv) {
+               dev_warn(&dd->pdev->dev,
+                       "Error in READ LOG EXT (10h) command\n");
+               /* non-critical error, don't fail the load */
+       } else {
+               buf = (unsigned char *)dd->port->log_buf;
+               if (buf[259] & 0x1) {
+                       dev_info(&dd->pdev->dev,
+                               "Write protect bit is set.\n");
+                       set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
+               }
+               if (buf[288] == 0xF7) {
+                       dev_info(&dd->pdev->dev,
+                               "Exceeded Tmax, drive in thermal shutdown.\n");
+                       set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
+               }
+               if (buf[288] == 0xBF) {
+                       dev_info(&dd->pdev->dev,
+                               "Drive indicates rebuild has failed.\n");
+                       /* TODO */
+               }
        }
+
+       /* get write protect progess */
+       memset(&attr242, 0, sizeof(struct smart_attr));
+       if (mtip_get_smart_attr(dd->port, 242, &attr242))
+               dev_warn(&dd->pdev->dev,
+                               "Unable to check write protect progress\n");
+       else
+               dev_info(&dd->pdev->dev,
+                               "Write protect progress: %d%% (%d blocks)\n",
+                               attr242.cur, attr242.data);
        return rv;
 
 out3:
@@ -2617,7 +3114,7 @@ out2:
 
        /* Free the command/command header memory. */
        dmam_free_coherent(&dd->pdev->dev,
-                               HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
+                               HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
                                dd->port->command_list,
                                dd->port->command_list_dma);
 out1:
@@ -2635,15 +3132,18 @@ out1:
  * return value
  *     0
  */
-int mtip_hw_exit(struct driver_data *dd)
+static int mtip_hw_exit(struct driver_data *dd)
 {
        /*
         * Send standby immediate (E0h) to the drive so that it
         * saves its state.
         */
-       if (atomic_read(&dd->drv_cleanup_done) != true) {
+       if (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
 
-               mtip_standby_immediate(dd->port);
+               if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags))
+                       if (mtip_standby_immediate(dd->port))
+                               dev_warn(&dd->pdev->dev,
+                                       "STANDBY IMMEDIATE failed\n");
 
                /* de-initialize the port. */
                mtip_deinit_port(dd->port);
@@ -2655,15 +3155,15 @@ int mtip_hw_exit(struct driver_data *dd)
 
        del_timer_sync(&dd->port->cmd_timer);
 
-       /* Stop the bottom half tasklet. */
-       tasklet_kill(&dd->tasklet);
-
        /* Release the IRQ. */
        devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
 
+       /* Stop the bottom half tasklet. */
+       tasklet_kill(&dd->tasklet);
+
        /* Free the command/command header memory. */
        dmam_free_coherent(&dd->pdev->dev,
-                       HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
+                       HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
                        dd->port->command_list,
                        dd->port->command_list_dma);
        /* Free the memory allocated for the for structure. */
@@ -2683,7 +3183,7 @@ int mtip_hw_exit(struct driver_data *dd)
  * return value
  *     0
  */
-int mtip_hw_shutdown(struct driver_data *dd)
+static int mtip_hw_shutdown(struct driver_data *dd)
 {
        /*
         * Send standby immediate (E0h) to the drive so that it
@@ -2706,7 +3206,7 @@ int mtip_hw_shutdown(struct driver_data *dd)
  *     0       Suspend was successful
  *     -EFAULT Suspend was not successful
  */
-int mtip_hw_suspend(struct driver_data *dd)
+static int mtip_hw_suspend(struct driver_data *dd)
 {
        /*
         * Send standby immediate (E0h) to the drive
@@ -2738,7 +3238,7 @@ int mtip_hw_suspend(struct driver_data *dd)
  *     0       Resume was successful
  *      -EFAULT Resume was not successful
  */
-int mtip_hw_resume(struct driver_data *dd)
+static int mtip_hw_resume(struct driver_data *dd)
 {
        /* Perform any needed hardware setup steps */
        hba_setup(dd);
@@ -2765,50 +3265,6 @@ int mtip_hw_resume(struct driver_data *dd)
 }
 
 /*
- * This function is called for clean the pending command in the
- * command slot during the surprise removal of device and return
- * error to the upper layer.
- *
- * @dd Pointer to the DRIVER_DATA structure.
- *
- * return value
- *     None
- */
-void mtip_command_cleanup(struct driver_data *dd)
-{
-       int group = 0, commandslot = 0, commandindex = 0;
-       struct mtip_cmd *command;
-       struct mtip_port *port = dd->port;
-
-       for (group = 0; group < 4; group++) {
-               for (commandslot = 0; commandslot < 32; commandslot++) {
-                       if (!(port->allocated[group] & (1 << commandslot)))
-                               continue;
-
-                       commandindex = group << 5 | commandslot;
-                       command = &port->commands[commandindex];
-
-                       if (atomic_read(&command->active)
-                           && (command->async_callback)) {
-                               command->async_callback(command->async_data,
-                                       -ENODEV);
-                               command->async_callback = NULL;
-                               command->async_data = NULL;
-                       }
-
-                       dma_unmap_sg(&port->dd->pdev->dev,
-                               command->sg,
-                               command->scatter_ents,
-                               command->direction);
-               }
-       }
-
-       up(&port->cmd_slot);
-
-       atomic_set(&dd->drv_cleanup_done, true);
-}
-
-/*
  * Helper function for reusing disk name
  * upon hot insertion.
  */
@@ -2865,14 +3321,18 @@ static int mtip_block_ioctl(struct block_device *dev,
        if (!dd)
                return -ENOTTY;
 
+       if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
+               return -ENOTTY;
+
        switch (cmd) {
        case BLKFLSBUF:
-               return 0;
+               return -ENOTTY;
        default:
-               return mtip_hw_ioctl(dd, cmd, arg, 0);
+               return mtip_hw_ioctl(dd, cmd, arg);
        }
 }
 
+#ifdef CONFIG_COMPAT
 /*
  * Block layer compat IOCTL handler.
  *
@@ -2899,13 +3359,56 @@ static int mtip_block_compat_ioctl(struct block_device *dev,
        if (!dd)
                return -ENOTTY;
 
+       if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
+               return -ENOTTY;
+
        switch (cmd) {
        case BLKFLSBUF:
-               return 0;
+               return -ENOTTY;
+       case HDIO_DRIVE_TASKFILE: {
+               struct mtip_compat_ide_task_request_s __user *compat_req_task;
+               ide_task_request_t req_task;
+               int compat_tasksize, outtotal, ret;
+
+               compat_tasksize =
+                       sizeof(struct mtip_compat_ide_task_request_s);
+
+               compat_req_task =
+                       (struct mtip_compat_ide_task_request_s __user *) arg;
+
+               if (copy_from_user(&req_task, (void __user *) arg,
+                       compat_tasksize - (2 * sizeof(compat_long_t))))
+                       return -EFAULT;
+
+               if (get_user(req_task.out_size, &compat_req_task->out_size))
+                       return -EFAULT;
+
+               if (get_user(req_task.in_size, &compat_req_task->in_size))
+                       return -EFAULT;
+
+               outtotal = sizeof(struct mtip_compat_ide_task_request_s);
+
+               ret = exec_drive_taskfile(dd, (void __user *) arg,
+                                               &req_task, outtotal);
+
+               if (copy_to_user((void __user *) arg, &req_task,
+                               compat_tasksize -
+                               (2 * sizeof(compat_long_t))))
+                       return -EFAULT;
+
+               if (put_user(req_task.out_size, &compat_req_task->out_size))
+                       return -EFAULT;
+
+               if (put_user(req_task.in_size, &compat_req_task->in_size))
+                       return -EFAULT;
+
+               return ret;
+       }
        default:
-               return mtip_hw_ioctl(dd, cmd, arg, 1);
+               return mtip_hw_ioctl(dd, cmd, arg);
        }
 }
+#endif
 
 /*
  * Obtain the geometry of the device.
@@ -2942,12 +3445,8 @@ static int mtip_block_getgeo(struct block_device *dev,
 
        geo->heads = 224;
        geo->sectors = 56;
-#if BITS_PER_LONG == 64
-       geo->cylinders = capacity / (geo->heads * geo->sectors);
-#else
-       do_div(capacity, (geo->heads * geo->sectors));
+       sector_div(capacity, (geo->heads * geo->sectors));
        geo->cylinders = capacity;
-#endif
        return 0;
 }
 
@@ -2959,7 +3458,9 @@ static int mtip_block_getgeo(struct block_device *dev,
  */
 static const struct block_device_operations mtip_block_ops = {
        .ioctl          = mtip_block_ioctl,
+#ifdef CONFIG_COMPAT
        .compat_ioctl   = mtip_block_compat_ioctl,
+#endif
        .getgeo         = mtip_block_getgeo,
        .owner          = THIS_MODULE
 };
@@ -2974,10 +3475,8 @@ static const struct block_device_operations mtip_block_ops = {
  *              the driver data structure.
  * @bio   Pointer to the BIO.
  *
- * return value
- *     0
  */
-static int mtip_make_request(struct request_queue *queue, struct bio *bio)
+static void mtip_make_request(struct request_queue *queue, struct bio *bio)
 {
        struct driver_data *dd = queue->queuedata;
        struct scatterlist *sg;
@@ -2985,15 +3484,28 @@ static int mtip_make_request(struct request_queue *queue, struct bio *bio)
        int nents = 0;
        int tag = 0;
 
+       if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
+               if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
+                                                       &dd->dd_flag))) {
+                       bio_endio(bio, -ENXIO);
+                       return;
+               }
+               if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
+                       bio_endio(bio, -ENODATA);
+                       return;
+               }
+               if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
+                                                       &dd->dd_flag) &&
+                               bio_data_dir(bio))) {
+                       bio_endio(bio, -ENODATA);
+                       return;
+               }
+       }
+
        if (unlikely(!bio_has_data(bio))) {
                blk_queue_flush(queue, 0);
                bio_endio(bio, 0);
-               return 0;
-       }
-
-       if (unlikely(atomic_read(&dd->eh_active))) {
-               bio_endio(bio, -EBUSY);
-               return 0;
+               return;
        }
 
        sg = mtip_hw_get_scatterlist(dd, &tag);
@@ -3002,10 +3514,10 @@ static int mtip_make_request(struct request_queue *queue, struct bio *bio)
 
                if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
                        dev_warn(&dd->pdev->dev,
-                               "Maximum number of SGL entries exceeded");
+                               "Maximum number of SGL entries exceeded\n");
                        bio_io_error(bio);
                        mtip_hw_release_scatterlist(dd, tag);
-                       return 0;
+                       return;
                }
 
                /* Create the scatter list for this bio. */
@@ -3024,13 +3536,9 @@ static int mtip_make_request(struct request_queue *queue, struct bio *bio)
                                tag,
                                bio_endio,
                                bio,
-                               bio->bi_rw & REQ_FLUSH,
                                bio_data_dir(bio));
-       } else {
+       } else
                bio_io_error(bio);
-       }
-
-       return 0;
 }
 
 /*
@@ -3044,40 +3552,26 @@ static int mtip_make_request(struct request_queue *queue, struct bio *bio)
  * return value
  *     0 on success else an error code.
  */
-int mtip_block_initialize(struct driver_data *dd)
+static int mtip_block_initialize(struct driver_data *dd)
 {
-       int rv = 0;
+       int rv = 0, wait_for_rebuild = 0;
        sector_t capacity;
        unsigned int index = 0;
        struct kobject *kobj;
+       unsigned char thd_name[16];
+
+       if (dd->disk)
+               goto skip_create_disk; /* hw init done, before rebuild */
 
        /* Initialize the protocol layer. */
-       rv = mtip_hw_init(dd);
-       if (rv < 0) {
+       wait_for_rebuild = mtip_hw_init(dd);
+       if (wait_for_rebuild < 0) {
                dev_err(&dd->pdev->dev,
                        "Protocol layer initialization failed\n");
                rv = -EINVAL;
                goto protocol_init_error;
        }
 
-       /* Allocate the request queue. */
-       dd->queue = blk_alloc_queue(GFP_KERNEL);
-       if (dd->queue == NULL) {
-               dev_err(&dd->pdev->dev,
-                       "Unable to allocate request queue\n");
-               rv = -ENOMEM;
-               goto block_queue_alloc_init_error;
-       }
-
-       /* Attach our request function to the request queue. */
-       blk_queue_make_request(dd->queue, mtip_make_request);
-
-       /* Set device limits. */
-       set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
-       blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
-       blk_queue_physical_block_size(dd->queue, 4096);
-       blk_queue_io_min(dd->queue, 4096);
-
        dd->disk = alloc_disk(MTIP_MAX_MINORS);
        if (dd->disk  == NULL) {
                dev_err(&dd->pdev->dev,
@@ -3110,11 +3604,43 @@ int mtip_block_initialize(struct driver_data *dd)
        dd->disk->major         = dd->major;
        dd->disk->first_minor   = dd->instance * MTIP_MAX_MINORS;
        dd->disk->fops          = &mtip_block_ops;
-       dd->disk->queue         = dd->queue;
        dd->disk->private_data  = dd;
-       dd->queue->queuedata    = dd;
        dd->index               = index;
 
+       /*
+        * if rebuild pending, start the service thread, and delay the block
+        * queue creation and add_disk()
+        */
+       if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
+               goto start_service_thread;
+
+skip_create_disk:
+       /* Allocate the request queue. */
+       dd->queue = blk_alloc_queue(GFP_KERNEL);
+       if (dd->queue == NULL) {
+               dev_err(&dd->pdev->dev,
+                       "Unable to allocate request queue\n");
+               rv = -ENOMEM;
+               goto block_queue_alloc_init_error;
+       }
+
+       /* Attach our request function to the request queue. */
+       blk_queue_make_request(dd->queue, mtip_make_request);
+
+       dd->disk->queue         = dd->queue;
+       dd->queue->queuedata    = dd;
+
+       /* Set device limits. */
+       set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
+       blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
+       blk_queue_physical_block_size(dd->queue, 4096);
+       blk_queue_io_min(dd->queue, 4096);
+       /*
+        * write back cache is not supported in the device. FUA depends on
+        * write back cache support, hence setting flush support to zero.
+        */
+       blk_queue_flush(dd->queue, 0);
+
        /* Set the capacity of the device in 512 byte sectors. */
        if (!(mtip_hw_get_capacity(dd, &capacity))) {
                dev_warn(&dd->pdev->dev,
@@ -3137,15 +3663,37 @@ int mtip_block_initialize(struct driver_data *dd)
                kobject_put(kobj);
        }
 
+       if (dd->mtip_svc_handler) {
+               set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
+               return rv; /* service thread created for handling rebuild */
+       }
+
+start_service_thread:
+       sprintf(thd_name, "mtip_svc_thd_%02d", index);
+
+       dd->mtip_svc_handler = kthread_run(mtip_service_thread,
+                                               dd, thd_name);
+
+       if (IS_ERR(dd->mtip_svc_handler)) {
+               dev_err(&dd->pdev->dev, "service thread failed to start\n");
+               dd->mtip_svc_handler = NULL;
+               rv = -EFAULT;
+               goto kthread_run_error;
+       }
+
+       if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
+               rv = wait_for_rebuild;
+
        return rv;
 
-read_capacity_error:
-       /*
-        * Delete our gendisk structure. This also removes the device
-        * from /dev
-        */
+kthread_run_error:
+       /* Delete our gendisk. This also removes the device from /dev */
        del_gendisk(dd->disk);
 
+read_capacity_error:
+       blk_cleanup_queue(dd->queue);
+
+block_queue_alloc_init_error:
 disk_index_error:
        spin_lock(&rssd_index_lock);
        ida_remove(&rssd_index_ida, index);
@@ -3155,11 +3703,7 @@ ida_get_error:
        put_disk(dd->disk);
 
 alloc_disk_error:
-       blk_cleanup_queue(dd->queue);
-
-block_queue_alloc_init_error:
-       /* De-initialize the protocol layer. */
-       mtip_hw_exit(dd);
+       mtip_hw_exit(dd); /* De-initialize the protocol layer. */
 
 protocol_init_error:
        return rv;
@@ -3175,14 +3719,23 @@ protocol_init_error:
  * return value
  *     0
  */
-int mtip_block_remove(struct driver_data *dd)
+static int mtip_block_remove(struct driver_data *dd)
 {
        struct kobject *kobj;
-       /* Clean up the sysfs attributes managed by the protocol layer. */
-       kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
-       if (kobj) {
-               mtip_hw_sysfs_exit(dd, kobj);
-               kobject_put(kobj);
+
+       if (dd->mtip_svc_handler) {
+               set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
+               wake_up_interruptible(&dd->port->svc_wait);
+               kthread_stop(dd->mtip_svc_handler);
+       }
+
+       /* Clean up the sysfs attributes, if created */
+       if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
+               kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
+               if (kobj) {
+                       mtip_hw_sysfs_exit(dd, kobj);
+                       kobject_put(kobj);
+               }
        }
 
        /*
@@ -3190,6 +3743,11 @@ int mtip_block_remove(struct driver_data *dd)
         * from /dev
         */
        del_gendisk(dd->disk);
+
+       spin_lock(&rssd_index_lock);
+       ida_remove(&rssd_index_ida, dd->index);
+       spin_unlock(&rssd_index_lock);
+
        blk_cleanup_queue(dd->queue);
        dd->disk  = NULL;
        dd->queue = NULL;
@@ -3212,13 +3770,18 @@ int mtip_block_remove(struct driver_data *dd)
  * return value
  *     0
  */
-int mtip_block_shutdown(struct driver_data *dd)
+static int mtip_block_shutdown(struct driver_data *dd)
 {
        dev_info(&dd->pdev->dev,
                "Shutting down %s ...\n", dd->disk->disk_name);
 
        /* Delete our gendisk structure, and cleanup the blk queue. */
        del_gendisk(dd->disk);
+
+       spin_lock(&rssd_index_lock);
+       ida_remove(&rssd_index_ida, dd->index);
+       spin_unlock(&rssd_index_lock);
+
        blk_cleanup_queue(dd->queue);
        dd->disk  = NULL;
        dd->queue = NULL;
@@ -3227,7 +3790,7 @@ int mtip_block_shutdown(struct driver_data *dd)
        return 0;
 }
 
-int mtip_block_suspend(struct driver_data *dd)
+static int mtip_block_suspend(struct driver_data *dd)
 {
        dev_info(&dd->pdev->dev,
                "Suspending %s ...\n", dd->disk->disk_name);
@@ -3235,7 +3798,7 @@ int mtip_block_suspend(struct driver_data *dd)
        return 0;
 }
 
-int mtip_block_resume(struct driver_data *dd)
+static int mtip_block_resume(struct driver_data *dd)
 {
        dev_info(&dd->pdev->dev, "Resuming %s ...\n",
                dd->disk->disk_name);
@@ -3266,12 +3829,6 @@ static int mtip_pci_probe(struct pci_dev *pdev,
                return -ENOMEM;
        }
 
-       /* Set the atomic variable as 1 in case of SRSI */
-       atomic_set(&dd->drv_cleanup_done, true);
-
-       atomic_set(&dd->resumeflag, false);
-       atomic_set(&dd->eh_active, 0);
-
        /* Attach the private data to this PCI device.  */
        pci_set_drvdata(pdev, dd);
 
@@ -3312,7 +3869,6 @@ static int mtip_pci_probe(struct pci_dev *pdev,
 
        /* Copy the info we may need later into the private data structure. */
        dd->major       = mtip_major;
-       dd->protocol    = ent->driver_data;
        dd->instance    = instance;
        dd->pdev        = pdev;
 
@@ -3329,7 +3885,8 @@ static int mtip_pci_probe(struct pci_dev *pdev,
         * instance number.
         */
        instance++;
-
+       if (rv != MTIP_FTL_REBUILD_MAGIC)
+               set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
        goto done;
 
 block_initialize_err:
@@ -3343,9 +3900,6 @@ iomap_err:
        pci_set_drvdata(pdev, NULL);
        return rv;
 done:
-       /* Set the atomic variable as 0 in case of SRSI */
-       atomic_set(&dd->drv_cleanup_done, true);
-
        return rv;
 }
 
@@ -3361,8 +3915,10 @@ static void mtip_pci_remove(struct pci_dev *pdev)
        struct driver_data *dd = pci_get_drvdata(pdev);
        int counter = 0;
 
+       set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
+
        if (mtip_check_surprise_removal(pdev)) {
-               while (atomic_read(&dd->drv_cleanup_done) == false) {
+               while (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
                        counter++;
                        msleep(20);
                        if (counter == 10) {
@@ -3372,8 +3928,6 @@ static void mtip_pci_remove(struct pci_dev *pdev)
                        }
                }
        }
-       /* Set the atomic variable as 1 in case of SRSI */
-       atomic_set(&dd->drv_cleanup_done, true);
 
        /* Clean up the block layer. */
        mtip_block_remove(dd);
@@ -3402,7 +3956,7 @@ static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
                return -EFAULT;
        }
 
-       atomic_set(&dd->resumeflag, true);
+       set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
 
        /* Disable ports & interrupts then send standby immediate */
        rv = mtip_block_suspend(dd);
@@ -3468,7 +4022,7 @@ static int mtip_pci_resume(struct pci_dev *pdev)
                dev_err(&pdev->dev, "Unable to resume\n");
 
 err:
-       atomic_set(&dd->resumeflag, false);
+       clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
 
        return rv;
 }
@@ -3486,28 +4040,6 @@ static void mtip_pci_shutdown(struct pci_dev *pdev)
                mtip_block_shutdown(dd);
 }
 
-/*
- * This function check_for_surprise_removal is called
- * while card is removed from the system and it will
- * read the vendor id from the configration space
- *
- * @pdev Pointer to the pci_dev structure.
- *
- * return value
- *      true if device removed, else false
- */
-bool mtip_check_surprise_removal(struct pci_dev *pdev)
-{
-       u16 vendor_id = 0;
-
-       /* Read the vendorID from the configuration space */
-       pci_read_config_word(pdev, 0x00, &vendor_id);
-       if (vendor_id == 0xFFFF)
-               return true; /* device removed */
-
-       return false; /* device present */
-}
-
 /* Table of device ids supported by this driver. */
 static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
        {  PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320_DEVICE_ID) },
@@ -3515,7 +4047,7 @@ static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
 };
 
 /* Structure that describes the PCI driver functions. */
-struct pci_driver mtip_pci_driver = {
+static struct pci_driver mtip_pci_driver = {
        .name                   = MTIP_DRV_NAME,
        .id_table               = mtip_pci_tbl,
        .probe                  = mtip_pci_probe,
@@ -3539,18 +4071,25 @@ MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
  */
 static int __init mtip_init(void)
 {
+       int error;
+
        printk(KERN_INFO MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
 
        /* Allocate a major block device number to use with this driver. */
-       mtip_major = register_blkdev(0, MTIP_DRV_NAME);
-       if (mtip_major < 0) {
+       error = register_blkdev(0, MTIP_DRV_NAME);
+       if (error <= 0) {
                printk(KERN_ERR "Unable to register block device (%d)\n",
-               mtip_major);
+               error);
                return -EBUSY;
        }
+       mtip_major = error;
 
        /* Register our PCI operations. */
-       return pci_register_driver(&mtip_pci_driver);
+       error = pci_register_driver(&mtip_pci_driver);
+       if (error)
+               unregister_blkdev(mtip_major, MTIP_DRV_NAME);
+
+       return error;
 }
 
 /*