Merge with /pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[sfrench/cifs-2.6.git] / drivers / block / cciss.c
index 28f2c177a54167a0ce7d3bd9e3f1dbe7ac1d7995..a9e33db46e68b5432627835ea1945f02d40d861c 100644 (file)
 #include <linux/completion.h>
 
 #define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
-#define DRIVER_NAME "HP CISS Driver (v 2.6.6)"
-#define DRIVER_VERSION CCISS_DRIVER_VERSION(2,6,6)
+#define DRIVER_NAME "HP CISS Driver (v 2.6.8)"
+#define DRIVER_VERSION CCISS_DRIVER_VERSION(2,6,8)
 
 /* Embedded module documentation macros - see modules.h */
 MODULE_AUTHOR("Hewlett-Packard Company");
-MODULE_DESCRIPTION("Driver for HP Controller SA5xxx SA6xxx version 2.6.6");
+MODULE_DESCRIPTION("Driver for HP Controller SA5xxx SA6xxx version 2.6.8");
 MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400"
-                       " SA6i P600 P800 E400 E300");
+                       " SA6i P600 P800 P400 P400i E200 E200i");
 MODULE_LICENSE("GPL");
 
 #include "cciss_cmd.h"
@@ -83,12 +83,22 @@ static const struct pci_device_id cciss_pci_device_id[] = {
                0x0E11, 0x4091, 0, 0, 0},
        { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSA,
                0x103C, 0x3225, 0, 0, 0},
-       { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSB,
+       { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC,
                0x103c, 0x3223, 0, 0, 0},
        { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC,
-               0x103c, 0x3231, 0, 0, 0},
+               0x103c, 0x3234, 0, 0, 0},
        { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC,
-               0x103c, 0x3233, 0, 0, 0},
+               0x103c, 0x3235, 0, 0, 0},
+       { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD,
+               0x103c, 0x3211, 0, 0, 0},
+       { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD,
+               0x103c, 0x3212, 0, 0, 0},
+       { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD,
+               0x103c, 0x3213, 0, 0, 0},
+       { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD,
+               0x103c, 0x3214, 0, 0, 0},
+       { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD,
+               0x103c, 0x3215, 0, 0, 0},
        {0,}
 };
 MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
@@ -111,8 +121,13 @@ static struct board_type products[] = {
        { 0x40910E11, "Smart Array 6i", &SA5_access},
        { 0x3225103C, "Smart Array P600", &SA5_access},
        { 0x3223103C, "Smart Array P800", &SA5_access},
-       { 0x3231103C, "Smart Array E400", &SA5_access},
-       { 0x3233103C, "Smart Array E300", &SA5_access},
+       { 0x3234103C, "Smart Array P400", &SA5_access},
+       { 0x3235103C, "Smart Array P400i", &SA5_access},
+       { 0x3211103C, "Smart Array E200i", &SA5_access},
+       { 0x3212103C, "Smart Array E200", &SA5_access},
+       { 0x3213103C, "Smart Array E200i", &SA5_access},
+       { 0x3214103C, "Smart Array E200i", &SA5_access},
+       { 0x3215103C, "Smart Array E200i", &SA5_access},
 };
 
 /* How long to wait (in millesconds) for board to go into simple mode */
@@ -133,6 +148,7 @@ static struct board_type products[] = {
 static ctlr_info_t *hba[MAX_CTLR];
 
 static void do_cciss_request(request_queue_t *q);
+static irqreturn_t do_cciss_intr(int irq, void *dev_id, struct pt_regs *regs);
 static int cciss_open(struct inode *inode, struct file *filep);
 static int cciss_release(struct inode *inode, struct file *filep);
 static int cciss_ioctl(struct inode *inode, struct file *filep, 
@@ -140,15 +156,26 @@ static int cciss_ioctl(struct inode *inode, struct file *filep,
 
 static int revalidate_allvol(ctlr_info_t *host);
 static int cciss_revalidate(struct gendisk *disk);
-static int deregister_disk(struct gendisk *disk);
-static int register_new_disk(ctlr_info_t *h);
+static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk);
+static int deregister_disk(struct gendisk *disk, drive_info_struct *drv, int clear_all);
 
+static void cciss_read_capacity(int ctlr, int logvol, ReadCapdata_struct *buf,
+       int withirq, unsigned int *total_size, unsigned int *block_size);
+static void cciss_geometry_inquiry(int ctlr, int logvol,
+                       int withirq, unsigned int total_size,
+                       unsigned int block_size, InquiryData_struct *inq_buff,
+                       drive_info_struct *drv);
 static void cciss_getgeometry(int cntl_num);
 
 static void start_io( ctlr_info_t *h);
 static int sendcmd( __u8 cmd, int ctlr, void *buff, size_t size,
        unsigned int use_unit_num, unsigned int log_unit, __u8 page_code,
        unsigned char *scsi3addr, int cmd_type);
+static int sendcmd_withirq(__u8        cmd, int ctlr, void *buff, size_t size,
+       unsigned int use_unit_num, unsigned int log_unit, __u8  page_code,
+       int cmd_type);
+
+static void fail_all_cmds(unsigned long ctlr);
 
 #ifdef CONFIG_PROC_FS
 static int cciss_proc_get_info(char *buffer, char **start, off_t offset, 
@@ -265,7 +292,7 @@ static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
        for(i=0; i<=h->highest_lun; i++) {
 
                 drv = &h->drv[i];
-               if (drv->block_size == 0)
+               if (drv->heads == 0)
                        continue;
 
                vol_sz = drv->nr_blocks;
@@ -363,6 +390,8 @@ static CommandList_struct * cmd_alloc(ctlr_info_t *h, int get_from_pool)
                        return NULL;
                memset(c, 0, sizeof(CommandList_struct));
 
+               c->cmdindex = -1;
+
                c->err_info = (ErrorInfo_struct *)pci_alloc_consistent(
                                        h->pdev, sizeof(ErrorInfo_struct), 
                                        &err_dma_handle);
@@ -393,6 +422,8 @@ static CommandList_struct * cmd_alloc(ctlr_info_t *h, int get_from_pool)
                err_dma_handle = h->errinfo_pool_dhandle 
                                        + i*sizeof(ErrorInfo_struct);
                 h->nr_allocs++;
+
+               c->cmdindex = i;
         }
 
        c->busaddr = (__u32) cmd_dma_handle;
@@ -453,6 +484,8 @@ static int cciss_open(struct inode *inode, struct file *filep)
        printk(KERN_DEBUG "cciss_open %s\n", inode->i_bdev->bd_disk->disk_name);
 #endif /* CCISS_DEBUG */ 
 
+       if (host->busy_initializing || drv->busy_configuring)
+               return -EBUSY;
        /*
         * Root is allowed to open raw volume zero even if it's not configured
         * so array config can still work. Root is also allowed to open any
@@ -796,10 +829,10 @@ static int cciss_ioctl(struct inode *inode, struct file *filep,
                return(0);
        }
        case CCISS_DEREGDISK:
-               return deregister_disk(disk);
+               return rebuild_lun_table(host, disk);
 
        case CCISS_REGNEWD:
-               return register_new_disk(host);
+               return rebuild_lun_table(host, NULL);
 
        case CCISS_PASSTHRU:
        {
@@ -984,10 +1017,11 @@ static int cciss_ioctl(struct inode *inode, struct file *filep,
                                status = -ENOMEM;
                                goto cleanup1;
                        }
-                       if (ioc->Request.Type.Direction == XFER_WRITE &&
-                               copy_from_user(buff[sg_used], data_ptr, sz)) {
+                       if (ioc->Request.Type.Direction == XFER_WRITE) {
+                               if (copy_from_user(buff[sg_used], data_ptr, sz)) {
                                        status = -ENOMEM;
-                                       goto cleanup1;                  
+                                       goto cleanup1;
+                               }
                        } else {
                                memset(buff[sg_used], 0, sz);
                        }
@@ -1064,14 +1098,11 @@ static int cciss_ioctl(struct inode *inode, struct file *filep,
 cleanup1:
                if (buff) {
                        for(i=0; i<sg_used; i++)
-                               if(buff[i] != NULL)
-                                       kfree(buff[i]);
+                               kfree(buff[i]);
                        kfree(buff);
                }
-               if (buff_size)
-                       kfree(buff_size);
-               if (ioc)
-                       kfree(ioc);
+               kfree(buff_size);
+               kfree(ioc);
                return(status);
        }
        default:
@@ -1108,8 +1139,15 @@ static int revalidate_allvol(ctlr_info_t *host)
 
        for(i=0; i< NWD; i++) {
                struct gendisk *disk = host->gendisk[i];
-               if (disk->flags & GENHD_FL_UP)
-                       del_gendisk(disk);
+               if (disk) {
+                       request_queue_t *q = disk->queue;
+
+                       if (disk->flags & GENHD_FL_UP)
+                               del_gendisk(disk);
+                       if (q)
+                               blk_cleanup_queue(q);
+                       put_disk(disk);
+               }
        }
 
         /*
@@ -1143,48 +1181,326 @@ static int revalidate_allvol(ctlr_info_t *host)
         return 0;
 }
 
-static int deregister_disk(struct gendisk *disk)
+/* This function will check the usage_count of the drive to be updated/added.
+ * If the usage_count is zero then the drive information will be updated and
+ * the disk will be re-registered with the kernel.  If not then it will be
+ * left alone for the next reboot.  The exception to this is disk 0 which
+ * will always be left registered with the kernel since it is also the
+ * controller node.  Any changes to disk 0 will show up on the next
+ * reboot.
+*/
+static void cciss_update_drive_info(int ctlr, int drv_index)
+  {
+       ctlr_info_t *h = hba[ctlr];
+       struct gendisk *disk;
+       ReadCapdata_struct *size_buff = NULL;
+       InquiryData_struct *inq_buff = NULL;
+       unsigned int block_size;
+       unsigned int total_size;
+       unsigned long flags = 0;
+       int ret = 0;
+
+       /* if the disk already exists then deregister it before proceeding*/
+       if (h->drv[drv_index].raid_level != -1){
+               spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
+               h->drv[drv_index].busy_configuring = 1;
+               spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
+               ret = deregister_disk(h->gendisk[drv_index],
+                       &h->drv[drv_index], 0);
+               h->drv[drv_index].busy_configuring = 0;
+       }
+
+       /* If the disk is in use return */
+       if (ret)
+               return;
+
+
+       /* Get information about the disk and modify the driver sturcture */
+       size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
+        if (size_buff == NULL)
+               goto mem_msg;
+       inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
+       if (inq_buff == NULL)
+               goto mem_msg;
+
+       cciss_read_capacity(ctlr, drv_index, size_buff, 1,
+               &total_size, &block_size);
+       cciss_geometry_inquiry(ctlr, drv_index, 1, total_size, block_size,
+               inq_buff, &h->drv[drv_index]);
+
+       ++h->num_luns;
+       disk = h->gendisk[drv_index];
+       set_capacity(disk, h->drv[drv_index].nr_blocks);
+
+
+       /* if it's the controller it's already added */
+       if (drv_index){
+               disk->queue = blk_init_queue(do_cciss_request, &h->lock);
+
+               /* Set up queue information */
+               disk->queue->backing_dev_info.ra_pages = READ_AHEAD;
+               blk_queue_bounce_limit(disk->queue, hba[ctlr]->pdev->dma_mask);
+
+               /* This is a hardware imposed limit. */
+               blk_queue_max_hw_segments(disk->queue, MAXSGENTRIES);
+
+               /* This is a limit in the driver and could be eliminated. */
+               blk_queue_max_phys_segments(disk->queue, MAXSGENTRIES);
+
+               blk_queue_max_sectors(disk->queue, 512);
+
+               disk->queue->queuedata = hba[ctlr];
+
+               blk_queue_hardsect_size(disk->queue,
+                       hba[ctlr]->drv[drv_index].block_size);
+
+               h->drv[drv_index].queue = disk->queue;
+               add_disk(disk);
+       }
+
+freeret:
+       kfree(size_buff);
+       kfree(inq_buff);
+       return;
+mem_msg:
+       printk(KERN_ERR "cciss: out of memory\n");
+       goto freeret;
+}
+
+/* This function will find the first index of the controllers drive array
+ * that has a -1 for the raid_level and will return that index.  This is
+ * where new drives will be added.  If the index to be returned is greater
+ * than the highest_lun index for the controller then highest_lun is set
+ * to this new index.  If there are no available indexes then -1 is returned.
+*/
+static int cciss_find_free_drive_index(int ctlr)
+{
+       int i;
+
+       for (i=0; i < CISS_MAX_LUN; i++){
+               if (hba[ctlr]->drv[i].raid_level == -1){
+                       if (i > hba[ctlr]->highest_lun)
+                               hba[ctlr]->highest_lun = i;
+                       return i;
+               }
+       }
+       return -1;
+}
+
+/* This function will add and remove logical drives from the Logical
+ * drive array of the controller and maintain persistancy of ordering
+ * so that mount points are preserved until the next reboot.  This allows
+ * for the removal of logical drives in the middle of the drive array
+ * without a re-ordering of those drives.
+ * INPUT
+ * h           = The controller to perform the operations on
+ * del_disk    = The disk to remove if specified.  If the value given
+ *               is NULL then no disk is removed.
+*/
+static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk)
 {
+       int ctlr = h->ctlr;
+       int num_luns;
+       ReportLunData_struct *ld_buff = NULL;
+       drive_info_struct *drv = NULL;
+       int return_code;
+       int listlength = 0;
+       int i;
+       int drv_found;
+       int drv_index = 0;
+       __u32 lunid = 0;
        unsigned long flags;
+
+       /* Set busy_configuring flag for this operation */
+       spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
+       if (h->num_luns >= CISS_MAX_LUN){
+               spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
+               return -EINVAL;
+       }
+
+       if (h->busy_configuring){
+               spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
+               return -EBUSY;
+       }
+       h->busy_configuring = 1;
+
+       /* if del_disk is NULL then we are being called to add a new disk
+        * and update the logical drive table.  If it is not NULL then
+        * we will check if the disk is in use or not.
+        */
+       if (del_disk != NULL){
+               drv = get_drv(del_disk);
+               drv->busy_configuring = 1;
+               spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
+               return_code = deregister_disk(del_disk, drv, 1);
+               drv->busy_configuring = 0;
+               h->busy_configuring = 0;
+               return return_code;
+       } else {
+               spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
+               if (!capable(CAP_SYS_RAWIO))
+                       return -EPERM;
+
+               ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
+               if (ld_buff == NULL)
+                       goto mem_msg;
+
+               return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
+                               sizeof(ReportLunData_struct), 0, 0, 0,
+                               TYPE_CMD);
+
+               if (return_code == IO_OK){
+                       listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
+                       listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
+                       listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
+                       listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
+               } else{ /* reading number of logical volumes failed */
+                       printk(KERN_WARNING "cciss: report logical volume"
+                               " command failed\n");
+                       listlength = 0;
+                       goto freeret;
+               }
+
+               num_luns = listlength / 8;      /* 8 bytes per entry */
+               if (num_luns > CISS_MAX_LUN){
+                       num_luns = CISS_MAX_LUN;
+                       printk(KERN_WARNING "cciss: more luns configured"
+                               " on controller than can be handled by"
+                               " this driver.\n");
+               }
+
+               /* Compare controller drive array to drivers drive array.
+               * Check for updates in the drive information and any new drives
+               * on the controller.
+               */
+               for (i=0; i < num_luns; i++){
+                       int j;
+
+                       drv_found = 0;
+
+                       lunid = (0xff &
+                               (unsigned int)(ld_buff->LUN[i][3])) << 24;
+                       lunid |= (0xff &
+                               (unsigned int)(ld_buff->LUN[i][2])) << 16;
+                       lunid |= (0xff &
+                               (unsigned int)(ld_buff->LUN[i][1])) << 8;
+                       lunid |= 0xff &
+                               (unsigned int)(ld_buff->LUN[i][0]);
+
+                       /* Find if the LUN is already in the drive array
+                        * of the controller.  If so then update its info
+                        * if not is use.  If it does not exist then find
+                        * the first free index and add it.
+                       */
+                       for (j=0; j <= h->highest_lun; j++){
+                               if (h->drv[j].LunID == lunid){
+                                       drv_index = j;
+                                       drv_found = 1;
+                               }
+                       }
+
+                       /* check if the drive was found already in the array */
+                       if (!drv_found){
+                               drv_index = cciss_find_free_drive_index(ctlr);
+                               if (drv_index == -1)
+                                       goto freeret;
+
+                       }
+                       h->drv[drv_index].LunID = lunid;
+                       cciss_update_drive_info(ctlr, drv_index);
+               } /* end for */
+       } /* end else */
+
+freeret:
+       kfree(ld_buff);
+       h->busy_configuring = 0;
+       /* We return -1 here to tell the ACU that we have registered/updated
+        * all of the drives that we can and to keep it from calling us
+        * additional times.
+       */
+       return -1;
+mem_msg:
+       printk(KERN_ERR "cciss: out of memory\n");
+       goto freeret;
+}
+
+/* This function will deregister the disk and it's queue from the
+ * kernel.  It must be called with the controller lock held and the
+ * drv structures busy_configuring flag set.  It's parameters are:
+ *
+ * disk = This is the disk to be deregistered
+ * drv  = This is the drive_info_struct associated with the disk to be
+ *        deregistered.  It contains information about the disk used
+ *        by the driver.
+ * clear_all = This flag determines whether or not the disk information
+ *             is going to be completely cleared out and the highest_lun
+ *             reset.  Sometimes we want to clear out information about
+ *             the disk in preperation for re-adding it.  In this case
+ *             the highest_lun should be left unchanged and the LunID
+ *             should not be cleared.
+*/
+static int deregister_disk(struct gendisk *disk, drive_info_struct *drv,
+                          int clear_all)
+{
        ctlr_info_t *h = get_host(disk);
-       drive_info_struct *drv = get_drv(disk);
-       int ctlr = h->ctlr;
 
        if (!capable(CAP_SYS_RAWIO))
                return -EPERM;
 
-       spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
        /* make sure logical volume is NOT is use */
-       if( drv->usage_count > 1) {
-               spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
+       if(clear_all || (h->gendisk[0] == disk)) {
+       if (drv->usage_count > 1)
                 return -EBUSY;
        }
-       drv->usage_count++;
-       spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
+        else
+               if( drv->usage_count > 0 )
+                       return -EBUSY;
+
+       /* invalidate the devices and deregister the disk.  If it is disk
+        * zero do not deregister it but just zero out it's values.  This
+        * allows us to delete disk zero but keep the controller registered.
+       */
+       if (h->gendisk[0] != disk){
+               if (disk) {
+                       request_queue_t *q = disk->queue;
+                       if (disk->flags & GENHD_FL_UP)
+                               del_gendisk(disk);
+                       if (q)  
+                               blk_cleanup_queue(q);
+                       put_disk(disk); 
+               }
+       }
 
-       /* invalidate the devices and deregister the disk */ 
-       if (disk->flags & GENHD_FL_UP)
-               del_gendisk(disk);
+       --h->num_luns;
+       /* zero out the disk size info */
+       drv->nr_blocks = 0;
+       drv->block_size = 0;
+       drv->heads = 0;
+       drv->sectors = 0;
+       drv->cylinders = 0;
+       drv->raid_level = -1;   /* This can be used as a flag variable to
+                                * indicate that this element of the drive
+                                * array is free.
+                               */
+
+       if (clear_all){
        /* check to see if it was the last disk */
        if (drv == h->drv + h->highest_lun) {
                /* if so, find the new hightest lun */
                int i, newhighest =-1;
                for(i=0; i<h->highest_lun; i++) {
                        /* if the disk has size > 0, it is available */
-                       if (h->drv[i].nr_blocks)
+                               if (h->drv[i].heads)
                                newhighest = i;
                }
                h->highest_lun = newhighest;
-                               
        }
-       --h->num_luns;
-       /* zero out the disk size info */ 
-       drv->nr_blocks = 0;
-       drv->block_size = 0;
-       drv->cylinders = 0;
+
        drv->LunID = 0;
+       }
        return(0);
 }
+
 static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
        size_t size,
        unsigned int use_unit_num, /* 0: address the controller,
@@ -1279,6 +1595,24 @@ static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
                }
        } else if (cmd_type == TYPE_MSG) {
                switch (cmd) {
+               case 0: /* ABORT message */
+                       c->Request.CDBLen = 12;
+                       c->Request.Type.Attribute = ATTR_SIMPLE;
+                       c->Request.Type.Direction = XFER_WRITE;
+                       c->Request.Timeout = 0;
+                       c->Request.CDB[0] = cmd; /* abort */
+                       c->Request.CDB[1] = 0;   /* abort a command */
+                       /* buff contains the tag of the command to abort */
+                       memcpy(&c->Request.CDB[4], buff, 8);
+                       break;
+               case 1: /* RESET message */
+                       c->Request.CDBLen = 12;
+                       c->Request.Type.Attribute = ATTR_SIMPLE;
+                       c->Request.Type.Direction = XFER_WRITE;
+                       c->Request.Timeout = 0;
+                       memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
+                       c->Request.CDB[0] = cmd;  /* reset */
+                       c->Request.CDB[1] = 0x04; /* reset a LUN */
                case 3: /* No-Op message */
                        c->Request.CDBLen = 1;
                        c->Request.Type.Attribute = ATTR_SIMPLE;
@@ -1420,8 +1754,10 @@ case CMD_HARDWARE_ERR:
                }
        }       
        /* unlock the buffers from DMA */
+       buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
+       buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
        pci_unmap_single( h->pdev, (dma_addr_t) buff_dma_handle.val,
-                       size, PCI_DMA_BIDIRECTIONAL);
+                       c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
        cmd_free(h, c, 0);
         return(return_status);
 
@@ -1495,164 +1831,6 @@ cciss_read_capacity(int ctlr, int logvol, ReadCapdata_struct *buf,
        return;
 }
 
-static int register_new_disk(ctlr_info_t *h)
-{
-        struct gendisk *disk;
-       int ctlr = h->ctlr;
-        int i;
-       int num_luns;
-       int logvol;
-       int new_lun_found = 0;
-       int new_lun_index = 0;
-       int free_index_found = 0;
-       int free_index = 0;
-       ReportLunData_struct *ld_buff = NULL;
-       ReadCapdata_struct *size_buff = NULL;
-       InquiryData_struct *inq_buff = NULL;
-       int return_code;
-       int listlength = 0;
-       __u32 lunid = 0;
-       unsigned int block_size;
-       unsigned int total_size;
-
-        if (!capable(CAP_SYS_RAWIO))
-                return -EPERM;
-       /* if we have no space in our disk array left to add anything */
-       if(  h->num_luns >= CISS_MAX_LUN)
-               return -EINVAL;
-       
-       ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
-       if (ld_buff == NULL)
-               goto mem_msg;
-       memset(ld_buff, 0, sizeof(ReportLunData_struct));
-       size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
-        if (size_buff == NULL)
-               goto mem_msg;
-       inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
-        if (inq_buff == NULL)
-               goto mem_msg;
-       
-       return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff, 
-                       sizeof(ReportLunData_struct), 0, 0, 0, TYPE_CMD);
-
-       if( return_code == IO_OK)
-       {
-               
-               // printk("LUN Data\n--------------------------\n");
-
-               listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
-               listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
-               listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;  
-               listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
-       } else /* reading number of logical volumes failed */
-       {
-               printk(KERN_WARNING "cciss: report logical volume"
-                       " command failed\n");
-               listlength = 0;
-               goto free_err;
-       }
-       num_luns = listlength / 8; // 8 bytes pre entry
-       if (num_luns > CISS_MAX_LUN)
-       {
-               num_luns = CISS_MAX_LUN;
-       }
-#ifdef CCISS_DEBUG
-       printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
-               ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
-               ld_buff->LUNListLength[3],  num_luns);
-#endif 
-       for(i=0; i<  num_luns; i++)
-       {
-               int j;
-               int lunID_found = 0;
-
-               lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3])) << 24;
-               lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2])) << 16;
-               lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1])) << 8;
-               lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
-               
-               /* check to see if this is a new lun */ 
-               for(j=0; j <= h->highest_lun; j++)
-               {
-#ifdef CCISS_DEBUG
-                       printk("Checking %d %x against %x\n", j,h->drv[j].LunID,
-                                               lunid);
-#endif /* CCISS_DEBUG */
-                       if (h->drv[j].LunID == lunid)
-                       {
-                               lunID_found = 1;
-                               break;
-                       }
-                       
-               }
-               if( lunID_found == 1)
-                       continue;
-               else
-               {       /* It is the new lun we have been looking for */
-#ifdef CCISS_DEBUG
-                       printk("new lun found at %d\n", i);
-#endif /* CCISS_DEBUG */
-                       new_lun_index = i;
-                       new_lun_found = 1;
-                       break;  
-               }
-        }
-        if (!new_lun_found)
-        {
-               printk(KERN_WARNING "cciss:  New Logical Volume not found\n");
-               goto free_err;
-        }
-        /* Now find the free index     */
-       for(i=0; i <CISS_MAX_LUN; i++)
-       {
-#ifdef CCISS_DEBUG
-               printk("Checking Index %d\n", i);
-#endif /* CCISS_DEBUG */
-               if(h->drv[i].LunID == 0)
-               {
-#ifdef CCISS_DEBUG
-                       printk("free index found at %d\n", i);
-#endif /* CCISS_DEBUG */
-                       free_index_found = 1;
-                       free_index = i;
-                       break;
-               }
-       }
-       if (!free_index_found)
-       {
-               printk(KERN_WARNING "cciss: unable to find free slot for disk\n");
-               goto free_err;
-         }
-
-       logvol = free_index;
-       h->drv[logvol].LunID = lunid;
-               /* there could be gaps in lun numbers, track hightest */
-       if(h->highest_lun < lunid)
-               h->highest_lun = logvol;
-       cciss_read_capacity(ctlr, logvol, size_buff, 1,
-               &total_size, &block_size);
-       cciss_geometry_inquiry(ctlr, logvol, 1, total_size, block_size,
-                       inq_buff, &h->drv[logvol]);
-       h->drv[logvol].usage_count = 0;
-       ++h->num_luns;
-       /* setup partitions per disk */
-        disk = h->gendisk[logvol];
-       set_capacity(disk, h->drv[logvol].nr_blocks);
-       /* if it's the controller it's already added */
-       if(logvol)
-               add_disk(disk);
-freeret:
-       kfree(ld_buff);
-       kfree(size_buff);
-       kfree(inq_buff);
-       return (logvol);
-mem_msg:
-       printk(KERN_ERR "cciss: out of memory\n");
-free_err:
-       logvol = -1;
-       goto freeret;
-}
-
 static int cciss_revalidate(struct gendisk *disk)
 {
        ctlr_info_t *h = get_host(disk);
@@ -1721,6 +1899,52 @@ static unsigned long pollcomplete(int ctlr)
        /* Invalid address to tell caller we ran out of time */
        return 1;
 }
+
+static int add_sendcmd_reject(__u8 cmd, int ctlr, unsigned long complete)
+{
+       /* We get in here if sendcmd() is polling for completions
+          and gets some command back that it wasn't expecting -- 
+          something other than that which it just sent down.  
+          Ordinarily, that shouldn't happen, but it can happen when 
+          the scsi tape stuff gets into error handling mode, and
+          starts using sendcmd() to try to abort commands and 
+          reset tape drives.  In that case, sendcmd may pick up
+          completions of commands that were sent to logical drives
+          through the block i/o system, or cciss ioctls completing, etc. 
+          In that case, we need to save those completions for later
+          processing by the interrupt handler.
+       */
+
+#ifdef CONFIG_CISS_SCSI_TAPE
+       struct sendcmd_reject_list *srl = &hba[ctlr]->scsi_rejects;     
+
+       /* If it's not the scsi tape stuff doing error handling, (abort */
+       /* or reset) then we don't expect anything weird. */
+       if (cmd != CCISS_RESET_MSG && cmd != CCISS_ABORT_MSG) {
+#endif
+               printk( KERN_WARNING "cciss cciss%d: SendCmd "
+                     "Invalid command list address returned! (%lx)\n",
+                       ctlr, complete);
+               /* not much we can do. */
+#ifdef CONFIG_CISS_SCSI_TAPE
+               return 1;
+       }
+
+       /* We've sent down an abort or reset, but something else
+          has completed */
+       if (srl->ncompletions >= (NR_CMDS + 2)) {
+               /* Uh oh.  No room to save it for later... */
+               printk(KERN_WARNING "cciss%d: Sendcmd: Invalid command addr, "
+                       "reject list overflow, command lost!\n", ctlr);
+               return 1;
+       }
+       /* Save it for later */
+       srl->complete[srl->ncompletions] = complete;
+       srl->ncompletions++;
+#endif
+       return 0;
+}
+
 /*
  * Send a command to the controller, and wait for it to complete.  
  * Only used at init time. 
@@ -1743,7 +1967,7 @@ static int sendcmd(
        unsigned long complete;
        ctlr_info_t *info_p= hba[ctlr];
        u64bit buff_dma_handle;
-       int status;
+       int status, done = 0;
 
        if ((c = cmd_alloc(info_p, 1)) == NULL) {
                printk(KERN_WARNING "cciss: unable to get memory");
@@ -1765,7 +1989,9 @@ resend_cmd1:
         info_p->access.set_intr_mask(info_p, CCISS_INTR_OFF);
        
        /* Make sure there is room in the command FIFO */
-        /* Actually it should be completely empty at this time. */
+        /* Actually it should be completely empty at this time */
+       /* unless we are in here doing error handling for the scsi */
+       /* tape side of the driver. */
         for (i = 200000; i > 0; i--) 
        {
                /* if fifo isn't full go */
@@ -1782,13 +2008,25 @@ resend_cmd1:
          * Send the cmd
          */
         info_p->access.submit_command(info_p, c);
-        complete = pollcomplete(ctlr);
+       done = 0;
+       do {
+               complete = pollcomplete(ctlr);
 
 #ifdef CCISS_DEBUG
-       printk(KERN_DEBUG "cciss: command completed\n");
+               printk(KERN_DEBUG "cciss: command completed\n");
 #endif /* CCISS_DEBUG */
 
-       if (complete != 1) {
+               if (complete == 1) {
+                       printk( KERN_WARNING
+                               "cciss cciss%d: SendCmd Timeout out, "
+                               "No command list address returned!\n",
+                               ctlr);
+                       status = IO_ERROR;
+                       done = 1;
+                       break;
+               }
+
+               /* This will need to change for direct lookup completions */
                if ( (complete & CISS_ERROR_BIT)
                     && (complete & ~CISS_ERROR_BIT) == c->busaddr)
                     {
@@ -1828,6 +2066,10 @@ resend_cmd1:
                                                status = IO_ERROR;
                                                goto cleanup1;
                                        }
+                               } else if (c->err_info->CommandStatus == CMD_UNABORTABLE) {
+                                       printk(KERN_WARNING "cciss%d: command could not be aborted.\n", ctlr);
+                                       status = IO_ERROR;
+                                       goto cleanup1;
                                }
                                printk(KERN_WARNING "ciss ciss%d: sendcmd"
                                " Error %x \n", ctlr, 
@@ -1842,25 +2084,27 @@ resend_cmd1:
                                goto cleanup1;
                        }
                }
+               /* This will need changing for direct lookup completions */
                 if (complete != c->busaddr) {
-                        printk( KERN_WARNING "cciss cciss%d: SendCmd "
-                      "Invalid command list address returned! (%lx)\n",
-                                ctlr, complete);
-                       status = IO_ERROR;
-                       goto cleanup1;
-                }
-        } else {
-                printk( KERN_WARNING
-                        "cciss cciss%d: SendCmd Timeout out, "
-                        "No command list address returned!\n",
-                        ctlr);
-               status = IO_ERROR;
-        }
+                       if (add_sendcmd_reject(cmd, ctlr, complete) != 0) {
+                               BUG(); /* we are pretty much hosed if we get here. */
+                       }
+                       continue;
+                } else
+                       done = 1;
+        } while (!done);
                
 cleanup1:      
        /* unlock the data buffer from DMA */
+       buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
+       buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
        pci_unmap_single(info_p->pdev, (dma_addr_t) buff_dma_handle.val,
-                               size, PCI_DMA_BIDIRECTIONAL);
+                               c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
+#ifdef CONFIG_CISS_SCSI_TAPE
+       /* if we saved some commands for later, process them now. */
+       if (info_p->scsi_rejects.ncompletions > 0)
+               do_cciss_intr(0, info_p, NULL);
+#endif
        cmd_free(info_p, c, 1);
        return (status);
 } 
@@ -2111,7 +2355,11 @@ queue:
        /* fill in the request */ 
        drv = creq->rq_disk->private_data;
        c->Header.ReplyQueue = 0;  // unused in simple mode
-       c->Header.Tag.lower = c->busaddr;  // use the physical address the cmd block for tag
+       /* got command from pool, so use the command block index instead */
+       /* for direct lookups. */
+       /* The first 2 bits are reserved for controller error reporting. */
+       c->Header.Tag.lower = (c->cmdindex << 3);
+       c->Header.Tag.lower |= 0x04; /* flag for direct lookup. */
        c->Header.LUN.LogDev.VolId= drv->LunID;
        c->Header.LUN.LogDev.Mode = 1;
        c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
@@ -2181,33 +2429,82 @@ startio:
        start_io(h);
 }
 
+static inline unsigned long get_next_completion(ctlr_info_t *h)
+{
+#ifdef CONFIG_CISS_SCSI_TAPE
+       /* Any rejects from sendcmd() lying around? Process them first */
+       if (h->scsi_rejects.ncompletions == 0)
+               return h->access.command_completed(h);
+       else {
+               struct sendcmd_reject_list *srl;
+               int n;
+               srl = &h->scsi_rejects;
+               n = --srl->ncompletions;
+               /* printk("cciss%d: processing saved reject\n", h->ctlr); */
+               printk("p");
+               return srl->complete[n];
+       }
+#else
+       return h->access.command_completed(h);
+#endif
+}
+
+static inline int interrupt_pending(ctlr_info_t *h)
+{
+#ifdef CONFIG_CISS_SCSI_TAPE
+       return ( h->access.intr_pending(h) 
+               || (h->scsi_rejects.ncompletions > 0));
+#else
+       return h->access.intr_pending(h);
+#endif
+}
+
+static inline long interrupt_not_for_us(ctlr_info_t *h)
+{
+#ifdef CONFIG_CISS_SCSI_TAPE
+       return (((h->access.intr_pending(h) == 0) || 
+                (h->interrupts_enabled == 0)) 
+             && (h->scsi_rejects.ncompletions == 0));
+#else
+       return (((h->access.intr_pending(h) == 0) || 
+                (h->interrupts_enabled == 0)));
+#endif
+}
+
 static irqreturn_t do_cciss_intr(int irq, void *dev_id, struct pt_regs *regs)
 {
        ctlr_info_t *h = dev_id;
        CommandList_struct *c;
        unsigned long flags;
-       __u32 a, a1;
+       __u32 a, a1, a2;
        int j;
        int start_queue = h->next_to_run;
 
-       /* Is this interrupt for us? */
-       if (( h->access.intr_pending(h) == 0) || (h->interrupts_enabled == 0))
+       if (interrupt_not_for_us(h))
                return IRQ_NONE;
-
        /*
         * If there are completed commands in the completion queue,
         * we had better do something about it.
         */
        spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
-       while( h->access.intr_pending(h))
-       {
-               while((a = h->access.command_completed(h)) != FIFO_EMPTY) 
-               {
+       while (interrupt_pending(h)) {
+               while((a = get_next_completion(h)) != FIFO_EMPTY) {
                        a1 = a;
+                       if ((a & 0x04)) {
+                               a2 = (a >> 3);
+                               if (a2 >= NR_CMDS) {
+                                       printk(KERN_WARNING "cciss: controller cciss%d failed, stopping.\n", h->ctlr);
+                                       fail_all_cmds(h->ctlr);
+                                       return IRQ_HANDLED;
+                               }
+
+                               c = h->cmd_pool + a2;
+                               a = c->busaddr;
+
+                       } else {
                        a &= ~3;
-                       if ((c = h->cmpQ) == NULL)
-                       {  
-                               printk(KERN_WARNING "cciss: Completion of %08lx ignored\n", (unsigned long)a1);
+                               if ((c = h->cmpQ) == NULL) {
+                                       printk(KERN_WARNING "cciss: Completion of %08x ignored\n", a1);
                                continue;       
                        } 
                        while(c->busaddr != a) {
@@ -2215,6 +2512,7 @@ static irqreturn_t do_cciss_intr(int irq, void *dev_id, struct pt_regs *regs)
                                if (c == h->cmpQ) 
                                        break;
                        }
+                       }
                        /*
                         * If we've found the command, take it off the
                         * completion Q and free it
@@ -2634,12 +2932,16 @@ static void cciss_getgeometry(int cntl_num)
 #endif /* CCISS_DEBUG */
 
        hba[cntl_num]->highest_lun = hba[cntl_num]->num_luns-1;
-       for(i=0; i<  hba[cntl_num]->num_luns; i++)
+//     for(i=0; i<  hba[cntl_num]->num_luns; i++)
+       for(i=0; i < CISS_MAX_LUN; i++)
        {
-
-               lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3])) << 24;
-               lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2])) << 16;
-               lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1])) << 8;
+               if (i < hba[cntl_num]->num_luns){
+                       lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3]))
+                                << 24;
+                       lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2]))
+                                << 16;
+                       lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1]))
+                                << 8;
                lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
                
                hba[cntl_num]->drv[i].LunID = lunid;
@@ -2647,13 +2949,18 @@ static void cciss_getgeometry(int cntl_num)
 
 #ifdef CCISS_DEBUG
                printk(KERN_DEBUG "LUN[%d]:  %x %x %x %x = %x\n", i, 
-               ld_buff->LUN[i][0], ld_buff->LUN[i][1],ld_buff->LUN[i][2], 
-               ld_buff->LUN[i][3], hba[cntl_num]->drv[i].LunID);
+                       ld_buff->LUN[i][0], ld_buff->LUN[i][1],
+                       ld_buff->LUN[i][2], ld_buff->LUN[i][3],
+                       hba[cntl_num]->drv[i].LunID);
 #endif /* CCISS_DEBUG */
                cciss_read_capacity(cntl_num, i, size_buff, 0,
                        &total_size, &block_size);
-               cciss_geometry_inquiry(cntl_num, i, 0, total_size, block_size,
-                       inq_buff, &hba[cntl_num]->drv[i]);
+                       cciss_geometry_inquiry(cntl_num, i, 0, total_size,
+                               block_size, inq_buff, &hba[cntl_num]->drv[i]);
+               } else {
+                       /* initialize raid_level to indicate a free space */
+                       hba[cntl_num]->drv[i].raid_level = -1;
+               }
        }
        kfree(ld_buff);
        kfree(size_buff);
@@ -2727,6 +3034,9 @@ static int __devinit cciss_init_one(struct pci_dev *pdev,
        i = alloc_cciss_hba();
        if(i < 0)
                return (-1);
+
+       hba[i]->busy_initializing = 1;
+
        if (cciss_pci_init(hba[i], pdev) != 0)
                goto clean1;
 
@@ -2785,7 +3095,15 @@ static int __devinit cciss_init_one(struct pci_dev *pdev,
                 printk( KERN_ERR "cciss: out of memory");
                goto clean4;
        }
-
+#ifdef CONFIG_CISS_SCSI_TAPE
+       hba[i]->scsi_rejects.complete = 
+               kmalloc(sizeof(hba[i]->scsi_rejects.complete[0]) * 
+                       (NR_CMDS + 5), GFP_KERNEL);
+       if (hba[i]->scsi_rejects.complete == NULL) {
+                printk( KERN_ERR "cciss: out of memory");
+               goto clean4;
+       }
+#endif
        spin_lock_init(&hba[i]->lock);
 
        /* Initialize the pdev driver private data. 
@@ -2807,6 +3125,7 @@ static int __devinit cciss_init_one(struct pci_dev *pdev,
        hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
 
        cciss_procinit(i);
+       hba[i]->busy_initializing = 0;
 
        for(j=0; j < NWD; j++) { /* mfm */
                drive_info_struct *drv = &(hba[i]->drv[j]);
@@ -2852,8 +3171,11 @@ static int __devinit cciss_init_one(struct pci_dev *pdev,
        return(1);
 
 clean4:
-       if(hba[i]->cmd_pool_bits)
-                       kfree(hba[i]->cmd_pool_bits);
+#ifdef CONFIG_CISS_SCSI_TAPE
+       if(hba[i]->scsi_rejects.complete)
+               kfree(hba[i]->scsi_rejects.complete);
+#endif
+       kfree(hba[i]->cmd_pool_bits);
        if(hba[i]->cmd_pool)
                pci_free_consistent(hba[i]->pdev,
                        NR_CMDS * sizeof(CommandList_struct),
@@ -2869,6 +3191,7 @@ clean2:
 clean1:
        release_io_mem(hba[i]);
        free_hba(i);
+       hba[i]->busy_initializing = 0;
        return(-1);
 }
 
@@ -2913,9 +3236,15 @@ static void __devexit cciss_remove_one (struct pci_dev *pdev)
        /* remove it from the disk list */
        for (j = 0; j < NWD; j++) {
                struct gendisk *disk = hba[i]->gendisk[j];
-               if (disk->flags & GENHD_FL_UP)
-                       blk_cleanup_queue(disk->queue);
-                       del_gendisk(disk);
+               if (disk) {
+                       request_queue_t *q = disk->queue;
+
+                       if (disk->flags & GENHD_FL_UP) 
+                               del_gendisk(disk);
+                       if (q)
+                               blk_cleanup_queue(q);
+                       put_disk(disk);
+               }
        }
 
        pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
@@ -2923,6 +3252,9 @@ static void __devexit cciss_remove_one (struct pci_dev *pdev)
        pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
                hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
        kfree(hba[i]->cmd_pool_bits);
+#ifdef CONFIG_CISS_SCSI_TAPE
+       kfree(hba[i]->scsi_rejects.complete);
+#endif
        release_io_mem(hba[i]);
        free_hba(i);
 }      
@@ -2964,5 +3296,43 @@ static void __exit cciss_cleanup(void)
        remove_proc_entry("cciss", proc_root_driver);
 }
 
+static void fail_all_cmds(unsigned long ctlr)
+{
+       /* If we get here, the board is apparently dead. */
+       ctlr_info_t *h = hba[ctlr];
+       CommandList_struct *c;
+       unsigned long flags;
+
+       printk(KERN_WARNING "cciss%d: controller not responding.\n", h->ctlr);
+       h->alive = 0;   /* the controller apparently died... */
+
+       spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
+
+       pci_disable_device(h->pdev); /* Make sure it is really dead. */
+
+       /* move everything off the request queue onto the completed queue */
+       while( (c = h->reqQ) != NULL ) {
+               removeQ(&(h->reqQ), c);
+               h->Qdepth--;
+               addQ (&(h->cmpQ), c);
+       }
+
+       /* Now, fail everything on the completed queue with a HW error */
+       while( (c = h->cmpQ) != NULL ) {
+               removeQ(&h->cmpQ, c);
+               c->err_info->CommandStatus = CMD_HARDWARE_ERR;
+               if (c->cmd_type == CMD_RWREQ) {
+                       complete_command(h, c, 0);
+               } else if (c->cmd_type == CMD_IOCTL_PEND)
+                       complete(c->waiting);
+#ifdef CONFIG_CISS_SCSI_TAPE
+                       else if (c->cmd_type == CMD_SCSI)
+                               complete_scsi_command(c, 0, 0);
+#endif
+       }
+       spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
+       return;
+}
+
 module_init(cciss_init);
 module_exit(cciss_cleanup);