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