]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/scsi/ips.c
Fix ipv4/igmp.c compile with gcc-4 and IP_MULTICAST
[linux-2.6.git] / drivers / scsi / ips.c
1 /*****************************************************************************/
2 /* ips.c -- driver for the Adaptec / IBM ServeRAID controller                */
3 /*                                                                           */
4 /* Written By: Keith Mitchell, IBM Corporation                               */
5 /*             Jack Hammer, Adaptec, Inc.                                    */
6 /*             David Jeffery, Adaptec, Inc.                                  */
7 /*                                                                           */
8 /* Copyright (C) 2000 IBM Corporation                                        */
9 /* Copyright (C) 2002,2003 Adaptec, Inc.                                     */
10 /*                                                                           */
11 /* This program is free software; you can redistribute it and/or modify      */
12 /* it under the terms of the GNU General Public License as published by      */
13 /* the Free Software Foundation; either version 2 of the License, or         */
14 /* (at your option) any later version.                                       */
15 /*                                                                           */
16 /* This program is distributed in the hope that it will be useful,           */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
19 /* GNU General Public License for more details.                              */
20 /*                                                                           */
21 /* NO WARRANTY                                                               */
22 /* THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR        */
23 /* CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT      */
24 /* LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,      */
25 /* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is    */
26 /* solely responsible for determining the appropriateness of using and       */
27 /* distributing the Program and assumes all risks associated with its        */
28 /* exercise of rights under this Agreement, including but not limited to     */
29 /* the risks and costs of program errors, damage to or loss of data,         */
30 /* programs or equipment, and unavailability or interruption of operations.  */
31 /*                                                                           */
32 /* DISCLAIMER OF LIABILITY                                                   */
33 /* NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY   */
34 /* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL        */
35 /* DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND   */
36 /* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR     */
37 /* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE    */
38 /* USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED  */
39 /* HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES             */
40 /*                                                                           */
41 /* You should have received a copy of the GNU General Public License         */
42 /* along with this program; if not, write to the Free Software               */
43 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
44 /*                                                                           */
45 /* Bugs/Comments/Suggestions about this driver should be mailed to:          */
46 /*      ipslinux@adaptec.com                                                 */
47 /*                                                                           */
48 /* For system support issues, contact your local IBM Customer support.       */
49 /* Directions to find IBM Customer Support for each country can be found at: */
50 /*      http://www.ibm.com/planetwide/                                       */
51 /*                                                                           */
52 /*****************************************************************************/
53
54 /*****************************************************************************/
55 /* Change Log                                                                */
56 /*                                                                           */
57 /* 0.99.02  - Breakup commands that are bigger than 8 * the stripe size      */
58 /* 0.99.03  - Make interrupt routine handle all completed request on the     */
59 /*            adapter not just the first one                                 */
60 /*          - Make sure passthru commands get woken up if we run out of      */
61 /*            SCBs                                                           */
62 /*          - Send all of the commands on the queue at once rather than      */
63 /*            one at a time since the card will support it.                  */
64 /* 0.99.04  - Fix race condition in the passthru mechanism -- this required  */
65 /*            the interface to the utilities to change                       */
66 /*          - Fix error recovery code                                        */
67 /* 0.99.05  - Fix an oops when we get certain passthru commands              */
68 /* 1.00.00  - Initial Public Release                                         */
69 /*            Functionally equivalent to 0.99.05                             */
70 /* 3.60.00  - Bump max commands to 128 for use with firmware 3.60            */
71 /*          - Change version to 3.60 to coincide with release numbering.     */
72 /* 3.60.01  - Remove bogus error check in passthru routine                   */
73 /* 3.60.02  - Make DCDB direction based on lookup table                      */
74 /*          - Only allow one DCDB command to a SCSI ID at a time             */
75 /* 4.00.00  - Add support for ServeRAID 4                                    */
76 /* 4.00.01  - Add support for First Failure Data Capture                     */
77 /* 4.00.02  - Fix problem with PT DCDB with no buffer                        */
78 /* 4.00.03  - Add alternative passthru interface                             */
79 /*          - Add ability to flash BIOS                                      */
80 /* 4.00.04  - Rename structures/constants to be prefixed with IPS_           */
81 /* 4.00.05  - Remove wish_block from init routine                            */
82 /*          - Use linux/spinlock.h instead of asm/spinlock.h for kernels     */
83 /*            2.3.18 and later                                               */
84 /*          - Sync with other changes from the 2.3 kernels                   */
85 /* 4.00.06  - Fix timeout with initial FFDC command                          */
86 /* 4.00.06a - Port to 2.4 (trivial) -- Christoph Hellwig <hch@infradead.org> */
87 /* 4.10.00  - Add support for ServeRAID 4M/4L                                */
88 /* 4.10.13  - Fix for dynamic unload and proc file system                    */
89 /* 4.20.03  - Rename version to coincide with new release schedules          */
90 /*            Performance fixes                                              */
91 /*            Fix truncation of /proc files with cat                         */
92 /*            Merge in changes through kernel 2.4.0test1ac21                 */
93 /* 4.20.13  - Fix some failure cases / reset code                            */
94 /*          - Hook into the reboot_notifier to flush the controller cache    */
95 /* 4.50.01  - Fix problem when there is a hole in logical drive numbering    */
96 /* 4.70.09  - Use a Common ( Large Buffer ) for Flashing from the JCRM CD    */
97 /*          - Add IPSSEND Flash Support                                      */
98 /*          - Set Sense Data for Unknown SCSI Command                        */
99 /*          - Use Slot Number from NVRAM Page 5                              */
100 /*          - Restore caller's DCDB Structure                                */
101 /* 4.70.12  - Corrective actions for bad controller ( during initialization )*/
102 /* 4.70.13  - Don't Send CDB's if we already know the device is not present  */
103 /*          - Don't release HA Lock in ips_next() until SC taken off queue   */
104 /*          - Unregister SCSI device in ips_release()                        */
105 /* 4.70.15  - Fix Breakup for very large ( non-SG ) requests in ips_done()   */
106 /* 4.71.00  - Change all memory allocations to not use GFP_DMA flag          */
107 /*            Code Clean-Up for 2.4.x kernel                                 */
108 /* 4.72.00  - Allow for a Scatter-Gather Element to exceed MAX_XFER Size     */
109 /* 4.72.01  - I/O Mapped Memory release ( so "insmod ips" does not Fail )    */
110 /*          - Don't Issue Internal FFDC Command if there are Active Commands */
111 /*          - Close Window for getting too many IOCTL's active               */
112 /* 4.80.00  - Make ia64 Safe                                                 */
113 /* 4.80.04  - Eliminate calls to strtok() if 2.4.x or greater                */
114 /*          - Adjustments to Device Queue Depth                              */
115 /* 4.80.14  - Take all semaphores off stack                                  */
116 /*          - Clean Up New_IOCTL path                                        */
117 /* 4.80.20  - Set max_sectors in Scsi_Host structure ( if >= 2.4.7 kernel )  */
118 /*          - 5 second delay needed after resetting an i960 adapter          */
119 /* 4.80.26  - Clean up potential code problems ( Arjan's recommendations )   */
120 /* 4.90.01  - Version Matching for FirmWare, BIOS, and Driver                */
121 /* 4.90.05  - Use New PCI Architecture to facilitate Hot Plug Development    */
122 /* 4.90.08  - Increase Delays in Flashing ( Trombone Only - 4H )             */
123 /* 4.90.08  - Data Corruption if First Scatter Gather Element is > 64K       */
124 /* 4.90.11  - Don't actually RESET unless it's physically required           */
125 /*          - Remove unused compile options                                  */
126 /* 5.00.01  - Sarasota ( 5i ) adapters must always be scanned first          */
127 /*          - Get rid on IOCTL_NEW_COMMAND code                              */
128 /*          - Add Extended DCDB Commands for Tape Support in 5I              */
129 /* 5.10.12  - use pci_dma interfaces, update for 2.5 kernel changes          */
130 /* 5.10.15  - remove unused code (sem, macros, etc.)                         */
131 /* 5.30.00  - use __devexit_p()                                              */
132 /* 6.00.00  - Add 6x Adapters and Battery Flash                              */
133 /* 6.10.00  - Remove 1G Addressing Limitations                               */
134 /* 6.11.xx  - Get VersionInfo buffer off the stack !              DDTS 60401 */
135 /* 6.11.xx  - Make Logical Drive Info structure safe for DMA      DDTS 60639 */
136 /* 7.10.18  - Add highmem_io flag in SCSI Templete for 2.4 kernels           */
137 /*          - Fix path/name for scsi_hosts.h include for 2.6 kernels         */
138 /*          - Fix sort order of 7k                                           */
139 /*          - Remove 3 unused "inline" functions                             */
140 /* 7.12.xx  - Use STATIC functions whereever possible                        */
141 /*          - Clean up deprecated MODULE_PARM calls                          */
142 /* 7.12.05  - Remove Version Matching per IBM request                        */
143 /*****************************************************************************/
144
145 /*
146  * Conditional Compilation directives for this driver:
147  *
148  * IPS_DEBUG            - Turn on debugging info
149  *
150  * Parameters:
151  *
152  * debug:<number>       - Set debug level to <number>
153  *                        NOTE: only works when IPS_DEBUG compile directive is used.
154  *       1              - Normal debug messages
155  *       2              - Verbose debug messages
156  *       11             - Method trace (non interrupt)
157  *       12             - Method trace (includes interrupt)
158  *
159  * noi2o                - Don't use I2O Queues (ServeRAID 4 only)
160  * nommap               - Don't use memory mapped I/O
161  * ioctlsize            - Initial size of the IOCTL buffer
162  */
163
164 #include <asm/io.h>
165 #include <asm/byteorder.h>
166 #include <asm/page.h>
167 #include <linux/stddef.h>
168 #include <linux/version.h>
169 #include <linux/string.h>
170 #include <linux/errno.h>
171 #include <linux/kernel.h>
172 #include <linux/ioport.h>
173 #include <linux/slab.h>
174 #include <linux/delay.h>
175 #include <linux/pci.h>
176 #include <linux/proc_fs.h>
177 #include <linux/reboot.h>
178 #include <linux/interrupt.h>
179
180 #include <linux/blkdev.h>
181 #include <linux/types.h>
182
183 #include <scsi/sg.h>
184
185 #include "scsi.h"
186
187 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0)
188 #include "hosts.h"
189 #else
190 #include <scsi/scsi_host.h>
191 #endif
192
193 #include "ips.h"
194
195 #include <linux/module.h>
196
197 #include <linux/stat.h>
198 #include <linux/config.h>
199
200 #include <linux/spinlock.h>
201 #include <linux/init.h>
202
203 #include <linux/smp.h>
204
205 #ifdef MODULE
206 static char *ips = NULL;
207 module_param(ips, charp, 0);
208 #endif
209
210 /*
211  * DRIVER_VER
212  */
213 #define IPS_VERSION_HIGH        "7.12"
214 #define IPS_VERSION_LOW         ".05 "
215
216 #if !defined(__i386__) && !defined(__ia64__) && !defined(__x86_64__)
217 #warning "This driver has only been tested on the x86/ia64/x86_64 platforms"
218 #endif
219
220 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0)
221 #include <linux/blk.h>
222 #include "sd.h"
223 #define IPS_LOCK_SAVE(lock,flags) spin_lock_irqsave(&io_request_lock,flags)
224 #define IPS_UNLOCK_RESTORE(lock,flags) spin_unlock_irqrestore(&io_request_lock,flags)
225 #ifndef __devexit_p
226 #define __devexit_p(x) x
227 #endif
228 #else
229 #define IPS_LOCK_SAVE(lock,flags) do{spin_lock(lock);(void)flags;}while(0)
230 #define IPS_UNLOCK_RESTORE(lock,flags) do{spin_unlock(lock);(void)flags;}while(0)
231 #endif
232
233 #define IPS_DMA_DIR(scb) ((!scb->scsi_cmd || ips_is_passthru(scb->scsi_cmd) || \
234                          DMA_NONE == scb->scsi_cmd->sc_data_direction) ? \
235                          PCI_DMA_BIDIRECTIONAL : \
236                          scb->scsi_cmd->sc_data_direction)
237
238 #ifdef IPS_DEBUG
239 #define METHOD_TRACE(s, i)    if (ips_debug >= (i+10)) printk(KERN_NOTICE s "\n");
240 #define DEBUG(i, s)           if (ips_debug >= i) printk(KERN_NOTICE s "\n");
241 #define DEBUG_VAR(i, s, v...) if (ips_debug >= i) printk(KERN_NOTICE s "\n", v);
242 #else
243 #define METHOD_TRACE(s, i)
244 #define DEBUG(i, s)
245 #define DEBUG_VAR(i, s, v...)
246 #endif
247
248 /*
249  * Function prototypes
250  */
251 static int ips_detect(struct scsi_host_template *);
252 static int ips_release(struct Scsi_Host *);
253 static int ips_eh_abort(Scsi_Cmnd *);
254 static int ips_eh_reset(Scsi_Cmnd *);
255 static int ips_queue(Scsi_Cmnd *, void (*)(Scsi_Cmnd *));
256 static const char *ips_info(struct Scsi_Host *);
257 static irqreturn_t do_ipsintr(int, void *, struct pt_regs *);
258 static int ips_hainit(ips_ha_t *);
259 static int ips_map_status(ips_ha_t *, ips_scb_t *, ips_stat_t *);
260 static int ips_send_wait(ips_ha_t *, ips_scb_t *, int, int);
261 static int ips_send_cmd(ips_ha_t *, ips_scb_t *);
262 static int ips_online(ips_ha_t *, ips_scb_t *);
263 static int ips_inquiry(ips_ha_t *, ips_scb_t *);
264 static int ips_rdcap(ips_ha_t *, ips_scb_t *);
265 static int ips_msense(ips_ha_t *, ips_scb_t *);
266 static int ips_reqsen(ips_ha_t *, ips_scb_t *);
267 static int ips_deallocatescbs(ips_ha_t *, int);
268 static int ips_allocatescbs(ips_ha_t *);
269 static int ips_reset_copperhead(ips_ha_t *);
270 static int ips_reset_copperhead_memio(ips_ha_t *);
271 static int ips_reset_morpheus(ips_ha_t *);
272 static int ips_issue_copperhead(ips_ha_t *, ips_scb_t *);
273 static int ips_issue_copperhead_memio(ips_ha_t *, ips_scb_t *);
274 static int ips_issue_i2o(ips_ha_t *, ips_scb_t *);
275 static int ips_issue_i2o_memio(ips_ha_t *, ips_scb_t *);
276 static int ips_isintr_copperhead(ips_ha_t *);
277 static int ips_isintr_copperhead_memio(ips_ha_t *);
278 static int ips_isintr_morpheus(ips_ha_t *);
279 static int ips_wait(ips_ha_t *, int, int);
280 static int ips_write_driver_status(ips_ha_t *, int);
281 static int ips_read_adapter_status(ips_ha_t *, int);
282 static int ips_read_subsystem_parameters(ips_ha_t *, int);
283 static int ips_read_config(ips_ha_t *, int);
284 static int ips_clear_adapter(ips_ha_t *, int);
285 static int ips_readwrite_page5(ips_ha_t *, int, int);
286 static int ips_init_copperhead(ips_ha_t *);
287 static int ips_init_copperhead_memio(ips_ha_t *);
288 static int ips_init_morpheus(ips_ha_t *);
289 static int ips_isinit_copperhead(ips_ha_t *);
290 static int ips_isinit_copperhead_memio(ips_ha_t *);
291 static int ips_isinit_morpheus(ips_ha_t *);
292 static int ips_erase_bios(ips_ha_t *);
293 static int ips_program_bios(ips_ha_t *, char *, uint32_t, uint32_t);
294 static int ips_verify_bios(ips_ha_t *, char *, uint32_t, uint32_t);
295 static int ips_erase_bios_memio(ips_ha_t *);
296 static int ips_program_bios_memio(ips_ha_t *, char *, uint32_t, uint32_t);
297 static int ips_verify_bios_memio(ips_ha_t *, char *, uint32_t, uint32_t);
298 static int ips_flash_copperhead(ips_ha_t *, ips_passthru_t *, ips_scb_t *);
299 static int ips_flash_bios(ips_ha_t *, ips_passthru_t *, ips_scb_t *);
300 static int ips_flash_firmware(ips_ha_t *, ips_passthru_t *, ips_scb_t *);
301 static void ips_free_flash_copperhead(ips_ha_t * ha);
302 static void ips_get_bios_version(ips_ha_t *, int);
303 static void ips_identify_controller(ips_ha_t *);
304 static void ips_chkstatus(ips_ha_t *, IPS_STATUS *);
305 static void ips_enable_int_copperhead(ips_ha_t *);
306 static void ips_enable_int_copperhead_memio(ips_ha_t *);
307 static void ips_enable_int_morpheus(ips_ha_t *);
308 static int ips_intr_copperhead(ips_ha_t *);
309 static int ips_intr_morpheus(ips_ha_t *);
310 static void ips_next(ips_ha_t *, int);
311 static void ipsintr_blocking(ips_ha_t *, struct ips_scb *);
312 static void ipsintr_done(ips_ha_t *, struct ips_scb *);
313 static void ips_done(ips_ha_t *, ips_scb_t *);
314 static void ips_free(ips_ha_t *);
315 static void ips_init_scb(ips_ha_t *, ips_scb_t *);
316 static void ips_freescb(ips_ha_t *, ips_scb_t *);
317 static void ips_setup_funclist(ips_ha_t *);
318 static void ips_statinit(ips_ha_t *);
319 static void ips_statinit_memio(ips_ha_t *);
320 static void ips_fix_ffdc_time(ips_ha_t *, ips_scb_t *, time_t);
321 static void ips_ffdc_reset(ips_ha_t *, int);
322 static void ips_ffdc_time(ips_ha_t *);
323 static uint32_t ips_statupd_copperhead(ips_ha_t *);
324 static uint32_t ips_statupd_copperhead_memio(ips_ha_t *);
325 static uint32_t ips_statupd_morpheus(ips_ha_t *);
326 static ips_scb_t *ips_getscb(ips_ha_t *);
327 static void ips_putq_scb_head(ips_scb_queue_t *, ips_scb_t *);
328 static void ips_putq_wait_tail(ips_wait_queue_t *, Scsi_Cmnd *);
329 static void ips_putq_copp_tail(ips_copp_queue_t *,
330                                       ips_copp_wait_item_t *);
331 static ips_scb_t *ips_removeq_scb_head(ips_scb_queue_t *);
332 static ips_scb_t *ips_removeq_scb(ips_scb_queue_t *, ips_scb_t *);
333 static Scsi_Cmnd *ips_removeq_wait_head(ips_wait_queue_t *);
334 static Scsi_Cmnd *ips_removeq_wait(ips_wait_queue_t *, Scsi_Cmnd *);
335 static ips_copp_wait_item_t *ips_removeq_copp(ips_copp_queue_t *,
336                                                      ips_copp_wait_item_t *);
337 static ips_copp_wait_item_t *ips_removeq_copp_head(ips_copp_queue_t *);
338
339 static int ips_is_passthru(Scsi_Cmnd *);
340 static int ips_make_passthru(ips_ha_t *, Scsi_Cmnd *, ips_scb_t *, int);
341 static int ips_usrcmd(ips_ha_t *, ips_passthru_t *, ips_scb_t *);
342 static void ips_cleanup_passthru(ips_ha_t *, ips_scb_t *);
343 static void ips_scmd_buf_write(Scsi_Cmnd * scmd, void *data,
344                                unsigned int count);
345 static void ips_scmd_buf_read(Scsi_Cmnd * scmd, void *data, unsigned int count);
346
347 static int ips_proc_info(struct Scsi_Host *, char *, char **, off_t, int, int);
348 static int ips_host_info(ips_ha_t *, char *, off_t, int);
349 static void copy_mem_info(IPS_INFOSTR *, char *, int);
350 static int copy_info(IPS_INFOSTR *, char *, ...);
351 static int ips_abort_init(ips_ha_t * ha, int index);
352 static int ips_init_phase2(int index);
353
354 static int ips_init_phase1(struct pci_dev *pci_dev, int *indexPtr);
355 static int ips_register_scsi(int index);
356
357 static int  ips_poll_for_flush_complete(ips_ha_t * ha);
358 static void ips_flush_and_reset(ips_ha_t *ha);
359
360 /*
361  * global variables
362  */
363 static const char ips_name[] = "ips";
364 static struct Scsi_Host *ips_sh[IPS_MAX_ADAPTERS];      /* Array of host controller structures */
365 static ips_ha_t *ips_ha[IPS_MAX_ADAPTERS];      /* Array of HA structures */
366 static unsigned int ips_next_controller;
367 static unsigned int ips_num_controllers;
368 static unsigned int ips_released_controllers;
369 static int ips_hotplug;
370 static int ips_cmd_timeout = 60;
371 static int ips_reset_timeout = 60 * 5;
372 static int ips_force_memio = 1;         /* Always use Memory Mapped I/O    */
373 static int ips_force_i2o = 1;   /* Always use I2O command delivery */
374 static int ips_ioctlsize = IPS_IOCTL_SIZE;      /* Size of the ioctl buffer        */
375 static int ips_cd_boot;                 /* Booting from Manager CD         */
376 static char *ips_FlashData = NULL;      /* CD Boot - Flash Data Buffer      */
377 static dma_addr_t ips_flashbusaddr;
378 static long ips_FlashDataInUse;         /* CD Boot - Flash Data In Use Flag */
379 static uint32_t MaxLiteCmds = 32;       /* Max Active Cmds for a Lite Adapter */
380 static struct scsi_host_template ips_driver_template = {
381         .detect                 = ips_detect,
382         .release                = ips_release,
383         .info                   = ips_info,
384         .queuecommand           = ips_queue,
385         .eh_abort_handler       = ips_eh_abort,
386         .eh_host_reset_handler  = ips_eh_reset,
387         .proc_name              = "ips",
388 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
389         .proc_info              = ips_proc_info,
390         .slave_configure        = ips_slave_configure,
391 #else
392         .proc_info              = ips_proc24_info,
393         .select_queue_depths    = ips_select_queue_depth,
394 #endif
395         .bios_param             = ips_biosparam,
396         .this_id                = -1,
397         .sg_tablesize           = IPS_MAX_SG,
398         .cmd_per_lun            = 3,
399         .use_clustering         = ENABLE_CLUSTERING,
400 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
401         .use_new_eh_code        = 1,
402 #endif
403 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,20)  &&  LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
404     .highmem_io          = 1,   
405 #endif
406 };
407
408
409 /* This table describes all ServeRAID Adapters */
410 static struct  pci_device_id  ips_pci_table[] = {
411         { 0x1014, 0x002E, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
412         { 0x1014, 0x01BD, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
413         { 0x9005, 0x0250, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
414         { 0, }
415 };
416
417 MODULE_DEVICE_TABLE( pci, ips_pci_table );
418
419 static char ips_hot_plug_name[] = "ips";
420    
421 static int __devinit  ips_insert_device(struct pci_dev *pci_dev, const struct pci_device_id *ent);
422 static void __devexit ips_remove_device(struct pci_dev *pci_dev);
423    
424 static struct pci_driver ips_pci_driver = {
425         .name           = ips_hot_plug_name,
426         .id_table       = ips_pci_table,
427         .probe          = ips_insert_device,
428         .remove         = __devexit_p(ips_remove_device),
429 };
430            
431
432 /*
433  * Necessary forward function protoypes
434  */
435 static int ips_halt(struct notifier_block *nb, ulong event, void *buf);
436
437 #define MAX_ADAPTER_NAME 15
438
439 static char ips_adapter_name[][30] = {
440         "ServeRAID",
441         "ServeRAID II",
442         "ServeRAID on motherboard",
443         "ServeRAID on motherboard",
444         "ServeRAID 3H",
445         "ServeRAID 3L",
446         "ServeRAID 4H",
447         "ServeRAID 4M",
448         "ServeRAID 4L",
449         "ServeRAID 4Mx",
450         "ServeRAID 4Lx",
451         "ServeRAID 5i",
452         "ServeRAID 5i",
453         "ServeRAID 6M",
454         "ServeRAID 6i",
455         "ServeRAID 7t",
456         "ServeRAID 7k",
457         "ServeRAID 7M"
458 };
459
460 static struct notifier_block ips_notifier = {
461         ips_halt, NULL, 0
462 };
463
464 /*
465  * Direction table
466  */
467 static char ips_command_direction[] = {
468         IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_IN, IPS_DATA_IN, IPS_DATA_OUT,
469         IPS_DATA_IN, IPS_DATA_IN, IPS_DATA_OUT, IPS_DATA_IN, IPS_DATA_UNK,
470         IPS_DATA_OUT, IPS_DATA_OUT, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
471         IPS_DATA_IN, IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_IN, IPS_DATA_OUT,
472         IPS_DATA_IN, IPS_DATA_OUT, IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_OUT,
473         IPS_DATA_NONE, IPS_DATA_IN, IPS_DATA_NONE, IPS_DATA_IN, IPS_DATA_OUT,
474         IPS_DATA_NONE, IPS_DATA_UNK, IPS_DATA_IN, IPS_DATA_UNK, IPS_DATA_IN,
475         IPS_DATA_UNK, IPS_DATA_OUT, IPS_DATA_IN, IPS_DATA_UNK, IPS_DATA_UNK,
476         IPS_DATA_IN, IPS_DATA_IN, IPS_DATA_OUT, IPS_DATA_NONE, IPS_DATA_UNK,
477         IPS_DATA_IN, IPS_DATA_OUT, IPS_DATA_OUT, IPS_DATA_OUT, IPS_DATA_OUT,
478         IPS_DATA_OUT, IPS_DATA_NONE, IPS_DATA_IN, IPS_DATA_NONE, IPS_DATA_NONE,
479         IPS_DATA_IN, IPS_DATA_OUT, IPS_DATA_OUT, IPS_DATA_OUT, IPS_DATA_OUT,
480         IPS_DATA_IN, IPS_DATA_OUT, IPS_DATA_IN, IPS_DATA_OUT, IPS_DATA_OUT,
481         IPS_DATA_OUT, IPS_DATA_IN, IPS_DATA_IN, IPS_DATA_IN, IPS_DATA_NONE,
482         IPS_DATA_UNK, IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_UNK,
483         IPS_DATA_NONE, IPS_DATA_OUT, IPS_DATA_IN, IPS_DATA_UNK, IPS_DATA_UNK,
484         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
485         IPS_DATA_OUT, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
486         IPS_DATA_IN, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
487         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
488         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
489         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
490         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
491         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
492         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
493         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
494         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
495         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
496         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
497         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
498         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
499         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
500         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
501         IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_UNK, IPS_DATA_IN, IPS_DATA_NONE,
502         IPS_DATA_OUT, IPS_DATA_UNK, IPS_DATA_NONE, IPS_DATA_UNK, IPS_DATA_OUT,
503         IPS_DATA_OUT, IPS_DATA_OUT, IPS_DATA_OUT, IPS_DATA_OUT, IPS_DATA_NONE,
504         IPS_DATA_UNK, IPS_DATA_IN, IPS_DATA_OUT, IPS_DATA_IN, IPS_DATA_IN,
505         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
506         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
507         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
508         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
509         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
510         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
511         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
512         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
513         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
514         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_OUT,
515         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
516         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
517         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
518         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK
519 };
520
521
522 /****************************************************************************/
523 /*                                                                          */
524 /* Routine Name: ips_setup                                                  */
525 /*                                                                          */
526 /* Routine Description:                                                     */
527 /*                                                                          */
528 /*   setup parameters to the driver                                         */
529 /*                                                                          */
530 /****************************************************************************/
531 static int
532 ips_setup(char *ips_str)
533 {
534
535         int i;
536         char *key;
537         char *value;
538         IPS_OPTION options[] = {
539                 {"noi2o", &ips_force_i2o, 0},
540                 {"nommap", &ips_force_memio, 0},
541                 {"ioctlsize", &ips_ioctlsize, IPS_IOCTL_SIZE},
542                 {"cdboot", &ips_cd_boot, 0},
543                 {"maxcmds", &MaxLiteCmds, 32},
544         };
545
546         /* Don't use strtok() anymore ( if 2.4 Kernel or beyond ) */
547         /* Search for value */
548         while ((key = strsep(&ips_str, ",."))) {
549                 if (!*key)
550                         continue;
551                 value = strchr(key, ':');
552                 if (value)
553                         *value++ = '\0';
554                 /*
555                  * We now have key/value pairs.
556                  * Update the variables
557                  */
558                 for (i = 0; i < (sizeof (options) / sizeof (options[0])); i++) {
559                         if (strnicmp
560                             (key, options[i].option_name,
561                              strlen(options[i].option_name)) == 0) {
562                                 if (value)
563                                         *options[i].option_flag =
564                                             simple_strtoul(value, NULL, 0);
565                                 else
566                                         *options[i].option_flag =
567                                             options[i].option_value;
568                                 break;
569                         }
570                 }
571         }
572
573         return (1);
574 }
575
576 __setup("ips=", ips_setup);
577
578 /****************************************************************************/
579 /*                                                                          */
580 /* Routine Name: ips_detect                                                 */
581 /*                                                                          */
582 /* Routine Description:                                                     */
583 /*                                                                          */
584 /*   Detect and initialize the driver                                       */
585 /*                                                                          */
586 /* NOTE: this routine is called under the io_request_lock spinlock          */
587 /*                                                                          */
588 /****************************************************************************/
589 static int
590 ips_detect(struct scsi_host_template * SHT)
591 {
592         int i;
593
594         METHOD_TRACE("ips_detect", 1);
595
596 #ifdef MODULE
597         if (ips)
598                 ips_setup(ips);
599 #endif
600
601         for (i = 0; i < ips_num_controllers; i++) {
602                 if (ips_register_scsi(i))
603                         ips_free(ips_ha[i]);
604                 ips_released_controllers++;
605         }
606         ips_hotplug = 1;
607         return (ips_num_controllers);
608 }
609
610 /****************************************************************************/
611 /*   configure the function pointers to use the functions that will work    */
612 /*   with the found version of the adapter                                  */
613 /****************************************************************************/
614 static void
615 ips_setup_funclist(ips_ha_t * ha)
616 {
617
618         /*                                
619          * Setup Functions
620          */
621         if (IPS_IS_MORPHEUS(ha) || IPS_IS_MARCO(ha)) {
622                 /* morpheus / marco / sebring */
623                 ha->func.isintr = ips_isintr_morpheus;
624                 ha->func.isinit = ips_isinit_morpheus;
625                 ha->func.issue = ips_issue_i2o_memio;
626                 ha->func.init = ips_init_morpheus;
627                 ha->func.statupd = ips_statupd_morpheus;
628                 ha->func.reset = ips_reset_morpheus;
629                 ha->func.intr = ips_intr_morpheus;
630                 ha->func.enableint = ips_enable_int_morpheus;
631         } else if (IPS_USE_MEMIO(ha)) {
632                 /* copperhead w/MEMIO */
633                 ha->func.isintr = ips_isintr_copperhead_memio;
634                 ha->func.isinit = ips_isinit_copperhead_memio;
635                 ha->func.init = ips_init_copperhead_memio;
636                 ha->func.statupd = ips_statupd_copperhead_memio;
637                 ha->func.statinit = ips_statinit_memio;
638                 ha->func.reset = ips_reset_copperhead_memio;
639                 ha->func.intr = ips_intr_copperhead;
640                 ha->func.erasebios = ips_erase_bios_memio;
641                 ha->func.programbios = ips_program_bios_memio;
642                 ha->func.verifybios = ips_verify_bios_memio;
643                 ha->func.enableint = ips_enable_int_copperhead_memio;
644                 if (IPS_USE_I2O_DELIVER(ha))
645                         ha->func.issue = ips_issue_i2o_memio;
646                 else
647                         ha->func.issue = ips_issue_copperhead_memio;
648         } else {
649                 /* copperhead */
650                 ha->func.isintr = ips_isintr_copperhead;
651                 ha->func.isinit = ips_isinit_copperhead;
652                 ha->func.init = ips_init_copperhead;
653                 ha->func.statupd = ips_statupd_copperhead;
654                 ha->func.statinit = ips_statinit;
655                 ha->func.reset = ips_reset_copperhead;
656                 ha->func.intr = ips_intr_copperhead;
657                 ha->func.erasebios = ips_erase_bios;
658                 ha->func.programbios = ips_program_bios;
659                 ha->func.verifybios = ips_verify_bios;
660                 ha->func.enableint = ips_enable_int_copperhead;
661
662                 if (IPS_USE_I2O_DELIVER(ha))
663                         ha->func.issue = ips_issue_i2o;
664                 else
665                         ha->func.issue = ips_issue_copperhead;
666         }
667 }
668
669 /****************************************************************************/
670 /*                                                                          */
671 /* Routine Name: ips_release                                                */
672 /*                                                                          */
673 /* Routine Description:                                                     */
674 /*                                                                          */
675 /*   Remove a driver                                                        */
676 /*                                                                          */
677 /****************************************************************************/
678 static int
679 ips_release(struct Scsi_Host *sh)
680 {
681         ips_scb_t *scb;
682         ips_ha_t *ha;
683         int i;
684
685         METHOD_TRACE("ips_release", 1);
686
687         for (i = 0; i < IPS_MAX_ADAPTERS && ips_sh[i] != sh; i++) ;
688
689         if (i == IPS_MAX_ADAPTERS) {
690                 printk(KERN_WARNING
691                        "(%s) release, invalid Scsi_Host pointer.\n", ips_name);
692                 BUG();
693                 return (FALSE);
694         }
695
696         ha = IPS_HA(sh);
697
698         if (!ha)
699                 return (FALSE);
700
701         /* flush the cache on the controller */
702         scb = &ha->scbs[ha->max_cmds - 1];
703
704         ips_init_scb(ha, scb);
705
706         scb->timeout = ips_cmd_timeout;
707         scb->cdb[0] = IPS_CMD_FLUSH;
708
709         scb->cmd.flush_cache.op_code = IPS_CMD_FLUSH;
710         scb->cmd.flush_cache.command_id = IPS_COMMAND_ID(ha, scb);
711         scb->cmd.flush_cache.state = IPS_NORM_STATE;
712         scb->cmd.flush_cache.reserved = 0;
713         scb->cmd.flush_cache.reserved2 = 0;
714         scb->cmd.flush_cache.reserved3 = 0;
715         scb->cmd.flush_cache.reserved4 = 0;
716
717         IPS_PRINTK(KERN_WARNING, ha->pcidev, "Flushing Cache.\n");
718
719         /* send command */
720         if (ips_send_wait(ha, scb, ips_cmd_timeout, IPS_INTR_ON) == IPS_FAILURE)
721                 IPS_PRINTK(KERN_WARNING, ha->pcidev, "Incomplete Flush.\n");
722
723         IPS_PRINTK(KERN_WARNING, ha->pcidev, "Flushing Complete.\n");
724
725         ips_sh[i] = NULL;
726         ips_ha[i] = NULL;
727
728         /* free extra memory */
729         ips_free(ha);
730
731         /* Free I/O Region */
732         if (ha->io_addr)
733                 release_region(ha->io_addr, ha->io_len);
734
735         /* free IRQ */
736         free_irq(ha->irq, ha);
737
738         IPS_REMOVE_HOST(sh);
739         scsi_host_put(sh);
740
741         ips_released_controllers++;
742
743         return (FALSE);
744 }
745
746 /****************************************************************************/
747 /*                                                                          */
748 /* Routine Name: ips_halt                                                   */
749 /*                                                                          */
750 /* Routine Description:                                                     */
751 /*                                                                          */
752 /*   Perform cleanup when the system reboots                                */
753 /*                                                                          */
754 /****************************************************************************/
755 static int
756 ips_halt(struct notifier_block *nb, ulong event, void *buf)
757 {
758         ips_scb_t *scb;
759         ips_ha_t *ha;
760         int i;
761
762         if ((event != SYS_RESTART) && (event != SYS_HALT) &&
763             (event != SYS_POWER_OFF))
764                 return (NOTIFY_DONE);
765
766         for (i = 0; i < ips_next_controller; i++) {
767                 ha = (ips_ha_t *) ips_ha[i];
768
769                 if (!ha)
770                         continue;
771
772                 if (!ha->active)
773                         continue;
774
775                 /* flush the cache on the controller */
776                 scb = &ha->scbs[ha->max_cmds - 1];
777
778                 ips_init_scb(ha, scb);
779
780                 scb->timeout = ips_cmd_timeout;
781                 scb->cdb[0] = IPS_CMD_FLUSH;
782
783                 scb->cmd.flush_cache.op_code = IPS_CMD_FLUSH;
784                 scb->cmd.flush_cache.command_id = IPS_COMMAND_ID(ha, scb);
785                 scb->cmd.flush_cache.state = IPS_NORM_STATE;
786                 scb->cmd.flush_cache.reserved = 0;
787                 scb->cmd.flush_cache.reserved2 = 0;
788                 scb->cmd.flush_cache.reserved3 = 0;
789                 scb->cmd.flush_cache.reserved4 = 0;
790
791                 IPS_PRINTK(KERN_WARNING, ha->pcidev, "Flushing Cache.\n");
792
793                 /* send command */
794                 if (ips_send_wait(ha, scb, ips_cmd_timeout, IPS_INTR_ON) ==
795                     IPS_FAILURE)
796                         IPS_PRINTK(KERN_WARNING, ha->pcidev,
797                                    "Incomplete Flush.\n");
798                 else
799                         IPS_PRINTK(KERN_WARNING, ha->pcidev,
800                                    "Flushing Complete.\n");
801         }
802
803         return (NOTIFY_OK);
804 }
805
806 /****************************************************************************/
807 /*                                                                          */
808 /* Routine Name: ips_eh_abort                                               */
809 /*                                                                          */
810 /* Routine Description:                                                     */
811 /*                                                                          */
812 /*   Abort a command (using the new error code stuff)                       */
813 /* Note: this routine is called under the io_request_lock                   */
814 /****************************************************************************/
815 int
816 ips_eh_abort(Scsi_Cmnd * SC)
817 {
818         ips_ha_t *ha;
819         ips_copp_wait_item_t *item;
820         int ret;
821         unsigned long cpu_flags;
822         struct Scsi_Host *host;
823
824         METHOD_TRACE("ips_eh_abort", 1);
825
826         if (!SC)
827                 return (FAILED);
828
829         host = SC->device->host;
830         ha = (ips_ha_t *) SC->device->host->hostdata;
831
832         if (!ha)
833                 return (FAILED);
834
835         if (!ha->active)
836                 return (FAILED);
837
838         IPS_LOCK_SAVE(host->host_lock, cpu_flags);
839
840         /* See if the command is on the copp queue */
841         item = ha->copp_waitlist.head;
842         while ((item) && (item->scsi_cmd != SC))
843                 item = item->next;
844
845         if (item) {
846                 /* Found it */
847                 ips_removeq_copp(&ha->copp_waitlist, item);
848                 ret = (SUCCESS);
849
850                 /* See if the command is on the wait queue */
851         } else if (ips_removeq_wait(&ha->scb_waitlist, SC)) {
852                 /* command not sent yet */
853                 ret = (SUCCESS);
854         } else {
855                 /* command must have already been sent */
856                 ret = (FAILED);
857         }
858
859         IPS_UNLOCK_RESTORE(host->host_lock, cpu_flags);
860         return ret;
861 }
862
863 /****************************************************************************/
864 /*                                                                          */
865 /* Routine Name: ips_eh_reset                                               */
866 /*                                                                          */
867 /* Routine Description:                                                     */
868 /*                                                                          */
869 /*   Reset the controller (with new eh error code)                          */
870 /*                                                                          */
871 /* NOTE: this routine is called under the io_request_lock spinlock          */
872 /*                                                                          */
873 /****************************************************************************/
874 static int
875 __ips_eh_reset(Scsi_Cmnd * SC)
876 {
877         int ret;
878         int i;
879         ips_ha_t *ha;
880         ips_scb_t *scb;
881         ips_copp_wait_item_t *item;
882
883         METHOD_TRACE("ips_eh_reset", 1);
884
885 #ifdef NO_IPS_RESET
886         return (FAILED);
887 #else
888
889         if (!SC) {
890                 DEBUG(1, "Reset called with NULL scsi command");
891
892                 return (FAILED);
893         }
894
895         ha = (ips_ha_t *) SC->device->host->hostdata;
896
897         if (!ha) {
898                 DEBUG(1, "Reset called with NULL ha struct");
899
900                 return (FAILED);
901         }
902
903         if (!ha->active)
904                 return (FAILED);
905
906         /* See if the command is on the copp queue */
907         item = ha->copp_waitlist.head;
908         while ((item) && (item->scsi_cmd != SC))
909                 item = item->next;
910
911         if (item) {
912                 /* Found it */
913                 ips_removeq_copp(&ha->copp_waitlist, item);
914                 return (SUCCESS);
915         }
916
917         /* See if the command is on the wait queue */
918         if (ips_removeq_wait(&ha->scb_waitlist, SC)) {
919                 /* command not sent yet */
920                 return (SUCCESS);
921         }
922
923         /* An explanation for the casual observer:                              */
924         /* Part of the function of a RAID controller is automatic error         */
925         /* detection and recovery.  As such, the only problem that physically   */
926         /* resetting an adapter will ever fix is when, for some reason,         */
927         /* the driver is not successfully communicating with the adapter.       */
928         /* Therefore, we will attempt to flush this adapter.  If that succeeds, */
929         /* then there's no real purpose in a physical reset. This will complete */
930         /* much faster and avoids any problems that might be caused by a        */
931         /* physical reset ( such as having to fail all the outstanding I/O's ). */
932
933         if (ha->ioctl_reset == 0) {     /* IF Not an IOCTL Requested Reset */
934                 scb = &ha->scbs[ha->max_cmds - 1];
935
936                 ips_init_scb(ha, scb);
937
938                 scb->timeout = ips_cmd_timeout;
939                 scb->cdb[0] = IPS_CMD_FLUSH;
940
941                 scb->cmd.flush_cache.op_code = IPS_CMD_FLUSH;
942                 scb->cmd.flush_cache.command_id = IPS_COMMAND_ID(ha, scb);
943                 scb->cmd.flush_cache.state = IPS_NORM_STATE;
944                 scb->cmd.flush_cache.reserved = 0;
945                 scb->cmd.flush_cache.reserved2 = 0;
946                 scb->cmd.flush_cache.reserved3 = 0;
947                 scb->cmd.flush_cache.reserved4 = 0;
948
949                 /* Attempt the flush command */
950                 ret = ips_send_wait(ha, scb, ips_cmd_timeout, IPS_INTR_IORL);
951                 if (ret == IPS_SUCCESS) {
952                         IPS_PRINTK(KERN_NOTICE, ha->pcidev,
953                                    "Reset Request - Flushed Cache\n");
954                         return (SUCCESS);
955                 }
956         }
957
958         /* Either we can't communicate with the adapter or it's an IOCTL request */
959         /* from a utility.  A physical reset is needed at this point.            */
960
961         ha->ioctl_reset = 0;    /* Reset the IOCTL Requested Reset Flag */
962
963         /*
964          * command must have already been sent
965          * reset the controller
966          */
967         IPS_PRINTK(KERN_NOTICE, ha->pcidev, "Resetting controller.\n");
968         ret = (*ha->func.reset) (ha);
969
970         if (!ret) {
971                 Scsi_Cmnd *scsi_cmd;
972
973                 IPS_PRINTK(KERN_NOTICE, ha->pcidev,
974                            "Controller reset failed - controller now offline.\n");
975
976                 /* Now fail all of the active commands */
977                 DEBUG_VAR(1, "(%s%d) Failing active commands",
978                           ips_name, ha->host_num);
979
980                 while ((scb = ips_removeq_scb_head(&ha->scb_activelist))) {
981                         scb->scsi_cmd->result = DID_ERROR << 16;
982                         scb->scsi_cmd->scsi_done(scb->scsi_cmd);
983                         ips_freescb(ha, scb);
984                 }
985
986                 /* Now fail all of the pending commands */
987                 DEBUG_VAR(1, "(%s%d) Failing pending commands",
988                           ips_name, ha->host_num);
989
990                 while ((scsi_cmd = ips_removeq_wait_head(&ha->scb_waitlist))) {
991                         scsi_cmd->result = DID_ERROR;
992                         scsi_cmd->scsi_done(scsi_cmd);
993                 }
994
995                 ha->active = FALSE;
996                 return (FAILED);
997         }
998
999         if (!ips_clear_adapter(ha, IPS_INTR_IORL)) {
1000                 Scsi_Cmnd *scsi_cmd;
1001
1002                 IPS_PRINTK(KERN_NOTICE, ha->pcidev,
1003                            "Controller reset failed - controller now offline.\n");
1004
1005                 /* Now fail all of the active commands */
1006                 DEBUG_VAR(1, "(%s%d) Failing active commands",
1007                           ips_name, ha->host_num);
1008
1009                 while ((scb = ips_removeq_scb_head(&ha->scb_activelist))) {
1010                         scb->scsi_cmd->result = DID_ERROR << 16;
1011                         scb->scsi_cmd->scsi_done(scb->scsi_cmd);
1012                         ips_freescb(ha, scb);
1013                 }
1014
1015                 /* Now fail all of the pending commands */
1016                 DEBUG_VAR(1, "(%s%d) Failing pending commands",
1017                           ips_name, ha->host_num);
1018
1019                 while ((scsi_cmd = ips_removeq_wait_head(&ha->scb_waitlist))) {
1020                         scsi_cmd->result = DID_ERROR << 16;
1021                         scsi_cmd->scsi_done(scsi_cmd);
1022                 }
1023
1024                 ha->active = FALSE;
1025                 return (FAILED);
1026         }
1027
1028         /* FFDC */
1029         if (le32_to_cpu(ha->subsys->param[3]) & 0x300000) {
1030                 struct timeval tv;
1031
1032                 do_gettimeofday(&tv);
1033                 ha->last_ffdc = tv.tv_sec;
1034                 ha->reset_count++;
1035                 ips_ffdc_reset(ha, IPS_INTR_IORL);
1036         }
1037
1038         /* Now fail all of the active commands */
1039         DEBUG_VAR(1, "(%s%d) Failing active commands", ips_name, ha->host_num);
1040
1041         while ((scb = ips_removeq_scb_head(&ha->scb_activelist))) {
1042                 scb->scsi_cmd->result =
1043                     (DID_RESET << 16) | (SUGGEST_RETRY << 24);
1044                 scb->scsi_cmd->scsi_done(scb->scsi_cmd);
1045                 ips_freescb(ha, scb);
1046         }
1047
1048         /* Reset DCDB active command bits */
1049         for (i = 1; i < ha->nbus; i++)
1050                 ha->dcdb_active[i - 1] = 0;
1051
1052         /* Reset the number of active IOCTLs */
1053         ha->num_ioctl = 0;
1054
1055         ips_next(ha, IPS_INTR_IORL);
1056
1057         return (SUCCESS);
1058 #endif                          /* NO_IPS_RESET */
1059
1060 }
1061
1062 static int
1063 ips_eh_reset(Scsi_Cmnd * SC)
1064 {
1065         int rc;
1066
1067         spin_lock_irq(SC->device->host->host_lock);
1068         rc = __ips_eh_reset(SC);
1069         spin_unlock_irq(SC->device->host->host_lock);
1070
1071         return rc;
1072 }
1073
1074 /****************************************************************************/
1075 /*                                                                          */
1076 /* Routine Name: ips_queue                                                  */
1077 /*                                                                          */
1078 /* Routine Description:                                                     */
1079 /*                                                                          */
1080 /*   Send a command to the controller                                       */
1081 /*                                                                          */
1082 /* NOTE:                                                                    */
1083 /*    Linux obtains io_request_lock before calling this function            */
1084 /*                                                                          */
1085 /****************************************************************************/
1086 static int
1087 ips_queue(Scsi_Cmnd * SC, void (*done) (Scsi_Cmnd *))
1088 {
1089         ips_ha_t *ha;
1090         ips_passthru_t *pt;
1091
1092         METHOD_TRACE("ips_queue", 1);
1093
1094         ha = (ips_ha_t *) SC->device->host->hostdata;
1095
1096         if (!ha)
1097                 return (1);
1098
1099         if (!ha->active)
1100                 return (DID_ERROR);
1101
1102         if (ips_is_passthru(SC)) {
1103                 if (ha->copp_waitlist.count == IPS_MAX_IOCTL_QUEUE) {
1104                         SC->result = DID_BUS_BUSY << 16;
1105                         done(SC);
1106
1107                         return (0);
1108                 }
1109         } else if (ha->scb_waitlist.count == IPS_MAX_QUEUE) {
1110                 SC->result = DID_BUS_BUSY << 16;
1111                 done(SC);
1112
1113                 return (0);
1114         }
1115
1116         SC->scsi_done = done;
1117
1118         DEBUG_VAR(2, "(%s%d): ips_queue: cmd 0x%X (%d %d %d)",
1119                   ips_name,
1120                   ha->host_num,
1121                   SC->cmnd[0],
1122                   SC->device->channel, SC->device->id, SC->device->lun);
1123
1124         /* Check for command to initiator IDs */
1125         if ((scmd_channel(SC) > 0)
1126             && (scmd_id(SC) == ha->ha_id[scmd_channel(SC)])) {
1127                 SC->result = DID_NO_CONNECT << 16;
1128                 done(SC);
1129
1130                 return (0);
1131         }
1132
1133         if (ips_is_passthru(SC)) {
1134
1135                 ips_copp_wait_item_t *scratch;
1136
1137                 /* A Reset IOCTL is only sent by the boot CD in extreme cases.           */
1138                 /* There can never be any system activity ( network or disk ), but check */
1139                 /* anyway just as a good practice.                                       */
1140                 pt = (ips_passthru_t *) SC->request_buffer;
1141                 if ((pt->CoppCP.cmd.reset.op_code == IPS_CMD_RESET_CHANNEL) &&
1142                     (pt->CoppCP.cmd.reset.adapter_flag == 1)) {
1143                         if (ha->scb_activelist.count != 0) {
1144                                 SC->result = DID_BUS_BUSY << 16;
1145                                 done(SC);
1146                                 return (0);
1147                         }
1148                         ha->ioctl_reset = 1;    /* This reset request is from an IOCTL */
1149                         ips_eh_reset(SC);
1150                         SC->result = DID_OK << 16;
1151                         SC->scsi_done(SC);
1152                         return (0);
1153                 }
1154
1155                 /* allocate space for the scribble */
1156                 scratch = kmalloc(sizeof (ips_copp_wait_item_t), GFP_ATOMIC);
1157
1158                 if (!scratch) {
1159                         SC->result = DID_ERROR << 16;
1160                         done(SC);
1161
1162                         return (0);
1163                 }
1164
1165                 scratch->scsi_cmd = SC;
1166                 scratch->next = NULL;
1167
1168                 ips_putq_copp_tail(&ha->copp_waitlist, scratch);
1169         } else {
1170                 ips_putq_wait_tail(&ha->scb_waitlist, SC);
1171         }
1172
1173         ips_next(ha, IPS_INTR_IORL);
1174
1175         return (0);
1176 }
1177
1178 /****************************************************************************/
1179 /*                                                                          */
1180 /* Routine Name: ips_biosparam                                              */
1181 /*                                                                          */
1182 /* Routine Description:                                                     */
1183 /*                                                                          */
1184 /*   Set bios geometry for the controller                                   */
1185 /*                                                                          */
1186 /****************************************************************************/
1187 static int
1188 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
1189 ips_biosparam(Disk * disk, kdev_t dev, int geom[])
1190 {
1191         ips_ha_t *ha = (ips_ha_t *) disk->device->host->hostdata;
1192         unsigned long capacity = disk->capacity;
1193 #else
1194 ips_biosparam(struct scsi_device *sdev, struct block_device *bdev,
1195               sector_t capacity, int geom[])
1196 {
1197         ips_ha_t *ha = (ips_ha_t *) sdev->host->hostdata;
1198 #endif
1199         int heads;
1200         int sectors;
1201         int cylinders;
1202
1203         METHOD_TRACE("ips_biosparam", 1);
1204
1205         if (!ha)
1206                 /* ?!?! host adater info invalid */
1207                 return (0);
1208
1209         if (!ha->active)
1210                 return (0);
1211
1212         if (!ips_read_adapter_status(ha, IPS_INTR_ON))
1213                 /* ?!?! Enquiry command failed */
1214                 return (0);
1215
1216         if ((capacity > 0x400000) && ((ha->enq->ucMiscFlag & 0x8) == 0)) {
1217                 heads = IPS_NORM_HEADS;
1218                 sectors = IPS_NORM_SECTORS;
1219         } else {
1220                 heads = IPS_COMP_HEADS;
1221                 sectors = IPS_COMP_SECTORS;
1222         }
1223
1224         cylinders = (unsigned long) capacity / (heads * sectors);
1225
1226         DEBUG_VAR(2, "Geometry: heads: %d, sectors: %d, cylinders: %d",
1227                   heads, sectors, cylinders);
1228
1229         geom[0] = heads;
1230         geom[1] = sectors;
1231         geom[2] = cylinders;
1232
1233         return (0);
1234 }
1235
1236 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
1237
1238 /* ips_proc24_info is a wrapper around ips_proc_info *
1239  * for compatibility with the 2.4 scsi parameters    */
1240 static int
1241 ips_proc24_info(char *buffer, char **start, off_t offset, int length,
1242                               int hostno, int func)
1243 {
1244         int i;
1245
1246         for (i = 0; i < ips_next_controller; i++) {
1247                 if (ips_sh[i] && ips_sh[i]->host_no == hostno) {
1248                         return ips_proc_info(ips_sh[i], buffer, start,
1249                                              offset, length, func);
1250                 }
1251         }
1252         return -EINVAL; 
1253 }
1254
1255 /****************************************************************************/
1256 /*                                                                          */
1257 /* Routine Name: ips_select_queue_depth                                     */
1258 /*                                                                          */
1259 /* Routine Description:                                                     */
1260 /*                                                                          */
1261 /*   Select queue depths for the devices on the contoller                   */
1262 /*                                                                          */
1263 /****************************************************************************/
1264 static void
1265 ips_select_queue_depth(struct Scsi_Host *host, struct scsi_device * scsi_devs)
1266 {
1267         struct scsi_device *device;
1268         ips_ha_t *ha;
1269         int count = 0;
1270         int min;
1271
1272         ha = IPS_HA(host);
1273         min = ha->max_cmds / 4;
1274
1275         for (device = scsi_devs; device; device = device->next) {
1276                 if (device->host == host) {
1277                         if ((device->channel == 0) && (device->type == 0))
1278                                 count++;
1279                 }
1280         }
1281
1282         for (device = scsi_devs; device; device = device->next) {
1283                 if (device->host == host) {
1284                         if ((device->channel == 0) && (device->type == 0)) {
1285                                 device->queue_depth =
1286                                     (ha->max_cmds - 1) / count;
1287                                 if (device->queue_depth < min)
1288                                         device->queue_depth = min;
1289                         } else {
1290                                 device->queue_depth = 2;
1291                         }
1292
1293                         if (device->queue_depth < 2)
1294                                 device->queue_depth = 2;
1295                 }
1296         }
1297 }
1298
1299 #else
1300 /****************************************************************************/
1301 /*                                                                          */
1302 /* Routine Name: ips_slave_configure                                        */
1303 /*                                                                          */
1304 /* Routine Description:                                                     */
1305 /*                                                                          */
1306 /*   Set queue depths on devices once scan is complete                      */
1307 /*                                                                          */
1308 /****************************************************************************/
1309 static int
1310 ips_slave_configure(struct scsi_device * SDptr)
1311 {
1312         ips_ha_t *ha;
1313         int min;
1314
1315         ha = IPS_HA(SDptr->host);
1316         if (SDptr->tagged_supported && SDptr->type == TYPE_DISK) {
1317                 min = ha->max_cmds / 2;
1318                 if (ha->enq->ucLogDriveCount <= 2)
1319                         min = ha->max_cmds - 1;
1320                 scsi_adjust_queue_depth(SDptr, MSG_ORDERED_TAG, min);
1321         }
1322
1323         SDptr->skip_ms_page_8 = 1;
1324         SDptr->skip_ms_page_3f = 1;
1325         return 0;
1326 }
1327 #endif
1328
1329 /****************************************************************************/
1330 /*                                                                          */
1331 /* Routine Name: do_ipsintr                                                 */
1332 /*                                                                          */
1333 /* Routine Description:                                                     */
1334 /*                                                                          */
1335 /*   Wrapper for the interrupt handler                                      */
1336 /*                                                                          */
1337 /****************************************************************************/
1338 static irqreturn_t
1339 do_ipsintr(int irq, void *dev_id, struct pt_regs * regs)
1340 {
1341         ips_ha_t *ha;
1342         unsigned long cpu_flags;
1343         struct Scsi_Host *host;
1344         int irqstatus;
1345
1346         METHOD_TRACE("do_ipsintr", 2);
1347
1348         ha = (ips_ha_t *) dev_id;
1349         if (!ha)
1350                 return IRQ_NONE;
1351         host = ips_sh[ha->host_num];
1352         /* interrupt during initialization */
1353         if (!host) {
1354                 (*ha->func.intr) (ha);
1355                 return IRQ_HANDLED;
1356         }
1357
1358         IPS_LOCK_SAVE(host->host_lock, cpu_flags);
1359
1360         if (!ha->active) {
1361                 IPS_UNLOCK_RESTORE(host->host_lock, cpu_flags);
1362                 return IRQ_HANDLED;
1363         }
1364
1365         irqstatus = (*ha->func.intr) (ha);
1366
1367         IPS_UNLOCK_RESTORE(host->host_lock, cpu_flags);
1368
1369         /* start the next command */
1370         ips_next(ha, IPS_INTR_ON);
1371         return IRQ_RETVAL(irqstatus);
1372 }
1373
1374 /****************************************************************************/
1375 /*                                                                          */
1376 /* Routine Name: ips_intr_copperhead                                        */
1377 /*                                                                          */
1378 /* Routine Description:                                                     */
1379 /*                                                                          */
1380 /*   Polling interrupt handler                                              */
1381 /*                                                                          */
1382 /*   ASSUMES interrupts are disabled                                        */
1383 /*                                                                          */
1384 /****************************************************************************/
1385 int
1386 ips_intr_copperhead(ips_ha_t * ha)
1387 {
1388         ips_stat_t *sp;
1389         ips_scb_t *scb;
1390         IPS_STATUS cstatus;
1391         int intrstatus;
1392
1393         METHOD_TRACE("ips_intr", 2);
1394
1395         if (!ha)
1396                 return 0;
1397
1398         if (!ha->active)
1399                 return 0;
1400
1401         intrstatus = (*ha->func.isintr) (ha);
1402
1403         if (!intrstatus) {
1404                 /*
1405                  * Unexpected/Shared interrupt
1406                  */
1407
1408                 return 0;
1409         }
1410
1411         while (TRUE) {
1412                 sp = &ha->sp;
1413
1414                 intrstatus = (*ha->func.isintr) (ha);
1415
1416                 if (!intrstatus)
1417                         break;
1418                 else
1419                         cstatus.value = (*ha->func.statupd) (ha);
1420
1421                 if (cstatus.fields.command_id > (IPS_MAX_CMDS - 1)) {
1422                         /* Spurious Interupt ? */
1423                         continue;
1424                 }
1425
1426                 ips_chkstatus(ha, &cstatus);
1427                 scb = (ips_scb_t *) sp->scb_addr;
1428
1429                 /*
1430                  * use the callback function to finish things up
1431                  * NOTE: interrupts are OFF for this
1432                  */
1433                 (*scb->callback) (ha, scb);
1434         }                       /* end while */
1435         return 1;
1436 }
1437
1438 /****************************************************************************/
1439 /*                                                                          */
1440 /* Routine Name: ips_intr_morpheus                                          */
1441 /*                                                                          */
1442 /* Routine Description:                                                     */
1443 /*                                                                          */
1444 /*   Polling interrupt handler                                              */
1445 /*                                                                          */
1446 /*   ASSUMES interrupts are disabled                                        */
1447 /*                                                                          */
1448 /****************************************************************************/
1449 int
1450 ips_intr_morpheus(ips_ha_t * ha)
1451 {
1452         ips_stat_t *sp;
1453         ips_scb_t *scb;
1454         IPS_STATUS cstatus;
1455         int intrstatus;
1456
1457         METHOD_TRACE("ips_intr_morpheus", 2);
1458
1459         if (!ha)
1460                 return 0;
1461
1462         if (!ha->active)
1463                 return 0;
1464
1465         intrstatus = (*ha->func.isintr) (ha);
1466
1467         if (!intrstatus) {
1468                 /*
1469                  * Unexpected/Shared interrupt
1470                  */
1471
1472                 return 0;
1473         }
1474
1475         while (TRUE) {
1476                 sp = &ha->sp;
1477
1478                 intrstatus = (*ha->func.isintr) (ha);
1479
1480                 if (!intrstatus)
1481                         break;
1482                 else
1483                         cstatus.value = (*ha->func.statupd) (ha);
1484
1485                 if (cstatus.value == 0xffffffff)
1486                         /* No more to process */
1487                         break;
1488
1489                 if (cstatus.fields.command_id > (IPS_MAX_CMDS - 1)) {
1490                         IPS_PRINTK(KERN_WARNING, ha->pcidev,
1491                                    "Spurious interrupt; no ccb.\n");
1492
1493                         continue;
1494                 }
1495
1496                 ips_chkstatus(ha, &cstatus);
1497                 scb = (ips_scb_t *) sp->scb_addr;
1498
1499                 /*
1500                  * use the callback function to finish things up
1501                  * NOTE: interrupts are OFF for this
1502                  */
1503                 (*scb->callback) (ha, scb);
1504         }                       /* end while */
1505         return 1;
1506 }
1507
1508 /****************************************************************************/
1509 /*                                                                          */
1510 /* Routine Name: ips_info                                                   */
1511 /*                                                                          */
1512 /* Routine Description:                                                     */
1513 /*                                                                          */
1514 /*   Return info about the driver                                           */
1515 /*                                                                          */
1516 /****************************************************************************/
1517 static const char *
1518 ips_info(struct Scsi_Host *SH)
1519 {
1520         static char buffer[256];
1521         char *bp;
1522         ips_ha_t *ha;
1523
1524         METHOD_TRACE("ips_info", 1);
1525
1526         ha = IPS_HA(SH);
1527
1528         if (!ha)
1529                 return (NULL);
1530
1531         bp = &buffer[0];
1532         memset(bp, 0, sizeof (buffer));
1533
1534         sprintf(bp, "%s%s%s Build %d", "IBM PCI ServeRAID ",
1535                 IPS_VERSION_HIGH, IPS_VERSION_LOW, IPS_BUILD_IDENT);
1536
1537         if (ha->ad_type > 0 && ha->ad_type <= MAX_ADAPTER_NAME) {
1538                 strcat(bp, " <");
1539                 strcat(bp, ips_adapter_name[ha->ad_type - 1]);
1540                 strcat(bp, ">");
1541         }
1542
1543         return (bp);
1544 }
1545
1546 /****************************************************************************/
1547 /*                                                                          */
1548 /* Routine Name: ips_proc_info                                              */
1549 /*                                                                          */
1550 /* Routine Description:                                                     */
1551 /*                                                                          */
1552 /*   The passthru interface for the driver                                  */
1553 /*                                                                          */
1554 /****************************************************************************/
1555 static int
1556 ips_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
1557               int length, int func)
1558 {
1559         int i;
1560         int ret;
1561         ips_ha_t *ha = NULL;
1562
1563         METHOD_TRACE("ips_proc_info", 1);
1564
1565         /* Find our host structure */
1566         for (i = 0; i < ips_next_controller; i++) {
1567                 if (ips_sh[i]) {
1568                         if (ips_sh[i] == host) {
1569                                 ha = (ips_ha_t *) ips_sh[i]->hostdata;
1570                                 break;
1571                         }
1572                 }
1573         }
1574
1575         if (!ha)
1576                 return (-EINVAL);
1577
1578         if (func) {
1579                 /* write */
1580                 return (0);
1581         } else {
1582                 /* read */
1583                 if (start)
1584                         *start = buffer;
1585
1586                 ret = ips_host_info(ha, buffer, offset, length);
1587
1588                 return (ret);
1589         }
1590 }
1591
1592 /*--------------------------------------------------------------------------*/
1593 /* Helper Functions                                                         */
1594 /*--------------------------------------------------------------------------*/
1595
1596 /****************************************************************************/
1597 /*                                                                          */
1598 /* Routine Name: ips_is_passthru                                            */
1599 /*                                                                          */
1600 /* Routine Description:                                                     */
1601 /*                                                                          */
1602 /*   Determine if the specified SCSI command is really a passthru command   */
1603 /*                                                                          */
1604 /****************************************************************************/
1605 static int
1606 ips_is_passthru(Scsi_Cmnd * SC)
1607 {
1608         unsigned long flags;
1609
1610         METHOD_TRACE("ips_is_passthru", 1);
1611
1612         if (!SC)
1613                 return (0);
1614
1615         if ((SC->cmnd[0] == IPS_IOCTL_COMMAND) &&
1616             (SC->device->channel == 0) &&
1617             (SC->device->id == IPS_ADAPTER_ID) &&
1618             (SC->device->lun == 0) && SC->request_buffer) {
1619                 if ((!SC->use_sg) && SC->request_bufflen &&
1620                     (((char *) SC->request_buffer)[0] == 'C') &&
1621                     (((char *) SC->request_buffer)[1] == 'O') &&
1622                     (((char *) SC->request_buffer)[2] == 'P') &&
1623                     (((char *) SC->request_buffer)[3] == 'P'))
1624                         return 1;
1625                 else if (SC->use_sg) {
1626                         struct scatterlist *sg = SC->request_buffer;
1627                         char  *buffer; 
1628
1629                         /* kmap_atomic() ensures addressability of the user buffer.*/
1630                         /* local_irq_save() protects the KM_IRQ0 address slot.     */
1631                         local_irq_save(flags);
1632                         buffer = kmap_atomic(sg->page, KM_IRQ0) + sg->offset; 
1633                         if (buffer && buffer[0] == 'C' && buffer[1] == 'O' &&
1634                             buffer[2] == 'P' && buffer[3] == 'P') {
1635                                 kunmap_atomic(buffer - sg->offset, KM_IRQ0);
1636                                 local_irq_restore(flags);
1637                                 return 1;
1638                         }
1639                         kunmap_atomic(buffer - sg->offset, KM_IRQ0);
1640                         local_irq_restore(flags);
1641                 }
1642         }
1643         return 0;
1644 }
1645
1646 /****************************************************************************/
1647 /*                                                                          */
1648 /* Routine Name: ips_alloc_passthru_buffer                                  */
1649 /*                                                                          */
1650 /* Routine Description:                                                     */
1651 /*   allocate a buffer large enough for the ioctl data if the ioctl buffer  */
1652 /*   is too small or doesn't exist                                          */
1653 /****************************************************************************/
1654 static int
1655 ips_alloc_passthru_buffer(ips_ha_t * ha, int length)
1656 {
1657         void *bigger_buf;
1658         dma_addr_t dma_busaddr;
1659
1660         if (ha->ioctl_data && length <= ha->ioctl_len)
1661                 return 0;
1662         /* there is no buffer or it's not big enough, allocate a new one */
1663         bigger_buf = pci_alloc_consistent(ha->pcidev, length, &dma_busaddr);
1664         if (bigger_buf) {
1665                 /* free the old memory */
1666                 pci_free_consistent(ha->pcidev, ha->ioctl_len, ha->ioctl_data,
1667                                     ha->ioctl_busaddr);
1668                 /* use the new memory */
1669                 ha->ioctl_data = (char *) bigger_buf;
1670                 ha->ioctl_len = length;
1671                 ha->ioctl_busaddr = dma_busaddr;
1672         } else {
1673                 return -1;
1674         }
1675         return 0;
1676 }
1677
1678 /****************************************************************************/
1679 /*                                                                          */
1680 /* Routine Name: ips_make_passthru                                          */
1681 /*                                                                          */
1682 /* Routine Description:                                                     */
1683 /*                                                                          */
1684 /*   Make a passthru command out of the info in the Scsi block              */
1685 /*                                                                          */
1686 /****************************************************************************/
1687 static int
1688 ips_make_passthru(ips_ha_t * ha, Scsi_Cmnd * SC, ips_scb_t * scb, int intr)
1689 {
1690         ips_passthru_t *pt;
1691         int length = 0;
1692         int ret;
1693
1694         METHOD_TRACE("ips_make_passthru", 1);
1695
1696         if (!SC->use_sg) {
1697                 length = SC->request_bufflen;
1698         } else {
1699                 struct scatterlist *sg = SC->request_buffer;
1700                 int i;
1701                 for (i = 0; i < SC->use_sg; i++)
1702                         length += sg[i].length;
1703         }
1704         if (length < sizeof (ips_passthru_t)) {
1705                 /* wrong size */
1706                 DEBUG_VAR(1, "(%s%d) Passthru structure wrong size",
1707                           ips_name, ha->host_num);
1708                 return (IPS_FAILURE);
1709         }
1710         if (ips_alloc_passthru_buffer(ha, length)) {
1711                 /* allocation failure!  If ha->ioctl_data exists, use it to return
1712                    some error codes.  Return a failed command to the scsi layer. */
1713                 if (ha->ioctl_data) {
1714                         pt = (ips_passthru_t *) ha->ioctl_data;
1715                         ips_scmd_buf_read(SC, pt, sizeof (ips_passthru_t));
1716                         pt->BasicStatus = 0x0B;
1717                         pt->ExtendedStatus = 0x00;
1718                         ips_scmd_buf_write(SC, pt, sizeof (ips_passthru_t));
1719                 }
1720                 return IPS_FAILURE;
1721         }
1722         ha->ioctl_datasize = length;
1723
1724         ips_scmd_buf_read(SC, ha->ioctl_data, ha->ioctl_datasize);
1725         pt = (ips_passthru_t *) ha->ioctl_data;
1726
1727         /*
1728          * Some notes about the passthru interface used
1729          *
1730          * IF the scsi op_code == 0x0d then we assume
1731          * that the data came along with/goes with the
1732          * packet we received from the sg driver. In this
1733          * case the CmdBSize field of the pt structure is
1734          * used for the size of the buffer.
1735          */
1736
1737         switch (pt->CoppCmd) {
1738         case IPS_NUMCTRLS:
1739                 memcpy(ha->ioctl_data + sizeof (ips_passthru_t),
1740                        &ips_num_controllers, sizeof (int));
1741                 ips_scmd_buf_write(SC, ha->ioctl_data,
1742                                    sizeof (ips_passthru_t) + sizeof (int));
1743                 SC->result = DID_OK << 16;
1744
1745                 return (IPS_SUCCESS_IMM);
1746
1747         case IPS_COPPUSRCMD:
1748         case IPS_COPPIOCCMD:
1749                 if (SC->cmnd[0] == IPS_IOCTL_COMMAND) {
1750                         if (length < (sizeof (ips_passthru_t) + pt->CmdBSize)) {
1751                                 /* wrong size */
1752                                 DEBUG_VAR(1,
1753                                           "(%s%d) Passthru structure wrong size",
1754                                           ips_name, ha->host_num);
1755
1756                                 return (IPS_FAILURE);
1757                         }
1758
1759                         if (ha->device_id == IPS_DEVICEID_COPPERHEAD &&
1760                             pt->CoppCP.cmd.flashfw.op_code ==
1761                             IPS_CMD_RW_BIOSFW) {
1762                                 ret = ips_flash_copperhead(ha, pt, scb);
1763                                 ips_scmd_buf_write(SC, ha->ioctl_data,
1764                                                    sizeof (ips_passthru_t));
1765                                 return ret;
1766                         }
1767                         if (ips_usrcmd(ha, pt, scb))
1768                                 return (IPS_SUCCESS);
1769                         else
1770                                 return (IPS_FAILURE);
1771                 }
1772
1773                 break;
1774
1775         }                       /* end switch */
1776
1777         return (IPS_FAILURE);
1778 }
1779
1780 /****************************************************************************/
1781 /* Routine Name: ips_flash_copperhead                                       */
1782 /* Routine Description:                                                     */
1783 /*   Flash the BIOS/FW on a Copperhead style controller                     */
1784 /****************************************************************************/
1785 static int
1786 ips_flash_copperhead(ips_ha_t * ha, ips_passthru_t * pt, ips_scb_t * scb)
1787 {
1788         int datasize;
1789
1790         /* Trombone is the only copperhead that can do packet flash, but only
1791          * for firmware. No one said it had to make sence. */
1792         if (IPS_IS_TROMBONE(ha) && pt->CoppCP.cmd.flashfw.type == IPS_FW_IMAGE) {
1793                 if (ips_usrcmd(ha, pt, scb))
1794                         return IPS_SUCCESS;
1795                 else
1796                         return IPS_FAILURE;
1797         }
1798         pt->BasicStatus = 0x0B;
1799         pt->ExtendedStatus = 0;
1800         scb->scsi_cmd->result = DID_OK << 16;
1801         /* IF it's OK to Use the "CD BOOT" Flash Buffer, then you can     */
1802         /* avoid allocating a huge buffer per adapter ( which can fail ). */
1803         if (pt->CoppCP.cmd.flashfw.type == IPS_BIOS_IMAGE &&
1804             pt->CoppCP.cmd.flashfw.direction == IPS_ERASE_BIOS) {
1805                 pt->BasicStatus = 0;
1806                 return ips_flash_bios(ha, pt, scb);
1807         } else if (pt->CoppCP.cmd.flashfw.packet_num == 0) {
1808                 if (ips_FlashData && !test_and_set_bit(0, &ips_FlashDataInUse)){
1809                         ha->flash_data = ips_FlashData;
1810                         ha->flash_busaddr = ips_flashbusaddr;
1811                         ha->flash_len = PAGE_SIZE << 7;
1812                         ha->flash_datasize = 0;
1813                 } else if (!ha->flash_data) {
1814                         datasize = pt->CoppCP.cmd.flashfw.total_packets *
1815                             pt->CoppCP.cmd.flashfw.count;
1816                         ha->flash_data = pci_alloc_consistent(ha->pcidev,
1817                                                               datasize,
1818                                                               &ha->flash_busaddr);
1819                         if (!ha->flash_data){
1820                                 printk(KERN_WARNING "Unable to allocate a flash buffer\n");
1821                                 return IPS_FAILURE;
1822                         }
1823                         ha->flash_datasize = 0;
1824                         ha->flash_len = datasize;
1825                 } else
1826                         return IPS_FAILURE;
1827         } else {
1828                 if (pt->CoppCP.cmd.flashfw.count + ha->flash_datasize >
1829                     ha->flash_len) {
1830                         ips_free_flash_copperhead(ha);
1831                         IPS_PRINTK(KERN_WARNING, ha->pcidev,
1832                                    "failed size sanity check\n");
1833                         return IPS_FAILURE;
1834                 }
1835         }
1836         if (!ha->flash_data)
1837                 return IPS_FAILURE;
1838         pt->BasicStatus = 0;
1839         memcpy(&ha->flash_data[ha->flash_datasize], pt + 1,
1840                pt->CoppCP.cmd.flashfw.count);
1841         ha->flash_datasize += pt->CoppCP.cmd.flashfw.count;
1842         if (pt->CoppCP.cmd.flashfw.packet_num ==
1843             pt->CoppCP.cmd.flashfw.total_packets - 1) {
1844                 if (pt->CoppCP.cmd.flashfw.type == IPS_BIOS_IMAGE)
1845                         return ips_flash_bios(ha, pt, scb);
1846                 else if (pt->CoppCP.cmd.flashfw.type == IPS_FW_IMAGE)
1847                         return ips_flash_firmware(ha, pt, scb);
1848         }
1849         return IPS_SUCCESS_IMM;
1850 }
1851
1852 /****************************************************************************/
1853 /* Routine Name: ips_flash_bios                                             */
1854 /* Routine Description:                                                     */
1855 /*   flashes the bios of a copperhead adapter                               */
1856 /****************************************************************************/
1857 static int
1858 ips_flash_bios(ips_ha_t * ha, ips_passthru_t * pt, ips_scb_t * scb)
1859 {
1860
1861         if (pt->CoppCP.cmd.flashfw.type == IPS_BIOS_IMAGE &&
1862             pt->CoppCP.cmd.flashfw.direction == IPS_WRITE_BIOS) {
1863                 if ((!ha->func.programbios) || (!ha->func.erasebios) ||
1864                     (!ha->func.verifybios))
1865                         goto error;
1866                 if ((*ha->func.erasebios) (ha)) {
1867                         DEBUG_VAR(1,
1868                                   "(%s%d) flash bios failed - unable to erase flash",
1869                                   ips_name, ha->host_num);
1870                         goto error;
1871                 } else
1872                     if ((*ha->func.programbios) (ha,
1873                                                  ha->flash_data +
1874                                                  IPS_BIOS_HEADER,
1875                                                  ha->flash_datasize -
1876                                                  IPS_BIOS_HEADER, 0)) {
1877                         DEBUG_VAR(1,
1878                                   "(%s%d) flash bios failed - unable to flash",
1879                                   ips_name, ha->host_num);
1880                         goto error;
1881                 } else
1882                     if ((*ha->func.verifybios) (ha,
1883                                                 ha->flash_data +
1884                                                 IPS_BIOS_HEADER,
1885                                                 ha->flash_datasize -
1886                                                 IPS_BIOS_HEADER, 0)) {
1887                         DEBUG_VAR(1,
1888                                   "(%s%d) flash bios failed - unable to verify flash",
1889                                   ips_name, ha->host_num);
1890                         goto error;
1891                 }
1892                 ips_free_flash_copperhead(ha);
1893                 return IPS_SUCCESS_IMM;
1894         } else if (pt->CoppCP.cmd.flashfw.type == IPS_BIOS_IMAGE &&
1895                    pt->CoppCP.cmd.flashfw.direction == IPS_ERASE_BIOS) {
1896                 if (!ha->func.erasebios)
1897                         goto error;
1898                 if ((*ha->func.erasebios) (ha)) {
1899                         DEBUG_VAR(1,
1900                                   "(%s%d) flash bios failed - unable to erase flash",
1901                                   ips_name, ha->host_num);
1902                         goto error;
1903                 }
1904                 return IPS_SUCCESS_IMM;
1905         }
1906       error:
1907         pt->BasicStatus = 0x0B;
1908         pt->ExtendedStatus = 0x00;
1909         ips_free_flash_copperhead(ha);
1910         return IPS_FAILURE;
1911 }
1912
1913 /****************************************************************************/
1914 /*                                                                          */
1915 /* Routine Name: ips_fill_scb_sg_single                                     */
1916 /*                                                                          */
1917 /* Routine Description:                                                     */
1918 /*   Fill in a single scb sg_list element from an address                   */
1919 /*   return a -1 if a breakup occurred                                      */
1920 /****************************************************************************/
1921 static int
1922 ips_fill_scb_sg_single(ips_ha_t * ha, dma_addr_t busaddr,
1923                        ips_scb_t * scb, int indx, unsigned int e_len)
1924 {
1925
1926         int ret_val = 0;
1927
1928         if ((scb->data_len + e_len) > ha->max_xfer) {
1929                 e_len = ha->max_xfer - scb->data_len;
1930                 scb->breakup = indx;
1931                 ++scb->sg_break;
1932                 ret_val = -1;
1933         } else {
1934                 scb->breakup = 0;
1935                 scb->sg_break = 0;
1936         }
1937         if (IPS_USE_ENH_SGLIST(ha)) {
1938                 scb->sg_list.enh_list[indx].address_lo =
1939                     cpu_to_le32(pci_dma_lo32(busaddr));
1940                 scb->sg_list.enh_list[indx].address_hi =
1941                     cpu_to_le32(pci_dma_hi32(busaddr));
1942                 scb->sg_list.enh_list[indx].length = cpu_to_le32(e_len);
1943         } else {
1944                 scb->sg_list.std_list[indx].address =
1945                     cpu_to_le32(pci_dma_lo32(busaddr));
1946                 scb->sg_list.std_list[indx].length = cpu_to_le32(e_len);
1947         }
1948
1949         ++scb->sg_len;
1950         scb->data_len += e_len;
1951         return ret_val;
1952 }
1953
1954 /****************************************************************************/
1955 /* Routine Name: ips_flash_firmware                                         */
1956 /* Routine Description:                                                     */
1957 /*   flashes the firmware of a copperhead adapter                           */
1958 /****************************************************************************/
1959 static int
1960 ips_flash_firmware(ips_ha_t * ha, ips_passthru_t * pt, ips_scb_t * scb)
1961 {
1962         IPS_SG_LIST sg_list;
1963         uint32_t cmd_busaddr;
1964
1965         if (pt->CoppCP.cmd.flashfw.type == IPS_FW_IMAGE &&
1966             pt->CoppCP.cmd.flashfw.direction == IPS_WRITE_FW) {
1967                 memset(&pt->CoppCP.cmd, 0, sizeof (IPS_HOST_COMMAND));
1968                 pt->CoppCP.cmd.flashfw.op_code = IPS_CMD_DOWNLOAD;
1969                 pt->CoppCP.cmd.flashfw.count = cpu_to_le32(ha->flash_datasize);
1970         } else {
1971                 pt->BasicStatus = 0x0B;
1972                 pt->ExtendedStatus = 0x00;
1973                 ips_free_flash_copperhead(ha);
1974                 return IPS_FAILURE;
1975         }
1976         /* Save the S/G list pointer so it doesn't get clobbered */
1977         sg_list.list = scb->sg_list.list;
1978         cmd_busaddr = scb->scb_busaddr;
1979         /* copy in the CP */
1980         memcpy(&scb->cmd, &pt->CoppCP.cmd, sizeof (IPS_IOCTL_CMD));
1981         /* FIX stuff that might be wrong */
1982         scb->sg_list.list = sg_list.list;
1983         scb->scb_busaddr = cmd_busaddr;
1984         scb->bus = scb->scsi_cmd->device->channel;
1985         scb->target_id = scb->scsi_cmd->device->id;
1986         scb->lun = scb->scsi_cmd->device->lun;
1987         scb->sg_len = 0;
1988         scb->data_len = 0;
1989         scb->flags = 0;
1990         scb->op_code = 0;
1991         scb->callback = ipsintr_done;
1992         scb->timeout = ips_cmd_timeout;
1993
1994         scb->data_len = ha->flash_datasize;
1995         scb->data_busaddr =
1996             pci_map_single(ha->pcidev, ha->flash_data, scb->data_len,
1997                            IPS_DMA_DIR(scb));
1998         scb->flags |= IPS_SCB_MAP_SINGLE;
1999         scb->cmd.flashfw.command_id = IPS_COMMAND_ID(ha, scb);
2000         scb->cmd.flashfw.buffer_addr = cpu_to_le32(scb->data_busaddr);
2001         if (pt->TimeOut)
2002                 scb->timeout = pt->TimeOut;
2003         scb->scsi_cmd->result = DID_OK << 16;
2004         return IPS_SUCCESS;
2005 }
2006
2007 /****************************************************************************/
2008 /* Routine Name: ips_free_flash_copperhead                                  */
2009 /* Routine Description:                                                     */
2010 /*   release the memory resources used to hold the flash image              */
2011 /****************************************************************************/
2012 static void
2013 ips_free_flash_copperhead(ips_ha_t * ha)
2014 {
2015         if (ha->flash_data == ips_FlashData)
2016                 test_and_clear_bit(0, &ips_FlashDataInUse);
2017         else if (ha->flash_data)
2018                 pci_free_consistent(ha->pcidev, ha->flash_len, ha->flash_data,
2019                                     ha->flash_busaddr);
2020         ha->flash_data = NULL;
2021 }
2022
2023 /****************************************************************************/
2024 /*                                                                          */
2025 /* Routine Name: ips_usrcmd                                                 */
2026 /*                                                                          */
2027 /* Routine Description:                                                     */
2028 /*                                                                          */
2029 /*   Process a user command and make it ready to send                       */
2030 /*                                                                          */
2031 /****************************************************************************/
2032 static int
2033 ips_usrcmd(ips_ha_t * ha, ips_passthru_t * pt, ips_scb_t * scb)
2034 {
2035         IPS_SG_LIST sg_list;
2036         uint32_t cmd_busaddr;
2037
2038         METHOD_TRACE("ips_usrcmd", 1);
2039
2040         if ((!scb) || (!pt) || (!ha))
2041                 return (0);
2042
2043         /* Save the S/G list pointer so it doesn't get clobbered */
2044         sg_list.list = scb->sg_list.list;
2045         cmd_busaddr = scb->scb_busaddr;
2046         /* copy in the CP */
2047         memcpy(&scb->cmd, &pt->CoppCP.cmd, sizeof (IPS_IOCTL_CMD));
2048         memcpy(&scb->dcdb, &pt->CoppCP.dcdb, sizeof (IPS_DCDB_TABLE));
2049
2050         /* FIX stuff that might be wrong */
2051         scb->sg_list.list = sg_list.list;
2052         scb->scb_busaddr = cmd_busaddr;
2053         scb->bus = scb->scsi_cmd->device->channel;
2054         scb->target_id = scb->scsi_cmd->device->id;
2055         scb->lun = scb->scsi_cmd->device->lun;
2056         scb->sg_len = 0;
2057         scb->data_len = 0;
2058         scb->flags = 0;
2059         scb->op_code = 0;
2060         scb->callback = ipsintr_done;
2061         scb->timeout = ips_cmd_timeout;
2062         scb->cmd.basic_io.command_id = IPS_COMMAND_ID(ha, scb);
2063
2064         /* we don't support DCDB/READ/WRITE Scatter Gather */
2065         if ((scb->cmd.basic_io.op_code == IPS_CMD_READ_SG) ||
2066             (scb->cmd.basic_io.op_code == IPS_CMD_WRITE_SG) ||
2067             (scb->cmd.basic_io.op_code == IPS_CMD_DCDB_SG))
2068                 return (0);
2069
2070         if (pt->CmdBSize) {
2071                 scb->data_len = pt->CmdBSize;
2072                 scb->data_busaddr = ha->ioctl_busaddr + sizeof (ips_passthru_t);
2073         } else {
2074                 scb->data_busaddr = 0L;
2075         }
2076
2077         if (scb->cmd.dcdb.op_code == IPS_CMD_DCDB)
2078                 scb->cmd.dcdb.dcdb_address = cpu_to_le32(scb->scb_busaddr +
2079                                                          (unsigned long) &scb->
2080                                                          dcdb -
2081                                                          (unsigned long) scb);
2082
2083         if (pt->CmdBSize) {
2084                 if (scb->cmd.dcdb.op_code == IPS_CMD_DCDB)
2085                         scb->dcdb.buffer_pointer =
2086                             cpu_to_le32(scb->data_busaddr);
2087                 else
2088                         scb->cmd.basic_io.sg_addr =
2089                             cpu_to_le32(scb->data_busaddr);
2090         }
2091
2092         /* set timeouts */
2093         if (pt->TimeOut) {
2094                 scb->timeout = pt->TimeOut;
2095
2096                 if (pt->TimeOut <= 10)
2097                         scb->dcdb.cmd_attribute |= IPS_TIMEOUT10;
2098                 else if (pt->TimeOut <= 60)
2099                         scb->dcdb.cmd_attribute |= IPS_TIMEOUT60;
2100                 else
2101                         scb->dcdb.cmd_attribute |= IPS_TIMEOUT20M;
2102         }
2103
2104         /* assume success */
2105         scb->scsi_cmd->result = DID_OK << 16;
2106
2107         /* success */
2108         return (1);
2109 }
2110
2111 /****************************************************************************/
2112 /*                                                                          */
2113 /* Routine Name: ips_cleanup_passthru                                       */
2114 /*                                                                          */
2115 /* Routine Description:                                                     */
2116 /*                                                                          */
2117 /*   Cleanup after a passthru command                                       */
2118 /*                                                                          */
2119 /****************************************************************************/
2120 static void
2121 ips_cleanup_passthru(ips_ha_t * ha, ips_scb_t * scb)
2122 {
2123         ips_passthru_t *pt;
2124
2125         METHOD_TRACE("ips_cleanup_passthru", 1);
2126
2127         if ((!scb) || (!scb->scsi_cmd) || (!scb->scsi_cmd->request_buffer)) {
2128                 DEBUG_VAR(1, "(%s%d) couldn't cleanup after passthru",
2129                           ips_name, ha->host_num);
2130
2131                 return;
2132         }
2133         pt = (ips_passthru_t *) ha->ioctl_data;
2134
2135         /* Copy data back to the user */
2136         if (scb->cmd.dcdb.op_code == IPS_CMD_DCDB)      /* Copy DCDB Back to Caller's Area */
2137                 memcpy(&pt->CoppCP.dcdb, &scb->dcdb, sizeof (IPS_DCDB_TABLE));
2138
2139         pt->BasicStatus = scb->basic_status;
2140         pt->ExtendedStatus = scb->extended_status;
2141         pt->AdapterType = ha->ad_type;
2142
2143         if (ha->device_id == IPS_DEVICEID_COPPERHEAD &&
2144             (scb->cmd.flashfw.op_code == IPS_CMD_DOWNLOAD ||
2145              scb->cmd.flashfw.op_code == IPS_CMD_RW_BIOSFW))
2146                 ips_free_flash_copperhead(ha);
2147
2148         ips_scmd_buf_write(scb->scsi_cmd, ha->ioctl_data, ha->ioctl_datasize);
2149 }
2150
2151 /****************************************************************************/
2152 /*                                                                          */
2153 /* Routine Name: ips_host_info                                              */
2154 /*                                                                          */
2155 /* Routine Description:                                                     */
2156 /*                                                                          */
2157 /*   The passthru interface for the driver                                  */
2158 /*                                                                          */
2159 /****************************************************************************/
2160 static int
2161 ips_host_info(ips_ha_t * ha, char *ptr, off_t offset, int len)
2162 {
2163         IPS_INFOSTR info;
2164
2165         METHOD_TRACE("ips_host_info", 1);
2166
2167         info.buffer = ptr;
2168         info.length = len;
2169         info.offset = offset;
2170         info.pos = 0;
2171         info.localpos = 0;
2172
2173         copy_info(&info, "\nIBM ServeRAID General Information:\n\n");
2174
2175         if ((le32_to_cpu(ha->nvram->signature) == IPS_NVRAM_P5_SIG) &&
2176             (le16_to_cpu(ha->nvram->adapter_type) != 0))
2177                 copy_info(&info, "\tController Type                   : %s\n",
2178                           ips_adapter_name[ha->ad_type - 1]);
2179         else
2180                 copy_info(&info,
2181                           "\tController Type                   : Unknown\n");
2182
2183         if (ha->io_addr)
2184                 copy_info(&info,
2185                           "\tIO region                         : 0x%lx (%d bytes)\n",
2186                           ha->io_addr, ha->io_len);
2187
2188         if (ha->mem_addr) {
2189                 copy_info(&info,
2190                           "\tMemory region                     : 0x%lx (%d bytes)\n",
2191                           ha->mem_addr, ha->mem_len);
2192                 copy_info(&info,
2193                           "\tShared memory address             : 0x%lx\n",
2194                           ha->mem_ptr);
2195         }
2196
2197         copy_info(&info, "\tIRQ number                        : %d\n", ha->irq);
2198
2199     /* For the Next 3 lines Check for Binary 0 at the end and don't include it if it's there. */
2200     /* That keeps everything happy for "text" operations on the proc file.                    */
2201
2202         if (le32_to_cpu(ha->nvram->signature) == IPS_NVRAM_P5_SIG) {
2203         if (ha->nvram->bios_low[3] == 0) { 
2204             copy_info(&info,
2205                                   "\tBIOS Version                      : %c%c%c%c%c%c%c\n",
2206                                   ha->nvram->bios_high[0], ha->nvram->bios_high[1],
2207                                   ha->nvram->bios_high[2], ha->nvram->bios_high[3],
2208                                   ha->nvram->bios_low[0], ha->nvram->bios_low[1],
2209                                   ha->nvram->bios_low[2]);
2210
2211         } else {
2212                     copy_info(&info,
2213                                   "\tBIOS Version                      : %c%c%c%c%c%c%c%c\n",
2214                                   ha->nvram->bios_high[0], ha->nvram->bios_high[1],
2215                                   ha->nvram->bios_high[2], ha->nvram->bios_high[3],
2216                                   ha->nvram->bios_low[0], ha->nvram->bios_low[1],
2217                                   ha->nvram->bios_low[2], ha->nvram->bios_low[3]);
2218         }
2219
2220     }
2221
2222     if (ha->enq->CodeBlkVersion[7] == 0) {
2223         copy_info(&info,
2224                           "\tFirmware Version                  : %c%c%c%c%c%c%c\n",
2225                           ha->enq->CodeBlkVersion[0], ha->enq->CodeBlkVersion[1],
2226                           ha->enq->CodeBlkVersion[2], ha->enq->CodeBlkVersion[3],
2227                           ha->enq->CodeBlkVersion[4], ha->enq->CodeBlkVersion[5],
2228                           ha->enq->CodeBlkVersion[6]);
2229     } else {
2230         copy_info(&info,
2231                           "\tFirmware Version                  : %c%c%c%c%c%c%c%c\n",
2232                           ha->enq->CodeBlkVersion[0], ha->enq->CodeBlkVersion[1],
2233                           ha->enq->CodeBlkVersion[2], ha->enq->CodeBlkVersion[3],
2234                           ha->enq->CodeBlkVersion[4], ha->enq->CodeBlkVersion[5],
2235                           ha->enq->CodeBlkVersion[6], ha->enq->CodeBlkVersion[7]);
2236     }
2237
2238     if (ha->enq->BootBlkVersion[7] == 0) {
2239         copy_info(&info,
2240                           "\tBoot Block Version                : %c%c%c%c%c%c%c\n",
2241                           ha->enq->BootBlkVersion[0], ha->enq->BootBlkVersion[1],
2242                           ha->enq->BootBlkVersion[2], ha->enq->BootBlkVersion[3],
2243                           ha->enq->BootBlkVersion[4], ha->enq->BootBlkVersion[5],
2244                           ha->enq->BootBlkVersion[6]);
2245     } else {
2246         copy_info(&info,
2247                           "\tBoot Block Version                : %c%c%c%c%c%c%c%c\n",
2248                           ha->enq->BootBlkVersion[0], ha->enq->BootBlkVersion[1],
2249                           ha->enq->BootBlkVersion[2], ha->enq->BootBlkVersion[3],
2250                           ha->enq->BootBlkVersion[4], ha->enq->BootBlkVersion[5],
2251                           ha->enq->BootBlkVersion[6], ha->enq->BootBlkVersion[7]);
2252     }
2253
2254         copy_info(&info, "\tDriver Version                    : %s%s\n",
2255                   IPS_VERSION_HIGH, IPS_VERSION_LOW);
2256
2257         copy_info(&info, "\tDriver Build                      : %d\n",
2258                   IPS_BUILD_IDENT);
2259
2260         copy_info(&info, "\tMax Physical Devices              : %d\n",
2261                   ha->enq->ucMaxPhysicalDevices);
2262         copy_info(&info, "\tMax Active Commands               : %d\n",
2263                   ha->max_cmds);
2264         copy_info(&info, "\tCurrent Queued Commands           : %d\n",
2265                   ha->scb_waitlist.count);
2266         copy_info(&info, "\tCurrent Active Commands           : %d\n",
2267                   ha->scb_activelist.count - ha->num_ioctl);
2268         copy_info(&info, "\tCurrent Queued PT Commands        : %d\n",
2269                   ha->copp_waitlist.count);
2270         copy_info(&info, "\tCurrent Active PT Commands        : %d\n",
2271                   ha->num_ioctl);
2272
2273         copy_info(&info, "\n");
2274
2275         return (info.localpos);
2276 }
2277
2278 /****************************************************************************/
2279 /*                                                                          */
2280 /* Routine Name: copy_mem_info                                              */
2281 /*                                                                          */
2282 /* Routine Description:                                                     */
2283 /*                                                                          */
2284 /*   Copy data into an IPS_INFOSTR structure                                */
2285 /*                                                                          */
2286 /****************************************************************************/
2287 static void
2288 copy_mem_info(IPS_INFOSTR * info, char *data, int len)
2289 {
2290         METHOD_TRACE("copy_mem_info", 1);
2291
2292         if (info->pos + len < info->offset) {
2293                 info->pos += len;
2294                 return;
2295         }
2296
2297         if (info->pos < info->offset) {
2298                 data += (info->offset - info->pos);
2299                 len -= (info->offset - info->pos);
2300                 info->pos += (info->offset - info->pos);
2301         }
2302
2303         if (info->localpos + len > info->length)
2304                 len = info->length - info->localpos;
2305
2306         if (len > 0) {
2307                 memcpy(info->buffer + info->localpos, data, len);
2308                 info->pos += len;
2309                 info->localpos += len;
2310         }
2311 }
2312
2313 /****************************************************************************/
2314 /*                                                                          */
2315 /* Routine Name: copy_info                                                  */
2316 /*                                                                          */
2317 /* Routine Description:                                                     */
2318 /*                                                                          */
2319 /*   printf style wrapper for an info structure                             */
2320 /*                                                                          */
2321 /****************************************************************************/
2322 static int
2323 copy_info(IPS_INFOSTR * info, char *fmt, ...)
2324 {
2325         va_list args;
2326         char buf[128];
2327         int len;
2328
2329         METHOD_TRACE("copy_info", 1);
2330
2331         va_start(args, fmt);
2332         len = vsprintf(buf, fmt, args);
2333         va_end(args);
2334
2335         copy_mem_info(info, buf, len);
2336
2337         return (len);
2338 }
2339
2340 /****************************************************************************/
2341 /*                                                                          */
2342 /* Routine Name: ips_identify_controller                                    */
2343 /*                                                                          */
2344 /* Routine Description:                                                     */
2345 /*                                                                          */
2346 /*   Identify this controller                                               */
2347 /*                                                                          */
2348 /****************************************************************************/
2349 static void
2350 ips_identify_controller(ips_ha_t * ha)
2351 {
2352         METHOD_TRACE("ips_identify_controller", 1);
2353
2354         switch (ha->device_id) {
2355         case IPS_DEVICEID_COPPERHEAD:
2356                 if (ha->revision_id <= IPS_REVID_SERVERAID) {
2357                         ha->ad_type = IPS_ADTYPE_SERVERAID;
2358                 } else if (ha->revision_id == IPS_REVID_SERVERAID2) {
2359                         ha->ad_type = IPS_ADTYPE_SERVERAID2;
2360                 } else if (ha->revision_id == IPS_REVID_NAVAJO) {
2361                         ha->ad_type = IPS_ADTYPE_NAVAJO;
2362                 } else if ((ha->revision_id == IPS_REVID_SERVERAID2)
2363                            && (ha->slot_num == 0)) {
2364                         ha->ad_type = IPS_ADTYPE_KIOWA;
2365                 } else if ((ha->revision_id >= IPS_REVID_CLARINETP1) &&
2366                            (ha->revision_id <= IPS_REVID_CLARINETP3)) {
2367                         if (ha->enq->ucMaxPhysicalDevices == 15)
2368                                 ha->ad_type = IPS_ADTYPE_SERVERAID3L;
2369                         else
2370                                 ha->ad_type = IPS_ADTYPE_SERVERAID3;
2371                 } else if ((ha->revision_id >= IPS_REVID_TROMBONE32) &&
2372                            (ha->revision_id <= IPS_REVID_TROMBONE64)) {
2373                         ha->ad_type = IPS_ADTYPE_SERVERAID4H;
2374                 }
2375                 break;
2376
2377         case IPS_DEVICEID_MORPHEUS:
2378                 switch (ha->subdevice_id) {
2379                 case IPS_SUBDEVICEID_4L:
2380                         ha->ad_type = IPS_ADTYPE_SERVERAID4L;
2381                         break;
2382
2383                 case IPS_SUBDEVICEID_4M:
2384                         ha->ad_type = IPS_ADTYPE_SERVERAID4M;
2385                         break;
2386
2387                 case IPS_SUBDEVICEID_4MX:
2388                         ha->ad_type = IPS_ADTYPE_SERVERAID4MX;
2389                         break;
2390
2391                 case IPS_SUBDEVICEID_4LX:
2392                         ha->ad_type = IPS_ADTYPE_SERVERAID4LX;
2393                         break;
2394
2395                 case IPS_SUBDEVICEID_5I2:
2396                         ha->ad_type = IPS_ADTYPE_SERVERAID5I2;
2397                         break;
2398
2399                 case IPS_SUBDEVICEID_5I1:
2400                         ha->ad_type = IPS_ADTYPE_SERVERAID5I1;
2401                         break;
2402                 }
2403
2404                 break;
2405
2406         case IPS_DEVICEID_MARCO:
2407                 switch (ha->subdevice_id) {
2408                 case IPS_SUBDEVICEID_6M:
2409                         ha->ad_type = IPS_ADTYPE_SERVERAID6M;
2410                         break;
2411                 case IPS_SUBDEVICEID_6I:
2412                         ha->ad_type = IPS_ADTYPE_SERVERAID6I;
2413                         break;
2414                 case IPS_SUBDEVICEID_7k:
2415                         ha->ad_type = IPS_ADTYPE_SERVERAID7k;
2416                         break;
2417                 case IPS_SUBDEVICEID_7M:
2418                         ha->ad_type = IPS_ADTYPE_SERVERAID7M;
2419                         break;
2420                 }
2421                 break;
2422         }
2423 }
2424
2425 /****************************************************************************/
2426 /*                                                                          */
2427 /* Routine Name: ips_get_bios_version                                       */
2428 /*                                                                          */
2429 /* Routine Description:                                                     */
2430 /*                                                                          */
2431 /*   Get the BIOS revision number                                           */
2432 /*                                                                          */
2433 /****************************************************************************/
2434 static void
2435 ips_get_bios_version(ips_ha_t * ha, int intr)
2436 {
2437         ips_scb_t *scb;
2438         int ret;
2439         uint8_t major;
2440         uint8_t minor;
2441         uint8_t subminor;
2442         uint8_t *buffer;
2443         char hexDigits[] =
2444             { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
2445      'D', 'E', 'F' };
2446
2447         METHOD_TRACE("ips_get_bios_version", 1);
2448
2449         major = 0;
2450         minor = 0;
2451
2452         strncpy(ha->bios_version, "       ?", 8);
2453
2454         if (ha->device_id == IPS_DEVICEID_COPPERHEAD) {
2455                 if (IPS_USE_MEMIO(ha)) {
2456                         /* Memory Mapped I/O */
2457
2458                         /* test 1st byte */
2459                         writel(0, ha->mem_ptr + IPS_REG_FLAP);
2460                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2461                                 udelay(25);     /* 25 us */
2462
2463                         if (readb(ha->mem_ptr + IPS_REG_FLDP) != 0x55)
2464                                 return;
2465
2466                         writel(1, ha->mem_ptr + IPS_REG_FLAP);
2467                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2468                                 udelay(25);     /* 25 us */
2469
2470                         if (readb(ha->mem_ptr + IPS_REG_FLDP) != 0xAA)
2471                                 return;
2472
2473                         /* Get Major version */
2474                         writel(0x1FF, ha->mem_ptr + IPS_REG_FLAP);
2475                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2476                                 udelay(25);     /* 25 us */
2477
2478                         major = readb(ha->mem_ptr + IPS_REG_FLDP);
2479
2480                         /* Get Minor version */
2481                         writel(0x1FE, ha->mem_ptr + IPS_REG_FLAP);
2482                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2483                                 udelay(25);     /* 25 us */
2484                         minor = readb(ha->mem_ptr + IPS_REG_FLDP);
2485
2486                         /* Get SubMinor version */
2487                         writel(0x1FD, ha->mem_ptr + IPS_REG_FLAP);
2488                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2489                                 udelay(25);     /* 25 us */
2490                         subminor = readb(ha->mem_ptr + IPS_REG_FLDP);
2491
2492                 } else {
2493                         /* Programmed I/O */
2494
2495                         /* test 1st byte */
2496                         outl(0, ha->io_addr + IPS_REG_FLAP);
2497                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2498                                 udelay(25);     /* 25 us */
2499
2500                         if (inb(ha->io_addr + IPS_REG_FLDP) != 0x55)
2501                                 return;
2502
2503                         outl(cpu_to_le32(1), ha->io_addr + IPS_REG_FLAP);
2504                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2505                                 udelay(25);     /* 25 us */
2506
2507                         if (inb(ha->io_addr + IPS_REG_FLDP) != 0xAA)
2508                                 return;
2509
2510                         /* Get Major version */
2511                         outl(cpu_to_le32(0x1FF), ha->io_addr + IPS_REG_FLAP);
2512                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2513                                 udelay(25);     /* 25 us */
2514
2515                         major = inb(ha->io_addr + IPS_REG_FLDP);
2516
2517                         /* Get Minor version */
2518                         outl(cpu_to_le32(0x1FE), ha->io_addr + IPS_REG_FLAP);
2519                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2520                                 udelay(25);     /* 25 us */
2521
2522                         minor = inb(ha->io_addr + IPS_REG_FLDP);
2523
2524                         /* Get SubMinor version */
2525                         outl(cpu_to_le32(0x1FD), ha->io_addr + IPS_REG_FLAP);
2526                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2527                                 udelay(25);     /* 25 us */
2528
2529                         subminor = inb(ha->io_addr + IPS_REG_FLDP);
2530
2531                 }
2532         } else {
2533                 /* Morpheus Family - Send Command to the card */
2534
2535                 buffer = ha->ioctl_data;
2536
2537                 memset(buffer, 0, 0x1000);
2538
2539                 scb = &ha->scbs[ha->max_cmds - 1];
2540
2541                 ips_init_scb(ha, scb);
2542
2543                 scb->timeout = ips_cmd_timeout;
2544                 scb->cdb[0] = IPS_CMD_RW_BIOSFW;
2545
2546                 scb->cmd.flashfw.op_code = IPS_CMD_RW_BIOSFW;
2547                 scb->cmd.flashfw.command_id = IPS_COMMAND_ID(ha, scb);
2548                 scb->cmd.flashfw.type = 1;
2549                 scb->cmd.flashfw.direction = 0;
2550                 scb->cmd.flashfw.count = cpu_to_le32(0x800);
2551                 scb->cmd.flashfw.total_packets = 1;
2552                 scb->cmd.flashfw.packet_num = 0;
2553                 scb->data_len = 0x1000;
2554                 scb->cmd.flashfw.buffer_addr = ha->ioctl_busaddr;
2555
2556                 /* issue the command */
2557                 if (((ret =
2558                       ips_send_wait(ha, scb, ips_cmd_timeout,
2559                                     intr)) == IPS_FAILURE)
2560                     || (ret == IPS_SUCCESS_IMM)
2561                     || ((scb->basic_status & IPS_GSC_STATUS_MASK) > 1)) {
2562                         /* Error occurred */
2563
2564                         return;
2565                 }
2566
2567                 if ((buffer[0xC0] == 0x55) && (buffer[0xC1] == 0xAA)) {
2568                         major = buffer[0x1ff + 0xC0];   /* Offset 0x1ff after the header (0xc0) */
2569                         minor = buffer[0x1fe + 0xC0];   /* Offset 0x1fe after the header (0xc0) */
2570                         subminor = buffer[0x1fd + 0xC0];        /* Offset 0x1fd after the header (0xc0) */
2571                 } else {
2572                         return;
2573                 }
2574         }
2575
2576         ha->bios_version[0] = hexDigits[(major & 0xF0) >> 4];
2577         ha->bios_version[1] = '.';
2578         ha->bios_version[2] = hexDigits[major & 0x0F];
2579         ha->bios_version[3] = hexDigits[subminor];
2580         ha->bios_version[4] = '.';
2581         ha->bios_version[5] = hexDigits[(minor & 0xF0) >> 4];
2582         ha->bios_version[6] = hexDigits[minor & 0x0F];
2583         ha->bios_version[7] = 0;
2584 }
2585
2586 /****************************************************************************/
2587 /*                                                                          */
2588 /* Routine Name: ips_hainit                                                 */
2589 /*                                                                          */
2590 /* Routine Description:                                                     */
2591 /*                                                                          */
2592 /*   Initialize the controller                                              */
2593 /*                                                                          */
2594 /* NOTE: Assumes to be called from with a lock                              */
2595 /*                                                                          */
2596 /****************************************************************************/
2597 static int
2598 ips_hainit(ips_ha_t * ha)
2599 {
2600         int i;
2601         struct timeval tv;
2602
2603         METHOD_TRACE("ips_hainit", 1);
2604
2605         if (!ha)
2606                 return (0);
2607
2608         if (ha->func.statinit)
2609                 (*ha->func.statinit) (ha);
2610
2611         if (ha->func.enableint)
2612                 (*ha->func.enableint) (ha);
2613
2614         /* Send FFDC */
2615         ha->reset_count = 1;
2616         do_gettimeofday(&tv);
2617         ha->last_ffdc = tv.tv_sec;
2618         ips_ffdc_reset(ha, IPS_INTR_IORL);
2619
2620         if (!ips_read_config(ha, IPS_INTR_IORL)) {
2621                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
2622                            "unable to read config from controller.\n");
2623
2624                 return (0);
2625         }
2626         /* end if */
2627         if (!ips_read_adapter_status(ha, IPS_INTR_IORL)) {
2628                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
2629                            "unable to read controller status.\n");
2630
2631                 return (0);
2632         }
2633
2634         /* Identify this controller */
2635         ips_identify_controller(ha);
2636
2637         if (!ips_read_subsystem_parameters(ha, IPS_INTR_IORL)) {
2638                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
2639                            "unable to read subsystem parameters.\n");
2640
2641                 return (0);
2642         }
2643
2644         /* write nvram user page 5 */
2645         if (!ips_write_driver_status(ha, IPS_INTR_IORL)) {
2646                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
2647                            "unable to write driver info to controller.\n");
2648
2649                 return (0);
2650         }
2651
2652         /* If there are Logical Drives and a Reset Occurred, then an EraseStripeLock is Needed */
2653         if ((ha->conf->ucLogDriveCount > 0) && (ha->requires_esl == 1))
2654                 ips_clear_adapter(ha, IPS_INTR_IORL);
2655
2656         /* set limits on SID, LUN, BUS */
2657         ha->ntargets = IPS_MAX_TARGETS + 1;
2658         ha->nlun = 1;
2659         ha->nbus = (ha->enq->ucMaxPhysicalDevices / IPS_MAX_TARGETS) + 1;
2660
2661         switch (ha->conf->logical_drive[0].ucStripeSize) {
2662         case 4:
2663                 ha->max_xfer = 0x10000;
2664                 break;
2665
2666         case 5:
2667                 ha->max_xfer = 0x20000;
2668                 break;
2669
2670         case 6:
2671                 ha->max_xfer = 0x40000;
2672                 break;
2673
2674         case 7:
2675         default:
2676                 ha->max_xfer = 0x80000;
2677                 break;
2678         }
2679
2680         /* setup max concurrent commands */
2681         if (le32_to_cpu(ha->subsys->param[4]) & 0x1) {
2682                 /* Use the new method */
2683                 ha->max_cmds = ha->enq->ucConcurrentCmdCount;
2684         } else {
2685                 /* use the old method */
2686                 switch (ha->conf->logical_drive[0].ucStripeSize) {
2687                 case 4:
2688                         ha->max_cmds = 32;
2689                         break;
2690
2691                 case 5:
2692                         ha->max_cmds = 16;
2693                         break;
2694
2695                 case 6:
2696                         ha->max_cmds = 8;
2697                         break;
2698
2699                 case 7:
2700                 default:
2701                         ha->max_cmds = 4;
2702                         break;
2703                 }
2704         }
2705
2706         /* Limit the Active Commands on a Lite Adapter */
2707         if ((ha->ad_type == IPS_ADTYPE_SERVERAID3L) ||
2708             (ha->ad_type == IPS_ADTYPE_SERVERAID4L) ||
2709             (ha->ad_type == IPS_ADTYPE_SERVERAID4LX)) {
2710                 if ((ha->max_cmds > MaxLiteCmds) && (MaxLiteCmds))
2711                         ha->max_cmds = MaxLiteCmds;
2712         }
2713
2714         /* set controller IDs */
2715         ha->ha_id[0] = IPS_ADAPTER_ID;
2716         for (i = 1; i < ha->nbus; i++) {
2717                 ha->ha_id[i] = ha->conf->init_id[i - 1] & 0x1f;
2718                 ha->dcdb_active[i - 1] = 0;
2719         }
2720
2721         return (1);
2722 }
2723
2724 /****************************************************************************/
2725 /*                                                                          */
2726 /* Routine Name: ips_next                                                   */
2727 /*                                                                          */
2728 /* Routine Description:                                                     */
2729 /*                                                                          */
2730 /*   Take the next command off the queue and send it to the controller      */
2731 /*                                                                          */
2732 /****************************************************************************/
2733 static void
2734 ips_next(ips_ha_t * ha, int intr)
2735 {
2736         ips_scb_t *scb;
2737         Scsi_Cmnd *SC;
2738         Scsi_Cmnd *p;
2739         Scsi_Cmnd *q;
2740         ips_copp_wait_item_t *item;
2741         int ret;
2742         unsigned long cpu_flags = 0;
2743         struct Scsi_Host *host;
2744         METHOD_TRACE("ips_next", 1);
2745
2746         if (!ha)
2747                 return;
2748         host = ips_sh[ha->host_num];
2749         /*
2750          * Block access to the queue function so
2751          * this command won't time out
2752          */
2753         if (intr == IPS_INTR_ON)
2754                 IPS_LOCK_SAVE(host->host_lock, cpu_flags);
2755
2756         if ((ha->subsys->param[3] & 0x300000)
2757             && (ha->scb_activelist.count == 0)) {
2758                 struct timeval tv;
2759
2760                 do_gettimeofday(&tv);
2761
2762                 if (tv.tv_sec - ha->last_ffdc > IPS_SECS_8HOURS) {
2763                         ha->last_ffdc = tv.tv_sec;
2764                         ips_ffdc_time(ha);
2765                 }
2766         }
2767
2768         /*
2769          * Send passthru commands
2770          * These have priority over normal I/O
2771          * but shouldn't affect performance too much
2772          * since we limit the number that can be active
2773          * on the card at any one time
2774          */
2775         while ((ha->num_ioctl < IPS_MAX_IOCTL) &&
2776                (ha->copp_waitlist.head) && (scb = ips_getscb(ha))) {
2777
2778                 item = ips_removeq_copp_head(&ha->copp_waitlist);
2779                 ha->num_ioctl++;
2780                 if (intr == IPS_INTR_ON)
2781                         IPS_UNLOCK_RESTORE(host->host_lock, cpu_flags);
2782                 scb->scsi_cmd = item->scsi_cmd;
2783                 kfree(item);
2784
2785                 ret = ips_make_passthru(ha, scb->scsi_cmd, scb, intr);
2786
2787                 if (intr == IPS_INTR_ON)
2788                         IPS_LOCK_SAVE(host->host_lock, cpu_flags);
2789                 switch (ret) {
2790                 case IPS_FAILURE:
2791                         if (scb->scsi_cmd) {
2792                                 scb->scsi_cmd->result = DID_ERROR << 16;
2793                                 scb->scsi_cmd->scsi_done(scb->scsi_cmd);
2794                         }
2795
2796                         ips_freescb(ha, scb);
2797                         break;
2798                 case IPS_SUCCESS_IMM:
2799                         if (scb->scsi_cmd) {
2800                                 scb->scsi_cmd->result = DID_OK << 16;
2801                                 scb->scsi_cmd->scsi_done(scb->scsi_cmd);
2802                         }
2803
2804                         ips_freescb(ha, scb);
2805                         break;
2806                 default:
2807                         break;
2808                 }               /* end case */
2809
2810                 if (ret != IPS_SUCCESS) {
2811                         ha->num_ioctl--;
2812                         continue;
2813                 }
2814
2815                 ret = ips_send_cmd(ha, scb);
2816
2817                 if (ret == IPS_SUCCESS)
2818                         ips_putq_scb_head(&ha->scb_activelist, scb);
2819                 else
2820                         ha->num_ioctl--;
2821
2822                 switch (ret) {
2823                 case IPS_FAILURE:
2824                         if (scb->scsi_cmd) {
2825                                 scb->scsi_cmd->result = DID_ERROR << 16;
2826                         }
2827
2828                         ips_freescb(ha, scb);
2829                         break;
2830                 case IPS_SUCCESS_IMM:
2831                         ips_freescb(ha, scb);
2832                         break;
2833                 default:
2834                         break;
2835                 }               /* end case */
2836
2837         }
2838
2839         /*
2840          * Send "Normal" I/O commands
2841          */
2842
2843         p = ha->scb_waitlist.head;
2844         while ((p) && (scb = ips_getscb(ha))) {
2845                 if ((scmd_channel(p) > 0)
2846                     && (ha->
2847                         dcdb_active[scmd_channel(p) -
2848                                     1] & (1 << scmd_id(p)))) {
2849                         ips_freescb(ha, scb);
2850                         p = (Scsi_Cmnd *) p->host_scribble;
2851                         continue;
2852                 }
2853
2854                 q = p;
2855                 SC = ips_removeq_wait(&ha->scb_waitlist, q);
2856
2857                 if (intr == IPS_INTR_ON)
2858                         IPS_UNLOCK_RESTORE(host->host_lock, cpu_flags); /* Unlock HA after command is taken off queue */
2859
2860                 SC->result = DID_OK;
2861                 SC->host_scribble = NULL;
2862
2863                 memset(SC->sense_buffer, 0, sizeof (SC->sense_buffer));
2864
2865                 scb->target_id = SC->device->id;
2866                 scb->lun = SC->device->lun;
2867                 scb->bus = SC->device->channel;
2868                 scb->scsi_cmd = SC;
2869                 scb->breakup = 0;
2870                 scb->data_len = 0;
2871                 scb->callback = ipsintr_done;
2872                 scb->timeout = ips_cmd_timeout;
2873                 memset(&scb->cmd, 0, 16);
2874
2875                 /* copy in the CDB */
2876                 memcpy(scb->cdb, SC->cmnd, SC->cmd_len);
2877
2878                 /* Now handle the data buffer */
2879                 if (SC->use_sg) {
2880                         struct scatterlist *sg;
2881                         int i;
2882
2883                         sg = SC->request_buffer;
2884                         scb->sg_count = pci_map_sg(ha->pcidev, sg, SC->use_sg,
2885                                                    SC->sc_data_direction);
2886                         scb->flags |= IPS_SCB_MAP_SG;
2887                         for (i = 0; i < scb->sg_count; i++) {
2888                                 if (ips_fill_scb_sg_single
2889                                     (ha, sg_dma_address(&sg[i]), scb, i,
2890                                      sg_dma_len(&sg[i])) < 0)
2891                                         break;
2892                         }
2893                         scb->dcdb.transfer_length = scb->data_len;
2894                 } else {
2895                         if (SC->request_bufflen) {
2896                                 scb->data_busaddr =
2897                                     pci_map_single(ha->pcidev,
2898                                                    SC->request_buffer,
2899                                                    SC->request_bufflen,
2900                                                    SC->sc_data_direction);
2901                                 scb->flags |= IPS_SCB_MAP_SINGLE;
2902                                 ips_fill_scb_sg_single(ha, scb->data_busaddr,
2903                                                        scb, 0,
2904                                                        SC->request_bufflen);
2905                                 scb->dcdb.transfer_length = scb->data_len;
2906                         } else {
2907                                 scb->data_busaddr = 0L;
2908                                 scb->sg_len = 0;
2909                                 scb->data_len = 0;
2910                                 scb->dcdb.transfer_length = 0;
2911                         }
2912
2913                 }
2914
2915                 scb->dcdb.cmd_attribute =
2916                     ips_command_direction[scb->scsi_cmd->cmnd[0]];
2917
2918         /* Allow a WRITE BUFFER Command to Have no Data */
2919         /* This is Used by Tape Flash Utilites          */
2920         if ((scb->scsi_cmd->cmnd[0] == WRITE_BUFFER) && (scb->data_len == 0)) 
2921             scb->dcdb.cmd_attribute = 0;                  
2922
2923                 if (!(scb->dcdb.cmd_attribute & 0x3))
2924                         scb->dcdb.transfer_length = 0;
2925
2926                 if (scb->data_len >= IPS_MAX_XFER) {
2927                         scb->dcdb.cmd_attribute |= IPS_TRANSFER64K;
2928                         scb->dcdb.transfer_length = 0;
2929                 }
2930                 if (intr == IPS_INTR_ON)
2931                         IPS_LOCK_SAVE(host->host_lock, cpu_flags);
2932
2933                 ret = ips_send_cmd(ha, scb);
2934
2935                 switch (ret) {
2936                 case IPS_SUCCESS:
2937                         ips_putq_scb_head(&ha->scb_activelist, scb);
2938                         break;
2939                 case IPS_FAILURE:
2940                         if (scb->scsi_cmd) {
2941                                 scb->scsi_cmd->result = DID_ERROR << 16;
2942                                 scb->scsi_cmd->scsi_done(scb->scsi_cmd);
2943                         }
2944
2945                         if (scb->bus)
2946                                 ha->dcdb_active[scb->bus - 1] &=
2947                                     ~(1 << scb->target_id);
2948
2949                         ips_freescb(ha, scb);
2950                         break;
2951                 case IPS_SUCCESS_IMM:
2952                         if (scb->scsi_cmd)
2953                                 scb->scsi_cmd->scsi_done(scb->scsi_cmd);
2954
2955                         if (scb->bus)
2956                                 ha->dcdb_active[scb->bus - 1] &=
2957                                     ~(1 << scb->target_id);
2958
2959                         ips_freescb(ha, scb);
2960                         break;
2961                 default:
2962                         break;
2963                 }               /* end case */
2964
2965                 p = (Scsi_Cmnd *) p->host_scribble;
2966
2967         }                       /* end while */
2968
2969         if (intr == IPS_INTR_ON)
2970                 IPS_UNLOCK_RESTORE(host->host_lock, cpu_flags);
2971 }
2972
2973 /****************************************************************************/
2974 /*                                                                          */
2975 /* Routine Name: ips_putq_scb_head                                          */
2976 /*                                                                          */
2977 /* Routine Description:                                                     */
2978 /*                                                                          */
2979 /*   Add an item to the head of the queue                                   */
2980 /*                                                                          */
2981 /* ASSUMED to be called from within the HA lock                             */
2982 /*                                                                          */
2983 /****************************************************************************/
2984 static void
2985 ips_putq_scb_head(ips_scb_queue_t * queue, ips_scb_t * item)
2986 {
2987         METHOD_TRACE("ips_putq_scb_head", 1);
2988
2989         if (!item)
2990                 return;
2991
2992         item->q_next = queue->head;
2993         queue->head = item;
2994
2995         if (!queue->tail)
2996                 queue->tail = item;
2997
2998         queue->count++;
2999 }
3000
3001 /****************************************************************************/
3002 /*                                                                          */
3003 /* Routine Name: ips_removeq_scb_head                                       */
3004 /*                                                                          */
3005 /* Routine Description:                                                     */
3006 /*                                                                          */
3007 /*   Remove the head of the queue                                           */
3008 /*                                                                          */
3009 /* ASSUMED to be called from within the HA lock                             */
3010 /*                                                                          */
3011 /****************************************************************************/
3012 static ips_scb_t *
3013 ips_removeq_scb_head(ips_scb_queue_t * queue)
3014 {
3015         ips_scb_t *item;
3016
3017         METHOD_TRACE("ips_removeq_scb_head", 1);