Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb-2.6
[safe/jmp/linux-2.6] / drivers / scsi / 3w-9xxx.c
index b92c19b..e9788f5 100644 (file)
@@ -4,7 +4,7 @@
    Written By: Adam Radford <linuxraid@amcc.com>
    Modifications By: Tom Couch <linuxraid@amcc.com>
 
-   Copyright (C) 2004-2008 Applied Micro Circuits Corporation.
+   Copyright (C) 2004-2009 Applied Micro Circuits Corporation.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -75,6 +75,8 @@
                  Add MSI support and "use_msi" module parameter.
                  Fix bug in twa_get_param() on 4GB+.
                  Use pci_resource_len() for ioremap().
+   2.26.02.012 - Add power management support.
+   2.26.02.013 - Fix bug in twa_load_sgl().
 */
 
 #include <linux/module.h>
@@ -89,6 +91,7 @@
 #include <linux/time.h>
 #include <linux/mutex.h>
 #include <linux/smp_lock.h>
+#include <linux/slab.h>
 #include <asm/io.h>
 #include <asm/irq.h>
 #include <asm/uaccess.h>
 #include "3w-9xxx.h"
 
 /* Globals */
-#define TW_DRIVER_VERSION "2.26.02.011"
+#define TW_DRIVER_VERSION "2.26.02.013"
 static TW_Device_Extension *twa_device_extension_list[TW_MAX_SLOT];
 static unsigned int twa_device_extension_count;
 static int twa_major = -1;
@@ -185,8 +188,12 @@ static ssize_t twa_show_stats(struct device *dev,
 } /* End twa_show_stats() */
 
 /* This function will set a devices queue depth */
-static int twa_change_queue_depth(struct scsi_device *sdev, int queue_depth)
+static int twa_change_queue_depth(struct scsi_device *sdev, int queue_depth,
+                                 int reason)
 {
+       if (reason != SCSI_QDEPTH_DEFAULT)
+               return -EOPNOTSUPP;
+
        if (queue_depth > TW_Q_LENGTH-2)
                queue_depth = TW_Q_LENGTH-2;
        scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
@@ -731,7 +738,7 @@ static int twa_chrdev_ioctl(struct inode *inode, struct file *file, unsigned int
                break;
        case TW_IOCTL_GET_COMPATIBILITY_INFO:
                tw_ioctl->driver_command.status = 0;
-               /* Copy compatiblity struct into ioctl data buffer */
+               /* Copy compatibility struct into ioctl data buffer */
                tw_compat_info = (TW_Compatibility_Info *)tw_ioctl->data_buffer;
                memcpy(tw_compat_info, &tw_dev->tw_compat_info, sizeof(TW_Compatibility_Info));
                break;
@@ -1377,10 +1384,12 @@ static void twa_load_sgl(TW_Device_Extension *tw_dev, TW_Command_Full *full_comm
                newcommand = &full_command_packet->command.newcommand;
                newcommand->request_id__lunl =
                        cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->request_id__lunl), request_id));
-               newcommand->sg_list[0].address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
-               newcommand->sg_list[0].length = cpu_to_le32(length);
+               if (length) {
+                       newcommand->sg_list[0].address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
+                       newcommand->sg_list[0].length = cpu_to_le32(length);
+               }
                newcommand->sgl_entries__lunh =
-                       cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), 1));
+                       cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), length ? 1 : 0));
        } else {
                oldcommand = &full_command_packet->command.oldcommand;
                oldcommand->request_id = request_id;
@@ -1924,12 +1933,9 @@ static void twa_scsiop_execute_scsi_complete(TW_Device_Extension *tw_dev, int re
            (cmd->sc_data_direction == DMA_FROM_DEVICE ||
             cmd->sc_data_direction == DMA_BIDIRECTIONAL)) {
                if (scsi_sg_count(cmd) == 1) {
-                       unsigned long flags;
                        void *buf = tw_dev->generic_buffer_virt[request_id];
 
-                       local_irq_save(flags);
                        scsi_sg_copy_from_buffer(cmd, buf, TW_SECTOR_SIZE);
-                       local_irq_restore(flags);
                }
        }
 } /* End twa_scsiop_execute_scsi_complete() */
@@ -1980,7 +1986,8 @@ static void twa_unmap_scsi_data(TW_Device_Extension *tw_dev, int request_id)
 {
        struct scsi_cmnd *cmd = tw_dev->srb[request_id];
 
-       scsi_dma_unmap(cmd);
+       if (cmd->SCp.phase == TW_PHASE_SGLIST)
+               scsi_dma_unmap(cmd);
 } /* End twa_unmap_scsi_data() */
 
 /* scsi_host_template initializer */
@@ -2018,10 +2025,10 @@ static int __devinit twa_probe(struct pci_dev *pdev, const struct pci_device_id
        pci_set_master(pdev);
        pci_try_set_mwi(pdev);
 
-       if (pci_set_dma_mask(pdev, DMA_64BIT_MASK)
-           || pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK))
-               if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)
-                   || pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK)) {
+       if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
+           || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
+               if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
+                   || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
                        TW_PRINTK(host, TW_DRIVER, 0x23, "Failed to set dma mask");
                        retval = -ENODEV;
                        goto out_disable_device;
@@ -2185,6 +2192,98 @@ static void twa_remove(struct pci_dev *pdev)
        twa_device_extension_count--;
 } /* End twa_remove() */
 
+#ifdef CONFIG_PM
+/* This function is called on PCI suspend */
+static int twa_suspend(struct pci_dev *pdev, pm_message_t state)
+{
+       struct Scsi_Host *host = pci_get_drvdata(pdev);
+       TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
+
+       printk(KERN_WARNING "3w-9xxx: Suspending host %d.\n", tw_dev->host->host_no);
+
+       TW_DISABLE_INTERRUPTS(tw_dev);
+       free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
+
+       if (test_bit(TW_USING_MSI, &tw_dev->flags))
+               pci_disable_msi(pdev);
+
+       /* Tell the card we are shutting down */
+       if (twa_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
+               TW_PRINTK(tw_dev->host, TW_DRIVER, 0x38, "Connection shutdown failed during suspend");
+       } else {
+               printk(KERN_WARNING "3w-9xxx: Suspend complete.\n");
+       }
+       TW_CLEAR_ALL_INTERRUPTS(tw_dev);
+
+       pci_save_state(pdev);
+       pci_disable_device(pdev);
+       pci_set_power_state(pdev, pci_choose_state(pdev, state));
+
+       return 0;
+} /* End twa_suspend() */
+
+/* This function is called on PCI resume */
+static int twa_resume(struct pci_dev *pdev)
+{
+       int retval = 0;
+       struct Scsi_Host *host = pci_get_drvdata(pdev);
+       TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
+
+       printk(KERN_WARNING "3w-9xxx: Resuming host %d.\n", tw_dev->host->host_no);
+       pci_set_power_state(pdev, PCI_D0);
+       pci_enable_wake(pdev, PCI_D0, 0);
+       pci_restore_state(pdev);
+
+       retval = pci_enable_device(pdev);
+       if (retval) {
+               TW_PRINTK(tw_dev->host, TW_DRIVER, 0x39, "Enable device failed during resume");
+               return retval;
+       }
+
+       pci_set_master(pdev);
+       pci_try_set_mwi(pdev);
+
+       if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
+           || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
+               if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
+                   || pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
+                       TW_PRINTK(host, TW_DRIVER, 0x40, "Failed to set dma mask during resume");
+                       retval = -ENODEV;
+                       goto out_disable_device;
+               }
+
+       /* Initialize the card */
+       if (twa_reset_sequence(tw_dev, 0)) {
+               retval = -ENODEV;
+               goto out_disable_device;
+       }
+
+       /* Now setup the interrupt handler */
+       retval = request_irq(pdev->irq, twa_interrupt, IRQF_SHARED, "3w-9xxx", tw_dev);
+       if (retval) {
+               TW_PRINTK(tw_dev->host, TW_DRIVER, 0x42, "Error requesting IRQ during resume");
+               retval = -ENODEV;
+               goto out_disable_device;
+       }
+
+       /* Now enable MSI if enabled */
+       if (test_bit(TW_USING_MSI, &tw_dev->flags))
+               pci_enable_msi(pdev);
+
+       /* Re-enable interrupts on the card */
+       TW_ENABLE_AND_CLEAR_INTERRUPTS(tw_dev);
+
+       printk(KERN_WARNING "3w-9xxx: Resume complete.\n");
+       return 0;
+
+out_disable_device:
+       scsi_remove_host(host);
+       pci_disable_device(pdev);
+
+       return retval;
+} /* End twa_resume() */
+#endif
+
 /* PCI Devices supported by this driver */
 static struct pci_device_id twa_pci_tbl[] __devinitdata = {
        { PCI_VENDOR_ID_3WARE, PCI_DEVICE_ID_3WARE_9000,
@@ -2205,6 +2304,10 @@ static struct pci_driver twa_driver = {
        .id_table       = twa_pci_tbl,
        .probe          = twa_probe,
        .remove         = twa_remove,
+#ifdef CONFIG_PM
+       .suspend        = twa_suspend,
+       .resume         = twa_resume,
+#endif
        .shutdown       = twa_shutdown
 };