[SCSI] aacraid: add parameter to control FUA and SYNCHRONIZE_CACHE policy
[safe/jmp/linux-2.6] / drivers / scsi / aacraid / commsup.c
index 9aca57e..81cdac1 100644 (file)
@@ -80,7 +80,11 @@ static int fib_map_alloc(struct aac_dev *dev)
 
 void aac_fib_map_free(struct aac_dev *dev)
 {
-       pci_free_consistent(dev->pdev, dev->max_fib_size * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB), dev->hw_fib_va, dev->hw_fib_pa);
+       pci_free_consistent(dev->pdev,
+         dev->max_fib_size * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB),
+         dev->hw_fib_va, dev->hw_fib_pa);
+       dev->hw_fib_va = NULL;
+       dev->hw_fib_pa = 0;
 }
 
 /**
@@ -792,13 +796,13 @@ static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
         */
        switch (le32_to_cpu(aifcmd->command)) {
        case AifCmdDriverNotify:
-               switch (le32_to_cpu(((u32 *)aifcmd->data)[0])) {
+               switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
                /*
                 *      Morph or Expand complete
                 */
                case AifDenMorphComplete:
                case AifDenVolumeExtendComplete:
-                       container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
+                       container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
                        if (container >= dev->maximum_num_containers)
                                break;
 
@@ -831,25 +835,29 @@ static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
                        if (container >= dev->maximum_num_containers)
                                break;
                        if ((dev->fsa_dev[container].config_waiting_on ==
-                           le32_to_cpu(*(u32 *)aifcmd->data)) &&
+                           le32_to_cpu(*(__le32 *)aifcmd->data)) &&
                         time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
                                dev->fsa_dev[container].config_waiting_on = 0;
                } else for (container = 0;
                    container < dev->maximum_num_containers; ++container) {
                        if ((dev->fsa_dev[container].config_waiting_on ==
-                           le32_to_cpu(*(u32 *)aifcmd->data)) &&
+                           le32_to_cpu(*(__le32 *)aifcmd->data)) &&
                         time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
                                dev->fsa_dev[container].config_waiting_on = 0;
                }
                break;
 
        case AifCmdEventNotify:
-               switch (le32_to_cpu(((u32 *)aifcmd->data)[0])) {
+               switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
+               case AifEnBatteryEvent:
+                       dev->cache_protected =
+                               (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
+                       break;
                /*
                 *      Add an Array.
                 */
                case AifEnAddContainer:
-                       container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
+                       container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
                        if (container >= dev->maximum_num_containers)
                                break;
                        dev->fsa_dev[container].config_needed = ADD;
@@ -862,7 +870,7 @@ static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
                 *      Delete an Array.
                 */
                case AifEnDeleteContainer:
-                       container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
+                       container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
                        if (container >= dev->maximum_num_containers)
                                break;
                        dev->fsa_dev[container].config_needed = DELETE;
@@ -876,7 +884,7 @@ static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
                 * waiting on something else, setup to wait on a Config Change.
                 */
                case AifEnContainerChange:
-                       container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
+                       container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
                        if (container >= dev->maximum_num_containers)
                                break;
                        if (dev->fsa_dev[container].config_waiting_on &&
@@ -901,13 +909,13 @@ static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
                        if (container >= dev->maximum_num_containers)
                                break;
                        if ((dev->fsa_dev[container].config_waiting_on ==
-                           le32_to_cpu(*(u32 *)aifcmd->data)) &&
+                           le32_to_cpu(*(__le32 *)aifcmd->data)) &&
                         time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
                                dev->fsa_dev[container].config_waiting_on = 0;
                } else for (container = 0;
                    container < dev->maximum_num_containers; ++container) {
                        if ((dev->fsa_dev[container].config_waiting_on ==
-                           le32_to_cpu(*(u32 *)aifcmd->data)) &&
+                           le32_to_cpu(*(__le32 *)aifcmd->data)) &&
                         time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
                                dev->fsa_dev[container].config_waiting_on = 0;
                }
@@ -922,9 +930,9 @@ static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
                 * wait for a container change.
                 */
 
-               if ((((u32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero))
-                && ((((u32 *)aifcmd->data)[6] == ((u32 *)aifcmd->data)[5])
-                 || (((u32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess)))) {
+               if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
+                   (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] ||
+                    ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) {
                        for (container = 0;
                            container < dev->maximum_num_containers;
                            ++container) {
@@ -939,9 +947,9 @@ static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
                                        jiffies;
                        }
                }
-               if ((((u32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero))
-                && (((u32 *)aifcmd->data)[6] == 0)
-                && (((u32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning))) {
+               if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
+                   ((__le32 *)aifcmd->data)[6] == 0 &&
+                   ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
                        for (container = 0;
                            container < dev->maximum_num_containers;
                            ++container) {
@@ -1021,7 +1029,7 @@ static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
 
 }
 
-static int _aac_reset_adapter(struct aac_dev *aac)
+static int _aac_reset_adapter(struct aac_dev *aac, int forced)
 {
        int index, quirks;
        int retval;
@@ -1029,25 +1037,32 @@ static int _aac_reset_adapter(struct aac_dev *aac)
        struct scsi_device *dev;
        struct scsi_cmnd *command;
        struct scsi_cmnd *command_list;
+       int jafo = 0;
 
        /*
         * Assumptions:
-        *      - host is locked.
+        *      - host is locked, unless called by the aacraid thread.
+        *        (a matter of convenience, due to legacy issues surrounding
+        *        eh_host_adapter_reset).
         *      - in_reset is asserted, so no new i/o is getting to the
         *        card.
-        *      - The card is dead.
+        *      - The card is dead, or will be very shortly ;-/ so no new
+        *        commands are completing in the interrupt service.
         */
        host = aac->scsi_host_ptr;
        scsi_block_requests(host);
        aac_adapter_disable_int(aac);
-       spin_unlock_irq(host->host_lock);
-       kthread_stop(aac->thread);
+       if (aac->thread->pid != current->pid) {
+               spin_unlock_irq(host->host_lock);
+               kthread_stop(aac->thread);
+               jafo = 1;
+       }
 
        /*
         *      If a positive health, means in a known DEAD PANIC
         * state and the adapter could be reset to `try again'.
         */
-       retval = aac_adapter_restart(aac, aac_adapter_check_health(aac));
+       retval = aac_adapter_restart(aac, forced ? 0 : aac_adapter_check_health(aac));
 
        if (retval)
                goto out;
@@ -1080,8 +1095,6 @@ static int _aac_reset_adapter(struct aac_dev *aac)
         * case.
         */
        aac_fib_map_free(aac);
-       aac->hw_fib_va = NULL;
-       aac->hw_fib_pa = 0;
        pci_free_consistent(aac->pdev, aac->comm_size, aac->comm_addr, aac->comm_phys);
        aac->comm_addr = NULL;
        aac->comm_phys = 0;
@@ -1090,27 +1103,29 @@ static int _aac_reset_adapter(struct aac_dev *aac)
        free_irq(aac->pdev->irq, aac);
        kfree(aac->fsa_dev);
        aac->fsa_dev = NULL;
-       if (aac_get_driver_ident(index)->quirks & AAC_QUIRK_31BIT) {
-               if (((retval = pci_set_dma_mask(aac->pdev, DMA_32BIT_MASK))) ||
-                 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_32BIT_MASK))))
+       quirks = aac_get_driver_ident(index)->quirks;
+       if (quirks & AAC_QUIRK_31BIT) {
+               if (((retval = pci_set_dma_mask(aac->pdev, DMA_31BIT_MASK))) ||
+                 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_31BIT_MASK))))
                        goto out;
        } else {
-               if (((retval = pci_set_dma_mask(aac->pdev, 0x7FFFFFFFULL))) ||
-                 ((retval = pci_set_consistent_dma_mask(aac->pdev, 0x7FFFFFFFULL))))
+               if (((retval = pci_set_dma_mask(aac->pdev, DMA_32BIT_MASK))) ||
+                 ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_32BIT_MASK))))
                        goto out;
        }
        if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
                goto out;
-       if (aac_get_driver_ident(index)->quirks & AAC_QUIRK_31BIT)
+       if (quirks & AAC_QUIRK_31BIT)
                if ((retval = pci_set_dma_mask(aac->pdev, DMA_32BIT_MASK)))
                        goto out;
-       aac->thread = kthread_run(aac_command_thread, aac, aac->name);
-       if (IS_ERR(aac->thread)) {
-               retval = PTR_ERR(aac->thread);
-               goto out;
+       if (jafo) {
+               aac->thread = kthread_run(aac_command_thread, aac, aac->name);
+               if (IS_ERR(aac->thread)) {
+                       retval = PTR_ERR(aac->thread);
+                       goto out;
+               }
        }
        (void)aac_get_adapter_info(aac);
-       quirks = aac_get_driver_ident(index)->quirks;
        if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
                host->sg_tablesize = 34;
                host->max_sectors = (host->sg_tablesize * 8) + 112;
@@ -1150,7 +1165,99 @@ static int _aac_reset_adapter(struct aac_dev *aac)
 out:
        aac->in_reset = 0;
        scsi_unblock_requests(host);
-       spin_lock_irq(host->host_lock);
+       if (jafo) {
+               spin_lock_irq(host->host_lock);
+       }
+       return retval;
+}
+
+int aac_reset_adapter(struct aac_dev * aac, int forced)
+{
+       unsigned long flagv = 0;
+       int retval;
+       struct Scsi_Host * host;
+
+       if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
+               return -EBUSY;
+
+       if (aac->in_reset) {
+               spin_unlock_irqrestore(&aac->fib_lock, flagv);
+               return -EBUSY;
+       }
+       aac->in_reset = 1;
+       spin_unlock_irqrestore(&aac->fib_lock, flagv);
+
+       /*
+        * Wait for all commands to complete to this specific
+        * target (block maximum 60 seconds). Although not necessary,
+        * it does make us a good storage citizen.
+        */
+       host = aac->scsi_host_ptr;
+       scsi_block_requests(host);
+       if (forced < 2) for (retval = 60; retval; --retval) {
+               struct scsi_device * dev;
+               struct scsi_cmnd * command;
+               int active = 0;
+
+               __shost_for_each_device(dev, host) {
+                       spin_lock_irqsave(&dev->list_lock, flagv);
+                       list_for_each_entry(command, &dev->cmd_list, list) {
+                               if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
+                                       active++;
+                                       break;
+                               }
+                       }
+                       spin_unlock_irqrestore(&dev->list_lock, flagv);
+                       if (active)
+                               break;
+
+               }
+               /*
+                * We can exit If all the commands are complete
+                */
+               if (active == 0)
+                       break;
+               ssleep(1);
+       }
+
+       /* Quiesce build, flush cache, write through mode */
+       if (forced < 2)
+               aac_send_shutdown(aac);
+       spin_lock_irqsave(host->host_lock, flagv);
+       retval = _aac_reset_adapter(aac, forced ? forced : ((aac_check_reset != 0) && (aac_check_reset != 1)));
+       spin_unlock_irqrestore(host->host_lock, flagv);
+
+       if ((forced < 2) && (retval == -ENODEV)) {
+               /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
+               struct fib * fibctx = aac_fib_alloc(aac);
+               if (fibctx) {
+                       struct aac_pause *cmd;
+                       int status;
+
+                       aac_fib_init(fibctx);
+
+                       cmd = (struct aac_pause *) fib_data(fibctx);
+
+                       cmd->command = cpu_to_le32(VM_ContainerConfig);
+                       cmd->type = cpu_to_le32(CT_PAUSE_IO);
+                       cmd->timeout = cpu_to_le32(1);
+                       cmd->min = cpu_to_le32(1);
+                       cmd->noRescan = cpu_to_le32(1);
+                       cmd->count = cpu_to_le32(0);
+
+                       status = aac_fib_send(ContainerCommand,
+                         fibctx,
+                         sizeof(struct aac_pause),
+                         FsaNormal,
+                         -2 /* Timeout silently */, 1,
+                         NULL, NULL);
+
+                       if (status >= 0)
+                               aac_fib_complete(fibctx);
+                       aac_fib_free(fibctx);
+               }
+       }
+
        return retval;
 }
 
@@ -1237,10 +1344,10 @@ int aac_check_health(struct aac_dev * aac)
                        aif = (struct aac_aifcmd *)hw_fib->data;
                        aif->command = cpu_to_le32(AifCmdEventNotify);
                        aif->seqnum = cpu_to_le32(0xFFFFFFFF);
-                       aif->data[0] = cpu_to_le32(AifEnExpEvent);
-                       aif->data[1] = cpu_to_le32(AifExeFirmwarePanic);
-                       aif->data[2] = cpu_to_le32(AifHighPriority);
-                       aif->data[3] = cpu_to_le32(BlinkLED);
+                       aif->data[0] = AifEnExpEvent;
+                       aif->data[1] = AifExeFirmwarePanic;
+                       aif->data[2] = AifHighPriority;
+                       aif->data[3] = BlinkLED;
 
                        /*
                         * Put the FIB onto the
@@ -1270,10 +1377,17 @@ int aac_check_health(struct aac_dev * aac)
 
        printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
 
+       if (!aac_check_reset ||
+           ((aac_check_reset != 1) &&
+            (aac->supplement_adapter_info.SupportedOptions2 &
+             cpu_to_le32(AAC_OPTION_IGNORE_RESET))))
+               goto out;
        host = aac->scsi_host_ptr;
-       spin_lock_irqsave(host->host_lock, flagv);
-       BlinkLED = _aac_reset_adapter(aac);
-       spin_unlock_irqrestore(host->host_lock, flagv);
+       if (aac->thread->pid != current->pid)
+               spin_lock_irqsave(host->host_lock, flagv);
+       BlinkLED = _aac_reset_adapter(aac, aac_check_reset != 1);
+       if (aac->thread->pid != current->pid)
+               spin_unlock_irqrestore(host->host_lock, flagv);
        return BlinkLED;
 
 out:
@@ -1300,6 +1414,9 @@ int aac_command_thread(void *data)
        struct aac_fib_context *fibctx;
        unsigned long flags;
        DECLARE_WAITQUEUE(wait, current);
+       unsigned long next_jiffies = jiffies + HZ;
+       unsigned long next_check_jiffies = next_jiffies;
+       long difference = HZ;
 
        /*
         *      We can only have one thread per adapter for AIF's.
@@ -1368,7 +1485,7 @@ int aac_command_thread(void *data)
                                     cpu_to_le32(AifCmdJobProgress))) {
                                        aac_handle_aif(dev, fib);
                                }
-                               
+
                                time_now = jiffies/HZ;
 
                                /*
@@ -1507,11 +1624,79 @@ int aac_command_thread(void *data)
                 *      There are no more AIF's
                 */
                spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
-               schedule();
+
+               /*
+                *      Background activity
+                */
+               if ((time_before(next_check_jiffies,next_jiffies))
+                && ((difference = next_check_jiffies - jiffies) <= 0)) {
+                       next_check_jiffies = next_jiffies;
+                       if (aac_check_health(dev) == 0) {
+                               difference = ((long)(unsigned)check_interval)
+                                          * HZ;
+                               next_check_jiffies = jiffies + difference;
+                       } else if (!dev->queues)
+                               break;
+               }
+               if (!time_before(next_check_jiffies,next_jiffies)
+                && ((difference = next_jiffies - jiffies) <= 0)) {
+                       struct timeval now;
+                       int ret;
+
+                       /* Don't even try to talk to adapter if its sick */
+                       ret = aac_check_health(dev);
+                       if (!ret && !dev->queues)
+                               break;
+                       next_check_jiffies = jiffies
+                                          + ((long)(unsigned)check_interval)
+                                          * HZ;
+                       do_gettimeofday(&now);
+
+                       /* Synchronize our watches */
+                       if (((1000000 - (1000000 / HZ)) > now.tv_usec)
+                        && (now.tv_usec > (1000000 / HZ)))
+                               difference = (((1000000 - now.tv_usec) * HZ)
+                                 + 500000) / 1000000;
+                       else if (ret == 0) {
+                               struct fib *fibptr;
+
+                               if ((fibptr = aac_fib_alloc(dev))) {
+                                       __le32 *info;
+
+                                       aac_fib_init(fibptr);
+
+                                       info = (__le32 *) fib_data(fibptr);
+                                       if (now.tv_usec > 500000)
+                                               ++now.tv_sec;
+
+                                       *info = cpu_to_le32(now.tv_sec);
+
+                                       (void)aac_fib_send(SendHostTime,
+                                               fibptr,
+                                               sizeof(*info),
+                                               FsaNormal,
+                                               1, 1,
+                                               NULL,
+                                               NULL);
+                                       aac_fib_complete(fibptr);
+                                       aac_fib_free(fibptr);
+                               }
+                               difference = (long)(unsigned)update_interval*HZ;
+                       } else {
+                               /* retry shortly */
+                               difference = 10 * HZ;
+                       }
+                       next_jiffies = jiffies + difference;
+                       if (time_before(next_check_jiffies,next_jiffies))
+                               difference = next_check_jiffies - jiffies;
+               }
+               if (difference <= 0)
+                       difference = 1;
+               set_current_state(TASK_INTERRUPTIBLE);
+               schedule_timeout(difference);
 
                if (kthread_should_stop())
                        break;
-               set_current_state(TASK_INTERRUPTIBLE);
        }
        if (dev->queues)
                remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);