[SCSI] aacraid: prohibit access to array container space
[safe/jmp/linux-2.6] / drivers / scsi / dpt_i2o.c
index dc6b2d4..0435d04 100644 (file)
@@ -49,6 +49,7 @@ MODULE_DESCRIPTION("Adaptec I2O RAID Driver");
 #include <linux/kernel.h>      /* for printk */
 #include <linux/sched.h>
 #include <linux/reboot.h>
+#include <linux/smp_lock.h>
 #include <linux/spinlock.h>
 #include <linux/dma-mapping.h>
 
@@ -111,6 +112,8 @@ static int sys_tbl_len;
 static adpt_hba* hba_chain = NULL;
 static int hba_count = 0;
 
+static struct class *adpt_sysfs_class;
+
 #ifdef CONFIG_COMPAT
 static long compat_adpt_ioctl(struct file *, unsigned int, unsigned long);
 #endif
@@ -162,7 +165,7 @@ static inline u32 dma_low(dma_addr_t addr)
 
 static u8 adpt_read_blink_led(adpt_hba* host)
 {
-       if(host->FwDebugBLEDflag_P != 0) {
+       if (host->FwDebugBLEDflag_P) {
                if( readb(host->FwDebugBLEDflag_P) == 0xbc ){
                        return readb(host->FwDebugBLEDvalue_P);
                }
@@ -185,7 +188,8 @@ MODULE_DEVICE_TABLE(pci,dptids);
 static int adpt_detect(struct scsi_host_template* sht)
 {
        struct pci_dev *pDev = NULL;
-       adpt_hba* pHba;
+       adpt_hba *pHba;
+       adpt_hba *next;
 
        PINFO("Detecting Adaptec I2O RAID controllers...\n");
 
@@ -203,7 +207,8 @@ static int adpt_detect(struct scsi_host_template* sht)
        }
 
        /* In INIT state, Activate IOPs */
-       for (pHba = hba_chain; pHba; pHba = pHba->next) {
+       for (pHba = hba_chain; pHba; pHba = next) {
+               next = pHba->next;
                // Activate does get status , init outbound, and get hrt
                if (adpt_i2o_activate_hba(pHba) < 0) {
                        adpt_i2o_delete_hba(pHba);
@@ -240,7 +245,8 @@ rebuild_sys_tab:
        PDEBUG("HBA's in OPERATIONAL state\n");
 
        printk("dpti: If you have a lot of devices this could take a few minutes.\n");
-       for (pHba = hba_chain; pHba; pHba = pHba->next) {
+       for (pHba = hba_chain; pHba; pHba = next) {
+               next = pHba->next;
                printk(KERN_INFO"%s: Reading the hardware resource table.\n", pHba->name);
                if (adpt_i2o_lct_get(pHba) < 0){
                        adpt_i2o_delete_hba(pHba);
@@ -254,13 +260,30 @@ rebuild_sys_tab:
                adpt_inquiry(pHba);
        }
 
-       for (pHba = hba_chain; pHba; pHba = pHba->next) {
+       adpt_sysfs_class = class_create(THIS_MODULE, "dpt_i2o");
+       if (IS_ERR(adpt_sysfs_class)) {
+               printk(KERN_WARNING"dpti: unable to create dpt_i2o class\n");
+               adpt_sysfs_class = NULL;
+       }
+
+       for (pHba = hba_chain; pHba; pHba = next) {
+               next = pHba->next;
                if (adpt_scsi_host_alloc(pHba, sht) < 0){
                        adpt_i2o_delete_hba(pHba);
                        continue;
                }
                pHba->initialized = TRUE;
                pHba->state &= ~DPTI_STATE_RESET;
+               if (adpt_sysfs_class) {
+                       struct device *dev = device_create(adpt_sysfs_class,
+                               NULL, MKDEV(DPTI_I2O_MAJOR, pHba->unit), NULL,
+                               "dpti%d", pHba->unit);
+                       if (IS_ERR(dev)) {
+                               printk(KERN_WARNING"dpti%d: unable to "
+                                       "create device in dpt_i2o class\n",
+                                       pHba->unit);
+                       }
+               }
        }
 
        // Register our control device node
@@ -995,15 +1018,15 @@ static int adpt_install_hba(struct scsi_host_template* sht, struct pci_dev* pDev
         *      See if we should enable dma64 mode.
         */
        if (sizeof(dma_addr_t) > 4 &&
-           pci_set_dma_mask(pDev, DMA_64BIT_MASK) == 0) {
-               if (dma_get_required_mask(&pDev->dev) > DMA_32BIT_MASK)
+           pci_set_dma_mask(pDev, DMA_BIT_MASK(64)) == 0) {
+               if (dma_get_required_mask(&pDev->dev) > DMA_BIT_MASK(32))
                        dma64 = 1;
        }
-       if (!dma64 && pci_set_dma_mask(pDev, DMA_32BIT_MASK) != 0)
+       if (!dma64 && pci_set_dma_mask(pDev, DMA_BIT_MASK(32)) != 0)
                return -EINVAL;
 
        /* adapter only supports message blocks below 4GB */
-       pci_set_consistent_dma_mask(pDev, DMA_32BIT_MASK);
+       pci_set_consistent_dma_mask(pDev, DMA_BIT_MASK(32));
 
        base_addr0_phys = pci_resource_start(pDev,0);
        hba_map0_area_size = pci_resource_len(pDev,0);
@@ -1210,10 +1233,17 @@ static void adpt_i2o_delete_hba(adpt_hba* pHba)
                }
        }
        pci_dev_put(pHba->pDev);
+       if (adpt_sysfs_class)
+               device_destroy(adpt_sysfs_class,
+                               MKDEV(DPTI_I2O_MAJOR, pHba->unit));
        kfree(pHba);
 
        if(hba_count <= 0){
                unregister_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER);   
+               if (adpt_sysfs_class) {
+                       class_destroy(adpt_sysfs_class);
+                       adpt_sysfs_class = NULL;
+               }
        }
 }
 
@@ -1701,10 +1731,12 @@ static int adpt_open(struct inode *inode, struct file *file)
        int minor;
        adpt_hba* pHba;
 
+       lock_kernel();
        //TODO check for root access
        //
        minor = iminor(inode);
        if (minor >= hba_count) {
+               unlock_kernel();
                return -ENXIO;
        }
        mutex_lock(&adpt_configuration_lock);
@@ -1715,6 +1747,7 @@ static int adpt_open(struct inode *inode, struct file *file)
        }
        if (pHba == NULL) {
                mutex_unlock(&adpt_configuration_lock);
+               unlock_kernel();
                return -ENXIO;
        }
 
@@ -1725,6 +1758,7 @@ static int adpt_open(struct inode *inode, struct file *file)
 
        pHba->in_use = 1;
        mutex_unlock(&adpt_configuration_lock);
+       unlock_kernel();
 
        return 0;
 }
@@ -1887,6 +1921,10 @@ static int adpt_i2o_passthru(adpt_hba* pHba, u32 __user *arg)
                }
                size = size>>16;
                size *= 4;
+               if (size > MAX_MESSAGE_SIZE) {
+                       rcode = -EINVAL;
+                       goto cleanup;
+               }
                /* Copy in the user's I2O command */
                if (copy_from_user (msg, user_msg, size)) {
                        rcode = -EFAULT;
@@ -1941,45 +1979,6 @@ cleanup:
        return rcode;
 }
 
-
-/*
- * This routine returns information about the system.  This does not effect
- * any logic and if the info is wrong - it doesn't matter.
- */
-
-/* Get all the info we can not get from kernel services */
-static int adpt_system_info(void __user *buffer)
-{
-       sysInfo_S si;
-
-       memset(&si, 0, sizeof(si));
-
-       si.osType = OS_LINUX;
-       si.osMajorVersion = 0;
-       si.osMinorVersion = 0;
-       si.osRevision = 0;
-       si.busType = SI_PCI_BUS;
-       si.processorFamily = DPTI_sig.dsProcessorFamily;
-
-#if defined __i386__ 
-       adpt_i386_info(&si);
-#elif defined (__ia64__)
-       adpt_ia64_info(&si);
-#elif defined(__sparc__)
-       adpt_sparc_info(&si);
-#elif defined (__alpha__)
-       adpt_alpha_info(&si);
-#else
-       si.processorType = 0xff ;
-#endif
-       if(copy_to_user(buffer, &si, sizeof(si))){
-               printk(KERN_WARNING"dpti: Could not copy buffer TO user\n");
-               return -EFAULT;
-       }
-
-       return 0;
-}
-
 #if defined __ia64__ 
 static void adpt_ia64_info(sysInfo_S* si)
 {
@@ -1990,7 +1989,6 @@ static void adpt_ia64_info(sysInfo_S* si)
 }
 #endif
 
-
 #if defined __sparc__ 
 static void adpt_sparc_info(sysInfo_S* si)
 {
@@ -2000,7 +1998,6 @@ static void adpt_sparc_info(sysInfo_S* si)
        si->processorType = PROC_ULTRASPARC;
 }
 #endif
-
 #if defined __alpha__ 
 static void adpt_alpha_info(sysInfo_S* si)
 {
@@ -2012,7 +2009,6 @@ static void adpt_alpha_info(sysInfo_S* si)
 #endif
 
 #if defined __i386__
-
 static void adpt_i386_info(sysInfo_S* si)
 {
        // This is all the info we need for now
@@ -2033,9 +2029,45 @@ static void adpt_i386_info(sysInfo_S* si)
                break;
        }
 }
+#endif
+
+/*
+ * This routine returns information about the system.  This does not effect
+ * any logic and if the info is wrong - it doesn't matter.
+ */
 
+/* Get all the info we can not get from kernel services */
+static int adpt_system_info(void __user *buffer)
+{
+       sysInfo_S si;
+
+       memset(&si, 0, sizeof(si));
+
+       si.osType = OS_LINUX;
+       si.osMajorVersion = 0;
+       si.osMinorVersion = 0;
+       si.osRevision = 0;
+       si.busType = SI_PCI_BUS;
+       si.processorFamily = DPTI_sig.dsProcessorFamily;
+
+#if defined __i386__
+       adpt_i386_info(&si);
+#elif defined (__ia64__)
+       adpt_ia64_info(&si);
+#elif defined(__sparc__)
+       adpt_sparc_info(&si);
+#elif defined (__alpha__)
+       adpt_alpha_info(&si);
+#else
+       si.processorType = 0xff ;
 #endif
+       if (copy_to_user(buffer, &si, sizeof(si))){
+               printk(KERN_WARNING"dpti: Could not copy buffer TO user\n");
+               return -EFAULT;
+       }
 
+       return 0;
+}
 
 static int adpt_ioctl(struct inode *inode, struct file *file, uint cmd,
              ulong arg)
@@ -2420,7 +2452,7 @@ static s32 adpt_i2o_to_scsi(void __iomem *reply, struct scsi_cmnd* cmd)
        hba_status = detailed_status >> 8;
 
        // calculate resid for sg 
-       scsi_set_resid(cmd, scsi_bufflen(cmd) - readl(reply+5));
+       scsi_set_resid(cmd, scsi_bufflen(cmd) - readl(reply+20));
 
        pHba = (adpt_hba*) cmd->device->host->hostdata[0];
 
@@ -2431,7 +2463,7 @@ static s32 adpt_i2o_to_scsi(void __iomem *reply, struct scsi_cmnd* cmd)
                case I2O_SCSI_DSC_SUCCESS:
                        cmd->result = (DID_OK << 16);
                        // handle underflow
-                       if(readl(reply+5) < cmd->underflow ) {
+                       if (readl(reply+20) < cmd->underflow) {
                                cmd->result = (DID_ERROR <<16);
                                printk(KERN_WARNING"%s: SCSI CMD underflow\n",pHba->name);
                        }