ips.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:261k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

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