ips.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:274k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*****************************************************************************/
  2. /* ips.c -- driver for the IBM ServeRAID controller                          */
  3. /*                                                                           */
  4. /* Written By: Keith Mitchell, IBM Corporation                               */
  5. /*             Jack Hammer, Adaptec, Inc.                                    */
  6. /*             David Jeffery, Adaptec, Inc.                                  */
  7. /*                                                                           */
  8. /* Copyright (C) 2000 IBM Corporation                                        */
  9. /*                                                                           */
  10. /* This program is free software; you can redistribute it and/or modify      */
  11. /* it under the terms of the GNU General Public License as published by      */
  12. /* the Free Software Foundation; either version 2 of the License, or         */
  13. /* (at your option) any later version.                                       */
  14. /*                                                                           */
  15. /* This program is distributed in the hope that it will be useful,           */
  16. /* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
  17. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
  18. /* GNU General Public License for more details.                              */
  19. /*                                                                           */
  20. /* NO WARRANTY                                                               */
  21. /* THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR        */
  22. /* CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT      */
  23. /* LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,      */
  24. /* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is    */
  25. /* solely responsible for determining the appropriateness of using and       */
  26. /* distributing the Program and assumes all risks associated with its        */
  27. /* exercise of rights under this Agreement, including but not limited to     */
  28. /* the risks and costs of program errors, damage to or loss of data,         */
  29. /* programs or equipment, and unavailability or interruption of operations.  */
  30. /*                                                                           */
  31. /* DISCLAIMER OF LIABILITY                                                   */
  32. /* NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY   */
  33. /* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL        */
  34. /* DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND   */
  35. /* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR     */
  36. /* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE    */
  37. /* USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED  */
  38. /* HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES             */
  39. /*                                                                           */
  40. /* You should have received a copy of the GNU General Public License         */
  41. /* along with this program; if not, write to the Free Software               */
  42. /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  43. /*                                                                           */
  44. /* Bugs/Comments/Suggestions about this driver should be mailed to:          */
  45. /*      ipslinux@adaptec.com                                               */
  46. /*                                                                           */
  47. /* For system support issues, contact your local IBM Customer support.       */
  48. /* Directions to find IBM Customer Support for each country can be found at: */
  49. /*      http://www.ibm.com/planetwide/                                       */
  50. /*                                                                           */
  51. /*****************************************************************************/
  52. /*****************************************************************************/
  53. /* Change Log                                                                */
  54. /*                                                                           */
  55. /* 0.99.02  - Breakup commands that are bigger than 8 * the stripe size      */
  56. /* 0.99.03  - Make interrupt routine handle all completed request on the     */
  57. /*            adapter not just the first one                                 */
  58. /*          - Make sure passthru commands get woken up if we run out of      */
  59. /*            SCBs                                                           */
  60. /*          - Send all of the commands on the queue at once rather than      */
  61. /*            one at a time since the card will support it.                  */
  62. /* 0.99.04  - Fix race condition in the passthru mechanism -- this required  */
  63. /*            the interface to the utilities to change                       */
  64. /*          - Fix error recovery code                                        */
  65. /* 0.99.05  - Fix an oops when we get certain passthru commands              */
  66. /* 1.00.00  - Initial Public Release                                         */
  67. /*            Functionally equivalent to 0.99.05                             */
  68. /* 3.60.00  - Bump max commands to 128 for use with ServeRAID firmware 3.60  */
  69. /*          - Change version to 3.60 to coincide with ServeRAID release      */
  70. /*            numbering.                                                     */
  71. /* 3.60.01  - Remove bogus error check in passthru routine                   */
  72. /* 3.60.02  - Make DCDB direction based on lookup table                      */
  73. /*          - Only allow one DCDB command to a SCSI ID at a time             */
  74. /* 4.00.00  - Add support for ServeRAID 4                                    */
  75. /* 4.00.01  - Add support for First Failure Data Capture                     */
  76. /* 4.00.02  - Fix problem with PT DCDB with no buffer                        */
  77. /* 4.00.03  - Add alternative passthru interface                             */
  78. /*          - Add ability to flash ServeRAID BIOS                            */
  79. /* 4.00.04  - Rename structures/constants to be prefixed with IPS_           */
  80. /* 4.00.05  - Remove wish_block from init routine                            */
  81. /*          - Use linux/spinlock.h instead of asm/spinlock.h for kernels     */
  82. /*            2.3.18 and later                                               */
  83. /*          - Sync with other changes from the 2.3 kernels                   */
  84. /* 4.00.06  - Fix timeout with initial FFDC command                          */
  85. /* 4.00.06a - Port to 2.4 (trivial) -- Christoph Hellwig <hch@caldera.de>    */
  86. /* 4.10.00  - Add support for ServeRAID 4M/4L                                */
  87. /* 4.10.13  - Fix for dynamic unload and proc file system                    */
  88. /* 4.20.03  - Rename version to coincide with new release schedules          */
  89. /*            Performance fixes                                              */
  90. /*            Fix truncation of /proc files with cat                         */
  91. /*            Merge in changes through kernel 2.4.0test1ac21                 */
  92. /* 4.20.13  - Fix some failure cases / reset code                            */
  93. /*          - Hook into the reboot_notifier to flush the controller cache    */
  94. /* 4.50.01  - Fix problem when there is a hole in logical drive numbering    */
  95. /* 4.70.09  - Use a Common ( Large Buffer ) for Flashing from the JCRM CD    */
  96. /*          - Add IPSSEND Flash Support                                      */
  97. /*          - Set Sense Data for Unknown SCSI Command                        */
  98. /*          - Use Slot Number from NVRAM Page 5                              */
  99. /*          - Restore caller's DCDB Structure                                */
  100. /* 4.70.12  - Corrective actions for bad controller ( during initialization )*/
  101. /* 4.70.13  - Don't Send CDB's if we already know the device is not present  */
  102. /*          - Don't release HA Lock in ips_next() until SC taken off queue   */
  103. /*          - Unregister SCSI device in ips_release()                        */
  104. /* 4.70.15  - Fix Breakup for very large ( non-SG ) requests in ips_done()   */
  105. /* 4.71.00  - Change all memory allocations to not use GFP_DMA flag          */
  106. /*            Code Clean-Up for 2.4.x kernel                                 */
  107. /* 4.72.00  - Allow for a Scatter-Gather Element to exceed MAX_XFER Size     */
  108. /* 4.72.01  - I/O Mapped Memory release ( so "insmod ips" does not Fail )    */
  109. /*          - Don't Issue Internal FFDC Command if there are Active Commands */
  110. /*          - Close Window for getting too many IOCTL's active               */
  111. /* 4.80.00  - Make ia64 Safe                                                 */
  112. /* 4.80.04  - Eliminate calls to strtok() if 2.4.x or greater                */
  113. /*          - Adjustments to Device Queue Depth                              */
  114. /* 4.80.14  - Take all semaphores off stack                                  */
  115. /*          - Clean Up New_IOCTL path                                        */
  116. /* 4.80.20  - Set max_sectors in Scsi_Host structure ( if >= 2.4.7 kernel )  */
  117. /*          - 5 second delay needed after resetting an i960 adapter          */
  118. /* 4.80.26  - Clean up potential code problems ( Arjan's recommendations )   */
  119. /* 4.90.01  - Version Matching for FirmWare, BIOS, and Driver                */
  120. /* 4.90.05  - Use New PCI Architecture to facilitate Hot Plug Development    */
  121. /* 4.90.08  - Increase Delays in Flashing ( Trombone Only - 4H )             */
  122. /* 4.90.08  - Data Corruption if First Scatter Gather Element is > 64K       */
  123. /* 4.90.11  - Don't actually RESET unless it's physically required           */
  124. /*          - Remove unused compile options                                  */
  125. /* 5.00.01  - Sarasota ( 5i ) adapters must always be scanned first          */
  126. /*          - Get rid on IOCTL_NEW_COMMAND code                              */
  127. /*          - Add Extended DCDB Commands for Tape Support in 5I              */
  128. /*****************************************************************************/
  129. /*
  130.  * Conditional Compilation directives for this driver:
  131.  *
  132.  * IPS_DEBUG            - Turn on debugging info
  133.  *
  134.  * Parameters:
  135.  *
  136.  * debug:<number>       - Set debug level to <number>
  137.  *                        NOTE: only works when IPS_DEBUG compile directive is used.
  138.  *       1              - Normal debug messages
  139.  *       2              - Verbose debug messages
  140.  *       11             - Method trace (non interrupt)
  141.  *       12             - Method trace (includes interrupt)
  142.  *
  143.  * noi2o                - Don't use I2O Queues (ServeRAID 4 only)
  144.  * nommap               - Don't use memory mapped I/O
  145.  * ioctlsize            - Initial size of the IOCTL buffer
  146.  */
  147.  
  148. #include <asm/io.h>
  149. #include <asm/byteorder.h>
  150. #include <asm/page.h>
  151. #include <linux/stddef.h>
  152. #include <linux/version.h>
  153. #include <linux/string.h>
  154. #include <linux/errno.h>
  155. #include <linux/kernel.h>
  156. #include <linux/ioport.h>
  157. #include <linux/slab.h>
  158. #include <linux/vmalloc.h>
  159. #include <linux/delay.h>
  160. #include <linux/sched.h>
  161. #include <linux/pci.h>
  162. #include <linux/proc_fs.h>
  163. #include <linux/reboot.h>
  164. #include <linux/tqueue.h>
  165. #include <linux/interrupt.h>
  166. #include <linux/blk.h>
  167. #include <linux/types.h>
  168. #include <scsi/sg.h>
  169. #include "sd.h"
  170. #include "scsi.h"
  171. #include "hosts.h"
  172. #include "ips.h"
  173. #include <linux/module.h>
  174. #include <linux/stat.h>
  175. #include <linux/config.h>
  176. #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
  177.   #include <linux/spinlock.h>
  178.   #include <linux/init.h>
  179. #else
  180.   #include <asm/spinlock.h>
  181. #endif
  182. #include <linux/smp.h>
  183. #ifdef MODULE
  184.    static char *ips = NULL;
  185.    MODULE_PARM(ips, "s");
  186. #endif
  187. /*
  188.  * DRIVER_VER
  189.  */
  190. #define IPS_VERSION_HIGH        "5.10"
  191. #define IPS_VERSION_LOW         ".21 "
  192. #if LINUX_VERSION_CODE < LinuxVersionCode(2,4,0)
  193. struct proc_dir_entry proc_scsi_ips = {
  194.    0,
  195.    3, "ips",
  196.    S_IFDIR | S_IRUGO | S_IXUGO, 2
  197. };
  198. #endif
  199. #if !defined(__i386__) && !defined(__ia64__)
  200.    #error "This driver has only been tested on the x86/ia64 platforms"
  201. #endif
  202. #if LINUX_VERSION_CODE < LinuxVersionCode(2,2,0)
  203.    #error "This driver only works with kernel 2.2.0 and later"
  204. #elif LINUX_VERSION_CODE <= LinuxVersionCode(2,3,18)
  205.     #define dma_addr_t uint32_t
  206.     
  207.     static inline void *pci_alloc_consistent(struct pci_dev *dev,int size,
  208.                                              dma_addr_t *dmahandle) {
  209.        void * ptr = kmalloc(size, GFP_ATOMIC); 
  210.        if(ptr){     
  211.           *dmahandle = VIRT_TO_BUS(ptr);
  212.        }
  213.        return ptr;
  214.     }
  215.     
  216.     #define pci_free_consistent(a,size,address,dmahandle) kfree(address)
  217.     
  218.     #define pci_map_sg(a,b,n,z)       (n)
  219.     #define pci_unmap_sg(a,b,c,d)     
  220.     #define pci_map_single(a,b,c,d)   (VIRT_TO_BUS(b))
  221.     #define pci_unmap_single(a,b,c,d) 
  222.     #ifndef sg_dma_address
  223.       #define sg_dma_address(x)         (VIRT_TO_BUS((x)->address))
  224.       #define sg_dma_len(x)             ((x)->length)
  225.     #endif
  226.     #define pci_unregister_driver(x)
  227. #endif
  228. #ifdef IPS_DEBUG
  229.    #define METHOD_TRACE(s, i)    if (ips_debug >= (i+10)) printk(KERN_NOTICE s "n");
  230.    #define DEBUG(i, s)           if (ips_debug >= i) printk(KERN_NOTICE s "n");
  231.    #define DEBUG_VAR(i, s, v...) if (ips_debug >= i) printk(KERN_NOTICE s "n", v);
  232. #else
  233.    #define METHOD_TRACE(s, i)
  234.    #define DEBUG(i, s)
  235.    #define DEBUG_VAR(i, s, v...)
  236. #endif
  237. /*
  238.  * global variables
  239.  */
  240. static const char   ips_name[] = "ips";
  241. static struct Scsi_Host *ips_sh[IPS_MAX_ADAPTERS];   /* Array of host controller structures */
  242. static ips_ha_t    *ips_ha[IPS_MAX_ADAPTERS];        /* Array of HA structures */
  243. static unsigned int ips_next_controller = 0;
  244. static unsigned int ips_num_controllers = 0;
  245. static unsigned int ips_released_controllers = 0;
  246. static int          ips_cmd_timeout = 60;
  247. static int          ips_reset_timeout = 60 * 5;
  248. static int          ips_force_memio = 1;             /* Always use Memory Mapped I/O    */
  249. static int          ips_force_i2o = 1;               /* Always use I2O command delivery */
  250. static int          ips_ioctlsize = IPS_IOCTL_SIZE;  /* Size of the ioctl buffer        */
  251. static int          ips_cd_boot = 0;                 /* Booting from ServeRAID Manager CD */
  252. static char        *ips_FlashData = NULL;            /* CD Boot - Flash Data Buffer      */
  253. static int          ips_FlashDataInUse = 0;          /* CD Boot - Flash Data In Use Flag */
  254. static uint32_t     MaxLiteCmds = 32;                /* Max Active Cmds for a Lite Adapter */  
  255. IPS_DEFINE_COMPAT_TABLE( Compatable );               /* Version Compatability Table      */
  256. #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
  257.    /* This table describes any / all ServeRAID Adapters */
  258.    static struct  pci_device_id  ips_pci_table[]  __devinitdata = {
  259.            { 0x1014, 0x002E, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
  260.            { 0x1014, 0x01BD, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
  261.            { 0, }
  262.    };
  263.    /* This table describes only Sarasota ( ServeRAID 5i ) Adapters */
  264.    static struct  pci_device_id  ips_pci_table_5i[]  __devinitdata = {
  265.                    { 0x1014, 0x01BD, PCI_ANY_ID, 0x259, 0, 0 },
  266.                    { 0x1014, 0x01BD, PCI_ANY_ID, 0x258, 0, 0 },
  267.                    { 0, }
  268.                    };   
  269.     
  270.    /* This table describes all i960 Adapters */
  271.    static struct  pci_device_id  ips_pci_table_i960[]  __devinitdata = {
  272.                    { 0x1014, 0x01BD, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
  273.                    { 0, }
  274.                    };   
  275.    MODULE_DEVICE_TABLE( pci, ips_pci_table );
  276.    static char ips_hot_plug_name[] = "ips";
  277.    
  278.    static int __devinit  ips_insert_device(struct pci_dev *pci_dev, const struct pci_device_id *ent);
  279.    static void __devexit ips_remove_device(struct pci_dev *pci_dev);
  280.    
  281.    struct pci_driver ips_pci_driver = {
  282.        name: ips_hot_plug_name,
  283.        id_table: ips_pci_table,
  284.        probe: ips_insert_device,
  285.        remove: __devexit_p(ips_remove_device),
  286.    }; 
  287.            
  288.    struct pci_driver ips_pci_driver_5i = {
  289.        name: ips_hot_plug_name,
  290.        id_table: ips_pci_table_5i,
  291.        probe: ips_insert_device,
  292.        remove: __devexit_p(ips_remove_device),
  293.    };
  294.    struct pci_driver ips_pci_driver_i960 = {
  295.        name: ips_hot_plug_name,
  296.        id_table: ips_pci_table_i960,
  297.        probe: ips_insert_device,
  298.        remove: __devexit_p(ips_remove_device),
  299.    };
  300. #endif
  301. /*
  302.  * Necessary forward function protoypes
  303.  */
  304. static int ips_halt(struct notifier_block *nb, ulong event, void *buf);
  305. #define MAX_ADAPTER_NAME 11
  306. static char ips_adapter_name[][30] = {
  307.    "ServeRAID",
  308.    "ServeRAID II",
  309.    "ServeRAID on motherboard",
  310.    "ServeRAID on motherboard",
  311.    "ServeRAID 3H",
  312.    "ServeRAID 3L",
  313.    "ServeRAID 4H",
  314.    "ServeRAID 4M",
  315.    "ServeRAID 4L",
  316.    "ServeRAID 4Mx",
  317.    "ServeRAID 4Lx",
  318.    "ServeRAID 5i",
  319.    "ServeRAID 5i"
  320. };
  321. static struct notifier_block ips_notifier = {
  322.    ips_halt, NULL, 0
  323. };
  324. /*
  325.  * Direction table
  326.  */
  327. static char ips_command_direction[] = {
  328. IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_IN,   IPS_DATA_IN,   IPS_DATA_OUT,
  329. IPS_DATA_IN,   IPS_DATA_IN,   IPS_DATA_OUT,  IPS_DATA_IN,   IPS_DATA_UNK,
  330. IPS_DATA_OUT,  IPS_DATA_OUT,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  331. IPS_DATA_IN,   IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_IN,   IPS_DATA_OUT,
  332. IPS_DATA_IN,   IPS_DATA_OUT,  IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_OUT,
  333. IPS_DATA_NONE, IPS_DATA_IN,   IPS_DATA_NONE, IPS_DATA_IN,   IPS_DATA_OUT,
  334. IPS_DATA_NONE, IPS_DATA_UNK,  IPS_DATA_IN,   IPS_DATA_UNK,  IPS_DATA_IN,
  335. IPS_DATA_UNK,  IPS_DATA_OUT,  IPS_DATA_IN,   IPS_DATA_UNK,  IPS_DATA_UNK,
  336. IPS_DATA_IN,   IPS_DATA_IN,   IPS_DATA_OUT,  IPS_DATA_NONE, IPS_DATA_UNK,
  337. IPS_DATA_IN,   IPS_DATA_OUT,  IPS_DATA_OUT,  IPS_DATA_OUT,  IPS_DATA_OUT,
  338. IPS_DATA_OUT,  IPS_DATA_NONE, IPS_DATA_IN,   IPS_DATA_NONE, IPS_DATA_NONE,
  339. IPS_DATA_IN,   IPS_DATA_OUT,  IPS_DATA_OUT,  IPS_DATA_OUT,  IPS_DATA_OUT,
  340. IPS_DATA_IN,   IPS_DATA_OUT,  IPS_DATA_IN,   IPS_DATA_OUT,  IPS_DATA_OUT,
  341. IPS_DATA_OUT,  IPS_DATA_IN,   IPS_DATA_IN,   IPS_DATA_IN,   IPS_DATA_NONE,
  342. IPS_DATA_UNK,  IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_UNK,
  343. IPS_DATA_NONE, IPS_DATA_OUT,  IPS_DATA_IN,   IPS_DATA_UNK,  IPS_DATA_UNK,
  344. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  345. IPS_DATA_OUT,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  346. IPS_DATA_IN,   IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  347. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  348. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  349. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  350. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  351. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  352. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  353. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  354. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  355. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  356. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  357. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  358. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  359. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  360. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  361. IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_UNK,  IPS_DATA_IN,   IPS_DATA_NONE,
  362. IPS_DATA_OUT,  IPS_DATA_UNK,  IPS_DATA_NONE, IPS_DATA_UNK,  IPS_DATA_OUT,
  363. IPS_DATA_OUT,  IPS_DATA_OUT,  IPS_DATA_OUT,  IPS_DATA_OUT,  IPS_DATA_NONE,
  364. IPS_DATA_UNK,  IPS_DATA_IN,   IPS_DATA_OUT,  IPS_DATA_IN,   IPS_DATA_IN,
  365. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  366. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  367. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  368. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  369. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  370. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  371. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  372. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  373. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  374. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_OUT,
  375. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  376. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  377. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,
  378. IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK,  IPS_DATA_UNK
  379. };
  380. /*
  381.  * Function prototypes
  382.  */
  383. int ips_detect(Scsi_Host_Template *);
  384. int ips_release(struct Scsi_Host *);
  385. int ips_eh_abort(Scsi_Cmnd *);
  386. int ips_eh_reset(Scsi_Cmnd *);
  387. int ips_queue(Scsi_Cmnd *, void (*) (Scsi_Cmnd *));
  388. int ips_biosparam(Disk *, kdev_t, int *);
  389. const char * ips_info(struct Scsi_Host *);
  390. void do_ipsintr(int, void *, struct pt_regs *);
  391. static int ips_hainit(ips_ha_t *);
  392. static int ips_map_status(ips_ha_t *, ips_scb_t *, ips_stat_t *);
  393. static int ips_send_wait(ips_ha_t *, ips_scb_t *, int, int);
  394. static int ips_send_cmd(ips_ha_t *, ips_scb_t *);
  395. static int ips_online(ips_ha_t *, ips_scb_t *);
  396. static int ips_inquiry(ips_ha_t *, ips_scb_t *);
  397. static int ips_rdcap(ips_ha_t *, ips_scb_t *);
  398. static int ips_msense(ips_ha_t *, ips_scb_t *);
  399. static int ips_reqsen(ips_ha_t *, ips_scb_t *);
  400. static int ips_deallocatescbs(ips_ha_t *, int);
  401. static int ips_allocatescbs(ips_ha_t *);
  402. static int ips_reset_copperhead(ips_ha_t *);
  403. static int ips_reset_copperhead_memio(ips_ha_t *);
  404. static int ips_reset_morpheus(ips_ha_t *);
  405. static int ips_issue_copperhead(ips_ha_t *, ips_scb_t *);
  406. static int ips_issue_copperhead_memio(ips_ha_t *, ips_scb_t *);
  407. static int ips_issue_i2o(ips_ha_t *, ips_scb_t *);
  408. static int ips_issue_i2o_memio(ips_ha_t *, ips_scb_t *);
  409. static int ips_isintr_copperhead(ips_ha_t *);
  410. static int ips_isintr_copperhead_memio(ips_ha_t *);
  411. static int ips_isintr_morpheus(ips_ha_t *);
  412. static int ips_wait(ips_ha_t *, int, int);
  413. static int ips_write_driver_status(ips_ha_t *, int);
  414. static int ips_read_adapter_status(ips_ha_t *, int);
  415. static int ips_read_subsystem_parameters(ips_ha_t *, int);
  416. static int ips_read_config(ips_ha_t *, int);
  417. static int ips_clear_adapter(ips_ha_t *, int);
  418. static int ips_readwrite_page5(ips_ha_t *, int, int);
  419. static int ips_init_copperhead(ips_ha_t *);
  420. static int ips_init_copperhead_memio(ips_ha_t *);
  421. static int ips_init_morpheus(ips_ha_t *);
  422. static int ips_isinit_copperhead(ips_ha_t *);
  423. static int ips_isinit_copperhead_memio(ips_ha_t *);
  424. static int ips_isinit_morpheus(ips_ha_t *);
  425. static int ips_erase_bios(ips_ha_t *);
  426. static int ips_program_bios(ips_ha_t *, char *, uint32_t, uint32_t);
  427. static int ips_verify_bios(ips_ha_t *, char *, uint32_t, uint32_t);
  428. static int ips_erase_bios_memio(ips_ha_t *);
  429. static int ips_program_bios_memio(ips_ha_t *, char *, uint32_t, uint32_t);
  430. static int ips_verify_bios_memio(ips_ha_t *, char *, uint32_t, uint32_t);
  431. static int ips_flash_copperhead(ips_ha_t *, ips_passthru_t *, ips_scb_t *);
  432. static int ips_flash_bios(ips_ha_t *, ips_passthru_t *, ips_scb_t *);
  433. static int ips_flash_firmware(ips_ha_t *, ips_passthru_t *, ips_scb_t *);
  434. static void ips_free_flash_copperhead(ips_ha_t *ha);
  435. static void ips_get_bios_version(ips_ha_t *, int);
  436. static void ips_identify_controller(ips_ha_t *);
  437. static void ips_select_queue_depth(struct Scsi_Host *, Scsi_Device *);
  438. static void ips_chkstatus(ips_ha_t *, IPS_STATUS *);
  439. static void ips_enable_int_copperhead(ips_ha_t *);
  440. static void ips_enable_int_copperhead_memio(ips_ha_t *);
  441. static void ips_enable_int_morpheus(ips_ha_t *);
  442. static void ips_intr_copperhead(ips_ha_t *);
  443. static void ips_intr_morpheus(ips_ha_t *);
  444. static void ips_next(ips_ha_t *, int);
  445. static void ipsintr_blocking(ips_ha_t *, struct ips_scb *);
  446. static void ipsintr_done(ips_ha_t *, struct ips_scb *);
  447. static void ips_done(ips_ha_t *, ips_scb_t *);
  448. static void ips_free(ips_ha_t *);
  449. static void ips_init_scb(ips_ha_t *, ips_scb_t *);
  450. static void ips_freescb(ips_ha_t *, ips_scb_t *);
  451. static void ips_statinit(ips_ha_t *);
  452. static void ips_statinit_memio(ips_ha_t *);
  453. static void ips_fix_ffdc_time(ips_ha_t *, ips_scb_t *, time_t);
  454. static void ips_ffdc_reset(ips_ha_t *, int);
  455. static void ips_ffdc_time(ips_ha_t *);
  456. static uint32_t ips_statupd_copperhead(ips_ha_t *);
  457. static uint32_t ips_statupd_copperhead_memio(ips_ha_t *);
  458. static uint32_t ips_statupd_morpheus(ips_ha_t *);
  459. static ips_scb_t * ips_getscb(ips_ha_t *);
  460. static inline void ips_putq_scb_head(ips_scb_queue_t *, ips_scb_t *);
  461. static inline void ips_putq_scb_tail(ips_scb_queue_t *, ips_scb_t *);
  462. static inline void ips_putq_wait_head(ips_wait_queue_t *, Scsi_Cmnd *);
  463. static inline void ips_putq_wait_tail(ips_wait_queue_t *, Scsi_Cmnd *);
  464. static inline void ips_putq_copp_head(ips_copp_queue_t *, ips_copp_wait_item_t *);
  465. static inline void ips_putq_copp_tail(ips_copp_queue_t *, ips_copp_wait_item_t *);
  466. static inline ips_scb_t * ips_removeq_scb_head(ips_scb_queue_t *);
  467. static inline ips_scb_t * ips_removeq_scb(ips_scb_queue_t *, ips_scb_t *);
  468. static inline Scsi_Cmnd * ips_removeq_wait_head(ips_wait_queue_t *);
  469. static inline Scsi_Cmnd * ips_removeq_wait(ips_wait_queue_t *, Scsi_Cmnd *);
  470. static inline ips_copp_wait_item_t * ips_removeq_copp(ips_copp_queue_t *, ips_copp_wait_item_t *);
  471. static inline ips_copp_wait_item_t * ips_removeq_copp_head(ips_copp_queue_t *);
  472. static int ips_is_passthru(Scsi_Cmnd *);
  473. static int ips_make_passthru(ips_ha_t *, Scsi_Cmnd *, ips_scb_t *, int);
  474. static int ips_usrcmd(ips_ha_t *, ips_passthru_t *, ips_scb_t *);
  475. static void ips_cleanup_passthru(ips_ha_t *, ips_scb_t *);
  476. int  ips_proc_info(char *, char **, off_t, int, int, int);
  477. static int ips_host_info(ips_ha_t *, char *, off_t, int);
  478. static void copy_mem_info(IPS_INFOSTR *, char *, int);
  479. static int copy_info(IPS_INFOSTR *, char *, ...);
  480. static int ips_get_version_info(ips_ha_t *ha, IPS_VERSION_DATA *Buffer, int intr );
  481. static void ips_version_check(ips_ha_t *ha, int intr);
  482. static int ips_init_phase2( int index );
  483. #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
  484. static int ips_init_phase1( struct pci_dev *pci_dev, int *indexPtr );
  485. #endif
  486. /*--------------------------------------------------------------------------*/
  487. /* Exported Functions                                                       */
  488. /*--------------------------------------------------------------------------*/
  489. /****************************************************************************/
  490. /*                                                                          */
  491. /* Routine Name: ips_setup                                                  */
  492. /*                                                                          */
  493. /* Routine Description:                                                     */
  494. /*                                                                          */
  495. /*   setup parameters to the driver                                         */
  496. /*                                                                          */
  497. /****************************************************************************/
  498. #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
  499. static int
  500. ips_setup(char *ips_str) {
  501. #else
  502. void
  503. ips_setup(char *ips_str, int *dummy) {
  504. #endif
  505.    int        i;
  506.    char      *key;
  507.    char      *value;
  508.    IPS_OPTION options[] = {
  509.       {"noi2o", &ips_force_i2o, 0},
  510.       {"nommap", &ips_force_memio, 0},
  511.       {"ioctlsize", &ips_ioctlsize, IPS_IOCTL_SIZE},
  512.       {"cdboot", &ips_cd_boot, 0},
  513.       {"maxcmds", &MaxLiteCmds, 32},
  514.    };
  515.     
  516.    /* Don't use strtok() anymore ( if 2.4 Kernel or beyond ) */
  517. #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
  518.    /* Search for value */
  519.    while ((key = strsep(&ips_str, ",."))) {
  520.       if (!*key)
  521.          continue;
  522.       value = strchr(key, ':');
  523.       if (value)
  524.          *value++ = '';
  525.      /*
  526.       * We now have key/value pairs.
  527.       * Update the variables
  528.       */
  529.       for (i = 0; i < (sizeof(options) / sizeof(options[0])); i++) {
  530.         if (strnicmp(key, options[i].option_name, strlen(options[i].option_name)) == 0) {
  531.            if (value)
  532.               *options[i].option_flag = simple_strtoul(value, NULL, 0);
  533.            else
  534.               *options[i].option_flag = options[i].option_value;
  535.            break;
  536.         }
  537.       }
  538.    }
  539.    return (1);
  540. #else
  541.   char      *p;
  542.    char       tokens[3] = {',', '.', 0};
  543.    for (key = strtok(ips_str, tokens); key; key = strtok(NULL, tokens)) {
  544.       p = key;
  545.       /* Search for value */
  546.       while ((p) && (*p != ':'))
  547.          p++;
  548.       if (p) {
  549.          *p = '';
  550.          value = p+1;
  551.       } else
  552.          value = NULL;
  553.       /*
  554.        * We now have key/value pairs.
  555.        * Update the variables
  556.        */
  557.       for (i = 0; i < (sizeof(options) / sizeof(options[0])); i++) {
  558.          if (strnicmp(key, options[i].option_name, strlen(ips_str)) == 0) {
  559.             if (value)
  560.                *options[i].option_flag = simple_strtoul(value, NULL, 0);
  561.             else
  562.                *options[i].option_flag = options[i].option_value;
  563.             break;
  564.          }
  565.       }
  566.    }
  567. #endif
  568. }
  569. #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
  570. __setup("ips=", ips_setup);
  571. #endif
  572. /****************************************************************************/
  573. /*                                                                          */
  574. /* Routine Name: ips_detect                                                 */
  575. /*                                                                          */
  576. /* Routine Description:                                                     */
  577. /*                                                                          */
  578. /*   Detect and initialize the driver                                       */
  579. /*                                                                          */
  580. /* NOTE: this routine is called under the io_request_lock spinlock          */
  581. /*                                                                          */
  582. /****************************************************************************/
  583. int
  584. ips_detect(Scsi_Host_Template *SHT) {
  585.    struct Scsi_Host *sh;
  586.    ips_ha_t         *ha;
  587.    uint32_t          io_addr;
  588.    uint32_t          mem_addr;
  589.    uint32_t          io_len;
  590.    uint32_t          mem_len;
  591.    uint16_t          planer;
  592.    uint8_t           revision_id;
  593.    uint8_t           bus;
  594.    uint8_t           func;
  595.    uint8_t           irq;
  596.    uint16_t          deviceID[2];
  597.    uint16_t          subdevice_id;
  598.    int               i;
  599.    int               j;
  600.    uint32_t          count;
  601.    char             *ioremap_ptr;
  602.    char             *mem_ptr;
  603.    struct pci_dev   *dev[2];
  604.    struct pci_dev   *morpheus = NULL;
  605.    struct pci_dev   *trombone = NULL;
  606. #if LINUX_VERSION_CODE < LinuxVersionCode(2,4,0)
  607.    uint32_t          currbar;
  608.    uint32_t          maskbar;
  609.    uint8_t           barnum;
  610. #endif
  611.    uint32_t          IsDead;
  612.    METHOD_TRACE("ips_detect", 1);
  613. #ifdef MODULE
  614.    if (ips)
  615. #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
  616.       ips_setup(ips);
  617. #else
  618.       ips_setup(ips, NULL);
  619. #endif
  620. #endif
  621.    /* If Booting from the ServeRAID Manager CD, Allocate a large Flash  */
  622.    /* Buffer ( so we won't need to allocate one for each adapter ).     */
  623.     if ( ips_cd_boot ) {            
  624.       ips_FlashData = ( char * ) __get_free_pages( GFP_KERNEL, 7 );   
  625.       if (ips_FlashData == NULL) {
  626.          /* The validity of this pointer is checked in ips_make_passthru() before it is used */
  627.          printk( KERN_WARNING "ERROR: Can't Allocate Large Buffer for Flashingn" );
  628.       }
  629.    }                                                                               
  630.    SHT->proc_info = ips_proc_info;
  631. #if LINUX_VERSION_CODE < LinuxVersionCode(2,4,0)
  632.    SHT->proc_dir = &proc_scsi_ips;
  633. #else
  634.    SHT->proc_name = "ips";
  635. #endif
  636. #if defined(CONFIG_PCI)
  637.    /* initalize number of controllers */
  638.    ips_num_controllers = 0;
  639.    ips_next_controller = 0;
  640.    ips_released_controllers = 0;
  641.    if (!pci_present())
  642.       return (0);
  643.    morpheus = pci_find_device(IPS_VENDORID, IPS_DEVICEID_MORPHEUS, morpheus);
  644.    trombone = pci_find_device(IPS_VENDORID, IPS_DEVICEID_COPPERHEAD, trombone);
  645.    /* determine which controller to probe first */
  646.    if (!morpheus) {
  647.       /* we only have trombone */
  648.       dev[0] = trombone;
  649.       dev[1] = NULL;
  650.       deviceID[0] = IPS_DEVICEID_COPPERHEAD;
  651.    } else if (!trombone) {
  652.       /* we only have morpheus */
  653.       dev[0] = morpheus;
  654.       dev[1] = NULL;
  655.       deviceID[0] = IPS_DEVICEID_MORPHEUS;
  656.    } else {
  657.       /* we have both in the system */
  658.       if (trombone->bus->number < morpheus->bus->number) {
  659.          dev[0] = trombone;
  660.          dev[1] = morpheus;
  661.          deviceID[0] = IPS_DEVICEID_COPPERHEAD;
  662.          deviceID[1] = IPS_DEVICEID_MORPHEUS;
  663.       } else if (trombone->bus->number > morpheus->bus->number) {
  664.          dev[0] = morpheus;
  665.          dev[1] = trombone;
  666.          deviceID[0] = IPS_DEVICEID_MORPHEUS;
  667.          deviceID[1] = IPS_DEVICEID_COPPERHEAD;
  668.       } else {
  669.          /* further detection required */
  670.          if (trombone->devfn < morpheus->devfn) {
  671.             dev[0] = trombone;
  672.             dev[1] = morpheus;
  673.             deviceID[0] = IPS_DEVICEID_COPPERHEAD;
  674.             deviceID[1] = IPS_DEVICEID_MORPHEUS;
  675.          } else {
  676.             dev[0] = morpheus;
  677.             dev[1] = trombone;
  678.             deviceID[0] = IPS_DEVICEID_MORPHEUS;
  679.             deviceID[1] = IPS_DEVICEID_COPPERHEAD;
  680.          }
  681.       }
  682.    }
  683. /**********************************************************************************/
  684. /* For Kernel Versions 2.4 or greater, use new PCI ( Hot Pluggable ) architecture */
  685. /**********************************************************************************/
  686. #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
  687.    spin_unlock_irq(&io_request_lock);
  688.    /* By definition, a Sarasota ( 5i ) Adapter MUST be enumerated first or the */
  689.    /* server may not boot properly. The adapters must be enumerated in exactly */
  690.    /* the same order as ServeRAID BIOS for the machine to come up properly.    */
  691.    pci_module_init(&ips_pci_driver_5i);          /* Ask for 5i Adapters First  */
  692.    if (ips_num_controllers)                      /* If there is a 5i Adapter   */
  693.       pci_module_init(&ips_pci_driver_i960);     /*    Get all i960's next     */
  694.    pci_module_init(&ips_pci_driver);             /* Get all remaining Adapters */
  695.                                                  /*  ( in normal BUS order )   */
  696.    spin_lock_irq(&io_request_lock);
  697.    if (ips_num_controllers > 0) 
  698.       register_reboot_notifier(&ips_notifier);
  699.    return (ips_num_controllers);
  700. #endif
  701.    /* Now scan the controllers */
  702.    for (i = 0; i < 2; i++) {
  703.       if (!dev[i])
  704.          break;
  705.       do {
  706.          if (ips_next_controller >= IPS_MAX_ADAPTERS)
  707.             break;
  708. #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
  709.          if (pci_enable_device(dev[i]))
  710.             break;
  711. #endif
  712.          /* stuff that we get in dev */
  713.          irq = dev[i]->irq;
  714.          bus = dev[i]->bus->number;
  715.          func = dev[i]->devfn;
  716.          /* Init MEM/IO addresses to 0 */
  717.          mem_addr = 0;
  718.          io_addr = 0;
  719.          mem_len = 0;
  720.          io_len = 0;
  721.          for (j = 0; j < 2; j++) {
  722. #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
  723.             if (!pci_resource_start(dev[i], j))
  724.                break;
  725.             if (pci_resource_flags(dev[i], j) & IORESOURCE_IO) {
  726.                io_addr = pci_resource_start(dev[i], j);
  727.                io_len = pci_resource_len(dev[i], j);
  728.             } else {
  729.                mem_addr = pci_resource_start(dev[i], j);
  730.                mem_len = pci_resource_len(dev[i], j);
  731.             }
  732. #else
  733.             if (!dev[i]->base_address[j])
  734.                break;
  735.             if ((dev[i]->base_address[j] & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
  736.                barnum = PCI_BASE_ADDRESS_0 + (j * 4);
  737.                io_addr = dev[i]->base_address[j] & PCI_BASE_ADDRESS_IO_MASK;
  738.                /* Get Size */
  739.                pci_read_config_dword(dev[i], barnum, &currbar);
  740.                pci_write_config_dword(dev[i], barnum, ~0);
  741.                pci_read_config_dword(dev[i], barnum, &maskbar);
  742.                pci_write_config_dword(dev[i], barnum, currbar);
  743.                io_len = ~(maskbar & PCI_BASE_ADDRESS_IO_MASK) + 1;
  744.             } else {
  745.                barnum = PCI_BASE_ADDRESS_0 + (j * 4);
  746.                mem_addr = dev[i]->base_address[j] & PCI_BASE_ADDRESS_MEM_MASK;
  747.                /* Get Size */
  748.                pci_read_config_dword(dev[i], barnum, &currbar);
  749.                pci_write_config_dword(dev[i], barnum, ~0);
  750.                pci_read_config_dword(dev[i], barnum, &maskbar);
  751.                pci_write_config_dword(dev[i], barnum, currbar);
  752.                mem_len = ~(maskbar & PCI_BASE_ADDRESS_MEM_MASK) + 1;
  753.             }
  754. #endif
  755.          }
  756.          /* setup memory mapped area (if applicable) */
  757.          if (mem_addr) {
  758.             uint32_t base;
  759.             uint32_t offs;
  760.             DEBUG_VAR(1, "(%s%d) detect, Memory region %x, size: %d",
  761.                       ips_name, ips_next_controller, mem_addr, mem_len);
  762. #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
  763.             if (check_mem_region(mem_addr, mem_len)) {
  764.                /* Couldn't allocate io space */
  765.                printk(KERN_WARNING "(%s%d) couldn't allocate IO space %x len %d.n",
  766.                       ips_name, ips_next_controller, io_addr, io_len);
  767.                ips_next_controller++;
  768.                continue;
  769.             }
  770.             request_mem_region(mem_addr, mem_len, "ips");
  771. #endif
  772.             base = mem_addr & PAGE_MASK;
  773.             offs = mem_addr - base;
  774.             ioremap_ptr = ioremap(base, PAGE_SIZE);
  775.             mem_ptr = ioremap_ptr + offs;
  776.          } else {
  777.             ioremap_ptr = NULL;
  778.             mem_ptr = NULL;
  779.          }
  780.          /* setup I/O mapped area (if applicable) */
  781.          if (io_addr) {
  782.             DEBUG_VAR(1, "(%s%d) detect, IO region %x, size: %d",
  783.                       ips_name, ips_next_controller, io_addr, io_len);
  784.             if (check_region(io_addr, io_len)) {
  785.                /* Couldn't allocate io space */
  786.                printk(KERN_WARNING "(%s%d) couldn't allocate IO space %x len %d.n",
  787.                       ips_name, ips_next_controller, io_addr, io_len);
  788.                ips_next_controller++;
  789.                continue;
  790.             }
  791.             request_region(io_addr, io_len, "ips");
  792.          }
  793.          /* get planer status */
  794.          if (pci_read_config_word(dev[i], 0x04, &planer)) {
  795.             printk(KERN_WARNING "(%s%d) can't get planer status.n",
  796.                    ips_name, ips_next_controller);
  797.             ips_next_controller++;
  798.             continue;
  799.          }
  800.          /* check to see if an onboard planer controller is disabled */
  801.          if (!(planer & 0x000C)) {
  802.             DEBUG_VAR(1, "(%s%d) detect, Onboard ServeRAID disabled by BIOS",
  803.                       ips_name, ips_next_controller);
  804.             ips_next_controller++;
  805.             continue;
  806.          }
  807.          DEBUG_VAR(1, "(%s%d) detect bus %d, func %x, irq %d, io %x, mem: %x, ptr: %p",
  808.                    ips_name, ips_next_controller, bus, func, irq, io_addr, mem_addr, mem_ptr);
  809.          /* get the revision ID */
  810.          if (pci_read_config_byte(dev[i], PCI_REVISION_ID, &revision_id)) {
  811.             printk(KERN_WARNING "(%s%d) can't get revision id.n",
  812.                    ips_name, ips_next_controller);
  813.             ips_next_controller++;
  814.             continue;
  815.          }
  816. #if LINUX_VERSION_CODE < LinuxVersionCode(2,4,0)
  817.          /* get the subdevice id */
  818.          if (pci_read_config_word(dev[i], PCI_SUBSYSTEM_ID, &subdevice_id)) {
  819.             printk(KERN_WARNING "(%s%d) can't get subdevice id.n",
  820.                    ips_name, ips_next_controller);
  821.             ips_next_controller++;
  822.             continue;
  823.          }
  824. #else
  825.          subdevice_id = dev[i]->subsystem_device;
  826. #endif
  827.          /* found a controller */
  828.          sh = scsi_register(SHT, sizeof(ips_ha_t));
  829.          if (sh == NULL) {
  830.             printk(KERN_WARNING "(%s%d) Unable to register controller with SCSI subsystem - skipping controllern",
  831.                    ips_name, ips_next_controller);
  832.             ips_next_controller++;
  833.             continue;
  834.          }
  835.          ha = IPS_HA(sh);
  836.          memset(ha, 0, sizeof(ips_ha_t));
  837.          /* Initialize spin lock */
  838.          spin_lock_init(&ha->scb_lock);
  839.          spin_lock_init(&ha->copp_lock);
  840.          spin_lock_init(&ha->ips_lock);
  841.          spin_lock_init(&ha->copp_waitlist.lock);
  842.          spin_lock_init(&ha->scb_waitlist.lock);
  843.          spin_lock_init(&ha->scb_activelist.lock);
  844.          ips_sh[ips_next_controller] = sh;
  845.          ips_ha[ips_next_controller] = ha;
  846.          ips_num_controllers++;
  847.          ha->active = 1;
  848.          ha->enq = kmalloc(sizeof(IPS_ENQ), GFP_KERNEL);
  849.          if (!ha->enq) {
  850.             printk(KERN_WARNING "(%s%d) Unable to allocate host inquiry structure - skipping contollern",
  851.                    ips_name, ips_next_controller);
  852.             ha->active = 0;
  853.             ips_free(ha);
  854.             scsi_unregister(sh);
  855.             ips_ha[ips_next_controller] = 0;
  856.             ips_sh[ips_next_controller] = 0;
  857.             ips_next_controller++;
  858.             ips_num_controllers--;
  859.             continue;
  860.          }
  861.          ha->adapt = kmalloc(sizeof(IPS_ADAPTER), GFP_KERNEL);
  862.          if (!ha->adapt) {
  863.             printk(KERN_WARNING "(%s%d) Unable to allocate host adapt structure - skipping controllern",
  864.                    ips_name, ips_next_controller);
  865.             ha->active = 0;
  866.             ips_free(ha);
  867.             scsi_unregister(sh);
  868.             ips_ha[ips_next_controller] = 0;
  869.             ips_sh[ips_next_controller] = 0;
  870.             ips_next_controller++;
  871.             ips_num_controllers--;
  872.             continue;
  873.          }
  874.          ha->conf = kmalloc(sizeof(IPS_CONF), GFP_KERNEL);
  875.          if (!ha->conf) {
  876.             printk(KERN_WARNING "(%s%d) Unable to allocate host conf structure - skipping controllern",
  877.                    ips_name, ips_next_controller);
  878.             ha->active = 0;
  879.             ips_free(ha);
  880.             scsi_unregister(sh);
  881.             ips_ha[ips_next_controller] = 0;
  882.             ips_sh[ips_next_controller] = 0;
  883.             ips_next_controller++;
  884.             ips_num_controllers--;
  885.             continue;
  886.          }
  887.          ha->nvram = kmalloc(sizeof(IPS_NVRAM_P5), GFP_KERNEL);
  888.          if (!ha->nvram) {
  889.             printk(KERN_WARNING "(%s%d) Unable to allocate host nvram structure - skipping controllern",
  890.                    ips_name, ips_next_controller);
  891.             ha->active = 0;
  892.             ips_free(ha);
  893.             scsi_unregister(sh);
  894.             ips_ha[ips_next_controller] = 0;
  895.             ips_sh[ips_next_controller] = 0;
  896.             ips_next_controller++;
  897.             ips_num_controllers--;
  898.             continue;
  899.          }
  900.          ha->subsys = kmalloc(sizeof(IPS_SUBSYS), GFP_KERNEL);
  901.          if (!ha->subsys) {
  902.             printk(KERN_WARNING "(%s%d) Unable to allocate host subsystem structure - skipping controllern",
  903.                    ips_name, ips_next_controller);
  904.             ha->active = 0;
  905.             ips_free(ha);
  906.             scsi_unregister(sh);
  907.             ips_ha[ips_next_controller] = 0;
  908.             ips_sh[ips_next_controller] = 0;
  909.             ips_next_controller++;
  910.             ips_num_controllers--;
  911.             continue;
  912.          }
  913.          ha->dummy = kmalloc(sizeof(IPS_IO_CMD), GFP_KERNEL);
  914.          if (!ha->dummy) {
  915.             printk(KERN_WARNING "(%s%d) Unable to allocate host dummy structure - skipping controllern",
  916.                    ips_name, ips_next_controller);
  917.             ha->active = 0;
  918.             ips_free(ha);
  919.             scsi_unregister(sh);
  920.             ips_ha[ips_next_controller] = 0;
  921.             ips_sh[ips_next_controller] = 0;
  922.             ips_next_controller++;
  923.             ips_num_controllers--;
  924.             continue;
  925.          }
  926.          for (count = PAGE_SIZE, ha->ioctl_order = 0;
  927.               count < ips_ioctlsize;
  928.               ha->ioctl_order++, count <<= 1);
  929.          ha->ioctl_data = (char *) __get_free_pages(GFP_KERNEL, ha->ioctl_order);
  930.          ha->ioctl_datasize = count;
  931.          if (!ha->ioctl_data) {
  932.             printk(KERN_WARNING "(%s%d) Unable to allocate ioctl datan",
  933.                    ips_name, ips_next_controller);
  934.             ha->ioctl_data = NULL;
  935.             ha->ioctl_order = 0;
  936.             ha->ioctl_datasize = 0;
  937.          }
  938.          /* Store away needed values for later use */
  939.          sh->io_port = io_addr;
  940.          sh->n_io_port = io_addr ? 255 : 0;
  941.          sh->unique_id = (io_addr) ? io_addr : mem_addr;
  942.          sh->irq = irq;
  943.          sh->select_queue_depths = ips_select_queue_depth;
  944.          sh->sg_tablesize = sh->hostt->sg_tablesize;
  945.          sh->can_queue = sh->hostt->can_queue;
  946.          sh->cmd_per_lun = sh->hostt->cmd_per_lun;
  947.          sh->unchecked_isa_dma = sh->hostt->unchecked_isa_dma;
  948.          sh->use_clustering = sh->hostt->use_clustering;
  949. #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,7)
  950.          sh->max_sectors = 128;
  951. #endif                      
  952. #if LINUX_VERSION_CODE < LinuxVersionCode(2,4,0)
  953.          sh->wish_block = FALSE;
  954. #endif
  955.          /* Store info in HA structure */
  956.          ha->irq = irq;
  957.          ha->io_addr = io_addr;
  958.          ha->io_len = io_len;
  959.          ha->mem_addr = mem_addr;
  960.          ha->mem_len = mem_len;
  961.          ha->mem_ptr = mem_ptr;
  962.          ha->ioremap_ptr = ioremap_ptr;
  963.          ha->host_num = ips_next_controller;
  964.          ha->revision_id = revision_id;
  965.          ha->slot_num = PCI_SLOT(dev[i]->devfn);
  966.          ha->device_id = deviceID[i];
  967.          ha->subdevice_id = subdevice_id;
  968.          ha->pcidev = dev[i];
  969.          /*
  970.           * Setup Functions
  971.           */
  972.          if (IPS_IS_MORPHEUS(ha)) {
  973.             /* morpheus */
  974.             ha->func.isintr = ips_isintr_morpheus;
  975.             ha->func.isinit = ips_isinit_morpheus;
  976.             ha->func.issue = ips_issue_i2o_memio;
  977.             ha->func.init = ips_init_morpheus;
  978.             ha->func.statupd = ips_statupd_morpheus;
  979.             ha->func.reset = ips_reset_morpheus;
  980.             ha->func.intr = ips_intr_morpheus;
  981.             ha->func.enableint = ips_enable_int_morpheus;
  982.          } else if (IPS_USE_MEMIO(ha)) {
  983.             /* copperhead w/MEMIO */
  984.             ha->func.isintr = ips_isintr_copperhead_memio;
  985.             ha->func.isinit = ips_isinit_copperhead_memio;
  986.             ha->func.init = ips_init_copperhead_memio;
  987.             ha->func.statupd = ips_statupd_copperhead_memio;
  988.             ha->func.statinit = ips_statinit_memio;
  989.             ha->func.reset = ips_reset_copperhead_memio;
  990.             ha->func.intr = ips_intr_copperhead;
  991.             ha->func.erasebios = ips_erase_bios_memio;
  992.             ha->func.programbios = ips_program_bios_memio;
  993.             ha->func.verifybios = ips_verify_bios_memio;
  994.             ha->func.enableint = ips_enable_int_copperhead_memio;
  995.             if (IPS_USE_I2O_DELIVER(ha))
  996.                ha->func.issue = ips_issue_i2o_memio;
  997.             else
  998.                ha->func.issue = ips_issue_copperhead_memio;
  999.          } else {
  1000.             /* copperhead */
  1001.             ha->func.isintr = ips_isintr_copperhead;
  1002.             ha->func.isinit = ips_isinit_copperhead;
  1003.             ha->func.init = ips_init_copperhead;
  1004.             ha->func.statupd = ips_statupd_copperhead;
  1005.             ha->func.statinit = ips_statinit;
  1006.             ha->func.reset = ips_reset_copperhead;
  1007.             ha->func.intr = ips_intr_copperhead;
  1008.             ha->func.erasebios = ips_erase_bios;
  1009.             ha->func.programbios = ips_program_bios;
  1010.             ha->func.verifybios = ips_verify_bios;
  1011.             ha->func.enableint = ips_enable_int_copperhead;
  1012.             if (IPS_USE_I2O_DELIVER(ha))
  1013.                ha->func.issue = ips_issue_i2o;
  1014.             else
  1015.                ha->func.issue = ips_issue_copperhead;
  1016.          }
  1017.             
  1018.          /* If Morpheus appears dead, reset it */
  1019.          if ( IPS_IS_MORPHEUS( ha ) ) {
  1020.              IsDead = readl( ha->mem_ptr + IPS_REG_I960_MSG1 );
  1021.              if ( IsDead == 0xDEADBEEF ) {
  1022.                  ips_reset_morpheus( ha );
  1023.              }
  1024.          }
  1025.          /*
  1026.           * Initialize the card if it isn't already
  1027.           */
  1028.          if (!(*ha->func.isinit)(ha)) {
  1029.             if (!(*ha->func.init)(ha)) {
  1030.                /*
  1031.                 * Initialization failed
  1032.                 */
  1033.                printk(KERN_WARNING "(%s%d) unable to initialize controller - skipping controllern",
  1034.                       ips_name, ips_next_controller);
  1035.                ha->active = 0;
  1036.                ips_free(ha);
  1037.                scsi_unregister(sh);
  1038.                ips_ha[ips_next_controller] = 0;
  1039.                ips_sh[ips_next_controller] = 0;
  1040.                ips_next_controller++;
  1041.                ips_num_controllers--;
  1042.                continue;
  1043.             }
  1044.          }
  1045.          /* install the interrupt handler */
  1046.          if (request_irq(irq, do_ipsintr, SA_SHIRQ, ips_name, ha)) {
  1047.             printk(KERN_WARNING "(%s%d) unable to install interrupt handler - skipping controllern",
  1048.                    ips_name, ips_next_controller);
  1049.             ha->active = 0;
  1050.             ips_free(ha);
  1051.             scsi_unregister(sh);
  1052.             ips_ha[ips_next_controller] = 0;
  1053.             ips_sh[ips_next_controller] = 0;
  1054.             ips_next_controller++;
  1055.             ips_num_controllers--;
  1056.             continue;
  1057.          }
  1058.          /*
  1059.           * Allocate a temporary SCB for initialization
  1060.           */
  1061.          ha->max_cmds = 1;
  1062.          if (!ips_allocatescbs(ha)) {
  1063.             /* couldn't allocate a temp SCB */
  1064.             printk(KERN_WARNING "(%s%d) unable to allocate CCBs - skipping contollern",
  1065.                    ips_name, ips_next_controller);
  1066.             ha->active = 0;
  1067.             ips_free(ha);
  1068.             scsi_unregister(sh);
  1069.             ips_ha[ips_next_controller] = 0;
  1070.             ips_sh[ips_next_controller] = 0;
  1071.             free_irq(ha->irq, ha);
  1072.             ips_next_controller++;
  1073.             ips_num_controllers--;
  1074.             continue;
  1075.          }
  1076.          ips_next_controller++;
  1077.       } while ((dev[i] = pci_find_device(IPS_VENDORID, deviceID[i], dev[i])));
  1078.    }
  1079.    /*
  1080.     * Do Phase 2 Initialization
  1081.     * Controller init
  1082.     */
  1083.    for (i = 0; i < ips_next_controller; i++) {
  1084.       if (ips_ha[i] == 0) {
  1085.          printk(KERN_WARNING "(%s%d) ignoring bad controllern", ips_name, i);
  1086.          continue;
  1087.       }
  1088.       if (ips_init_phase2(i) != SUCCESS)
  1089.          ips_num_controllers--;
  1090.    }
  1091.    if (ips_num_controllers > 0)
  1092.       register_reboot_notifier(&ips_notifier);
  1093.    return (ips_num_controllers);
  1094. #else
  1095.    /* No PCI -- No ServeRAID */
  1096.    return (0);
  1097. #endif /* CONFIG_PCI */
  1098. }
  1099. /****************************************************************************/
  1100. /*                                                                          */
  1101. /* Routine Name: ips_release                                                */
  1102. /*                                                                          */
  1103. /* Routine Description:                                                     */
  1104. /*                                                                          */
  1105. /*   Remove a driver                                                        */
  1106. /*                                                                          */
  1107. /****************************************************************************/
  1108. int
  1109. ips_release(struct Scsi_Host *sh) {
  1110.    ips_scb_t *scb;
  1111.    ips_ha_t  *ha;
  1112.    int        i;
  1113.    METHOD_TRACE("ips_release", 1);
  1114.    for (i = 0; i < IPS_MAX_ADAPTERS && ips_sh[i] != sh; i++);
  1115.    if (i == IPS_MAX_ADAPTERS) {
  1116.       printk(KERN_WARNING "(%s) release, invalid Scsi_Host pointer.n",
  1117.             ips_name);
  1118. #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
  1119.       BUG();
  1120. #endif
  1121.       return (FALSE);
  1122.    }
  1123.    ha = IPS_HA(sh);
  1124.    if (!ha)
  1125.       return (FALSE);
  1126.    /* flush the cache on the controller */
  1127.    scb = &ha->scbs[ha->max_cmds-1];
  1128.    ips_init_scb(ha, scb);
  1129.    scb->timeout = ips_cmd_timeout;
  1130.    scb->cdb[0] = IPS_CMD_FLUSH;
  1131.    scb->cmd.flush_cache.op_code = IPS_CMD_FLUSH;
  1132.    scb->cmd.flush_cache.command_id = IPS_COMMAND_ID(ha, scb);
  1133.    scb->cmd.flush_cache.state = IPS_NORM_STATE;
  1134.    scb->cmd.flush_cache.reserved = 0;
  1135.    scb->cmd.flush_cache.reserved2 = 0;
  1136.    scb->cmd.flush_cache.reserved3 = 0;
  1137.    scb->cmd.flush_cache.reserved4 = 0;
  1138.    printk(KERN_NOTICE "(%s%d) Flushing Cache.n", ips_name, ha->host_num);
  1139.    /* send command */
  1140.    if (ips_send_wait(ha, scb, ips_cmd_timeout, IPS_INTR_ON) == IPS_FAILURE)
  1141.       printk(KERN_NOTICE "(%s%d) Incomplete Flush.n", ips_name, ha->host_num);
  1142.    printk(KERN_NOTICE "(%s%d) Flushing Complete.n", ips_name, ha->host_num);
  1143.    ips_sh[i] = NULL;
  1144.    ips_ha[i] = NULL;
  1145.    /* free extra memory */
  1146.    ips_free(ha);
  1147.    /* Free I/O Region */
  1148.    if (ha->io_addr)
  1149.       release_region(ha->io_addr, ha->io_len);
  1150.    /* free IRQ */
  1151.    free_irq(ha->irq, ha);
  1152.    scsi_unregister(sh);
  1153.    ips_released_controllers++;
  1154.    if (ips_num_controllers == ips_released_controllers){
  1155.       unregister_reboot_notifier(&ips_notifier);
  1156.       pci_unregister_driver(&ips_pci_driver);
  1157.    }
  1158.    return (FALSE);
  1159. }
  1160. /****************************************************************************/
  1161. /*                                                                          */
  1162. /* Routine Name: ips_halt                                                   */
  1163. /*                                                                          */
  1164. /* Routine Description:                                                     */
  1165. /*                                                                          */
  1166. /*   Perform cleanup when the system reboots                                */
  1167. /*                                                                          */
  1168. /****************************************************************************/
  1169. static int
  1170. ips_halt(struct notifier_block *nb, ulong event, void *buf) {
  1171.    ips_scb_t *scb;
  1172.    ips_ha_t  *ha;
  1173.    int        i;
  1174.    if ((event != SYS_RESTART) && (event != SYS_HALT) &&
  1175.        (event != SYS_POWER_OFF))
  1176.       return (NOTIFY_DONE);
  1177.    for (i = 0; i < ips_next_controller; i++) {
  1178.       ha = (ips_ha_t *) ips_ha[i];
  1179.       if (!ha)
  1180.          continue;
  1181.       if (!ha->active)
  1182.          continue;
  1183.       /* flush the cache on the controller */
  1184.       scb = &ha->scbs[ha->max_cmds-1];
  1185.       ips_init_scb(ha, scb);
  1186.       scb->timeout = ips_cmd_timeout;
  1187.       scb->cdb[0] = IPS_CMD_FLUSH;
  1188.       scb->cmd.flush_cache.op_code = IPS_CMD_FLUSH;
  1189.       scb->cmd.flush_cache.command_id = IPS_COMMAND_ID(ha, scb);
  1190.       scb->cmd.flush_cache.state = IPS_NORM_STATE;
  1191.       scb->cmd.flush_cache.reserved = 0;
  1192.       scb->cmd.flush_cache.reserved2 = 0;
  1193.       scb->cmd.flush_cache.reserved3 = 0;
  1194.       scb->cmd.flush_cache.reserved4 = 0;
  1195.       printk(KERN_NOTICE "(%s%d) Flushing Cache.n", ips_name, ha->host_num);
  1196.       /* send command */
  1197.       if (ips_send_wait(ha, scb, ips_cmd_timeout, IPS_INTR_ON) == IPS_FAILURE)
  1198.          printk(KERN_NOTICE "(%s%d) Incomplete Flush.n", ips_name, ha->host_num);
  1199.       else
  1200.          printk(KERN_NOTICE "(%s%d) Flushing Complete.n", ips_name, ha->host_num);
  1201.    }
  1202.    return (NOTIFY_OK);
  1203. }
  1204. /****************************************************************************/
  1205. /*                                                                          */
  1206. /* Routine Name: ips_eh_abort                                               */
  1207. /*                                                                          */
  1208. /* Routine Description:                                                     */
  1209. /*                                                                          */
  1210. /*   Abort a command (using the new error code stuff)                       */
  1211. /*                                                                          */
  1212. /****************************************************************************/
  1213. int
  1214. ips_eh_abort(Scsi_Cmnd *SC) {
  1215.    ips_ha_t         *ha;
  1216.    ips_copp_wait_item_t *item;
  1217.    METHOD_TRACE("ips_eh_abort", 1);
  1218.    if (!SC)
  1219.       return (FAILED);
  1220.    ha = (ips_ha_t *) SC->host->hostdata;
  1221.    if (!ha)
  1222.       return (FAILED);
  1223.    if (!ha->active)
  1224.       return (FAILED);
  1225.    if (SC->serial_number != SC->serial_number_at_timeout) {
  1226.       /* HMM, looks like a bogus command */
  1227.       DEBUG(1, "Abort called with bogus scsi command");
  1228.       return (FAILED);
  1229.    }
  1230.    if (test_and_set_bit(IPS_IN_ABORT, &ha->flags))
  1231.       return (FAILED);
  1232.    /* See if the command is on the copp queue */
  1233.    IPS_QUEUE_LOCK(&ha->copp_waitlist);
  1234.    item = ha->copp_waitlist.head;
  1235.    while ((item) && (item->scsi_cmd != SC))
  1236.       item = item->next;
  1237.    IPS_QUEUE_UNLOCK(&ha->copp_waitlist);
  1238.    if (item) {
  1239.       /* Found it */
  1240.       ips_removeq_copp(&ha->copp_waitlist, item);
  1241.       clear_bit(IPS_IN_ABORT, &ha->flags);
  1242.       return (SUCCESS);
  1243.    }
  1244.    /* See if the command is on the wait queue */
  1245.    if (ips_removeq_wait(&ha->scb_waitlist, SC)) {
  1246.       /* command not sent yet */
  1247.       clear_bit(IPS_IN_ABORT, &ha->flags);
  1248.       return (SUCCESS);
  1249.    } else {
  1250.       /* command must have already been sent */
  1251.       clear_bit(IPS_IN_ABORT, &ha->flags);
  1252.       return (FAILED);
  1253.    }
  1254. }
  1255. /****************************************************************************/
  1256. /*                                                                          */
  1257. /* Routine Name: ips_eh_reset                                               */
  1258. /*                                                                          */
  1259. /* Routine Description:                                                     */
  1260. /*                                                                          */
  1261. /*   Reset the controller (with new eh error code)                          */
  1262. /*                                                                          */
  1263. /* NOTE: this routine is called under the io_request_lock spinlock          */
  1264. /*                                                                          */
  1265. /****************************************************************************/
  1266. int
  1267. ips_eh_reset(Scsi_Cmnd *SC) {
  1268.    int                   ret;
  1269.    int                   i;
  1270.    ips_ha_t             *ha;
  1271.    ips_scb_t            *scb;
  1272.    ips_copp_wait_item_t *item;
  1273.    unsigned long         cpu_flags;
  1274.    METHOD_TRACE("ips_eh_reset", 1);
  1275. #ifdef NO_IPS_RESET
  1276.    return (FAILED);
  1277. #else
  1278.    if (!SC) {
  1279.       DEBUG(1, "Reset called with NULL scsi command");
  1280.       return (FAILED);
  1281.    }
  1282.    ha = (ips_ha_t *) SC->host->hostdata;
  1283.    if (!ha) {
  1284.       DEBUG(1, "Reset called with NULL ha struct");
  1285.       return (FAILED);
  1286.    }
  1287.    if (!ha->active)
  1288.       return (FAILED);
  1289.    if (test_and_set_bit(IPS_IN_RESET, &ha->flags))
  1290.       return (FAILED);
  1291.    /* See if the command is on the copp queue */
  1292.    IPS_QUEUE_LOCK(&ha->copp_waitlist);
  1293.    item = ha->copp_waitlist.head;
  1294.    while ((item) && (item->scsi_cmd != SC))
  1295.       item = item->next;
  1296.    IPS_QUEUE_UNLOCK(&ha->copp_waitlist);
  1297.    if (item) {
  1298.       /* Found it */
  1299.       ips_removeq_copp(&ha->copp_waitlist, item);
  1300.       clear_bit(IPS_IN_RESET, &ha->flags);
  1301.       return (SUCCESS);
  1302.    }
  1303.    /* See if the command is on the wait queue */
  1304.    if (ips_removeq_wait(&ha->scb_waitlist, SC)) {
  1305.       /* command not sent yet */
  1306.       clear_bit(IPS_IN_RESET, &ha->flags);
  1307.       return (SUCCESS);
  1308.    }
  1309.    /* An explanation for the casual observer:                              */
  1310.    /* Part of the function of a RAID controller is automatic error         */
  1311.    /* detection and recovery.  As such, the only problem that physically   */
  1312.    /* resetting a ServeRAID adapter will ever fix is when, for some reason,*/
  1313.    /* the driver is not successfully communicating with the adapter.       */
  1314.    /* Therefore, we will attempt to flush this adapter.  If that succeeds, */
  1315.    /* then there's no real purpose in a physical reset. This will complete */
  1316.    /* much faster and avoids any problems that might be caused by a        */
  1317.    /* physical reset ( such as having to fail all the outstanding I/O's ). */
  1318.                                                            
  1319.    if (ha->ioctl_reset == 0) {          /* IF Not an IOCTL Requested Reset */
  1320.       scb = &ha->scbs[ha->max_cmds-1];
  1321.       ips_init_scb(ha, scb);
  1322.       scb->timeout = ips_cmd_timeout;
  1323.       scb->cdb[0] = IPS_CMD_FLUSH;
  1324.       scb->cmd.flush_cache.op_code = IPS_CMD_FLUSH;
  1325.       scb->cmd.flush_cache.command_id = IPS_COMMAND_ID(ha, scb);
  1326.       scb->cmd.flush_cache.state = IPS_NORM_STATE;
  1327.       scb->cmd.flush_cache.reserved = 0;
  1328.       scb->cmd.flush_cache.reserved2 = 0;
  1329.       scb->cmd.flush_cache.reserved3 = 0;
  1330.       scb->cmd.flush_cache.reserved4 = 0;
  1331.       /* Attempt the flush command */
  1332.       ret = ips_send_wait(ha, scb, ips_cmd_timeout, IPS_INTR_IORL);
  1333.       if (ret == IPS_SUCCESS) {
  1334.          printk(KERN_NOTICE "(%s%d) Reset Request - Flushed Cachen", ips_name, ha->host_num);
  1335.          clear_bit(IPS_IN_RESET, &ha->flags);
  1336.          return (SUCCESS);
  1337.          }
  1338.    }
  1339.    /* Either we can't communicate with the adapter or it's an IOCTL request */
  1340.    /* from a ServeRAID utility.  A physical reset is needed at this point.  */
  1341.    ha->ioctl_reset = 0;             /* Reset the IOCTL Requested Reset Flag */
  1342.    /*
  1343.     * command must have already been sent
  1344.     * reset the controller
  1345.     */
  1346.    printk(KERN_NOTICE "(%s%d) Resetting controller.n",
  1347.           ips_name, ha->host_num);
  1348.    ret = (*ha->func.reset)(ha);
  1349.    if (!ret) {
  1350.       Scsi_Cmnd *scsi_cmd;
  1351.       printk(KERN_NOTICE
  1352.              "(%s%d) Controller reset failed - controller now offline.n",
  1353.              ips_name, ha->host_num);
  1354.       /* Now fail all of the active commands */
  1355.       DEBUG_VAR(1, "(%s%d) Failing active commands",
  1356.                 ips_name, ha->host_num);
  1357.       while ((scb = ips_removeq_scb_head(&ha->scb_activelist))) {
  1358.          scb->scsi_cmd->result = DID_ERROR << 16;
  1359.          scb->scsi_cmd->scsi_done(scb->scsi_cmd);
  1360.          ips_freescb(ha, scb);
  1361.       }
  1362.       /* Now fail all of the pending commands */
  1363.       DEBUG_VAR(1, "(%s%d) Failing pending commands",
  1364.                 ips_name, ha->host_num);
  1365.       while ((scsi_cmd = ips_removeq_wait_head(&ha->scb_waitlist))) {
  1366.          scsi_cmd->result = DID_ERROR;
  1367.          scsi_cmd->scsi_done(scsi_cmd);
  1368.       }
  1369.       ha->active = FALSE;
  1370.       clear_bit(IPS_IN_RESET, &ha->flags);
  1371.       return (FAILED);
  1372.    }
  1373.    if (!ips_clear_adapter(ha, IPS_INTR_IORL)) {
  1374.       Scsi_Cmnd *scsi_cmd;
  1375.       printk(KERN_NOTICE
  1376.              "(%s%d) Controller reset failed - controller now offline.n",
  1377.              ips_name, ha->host_num);
  1378.       /* Now fail all of the active commands */
  1379.       DEBUG_VAR(1, "(%s%d) Failing active commands",
  1380.                 ips_name, ha->host_num);
  1381.       while ((scb = ips_removeq_scb_head(&ha->scb_activelist))) {
  1382.          scb->scsi_cmd->result = DID_ERROR << 16;
  1383.          scb->scsi_cmd->scsi_done(scb->scsi_cmd);
  1384.          ips_freescb(ha, scb);
  1385.       }
  1386.       /* Now fail all of the pending commands */
  1387.       DEBUG_VAR(1, "(%s%d) Failing pending commands",
  1388.                 ips_name, ha->host_num);
  1389.       while ((scsi_cmd = ips_removeq_wait_head(&ha->scb_waitlist))) {
  1390.          scsi_cmd->result = DID_ERROR << 16;
  1391.          scsi_cmd->scsi_done(scsi_cmd);
  1392.       }
  1393.       ha->active = FALSE;
  1394.       clear_bit(IPS_IN_RESET, &ha->flags);
  1395.       return (FAILED);
  1396.    }
  1397.    /* FFDC */
  1398.    if (le32_to_cpu(ha->subsys->param[3]) & 0x300000) {
  1399.       struct timeval tv;
  1400.       do_gettimeofday(&tv);
  1401.       IPS_HA_LOCK(cpu_flags);
  1402.       ha->last_ffdc = tv.tv_sec;
  1403.       ha->reset_count++;
  1404.       IPS_HA_UNLOCK(cpu_flags);
  1405.       ips_ffdc_reset(ha, IPS_INTR_IORL);
  1406.    }
  1407.    /* Now fail all of the active commands */
  1408.    DEBUG_VAR(1, "(%s%d) Failing active commands",
  1409.              ips_name, ha->host_num);
  1410.    while ((scb = ips_removeq_scb_head(&ha->scb_activelist))) {
  1411.       scb->scsi_cmd->result = (DID_RESET << 16) | (SUGGEST_RETRY << 24);
  1412.       scb->scsi_cmd->scsi_done(scb->scsi_cmd);
  1413.       ips_freescb(ha, scb);
  1414.    }
  1415.    /* Reset DCDB active command bits */
  1416.    for (i = 1; i < ha->nbus; i++)
  1417.       ha->dcdb_active[i-1] = 0;
  1418.    /* Reset the number of active IOCTLs */
  1419.    IPS_HA_LOCK(cpu_flags);
  1420.    ha->num_ioctl = 0;
  1421.    IPS_HA_UNLOCK(cpu_flags);
  1422.    clear_bit(IPS_IN_RESET, &ha->flags);
  1423.    if (!test_bit(IPS_IN_INTR, &ha->flags)) {
  1424.       /*
  1425.        * Only execute the next command when
  1426.        * we are not being called from the
  1427.        * interrupt handler.  The interrupt
  1428.        * handler wants to do this and since
  1429.        * interrupts are turned off here....
  1430.        */
  1431.       ips_next(ha, IPS_INTR_IORL);
  1432.    }
  1433.    return (SUCCESS);
  1434. #endif /* NO_IPS_RESET */
  1435. }
  1436. /****************************************************************************/
  1437. /*                                                                          */
  1438. /* Routine Name: ips_queue                                                  */
  1439. /*                                                                          */
  1440. /* Routine Description:                                                     */
  1441. /*                                                                          */
  1442. /*   Send a command to the controller                                       */
  1443. /*                                                                          */
  1444. /* NOTE:                                                                    */
  1445. /*    Linux obtains io_request_lock before calling this function            */
  1446. /*                                                                          */
  1447. /****************************************************************************/
  1448. int
  1449. ips_queue(Scsi_Cmnd *SC, void (*done) (Scsi_Cmnd *)) {
  1450.    ips_ha_t         *ha;
  1451.    unsigned long     cpu_flags;
  1452.    ips_passthru_t   *pt;                                             
  1453.    METHOD_TRACE("ips_queue", 1);
  1454.    ha = (ips_ha_t *) SC->host->hostdata;
  1455.    if (!ha)
  1456.       return (1);
  1457.    if (!ha->active)
  1458.       return (DID_ERROR);
  1459.    if (ips_is_passthru(SC)) {
  1460.       IPS_QUEUE_LOCK(&ha->copp_waitlist);
  1461.       if (ha->copp_waitlist.count == IPS_MAX_IOCTL_QUEUE) {
  1462.          IPS_QUEUE_UNLOCK(&ha->copp_waitlist);
  1463.          SC->result = DID_BUS_BUSY << 16;
  1464.          done(SC);
  1465.          return (0);
  1466.       } else {
  1467.          IPS_QUEUE_UNLOCK(&ha->copp_waitlist);
  1468.       }
  1469.    } else {
  1470.       IPS_QUEUE_LOCK(&ha->scb_waitlist);
  1471.       if (ha->scb_waitlist.count == IPS_MAX_QUEUE) {
  1472.          IPS_QUEUE_UNLOCK(&ha->scb_waitlist);
  1473.          SC->result = DID_BUS_BUSY << 16;
  1474.          done(SC);
  1475.          return (0);
  1476.       } else {
  1477.          IPS_QUEUE_UNLOCK(&ha->scb_waitlist);
  1478.       }
  1479.    }
  1480.    SC->scsi_done = done;
  1481.    DEBUG_VAR(2, "(%s%d): ips_queue: cmd 0x%X (%d %d %d)",
  1482.              ips_name,
  1483.              ha->host_num,
  1484.              SC->cmnd[0],
  1485.              SC->channel,
  1486.              SC->target,
  1487.              SC->lun);
  1488.    /* Check for command to initiator IDs */
  1489.    if ((SC->channel > 0) && (SC->target == ha->ha_id[SC->channel])) {
  1490.       SC->result = DID_NO_CONNECT << 16;
  1491.       done(SC);
  1492.       return (0);
  1493.    }
  1494.    if (ips_is_passthru(SC)) {
  1495.       ips_copp_wait_item_t *scratch;
  1496.       
  1497.       /* A Reset IOCTL is only sent by the ServeRAID boot CD in extreme cases. */
  1498.       /* There can never be any system activity ( network or disk ), but check */
  1499.       /* anyway just as a good practice.                                       */
  1500.       pt = (ips_passthru_t *) SC->request_buffer;                                
  1501.       if ((pt->CoppCP.cmd.reset.op_code == IPS_CMD_RESET_CHANNEL) &&            
  1502.            (pt->CoppCP.cmd.reset.adapter_flag == 1))  {                         
  1503.          if (ha->scb_activelist.count != 0) {
  1504.              SC->result = DID_BUS_BUSY << 16;
  1505.              done(SC);
  1506.              return (0);
  1507.          }
  1508.          ha->ioctl_reset = 1;           /* This reset request is from an IOCTL */
  1509.          ips_eh_reset(SC);                                                         
  1510.          SC->result = DID_OK << 16;                                                
  1511.          SC->scsi_done(SC);                                                        
  1512.          return (0);                                                               
  1513.       }
  1514.       /* allocate space for the scribble */
  1515.       scratch = kmalloc(sizeof(ips_copp_wait_item_t), GFP_ATOMIC);
  1516.       if (!scratch) {
  1517.          SC->result = DID_ERROR << 16;
  1518.          done(SC);
  1519.          return (0);
  1520.       }
  1521.       scratch->scsi_cmd = SC;
  1522.       sema_init(&ha->ioctl_sem, 0);
  1523.       scratch->sem = &ha->ioctl_sem;
  1524.       scratch->next = NULL;
  1525.       ips_putq_copp_tail(&ha->copp_waitlist, scratch);
  1526.    }
  1527.    else
  1528.       ips_putq_wait_tail(&ha->scb_waitlist, SC);
  1529.    if(ha->scb_waitlist.count + ha->scb_activelist.count > 32)
  1530.          mod_timer(&SC->eh_timeout, jiffies + 120 * HZ);
  1531.    IPS_HA_LOCK(cpu_flags);
  1532.    if ((!test_bit(IPS_IN_INTR, &ha->flags)) &&
  1533.        (!test_bit(IPS_IN_ABORT, &ha->flags)) &&
  1534.        (!test_bit(IPS_IN_RESET, &ha->flags))) {
  1535.       IPS_HA_UNLOCK(cpu_flags);
  1536.       ips_next(ha, IPS_INTR_IORL);
  1537.    } else {
  1538.       IPS_HA_UNLOCK(cpu_flags);
  1539.    }
  1540.    
  1541.    /* If We were using the CD Boot Flash Buffer, Restore the Old Values */
  1542.    if ( ips_FlashData == ha->ioctl_data ) {                               
  1543.       ha->ioctl_data = ha->flash_data;                           
  1544.       ha->ioctl_order = ha->flash_order;                          
  1545.       ha->ioctl_datasize = ha->flash_datasize;                       
  1546.       ips_FlashDataInUse = 0;                                             
  1547.    }
  1548.    return (0);
  1549. }
  1550. /****************************************************************************/
  1551. /*                                                                          */
  1552. /* Routine Name: ips_biosparam                                              */
  1553. /*                                                                          */
  1554. /* Routine Description:                                                     */
  1555. /*                                                                          */
  1556. /*   Set bios geometry for the controller                                   */
  1557. /*                                                                          */
  1558. /****************************************************************************/
  1559. int
  1560. ips_biosparam(Disk *disk, kdev_t dev, int geom[]) {
  1561.    ips_ha_t         *ha;
  1562.    int               heads;
  1563.    int               sectors;
  1564.    int               cylinders;
  1565.    METHOD_TRACE("ips_biosparam", 1);
  1566.    ha = (ips_ha_t *) disk->device->host->hostdata;
  1567.    if (!ha)
  1568.       /* ?!?! host adater info invalid */
  1569.       return (0);
  1570.    if (!ha->active)
  1571.       return (0);
  1572.    if (!ips_read_adapter_status(ha, IPS_INTR_ON))
  1573.       /* ?!?! Enquiry command failed */
  1574.       return (0);
  1575.    if ((disk->capacity > 0x400000) &&
  1576.        ((ha->enq->ucMiscFlag & 0x8) == 0)) {
  1577.       heads = IPS_NORM_HEADS;
  1578.       sectors = IPS_NORM_SECTORS;
  1579.    } else {
  1580.       heads = IPS_COMP_HEADS;
  1581.       sectors = IPS_COMP_SECTORS;
  1582.    }
  1583.    cylinders = disk->capacity / (heads * sectors);
  1584.    DEBUG_VAR(2, "Geometry: heads: %d, sectors: %d, cylinders: %d",
  1585.              heads, sectors, cylinders);
  1586.    geom[0] = heads;
  1587.    geom[1] = sectors;
  1588.    geom[2] = cylinders;
  1589.    return (0);
  1590. }
  1591. /****************************************************************************/
  1592. /*                                                                          */
  1593. /* Routine Name: ips_select_queue_depth                                     */
  1594. /*                                                                          */
  1595. /* Routine Description:                                                     */
  1596. /*                                                                          */
  1597. /*   Select queue depths for the devices on the contoller                   */
  1598. /*                                                                          */
  1599. /****************************************************************************/
  1600. static void
  1601. ips_select_queue_depth(struct Scsi_Host *host, Scsi_Device *scsi_devs) {
  1602.    Scsi_Device *device;
  1603.    ips_ha_t    *ha;
  1604.    int          count = 0;
  1605.    int          min;
  1606.    ha = IPS_HA(host);
  1607.    min = ha->max_cmds / 4;
  1608.    for (device = scsi_devs; device; device = device->next) {
  1609.      if (device->host == host) {
  1610.         if ((device->channel == 0) && (device->type == 0))
  1611.            count++;
  1612.      }
  1613.    }
  1614.    for (device = scsi_devs; device; device = device->next) {
  1615.       if (device->host == host) {
  1616.          if ((device->channel == 0) && (device->type == 0)) {
  1617.             device->queue_depth = ( ha->max_cmds - 1 ) / count;
  1618.             if (device->queue_depth < min) 
  1619.                device->queue_depth = min;
  1620.          }
  1621.          else {
  1622.             device->queue_depth = 2;
  1623.          }
  1624.          if (device->queue_depth < 2)
  1625.             device->queue_depth = 2;
  1626.       }
  1627.    }
  1628. }
  1629. /****************************************************************************/
  1630. /*                                                                          */
  1631. /* Routine Name: do_ipsintr                                                 */
  1632. /*                                                                          */
  1633. /* Routine Description:                                                     */
  1634. /*                                                                          */
  1635. /*   Wrapper for the interrupt handler                                      */
  1636. /*                                                                          */
  1637. /****************************************************************************/
  1638. void
  1639. do_ipsintr(int irq, void *dev_id, struct pt_regs *regs) {
  1640.    ips_ha_t         *ha;
  1641.    unsigned long     cpu_flags;
  1642.    METHOD_TRACE("do_ipsintr", 2);
  1643.    ha = (ips_ha_t *) dev_id;
  1644.    if (!ha) 
  1645.       return;
  1646.    
  1647.    spin_lock_irqsave(&io_request_lock, cpu_flags);
  1648.    if (test_and_set_bit(IPS_IN_INTR, &ha->flags)) {
  1649.       spin_unlock_irqrestore(&io_request_lock, cpu_flags);
  1650.       return ;
  1651.    }
  1652.    if (!ha->active) {
  1653.       clear_bit(IPS_IN_INTR, &ha->flags);
  1654.       spin_unlock_irqrestore(&io_request_lock, cpu_flags);
  1655.       return;
  1656.    }
  1657.    (*ha->func.intr)(ha);
  1658.    clear_bit(IPS_IN_INTR, &ha->flags);
  1659.    spin_unlock_irqrestore(&io_request_lock, cpu_flags);
  1660.    /* start the next command */
  1661.    ips_next(ha, IPS_INTR_ON);
  1662. }
  1663. /****************************************************************************/
  1664. /*                                                                          */
  1665. /* Routine Name: ips_intr_copperhead                                        */
  1666. /*                                                                          */
  1667. /* Routine Description:                                                     */
  1668. /*                                                                          */
  1669. /*   Polling interrupt handler                                              */
  1670. /*                                                                          */
  1671. /*   ASSUMES interrupts are disabled                                        */
  1672. /*                                                                          */
  1673. /****************************************************************************/
  1674. void
  1675. ips_intr_copperhead(ips_ha_t *ha) {
  1676.    ips_stat_t       *sp;
  1677.    ips_scb_t        *scb;
  1678.    IPS_STATUS        cstatus;
  1679.    int               intrstatus;
  1680.    unsigned long     cpu_flags;
  1681.    METHOD_TRACE("ips_intr", 2);
  1682.    if (!ha)
  1683.       return;
  1684.    if (!ha->active)
  1685.       return;
  1686.    IPS_HA_LOCK(cpu_flags);
  1687.    intrstatus = (*ha->func.isintr)(ha);
  1688.    if (!intrstatus) {
  1689.       /*
  1690.        * Unexpected/Shared interrupt
  1691.        */
  1692.       IPS_HA_UNLOCK(cpu_flags);
  1693.       return;
  1694.    }
  1695.    while (TRUE) {
  1696.       sp = &ha->sp;
  1697.       intrstatus = (*ha->func.isintr)(ha);
  1698.       if (!intrstatus)
  1699.          break;
  1700.       else
  1701.          cstatus.value = (*ha->func.statupd)(ha);
  1702.       if (cstatus.fields.command_id > (IPS_MAX_CMDS - 1)) {
  1703.          /* Spurious Interupt ? */
  1704.          continue;
  1705.       }
  1706.       ips_chkstatus(ha, &cstatus);
  1707.       scb = (ips_scb_t *) sp->scb_addr;
  1708.       /*
  1709.        * use the callback function to finish things up
  1710.        * NOTE: interrupts are OFF for this
  1711.        */
  1712.       IPS_HA_UNLOCK(cpu_flags);
  1713.       (*scb->callback) (ha, scb);
  1714.       IPS_HA_LOCK(cpu_flags);
  1715.    } /* end while */
  1716.    IPS_HA_UNLOCK(cpu_flags);
  1717. }
  1718. /****************************************************************************/
  1719. /*                                                                          */
  1720. /* Routine Name: ips_intr_morpheus                                          */
  1721. /*                                                                          */
  1722. /* Routine Description:                                                     */
  1723. /*                                                                          */
  1724. /*   Polling interrupt handler                                              */
  1725. /*                                                                          */
  1726. /*   ASSUMES interrupts are disabled                                        */
  1727. /*                                                                          */
  1728. /****************************************************************************/
  1729. void
  1730. ips_intr_morpheus(ips_ha_t *ha) {
  1731.    ips_stat_t       *sp;
  1732.    ips_scb_t        *scb;
  1733.    IPS_STATUS        cstatus;
  1734.    int               intrstatus;
  1735.    unsigned long     cpu_flags;
  1736.    METHOD_TRACE("ips_intr_morpheus", 2);
  1737.    if (!ha)
  1738.       return;
  1739.    if (!ha->active)
  1740.       return;
  1741.    IPS_HA_LOCK(cpu_flags);
  1742.    intrstatus = (*ha->func.isintr)(ha);
  1743.    if (!intrstatus) {
  1744.       /*
  1745.        * Unexpected/Shared interrupt
  1746.        */
  1747.       IPS_HA_UNLOCK(cpu_flags);
  1748.       return;
  1749.    }
  1750.    while (TRUE) {
  1751.       sp = &ha->sp;
  1752.       intrstatus = (*ha->func.isintr)(ha);
  1753.       if (!intrstatus)
  1754.          break;
  1755.       else
  1756.          cstatus.value = (*ha->func.statupd)(ha);
  1757.       if (cstatus.value == 0xffffffff)
  1758.          /* No more to process */
  1759.          break;
  1760.       if (cstatus.fields.command_id > (IPS_MAX_CMDS - 1)) {
  1761.          printk(KERN_WARNING "(%s%d) Spurious interrupt; no ccb.n",
  1762.                 ips_name, ha->host_num);
  1763.          continue;
  1764.       }
  1765.       ips_chkstatus(ha, &cstatus);
  1766.       scb = (ips_scb_t *) sp->scb_addr;
  1767.       /*
  1768.        * use the callback function to finish things up
  1769.        * NOTE: interrupts are OFF for this
  1770.        */
  1771.       IPS_HA_UNLOCK(cpu_flags);
  1772.       (*scb->callback) (ha, scb);
  1773.       IPS_HA_LOCK(cpu_flags);
  1774.    } /* end while */
  1775.    IPS_HA_UNLOCK(cpu_flags);
  1776. }
  1777. /****************************************************************************/
  1778. /*                                                                          */
  1779. /* Routine Name: ips_info                                                   */
  1780. /*                                                                          */
  1781. /* Routine Description:                                                     */
  1782. /*                                                                          */
  1783. /*   Return info about the driver                                           */
  1784. /*                                                                          */
  1785. /****************************************************************************/
  1786. const char *
  1787. ips_info(struct Scsi_Host *SH) {
  1788.    static char  buffer[256];
  1789.    char        *bp;
  1790.    ips_ha_t    *ha;
  1791.    METHOD_TRACE("ips_info", 1);
  1792.    ha = IPS_HA(SH);
  1793.    if (!ha)
  1794.       return (NULL);
  1795.    bp = &buffer[0];
  1796.    memset(bp, 0, sizeof(buffer));
  1797.    sprintf(bp, "%s%s%s", "IBM PCI ServeRAID ", IPS_VERSION_HIGH, IPS_VERSION_LOW );
  1798.    if (ha->ad_type > 0 &&
  1799.        ha->ad_type <= MAX_ADAPTER_NAME) {
  1800.       strcat(bp, " <");
  1801.       strcat(bp, ips_adapter_name[ha->ad_type-1]);
  1802.       strcat(bp, ">");
  1803.    }
  1804.    return (bp);
  1805. }
  1806. /****************************************************************************/
  1807. /*                                                                          */
  1808. /* Routine Name: ips_proc_info                                              */
  1809. /*                                                                          */
  1810. /* Routine Description:                                                     */
  1811. /*                                                                          */
  1812. /*   The passthru interface for the driver                                  */
  1813. /*                                                                          */
  1814. /****************************************************************************/
  1815. int
  1816. ips_proc_info(char *buffer, char **start, off_t offset,
  1817.               int length, int hostno, int func) {
  1818.    int           i;
  1819.    int           ret;
  1820.    ips_ha_t     *ha = NULL;
  1821.    METHOD_TRACE("ips_proc_info", 1);
  1822.    /* Find our host structure */
  1823.    for (i = 0; i < ips_next_controller; i++) {
  1824.       if (ips_sh[i]) {
  1825.          if (ips_sh[i]->host_no == hostno) {
  1826.             ha = (ips_ha_t *) ips_sh[i]->hostdata;
  1827.             break;
  1828.          }
  1829.       }
  1830.    }
  1831.    if (!ha)
  1832.       return (-EINVAL);
  1833.    if (func) {
  1834.       /* write */
  1835.       return (0);
  1836.    } else {
  1837.       /* read */
  1838.       if (start)
  1839.          *start = buffer;
  1840.       ret = ips_host_info(ha, buffer, offset, length);
  1841.       return (ret);
  1842.    }
  1843. }
  1844. /*--------------------------------------------------------------------------*/
  1845. /* Helper Functions                                                         */
  1846. /*--------------------------------------------------------------------------*/
  1847. /****************************************************************************/
  1848. /*                                                                          */
  1849. /* Routine Name: ips_is_passthru                                            */
  1850. /*                                                                          */
  1851. /* Routine Description:                                                     */
  1852. /*                                                                          */
  1853. /*   Determine if the specified SCSI command is really a passthru command   */
  1854. /*                                                                          */
  1855. /****************************************************************************/
  1856. static int
  1857. ips_is_passthru(Scsi_Cmnd *SC) {
  1858.    METHOD_TRACE("ips_is_passthru", 1);
  1859.    if (!SC)
  1860.       return (0);
  1861.    if ((SC->cmnd[0] == IPS_IOCTL_COMMAND) &&
  1862.        (SC->channel == 0) &&
  1863.        (SC->target == IPS_ADAPTER_ID) &&
  1864.        (SC->lun == 0) &&
  1865.         SC->request_buffer){
  1866.       if((!SC->use_sg) && SC->request_bufflen &&
  1867.          (((char *) SC->request_buffer)[0] == 'C') &&
  1868.          (((char *) SC->request_buffer)[1] == 'O') &&
  1869.          (((char *) SC->request_buffer)[2] == 'P') &&
  1870.          (((char *) SC->request_buffer)[3] == 'P'))
  1871.          return 1;
  1872.       else if(SC->use_sg){
  1873.          struct scatterlist *sg = SC->request_buffer;
  1874.          char *buffer = sg[0].address;
  1875.          if(buffer && buffer[0] == 'C' && buffer[1] == 'O' && 
  1876.             buffer[2] == 'P' && buffer[3] == 'P')
  1877.             return 1;
  1878.       }
  1879.    }
  1880.    return 0;
  1881. }
  1882. /****************************************************************************/
  1883. /*                                                                          */
  1884. /* Routine Name: ips_make_passthru                                          */
  1885. /*                                                                          */
  1886. /* Routine Description:                                                     */
  1887. /*                                                                          */
  1888. /*   Make a passthru command out of the info in the Scsi block              */
  1889. /*                                                                          */
  1890. /****************************************************************************/
  1891. static int
  1892. ips_make_passthru(ips_ha_t *ha, Scsi_Cmnd *SC, ips_scb_t *scb, int intr) {
  1893.    ips_passthru_t *pt;
  1894.    char *buffer;
  1895.    int length = 0;
  1896.    METHOD_TRACE("ips_make_passthru", 1);
  1897.    if(!SC->use_sg){
  1898.       buffer = SC->request_buffer;
  1899.       length = SC->request_bufflen;
  1900.    }else{
  1901.       struct scatterlist *sg = SC->request_buffer;
  1902.       int i;
  1903.       for(i = 0; i < SC->use_sg; i++)
  1904.          length += sg[i].length;
  1905.       if (length < sizeof(ips_passthru_t)) {
  1906.          /* wrong size */
  1907.          DEBUG_VAR(1, "(%s%d) Passthru structure wrong size",
  1908.              ips_name, ha->host_num);
  1909.          return (IPS_FAILURE);
  1910.       }else if(!ha->ioctl_data || length > (PAGE_SIZE << ha->ioctl_order)){
  1911.          void *bigger_buf;
  1912.          int count;
  1913.          int order;
  1914.          /* try to allocate a bigger buffer */
  1915.          for (count = PAGE_SIZE, order = 0;
  1916.               count < length;
  1917.               order++, count <<= 1);
  1918.          bigger_buf = (void *) __get_free_pages(GFP_ATOMIC, order);
  1919.          if (bigger_buf) {
  1920.             /* free the old memory */
  1921.             free_pages((unsigned long) ha->ioctl_data, ha->ioctl_order);
  1922.             /* use the new memory */
  1923.             ha->ioctl_data = (char *) bigger_buf;
  1924.             ha->ioctl_order = order;
  1925.             ha->ioctl_datasize = count;
  1926.          } else {
  1927.              pt = (ips_passthru_t*)sg[0].address;
  1928.              pt->BasicStatus = 0x0B;
  1929.              pt->ExtendedStatus = 0x00;
  1930.              SC->result = DID_ERROR << 16;
  1931.              return (IPS_FAILURE);
  1932.          }
  1933.       }
  1934.       ha->ioctl_datasize = length;
  1935.       length = 0;
  1936.       for(i = 0; i < SC->use_sg; i++){
  1937.          memcpy(&ha->ioctl_data[length], sg[i].address, sg[i].length);
  1938.          length += sg[i].length;
  1939.       }
  1940.       pt = (ips_passthru_t *)ha->ioctl_data;
  1941.       buffer = ha->ioctl_data;
  1942.    }
  1943.    if (!length || !buffer) {
  1944.       /* no data */
  1945.       DEBUG_VAR(1, "(%s%d) No passthru structure",
  1946.                 ips_name, ha->host_num);
  1947.       return (IPS_FAILURE);
  1948.    }
  1949.    if (length < sizeof(ips_passthru_t)) {
  1950.       /* wrong size */
  1951.       DEBUG_VAR(1, "(%s%d) Passthru structure wrong size",
  1952.              ips_name, ha->host_num);
  1953.       return (IPS_FAILURE);
  1954.    }
  1955.    pt = (ips_passthru_t*) buffer;
  1956.    /*
  1957.     * Some notes about the passthru interface used
  1958.     *
  1959.     * IF the scsi op_code == 0x0d then we assume
  1960.     * that the data came along with/goes with the
  1961.     * packet we received from the sg driver. In this
  1962.     * case the CmdBSize field of the pt structure is
  1963.     * used for the size of the buffer.
  1964.     *
  1965.     * IF the scsi op_code == 0x81 then we assume that
  1966.     * we will need our own buffer and we will copy the
  1967.     * data to/from the user buffer passed in the scsi
  1968.     * command.  The data address resides at offset 4
  1969.     * in the scsi command.  The length of the data resides
  1970.     * at offset 8 in the scsi command.
  1971.     */
  1972.    switch (pt->CoppCmd) {
  1973.    case IPS_NUMCTRLS:
  1974.       memcpy(buffer + sizeof(ips_passthru_t),
  1975.              &ips_num_controllers, sizeof(int));
  1976.       SC->result = DID_OK << 16;
  1977.       return (IPS_SUCCESS_IMM);
  1978.    case IPS_CTRLINFO:
  1979.       memcpy(buffer + sizeof(ips_passthru_t),
  1980.              ha, sizeof(ips_ha_t));
  1981.       SC->result = DID_OK << 16;
  1982.       return (IPS_SUCCESS_IMM);
  1983.    case IPS_COPPUSRCMD:
  1984.    case IPS_COPPIOCCMD:
  1985.       if (SC->cmnd[0] == IPS_IOCTL_COMMAND) {
  1986.          if (length < (sizeof(ips_passthru_t) + pt->CmdBSize)) {
  1987.             /* wrong size */
  1988.             DEBUG_VAR(1, "(%s%d) Passthru structure wrong size",
  1989.                       ips_name, ha->host_num);
  1990.             return (IPS_FAILURE);
  1991.          }
  1992.          if(ha->device_id == IPS_DEVICEID_COPPERHEAD &&
  1993.             pt->CoppCP.cmd.flashfw.op_code == IPS_CMD_RW_BIOSFW)
  1994.             return ips_flash_copperhead(ha, pt, scb);
  1995.          if (ips_usrcmd(ha, pt, scb))
  1996.             return (IPS_SUCCESS);
  1997.          else
  1998.             return (IPS_FAILURE);
  1999.       } 
  2000.      
  2001.       break;
  2002.    } /* end switch */
  2003.          return (IPS_FAILURE);
  2004.       }
  2005. /****************************************************************************/
  2006. /* Routine Name: ips_flash_copperhead                                       */
  2007. /* Routine Description:                                                     */
  2008. /*   Flash the BIOS/FW on a Copperhead style controller                     */
  2009. /****************************************************************************/
  2010. static int
  2011. ips_flash_copperhead(ips_ha_t *ha, ips_passthru_t *pt, ips_scb_t *scb){
  2012.    int datasize, count;
  2013.    /* Trombone is the only copperhead that can do packet flash, but only
  2014.     * for firmware. No one said it had to make sence. */
  2015.    if(IPS_IS_TROMBONE(ha) && pt->CoppCP.cmd.flashfw.type == IPS_FW_IMAGE){
  2016.       if(ips_usrcmd(ha, pt, scb))
  2017.          return IPS_SUCCESS;
  2018.       else
  2019.          return IPS_FAILURE;
  2020.    }
  2021.    pt->BasicStatus = 0x0B;
  2022.    pt->ExtendedStatus = 0;
  2023.    scb->scsi_cmd->result = DID_OK <<16;
  2024.    /* IF it's OK to Use the "CD BOOT" Flash Buffer, then you can     */
  2025.    /* avoid allocating a huge buffer per adapter ( which can fail ). */
  2026.    if(pt->CoppCP.cmd.flashfw.type == IPS_BIOS_IMAGE &&
  2027.       pt->CoppCP.cmd.flashfw.direction == IPS_ERASE_BIOS){
  2028.       pt->BasicStatus = 0;
  2029.       return ips_flash_bios(ha, pt, scb);
  2030.    }else if(pt->CoppCP.cmd.flashfw.packet_num == 0){
  2031.       if(ips_FlashData && !test_and_set_bit(0, &ips_FlashDataInUse)){
  2032.          ha->flash_data  = ips_FlashData;
  2033.          ha->flash_order = 7;
  2034.          ha->flash_datasize = 0;
  2035.       }else if(!ha->flash_data){
  2036.          datasize = pt->CoppCP.cmd.flashfw.total_packets *
  2037.                     pt->CoppCP.cmd.flashfw.count;
  2038.          for (count = PAGE_SIZE, ha->flash_order = 0; count < datasize;
  2039.               ha->flash_order++, count <<= 1);
  2040.          ha->flash_data = (char *)__get_free_pages(GFP_ATOMIC, ha->flash_order);
  2041.          ha->flash_datasize = 0;
  2042.       }else
  2043.          return IPS_FAILURE;
  2044.    }else{
  2045.       if(pt->CoppCP.cmd.flashfw.count + ha->flash_datasize >
  2046.         (PAGE_SIZE << ha->flash_order)){
  2047.          ips_free_flash_copperhead(ha);
  2048.          printk(KERN_WARNING "failed size sanity checkn");
  2049.          return IPS_FAILURE;
  2050.       }
  2051.    }
  2052.    if(!ha->flash_data)
  2053.       return IPS_FAILURE;
  2054.    pt->BasicStatus = 0;
  2055.    memcpy(&ha->flash_data[ha->flash_datasize], pt + 1,
  2056.           pt->CoppCP.cmd.flashfw.count);
  2057.    ha->flash_datasize += pt->CoppCP.cmd.flashfw.count;
  2058.    if(pt->CoppCP.cmd.flashfw.packet_num ==
  2059.       pt->CoppCP.cmd.flashfw.total_packets - 1){
  2060.       if(pt->CoppCP.cmd.flashfw.type == IPS_BIOS_IMAGE)
  2061.          return ips_flash_bios(ha, pt, scb);
  2062.       else if(pt->CoppCP.cmd.flashfw.type == IPS_FW_IMAGE)
  2063.          return ips_flash_firmware(ha, pt, scb);
  2064.    }
  2065.    return IPS_SUCCESS_IMM;
  2066. }
  2067. /****************************************************************************/
  2068. /* Routine Name: ips_flash_bios                                             */
  2069. /* Routine Description:                                                     */
  2070. /*   flashes the bios of a copperhead adapter                               */
  2071. /****************************************************************************/
  2072. static int
  2073. ips_flash_bios(ips_ha_t * ha, ips_passthru_t *pt, ips_scb_t *scb){
  2074.    if(pt->CoppCP.cmd.flashfw.type == IPS_BIOS_IMAGE &&
  2075.       pt->CoppCP.cmd.flashfw.direction == IPS_WRITE_BIOS){
  2076.       if ((!ha->func.programbios) || (!ha->func.erasebios) ||
  2077.           (!ha->func.verifybios))
  2078.          goto error;
  2079.       if((*ha->func.erasebios)(ha)){
  2080.          DEBUG_VAR(1, "(%s%d) flash bios failed - unable to erase flash",
  2081.                    ips_name, ha->host_num);
  2082.          goto error;
  2083.       }else if ((*ha->func.programbios)(ha, ha->flash_data + IPS_BIOS_HEADER,
  2084.           ha->flash_datasize - IPS_BIOS_HEADER, 0 )) {
  2085.          DEBUG_VAR(1, "(%s%d) flash bios failed - unable to flash",
  2086.                    ips_name, ha->host_num);
  2087.          goto error;
  2088.       }else if ((*ha->func.verifybios)(ha, ha->flash_data + IPS_BIOS_HEADER,
  2089.           ha->flash_datasize - IPS_BIOS_HEADER, 0 )) {
  2090.          DEBUG_VAR(1, "(%s%d) flash bios failed - unable to verify flash",
  2091.                    ips_name, ha->host_num);
  2092.          goto error;
  2093.       }
  2094.       ips_free_flash_copperhead(ha);
  2095.       return IPS_SUCCESS_IMM;
  2096.    }else if(pt->CoppCP.cmd.flashfw.type == IPS_BIOS_IMAGE &&
  2097.       pt->CoppCP.cmd.flashfw.direction == IPS_ERASE_BIOS){
  2098.       if(!ha->func.erasebios)
  2099.          goto error;
  2100.       if((*ha->func.erasebios)(ha)){
  2101.          DEBUG_VAR(1, "(%s%d) flash bios failed - unable to erase flash",
  2102.                    ips_name, ha->host_num);
  2103.          goto error;
  2104.       }
  2105.       return IPS_SUCCESS_IMM;
  2106.    }
  2107. error:
  2108.    pt->BasicStatus = 0x0B;
  2109.    pt->ExtendedStatus = 0x00;
  2110.    ips_free_flash_copperhead(ha);
  2111.    return IPS_FAILURE;
  2112. }
  2113. /****************************************************************************/
  2114. /* Routine Name: ips_flash_firmware                                         */
  2115. /* Routine Description:                                                     */
  2116. /*   flashes the firmware of a copperhead adapter                           */
  2117. /****************************************************************************/
  2118. static int
  2119. ips_flash_firmware(ips_ha_t * ha, ips_passthru_t *pt, ips_scb_t *scb){
  2120.    IPS_SG_LIST *sg_list;
  2121.    if(pt->CoppCP.cmd.flashfw.type == IPS_FW_IMAGE &&
  2122.       pt->CoppCP.cmd.flashfw.direction == IPS_WRITE_FW ){
  2123.       memset(&pt->CoppCP.cmd, 0, sizeof(IPS_HOST_COMMAND));
  2124.       pt->CoppCP.cmd.flashfw.op_code = IPS_CMD_DOWNLOAD;
  2125.       pt->CoppCP.cmd.flashfw.count = cpu_to_le32(ha->flash_datasize);
  2126.    }else{
  2127.       pt->BasicStatus = 0x0B;
  2128.       pt->ExtendedStatus = 0x00;
  2129.       ips_free_flash_copperhead(ha);
  2130.       return IPS_FAILURE;
  2131.    }
  2132.    /* Save the S/G list pointer so it doesn't get clobbered */
  2133.    sg_list = scb->sg_list;
  2134.    /* copy in the CP */
  2135.    memcpy(&scb->cmd, &pt->CoppCP.cmd, sizeof(IPS_IOCTL_CMD));
  2136.    /* FIX stuff that might be wrong */
  2137.    scb->sg_list = sg_list;
  2138.    scb->scb_busaddr = VIRT_TO_BUS(scb);
  2139.    scb->bus = scb->scsi_cmd->channel;
  2140.    scb->target_id = scb->scsi_cmd->target;
  2141.    scb->lun = scb->scsi_cmd->lun;
  2142.    scb->sg_len = 0;
  2143.    scb->data_len = 0;
  2144.    scb->flags = 0;
  2145.    scb->op_code = 0;
  2146.    scb->callback = ipsintr_done;
  2147.    scb->timeout = ips_cmd_timeout;
  2148.    scb->cmd.flashfw.command_id = IPS_COMMAND_ID(ha, scb);
  2149.    scb->cmd.flashfw.buffer_addr = VIRT_TO_BUS(ha->flash_data);
  2150.    if (pt->TimeOut)
  2151.       scb->timeout = pt->TimeOut;
  2152.    scb->scsi_cmd->result = DID_OK <<16;
  2153.    return IPS_SUCCESS;
  2154. }
  2155. /****************************************************************************/
  2156. /* Routine Name: ips_free_flash_copperhead                                  */
  2157. /* Routine Description:                                                     */
  2158. /*   release the memory resources used to hold the flash image              */
  2159. /****************************************************************************/
  2160. static void
  2161. ips_free_flash_copperhead(ips_ha_t *ha){
  2162.    if(ha->flash_data == ips_FlashData)
  2163.       test_and_clear_bit(0, &ips_FlashDataInUse);
  2164.    else if(ha->flash_data)
  2165.       free_pages((unsigned long)ha->flash_data, ha->flash_order);
  2166.    ha->flash_data = NULL;
  2167. }
  2168. /****************************************************************************/
  2169. /*                                                                          */
  2170. /* Routine Name: ips_usrcmd                                                 */
  2171. /*                                                                          */
  2172. /* Routine Description:                                                     */
  2173. /*                                                                          */
  2174. /*   Process a user command and make it ready to send                       */
  2175. /*                                                                          */
  2176. /****************************************************************************/
  2177. static int
  2178. ips_usrcmd(ips_ha_t *ha, ips_passthru_t *pt, ips_scb_t *scb) {
  2179.    IPS_SG_LIST *sg_list;
  2180.    METHOD_TRACE("ips_usrcmd", 1);
  2181.    if ((!scb) || (!pt) || (!ha))
  2182.       return (0);
  2183.    /* Save the S/G list pointer so it doesn't get clobbered */
  2184.    sg_list = scb->sg_list;
  2185.    /* copy in the CP */
  2186.    memcpy(&scb->cmd, &pt->CoppCP.cmd, sizeof(IPS_IOCTL_CMD));
  2187.    memcpy(&scb->dcdb, &pt->CoppCP.dcdb, sizeof(IPS_DCDB_TABLE));
  2188.    /* FIX stuff that might be wrong */
  2189.    scb->sg_list = sg_list;
  2190.    scb->scb_busaddr = VIRT_TO_BUS(scb);
  2191.    scb->bus = scb->scsi_cmd->channel;
  2192.    scb->target_id = scb->scsi_cmd->target;
  2193.    scb->lun = scb->scsi_cmd->lun;
  2194.    scb->sg_len = 0;
  2195.    scb->data_len = 0;
  2196.    scb->flags = 0;
  2197.    scb->op_code = 0;
  2198.    scb->callback = ipsintr_done;
  2199.    scb->timeout = ips_cmd_timeout;
  2200.    scb->cmd.basic_io.command_id = IPS_COMMAND_ID(ha, scb);
  2201.    /* we don't support DCDB/READ/WRITE Scatter Gather */
  2202.    if ((scb->cmd.basic_io.op_code == IPS_CMD_READ_SG) ||
  2203.        (scb->cmd.basic_io.op_code == IPS_CMD_WRITE_SG) ||
  2204.        (scb->cmd.basic_io.op_code == IPS_CMD_DCDB_SG))
  2205.       return (0);
  2206.    if (pt->CmdBSize) {
  2207.       if(!scb->scsi_cmd->use_sg)
  2208.          scb->data_busaddr = VIRT_TO_BUS(scb->scsi_cmd->request_buffer + sizeof(ips_passthru_t));
  2209.       else
  2210.          scb->data_busaddr = VIRT_TO_BUS(ha->ioctl_data + sizeof(ips_passthru_t));
  2211.    } else {
  2212.       scb->data_busaddr = 0L;
  2213.    }
  2214.    if (scb->cmd.dcdb.op_code == IPS_CMD_DCDB)