[SCSI] aacraid: Fix ioctl handling when adapter resets
[powerpc.git] / drivers / scsi / aacraid / commctrl.c
index 9f75144..3a8e7ca 100644 (file)
 #include <linux/kernel.h>
 #include <linux/init.h>
 #include <linux/types.h>
-#include <linux/sched.h>
 #include <linux/pci.h>
 #include <linux/spinlock.h>
 #include <linux/slab.h>
 #include <linux/completion.h>
 #include <linux/dma-mapping.h>
 #include <linux/blkdev.h>
-#include <linux/delay.h>
+#include <linux/delay.h> /* ssleep prototype */
 #include <linux/kthread.h>
 #include <asm/semaphore.h>
 #include <asm/uaccess.h>
@@ -65,12 +64,15 @@ static int ioctl_send_fib(struct aac_dev * dev, void __user *arg)
        unsigned size;
        int retval;
 
+       if (dev->in_reset) {
+               return -EBUSY;
+       }
        fibptr = aac_fib_alloc(dev);
        if(fibptr == NULL) {
                return -ENOMEM;
        }
                
-       kfib = fibptr->hw_fib;
+       kfib = fibptr->hw_fib_va;
        /*
         *      First copy in the header so that we can check the size field.
         */
@@ -92,9 +94,9 @@ static int ioctl_send_fib(struct aac_dev * dev, void __user *arg)
                        goto cleanup;
                }
                /* Highjack the hw_fib */
-               hw_fib = fibptr->hw_fib;
+               hw_fib = fibptr->hw_fib_va;
                hw_fib_pa = fibptr->hw_fib_pa;
-               fibptr->hw_fib = kfib = pci_alloc_consistent(dev->pdev, size, &fibptr->hw_fib_pa);
+               fibptr->hw_fib_va = kfib = pci_alloc_consistent(dev->pdev, size, &fibptr->hw_fib_pa);
                memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size);
                memcpy(kfib, hw_fib, dev->max_fib_size);
        }
@@ -138,9 +140,10 @@ cleanup:
        if (hw_fib) {
                pci_free_consistent(dev->pdev, size, kfib, fibptr->hw_fib_pa);
                fibptr->hw_fib_pa = hw_fib_pa;
-               fibptr->hw_fib = hw_fib;
+               fibptr->hw_fib_va = hw_fib;
        }
-       aac_fib_free(fibptr);
+       if (retval != -EINTR)
+               aac_fib_free(fibptr);
        return retval;
 }
 
@@ -282,22 +285,22 @@ return_fib:
                fib = list_entry(entry, struct fib, fiblink);
                fibctx->count--;
                spin_unlock_irqrestore(&dev->fib_lock, flags);
-               if (copy_to_user(f.fib, fib->hw_fib, sizeof(struct hw_fib))) {
-                       kfree(fib->hw_fib);
+               if (copy_to_user(f.fib, fib->hw_fib_va, sizeof(struct hw_fib))) {
+                       kfree(fib->hw_fib_va);
                        kfree(fib);
                        return -EFAULT;
                }       
                /*
                 *      Free the space occupied by this copy of the fib.
                 */
-               kfree(fib->hw_fib);
+               kfree(fib->hw_fib_va);
                kfree(fib);
                status = 0;
        } else {
                spin_unlock_irqrestore(&dev->fib_lock, flags);
                /* If someone killed the AIF aacraid thread, restart it */
                status = !dev->aif_thread;
-               if (status && dev->queues && dev->fsa_dev) {
+               if (status && !dev->in_reset && dev->queues && dev->fsa_dev) {
                        /* Be paranoid, be very paranoid! */
                        kthread_stop(dev->thread);
                        ssleep(1);
@@ -340,7 +343,7 @@ int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
                /*
                 *      Free the space occupied by this copy of the fib.
                 */
-               kfree(fib->hw_fib);
+               kfree(fib->hw_fib_va);
                kfree(fib);
        }
        /*
@@ -469,6 +472,10 @@ static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
        int i;
 
 
+       if (dev->in_reset) {
+               dprintk((KERN_DEBUG"aacraid: send raw srb -EBUSY\n"));
+               return -EBUSY;
+       }
        if (!capable(CAP_SYS_ADMIN)){
                dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n")); 
                return -EPERM;
@@ -535,7 +542,7 @@ static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
        default:
                data_dir = DMA_NONE;
        }
-       if (user_srbcmd->sg.count > (sizeof(sg_list)/sizeof(sg_list[0]))) {
+       if (user_srbcmd->sg.count > ARRAY_SIZE(sg_list)) {
                dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
                  le32_to_cpu(srbcmd->sg.count)));
                rcode = -EINVAL;
@@ -621,7 +628,13 @@ static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
 
                actual_fibsize = sizeof (struct aac_srb) + (((user_srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry));
                if(actual_fibsize != fibsize){ // User made a mistake - should not continue
-                       dprintk((KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n"));
+                       dprintk((KERN_DEBUG"aacraid: Bad Size specified in "
+                         "Raw SRB command calculated fibsize=%d "
+                         "user_srbcmd->sg.count=%d aac_srb=%d sgentry=%d "
+                         "issued fibsize=%d\n",
+                         actual_fibsize, user_srbcmd->sg.count,
+                         sizeof(struct aac_srb), sizeof(struct sgentry),
+                         fibsize));
                        rcode = -EINVAL;
                        goto cleanup;
                }
@@ -663,6 +676,10 @@ static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
                psg->count = cpu_to_le32(sg_indx+1);
                status = aac_fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
        }
+       if (status == -EINTR) {
+               rcode = -EINTR;
+               goto cleanup;
+       }
 
        if (status != 0){
                dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n")); 
@@ -696,8 +713,10 @@ cleanup:
        for(i=0; i <= sg_indx; i++){
                kfree(sg_list[i]);
        }
-       aac_fib_complete(srbfib);
-       aac_fib_free(srbfib);
+       if (rcode != -EINTR) {
+               aac_fib_complete(srbfib);
+               aac_fib_free(srbfib);
+       }
 
        return rcode;
 }