ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / scsi / ips.c
1 /*****************************************************************************/
2 /* ips.c -- driver for the Adaptec / IBM ServeRAID controller                */
3 /*                                                                           */
4 /* Written By: Keith Mitchell, IBM Corporation                               */
5 /*             Jack Hammer, Adaptec, Inc.                                    */
6 /*             David Jeffery, Adaptec, Inc.                                  */
7 /*                                                                           */
8 /* Copyright (C) 2000 IBM Corporation                                        */
9 /* Copyright (C) 2002,2003 Adaptec, Inc.                                     */
10 /*                                                                           */
11 /* This program is free software; you can redistribute it and/or modify      */
12 /* it under the terms of the GNU General Public License as published by      */
13 /* the Free Software Foundation; either version 2 of the License, or         */
14 /* (at your option) any later version.                                       */
15 /*                                                                           */
16 /* This program is distributed in the hope that it will be useful,           */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
19 /* GNU General Public License for more details.                              */
20 /*                                                                           */
21 /* NO WARRANTY                                                               */
22 /* THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR        */
23 /* CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT      */
24 /* LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,      */
25 /* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is    */
26 /* solely responsible for determining the appropriateness of using and       */
27 /* distributing the Program and assumes all risks associated with its        */
28 /* exercise of rights under this Agreement, including but not limited to     */
29 /* the risks and costs of program errors, damage to or loss of data,         */
30 /* programs or equipment, and unavailability or interruption of operations.  */
31 /*                                                                           */
32 /* DISCLAIMER OF LIABILITY                                                   */
33 /* NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY   */
34 /* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL        */
35 /* DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND   */
36 /* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR     */
37 /* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE    */
38 /* USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED  */
39 /* HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES             */
40 /*                                                                           */
41 /* You should have received a copy of the GNU General Public License         */
42 /* along with this program; if not, write to the Free Software               */
43 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
44 /*                                                                           */
45 /* Bugs/Comments/Suggestions about this driver should be mailed to:          */
46 /*      ipslinux@adaptec.com                                                 */
47 /*                                                                           */
48 /* For system support issues, contact your local IBM Customer support.       */
49 /* Directions to find IBM Customer Support for each country can be found at: */
50 /*      http://www.ibm.com/planetwide/                                       */
51 /*                                                                           */
52 /*****************************************************************************/
53
54 /*****************************************************************************/
55 /* Change Log                                                                */
56 /*                                                                           */
57 /* 0.99.02  - Breakup commands that are bigger than 8 * the stripe size      */
58 /* 0.99.03  - Make interrupt routine handle all completed request on the     */
59 /*            adapter not just the first one                                 */
60 /*          - Make sure passthru commands get woken up if we run out of      */
61 /*            SCBs                                                           */
62 /*          - Send all of the commands on the queue at once rather than      */
63 /*            one at a time since the card will support it.                  */
64 /* 0.99.04  - Fix race condition in the passthru mechanism -- this required  */
65 /*            the interface to the utilities to change                       */
66 /*          - Fix error recovery code                                        */
67 /* 0.99.05  - Fix an oops when we get certain passthru commands              */
68 /* 1.00.00  - Initial Public Release                                         */
69 /*            Functionally equivalent to 0.99.05                             */
70 /* 3.60.00  - Bump max commands to 128 for use with firmware 3.60            */
71 /*          - Change version to 3.60 to coincide with release numbering.     */
72 /* 3.60.01  - Remove bogus error check in passthru routine                   */
73 /* 3.60.02  - Make DCDB direction based on lookup table                      */
74 /*          - Only allow one DCDB command to a SCSI ID at a time             */
75 /* 4.00.00  - Add support for ServeRAID 4                                    */
76 /* 4.00.01  - Add support for First Failure Data Capture                     */
77 /* 4.00.02  - Fix problem with PT DCDB with no buffer                        */
78 /* 4.00.03  - Add alternative passthru interface                             */
79 /*          - Add ability to flash BIOS                                      */
80 /* 4.00.04  - Rename structures/constants to be prefixed with IPS_           */
81 /* 4.00.05  - Remove wish_block from init routine                            */
82 /*          - Use linux/spinlock.h instead of asm/spinlock.h for kernels     */
83 /*            2.3.18 and later                                               */
84 /*          - Sync with other changes from the 2.3 kernels                   */
85 /* 4.00.06  - Fix timeout with initial FFDC command                          */
86 /* 4.00.06a - Port to 2.4 (trivial) -- Christoph Hellwig <hch@infradead.org> */
87 /* 4.10.00  - Add support for ServeRAID 4M/4L                                */
88 /* 4.10.13  - Fix for dynamic unload and proc file system                    */
89 /* 4.20.03  - Rename version to coincide with new release schedules          */
90 /*            Performance fixes                                              */
91 /*            Fix truncation of /proc files with cat                         */
92 /*            Merge in changes through kernel 2.4.0test1ac21                 */
93 /* 4.20.13  - Fix some failure cases / reset code                            */
94 /*          - Hook into the reboot_notifier to flush the controller cache    */
95 /* 4.50.01  - Fix problem when there is a hole in logical drive numbering    */
96 /* 4.70.09  - Use a Common ( Large Buffer ) for Flashing from the JCRM CD    */
97 /*          - Add IPSSEND Flash Support                                      */
98 /*          - Set Sense Data for Unknown SCSI Command                        */
99 /*          - Use Slot Number from NVRAM Page 5                              */
100 /*          - Restore caller's DCDB Structure                                */
101 /* 4.70.12  - Corrective actions for bad controller ( during initialization )*/
102 /* 4.70.13  - Don't Send CDB's if we already know the device is not present  */
103 /*          - Don't release HA Lock in ips_next() until SC taken off queue   */
104 /*          - Unregister SCSI device in ips_release()                        */
105 /* 4.70.15  - Fix Breakup for very large ( non-SG ) requests in ips_done()   */
106 /* 4.71.00  - Change all memory allocations to not use GFP_DMA flag          */
107 /*            Code Clean-Up for 2.4.x kernel                                 */
108 /* 4.72.00  - Allow for a Scatter-Gather Element to exceed MAX_XFER Size     */
109 /* 4.72.01  - I/O Mapped Memory release ( so "insmod ips" does not Fail )    */
110 /*          - Don't Issue Internal FFDC Command if there are Active Commands */
111 /*          - Close Window for getting too many IOCTL's active               */
112 /* 4.80.00  - Make ia64 Safe                                                 */
113 /* 4.80.04  - Eliminate calls to strtok() if 2.4.x or greater                */
114 /*          - Adjustments to Device Queue Depth                              */
115 /* 4.80.14  - Take all semaphores off stack                                  */
116 /*          - Clean Up New_IOCTL path                                        */
117 /* 4.80.20  - Set max_sectors in Scsi_Host structure ( if >= 2.4.7 kernel )  */
118 /*          - 5 second delay needed after resetting an i960 adapter          */
119 /* 4.80.26  - Clean up potential code problems ( Arjan's recommendations )   */
120 /* 4.90.01  - Version Matching for FirmWare, BIOS, and Driver                */
121 /* 4.90.05  - Use New PCI Architecture to facilitate Hot Plug Development    */
122 /* 4.90.08  - Increase Delays in Flashing ( Trombone Only - 4H )             */
123 /* 4.90.08  - Data Corruption if First Scatter Gather Element is > 64K       */
124 /* 4.90.11  - Don't actually RESET unless it's physically required           */
125 /*          - Remove unused compile options                                  */
126 /* 5.00.01  - Sarasota ( 5i ) adapters must always be scanned first          */
127 /*          - Get rid on IOCTL_NEW_COMMAND code                              */
128 /*          - Add Extended DCDB Commands for Tape Support in 5I              */
129 /* 5.10.12  - use pci_dma interfaces, update for 2.5 kernel changes          */
130 /* 5.10.15  - remove unused code (sem, macros, etc.)                         */
131 /* 5.30.00  - use __devexit_p()                                              */
132 /* 6.00.00  - Add 6x Adapters and Battery Flash                              */
133 /* 6.10.00  - Remove 1G Addressing Limitations                               */
134 /* 6.11.xx  - Get VersionInfo buffer off the stack !              DDTS 60401 */
135 /* 6.11.xx  - Make Logical Drive Info structure safe for DMA      DDTS 60639 */
136 /*****************************************************************************/
137
138 /*
139  * Conditional Compilation directives for this driver:
140  *
141  * IPS_DEBUG            - Turn on debugging info
142  *
143  * Parameters:
144  *
145  * debug:<number>       - Set debug level to <number>
146  *                        NOTE: only works when IPS_DEBUG compile directive is used.
147  *       1              - Normal debug messages
148  *       2              - Verbose debug messages
149  *       11             - Method trace (non interrupt)
150  *       12             - Method trace (includes interrupt)
151  *
152  * noi2o                - Don't use I2O Queues (ServeRAID 4 only)
153  * nommap               - Don't use memory mapped I/O
154  * ioctlsize            - Initial size of the IOCTL buffer
155  */
156
157 #include <asm/io.h>
158 #include <asm/byteorder.h>
159 #include <asm/page.h>
160 #include <linux/stddef.h>
161 #include <linux/version.h>
162 #include <linux/string.h>
163 #include <linux/errno.h>
164 #include <linux/kernel.h>
165 #include <linux/ioport.h>
166 #include <linux/slab.h>
167 #include <linux/delay.h>
168 #include <linux/pci.h>
169 #include <linux/proc_fs.h>
170 #include <linux/reboot.h>
171 #include <linux/interrupt.h>
172
173 #include <linux/blkdev.h>
174 #include <linux/types.h>
175
176 #include <scsi/sg.h>
177
178 #include "scsi.h"
179 #include "hosts.h"
180 #include "ips.h"
181
182 #include <linux/module.h>
183
184 #include <linux/stat.h>
185 #include <linux/config.h>
186
187 #include <linux/spinlock.h>
188 #include <linux/init.h>
189
190 #include <linux/smp.h>
191
192 #ifdef MODULE
193 static char *ips = NULL;
194 MODULE_PARM(ips, "s");
195 #endif
196
197 /*
198  * DRIVER_VER
199  */
200 #define IPS_VERSION_HIGH        "7.00"
201 #define IPS_VERSION_LOW         ".15 "
202
203 #if !defined(__i386__) && !defined(__ia64__) && !defined(__x86_64__)
204 #warning "This driver has only been tested on the x86/ia64/x86_64 platforms"
205 #endif
206
207 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0)
208 #include <linux/blk.h>
209 #include "sd.h"
210 #define IPS_SG_ADDRESS(sg)       ((sg)->address)
211 #define IPS_LOCK_SAVE(lock,flags) spin_lock_irqsave(&io_request_lock,flags)
212 #define IPS_UNLOCK_RESTORE(lock,flags) spin_unlock_irqrestore(&io_request_lock,flags)
213 #ifndef __devexit_p
214 #define __devexit_p(x) x
215 #endif
216 #else
217 #define IPS_SG_ADDRESS(sg)      (page_address((sg)->page) ? \
218                                      page_address((sg)->page)+(sg)->offset : 0)
219 #define IPS_LOCK_SAVE(lock,flags) do{spin_lock(lock);(void)flags;}while(0)
220 #define IPS_UNLOCK_RESTORE(lock,flags) do{spin_unlock(lock);(void)flags;}while(0)
221 #endif
222
223 #define IPS_DMA_DIR(scb) ((!scb->scsi_cmd || ips_is_passthru(scb->scsi_cmd) || \
224                          SCSI_DATA_NONE == scb->scsi_cmd->sc_data_direction) ? \
225                          PCI_DMA_BIDIRECTIONAL : \
226                          scsi_to_pci_dma_dir(scb->scsi_cmd->sc_data_direction))
227
228 #ifdef IPS_DEBUG
229 #define METHOD_TRACE(s, i)    if (ips_debug >= (i+10)) printk(KERN_NOTICE s "\n");
230 #define DEBUG(i, s)           if (ips_debug >= i) printk(KERN_NOTICE s "\n");
231 #define DEBUG_VAR(i, s, v...) if (ips_debug >= i) printk(KERN_NOTICE s "\n", v);
232 #else
233 #define METHOD_TRACE(s, i)
234 #define DEBUG(i, s)
235 #define DEBUG_VAR(i, s, v...)
236 #endif
237
238 /*
239  * global variables
240  */
241 static const char ips_name[] = "ips";
242 static struct Scsi_Host *ips_sh[IPS_MAX_ADAPTERS];      /* Array of host controller structures */
243 static ips_ha_t *ips_ha[IPS_MAX_ADAPTERS];      /* Array of HA structures */
244 static unsigned int ips_next_controller;
245 static unsigned int ips_num_controllers;
246 static unsigned int ips_released_controllers;
247 static int ips_hotplug;
248 static int ips_cmd_timeout = 60;
249 static int ips_reset_timeout = 60 * 5;
250 static int ips_force_memio = 1;         /* Always use Memory Mapped I/O    */
251 static int ips_force_i2o = 1;   /* Always use I2O command delivery */
252 static int ips_ioctlsize = IPS_IOCTL_SIZE;      /* Size of the ioctl buffer        */
253 static int ips_cd_boot;                 /* Booting from Manager CD         */
254 static char *ips_FlashData = NULL;      /* CD Boot - Flash Data Buffer      */
255 static dma_addr_t ips_flashbusaddr;
256 static long ips_FlashDataInUse;         /* CD Boot - Flash Data In Use Flag */
257 static uint32_t MaxLiteCmds = 32;       /* Max Active Cmds for a Lite Adapter */
258 static Scsi_Host_Template ips_driver_template = {
259         .detect                 = ips_detect,
260         .release                = ips_release,
261         .info                   = ips_info,
262         .queuecommand           = ips_queue,
263         .eh_abort_handler       = ips_eh_abort,
264         .eh_host_reset_handler  = ips_eh_reset,
265         .proc_name              = "ips",
266 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
267         .proc_info              = ips_proc_info,
268         .slave_configure        = ips_slave_configure,
269 #else
270         .proc_info              = ips_proc24_info,
271         .select_queue_depths    = ips_select_queue_depth,
272 #endif
273         .bios_param             = ips_biosparam,
274         .this_id                = -1,
275         .sg_tablesize           = IPS_MAX_SG,
276         .cmd_per_lun            = 3,
277         .use_clustering         = ENABLE_CLUSTERING,
278 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
279         .use_new_eh_code        = 1,
280 #endif
281 };
282
283 IPS_DEFINE_COMPAT_TABLE( Compatable );  /* Version Compatability Table      */
284
285
286 /* This table describes all ServeRAID Adapters */
287 static struct  pci_device_id  ips_pci_table[] = {
288         { 0x1014, 0x002E, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
289         { 0x1014, 0x01BD, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
290         { 0x9005, 0x0250, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
291         { 0, }
292 };
293
294 MODULE_DEVICE_TABLE( pci, ips_pci_table );
295
296 static char ips_hot_plug_name[] = "ips";
297    
298 static int __devinit  ips_insert_device(struct pci_dev *pci_dev, const struct pci_device_id *ent);
299 static void __devexit ips_remove_device(struct pci_dev *pci_dev);
300    
301 struct pci_driver ips_pci_driver = {
302         .name           = ips_hot_plug_name,
303         .id_table       = ips_pci_table,
304         .probe          = ips_insert_device,
305         .remove         = __devexit_p(ips_remove_device),
306 };
307            
308
309 /*
310  * Necessary forward function protoypes
311  */
312 static int ips_halt(struct notifier_block *nb, ulong event, void *buf);
313
314 #define MAX_ADAPTER_NAME 15
315
316 static char ips_adapter_name[][30] = {
317         "ServeRAID",
318         "ServeRAID II",
319         "ServeRAID on motherboard",
320         "ServeRAID on motherboard",
321         "ServeRAID 3H",
322         "ServeRAID 3L",
323         "ServeRAID 4H",
324         "ServeRAID 4M",
325         "ServeRAID 4L",
326         "ServeRAID 4Mx",
327         "ServeRAID 4Lx",
328         "ServeRAID 5i",
329         "ServeRAID 5i",
330         "ServeRAID 6M",
331         "ServeRAID 6i",
332         "ServeRAID 7t",
333         "ServeRAID 7k",
334         "ServeRAID 7M"
335 };
336
337 static struct notifier_block ips_notifier = {
338         ips_halt, NULL, 0
339 };
340
341 /*
342  * Direction table
343  */
344 static char ips_command_direction[] = {
345         IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_IN, IPS_DATA_IN, IPS_DATA_OUT,
346         IPS_DATA_IN, IPS_DATA_IN, IPS_DATA_OUT, IPS_DATA_IN, IPS_DATA_UNK,
347         IPS_DATA_OUT, IPS_DATA_OUT, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
348         IPS_DATA_IN, IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_IN, IPS_DATA_OUT,
349         IPS_DATA_IN, IPS_DATA_OUT, IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_OUT,
350         IPS_DATA_NONE, IPS_DATA_IN, IPS_DATA_NONE, IPS_DATA_IN, IPS_DATA_OUT,
351         IPS_DATA_NONE, IPS_DATA_UNK, IPS_DATA_IN, IPS_DATA_UNK, IPS_DATA_IN,
352         IPS_DATA_UNK, IPS_DATA_OUT, IPS_DATA_IN, IPS_DATA_UNK, IPS_DATA_UNK,
353         IPS_DATA_IN, IPS_DATA_IN, IPS_DATA_OUT, IPS_DATA_NONE, IPS_DATA_UNK,
354         IPS_DATA_IN, IPS_DATA_OUT, IPS_DATA_OUT, IPS_DATA_OUT, IPS_DATA_OUT,
355         IPS_DATA_OUT, IPS_DATA_NONE, IPS_DATA_IN, IPS_DATA_NONE, IPS_DATA_NONE,
356         IPS_DATA_IN, IPS_DATA_OUT, IPS_DATA_OUT, IPS_DATA_OUT, IPS_DATA_OUT,
357         IPS_DATA_IN, IPS_DATA_OUT, IPS_DATA_IN, IPS_DATA_OUT, IPS_DATA_OUT,
358         IPS_DATA_OUT, IPS_DATA_IN, IPS_DATA_IN, IPS_DATA_IN, IPS_DATA_NONE,
359         IPS_DATA_UNK, IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_UNK,
360         IPS_DATA_NONE, IPS_DATA_OUT, IPS_DATA_IN, IPS_DATA_UNK, IPS_DATA_UNK,
361         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
362         IPS_DATA_OUT, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
363         IPS_DATA_IN, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
364         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
365         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
366         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
367         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
368         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
369         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
370         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
371         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
372         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
373         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
374         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
375         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
376         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
377         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
378         IPS_DATA_NONE, IPS_DATA_NONE, IPS_DATA_UNK, IPS_DATA_IN, IPS_DATA_NONE,
379         IPS_DATA_OUT, IPS_DATA_UNK, IPS_DATA_NONE, IPS_DATA_UNK, IPS_DATA_OUT,
380         IPS_DATA_OUT, IPS_DATA_OUT, IPS_DATA_OUT, IPS_DATA_OUT, IPS_DATA_NONE,
381         IPS_DATA_UNK, IPS_DATA_IN, IPS_DATA_OUT, IPS_DATA_IN, IPS_DATA_IN,
382         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
383         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
384         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
385         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
386         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
387         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
388         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
389         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
390         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
391         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_OUT,
392         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
393         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
394         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK,
395         IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK, IPS_DATA_UNK
396 };
397
398 /*
399  * Function prototypes
400  */
401 int ips_detect(Scsi_Host_Template *);
402 int ips_release(struct Scsi_Host *);
403 int ips_eh_abort(Scsi_Cmnd *);
404 int ips_eh_reset(Scsi_Cmnd *);
405 int ips_queue(Scsi_Cmnd *, void (*)(Scsi_Cmnd *));
406 const char *ips_info(struct Scsi_Host *);
407 irqreturn_t do_ipsintr(int, void *, struct pt_regs *);
408 static int ips_hainit(ips_ha_t *);
409 static int ips_map_status(ips_ha_t *, ips_scb_t *, ips_stat_t *);
410 static int ips_send_wait(ips_ha_t *, ips_scb_t *, int, int);
411 static int ips_send_cmd(ips_ha_t *, ips_scb_t *);
412 static int ips_online(ips_ha_t *, ips_scb_t *);
413 static int ips_inquiry(ips_ha_t *, ips_scb_t *);
414 static int ips_rdcap(ips_ha_t *, ips_scb_t *);
415 static int ips_msense(ips_ha_t *, ips_scb_t *);
416 static int ips_reqsen(ips_ha_t *, ips_scb_t *);
417 static int ips_deallocatescbs(ips_ha_t *, int);
418 static int ips_allocatescbs(ips_ha_t *);
419 static int ips_reset_copperhead(ips_ha_t *);
420 static int ips_reset_copperhead_memio(ips_ha_t *);
421 static int ips_reset_morpheus(ips_ha_t *);
422 static int ips_issue_copperhead(ips_ha_t *, ips_scb_t *);
423 static int ips_issue_copperhead_memio(ips_ha_t *, ips_scb_t *);
424 static int ips_issue_i2o(ips_ha_t *, ips_scb_t *);
425 static int ips_issue_i2o_memio(ips_ha_t *, ips_scb_t *);
426 static int ips_isintr_copperhead(ips_ha_t *);
427 static int ips_isintr_copperhead_memio(ips_ha_t *);
428 static int ips_isintr_morpheus(ips_ha_t *);
429 static int ips_wait(ips_ha_t *, int, int);
430 static int ips_write_driver_status(ips_ha_t *, int);
431 static int ips_read_adapter_status(ips_ha_t *, int);
432 static int ips_read_subsystem_parameters(ips_ha_t *, int);
433 static int ips_read_config(ips_ha_t *, int);
434 static int ips_clear_adapter(ips_ha_t *, int);
435 static int ips_readwrite_page5(ips_ha_t *, int, int);
436 static int ips_init_copperhead(ips_ha_t *);
437 static int ips_init_copperhead_memio(ips_ha_t *);
438 static int ips_init_morpheus(ips_ha_t *);
439 static int ips_isinit_copperhead(ips_ha_t *);
440 static int ips_isinit_copperhead_memio(ips_ha_t *);
441 static int ips_isinit_morpheus(ips_ha_t *);
442 static int ips_erase_bios(ips_ha_t *);
443 static int ips_program_bios(ips_ha_t *, char *, uint32_t, uint32_t);
444 static int ips_verify_bios(ips_ha_t *, char *, uint32_t, uint32_t);
445 static int ips_erase_bios_memio(ips_ha_t *);
446 static int ips_program_bios_memio(ips_ha_t *, char *, uint32_t, uint32_t);
447 static int ips_verify_bios_memio(ips_ha_t *, char *, uint32_t, uint32_t);
448 static int ips_flash_copperhead(ips_ha_t *, ips_passthru_t *, ips_scb_t *);
449 static int ips_flash_bios(ips_ha_t *, ips_passthru_t *, ips_scb_t *);
450 static int ips_flash_firmware(ips_ha_t *, ips_passthru_t *, ips_scb_t *);
451 static void ips_free_flash_copperhead(ips_ha_t * ha);
452 static void ips_get_bios_version(ips_ha_t *, int);
453 static void ips_identify_controller(ips_ha_t *);
454 static void ips_chkstatus(ips_ha_t *, IPS_STATUS *);
455 static void ips_enable_int_copperhead(ips_ha_t *);
456 static void ips_enable_int_copperhead_memio(ips_ha_t *);
457 static void ips_enable_int_morpheus(ips_ha_t *);
458 static int ips_intr_copperhead(ips_ha_t *);
459 static int ips_intr_morpheus(ips_ha_t *);
460 static void ips_next(ips_ha_t *, int);
461 static void ipsintr_blocking(ips_ha_t *, struct ips_scb *);
462 static void ipsintr_done(ips_ha_t *, struct ips_scb *);
463 static void ips_done(ips_ha_t *, ips_scb_t *);
464 static void ips_free(ips_ha_t *);
465 static void ips_init_scb(ips_ha_t *, ips_scb_t *);
466 static void ips_freescb(ips_ha_t *, ips_scb_t *);
467 static void ips_setup_funclist(ips_ha_t *);
468 static void ips_statinit(ips_ha_t *);
469 static void ips_statinit_memio(ips_ha_t *);
470 static void ips_fix_ffdc_time(ips_ha_t *, ips_scb_t *, time_t);
471 static void ips_ffdc_reset(ips_ha_t *, int);
472 static void ips_ffdc_time(ips_ha_t *);
473 static uint32_t ips_statupd_copperhead(ips_ha_t *);
474 static uint32_t ips_statupd_copperhead_memio(ips_ha_t *);
475 static uint32_t ips_statupd_morpheus(ips_ha_t *);
476 static ips_scb_t *ips_getscb(ips_ha_t *);
477 static inline void ips_putq_scb_head(ips_scb_queue_t *, ips_scb_t *);
478 static inline void ips_putq_scb_tail(ips_scb_queue_t *, ips_scb_t *);
479 static inline void ips_putq_wait_head(ips_wait_queue_t *, Scsi_Cmnd *);
480 static inline void ips_putq_wait_tail(ips_wait_queue_t *, Scsi_Cmnd *);
481 static inline void ips_putq_copp_head(ips_copp_queue_t *,
482                                       ips_copp_wait_item_t *);
483 static inline void ips_putq_copp_tail(ips_copp_queue_t *,
484                                       ips_copp_wait_item_t *);
485 static inline ips_scb_t *ips_removeq_scb_head(ips_scb_queue_t *);
486 static inline ips_scb_t *ips_removeq_scb(ips_scb_queue_t *, ips_scb_t *);
487 static inline Scsi_Cmnd *ips_removeq_wait_head(ips_wait_queue_t *);
488 static inline Scsi_Cmnd *ips_removeq_wait(ips_wait_queue_t *, Scsi_Cmnd *);
489 static inline ips_copp_wait_item_t *ips_removeq_copp(ips_copp_queue_t *,
490                                                      ips_copp_wait_item_t *);
491 static inline ips_copp_wait_item_t *ips_removeq_copp_head(ips_copp_queue_t *);
492
493 static int ips_is_passthru(Scsi_Cmnd *);
494 static int ips_make_passthru(ips_ha_t *, Scsi_Cmnd *, ips_scb_t *, int);
495 static int ips_usrcmd(ips_ha_t *, ips_passthru_t *, ips_scb_t *);
496 static void ips_cleanup_passthru(ips_ha_t *, ips_scb_t *);
497 static void ips_scmd_buf_write(Scsi_Cmnd * scmd, void *data,
498                                unsigned int count);
499 static void ips_scmd_buf_read(Scsi_Cmnd * scmd, void *data, unsigned int count);
500
501 int ips_proc_info(struct Scsi_Host *, char *, char **, off_t, int, int);
502 static int ips_host_info(ips_ha_t *, char *, off_t, int);
503 static void copy_mem_info(IPS_INFOSTR *, char *, int);
504 static int copy_info(IPS_INFOSTR *, char *, ...);
505 static int ips_get_version_info(ips_ha_t * ha, dma_addr_t, int intr);
506 static void ips_version_check(ips_ha_t * ha, int intr);
507 static int ips_abort_init(ips_ha_t * ha, int index);
508 static int ips_init_phase2(int index);
509
510 static int ips_init_phase1(struct pci_dev *pci_dev, int *indexPtr);
511 static int ips_register_scsi(int index);
512 /*--------------------------------------------------------------------------*/
513 /* Exported Functions                                                       */
514 /*--------------------------------------------------------------------------*/
515
516 /****************************************************************************/
517 /*                                                                          */
518 /* Routine Name: ips_setup                                                  */
519 /*                                                                          */
520 /* Routine Description:                                                     */
521 /*                                                                          */
522 /*   setup parameters to the driver                                         */
523 /*                                                                          */
524 /****************************************************************************/
525 static int
526 ips_setup(char *ips_str)
527 {
528
529         int i;
530         char *key;
531         char *value;
532         IPS_OPTION options[] = {
533                 {"noi2o", &ips_force_i2o, 0},
534                 {"nommap", &ips_force_memio, 0},
535                 {"ioctlsize", &ips_ioctlsize, IPS_IOCTL_SIZE},
536                 {"cdboot", &ips_cd_boot, 0},
537                 {"maxcmds", &MaxLiteCmds, 32},
538         };
539
540         /* Don't use strtok() anymore ( if 2.4 Kernel or beyond ) */
541         /* Search for value */
542         while ((key = strsep(&ips_str, ",."))) {
543                 if (!*key)
544                         continue;
545                 value = strchr(key, ':');
546                 if (value)
547                         *value++ = '\0';
548                 /*
549                  * We now have key/value pairs.
550                  * Update the variables
551                  */
552                 for (i = 0; i < (sizeof (options) / sizeof (options[0])); i++) {
553                         if (strnicmp
554                             (key, options[i].option_name,
555                              strlen(options[i].option_name)) == 0) {
556                                 if (value)
557                                         *options[i].option_flag =
558                                             simple_strtoul(value, NULL, 0);
559                                 else
560                                         *options[i].option_flag =
561                                             options[i].option_value;
562                                 break;
563                         }
564                 }
565         }
566
567         return (1);
568 }
569
570 __setup("ips=", ips_setup);
571
572 /****************************************************************************/
573 /*                                                                          */
574 /* Routine Name: ips_detect                                                 */
575 /*                                                                          */
576 /* Routine Description:                                                     */
577 /*                                                                          */
578 /*   Detect and initialize the driver                                       */
579 /*                                                                          */
580 /* NOTE: this routine is called under the io_request_lock spinlock          */
581 /*                                                                          */
582 /****************************************************************************/
583 int
584 ips_detect(Scsi_Host_Template * SHT)
585 {
586         int i;
587
588         METHOD_TRACE("ips_detect", 1);
589
590 #ifdef MODULE
591         if (ips)
592                 ips_setup(ips);
593 #endif
594
595         for (i = 0; i < ips_num_controllers; i++) {
596                 if (ips_register_scsi(i))
597                         ips_free(ips_ha[i]);
598                 ips_released_controllers++;
599         }
600         ips_hotplug = 1;
601         return (ips_num_controllers);
602 }
603
604 /****************************************************************************/
605 /*   configure the function pointers to use the functions that will work    */
606 /*   with the found version of the adapter                                  */
607 /****************************************************************************/
608 static void
609 ips_setup_funclist(ips_ha_t * ha)
610 {
611
612         /*                                
613          * Setup Functions
614          */
615         if (IPS_IS_MORPHEUS(ha) || IPS_IS_MARCO(ha)) {
616                 /* morpheus / marco / sebring */
617                 ha->func.isintr = ips_isintr_morpheus;
618                 ha->func.isinit = ips_isinit_morpheus;
619                 ha->func.issue = ips_issue_i2o_memio;
620                 ha->func.init = ips_init_morpheus;
621                 ha->func.statupd = ips_statupd_morpheus;
622                 ha->func.reset = ips_reset_morpheus;
623                 ha->func.intr = ips_intr_morpheus;
624                 ha->func.enableint = ips_enable_int_morpheus;
625         } else if (IPS_USE_MEMIO(ha)) {
626                 /* copperhead w/MEMIO */
627                 ha->func.isintr = ips_isintr_copperhead_memio;
628                 ha->func.isinit = ips_isinit_copperhead_memio;
629                 ha->func.init = ips_init_copperhead_memio;
630                 ha->func.statupd = ips_statupd_copperhead_memio;
631                 ha->func.statinit = ips_statinit_memio;
632                 ha->func.reset = ips_reset_copperhead_memio;
633                 ha->func.intr = ips_intr_copperhead;
634                 ha->func.erasebios = ips_erase_bios_memio;
635                 ha->func.programbios = ips_program_bios_memio;
636                 ha->func.verifybios = ips_verify_bios_memio;
637                 ha->func.enableint = ips_enable_int_copperhead_memio;
638                 if (IPS_USE_I2O_DELIVER(ha))
639                         ha->func.issue = ips_issue_i2o_memio;
640                 else
641                         ha->func.issue = ips_issue_copperhead_memio;
642         } else {
643                 /* copperhead */
644                 ha->func.isintr = ips_isintr_copperhead;
645                 ha->func.isinit = ips_isinit_copperhead;
646                 ha->func.init = ips_init_copperhead;
647                 ha->func.statupd = ips_statupd_copperhead;
648                 ha->func.statinit = ips_statinit;
649                 ha->func.reset = ips_reset_copperhead;
650                 ha->func.intr = ips_intr_copperhead;
651                 ha->func.erasebios = ips_erase_bios;
652                 ha->func.programbios = ips_program_bios;
653                 ha->func.verifybios = ips_verify_bios;
654                 ha->func.enableint = ips_enable_int_copperhead;
655
656                 if (IPS_USE_I2O_DELIVER(ha))
657                         ha->func.issue = ips_issue_i2o;
658                 else
659                         ha->func.issue = ips_issue_copperhead;
660         }
661 }
662
663 /****************************************************************************/
664 /*                                                                          */
665 /* Routine Name: ips_release                                                */
666 /*                                                                          */
667 /* Routine Description:                                                     */
668 /*                                                                          */
669 /*   Remove a driver                                                        */
670 /*                                                                          */
671 /****************************************************************************/
672 int
673 ips_release(struct Scsi_Host *sh)
674 {
675         ips_scb_t *scb;
676         ips_ha_t *ha;
677         int i;
678
679         METHOD_TRACE("ips_release", 1);
680
681         for (i = 0; i < IPS_MAX_ADAPTERS && ips_sh[i] != sh; i++) ;
682
683         if (i == IPS_MAX_ADAPTERS) {
684                 printk(KERN_WARNING
685                        "(%s) release, invalid Scsi_Host pointer.\n", ips_name);
686                 BUG();
687                 return (FALSE);
688         }
689
690         ha = IPS_HA(sh);
691
692         if (!ha)
693                 return (FALSE);
694
695         /* flush the cache on the controller */
696         scb = &ha->scbs[ha->max_cmds - 1];
697
698         ips_init_scb(ha, scb);
699
700         scb->timeout = ips_cmd_timeout;
701         scb->cdb[0] = IPS_CMD_FLUSH;
702
703         scb->cmd.flush_cache.op_code = IPS_CMD_FLUSH;
704         scb->cmd.flush_cache.command_id = IPS_COMMAND_ID(ha, scb);
705         scb->cmd.flush_cache.state = IPS_NORM_STATE;
706         scb->cmd.flush_cache.reserved = 0;
707         scb->cmd.flush_cache.reserved2 = 0;
708         scb->cmd.flush_cache.reserved3 = 0;
709         scb->cmd.flush_cache.reserved4 = 0;
710
711         IPS_PRINTK(KERN_WARNING, ha->pcidev, "Flushing Cache.\n");
712
713         /* send command */
714         if (ips_send_wait(ha, scb, ips_cmd_timeout, IPS_INTR_ON) == IPS_FAILURE)
715                 IPS_PRINTK(KERN_WARNING, ha->pcidev, "Incomplete Flush.\n");
716
717         IPS_PRINTK(KERN_WARNING, ha->pcidev, "Flushing Complete.\n");
718
719         ips_sh[i] = NULL;
720         ips_ha[i] = NULL;
721
722         /* free extra memory */
723         ips_free(ha);
724
725         /* Free I/O Region */
726         if (ha->io_addr)
727                 release_region(ha->io_addr, ha->io_len);
728
729         /* free IRQ */
730         free_irq(ha->irq, ha);
731
732         IPS_REMOVE_HOST(sh);
733         scsi_host_put(sh);
734
735         ips_released_controllers++;
736
737         return (FALSE);
738 }
739
740 /****************************************************************************/
741 /*                                                                          */
742 /* Routine Name: ips_halt                                                   */
743 /*                                                                          */
744 /* Routine Description:                                                     */
745 /*                                                                          */
746 /*   Perform cleanup when the system reboots                                */
747 /*                                                                          */
748 /****************************************************************************/
749 static int
750 ips_halt(struct notifier_block *nb, ulong event, void *buf)
751 {
752         ips_scb_t *scb;
753         ips_ha_t *ha;
754         int i;
755
756         if ((event != SYS_RESTART) && (event != SYS_HALT) &&
757             (event != SYS_POWER_OFF))
758                 return (NOTIFY_DONE);
759
760         for (i = 0; i < ips_next_controller; i++) {
761                 ha = (ips_ha_t *) ips_ha[i];
762
763                 if (!ha)
764                         continue;
765
766                 if (!ha->active)
767                         continue;
768
769                 /* flush the cache on the controller */
770                 scb = &ha->scbs[ha->max_cmds - 1];
771
772                 ips_init_scb(ha, scb);
773
774                 scb->timeout = ips_cmd_timeout;
775                 scb->cdb[0] = IPS_CMD_FLUSH;
776
777                 scb->cmd.flush_cache.op_code = IPS_CMD_FLUSH;
778                 scb->cmd.flush_cache.command_id = IPS_COMMAND_ID(ha, scb);
779                 scb->cmd.flush_cache.state = IPS_NORM_STATE;
780                 scb->cmd.flush_cache.reserved = 0;
781                 scb->cmd.flush_cache.reserved2 = 0;
782                 scb->cmd.flush_cache.reserved3 = 0;
783                 scb->cmd.flush_cache.reserved4 = 0;
784
785                 IPS_PRINTK(KERN_WARNING, ha->pcidev, "Flushing Cache.\n");
786
787                 /* send command */
788                 if (ips_send_wait(ha, scb, ips_cmd_timeout, IPS_INTR_ON) ==
789                     IPS_FAILURE)
790                         IPS_PRINTK(KERN_WARNING, ha->pcidev,
791                                    "Incomplete Flush.\n");
792                 else
793                         IPS_PRINTK(KERN_WARNING, ha->pcidev,
794                                    "Flushing Complete.\n");
795         }
796
797         return (NOTIFY_OK);
798 }
799
800 /****************************************************************************/
801 /*                                                                          */
802 /* Routine Name: ips_eh_abort                                               */
803 /*                                                                          */
804 /* Routine Description:                                                     */
805 /*                                                                          */
806 /*   Abort a command (using the new error code stuff)                       */
807 /* Note: this routine is called under the io_request_lock                   */
808 /****************************************************************************/
809 int
810 ips_eh_abort(Scsi_Cmnd * SC)
811 {
812         ips_ha_t *ha;
813         ips_copp_wait_item_t *item;
814         int ret;
815
816         METHOD_TRACE("ips_eh_abort", 1);
817
818         if (!SC)
819                 return (FAILED);
820
821         ha = (ips_ha_t *) SC->device->host->hostdata;
822
823         if (!ha)
824                 return (FAILED);
825
826         if (!ha->active)
827                 return (FAILED);
828
829         if (SC->serial_number != SC->serial_number_at_timeout) {
830                 /* HMM, looks like a bogus command */
831                 DEBUG(1, "Abort called with bogus scsi command");
832
833                 return (FAILED);
834         }
835
836         /* See if the command is on the copp queue */
837         item = ha->copp_waitlist.head;
838         while ((item) && (item->scsi_cmd != SC))
839                 item = item->next;
840
841         if (item) {
842                 /* Found it */
843                 ips_removeq_copp(&ha->copp_waitlist, item);
844                 ret = (SUCCESS);
845
846                 /* See if the command is on the wait queue */
847         } else if (ips_removeq_wait(&ha->scb_waitlist, SC)) {
848                 /* command not sent yet */
849                 ret = (SUCCESS);
850         } else {
851                 /* command must have already been sent */
852                 ret = (FAILED);
853         }
854         return ret;
855 }
856
857 /****************************************************************************/
858 /*                                                                          */
859 /* Routine Name: ips_eh_reset                                               */
860 /*                                                                          */
861 /* Routine Description:                                                     */
862 /*                                                                          */
863 /*   Reset the controller (with new eh error code)                          */
864 /*                                                                          */
865 /* NOTE: this routine is called under the io_request_lock spinlock          */
866 /*                                                                          */
867 /****************************************************************************/
868 int
869 ips_eh_reset(Scsi_Cmnd * SC)
870 {
871         int ret;
872         int i;
873         ips_ha_t *ha;
874         ips_scb_t *scb;
875         ips_copp_wait_item_t *item;
876
877         METHOD_TRACE("ips_eh_reset", 1);
878
879 #ifdef NO_IPS_RESET
880         return (FAILED);
881 #else
882
883         if (!SC) {
884                 DEBUG(1, "Reset called with NULL scsi command");
885
886                 return (FAILED);
887         }
888
889         ha = (ips_ha_t *) SC->device->host->hostdata;
890
891         if (!ha) {
892                 DEBUG(1, "Reset called with NULL ha struct");
893
894                 return (FAILED);
895         }
896
897         if (!ha->active)
898                 return (FAILED);
899
900         /* See if the command is on the copp queue */
901         item = ha->copp_waitlist.head;
902         while ((item) && (item->scsi_cmd != SC))
903                 item = item->next;
904
905         if (item) {
906                 /* Found it */
907                 ips_removeq_copp(&ha->copp_waitlist, item);
908                 return (SUCCESS);
909         }
910
911         /* See if the command is on the wait queue */
912         if (ips_removeq_wait(&ha->scb_waitlist, SC)) {
913                 /* command not sent yet */
914                 return (SUCCESS);
915         }
916
917         /* An explanation for the casual observer:                              */
918         /* Part of the function of a RAID controller is automatic error         */
919         /* detection and recovery.  As such, the only problem that physically   */
920         /* resetting an adapter will ever fix is when, for some reason,         */
921         /* the driver is not successfully communicating with the adapter.       */
922         /* Therefore, we will attempt to flush this adapter.  If that succeeds, */
923         /* then there's no real purpose in a physical reset. This will complete */
924         /* much faster and avoids any problems that might be caused by a        */
925         /* physical reset ( such as having to fail all the outstanding I/O's ). */
926
927         if (ha->ioctl_reset == 0) {     /* IF Not an IOCTL Requested Reset */
928                 scb = &ha->scbs[ha->max_cmds - 1];
929
930                 ips_init_scb(ha, scb);
931
932                 scb->timeout = ips_cmd_timeout;
933                 scb->cdb[0] = IPS_CMD_FLUSH;
934
935                 scb->cmd.flush_cache.op_code = IPS_CMD_FLUSH;
936                 scb->cmd.flush_cache.command_id = IPS_COMMAND_ID(ha, scb);
937                 scb->cmd.flush_cache.state = IPS_NORM_STATE;
938                 scb->cmd.flush_cache.reserved = 0;
939                 scb->cmd.flush_cache.reserved2 = 0;
940                 scb->cmd.flush_cache.reserved3 = 0;
941                 scb->cmd.flush_cache.reserved4 = 0;
942
943                 /* Attempt the flush command */
944                 ret = ips_send_wait(ha, scb, ips_cmd_timeout, IPS_INTR_IORL);
945                 if (ret == IPS_SUCCESS) {
946                         IPS_PRINTK(KERN_NOTICE, ha->pcidev,
947                                    "Reset Request - Flushed Cache\n");
948                         return (SUCCESS);
949                 }
950         }
951
952         /* Either we can't communicate with the adapter or it's an IOCTL request */
953         /* from a utility.  A physical reset is needed at this point.            */
954
955         ha->ioctl_reset = 0;    /* Reset the IOCTL Requested Reset Flag */
956
957         /*
958          * command must have already been sent
959          * reset the controller
960          */
961         IPS_PRINTK(KERN_NOTICE, ha->pcidev, "Resetting controller.\n");
962         ret = (*ha->func.reset) (ha);
963
964         if (!ret) {
965                 Scsi_Cmnd *scsi_cmd;
966
967                 IPS_PRINTK(KERN_NOTICE, ha->pcidev,
968                            "Controller reset failed - controller now offline.\n");
969
970                 /* Now fail all of the active commands */
971                 DEBUG_VAR(1, "(%s%d) Failing active commands",
972                           ips_name, ha->host_num);
973
974                 while ((scb = ips_removeq_scb_head(&ha->scb_activelist))) {
975                         scb->scsi_cmd->result = DID_ERROR << 16;
976                         scb->scsi_cmd->scsi_done(scb->scsi_cmd);
977                         ips_freescb(ha, scb);
978                 }
979
980                 /* Now fail all of the pending commands */
981                 DEBUG_VAR(1, "(%s%d) Failing pending commands",
982                           ips_name, ha->host_num);
983
984                 while ((scsi_cmd = ips_removeq_wait_head(&ha->scb_waitlist))) {
985                         scsi_cmd->result = DID_ERROR;
986                         scsi_cmd->scsi_done(scsi_cmd);
987                 }
988
989                 ha->active = FALSE;
990                 return (FAILED);
991         }
992
993         if (!ips_clear_adapter(ha, IPS_INTR_IORL)) {
994                 Scsi_Cmnd *scsi_cmd;
995
996                 IPS_PRINTK(KERN_NOTICE, ha->pcidev,
997                            "Controller reset failed - controller now offline.\n");
998
999                 /* Now fail all of the active commands */
1000                 DEBUG_VAR(1, "(%s%d) Failing active commands",
1001                           ips_name, ha->host_num);
1002
1003                 while ((scb = ips_removeq_scb_head(&ha->scb_activelist))) {
1004                         scb->scsi_cmd->result = DID_ERROR << 16;
1005                         scb->scsi_cmd->scsi_done(scb->scsi_cmd);
1006                         ips_freescb(ha, scb);
1007                 }
1008
1009                 /* Now fail all of the pending commands */
1010                 DEBUG_VAR(1, "(%s%d) Failing pending commands",
1011                           ips_name, ha->host_num);
1012
1013                 while ((scsi_cmd = ips_removeq_wait_head(&ha->scb_waitlist))) {
1014                         scsi_cmd->result = DID_ERROR << 16;
1015                         scsi_cmd->scsi_done(scsi_cmd);
1016                 }
1017
1018                 ha->active = FALSE;
1019                 return (FAILED);
1020         }
1021
1022         /* FFDC */
1023         if (le32_to_cpu(ha->subsys->param[3]) & 0x300000) {
1024                 struct timeval tv;
1025
1026                 do_gettimeofday(&tv);
1027                 ha->last_ffdc = tv.tv_sec;
1028                 ha->reset_count++;
1029                 ips_ffdc_reset(ha, IPS_INTR_IORL);
1030         }
1031
1032         /* Now fail all of the active commands */
1033         DEBUG_VAR(1, "(%s%d) Failing active commands", ips_name, ha->host_num);
1034
1035         while ((scb = ips_removeq_scb_head(&ha->scb_activelist))) {
1036                 scb->scsi_cmd->result =
1037                     (DID_RESET << 16) | (SUGGEST_RETRY << 24);
1038                 scb->scsi_cmd->scsi_done(scb->scsi_cmd);
1039                 ips_freescb(ha, scb);
1040         }
1041
1042         /* Reset DCDB active command bits */
1043         for (i = 1; i < ha->nbus; i++)
1044                 ha->dcdb_active[i - 1] = 0;
1045
1046         /* Reset the number of active IOCTLs */
1047         ha->num_ioctl = 0;
1048
1049         ips_next(ha, IPS_INTR_IORL);
1050
1051         return (SUCCESS);
1052 #endif                          /* NO_IPS_RESET */
1053
1054 }
1055
1056 /****************************************************************************/
1057 /*                                                                          */
1058 /* Routine Name: ips_queue                                                  */
1059 /*                                                                          */
1060 /* Routine Description:                                                     */
1061 /*                                                                          */
1062 /*   Send a command to the controller                                       */
1063 /*                                                                          */
1064 /* NOTE:                                                                    */
1065 /*    Linux obtains io_request_lock before calling this function            */
1066 /*                                                                          */
1067 /****************************************************************************/
1068 int
1069 ips_queue(Scsi_Cmnd * SC, void (*done) (Scsi_Cmnd *))
1070 {
1071         ips_ha_t *ha;
1072         ips_passthru_t *pt;
1073
1074         METHOD_TRACE("ips_queue", 1);
1075
1076         ha = (ips_ha_t *) SC->device->host->hostdata;
1077
1078         if (!ha)
1079                 return (1);
1080
1081         if (!ha->active)
1082                 return (DID_ERROR);
1083
1084         if (ips_is_passthru(SC)) {
1085                 if (ha->copp_waitlist.count == IPS_MAX_IOCTL_QUEUE) {
1086                         SC->result = DID_BUS_BUSY << 16;
1087                         done(SC);
1088
1089                         return (0);
1090                 }
1091         } else if (ha->scb_waitlist.count == IPS_MAX_QUEUE) {
1092                 SC->result = DID_BUS_BUSY << 16;
1093                 done(SC);
1094
1095                 return (0);
1096         }
1097
1098         SC->scsi_done = done;
1099
1100         DEBUG_VAR(2, "(%s%d): ips_queue: cmd 0x%X (%d %d %d)",
1101                   ips_name,
1102                   ha->host_num,
1103                   SC->cmnd[0],
1104                   SC->device->channel, SC->device->id, SC->device->lun);
1105
1106         /* Check for command to initiator IDs */
1107         if ((SC->device->channel > 0)
1108             && (SC->device->id == ha->ha_id[SC->device->channel])) {
1109                 SC->result = DID_NO_CONNECT << 16;
1110                 done(SC);
1111
1112                 return (0);
1113         }
1114
1115         if (ips_is_passthru(SC)) {
1116
1117                 ips_copp_wait_item_t *scratch;
1118
1119                 /* A Reset IOCTL is only sent by the boot CD in extreme cases.           */
1120                 /* There can never be any system activity ( network or disk ), but check */
1121                 /* anyway just as a good practice.                                       */
1122                 pt = (ips_passthru_t *) SC->request_buffer;
1123                 if ((pt->CoppCP.cmd.reset.op_code == IPS_CMD_RESET_CHANNEL) &&
1124                     (pt->CoppCP.cmd.reset.adapter_flag == 1)) {
1125                         if (ha->scb_activelist.count != 0) {
1126                                 SC->result = DID_BUS_BUSY << 16;
1127                                 done(SC);
1128                                 return (0);
1129                         }
1130                         ha->ioctl_reset = 1;    /* This reset request is from an IOCTL */
1131                         ips_eh_reset(SC);
1132                         SC->result = DID_OK << 16;
1133                         SC->scsi_done(SC);
1134                         return (0);
1135                 }
1136
1137                 /* allocate space for the scribble */
1138                 scratch = kmalloc(sizeof (ips_copp_wait_item_t), GFP_ATOMIC);
1139
1140                 if (!scratch) {
1141                         SC->result = DID_ERROR << 16;
1142                         done(SC);
1143
1144                         return (0);
1145                 }
1146
1147                 scratch->scsi_cmd = SC;
1148                 scratch->next = NULL;
1149
1150                 ips_putq_copp_tail(&ha->copp_waitlist, scratch);
1151         } else {
1152                 ips_putq_wait_tail(&ha->scb_waitlist, SC);
1153         }
1154
1155         ips_next(ha, IPS_INTR_IORL);
1156
1157         return (0);
1158 }
1159
1160 /****************************************************************************/
1161 /*                                                                          */
1162 /* Routine Name: ips_biosparam                                              */
1163 /*                                                                          */
1164 /* Routine Description:                                                     */
1165 /*                                                                          */
1166 /*   Set bios geometry for the controller                                   */
1167 /*                                                                          */
1168 /****************************************************************************/
1169 static int
1170 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
1171 ips_biosparam(Disk * disk, kdev_t dev, int geom[])
1172 {
1173         ips_ha_t *ha = (ips_ha_t *) disk->device->host->hostdata;
1174         unsigned long capacity = disk->capacity;
1175 #else
1176 ips_biosparam(struct scsi_device *sdev, struct block_device *bdev,
1177               sector_t capacity, int geom[])
1178 {
1179         ips_ha_t *ha = (ips_ha_t *) sdev->host->hostdata;
1180 #endif
1181         int heads;
1182         int sectors;
1183         int cylinders;
1184
1185         METHOD_TRACE("ips_biosparam", 1);
1186
1187         if (!ha)
1188                 /* ?!?! host adater info invalid */
1189                 return (0);
1190
1191         if (!ha->active)
1192                 return (0);
1193
1194         if (!ips_read_adapter_status(ha, IPS_INTR_ON))
1195                 /* ?!?! Enquiry command failed */
1196                 return (0);
1197
1198         if ((capacity > 0x400000) && ((ha->enq->ucMiscFlag & 0x8) == 0)) {
1199                 heads = IPS_NORM_HEADS;
1200                 sectors = IPS_NORM_SECTORS;
1201         } else {
1202                 heads = IPS_COMP_HEADS;
1203                 sectors = IPS_COMP_SECTORS;
1204         }
1205
1206         cylinders = (unsigned long) capacity / (heads * sectors);
1207
1208         DEBUG_VAR(2, "Geometry: heads: %d, sectors: %d, cylinders: %d",
1209                   heads, sectors, cylinders);
1210
1211         geom[0] = heads;
1212         geom[1] = sectors;
1213         geom[2] = cylinders;
1214
1215         return (0);
1216 }
1217
1218 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
1219
1220 /* ips_proc24_info is a wrapper around ips_proc_info *
1221  * for compatibility with the 2.4 scsi parameters    */
1222 static int
1223 ips_proc24_info(char *buffer, char **start, off_t offset, int length,
1224                               int hostno, int func)
1225 {
1226         int i;
1227
1228         for (i = 0; i < ips_next_controller; i++) {
1229                 if (ips_sh[i] && ips_sh[i]->host_no == hostno) {
1230                         return ips_proc_info(ips_sh[i], buffer, start,
1231                                              offset, length, func);
1232                 }
1233         }
1234         return -EINVAL; 
1235 }
1236
1237 /****************************************************************************/
1238 /*                                                                          */
1239 /* Routine Name: ips_select_queue_depth                                     */
1240 /*                                                                          */
1241 /* Routine Description:                                                     */
1242 /*                                                                          */
1243 /*   Select queue depths for the devices on the contoller                   */
1244 /*                                                                          */
1245 /****************************************************************************/
1246 static void
1247 ips_select_queue_depth(struct Scsi_Host *host, Scsi_Device * scsi_devs)
1248 {
1249         Scsi_Device *device;
1250         ips_ha_t *ha;
1251         int count = 0;
1252         int min;
1253
1254         ha = IPS_HA(host);
1255         min = ha->max_cmds / 4;
1256
1257         for (device = scsi_devs; device; device = device->next) {
1258                 if (device->host == host) {
1259                         if ((device->channel == 0) && (device->type == 0))
1260                                 count++;
1261                 }
1262         }
1263
1264         for (device = scsi_devs; device; device = device->next) {
1265                 if (device->host == host) {
1266                         if ((device->channel == 0) && (device->type == 0)) {
1267                                 device->queue_depth =
1268                                     (ha->max_cmds - 1) / count;
1269                                 if (device->queue_depth < min)
1270                                         device->queue_depth = min;
1271                         } else {
1272                                 device->queue_depth = 2;
1273                         }
1274
1275                         if (device->queue_depth < 2)
1276                                 device->queue_depth = 2;
1277                 }
1278         }
1279 }
1280
1281 #else
1282 /****************************************************************************/
1283 /*                                                                          */
1284 /* Routine Name: ips_slave_configure                                        */
1285 /*                                                                          */
1286 /* Routine Description:                                                     */
1287 /*                                                                          */
1288 /*   Set queue depths on devices once scan is complete                      */
1289 /*                                                                          */
1290 /****************************************************************************/
1291 int
1292 ips_slave_configure(Scsi_Device * SDptr)
1293 {
1294         ips_ha_t *ha;
1295         int min;
1296
1297         ha = IPS_HA(SDptr->host);
1298         if (SDptr->tagged_supported && SDptr->type == TYPE_DISK) {
1299                 min = ha->max_cmds / 2;
1300                 if (ha->enq->ucLogDriveCount <= 2)
1301                         min = ha->max_cmds - 1;
1302                 scsi_adjust_queue_depth(SDptr, MSG_ORDERED_TAG, min);
1303         }
1304         return 0;
1305 }
1306 #endif
1307
1308 /****************************************************************************/
1309 /*                                                                          */
1310 /* Routine Name: do_ipsintr                                                 */
1311 /*                                                                          */
1312 /* Routine Description:                                                     */
1313 /*                                                                          */
1314 /*   Wrapper for the interrupt handler                                      */
1315 /*                                                                          */
1316 /****************************************************************************/
1317 irqreturn_t
1318 do_ipsintr(int irq, void *dev_id, struct pt_regs * regs)
1319 {
1320         ips_ha_t *ha;
1321         unsigned long cpu_flags;
1322         struct Scsi_Host *host;
1323         int irqstatus;
1324
1325         METHOD_TRACE("do_ipsintr", 2);
1326
1327         ha = (ips_ha_t *) dev_id;
1328         if (!ha)
1329                 return IRQ_NONE;
1330         host = ips_sh[ha->host_num];
1331         /* interrupt during initialization */
1332         if (!host) {
1333                 (*ha->func.intr) (ha);
1334                 return IRQ_HANDLED;
1335         }
1336
1337         IPS_LOCK_SAVE(host->host_lock, cpu_flags);
1338
1339         if (!ha->active) {
1340                 IPS_UNLOCK_RESTORE(host->host_lock, cpu_flags);
1341                 return IRQ_HANDLED;
1342         }
1343
1344         irqstatus = (*ha->func.intr) (ha);
1345
1346         IPS_UNLOCK_RESTORE(host->host_lock, cpu_flags);
1347
1348         /* start the next command */
1349         ips_next(ha, IPS_INTR_ON);
1350         return IRQ_RETVAL(irqstatus);
1351 }
1352
1353 /****************************************************************************/
1354 /*                                                                          */
1355 /* Routine Name: ips_intr_copperhead                                        */
1356 /*                                                                          */
1357 /* Routine Description:                                                     */
1358 /*                                                                          */
1359 /*   Polling interrupt handler                                              */
1360 /*                                                                          */
1361 /*   ASSUMES interrupts are disabled                                        */
1362 /*                                                                          */
1363 /****************************************************************************/
1364 int
1365 ips_intr_copperhead(ips_ha_t * ha)
1366 {
1367         ips_stat_t *sp;
1368         ips_scb_t *scb;
1369         IPS_STATUS cstatus;
1370         int intrstatus;
1371
1372         METHOD_TRACE("ips_intr", 2);
1373
1374         if (!ha)
1375                 return 0;
1376
1377         if (!ha->active)
1378                 return 0;
1379
1380         intrstatus = (*ha->func.isintr) (ha);
1381
1382         if (!intrstatus) {
1383                 /*
1384                  * Unexpected/Shared interrupt
1385                  */
1386
1387                 return 0;
1388         }
1389
1390         while (TRUE) {
1391                 sp = &ha->sp;
1392
1393                 intrstatus = (*ha->func.isintr) (ha);
1394
1395                 if (!intrstatus)
1396                         break;
1397                 else
1398                         cstatus.value = (*ha->func.statupd) (ha);
1399
1400                 if (cstatus.fields.command_id > (IPS_MAX_CMDS - 1)) {
1401                         /* Spurious Interupt ? */
1402                         continue;
1403                 }
1404
1405                 ips_chkstatus(ha, &cstatus);
1406                 scb = (ips_scb_t *) sp->scb_addr;
1407
1408                 /*
1409                  * use the callback function to finish things up
1410                  * NOTE: interrupts are OFF for this
1411                  */
1412                 (*scb->callback) (ha, scb);
1413         }                       /* end while */
1414         return 1;
1415 }
1416
1417 /****************************************************************************/
1418 /*                                                                          */
1419 /* Routine Name: ips_intr_morpheus                                          */
1420 /*                                                                          */
1421 /* Routine Description:                                                     */
1422 /*                                                                          */
1423 /*   Polling interrupt handler                                              */
1424 /*                                                                          */
1425 /*   ASSUMES interrupts are disabled                                        */
1426 /*                                                                          */
1427 /****************************************************************************/
1428 int
1429 ips_intr_morpheus(ips_ha_t * ha)
1430 {
1431         ips_stat_t *sp;
1432         ips_scb_t *scb;
1433         IPS_STATUS cstatus;
1434         int intrstatus;
1435
1436         METHOD_TRACE("ips_intr_morpheus", 2);
1437
1438         if (!ha)
1439                 return 0;
1440
1441         if (!ha->active)
1442                 return 0;
1443
1444         intrstatus = (*ha->func.isintr) (ha);
1445
1446         if (!intrstatus) {
1447                 /*
1448                  * Unexpected/Shared interrupt
1449                  */
1450
1451                 return 0;
1452         }
1453
1454         while (TRUE) {
1455                 sp = &ha->sp;
1456
1457                 intrstatus = (*ha->func.isintr) (ha);
1458
1459                 if (!intrstatus)
1460                         break;
1461                 else
1462                         cstatus.value = (*ha->func.statupd) (ha);
1463
1464                 if (cstatus.value == 0xffffffff)
1465                         /* No more to process */
1466                         break;
1467
1468                 if (cstatus.fields.command_id > (IPS_MAX_CMDS - 1)) {
1469                         IPS_PRINTK(KERN_WARNING, ha->pcidev,
1470                                    "Spurious interrupt; no ccb.\n");
1471
1472                         continue;
1473                 }
1474
1475                 ips_chkstatus(ha, &cstatus);
1476                 scb = (ips_scb_t *) sp->scb_addr;
1477
1478                 /*
1479                  * use the callback function to finish things up
1480                  * NOTE: interrupts are OFF for this
1481                  */
1482                 (*scb->callback) (ha, scb);
1483         }                       /* end while */
1484         return 1;
1485 }
1486
1487 /****************************************************************************/
1488 /*                                                                          */
1489 /* Routine Name: ips_info                                                   */
1490 /*                                                                          */
1491 /* Routine Description:                                                     */
1492 /*                                                                          */
1493 /*   Return info about the driver                                           */
1494 /*                                                                          */
1495 /****************************************************************************/
1496 const char *
1497 ips_info(struct Scsi_Host *SH)
1498 {
1499         static char buffer[256];
1500         char *bp;
1501         ips_ha_t *ha;
1502
1503         METHOD_TRACE("ips_info", 1);
1504
1505         ha = IPS_HA(SH);
1506
1507         if (!ha)
1508                 return (NULL);
1509
1510         bp = &buffer[0];
1511         memset(bp, 0, sizeof (buffer));
1512
1513         sprintf(bp, "%s%s%s Build %d", "IBM PCI ServeRAID ",
1514                 IPS_VERSION_HIGH, IPS_VERSION_LOW, IPS_BUILD_IDENT);
1515
1516         if (ha->ad_type > 0 && ha->ad_type <= MAX_ADAPTER_NAME) {
1517                 strcat(bp, " <");
1518                 strcat(bp, ips_adapter_name[ha->ad_type - 1]);
1519                 strcat(bp, ">");
1520         }
1521
1522         return (bp);
1523 }
1524
1525 /****************************************************************************/
1526 /*                                                                          */
1527 /* Routine Name: ips_proc_info                                              */
1528 /*                                                                          */
1529 /* Routine Description:                                                     */
1530 /*                                                                          */
1531 /*   The passthru interface for the driver                                  */
1532 /*                                                                          */
1533 /****************************************************************************/
1534 int
1535 ips_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
1536               int length, int func)
1537 {
1538         int i;
1539         int ret;
1540         ips_ha_t *ha = NULL;
1541
1542         METHOD_TRACE("ips_proc_info", 1);
1543
1544         /* Find our host structure */
1545         for (i = 0; i < ips_next_controller; i++) {
1546                 if (ips_sh[i]) {
1547                         if (ips_sh[i] == host) {
1548                                 ha = (ips_ha_t *) ips_sh[i]->hostdata;
1549                                 break;
1550                         }
1551                 }
1552         }
1553
1554         if (!ha)
1555                 return (-EINVAL);
1556
1557         if (func) {
1558                 /* write */
1559                 return (0);
1560         } else {
1561                 /* read */
1562                 if (start)
1563                         *start = buffer;
1564
1565                 ret = ips_host_info(ha, buffer, offset, length);
1566
1567                 return (ret);
1568         }
1569 }
1570
1571 /*--------------------------------------------------------------------------*/
1572 /* Helper Functions                                                         */
1573 /*--------------------------------------------------------------------------*/
1574
1575 /****************************************************************************/
1576 /*                                                                          */
1577 /* Routine Name: ips_is_passthru                                            */
1578 /*                                                                          */
1579 /* Routine Description:                                                     */
1580 /*                                                                          */
1581 /*   Determine if the specified SCSI command is really a passthru command   */
1582 /*                                                                          */
1583 /****************************************************************************/
1584 static int
1585 ips_is_passthru(Scsi_Cmnd * SC)
1586 {
1587         METHOD_TRACE("ips_is_passthru", 1);
1588
1589         if (!SC)
1590                 return (0);
1591
1592         if ((SC->cmnd[0] == IPS_IOCTL_COMMAND) &&
1593             (SC->device->channel == 0) &&
1594             (SC->device->id == IPS_ADAPTER_ID) &&
1595             (SC->device->lun == 0) && SC->request_buffer) {
1596                 if ((!SC->use_sg) && SC->request_bufflen &&
1597                     (((char *) SC->request_buffer)[0] == 'C') &&
1598                     (((char *) SC->request_buffer)[1] == 'O') &&
1599                     (((char *) SC->request_buffer)[2] == 'P') &&
1600                     (((char *) SC->request_buffer)[3] == 'P'))
1601                         return 1;
1602                 else if (SC->use_sg) {
1603                         struct scatterlist *sg = SC->request_buffer;
1604                         char *buffer = IPS_SG_ADDRESS(sg);
1605                         if (buffer && buffer[0] == 'C' && buffer[1] == 'O' &&
1606                             buffer[2] == 'P' && buffer[3] == 'P')
1607                                 return 1;
1608                 }
1609         }
1610         return 0;
1611 }
1612
1613 /****************************************************************************/
1614 /*                                                                          */
1615 /* Routine Name: ips_alloc_passthru_buffer                                  */
1616 /*                                                                          */
1617 /* Routine Description:                                                     */
1618 /*   allocate a buffer large enough for the ioctl data if the ioctl buffer  */
1619 /*   is too small or doesn't exist                                          */
1620 /****************************************************************************/
1621 static int
1622 ips_alloc_passthru_buffer(ips_ha_t * ha, int length)
1623 {
1624         void *bigger_buf;
1625         dma_addr_t dma_busaddr;
1626
1627         if (ha->ioctl_data && length <= ha->ioctl_len)
1628                 return 0;
1629         /* there is no buffer or it's not big enough, allocate a new one */
1630         bigger_buf = pci_alloc_consistent(ha->pcidev, length, &dma_busaddr);
1631         if (bigger_buf) {
1632                 /* free the old memory */
1633                 pci_free_consistent(ha->pcidev, ha->ioctl_len, ha->ioctl_data,
1634                                     ha->ioctl_busaddr);
1635                 /* use the new memory */
1636                 ha->ioctl_data = (char *) bigger_buf;
1637                 ha->ioctl_len = length;
1638                 ha->ioctl_busaddr = dma_busaddr;
1639         } else {
1640                 return -1;
1641         }
1642         return 0;
1643 }
1644
1645 /****************************************************************************/
1646 /*                                                                          */
1647 /* Routine Name: ips_make_passthru                                          */
1648 /*                                                                          */
1649 /* Routine Description:                                                     */
1650 /*                                                                          */
1651 /*   Make a passthru command out of the info in the Scsi block              */
1652 /*                                                                          */
1653 /****************************************************************************/
1654 static int
1655 ips_make_passthru(ips_ha_t * ha, Scsi_Cmnd * SC, ips_scb_t * scb, int intr)
1656 {
1657         ips_passthru_t *pt;
1658         int length = 0;
1659         int ret;
1660
1661         METHOD_TRACE("ips_make_passthru", 1);
1662
1663         if (!SC->use_sg) {
1664                 length = SC->request_bufflen;
1665         } else {
1666                 struct scatterlist *sg = SC->request_buffer;
1667                 int i;
1668                 for (i = 0; i < SC->use_sg; i++)
1669                         length += sg[i].length;
1670         }
1671         if (length < sizeof (ips_passthru_t)) {
1672                 /* wrong size */
1673                 DEBUG_VAR(1, "(%s%d) Passthru structure wrong size",
1674                           ips_name, ha->host_num);
1675                 return (IPS_FAILURE);
1676         }
1677         if (ips_alloc_passthru_buffer(ha, length)) {
1678                 /* allocation failure!  If ha->ioctl_data exists, use it to return
1679                    some error codes.  Return a failed command to the scsi layer. */
1680                 if (ha->ioctl_data) {
1681                         pt = (ips_passthru_t *) ha->ioctl_data;
1682                         ips_scmd_buf_read(SC, pt, sizeof (ips_passthru_t));
1683                         pt->BasicStatus = 0x0B;
1684                         pt->ExtendedStatus = 0x00;
1685                         ips_scmd_buf_write(SC, pt, sizeof (ips_passthru_t));
1686                 }
1687                 return IPS_FAILURE;
1688         }
1689         ha->ioctl_datasize = length;
1690
1691         ips_scmd_buf_read(SC, ha->ioctl_data, ha->ioctl_datasize);
1692         pt = (ips_passthru_t *) ha->ioctl_data;
1693
1694         /*
1695          * Some notes about the passthru interface used
1696          *
1697          * IF the scsi op_code == 0x0d then we assume
1698          * that the data came along with/goes with the
1699          * packet we received from the sg driver. In this
1700          * case the CmdBSize field of the pt structure is
1701          * used for the size of the buffer.
1702          */
1703
1704         switch (pt->CoppCmd) {
1705         case IPS_NUMCTRLS:
1706                 memcpy(ha->ioctl_data + sizeof (ips_passthru_t),
1707                        &ips_num_controllers, sizeof (int));
1708                 ips_scmd_buf_write(SC, ha->ioctl_data,
1709                                    sizeof (ips_passthru_t) + sizeof (int));
1710                 SC->result = DID_OK << 16;
1711
1712                 return (IPS_SUCCESS_IMM);
1713
1714         case IPS_COPPUSRCMD:
1715         case IPS_COPPIOCCMD:
1716                 if (SC->cmnd[0] == IPS_IOCTL_COMMAND) {
1717                         if (length < (sizeof (ips_passthru_t) + pt->CmdBSize)) {
1718                                 /* wrong size */
1719                                 DEBUG_VAR(1,
1720                                           "(%s%d) Passthru structure wrong size",
1721                                           ips_name, ha->host_num);
1722
1723                                 return (IPS_FAILURE);
1724                         }
1725
1726                         if (ha->device_id == IPS_DEVICEID_COPPERHEAD &&
1727                             pt->CoppCP.cmd.flashfw.op_code ==
1728                             IPS_CMD_RW_BIOSFW) {
1729                                 ret = ips_flash_copperhead(ha, pt, scb);
1730                                 ips_scmd_buf_write(SC, ha->ioctl_data,
1731                                                    sizeof (ips_passthru_t));
1732                                 return ret;
1733                         }
1734                         if (ips_usrcmd(ha, pt, scb))
1735                                 return (IPS_SUCCESS);
1736                         else
1737                                 return (IPS_FAILURE);
1738                 }
1739
1740                 break;
1741
1742         }                       /* end switch */
1743
1744         return (IPS_FAILURE);
1745 }
1746
1747 /****************************************************************************/
1748 /* Routine Name: ips_flash_copperhead                                       */
1749 /* Routine Description:                                                     */
1750 /*   Flash the BIOS/FW on a Copperhead style controller                     */
1751 /****************************************************************************/
1752 static int
1753 ips_flash_copperhead(ips_ha_t * ha, ips_passthru_t * pt, ips_scb_t * scb)
1754 {
1755         int datasize;
1756
1757         /* Trombone is the only copperhead that can do packet flash, but only
1758          * for firmware. No one said it had to make sence. */
1759         if (IPS_IS_TROMBONE(ha) && pt->CoppCP.cmd.flashfw.type == IPS_FW_IMAGE) {
1760                 if (ips_usrcmd(ha, pt, scb))
1761                         return IPS_SUCCESS;
1762                 else
1763                         return IPS_FAILURE;
1764         }
1765         pt->BasicStatus = 0x0B;
1766         pt->ExtendedStatus = 0;
1767         scb->scsi_cmd->result = DID_OK << 16;
1768         /* IF it's OK to Use the "CD BOOT" Flash Buffer, then you can     */
1769         /* avoid allocating a huge buffer per adapter ( which can fail ). */
1770         if (pt->CoppCP.cmd.flashfw.type == IPS_BIOS_IMAGE &&
1771             pt->CoppCP.cmd.flashfw.direction == IPS_ERASE_BIOS) {
1772                 pt->BasicStatus = 0;
1773                 return ips_flash_bios(ha, pt, scb);
1774         } else if (pt->CoppCP.cmd.flashfw.packet_num == 0) {
1775                 if (ips_FlashData && !test_and_set_bit(0, &ips_FlashDataInUse)){
1776                         ha->flash_data = ips_FlashData;
1777                         ha->flash_busaddr = ips_flashbusaddr;
1778                         ha->flash_len = PAGE_SIZE << 7;
1779                         ha->flash_datasize = 0;
1780                 } else if (!ha->flash_data) {
1781                         datasize = pt->CoppCP.cmd.flashfw.total_packets *
1782                             pt->CoppCP.cmd.flashfw.count;
1783                         ha->flash_data = pci_alloc_consistent(ha->pcidev,
1784                                                               datasize,
1785                                                               &ha->flash_busaddr);
1786                         if (!ha->flash_data){
1787                                 printk(KERN_WARNING "Unable to allocate a flash buffer\n");
1788                                 return IPS_FAILURE;
1789                         }
1790                         ha->flash_datasize = 0;
1791                         ha->flash_len = datasize;
1792                 } else
1793                         return IPS_FAILURE;
1794         } else {
1795                 if (pt->CoppCP.cmd.flashfw.count + ha->flash_datasize >
1796                     ha->flash_len) {
1797                         ips_free_flash_copperhead(ha);
1798                         IPS_PRINTK(KERN_WARNING, ha->pcidev,
1799                                    "failed size sanity check\n");
1800                         return IPS_FAILURE;
1801                 }
1802         }
1803         if (!ha->flash_data)
1804                 return IPS_FAILURE;
1805         pt->BasicStatus = 0;
1806         memcpy(&ha->flash_data[ha->flash_datasize], pt + 1,
1807                pt->CoppCP.cmd.flashfw.count);
1808         ha->flash_datasize += pt->CoppCP.cmd.flashfw.count;
1809         if (pt->CoppCP.cmd.flashfw.packet_num ==
1810             pt->CoppCP.cmd.flashfw.total_packets - 1) {
1811                 if (pt->CoppCP.cmd.flashfw.type == IPS_BIOS_IMAGE)
1812                         return ips_flash_bios(ha, pt, scb);
1813                 else if (pt->CoppCP.cmd.flashfw.type == IPS_FW_IMAGE)
1814                         return ips_flash_firmware(ha, pt, scb);
1815         }
1816         return IPS_SUCCESS_IMM;
1817 }
1818
1819 /****************************************************************************/
1820 /* Routine Name: ips_flash_bios                                             */
1821 /* Routine Description:                                                     */
1822 /*   flashes the bios of a copperhead adapter                               */
1823 /****************************************************************************/
1824 static int
1825 ips_flash_bios(ips_ha_t * ha, ips_passthru_t * pt, ips_scb_t * scb)
1826 {
1827
1828         if (pt->CoppCP.cmd.flashfw.type == IPS_BIOS_IMAGE &&
1829             pt->CoppCP.cmd.flashfw.direction == IPS_WRITE_BIOS) {
1830                 if ((!ha->func.programbios) || (!ha->func.erasebios) ||
1831                     (!ha->func.verifybios))
1832                         goto error;
1833                 if ((*ha->func.erasebios) (ha)) {
1834                         DEBUG_VAR(1,
1835                                   "(%s%d) flash bios failed - unable to erase flash",
1836                                   ips_name, ha->host_num);
1837                         goto error;
1838                 } else
1839                     if ((*ha->func.programbios) (ha,
1840                                                  ha->flash_data +
1841                                                  IPS_BIOS_HEADER,
1842                                                  ha->flash_datasize -
1843                                                  IPS_BIOS_HEADER, 0)) {
1844                         DEBUG_VAR(1,
1845                                   "(%s%d) flash bios failed - unable to flash",
1846                                   ips_name, ha->host_num);
1847                         goto error;
1848                 } else
1849                     if ((*ha->func.verifybios) (ha,
1850                                                 ha->flash_data +
1851                                                 IPS_BIOS_HEADER,
1852                                                 ha->flash_datasize -
1853                                                 IPS_BIOS_HEADER, 0)) {
1854                         DEBUG_VAR(1,
1855                                   "(%s%d) flash bios failed - unable to verify flash",
1856                                   ips_name, ha->host_num);
1857                         goto error;
1858                 }
1859                 ips_free_flash_copperhead(ha);
1860                 return IPS_SUCCESS_IMM;
1861         } else if (pt->CoppCP.cmd.flashfw.type == IPS_BIOS_IMAGE &&
1862                    pt->CoppCP.cmd.flashfw.direction == IPS_ERASE_BIOS) {
1863                 if (!ha->func.erasebios)
1864                         goto error;
1865                 if ((*ha->func.erasebios) (ha)) {
1866                         DEBUG_VAR(1,
1867                                   "(%s%d) flash bios failed - unable to erase flash",
1868                                   ips_name, ha->host_num);
1869                         goto error;
1870                 }
1871                 return IPS_SUCCESS_IMM;
1872         }
1873       error:
1874         pt->BasicStatus = 0x0B;
1875         pt->ExtendedStatus = 0x00;
1876         ips_free_flash_copperhead(ha);
1877         return IPS_FAILURE;
1878 }
1879
1880 /****************************************************************************/
1881 /*                                                                          */
1882 /* Routine Name: ips_fill_scb_sg_single                                     */
1883 /*                                                                          */
1884 /* Routine Description:                                                     */
1885 /*   Fill in a single scb sg_list element from an address                   */
1886 /*   return a -1 if a breakup occurred                                      */
1887 /****************************************************************************/
1888 static inline int
1889 ips_fill_scb_sg_single(ips_ha_t * ha, dma_addr_t busaddr,
1890                        ips_scb_t * scb, int indx, unsigned int e_len)
1891 {
1892
1893         int ret_val = 0;
1894
1895         if ((scb->data_len + e_len) > ha->max_xfer) {
1896                 e_len = ha->max_xfer - scb->data_len;
1897                 scb->breakup = indx;
1898                 ++scb->sg_break;
1899                 ret_val = -1;
1900         } else {
1901                 scb->breakup = 0;
1902                 scb->sg_break = 0;
1903         }
1904         if (IPS_USE_ENH_SGLIST(ha)) {
1905                 scb->sg_list.enh_list[indx].address_lo =
1906                     cpu_to_le32(pci_dma_lo32(busaddr));
1907                 scb->sg_list.enh_list[indx].address_hi =
1908                     cpu_to_le32(pci_dma_hi32(busaddr));
1909                 scb->sg_list.enh_list[indx].length = cpu_to_le32(e_len);
1910         } else {
1911                 scb->sg_list.std_list[indx].address =
1912                     cpu_to_le32(pci_dma_lo32(busaddr));
1913                 scb->sg_list.std_list[indx].length = cpu_to_le32(e_len);
1914         }
1915
1916         ++scb->sg_len;
1917         scb->data_len += e_len;
1918         return ret_val;
1919 }
1920
1921 /****************************************************************************/
1922 /* Routine Name: ips_flash_firmware                                         */
1923 /* Routine Description:                                                     */
1924 /*   flashes the firmware of a copperhead adapter                           */
1925 /****************************************************************************/
1926 static int
1927 ips_flash_firmware(ips_ha_t * ha, ips_passthru_t * pt, ips_scb_t * scb)
1928 {
1929         IPS_SG_LIST sg_list;
1930         uint32_t cmd_busaddr;
1931
1932         if (pt->CoppCP.cmd.flashfw.type == IPS_FW_IMAGE &&
1933             pt->CoppCP.cmd.flashfw.direction == IPS_WRITE_FW) {
1934                 memset(&pt->CoppCP.cmd, 0, sizeof (IPS_HOST_COMMAND));
1935                 pt->CoppCP.cmd.flashfw.op_code = IPS_CMD_DOWNLOAD;
1936                 pt->CoppCP.cmd.flashfw.count = cpu_to_le32(ha->flash_datasize);
1937         } else {
1938                 pt->BasicStatus = 0x0B;
1939                 pt->ExtendedStatus = 0x00;
1940                 ips_free_flash_copperhead(ha);
1941                 return IPS_FAILURE;
1942         }
1943         /* Save the S/G list pointer so it doesn't get clobbered */
1944         sg_list.list = scb->sg_list.list;
1945         cmd_busaddr = scb->scb_busaddr;
1946         /* copy in the CP */
1947         memcpy(&scb->cmd, &pt->CoppCP.cmd, sizeof (IPS_IOCTL_CMD));
1948         /* FIX stuff that might be wrong */
1949         scb->sg_list.list = sg_list.list;
1950         scb->scb_busaddr = cmd_busaddr;
1951         scb->bus = scb->scsi_cmd->device->channel;
1952         scb->target_id = scb->scsi_cmd->device->id;
1953         scb->lun = scb->scsi_cmd->device->lun;
1954         scb->sg_len = 0;
1955         scb->data_len = 0;
1956         scb->flags = 0;
1957         scb->op_code = 0;
1958         scb->callback = ipsintr_done;
1959         scb->timeout = ips_cmd_timeout;
1960
1961         scb->data_len = ha->flash_datasize;
1962         scb->data_busaddr =
1963             pci_map_single(ha->pcidev, ha->flash_data, scb->data_len,
1964                            IPS_DMA_DIR(scb));
1965         scb->flags |= IPS_SCB_MAP_SINGLE;
1966         scb->cmd.flashfw.command_id = IPS_COMMAND_ID(ha, scb);
1967         scb->cmd.flashfw.buffer_addr = cpu_to_le32(scb->data_busaddr);
1968         if (pt->TimeOut)
1969                 scb->timeout = pt->TimeOut;
1970         scb->scsi_cmd->result = DID_OK << 16;
1971         return IPS_SUCCESS;
1972 }
1973
1974 /****************************************************************************/
1975 /* Routine Name: ips_free_flash_copperhead                                  */
1976 /* Routine Description:                                                     */
1977 /*   release the memory resources used to hold the flash image              */
1978 /****************************************************************************/
1979 static void
1980 ips_free_flash_copperhead(ips_ha_t * ha)
1981 {
1982         if (ha->flash_data == ips_FlashData)
1983                 test_and_clear_bit(0, &ips_FlashDataInUse);
1984         else if (ha->flash_data)
1985                 pci_free_consistent(ha->pcidev, ha->flash_len, ha->flash_data,
1986                                     ha->flash_busaddr);
1987         ha->flash_data = NULL;
1988 }
1989
1990 /****************************************************************************/
1991 /*                                                                          */
1992 /* Routine Name: ips_usrcmd                                                 */
1993 /*                                                                          */
1994 /* Routine Description:                                                     */
1995 /*                                                                          */
1996 /*   Process a user command and make it ready to send                       */
1997 /*                                                                          */
1998 /****************************************************************************/
1999 static int
2000 ips_usrcmd(ips_ha_t * ha, ips_passthru_t * pt, ips_scb_t * scb)
2001 {
2002         IPS_SG_LIST sg_list;
2003         uint32_t cmd_busaddr;
2004
2005         METHOD_TRACE("ips_usrcmd", 1);
2006
2007         if ((!scb) || (!pt) || (!ha))
2008                 return (0);
2009
2010         /* Save the S/G list pointer so it doesn't get clobbered */
2011         sg_list.list = scb->sg_list.list;
2012         cmd_busaddr = scb->scb_busaddr;
2013         /* copy in the CP */
2014         memcpy(&scb->cmd, &pt->CoppCP.cmd, sizeof (IPS_IOCTL_CMD));
2015         memcpy(&scb->dcdb, &pt->CoppCP.dcdb, sizeof (IPS_DCDB_TABLE));
2016
2017         /* FIX stuff that might be wrong */
2018         scb->sg_list.list = sg_list.list;
2019         scb->scb_busaddr = cmd_busaddr;
2020         scb->bus = scb->scsi_cmd->device->channel;
2021         scb->target_id = scb->scsi_cmd->device->id;
2022         scb->lun = scb->scsi_cmd->device->lun;
2023         scb->sg_len = 0;
2024         scb->data_len = 0;
2025         scb->flags = 0;
2026         scb->op_code = 0;
2027         scb->callback = ipsintr_done;
2028         scb->timeout = ips_cmd_timeout;
2029         scb->cmd.basic_io.command_id = IPS_COMMAND_ID(ha, scb);
2030
2031         /* we don't support DCDB/READ/WRITE Scatter Gather */
2032         if ((scb->cmd.basic_io.op_code == IPS_CMD_READ_SG) ||
2033             (scb->cmd.basic_io.op_code == IPS_CMD_WRITE_SG) ||
2034             (scb->cmd.basic_io.op_code == IPS_CMD_DCDB_SG))
2035                 return (0);
2036
2037         if (pt->CmdBSize) {
2038                 scb->data_len = pt->CmdBSize;
2039                 scb->data_busaddr = ha->ioctl_busaddr + sizeof (ips_passthru_t);
2040         } else {
2041                 scb->data_busaddr = 0L;
2042         }
2043
2044         if (scb->cmd.dcdb.op_code == IPS_CMD_DCDB)
2045                 scb->cmd.dcdb.dcdb_address = cpu_to_le32(scb->scb_busaddr +
2046                                                          (unsigned long) &scb->
2047                                                          dcdb -
2048                                                          (unsigned long) scb);
2049
2050         if (pt->CmdBSize) {
2051                 if (scb->cmd.dcdb.op_code == IPS_CMD_DCDB)
2052                         scb->dcdb.buffer_pointer =
2053                             cpu_to_le32(scb->data_busaddr);
2054                 else
2055                         scb->cmd.basic_io.sg_addr =
2056                             cpu_to_le32(scb->data_busaddr);
2057         }
2058
2059         /* set timeouts */
2060         if (pt->TimeOut) {
2061                 scb->timeout = pt->TimeOut;
2062
2063                 if (pt->TimeOut <= 10)
2064                         scb->dcdb.cmd_attribute |= IPS_TIMEOUT10;
2065                 else if (pt->TimeOut <= 60)
2066                         scb->dcdb.cmd_attribute |= IPS_TIMEOUT60;
2067                 else
2068                         scb->dcdb.cmd_attribute |= IPS_TIMEOUT20M;
2069         }
2070
2071         /* assume success */
2072         scb->scsi_cmd->result = DID_OK << 16;
2073
2074         /* success */
2075         return (1);
2076 }
2077
2078 /****************************************************************************/
2079 /*                                                                          */
2080 /* Routine Name: ips_cleanup_passthru                                       */
2081 /*                                                                          */
2082 /* Routine Description:                                                     */
2083 /*                                                                          */
2084 /*   Cleanup after a passthru command                                       */
2085 /*                                                                          */
2086 /****************************************************************************/
2087 static void
2088 ips_cleanup_passthru(ips_ha_t * ha, ips_scb_t * scb)
2089 {
2090         ips_passthru_t *pt;
2091
2092         METHOD_TRACE("ips_cleanup_passthru", 1);
2093
2094         if ((!scb) || (!scb->scsi_cmd) || (!scb->scsi_cmd->request_buffer)) {
2095                 DEBUG_VAR(1, "(%s%d) couldn't cleanup after passthru",
2096                           ips_name, ha->host_num);
2097
2098                 return;
2099         }
2100         pt = (ips_passthru_t *) ha->ioctl_data;
2101
2102         /* Copy data back to the user */
2103         if (scb->cmd.dcdb.op_code == IPS_CMD_DCDB)      /* Copy DCDB Back to Caller's Area */
2104                 memcpy(&pt->CoppCP.dcdb, &scb->dcdb, sizeof (IPS_DCDB_TABLE));
2105
2106         pt->BasicStatus = scb->basic_status;
2107         pt->ExtendedStatus = scb->extended_status;
2108         pt->AdapterType = ha->ad_type;
2109
2110         if (ha->device_id == IPS_DEVICEID_COPPERHEAD &&
2111             (scb->cmd.flashfw.op_code == IPS_CMD_DOWNLOAD ||
2112              scb->cmd.flashfw.op_code == IPS_CMD_RW_BIOSFW))
2113                 ips_free_flash_copperhead(ha);
2114
2115         ips_scmd_buf_write(scb->scsi_cmd, ha->ioctl_data, ha->ioctl_datasize);
2116 }
2117
2118 /****************************************************************************/
2119 /*                                                                          */
2120 /* Routine Name: ips_host_info                                              */
2121 /*                                                                          */
2122 /* Routine Description:                                                     */
2123 /*                                                                          */
2124 /*   The passthru interface for the driver                                  */
2125 /*                                                                          */
2126 /****************************************************************************/
2127 static int
2128 ips_host_info(ips_ha_t * ha, char *ptr, off_t offset, int len)
2129 {
2130         IPS_INFOSTR info;
2131
2132         METHOD_TRACE("ips_host_info", 1);
2133
2134         info.buffer = ptr;
2135         info.length = len;
2136         info.offset = offset;
2137         info.pos = 0;
2138         info.localpos = 0;
2139
2140         copy_info(&info, "\nIBM ServeRAID General Information:\n\n");
2141
2142         if ((le32_to_cpu(ha->nvram->signature) == IPS_NVRAM_P5_SIG) &&
2143             (le16_to_cpu(ha->nvram->adapter_type) != 0))
2144                 copy_info(&info, "\tController Type                   : %s\n",
2145                           ips_adapter_name[ha->ad_type - 1]);
2146         else
2147                 copy_info(&info,
2148                           "\tController Type                   : Unknown\n");
2149
2150         if (ha->io_addr)
2151                 copy_info(&info,
2152                           "\tIO region                         : 0x%lx (%d bytes)\n",
2153                           ha->io_addr, ha->io_len);
2154
2155         if (ha->mem_addr) {
2156                 copy_info(&info,
2157                           "\tMemory region                     : 0x%lx (%d bytes)\n",
2158                           ha->mem_addr, ha->mem_len);
2159                 copy_info(&info,
2160                           "\tShared memory address             : 0x%lx\n",
2161                           ha->mem_ptr);
2162         }
2163
2164         copy_info(&info, "\tIRQ number                        : %d\n", ha->irq);
2165
2166     /* For the Next 3 lines Check for Binary 0 at the end and don't include it if it's there. */
2167     /* That keeps everything happy for "text" operations on the proc file.                    */
2168
2169         if (le32_to_cpu(ha->nvram->signature) == IPS_NVRAM_P5_SIG) {
2170         if (ha->nvram->bios_low[3] == 0) { 
2171             copy_info(&info,
2172                                   "\tBIOS Version                      : %c%c%c%c%c%c%c\n",
2173                                   ha->nvram->bios_high[0], ha->nvram->bios_high[1],
2174                                   ha->nvram->bios_high[2], ha->nvram->bios_high[3],
2175                                   ha->nvram->bios_low[0], ha->nvram->bios_low[1],
2176                                   ha->nvram->bios_low[2]);
2177
2178         } else {
2179                     copy_info(&info,
2180                                   "\tBIOS Version                      : %c%c%c%c%c%c%c%c\n",
2181                                   ha->nvram->bios_high[0], ha->nvram->bios_high[1],
2182                                   ha->nvram->bios_high[2], ha->nvram->bios_high[3],
2183                                   ha->nvram->bios_low[0], ha->nvram->bios_low[1],
2184                                   ha->nvram->bios_low[2], ha->nvram->bios_low[3]);
2185         }
2186
2187     }
2188
2189     if (ha->enq->CodeBlkVersion[7] == 0) {
2190         copy_info(&info,
2191                           "\tFirmware Version                  : %c%c%c%c%c%c%c\n",
2192                           ha->enq->CodeBlkVersion[0], ha->enq->CodeBlkVersion[1],
2193                           ha->enq->CodeBlkVersion[2], ha->enq->CodeBlkVersion[3],
2194                           ha->enq->CodeBlkVersion[4], ha->enq->CodeBlkVersion[5],
2195                           ha->enq->CodeBlkVersion[6]);
2196     } else {
2197         copy_info(&info,
2198                           "\tFirmware Version                  : %c%c%c%c%c%c%c%c\n",
2199                           ha->enq->CodeBlkVersion[0], ha->enq->CodeBlkVersion[1],
2200                           ha->enq->CodeBlkVersion[2], ha->enq->CodeBlkVersion[3],
2201                           ha->enq->CodeBlkVersion[4], ha->enq->CodeBlkVersion[5],
2202                           ha->enq->CodeBlkVersion[6], ha->enq->CodeBlkVersion[7]);
2203     }
2204
2205     if (ha->enq->BootBlkVersion[7] == 0) {
2206         copy_info(&info,
2207                           "\tBoot Block Version                : %c%c%c%c%c%c%c\n",
2208                           ha->enq->BootBlkVersion[0], ha->enq->BootBlkVersion[1],
2209                           ha->enq->BootBlkVersion[2], ha->enq->BootBlkVersion[3],
2210                           ha->enq->BootBlkVersion[4], ha->enq->BootBlkVersion[5],
2211                           ha->enq->BootBlkVersion[6]);
2212     } else {
2213         copy_info(&info,
2214                           "\tBoot Block Version                : %c%c%c%c%c%c%c%c\n",
2215                           ha->enq->BootBlkVersion[0], ha->enq->BootBlkVersion[1],
2216                           ha->enq->BootBlkVersion[2], ha->enq->BootBlkVersion[3],
2217                           ha->enq->BootBlkVersion[4], ha->enq->BootBlkVersion[5],
2218                           ha->enq->BootBlkVersion[6], ha->enq->BootBlkVersion[7]);
2219     }
2220
2221         copy_info(&info, "\tDriver Version                    : %s%s\n",
2222                   IPS_VERSION_HIGH, IPS_VERSION_LOW);
2223
2224         copy_info(&info, "\tDriver Build                      : %d\n",
2225                   IPS_BUILD_IDENT);
2226
2227         copy_info(&info, "\tMax Physical Devices              : %d\n",
2228                   ha->enq->ucMaxPhysicalDevices);
2229         copy_info(&info, "\tMax Active Commands               : %d\n",
2230                   ha->max_cmds);
2231         copy_info(&info, "\tCurrent Queued Commands           : %d\n",
2232                   ha->scb_waitlist.count);
2233         copy_info(&info, "\tCurrent Active Commands           : %d\n",
2234                   ha->scb_activelist.count - ha->num_ioctl);
2235         copy_info(&info, "\tCurrent Queued PT Commands        : %d\n",
2236                   ha->copp_waitlist.count);
2237         copy_info(&info, "\tCurrent Active PT Commands        : %d\n",
2238                   ha->num_ioctl);
2239
2240         copy_info(&info, "\n");
2241
2242         return (info.localpos);
2243 }
2244
2245 /****************************************************************************/
2246 /*                                                                          */
2247 /* Routine Name: copy_mem_info                                              */
2248 /*                                                                          */
2249 /* Routine Description:                                                     */
2250 /*                                                                          */
2251 /*   Copy data into an IPS_INFOSTR structure                                */
2252 /*                                                                          */
2253 /****************************************************************************/
2254 static void
2255 copy_mem_info(IPS_INFOSTR * info, char *data, int len)
2256 {
2257         METHOD_TRACE("copy_mem_info", 1);
2258
2259         if (info->pos + len < info->offset) {
2260                 info->pos += len;
2261                 return;
2262         }
2263
2264         if (info->pos < info->offset) {
2265                 data += (info->offset - info->pos);
2266                 len -= (info->offset - info->pos);
2267                 info->pos += (info->offset - info->pos);
2268         }
2269
2270         if (info->localpos + len > info->length)
2271                 len = info->length - info->localpos;
2272
2273         if (len > 0) {
2274                 memcpy(info->buffer + info->localpos, data, len);
2275                 info->pos += len;
2276                 info->localpos += len;
2277         }
2278 }
2279
2280 /****************************************************************************/
2281 /*                                                                          */
2282 /* Routine Name: copy_info                                                  */
2283 /*                                                                          */
2284 /* Routine Description:                                                     */
2285 /*                                                                          */
2286 /*   printf style wrapper for an info structure                             */
2287 /*                                                                          */
2288 /****************************************************************************/
2289 static int
2290 copy_info(IPS_INFOSTR * info, char *fmt, ...)
2291 {
2292         va_list args;
2293         char buf[128];
2294         int len;
2295
2296         METHOD_TRACE("copy_info", 1);
2297
2298         va_start(args, fmt);
2299         len = vsprintf(buf, fmt, args);
2300         va_end(args);
2301
2302         copy_mem_info(info, buf, len);
2303
2304         return (len);
2305 }
2306
2307 /****************************************************************************/
2308 /*                                                                          */
2309 /* Routine Name: ips_identify_controller                                    */
2310 /*                                                                          */
2311 /* Routine Description:                                                     */
2312 /*                                                                          */
2313 /*   Identify this controller                                               */
2314 /*                                                                          */
2315 /****************************************************************************/
2316 static void
2317 ips_identify_controller(ips_ha_t * ha)
2318 {
2319         METHOD_TRACE("ips_identify_controller", 1);
2320
2321         switch (ha->device_id) {
2322         case IPS_DEVICEID_COPPERHEAD:
2323                 if (ha->revision_id <= IPS_REVID_SERVERAID) {
2324                         ha->ad_type = IPS_ADTYPE_SERVERAID;
2325                 } else if (ha->revision_id == IPS_REVID_SERVERAID2) {
2326                         ha->ad_type = IPS_ADTYPE_SERVERAID2;
2327                 } else if (ha->revision_id == IPS_REVID_NAVAJO) {
2328                         ha->ad_type = IPS_ADTYPE_NAVAJO;
2329                 } else if ((ha->revision_id == IPS_REVID_SERVERAID2)
2330                            && (ha->slot_num == 0)) {
2331                         ha->ad_type = IPS_ADTYPE_KIOWA;
2332                 } else if ((ha->revision_id >= IPS_REVID_CLARINETP1) &&
2333                            (ha->revision_id <= IPS_REVID_CLARINETP3)) {
2334                         if (ha->enq->ucMaxPhysicalDevices == 15)
2335                                 ha->ad_type = IPS_ADTYPE_SERVERAID3L;
2336                         else
2337                                 ha->ad_type = IPS_ADTYPE_SERVERAID3;
2338                 } else if ((ha->revision_id >= IPS_REVID_TROMBONE32) &&
2339                            (ha->revision_id <= IPS_REVID_TROMBONE64)) {
2340                         ha->ad_type = IPS_ADTYPE_SERVERAID4H;
2341                 }
2342                 break;
2343
2344         case IPS_DEVICEID_MORPHEUS:
2345                 switch (ha->subdevice_id) {
2346                 case IPS_SUBDEVICEID_4L:
2347                         ha->ad_type = IPS_ADTYPE_SERVERAID4L;
2348                         break;
2349
2350                 case IPS_SUBDEVICEID_4M:
2351                         ha->ad_type = IPS_ADTYPE_SERVERAID4M;
2352                         break;
2353
2354                 case IPS_SUBDEVICEID_4MX:
2355                         ha->ad_type = IPS_ADTYPE_SERVERAID4MX;
2356                         break;
2357
2358                 case IPS_SUBDEVICEID_4LX:
2359                         ha->ad_type = IPS_ADTYPE_SERVERAID4LX;
2360                         break;
2361
2362                 case IPS_SUBDEVICEID_5I2:
2363                         ha->ad_type = IPS_ADTYPE_SERVERAID5I2;
2364                         break;
2365
2366                 case IPS_SUBDEVICEID_5I1:
2367                         ha->ad_type = IPS_ADTYPE_SERVERAID5I1;
2368                         break;
2369                 }
2370
2371                 break;
2372
2373         case IPS_DEVICEID_MARCO:
2374                 switch (ha->subdevice_id) {
2375                 case IPS_SUBDEVICEID_6M:
2376                         ha->ad_type = IPS_ADTYPE_SERVERAID6M;
2377                         break;
2378                 case IPS_SUBDEVICEID_6I:
2379                         ha->ad_type = IPS_ADTYPE_SERVERAID6I;
2380                         break;
2381                 case IPS_SUBDEVICEID_7k:
2382                         ha->ad_type = IPS_ADTYPE_SERVERAID7k;
2383                         break;
2384                 case IPS_SUBDEVICEID_7M:
2385                         ha->ad_type = IPS_ADTYPE_SERVERAID7M;
2386                         break;
2387                 }
2388                 break;
2389         }
2390 }
2391
2392 /****************************************************************************/
2393 /*                                                                          */
2394 /* Routine Name: ips_get_bios_version                                       */
2395 /*                                                                          */
2396 /* Routine Description:                                                     */
2397 /*                                                                          */
2398 /*   Get the BIOS revision number                                           */
2399 /*                                                                          */
2400 /****************************************************************************/
2401 static void
2402 ips_get_bios_version(ips_ha_t * ha, int intr)
2403 {
2404         ips_scb_t *scb;
2405         int ret;
2406         uint8_t major;
2407         uint8_t minor;
2408         uint8_t subminor;
2409         uint8_t *buffer;
2410         char hexDigits[] =
2411             { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
2412      'D', 'E', 'F' };
2413
2414         METHOD_TRACE("ips_get_bios_version", 1);
2415
2416         major = 0;
2417         minor = 0;
2418
2419         strncpy(ha->bios_version, "       ?", 8);
2420
2421         if (ha->device_id == IPS_DEVICEID_COPPERHEAD) {
2422                 if (IPS_USE_MEMIO(ha)) {
2423                         /* Memory Mapped I/O */
2424
2425                         /* test 1st byte */
2426                         writel(0, ha->mem_ptr + IPS_REG_FLAP);
2427                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2428                                 udelay(25);     /* 25 us */
2429
2430                         if (readb(ha->mem_ptr + IPS_REG_FLDP) != 0x55)
2431                                 return;
2432
2433                         writel(1, ha->mem_ptr + IPS_REG_FLAP);
2434                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2435                                 udelay(25);     /* 25 us */
2436
2437                         if (readb(ha->mem_ptr + IPS_REG_FLDP) != 0xAA)
2438                                 return;
2439
2440                         /* Get Major version */
2441                         writel(0x1FF, ha->mem_ptr + IPS_REG_FLAP);
2442                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2443                                 udelay(25);     /* 25 us */
2444
2445                         major = readb(ha->mem_ptr + IPS_REG_FLDP);
2446
2447                         /* Get Minor version */
2448                         writel(0x1FE, ha->mem_ptr + IPS_REG_FLAP);
2449                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2450                                 udelay(25);     /* 25 us */
2451                         minor = readb(ha->mem_ptr + IPS_REG_FLDP);
2452
2453                         /* Get SubMinor version */
2454                         writel(0x1FD, ha->mem_ptr + IPS_REG_FLAP);
2455                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2456                                 udelay(25);     /* 25 us */
2457                         subminor = readb(ha->mem_ptr + IPS_REG_FLDP);
2458
2459                 } else {
2460                         /* Programmed I/O */
2461
2462                         /* test 1st byte */
2463                         outl(0, ha->io_addr + IPS_REG_FLAP);
2464                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2465                                 udelay(25);     /* 25 us */
2466
2467                         if (inb(ha->io_addr + IPS_REG_FLDP) != 0x55)
2468                                 return;
2469
2470                         outl(cpu_to_le32(1), ha->io_addr + IPS_REG_FLAP);
2471                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2472                                 udelay(25);     /* 25 us */
2473
2474                         if (inb(ha->io_addr + IPS_REG_FLDP) != 0xAA)
2475                                 return;
2476
2477                         /* Get Major version */
2478                         outl(cpu_to_le32(0x1FF), ha->io_addr + IPS_REG_FLAP);
2479                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2480                                 udelay(25);     /* 25 us */
2481
2482                         major = inb(ha->io_addr + IPS_REG_FLDP);
2483
2484                         /* Get Minor version */
2485                         outl(cpu_to_le32(0x1FE), ha->io_addr + IPS_REG_FLAP);
2486                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2487                                 udelay(25);     /* 25 us */
2488
2489                         minor = inb(ha->io_addr + IPS_REG_FLDP);
2490
2491                         /* Get SubMinor version */
2492                         outl(cpu_to_le32(0x1FD), ha->io_addr + IPS_REG_FLAP);
2493                         if (ha->revision_id == IPS_REVID_TROMBONE64)
2494                                 udelay(25);     /* 25 us */
2495
2496                         subminor = inb(ha->io_addr + IPS_REG_FLDP);
2497
2498                 }
2499         } else {
2500                 /* Morpheus Family - Send Command to the card */
2501
2502                 buffer = ha->ioctl_data;
2503
2504                 memset(buffer, 0, 0x1000);
2505
2506                 scb = &ha->scbs[ha->max_cmds - 1];
2507
2508                 ips_init_scb(ha, scb);
2509
2510                 scb->timeout = ips_cmd_timeout;
2511                 scb->cdb[0] = IPS_CMD_RW_BIOSFW;
2512
2513                 scb->cmd.flashfw.op_code = IPS_CMD_RW_BIOSFW;
2514                 scb->cmd.flashfw.command_id = IPS_COMMAND_ID(ha, scb);
2515                 scb->cmd.flashfw.type = 1;
2516                 scb->cmd.flashfw.direction = 0;
2517                 scb->cmd.flashfw.count = cpu_to_le32(0x800);
2518                 scb->cmd.flashfw.total_packets = 1;
2519                 scb->cmd.flashfw.packet_num = 0;
2520                 scb->data_len = 0x1000;
2521                 scb->cmd.flashfw.buffer_addr = ha->ioctl_busaddr;
2522
2523                 /* issue the command */
2524                 if (((ret =
2525                       ips_send_wait(ha, scb, ips_cmd_timeout,
2526                                     intr)) == IPS_FAILURE)
2527                     || (ret == IPS_SUCCESS_IMM)
2528                     || ((scb->basic_status & IPS_GSC_STATUS_MASK) > 1)) {
2529                         /* Error occurred */
2530
2531                         return;
2532                 }
2533
2534                 if ((buffer[0xC0] == 0x55) && (buffer[0xC1] == 0xAA)) {
2535                         major = buffer[0x1ff + 0xC0];   /* Offset 0x1ff after the header (0xc0) */
2536                         minor = buffer[0x1fe + 0xC0];   /* Offset 0x1fe after the header (0xc0) */
2537                         subminor = buffer[0x1fd + 0xC0];        /* Offset 0x1fd after the header (0xc0) */
2538                 } else {
2539                         return;
2540                 }
2541         }
2542
2543         ha->bios_version[0] = hexDigits[(major & 0xF0) >> 4];
2544         ha->bios_version[1] = '.';
2545         ha->bios_version[2] = hexDigits[major & 0x0F];
2546         ha->bios_version[3] = hexDigits[subminor];
2547         ha->bios_version[4] = '.';
2548         ha->bios_version[5] = hexDigits[(minor & 0xF0) >> 4];
2549         ha->bios_version[6] = hexDigits[minor & 0x0F];
2550         ha->bios_version[7] = 0;
2551 }
2552
2553 /****************************************************************************/
2554 /*                                                                          */
2555 /* Routine Name: ips_hainit                                                 */
2556 /*                                                                          */
2557 /* Routine Description:                                                     */
2558 /*                                                                          */
2559 /*   Initialize the controller                                              */
2560 /*                                                                          */
2561 /* NOTE: Assumes to be called from with a lock                              */
2562 /*                                                                          */
2563 /****************************************************************************/
2564 static int
2565 ips_hainit(ips_ha_t * ha)
2566 {
2567         int i;
2568         struct timeval tv;
2569
2570         METHOD_TRACE("ips_hainit", 1);
2571
2572         if (!ha)
2573                 return (0);
2574
2575         if (ha->func.statinit)
2576                 (*ha->func.statinit) (ha);
2577
2578         if (ha->func.enableint)
2579                 (*ha->func.enableint) (ha);
2580
2581         /* Send FFDC */
2582         ha->reset_count = 1;
2583         do_gettimeofday(&tv);
2584         ha->last_ffdc = tv.tv_sec;
2585         ips_ffdc_reset(ha, IPS_INTR_IORL);
2586
2587         if (!ips_read_config(ha, IPS_INTR_IORL)) {
2588                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
2589                            "unable to read config from controller.\n");
2590
2591                 return (0);
2592         }
2593         /* end if */
2594         if (!ips_read_adapter_status(ha, IPS_INTR_IORL)) {
2595                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
2596                            "unable to read controller status.\n");
2597
2598                 return (0);
2599         }
2600
2601         /* Identify this controller */
2602         ips_identify_controller(ha);
2603
2604         if (!ips_read_subsystem_parameters(ha, IPS_INTR_IORL)) {
2605                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
2606                            "unable to read subsystem parameters.\n");
2607
2608                 return (0);
2609         }
2610
2611         /* write nvram user page 5 */
2612         if (!ips_write_driver_status(ha, IPS_INTR_IORL)) {
2613                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
2614                            "unable to write driver info to controller.\n");
2615
2616                 return (0);
2617         }
2618
2619         /* If there are Logical Drives and a Reset Occurred, then an EraseStripeLock is Needed */
2620         if ((ha->conf->ucLogDriveCount > 0) && (ha->requires_esl == 1))
2621                 ips_clear_adapter(ha, IPS_INTR_IORL);
2622
2623         /* set limits on SID, LUN, BUS */
2624         ha->ntargets = IPS_MAX_TARGETS + 1;
2625         ha->nlun = 1;
2626         ha->nbus = (ha->enq->ucMaxPhysicalDevices / IPS_MAX_TARGETS) + 1;
2627
2628         switch (ha->conf->logical_drive[0].ucStripeSize) {
2629         case 4:
2630                 ha->max_xfer = 0x10000;
2631                 break;
2632
2633         case 5:
2634                 ha->max_xfer = 0x20000;
2635                 break;
2636
2637         case 6:
2638                 ha->max_xfer = 0x40000;
2639                 break;
2640
2641         case 7:
2642         default:
2643                 ha->max_xfer = 0x80000;
2644                 break;
2645         }
2646
2647         /* setup max concurrent commands */
2648         if (le32_to_cpu(ha->subsys->param[4]) & 0x1) {
2649                 /* Use the new method */
2650                 ha->max_cmds = ha->enq->ucConcurrentCmdCount;
2651         } else {
2652                 /* use the old method */
2653                 switch (ha->conf->logical_drive[0].ucStripeSize) {
2654                 case 4:
2655                         ha->max_cmds = 32;
2656                         break;
2657
2658                 case 5:
2659                         ha->max_cmds = 16;
2660                         break;
2661
2662                 case 6:
2663                         ha->max_cmds = 8;
2664                         break;
2665
2666                 case 7:
2667                 default:
2668                         ha->max_cmds = 4;
2669                         break;
2670                 }
2671         }
2672
2673         /* Limit the Active Commands on a Lite Adapter */
2674         if ((ha->ad_type == IPS_ADTYPE_SERVERAID3L) ||
2675             (ha->ad_type == IPS_ADTYPE_SERVERAID4L) ||
2676             (ha->ad_type == IPS_ADTYPE_SERVERAID4LX)) {
2677                 if ((ha->max_cmds > MaxLiteCmds) && (MaxLiteCmds))
2678                         ha->max_cmds = MaxLiteCmds;
2679         }
2680
2681         /* set controller IDs */
2682         ha->ha_id[0] = IPS_ADAPTER_ID;
2683         for (i = 1; i < ha->nbus; i++) {
2684                 ha->ha_id[i] = ha->conf->init_id[i - 1] & 0x1f;
2685                 ha->dcdb_active[i - 1] = 0;
2686         }
2687
2688         return (1);
2689 }
2690
2691 /****************************************************************************/
2692 /*                                                                          */
2693 /* Routine Name: ips_next                                                   */
2694 /*                                                                          */
2695 /* Routine Description:                                                     */
2696 /*                                                                          */
2697 /*   Take the next command off the queue and send it to the controller      */
2698 /*                                                                          */
2699 /****************************************************************************/
2700 static void
2701 ips_next(ips_ha_t * ha, int intr)
2702 {
2703         ips_scb_t *scb;
2704         Scsi_Cmnd *SC;
2705         Scsi_Cmnd *p;
2706         Scsi_Cmnd *q;
2707         ips_copp_wait_item_t *item;
2708         int ret;
2709         unsigned long cpu_flags = 0;
2710         struct Scsi_Host *host;
2711         METHOD_TRACE("ips_next", 1);
2712
2713         if (!ha)
2714                 return;
2715         host = ips_sh[ha->host_num];
2716         /*
2717          * Block access to the queue function so
2718          * this command won't time out
2719          */
2720         if (intr == IPS_INTR_ON)
2721                 IPS_LOCK_SAVE(host->host_lock, cpu_flags);
2722
2723         if ((ha->subsys->param[3] & 0x300000)
2724             && (ha->scb_activelist.count == 0)) {
2725                 struct timeval tv;
2726
2727                 do_gettimeofday(&tv);
2728
2729                 if (tv.tv_sec - ha->last_ffdc > IPS_SECS_8HOURS) {
2730                         ha->last_ffdc = tv.tv_sec;
2731                         ips_ffdc_time(ha);
2732                 }
2733         }
2734
2735         /*
2736          * Send passthru commands
2737          * These have priority over normal I/O
2738          * but shouldn't affect performance too much
2739          * since we limit the number that can be active
2740          * on the card at any one time
2741          */
2742         while ((ha->num_ioctl < IPS_MAX_IOCTL) &&
2743                (ha->copp_waitlist.head) && (scb = ips_getscb(ha))) {
2744
2745                 item = ips_removeq_copp_head(&ha->copp_waitlist);
2746                 ha->num_ioctl++;
2747                 if (intr == IPS_INTR_ON)
2748                         IPS_UNLOCK_RESTORE(host->host_lock, cpu_flags);
2749                 scb->scsi_cmd = item->scsi_cmd;
2750                 kfree(item);
2751
2752                 ret = ips_make_passthru(ha, scb->scsi_cmd, scb, intr);
2753
2754                 if (intr == IPS_INTR_ON)
2755                         IPS_LOCK_SAVE(host->host_lock, cpu_flags);
2756                 switch (ret) {
2757                 case IPS_FAILURE:
2758                         if (scb->scsi_cmd) {
2759                                 scb->scsi_cmd->result = DID_ERROR << 16;
2760                                 scb->scsi_cmd->scsi_done(scb->scsi_cmd);
2761                         }
2762
2763                         ips_freescb(ha, scb);
2764                         break;
2765                 case IPS_SUCCESS_IMM:
2766                         if (scb->scsi_cmd) {
2767                                 scb->scsi_cmd->result = DID_OK << 16;
2768                                 scb->scsi_cmd->scsi_done(scb->scsi_cmd);
2769                         }
2770
2771                         ips_freescb(ha, scb);
2772                         break;
2773                 default:
2774                         break;
2775                 }               /* end case */
2776
2777                 if (ret != IPS_SUCCESS) {
2778                         ha->num_ioctl--;
2779                         continue;
2780                 }
2781
2782                 ret = ips_send_cmd(ha, scb);
2783
2784                 if (ret == IPS_SUCCESS)
2785                         ips_putq_scb_head(&ha->scb_activelist, scb);
2786                 else
2787                         ha->num_ioctl--;
2788
2789                 switch (ret) {
2790                 case IPS_FAILURE:
2791                         if (scb->scsi_cmd) {
2792                                 scb->scsi_cmd->result = DID_ERROR << 16;
2793                         }
2794
2795                         ips_freescb(ha, scb);
2796                         break;
2797                 case IPS_SUCCESS_IMM:
2798                         ips_freescb(ha, scb);
2799                         break;
2800                 default:
2801                         break;
2802                 }               /* end case */
2803
2804         }
2805
2806         /*
2807          * Send "Normal" I/O commands
2808          */
2809
2810         p = ha->scb_waitlist.head;
2811         while ((p) && (scb = ips_getscb(ha))) {
2812                 if ((p->device->channel > 0)
2813                     && (ha->
2814                         dcdb_active[p->device->channel -
2815                                     1] & (1 << p->device->id))) {
2816                         ips_freescb(ha, scb);
2817                         p = (Scsi_Cmnd *) p->host_scribble;
2818                         continue;
2819                 }
2820
2821                 q = p;
2822                 SC = ips_removeq_wait(&ha->scb_waitlist, q);
2823
2824                 if (intr == IPS_INTR_ON)
2825                         IPS_UNLOCK_RESTORE(host->host_lock, cpu_flags); /* Unlock HA after command is taken off queue */
2826
2827                 SC->result = DID_OK;
2828                 SC->host_scribble = NULL;
2829
2830                 memset(SC->sense_buffer, 0, sizeof (SC->sense_buffer));
2831
2832                 scb->target_id = SC->device->id;
2833                 scb->lun = SC->device->lun;
2834                 scb->bus = SC->device->channel;
2835                 scb->scsi_cmd = SC;
2836                 scb->breakup = 0;
2837                 scb->data_len = 0;
2838                 scb->callback = ipsintr_done;
2839                 scb->timeout = ips_cmd_timeout;
2840                 memset(&scb->cmd, 0, 16);
2841
2842                 /* copy in the CDB */
2843                 memcpy(scb->cdb, SC->cmnd, SC->cmd_len);
2844
2845                 /* Now handle the data buffer */
2846                 if (SC->use_sg) {
2847                         struct scatterlist *sg;
2848                         int i;
2849
2850                         sg = SC->request_buffer;
2851                         scb->sg_count = pci_map_sg(ha->pcidev, sg, SC->use_sg,
2852                                                    scsi_to_pci_dma_dir(SC->
2853                                                                        sc_data_direction));
2854                         scb->flags |= IPS_SCB_MAP_SG;
2855                         for (i = 0; i < scb->sg_count; i++) {
2856                                 if (ips_fill_scb_sg_single
2857                                     (ha, sg_dma_address(&sg[i]), scb, i,
2858                                      sg_dma_len(&sg[i])) < 0)
2859                                         break;
2860                         }
2861                         scb->dcdb.transfer_length = scb->data_len;
2862                 } else {
2863                         if (SC->request_bufflen) {
2864                                 scb->data_busaddr =
2865                                     pci_map_single(ha->pcidev,
2866                                                    SC->request_buffer,
2867                                                    SC->request_bufflen,
2868                                                    scsi_to_pci_dma_dir(SC->
2869                                                                        sc_data_direction));
2870                                 scb->flags |= IPS_SCB_MAP_SINGLE;
2871                                 ips_fill_scb_sg_single(ha, scb->data_busaddr,
2872                                                        scb, 0,
2873                                                        SC->request_bufflen);
2874                                 scb->dcdb.transfer_length = scb->data_len;
2875                         } else {
2876                                 scb->data_busaddr = 0L;
2877                                 scb->sg_len = 0;
2878                                 scb->data_len = 0;
2879                                 scb->dcdb.transfer_length = 0;
2880                         }
2881
2882                 }
2883
2884                 scb->dcdb.cmd_attribute =
2885                     ips_command_direction[scb->scsi_cmd->cmnd[0]];
2886
2887         /* Allow a WRITE BUFFER Command to Have no Data */
2888         /* This is Used by Tape Flash Utilites          */
2889         if ((scb->scsi_cmd->cmnd[0] == WRITE_BUFFER) && (scb->data_len == 0)) 
2890             scb->dcdb.cmd_attribute = 0;                  
2891
2892                 if (!(scb->dcdb.cmd_attribute & 0x3))
2893                         scb->dcdb.transfer_length = 0;
2894
2895                 if (scb->data_len >= IPS_MAX_XFER) {
2896                         scb->dcdb.cmd_attribute |= IPS_TRANSFER64K;
2897                         scb->dcdb.transfer_length = 0;
2898                 }
2899                 if (intr == IPS_INTR_ON)
2900                         IPS_LOCK_SAVE(host->host_lock, cpu_flags);
2901
2902                 ret = ips_send_cmd(ha, scb);
2903
2904                 switch (ret) {
2905                 case IPS_SUCCESS:
2906                         ips_putq_scb_head(&ha->scb_activelist, scb);
2907                         break;
2908                 case IPS_FAILURE:
2909                         if (scb->scsi_cmd) {
2910                                 scb->scsi_cmd->result = DID_ERROR << 16;
2911                                 scb->scsi_cmd->scsi_done(scb->scsi_cmd);
2912                         }
2913
2914                         if (scb->bus)
2915                                 ha->dcdb_active[scb->bus - 1] &=
2916                                     ~(1 << scb->target_id);
2917
2918                         ips_freescb(ha, scb);
2919                         break;
2920                 case IPS_SUCCESS_IMM:
2921                         if (scb->scsi_cmd)
2922                                 scb->scsi_cmd->scsi_done(scb->scsi_cmd);
2923
2924                         if (scb->bus)
2925                                 ha->dcdb_active[scb->bus - 1] &=
2926                                     ~(1 << scb->target_id);
2927
2928                         ips_freescb(ha, scb);
2929                         break;
2930                 default:
2931                         break;
2932                 }               /* end case */
2933
2934                 p = (Scsi_Cmnd *) p->host_scribble;
2935
2936         }                       /* end while */
2937
2938         if (intr == IPS_INTR_ON)
2939                 IPS_UNLOCK_RESTORE(host->host_lock, cpu_flags);
2940 }
2941
2942 /****************************************************************************/
2943 /*                                                                          */
2944 /* Routine Name: ips_putq_scb_head                                          */
2945 /*                                                                          */
2946 /* Routine Description:                                                     */
2947 /*                                                                          */
2948 /*   Add an item to the head of the queue                                   */
2949 /*                                                                          */
2950 /* ASSUMED to be called from within the HA lock                             */
2951 /*                                                                          */
2952 /****************************************************************************/
2953 static inline void
2954 ips_putq_scb_head(ips_scb_queue_t * queue, ips_scb_t * item)
2955 {
2956         METHOD_TRACE("ips_putq_scb_head", 1);
2957
2958         if (!item)
2959                 return;
2960
2961         item->q_next = queue->head;
2962         queue->head = item;
2963
2964         if (!queue->tail)
2965                 queue->tail = item;
2966
2967         queue->count++;
2968 }
2969
2970 /****************************************************************************/
2971 /*                                                                          */
2972 /* Routine Name: ips_putq_scb_tail                                          */
2973 /*                                                                          */
2974 /* Routine Description:                                                     */
2975 /*                                                                          */
2976 /*   Add an item to the tail of the queue                                   */
2977 /*                                                                          */
2978 /* ASSUMED to be called from within the HA lock                             */
2979 /*                                                                          */
2980 /****************************************************************************/
2981 static inline void
2982 ips_putq_scb_tail(ips_scb_queue_t * queue, ips_scb_t * item)
2983 {
2984         METHOD_TRACE("ips_putq_scb_tail", 1);
2985
2986         if (!item)
2987                 return;
2988
2989         item->q_next = NULL;
2990
2991         if (queue->tail)
2992                 queue->tail->q_next = item;
2993
2994         queue->tail = item;
2995
2996         if (!queue->head)
2997                 queue->head = item;
2998
2999         queue->count++;
3000 }
3001
3002 /****************************************************************************/
3003 /*                                                                          */
3004 /* Routine Name: ips_removeq_scb_head                                       */
3005 /*                                                                          */
3006 /* Routine Description:                                                     */
3007 /*                                                                          */
3008 /*   Remove the head of the queue                                           */
3009 /*                                                                          */
3010 /* ASSUMED to be called from within the HA lock                             */
3011 /*                                                                          */
3012 /****************************************************************************/
3013 static inline ips_scb_t *
3014 ips_removeq_scb_head(ips_scb_queue_t * queue)
3015 {
3016         ips_scb_t *item;
3017
3018         METHOD_TRACE("ips_removeq_scb_head", 1);
3019
3020         item = queue->head;
3021
3022         if (!item) {
3023                 return (NULL);
3024         }
3025
3026         queue->head = item->q_next;
3027         item->q_next = NULL;
3028
3029         if (queue->tail == item)
3030                 queue->tail = NULL;
3031
3032         queue->count--;
3033
3034         return (item);
3035 }
3036
3037 /****************************************************************************/
3038 /*                                                                          */
3039 /* Routine Name: ips_removeq_scb                                            */
3040 /*                                                                          */
3041 /* Routine Description:                                                     */
3042 /*                                                                          */
3043 /*   Remove an item from a queue                                            */
3044 /*                                                                          */
3045 /* ASSUMED to be called from within the HA lock                             */
3046 /*                                                                          */
3047 /****************************************************************************/
3048 static inline ips_scb_t *
3049 ips_removeq_scb(ips_scb_queue_t * queue, ips_scb_t * item)
3050 {
3051         ips_scb_t *p;
3052
3053         METHOD_TRACE("ips_removeq_scb", 1);
3054
3055         if (!item)
3056                 return (NULL);
3057
3058         if (item == queue->head) {
3059                 return (ips_removeq_scb_head(queue));
3060         }
3061
3062         p = queue->head;
3063
3064         while ((p) && (item != p->q_next))
3065                 p = p->q_next;
3066
3067         if (p) {
3068                 /* found a match */
3069                 p->q_next = item->q_next;
3070
3071                 if (!item->q_next)
3072                         queue->tail = p;
3073
3074                 item->q_next = NULL;
3075                 queue->count--;
3076
3077                 return (item);
3078         }
3079
3080         return (NULL);
3081 }
3082
3083 /****************************************************************************/
3084 /*                                                                          */
3085 /* Routine Name: ips_putq_wait_head                                         */
3086 /*                                                                          */
3087 /* Routine Description:                                                     */
3088 /*                                                                          */
3089 /*   Add an item to the head of the queue                                   */
3090 /*                                                                          */
3091 /* ASSUMED to be called from within the HA lock                             */
3092 /*                                                                          */
3093 /****************************************************************************/
3094 static inline void
3095 ips_putq_wait_head(ips_wait_queue_t * queue, Scsi_Cmnd * item)
3096 {
3097         METHOD_TRACE("ips_putq_wait_head", 1);
3098
3099         if (!item)
3100                 return;
3101
3102         item->host_scribble = (char *) queue->head;
3103         queue->head = item;
3104
3105         if (!queue->tail)
3106                 queue->tail = item;
3107
3108         queue->count++;
3109 }
3110
3111 /****************************************************************************/
3112 /*                                                                          */
3113 /* Routine Name: ips_putq_wait_tail                                         */
3114 /*                                                                          */
3115 /* Routine Description:                                                     */
3116 /*                                                                          */
3117 /*   Add an item to the tail of the queue                                   */
3118 /*                                                                          */
3119 /* ASSUMED to be called from within the HA lock                             */
3120 /*                                                                          */
3121 /****************************************************************************/
3122 static inline void
3123 ips_putq_wait_tail(ips_wait_queue_t * queue, Scsi_Cmnd * item)
3124 {
3125         METHOD_TRACE("ips_putq_wait_tail", 1);
3126
3127         if (!item)
3128                 return;
3129
3130         item->host_scribble = NULL;
3131
3132         if (queue->tail)
3133                 queue->tail->host_scribble = (char *) item;
3134
3135         queue->tail = item;
3136
3137         if (!queue->head)
3138                 queue->head = item;
3139
3140         queue->count++;
3141 }
3142
3143 /****************************************************************************/
3144 /*                                                                          */
3145 /* Routine Name: ips_removeq_wait_head                                      */
3146 /*                                                                          */
3147 /* Routine Description:                                                     */
3148 /*                                                                          */
3149 /*   Remove the head of the queue                                           */
3150 /*                                                                          */
3151 /* ASSUMED to be called from within the HA lock                             */
3152 /*                                                                          */
3153 /****************************************************************************/
3154 static inline Scsi_Cmnd *
3155 ips_removeq_wait_head(ips_wait_queue_t * queue)
3156 {
3157         Scsi_Cmnd *item;
3158
3159         METHOD_TRACE("ips_removeq_wait_head", 1);
3160
3161         item = queue->head;
3162
3163         if (!item) {
3164                 return (NULL);
3165         }
3166
3167         queue->head = (Scsi_Cmnd *) item->host_scribble;
3168         item->host_scribble = NULL;
3169
3170         if (queue->tail == item)
3171                 queue->tail = NULL;
3172
3173         queue->count--;
3174
3175         return (item);
3176 }
3177
3178 /****************************************************************************/
3179 /*                                                                          */
3180 /* Routine Name: ips_removeq_wait                                           */
3181 /*                                                                          */
3182 /* Routine Description:                                                     */
3183 /*                                                                          */
3184 /*   Remove an item from a queue                                            */
3185 /*                                                                          */
3186 /* ASSUMED to be called from within the HA lock                             */
3187 /*                                                                          */
3188 /****************************************************************************/
3189 static inline Scsi_Cmnd *
3190 ips_removeq_wait(ips_wait_queue_t * queue, Scsi_Cmnd * item)
3191 {
3192         Scsi_Cmnd *p;
3193
3194         METHOD_TRACE("ips_removeq_wait", 1);
3195
3196         if (!item)
3197                 return (NULL);
3198
3199         if (item == queue->head) {
3200                 return (ips_removeq_wait_head(queue));
3201         }
3202
3203         p = queue->head;
3204
3205         while ((p) && (item != (Scsi_Cmnd *) p->host_scribble))
3206                 p = (Scsi_Cmnd *) p->host_scribble;
3207
3208         if (p) {
3209                 /* found a match */
3210                 p->host_scribble = item->host_scribble;
3211
3212                 if (!item->host_scribble)
3213                         queue->tail = p;
3214
3215                 item->host_scribble = NULL;
3216                 queue->count--;
3217
3218                 return (item);
3219         }
3220
3221         return (NULL);
3222 }
3223
3224 /****************************************************************************/
3225 /*                                                                          */
3226 /* Routine Name: ips_putq_copp_head                                         */
3227 /*                                                                          */
3228 /* Routine Description:                                                     */
3229 /*                                                                          */
3230 /*   Add an item to the head of the queue                                   */
3231 /*                                                                          */
3232 /* ASSUMED to be called from within the HA lock                             */
3233 /*                                                                          */
3234 /****************************************************************************/
3235 static inline void
3236 ips_putq_copp_head(ips_copp_queue_t * queue, ips_copp_wait_item_t * item)
3237 {
3238         METHOD_TRACE("ips_putq_copp_head", 1);
3239
3240         if (!item)
3241                 return;
3242
3243         item->next = queue->head;
3244         queue->head = item;
3245
3246         if (!queue->tail)
3247                 queue->tail = item;
3248
3249         queue->count++;
3250 }
3251
3252 /****************************************************************************/
3253 /*                                                                          */
3254 /* Routine Name: ips_putq_copp_tail                                         */
3255 /*                                                                          */
3256 /* Routine Description:                                                     */
3257 /*                                                                          */
3258 /*   Add an item to the tail of the queue                                   */
3259 /*                                                                          */
3260 /* ASSUMED to be called from within the HA lock                             */
3261 /*                                                                          */
3262 /****************************************************************************/
3263 static inline void
3264 ips_putq_copp_tail(ips_copp_queue_t * queue, ips_copp_wait_item_t * item)
3265 {
3266         METHOD_TRACE("ips_putq_copp_tail", 1);
3267
3268         if (!item)
3269                 return;
3270
3271         item->next = NULL;
3272
3273         if (queue->tail)
3274                 queue->tail->next = item;
3275
3276         queue->tail = item;
3277
3278         if (!queue->head)
3279                 queue->head = item;
3280
3281         queue->count++;
3282 }
3283
3284 /****************************************************************************/
3285 /*                                                                          */
3286 /* Routine Name: ips_removeq_copp_head                                      */
3287 /*                                                                          */
3288 /* Routine Description:                                                     */
3289 /*                                                                          */
3290 /*   Remove the head of the queue                                           */
3291 /*                                                                          */
3292 /* ASSUMED to be called from within the HA lock                             */
3293 /*                                                                          */
3294 /****************************************************************************/
3295 static inline ips_copp_wait_item_t *
3296 ips_removeq_copp_head(ips_copp_queue_t * queue)
3297 {
3298         ips_copp_wait_item_t *item;
3299
3300         METHOD_TRACE("ips_removeq_copp_head", 1);
3301
3302         item = queue->head;
3303
3304         if (!item) {
3305                 return (NULL);
3306         }
3307
3308         queue->head = item->next;
3309         item->next = NULL;
3310
3311         if (queue->tail == item)
3312                 queue->tail = NULL;
3313
3314         queue->count--;
3315
3316         return (item);
3317 }
3318
3319 /****************************************************************************/
3320 /*                                                                          */
3321 /* Routine Name: ips_removeq_copp                                           */
3322 /*                                                                          */
3323 /* Routine Description:                                                     */
3324 /*                                                                          */
3325 /*   Remove an item from a queue                                            */
3326 /*                                                                          */
3327 /* ASSUMED to be called from within the HA lock                             */
3328 /*                                                                          */
3329 /****************************************************************************/
3330 static inline ips_copp_wait_item_t *
3331 ips_removeq_copp(ips_copp_queue_t * queue, ips_copp_wait_item_t * item)
3332 {
3333         ips_copp_wait_item_t *p;
3334
3335         METHOD_TRACE("ips_removeq_copp", 1);
3336
3337         if (!item)
3338                 return (NULL);
3339
3340         if (item == queue->head) {
3341                 return (ips_removeq_copp_head(queue));
3342         }
3343
3344         p = queue->head;
3345
3346         while ((p) && (item != p->next))
3347                 p = p->next;
3348
3349         if (p) {
3350                 /* found a match */
3351                 p->next = item->next;
3352
3353                 if (!item->next)
3354                         queue->tail = p;
3355
3356                 item->next = NULL;
3357                 queue->count--;
3358
3359                 return (item);
3360         }
3361
3362         return (NULL);
3363 }
3364
3365 /****************************************************************************/
3366 /*                                                                          */
3367 /* Routine Name: ipsintr_blocking                                           */
3368 /*                                                                          */
3369 /* Routine Description:                                                     */
3370 /*                                                                          */
3371 /*   Finalize an interrupt for internal commands                            */
3372 /*                                                                          */
3373 /****************************************************************************/
3374 static void
3375 ipsintr_blocking(ips_ha_t * ha, ips_scb_t * scb)
3376 {
3377         METHOD_TRACE("ipsintr_blocking", 2);
3378
3379         ips_freescb(ha, scb);
3380         if ((ha->waitflag == TRUE) && (ha->cmd_in_progress == scb->cdb[0])) {
3381                 ha->waitflag = FALSE;
3382
3383                 return;
3384         }
3385 }
3386
3387 /****************************************************************************/
3388 /*                                                                          */
3389 /* Routine Name: ipsintr_done                                               */
3390 /*                                                                          */
3391 /* Routine Description:                                                     */
3392 /*                                                                          */
3393 /*   Finalize an interrupt for non-internal commands                        */
3394 /*                                                                          */
3395 /****************************************************************************/
3396 static void
3397 ipsintr_done(ips_ha_t * ha, ips_scb_t * scb)
3398 {
3399         METHOD_TRACE("ipsintr_done", 2);
3400
3401         if (!scb) {
3402                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
3403                            "Spurious interrupt; scb NULL.\n");
3404
3405                 return;
3406         }
3407
3408         if (scb->scsi_cmd == NULL) {
3409                 /* unexpected interrupt */
3410                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
3411                            "Spurious interrupt; scsi_cmd not set.\n");
3412
3413                 return;
3414         }
3415
3416         ips_done(ha, scb);
3417 }
3418
3419 /****************************************************************************/
3420 /*                                                                          */
3421 /* Routine Name: ips_done                                                   */
3422 /*                                                                          */
3423 /* Routine Description:                                                     */
3424 /*                                                                          */
3425 /*   Do housekeeping on completed commands                                  */
3426 /*  ASSUMED to be called form within the request lock                       */
3427 /****************************************************************************/
3428 static void
3429 ips_done(ips_ha_t * ha, ips_scb_t * scb)
3430 {
3431         int ret;
3432
3433         METHOD_TRACE("ips_done", 1);
3434
3435         if (!scb)
3436                 return;
3437
3438         if ((scb->scsi_cmd) && (ips_is_passthru(scb->scsi_cmd))) {
3439                 ips_cleanup_passthru(ha, scb);
3440                 ha->num_ioctl--;
3441         } else {
3442                 /*
3443                  * Check to see if this command had too much
3444                  * data and had to be broke up.  If so, queue
3445                  * the rest of the data and continue.
3446                  */
3447                 if ((scb->breakup) || (scb->sg_break)) {
3448                         /* we had a data breakup */
3449                         scb->data_len = 0;
3450
3451                         if (scb->sg_count) {
3452                                 /* S/G request */
3453                                 struct scatterlist *sg;
3454                                 int ips_sg_index = 0;
3455                                 int sg_dma_index;
3456
3457                                 sg = scb->scsi_cmd->request_buffer;
3458
3459                                 /* Spin forward to last dma chunk */
3460                                 sg_dma_index = scb->breakup;
3461
3462                                 /* Take care of possible partial on last chunk */
3463                                 ips_fill_scb_sg_single(ha,
3464                                                        sg_dma_address(&sg
3465                                                                       [sg_dma_index]),
3466                                                        scb, ips_sg_index++,
3467                                                        sg_dma_len(&sg
3468                                                                   [sg_dma_index]));
3469
3470                                 for (; sg_dma_index < scb->sg_count;
3471                                      sg_dma_index++) {
3472                                         if (ips_fill_scb_sg_single
3473                                             (ha,
3474                                              sg_dma_address(&sg[sg_dma_index]),
3475                                              scb, ips_sg_index++,
3476                                              sg_dma_len(&sg[sg_dma_index])) < 0)
3477                                                 break;
3478
3479                                 }
3480
3481                         } else {
3482                                 /* Non S/G Request */
3483                                 (void) ips_fill_scb_sg_single(ha,
3484                                                               scb->
3485                                                               data_busaddr +
3486                                                               (scb->sg_break *
3487                                                                ha->max_xfer),
3488                                                               scb, 0,
3489                                                               scb->scsi_cmd->
3490                                                               request_bufflen -
3491                                                               (scb->sg_break *
3492                                                                ha->max_xfer));
3493                         }
3494
3495                         scb->dcdb.transfer_length = scb->data_len;
3496                         scb->dcdb.cmd_attribute |=
3497                             ips_command_direction[scb->scsi_cmd->cmnd[0]];
3498
3499                         if (!(scb->dcdb.cmd_attribute & 0x3))
3500                                 scb->dcdb.transfer_length = 0;
3501
3502                         if (scb->data_len >= IPS_MAX_XFER) {
3503                                 scb->dcdb.cmd_attribute |= IPS_TRANSFER64K;
3504                                 scb->dcdb.transfer_length = 0;
3505                         }
3506
3507                         ret = ips_send_cmd(ha, scb);
3508
3509                         switch (ret) {
3510                         case IPS_FAILURE:
3511                                 if (scb->scsi_cmd) {
3512                                         scb->scsi_cmd->result = DID_ERROR << 16;
3513                                         scb->scsi_cmd->scsi_done(scb->scsi_cmd);
3514                                 }
3515
3516                                 ips_freescb(ha, scb);
3517                                 break;
3518                         case IPS_SUCCESS_IMM:
3519                                 if (scb->scsi_cmd) {
3520                                         scb->scsi_cmd->result = DID_ERROR << 16;
3521                                         scb->scsi_cmd->scsi_done(scb->scsi_cmd);
3522                                 }
3523
3524                                 ips_freescb(ha, scb);
3525                                 break;
3526                         default:
3527                                 break;
3528                         }       /* end case */
3529
3530                         return;
3531                 }
3532         }                       /* end if passthru */
3533
3534         if (scb->bus) {
3535                 ha->dcdb_active[scb->bus - 1] &= ~(1 << scb->target_id);
3536         }
3537
3538         scb->scsi_cmd->scsi_done(scb->scsi_cmd);
3539
3540         ips_freescb(ha, scb);
3541 }
3542
3543 /****************************************************************************/
3544 /*                                                                          */
3545 /* Routine Name: ips_map_status                                             */
3546 /*                                                                          */
3547 /* Routine Description:                                                     */
3548 /*                                                                          */
3549 /*   Map Controller Error codes to Linux Error Codes                        */
3550 /*                                                                          */
3551 /****************************************************************************/
3552 static int
3553 ips_map_status(ips_ha_t * ha, ips_scb_t * scb, ips_stat_t * sp)
3554 {
3555         int errcode;
3556         int device_error;
3557         uint32_t transfer_len;
3558         IPS_DCDB_TABLE_TAPE *tapeDCDB;
3559
3560         METHOD_TRACE("ips_map_status", 1);
3561
3562         if (scb->bus) {
3563                 DEBUG_VAR(2,
3564                           "(%s%d) Physical device error (%d %d %d): %x %x, Sense Key: %x, ASC: %x, ASCQ: %x",
3565                           ips_name, ha->host_num,
3566                           scb->scsi_cmd->device->channel,
3567                           scb->scsi_cmd->device->id, scb->scsi_cmd->device->lun,
3568                           scb->basic_status, scb->extended_status,
3569                           scb->extended_status ==
3570                           IPS_ERR_CKCOND ? scb->dcdb.sense_info[2] & 0xf : 0,
3571                           scb->extended_status ==
3572                           IPS_ERR_CKCOND ? scb->dcdb.sense_info[12] : 0,
3573                           scb->extended_status ==
3574                           IPS_ERR_CKCOND ? scb->dcdb.sense_info[13] : 0);
3575         }
3576
3577         /* default driver error */
3578         errcode = DID_ERROR;
3579         device_error = 0;
3580
3581         switch (scb->basic_status & IPS_GSC_STATUS_MASK) {
3582         case IPS_CMD_TIMEOUT:
3583                 errcode = DID_TIME_OUT;
3584                 break;
3585
3586         case IPS_INVAL_OPCO:
3587         case IPS_INVAL_CMD_BLK:
3588         case IPS_INVAL_PARM_BLK:
3589         case IPS_LD_ERROR:
3590         case IPS_CMD_CMPLT_WERROR:
3591                 break;
3592
3593         case IPS_PHYS_DRV_ERROR:
3594                 switch (scb->extended_status) {
3595                 case IPS_ERR_SEL_TO:
3596                         if (scb->bus)
3597                                 errcode = DID_NO_CONNECT;
3598
3599                         break;
3600
3601                 case IPS_ERR_OU_RUN:
3602                         if ((scb->cmd.dcdb.op_code == IPS_CMD_EXTENDED_DCDB) ||
3603                             (scb->cmd.dcdb.op_code ==
3604                              IPS_CMD_EXTENDED_DCDB_SG)) {
3605                                 tapeDCDB = (IPS_DCDB_TABLE_TAPE *) & scb->dcdb;
3606                                 transfer_len = tapeDCDB->transfer_length;
3607                         } else {
3608                                 transfer_len =
3609                                     (uint32_t) scb->dcdb.transfer_length;
3610                         }
3611
3612                         if ((scb->bus) && (transfer_len < scb->data_len)) {
3613                                 /* Underrun - set default to no error */
3614                                 errcode = DID_OK;
3615
3616                                 /* Restrict access to physical DASD */
3617                                 if ((scb->scsi_cmd->cmnd[0] == INQUIRY) &&
3618                                     ((((char *) scb->scsi_cmd->
3619                                        buffer)[0] & 0x1f) == TYPE_DISK)) {
3620                                         /* underflow -- no error               */
3621                                         /* restrict access to physical DASD    */
3622                                         errcode = DID_TIME_OUT;
3623                                         break;
3624                                 }
3625                         } else
3626                                 errcode = DID_ERROR;
3627
3628                         break;
3629
3630                 case IPS_ERR_RECOVERY:
3631                         /* don't fail recovered errors */
3632                         if (scb->bus)
3633                                 errcode = DID_OK;
3634
3635                         break;
3636
3637                 case IPS_ERR_HOST_RESET:
3638                 case IPS_ERR_DEV_RESET:
3639                         errcode = DID_RESET;
3640                         break;
3641
3642                 case IPS_ERR_CKCOND:
3643                         if (scb->bus) {
3644                                 if ((scb->cmd.dcdb.op_code ==
3645                                      IPS_CMD_EXTENDED_DCDB)
3646                                     || (scb->cmd.dcdb.op_code ==
3647                                         IPS_CMD_EXTENDED_DCDB_SG)) {
3648                                         tapeDCDB =
3649                                             (IPS_DCDB_TABLE_TAPE *) & scb->dcdb;
3650                                         memcpy(scb->scsi_cmd->sense_buffer,
3651                                                tapeDCDB->sense_info,
3652                                                sizeof (scb->scsi_cmd->
3653                                                        sense_buffer));
3654                                 } else {
3655                                         memcpy(scb->scsi_cmd->sense_buffer,
3656                                                scb->dcdb.sense_info,
3657                                                sizeof (scb->scsi_cmd->
3658                                                        sense_buffer));
3659                                 }
3660                                 device_error = 2;       /* check condition */
3661                         }
3662
3663                         errcode = DID_OK;
3664
3665                         break;
3666
3667                 default:
3668                         errcode = DID_ERROR;
3669                         break;
3670
3671                 }               /* end switch */
3672         }                       /* end switch */
3673
3674         scb->scsi_cmd->result = device_error | (errcode << 16);
3675
3676         return (1);
3677 }
3678
3679 /****************************************************************************/
3680 /*                                                                          */
3681 /* Routine Name: ips_send_wait                                              */
3682 /*                                                                          */
3683 /* Routine Description:                                                     */
3684 /*                                                                          */
3685 /*   Send a command to the controller and wait for it to return             */
3686 /*                                                                          */
3687 /*   The FFDC Time Stamp use this function for the callback, but doesn't    */
3688 /*   actually need to wait.                                                 */
3689 /****************************************************************************/
3690 static int
3691 ips_send_wait(ips_ha_t * ha, ips_scb_t * scb, int timeout, int intr)
3692 {
3693         int ret;
3694
3695         METHOD_TRACE("ips_send_wait", 1);
3696
3697         if (intr != IPS_FFDC) { /* Won't be Waiting if this is a Time Stamp */
3698                 ha->waitflag = TRUE;
3699                 ha->cmd_in_progress = scb->cdb[0];
3700         }
3701         scb->callback = ipsintr_blocking;
3702         ret = ips_send_cmd(ha, scb);
3703
3704         if ((ret == IPS_FAILURE) || (ret == IPS_SUCCESS_IMM))
3705                 return (ret);
3706
3707         if (intr != IPS_FFDC)   /* Don't Wait around if this is a Time Stamp */
3708                 ret = ips_wait(ha, timeout, intr);
3709
3710         return (ret);
3711 }
3712
3713 /****************************************************************************/
3714 /*                                                                          */
3715 /* Routine Name: ips_scmd_buf_write                                         */
3716 /*                                                                          */
3717 /* Routine Description:                                                     */
3718 /*  Write data to Scsi_Cmnd request_buffer at proper offsets                */
3719 /****************************************************************************/
3720 static void
3721 ips_scmd_buf_write(Scsi_Cmnd * scmd, void *data, unsigned
3722                    int count)
3723 {
3724         if (scmd->use_sg) {
3725                 int i;
3726                 unsigned int min_cnt, xfer_cnt;
3727                 char *cdata = (char *) data;
3728                 struct scatterlist *sg = scmd->request_buffer;
3729                 for (i = 0, xfer_cnt = 0;
3730                      (i < scmd->use_sg) && (xfer_cnt < count); i++) {
3731                         if (!IPS_SG_ADDRESS(&sg[i]))
3732                                 return;
3733                         min_cnt = min(count - xfer_cnt, sg[i].length);
3734                         memcpy(IPS_SG_ADDRESS(&sg[i]), &cdata[xfer_cnt],
3735                                min_cnt);
3736                         xfer_cnt += min_cnt;
3737                 }
3738
3739         } else {
3740                 unsigned int min_cnt = min(count, scmd->request_bufflen);
3741                 memcpy(scmd->request_buffer, data, min_cnt);
3742         }
3743 }
3744
3745 /****************************************************************************/
3746 /*                                                                          */
3747 /* Routine Name: ips_scmd_buf_read                                          */
3748 /*                                                                          */
3749 /* Routine Description:                                                     */
3750 /*  Copy data from a Scsi_Cmnd to a new, linear buffer                      */
3751 /****************************************************************************/
3752 static void
3753 ips_scmd_buf_read(Scsi_Cmnd * scmd, void *data, unsigned
3754                   int count)
3755 {
3756         if (scmd->use_sg) {
3757                 int i;
3758                 unsigned int min_cnt, xfer_cnt;
3759                 char *cdata = (char *) data;
3760                 struct scatterlist *sg = scmd->request_buffer;
3761                 for (i = 0, xfer_cnt = 0;
3762                      (i < scmd->use_sg) && (xfer_cnt < count); i++) {
3763                         if (!IPS_SG_ADDRESS(&sg[i]))
3764                                 return;
3765                         min_cnt = min(count - xfer_cnt, sg[i].length);
3766                         memcpy(&cdata[xfer_cnt], IPS_SG_ADDRESS(&sg[i]),
3767                                min_cnt);
3768                         xfer_cnt += min_cnt;
3769                 }
3770
3771         } else {
3772                 unsigned int min_cnt = min(count, scmd->request_bufflen);
3773                 memcpy(data, scmd->request_buffer, min_cnt);
3774         }
3775 }
3776
3777 /****************************************************************************/
3778 /*                                                                          */
3779 /* Routine Name: ips_send_cmd                                               */
3780 /*                                                                          */
3781 /* Routine Description:                                                     */
3782 /*                                                                          */
3783 /*   Map SCSI commands to ServeRAID commands for logical drives             */
3784 /*                                                                          */
3785 /****************************************************************************/
3786 static int
3787 ips_send_cmd(ips_ha_t * ha, ips_scb_t * scb)
3788 {
3789         int ret;
3790         char *sp;
3791         int device_error;
3792         IPS_DCDB_TABLE_TAPE *tapeDCDB;
3793         int TimeOut;
3794
3795         METHOD_TRACE("ips_send_cmd", 1);
3796
3797         ret = IPS_SUCCESS;
3798
3799         if (!scb->scsi_cmd) {
3800                 /* internal command */
3801
3802                 if (scb->bus > 0) {
3803                         /* Controller commands can't be issued */
3804                         /* to real devices -- fail them        */
3805                         if ((ha->waitflag == TRUE) &&
3806                             (ha->cmd_in_progress == scb->cdb[0])) {
3807                                 ha->waitflag = FALSE;
3808                         }
3809
3810                         return (1);
3811                 }
3812         } else if ((scb->bus == 0) && (!ips_is_passthru(scb->scsi_cmd))) {
3813                 /* command to logical bus -- interpret */
3814                 ret = IPS_SUCCESS_IMM;
3815
3816                 switch (scb->scsi_cmd->cmnd[0]) {
3817                 case ALLOW_MEDIUM_REMOVAL:
3818                 case REZERO_UNIT:
3819                 case ERASE:
3820                 case WRITE_FILEMARKS:
3821                 case SPACE:
3822                         scb->scsi_cmd->result = DID_ERROR << 16;
3823                         break;
3824
3825                 case START_STOP:
3826                         scb->scsi_cmd->result = DID_OK << 16;
3827
3828                 case TEST_UNIT_READY:
3829                 case INQUIRY:
3830                         if (scb->target_id == IPS_ADAPTER_ID) {
3831                                 /*
3832                                  * Either we have a TUR
3833                                  * or we have a SCSI inquiry
3834                                  */
3835                                 if (scb->scsi_cmd->cmnd[0] == TEST_UNIT_READY)
3836                                         scb->scsi_cmd->result = DID_OK << 16;
3837
3838                                 if (scb->scsi_cmd->cmnd[0] == INQUIRY) {
3839                                         IPS_SCSI_INQ_DATA inquiry;
3840
3841                                         memset(&inquiry, 0,
3842                                                sizeof (IPS_SCSI_INQ_DATA));
3843
3844                                         inquiry.DeviceType =
3845                                             IPS_SCSI_INQ_TYPE_PROCESSOR;
3846                                         inquiry.DeviceTypeQualifier =
3847                                             IPS_SCSI_INQ_LU_CONNECTED;
3848                                         inquiry.Version = IPS_SCSI_INQ_REV2;
3849                                         inquiry.ResponseDataFormat =
3850                                             IPS_SCSI_INQ_RD_REV2;
3851                                         inquiry.AdditionalLength = 31;
3852                                         inquiry.Flags[0] =
3853                                             IPS_SCSI_INQ_Address16;
3854                                         inquiry.Flags[1] =
3855                                             IPS_SCSI_INQ_WBus16 |
3856                                             IPS_SCSI_INQ_Sync;
3857                                         strncpy(inquiry.VendorId, "IBM     ",
3858                                                 8);
3859                                         strncpy(inquiry.ProductId,
3860                                                 "SERVERAID       ", 16);
3861                                         strncpy(inquiry.ProductRevisionLevel,
3862                                                 "1.00", 4);
3863
3864                                         ips_scmd_buf_write(scb->scsi_cmd,
3865                                                            &inquiry,
3866                                                            sizeof (inquiry));
3867
3868                                         scb->scsi_cmd->result = DID_OK << 16;
3869                                 }
3870                         } else {
3871                                 scb->cmd.logical_info.op_code = IPS_CMD_GET_LD_INFO;
3872                                 scb->cmd.logical_info.command_id = IPS_COMMAND_ID(ha, scb);
3873                                 scb->cmd.logical_info.reserved = 0;
3874                                 scb->cmd.logical_info.reserved2 = 0;
3875                                 scb->data_len = sizeof (IPS_LD_INFO);
3876                                 scb->data_busaddr = ha->logical_drive_info_dma_addr;
3877                                 scb->flags = 0;
3878                                 scb->cmd.logical_info.buffer_addr = scb->data_busaddr;
3879                                 ret = IPS_SUCCESS;
3880                         }
3881
3882                         break;
3883
3884                 case REQUEST_SENSE:
3885                         ips_reqsen(ha, scb);
3886                         scb->scsi_cmd->result = DID_OK << 16;
3887                         break;
3888
3889                 case READ_6:
3890                 case WRITE_6:
3891                         if (!scb->sg_len) {
3892                                 scb->cmd.basic_io.op_code =
3893                                     (scb->scsi_cmd->cmnd[0] ==
3894                                      READ_6) ? IPS_CMD_READ : IPS_CMD_WRITE;
3895                                 scb->cmd.basic_io.enhanced_sg = 0;
3896                                 scb->cmd.basic_io.sg_addr =
3897                                     cpu_to_le32(scb->data_busaddr);
3898                         } else {
3899                                 scb->cmd.basic_io.op_code =
3900                                     (scb->scsi_cmd->cmnd[0] ==
3901                                      READ_6) ? IPS_CMD_READ_SG :
3902                                     IPS_CMD_WRITE_SG;
3903                                 scb->cmd.basic_io.enhanced_sg =
3904                                     IPS_USE_ENH_SGLIST(ha) ? 0xFF : 0;
3905                                 scb->cmd.basic_io.sg_addr =
3906                                     cpu_to_le32(scb->sg_busaddr);
3907                         }
3908
3909                         scb->cmd.basic_io.segment_4G = 0;
3910                         scb->cmd.basic_io.command_id = IPS_COMMAND_ID(ha, scb);
3911                         scb->cmd.basic_io.log_drv = scb->target_id;
3912                         scb->cmd.basic_io.sg_count = scb->sg_len;
3913
3914                         if (scb->cmd.basic_io.lba)
3915                                 scb->cmd.basic_io.lba =
3916                                     cpu_to_le32(le32_to_cpu
3917                                                 (scb->cmd.basic_io.lba) +
3918                                                 le16_to_cpu(scb->cmd.basic_io.
3919                                                             sector_count));
3920                         else
3921                                 scb->cmd.basic_io.lba =
3922                                     (((scb->scsi_cmd->
3923                                        cmnd[1] & 0x1f) << 16) | (scb->scsi_cmd->
3924                                                                  cmnd[2] << 8) |
3925                                      (scb->scsi_cmd->cmnd[3]));
3926
3927                         scb->cmd.basic_io.sector_count =
3928                             cpu_to_le16(scb->data_len / IPS_BLKSIZE);
3929
3930                         if (le16_to_cpu(scb->cmd.basic_io.sector_count) == 0)
3931                                 scb->cmd.basic_io.sector_count =
3932                                     cpu_to_le16(256);
3933
3934                         ret = IPS_SUCCESS;
3935                         break;
3936
3937                 case READ_10:
3938                 case WRITE_10:
3939                         if (!scb->sg_len) {
3940                                 scb->cmd.basic_io.op_code =
3941                                     (scb->scsi_cmd->cmnd[0] ==
3942                                      READ_10) ? IPS_CMD_READ : IPS_CMD_WRITE;
3943                                 scb->cmd.basic_io.enhanced_sg = 0;
3944                                 scb->cmd.basic_io.sg_addr =
3945                                     cpu_to_le32(scb->data_busaddr);
3946                         } else {
3947                                 scb->cmd.basic_io.op_code =
3948                                     (scb->scsi_cmd->cmnd[0] ==
3949                                      READ_10) ? IPS_CMD_READ_SG :
3950                                     IPS_CMD_WRITE_SG;
3951                                 scb->cmd.basic_io.enhanced_sg =
3952                                     IPS_USE_ENH_SGLIST(ha) ? 0xFF : 0;
3953                                 scb->cmd.basic_io.sg_addr =
3954                                     cpu_to_le32(scb->sg_busaddr);
3955                         }
3956
3957                         scb->cmd.basic_io.segment_4G = 0;
3958                         scb->cmd.basic_io.command_id = IPS_COMMAND_ID(ha, scb);
3959                         scb->cmd.basic_io.log_drv = scb->target_id;
3960                         scb->cmd.basic_io.sg_count = scb->sg_len;
3961
3962                         if (scb->cmd.basic_io.lba)
3963                                 scb->cmd.basic_io.lba =
3964                                     cpu_to_le32(le32_to_cpu
3965                                                 (scb->cmd.basic_io.lba) +
3966                                                 le16_to_cpu(scb->cmd.basic_io.
3967                                                             sector_count));
3968                         else
3969                                 scb->cmd.basic_io.lba =
3970                                     ((scb->scsi_cmd->cmnd[2] << 24) | (scb->
3971                                                                        scsi_cmd->
3972                                                                        cmnd[3]
3973                                                                        << 16) |
3974                                      (scb->scsi_cmd->cmnd[4] << 8) | scb->
3975                                      scsi_cmd->cmnd[5]);
3976
3977                         scb->cmd.basic_io.sector_count =
3978                             cpu_to_le16(scb->data_len / IPS_BLKSIZE);
3979
3980                         if (cpu_to_le16(scb->cmd.basic_io.sector_count) == 0) {
3981                                 /*
3982                                  * This is a null condition
3983                                  * we don't have to do anything
3984                                  * so just return
3985                                  */
3986                                 scb->scsi_cmd->result = DID_OK << 16;
3987                         } else
3988                                 ret = IPS_SUCCESS;
3989
3990                         break;
3991
3992                 case RESERVE:
3993                 case RELEASE:
3994                         scb->scsi_cmd->result = DID_OK << 16;
3995                         break;
3996
3997                 case MODE_SENSE:
3998                         scb->cmd.basic_io.op_code = IPS_CMD_ENQUIRY;
3999                         scb->cmd.basic_io.command_id = IPS_COMMAND_ID(ha, scb);
4000                         scb->cmd.basic_io.segment_4G = 0;
4001                         scb->cmd.basic_io.enhanced_sg = 0;
4002                         scb->data_len = sizeof (*ha->enq);
4003                         scb->cmd.basic_io.sg_addr = ha->enq_busaddr;
4004                         ret = IPS_SUCCESS;
4005                         break;
4006
4007                 case READ_CAPACITY:
4008                         scb->cmd.logical_info.op_code = IPS_CMD_GET_LD_INFO;
4009                         scb->cmd.logical_info.command_id = IPS_COMMAND_ID(ha, scb);
4010                         scb->cmd.logical_info.reserved = 0;
4011                         scb->cmd.logical_info.reserved2 = 0;
4012                         scb->cmd.logical_info.reserved3 = 0;
4013                         scb->data_len = sizeof (IPS_LD_INFO);
4014                         scb->data_busaddr = ha->logical_drive_info_dma_addr;
4015                         scb->flags = 0;
4016                         scb->cmd.logical_info.buffer_addr = scb->data_busaddr;
4017                         ret = IPS_SUCCESS;
4018                         break;
4019
4020                 case SEND_DIAGNOSTIC:
4021                 case REASSIGN_BLOCKS:
4022                 case FORMAT_UNIT:
4023                 case SEEK_10:
4024                 case VERIFY:
4025                 case READ_DEFECT_DATA:
4026                 case READ_BUFFER:
4027                 case WRITE_BUFFER:
4028                         scb->scsi_cmd->result = DID_OK << 16;
4029                         break;
4030
4031                 default:
4032                         /* Set the Return Info to appear like the Command was */
4033                         /* attempted, a Check Condition occurred, and Sense   */
4034                         /* Data indicating an Invalid CDB OpCode is returned. */
4035                         sp = (char *) scb->scsi_cmd->sense_buffer;
4036                         memset(sp, 0, sizeof (scb->scsi_cmd->sense_buffer));
4037
4038                         sp[0] = 0x70;   /* Error Code               */
4039                         sp[2] = ILLEGAL_REQUEST;        /* Sense Key 5 Illegal Req. */
4040                         sp[7] = 0x0A;   /* Additional Sense Length  */
4041                         sp[12] = 0x20;  /* ASC = Invalid OpCode     */
4042                         sp[13] = 0x00;  /* ASCQ                     */
4043
4044                         device_error = 2;       /* Indicate Check Condition */
4045                         scb->scsi_cmd->result = device_error | (DID_OK << 16);
4046                         break;
4047                 }               /* end switch */
4048         }
4049         /* end if */
4050         if (ret == IPS_SUCCESS_IMM)
4051                 return (ret);
4052
4053         /* setup DCDB */
4054         if (scb->bus > 0) {
4055
4056                 /* If we already know the Device is Not there, no need to attempt a Command   */
4057                 /* This also protects an NT FailOver Controller from getting CDB's sent to it */
4058                 if (ha->conf->dev[scb->bus - 1][scb->target_id].ucState == 0) {
4059                         scb->scsi_cmd->result = DID_NO_CONNECT << 16;
4060                         return (IPS_SUCCESS_IMM);
4061                 }
4062
4063                 ha->dcdb_active[scb->bus - 1] |= (1 << scb->target_id);
4064                 scb->cmd.dcdb.command_id = IPS_COMMAND_ID(ha, scb);
4065                 scb->cmd.dcdb.dcdb_address = cpu_to_le32(scb->scb_busaddr +
4066                                                          (unsigned long) &scb->
4067                                                          dcdb -
4068                                                          (unsigned long) scb);
4069                 scb->cmd.dcdb.reserved = 0;
4070                 scb->cmd.dcdb.reserved2 = 0;
4071                 scb->cmd.dcdb.reserved3 = 0;
4072                 scb->cmd.dcdb.segment_4G = 0;
4073                 scb->cmd.dcdb.enhanced_sg = 0;
4074
4075                 TimeOut = scb->scsi_cmd->timeout_per_command;
4076
4077                 if (ha->subsys->param[4] & 0x00100000) {        /* If NEW Tape DCDB is Supported */
4078                         if (!scb->sg_len) {
4079                                 scb->cmd.dcdb.op_code = IPS_CMD_EXTENDED_DCDB;
4080                         } else {
4081                                 scb->cmd.dcdb.op_code =
4082                                     IPS_CMD_EXTENDED_DCDB_SG;
4083                                 scb->cmd.dcdb.enhanced_sg =
4084                                     IPS_USE_ENH_SGLIST(ha) ? 0xFF : 0;
4085                         }
4086
4087                         tapeDCDB = (IPS_DCDB_TABLE_TAPE *) & scb->dcdb; /* Use Same Data Area as Old DCDB Struct */
4088                         tapeDCDB->device_address =
4089                             ((scb->bus - 1) << 4) | scb->target_id;
4090                         tapeDCDB->cmd_attribute |= IPS_DISCONNECT_ALLOWED;
4091                         tapeDCDB->cmd_attribute &= ~IPS_TRANSFER64K;    /* Always Turn OFF 64K Size Flag */
4092
4093                         if (TimeOut) {
4094                                 if (TimeOut < (10 * HZ))
4095                                         tapeDCDB->cmd_attribute |= IPS_TIMEOUT10;       /* TimeOut is 10 Seconds */
4096                                 else if (TimeOut < (60 * HZ))
4097                                         tapeDCDB->cmd_attribute |= IPS_TIMEOUT60;       /* TimeOut is 60 Seconds */
4098                                 else if (TimeOut < (1200 * HZ))
4099                                         tapeDCDB->cmd_attribute |= IPS_TIMEOUT20M;      /* TimeOut is 20 Minutes */
4100                         }
4101
4102                         tapeDCDB->cdb_length = scb->scsi_cmd->cmd_len;
4103                         tapeDCDB->reserved_for_LUN = 0;
4104                         tapeDCDB->transfer_length = scb->data_len;
4105                         if (scb->cmd.dcdb.op_code == IPS_CMD_EXTENDED_DCDB_SG)
4106                                 tapeDCDB->buffer_pointer =
4107                                     cpu_to_le32(scb->sg_busaddr);
4108                         else
4109                                 tapeDCDB->buffer_pointer =
4110                                     cpu_to_le32(scb->data_busaddr);
4111                         tapeDCDB->sg_count = scb->sg_len;
4112                         tapeDCDB->sense_length = sizeof (tapeDCDB->sense_info);
4113                         tapeDCDB->scsi_status = 0;
4114                         tapeDCDB->reserved = 0;
4115                         memcpy(tapeDCDB->scsi_cdb, scb->scsi_cmd->cmnd,
4116                                scb->scsi_cmd->cmd_len);
4117                 } else {
4118                         if (!scb->sg_len) {
4119                                 scb->cmd.dcdb.op_code = IPS_CMD_DCDB;
4120                         } else {
4121                                 scb->cmd.dcdb.op_code = IPS_CMD_DCDB_SG;
4122                                 scb->cmd.dcdb.enhanced_sg =
4123                                     IPS_USE_ENH_SGLIST(ha) ? 0xFF : 0;
4124                         }
4125
4126                         scb->dcdb.device_address =
4127                             ((scb->bus - 1) << 4) | scb->target_id;
4128                         scb->dcdb.cmd_attribute |= IPS_DISCONNECT_ALLOWED;
4129
4130                         if (TimeOut) {
4131                                 if (TimeOut < (10 * HZ))
4132                                         scb->dcdb.cmd_attribute |= IPS_TIMEOUT10;       /* TimeOut is 10 Seconds */
4133                                 else if (TimeOut < (60 * HZ))
4134                                         scb->dcdb.cmd_attribute |= IPS_TIMEOUT60;       /* TimeOut is 60 Seconds */
4135                                 else if (TimeOut < (1200 * HZ))
4136                                         scb->dcdb.cmd_attribute |= IPS_TIMEOUT20M;      /* TimeOut is 20 Minutes */
4137                         }
4138
4139                         scb->dcdb.transfer_length = scb->data_len;
4140                         if (scb->dcdb.cmd_attribute & IPS_TRANSFER64K)
4141                                 scb->dcdb.transfer_length = 0;
4142                         if (scb->cmd.dcdb.op_code == IPS_CMD_DCDB_SG)
4143                                 scb->dcdb.buffer_pointer =
4144                                     cpu_to_le32(scb->sg_busaddr);
4145                         else
4146                                 scb->dcdb.buffer_pointer =
4147                                     cpu_to_le32(scb->data_busaddr);
4148                         scb->dcdb.cdb_length = scb->scsi_cmd->cmd_len;
4149                         scb->dcdb.sense_length = sizeof (scb->dcdb.sense_info);
4150                         scb->dcdb.sg_count = scb->sg_len;
4151                         scb->dcdb.reserved = 0;
4152                         memcpy(scb->dcdb.scsi_cdb, scb->scsi_cmd->cmnd,
4153                                scb->scsi_cmd->cmd_len);
4154                         scb->dcdb.scsi_status = 0;
4155                         scb->dcdb.reserved2[0] = 0;
4156                         scb->dcdb.reserved2[1] = 0;
4157                         scb->dcdb.reserved2[2] = 0;
4158                 }
4159         }
4160
4161         return ((*ha->func.issue) (ha, scb));
4162 }
4163
4164 /****************************************************************************/
4165 /*                                                                          */
4166 /* Routine Name: ips_chk_status                                             */
4167 /*                                                                          */
4168 /* Routine Description:                                                     */
4169 /*                                                                          */
4170 /*   Check the status of commands to logical drives                         */
4171 /*   Assumed to be called with the HA lock                                  */
4172 /****************************************************************************/
4173 static void
4174 ips_chkstatus(ips_ha_t * ha, IPS_STATUS * pstatus)
4175 {
4176         ips_scb_t *scb;
4177         ips_stat_t *sp;
4178         uint8_t basic_status;
4179         uint8_t ext_status;
4180         int errcode;
4181
4182         METHOD_TRACE("ips_chkstatus", 1);
4183
4184         scb = &ha->scbs[pstatus->fields.command_id];
4185         scb->basic_status = basic_status =
4186             pstatus->fields.basic_status & IPS_BASIC_STATUS_MASK;
4187         scb->extended_status = ext_status = pstatus->fields.extended_status;
4188
4189         sp = &ha->sp;
4190         sp->residue_len = 0;
4191         sp->scb_addr = (void *) scb;
4192
4193         /* Remove the item from the active queue */
4194         ips_removeq_scb(&ha->scb_activelist, scb);
4195
4196         if (!scb->scsi_cmd)
4197                 /* internal commands are handled in do_ipsintr */
4198                 return;
4199
4200         DEBUG_VAR(2, "(%s%d) ips_chkstatus: cmd 0x%X id %d (%d %d %d)",
4201                   ips_name,
4202                   ha->host_num,
4203                   scb->cdb[0],
4204                   scb->cmd.basic_io.command_id,
4205                   scb->bus, scb->target_id, scb->lun);
4206
4207         if ((scb->scsi_cmd) && (ips_is_passthru(scb->scsi_cmd)))
4208                 /* passthru - just returns the raw result */
4209                 return;
4210
4211         errcode = DID_OK;
4212
4213         if (((basic_status & IPS_GSC_STATUS_MASK) == IPS_CMD_SUCCESS) ||
4214             ((basic_status & IPS_GSC_STATUS_MASK) == IPS_CMD_RECOVERED_ERROR)) {
4215
4216                 if (scb->bus == 0) {
4217                         if ((basic_status & IPS_GSC_STATUS_MASK) ==
4218                             IPS_CMD_RECOVERED_ERROR) {
4219                                 DEBUG_VAR(1,
4220                                           "(%s%d) Recovered Logical Drive Error OpCode: %x, BSB: %x, ESB: %x",
4221                                           ips_name, ha->host_num,
4222                                           scb->cmd.basic_io.op_code,
4223                                           basic_status, ext_status);
4224                         }
4225
4226                         switch (scb->scsi_cmd->cmnd[0]) {
4227                         case ALLOW_MEDIUM_REMOVAL:
4228                         case REZERO_UNIT:
4229                         case ERASE:
4230                         case WRITE_FILEMARKS:
4231                         case SPACE:
4232                                 errcode = DID_ERROR;
4233                                 break;
4234
4235                         case START_STOP:
4236                                 break;
4237
4238                         case TEST_UNIT_READY:
4239                                 if (!ips_online(ha, scb)) {
4240                                         errcode = DID_TIME_OUT;
4241                                 }
4242                                 break;
4243
4244                         case INQUIRY:
4245                                 if (ips_online(ha, scb)) {
4246                                         ips_inquiry(ha, scb);
4247                                 } else {
4248                                         errcode = DID_TIME_OUT;
4249                                 }
4250                                 break;
4251
4252                         case REQUEST_SENSE:
4253                                 ips_reqsen(ha, scb);
4254                                 break;
4255
4256                         case READ_6:
4257                         case WRITE_6:
4258                         case READ_10:
4259                         case WRITE_10:
4260                         case RESERVE:
4261                         case RELEASE:
4262                                 break;
4263
4264                         case MODE_SENSE:
4265                                 if (!ips_online(ha, scb)
4266                                     || !ips_msense(ha, scb)) {
4267                                         errcode = DID_ERROR;
4268                                 }
4269                                 break;
4270
4271                         case READ_CAPACITY:
4272                                 if (ips_online(ha, scb))
4273                                         ips_rdcap(ha, scb);
4274                                 else {
4275                                         errcode = DID_TIME_OUT;
4276                                 }
4277                                 break;
4278
4279                         case SEND_DIAGNOSTIC:
4280                         case REASSIGN_BLOCKS:
4281                                 break;
4282
4283                         case FORMAT_UNIT:
4284                                 errcode = DID_ERROR;
4285                                 break;
4286
4287                         case SEEK_10:
4288                         case VERIFY:
4289                         case READ_DEFECT_DATA:
4290                         case READ_BUFFER:
4291                         case WRITE_BUFFER:
4292                                 break;
4293
4294                         default:
4295                                 errcode = DID_ERROR;
4296                         }       /* end switch */
4297
4298                         scb->scsi_cmd->result = errcode << 16;
4299                 } else {        /* bus == 0 */
4300                         /* restrict access to physical drives */
4301                         if ((scb->scsi_cmd->cmnd[0] == INQUIRY) &&
4302                             ((((char *) scb->scsi_cmd->buffer)[0] & 0x1f) ==
4303                              TYPE_DISK)) {
4304
4305                                 scb->scsi_cmd->result = DID_TIME_OUT << 16;
4306                         }
4307                 }               /* else */
4308         } else {                /* recovered error / success */
4309                 if (scb->bus == 0) {
4310                         DEBUG_VAR(1,
4311                                   "(%s%d) Unrecovered Logical Drive Error OpCode: %x, BSB: %x, ESB: %x",
4312                                   ips_name, ha->host_num,
4313                                   scb->cmd.basic_io.op_code, basic_status,
4314                                   ext_status);
4315                 }
4316
4317                 ips_map_status(ha, scb, sp);
4318         }                       /* else */
4319 }
4320
4321 /****************************************************************************/
4322 /*                                                                          */
4323 /* Routine Name: ips_online                                                 */
4324 /*                                                                          */
4325 /* Routine Description:                                                     */
4326 /*                                                                          */
4327 /*   Determine if a logical drive is online                                 */
4328 /*                                                                          */
4329 /****************************************************************************/
4330 static int
4331 ips_online(ips_ha_t * ha, ips_scb_t * scb)
4332 {
4333         METHOD_TRACE("ips_online", 1);
4334
4335         if (scb->target_id >= IPS_MAX_LD)
4336                 return (0);
4337
4338         if ((scb->basic_status & IPS_GSC_STATUS_MASK) > 1) {
4339                 memset(ha->logical_drive_info, 0, sizeof (IPS_LD_INFO));
4340                 return (0);
4341         }
4342
4343         if (ha->logical_drive_info->drive_info[scb->target_id].state !=
4344             IPS_LD_OFFLINE
4345             && ha->logical_drive_info->drive_info[scb->target_id].state !=
4346             IPS_LD_FREE
4347             && ha->logical_drive_info->drive_info[scb->target_id].state !=
4348             IPS_LD_CRS
4349             && ha->logical_drive_info->drive_info[scb->target_id].state !=
4350             IPS_LD_SYS)
4351                 return (1);
4352         else
4353                 return (0);
4354 }
4355
4356 /****************************************************************************/
4357 /*                                                                          */
4358 /* Routine Name: ips_inquiry                                                */
4359 /*                                                                          */
4360 /* Routine Description:                                                     */
4361 /*                                                                          */
4362 /*   Simulate an inquiry command to a logical drive                         */
4363 /*                                                                          */
4364 /****************************************************************************/
4365 static int
4366 ips_inquiry(ips_ha_t * ha, ips_scb_t * scb)
4367 {
4368         IPS_SCSI_INQ_DATA inquiry;
4369
4370         METHOD_TRACE("ips_inquiry", 1);
4371
4372         memset(&inquiry, 0, sizeof (IPS_SCSI_INQ_DATA));
4373
4374         inquiry.DeviceType = IPS_SCSI_INQ_TYPE_DASD;
4375         inquiry.DeviceTypeQualifier = IPS_SCSI_INQ_LU_CONNECTED;
4376         inquiry.Version = IPS_SCSI_INQ_REV2;
4377         inquiry.ResponseDataFormat = IPS_SCSI_INQ_RD_REV2;
4378         inquiry.AdditionalLength = 31;
4379         inquiry.Flags[0] = IPS_SCSI_INQ_Address16;
4380         inquiry.Flags[1] =
4381             IPS_SCSI_INQ_WBus16 | IPS_SCSI_INQ_Sync | IPS_SCSI_INQ_CmdQue;
4382         strncpy(inquiry.VendorId, "IBM     ", 8);
4383         strncpy(inquiry.ProductId, "SERVERAID       ", 16);
4384         strncpy(inquiry.ProductRevisionLevel, "1.00", 4);
4385
4386         ips_scmd_buf_write(scb->scsi_cmd, &inquiry, sizeof (inquiry));
4387
4388         return (1);
4389 }
4390
4391 /****************************************************************************/
4392 /*                                                                          */
4393 /* Routine Name: ips_rdcap                                                  */
4394 /*                                                                          */
4395 /* Routine Description:                                                     */
4396 /*                                                                          */
4397 /*   Simulate a read capacity command to a logical drive                    */
4398 /*                                                                          */
4399 /****************************************************************************/
4400 static int
4401 ips_rdcap(ips_ha_t * ha, ips_scb_t * scb)
4402 {
4403         IPS_SCSI_CAPACITY cap;
4404
4405         METHOD_TRACE("ips_rdcap", 1);
4406
4407         if (scb->scsi_cmd->bufflen < 8)
4408                 return (0);
4409
4410         cap.lba =
4411             cpu_to_be32(le32_to_cpu
4412                         (ha->logical_drive_info->
4413                          drive_info[scb->target_id].sector_count) - 1);
4414         cap.len = cpu_to_be32((uint32_t) IPS_BLKSIZE);
4415
4416         ips_scmd_buf_write(scb->scsi_cmd, &cap, sizeof (cap));
4417
4418         return (1);
4419 }
4420
4421 /****************************************************************************/
4422 /*                                                                          */
4423 /* Routine Name: ips_msense                                                 */
4424 /*                                                                          */
4425 /* Routine Description:                                                     */
4426 /*                                                                          */
4427 /*   Simulate a mode sense command to a logical drive                       */
4428 /*                                                                          */
4429 /****************************************************************************/
4430 static int
4431 ips_msense(ips_ha_t * ha, ips_scb_t * scb)
4432 {
4433         uint16_t heads;
4434         uint16_t sectors;
4435         uint32_t cylinders;
4436         IPS_SCSI_MODE_PAGE_DATA mdata;
4437
4438         METHOD_TRACE("ips_msense", 1);
4439
4440         if (le32_to_cpu(ha->enq->ulDriveSize[scb->target_id]) > 0x400000 &&
4441             (ha->enq->ucMiscFlag & 0x8) == 0) {
4442                 heads = IPS_NORM_HEADS;
4443                 sectors = IPS_NORM_SECTORS;
4444         } else {
4445                 heads = IPS_COMP_HEADS;
4446                 sectors = IPS_COMP_SECTORS;
4447         }
4448
4449         cylinders =
4450             (le32_to_cpu(ha->enq->ulDriveSize[scb->target_id]) -
4451              1) / (heads * sectors);
4452
4453         memset(&mdata, 0, sizeof (IPS_SCSI_MODE_PAGE_DATA));
4454
4455         mdata.hdr.BlockDescLength = 8;
4456
4457         switch (scb->scsi_cmd->cmnd[2] & 0x3f) {
4458         case 0x03:              /* page 3 */
4459                 mdata.pdata.pg3.PageCode = 3;
4460                 mdata.pdata.pg3.PageLength = sizeof (IPS_SCSI_MODE_PAGE3);
4461                 mdata.hdr.DataLength =
4462                     3 + mdata.hdr.BlockDescLength + mdata.pdata.pg3.PageLength;
4463                 mdata.pdata.pg3.TracksPerZone = 0;
4464                 mdata.pdata.pg3.AltSectorsPerZone = 0;
4465                 mdata.pdata.pg3.AltTracksPerZone = 0;
4466                 mdata.pdata.pg3.AltTracksPerVolume = 0;
4467                 mdata.pdata.pg3.SectorsPerTrack = cpu_to_be16(sectors);
4468                 mdata.pdata.pg3.BytesPerSector = cpu_to_be16(IPS_BLKSIZE);
4469                 mdata.pdata.pg3.Interleave = cpu_to_be16(1);
4470                 mdata.pdata.pg3.TrackSkew = 0;
4471                 mdata.pdata.pg3.CylinderSkew = 0;
4472                 mdata.pdata.pg3.flags = IPS_SCSI_MP3_SoftSector;
4473                 break;
4474
4475         case 0x4:
4476                 mdata.pdata.pg4.PageCode = 4;
4477                 mdata.pdata.pg4.PageLength = sizeof (IPS_SCSI_MODE_PAGE4);
4478                 mdata.hdr.DataLength =
4479                     3 + mdata.hdr.BlockDescLength + mdata.pdata.pg4.PageLength;
4480                 mdata.pdata.pg4.CylindersHigh =
4481                     cpu_to_be16((cylinders >> 8) & 0xFFFF);
4482                 mdata.pdata.pg4.CylindersLow = (cylinders & 0xFF);
4483                 mdata.pdata.pg4.Heads = heads;
4484                 mdata.pdata.pg4.WritePrecompHigh = 0;
4485                 mdata.pdata.pg4.WritePrecompLow = 0;
4486                 mdata.pdata.pg4.ReducedWriteCurrentHigh = 0;
4487                 mdata.pdata.pg4.ReducedWriteCurrentLow = 0;
4488                 mdata.pdata.pg4.StepRate = cpu_to_be16(1);
4489                 mdata.pdata.pg4.LandingZoneHigh = 0;
4490                 mdata.pdata.pg4.LandingZoneLow = 0;
4491                 mdata.pdata.pg4.flags = 0;
4492                 mdata.pdata.pg4.RotationalOffset = 0;
4493                 mdata.pdata.pg4.MediumRotationRate = 0;
4494                 break;
4495         case 0x8:
4496                 mdata.pdata.pg8.PageCode = 8;
4497                 mdata.pdata.pg8.PageLength = sizeof (IPS_SCSI_MODE_PAGE8);
4498                 mdata.hdr.DataLength =
4499                     3 + mdata.hdr.BlockDescLength + mdata.pdata.pg8.PageLength;
4500                 /* everything else is left set to 0 */
4501                 break;
4502
4503         default:
4504                 return (0);
4505         }                       /* end switch */
4506
4507         ips_scmd_buf_write(scb->scsi_cmd, &mdata, sizeof (mdata));
4508
4509         return (1);
4510 }
4511
4512 /****************************************************************************/
4513 /*                                                                          */
4514 /* Routine Name: ips_reqsen                                                 */
4515 /*                                                                          */
4516 /* Routine Description:                                                     */
4517 /*                                                                          */
4518 /*   Simulate a request sense command to a logical drive                    */
4519 /*                                                                          */
4520 /****************************************************************************/
4521 static int
4522 ips_reqsen(ips_ha_t * ha, ips_scb_t * scb)
4523 {
4524         IPS_SCSI_REQSEN reqsen;
4525
4526         METHOD_TRACE("ips_reqsen", 1);
4527
4528         memset(&reqsen, 0, sizeof (IPS_SCSI_REQSEN));
4529
4530         reqsen.ResponseCode =
4531             IPS_SCSI_REQSEN_VALID | IPS_SCSI_REQSEN_CURRENT_ERR;
4532         reqsen.AdditionalLength = 10;
4533         reqsen.AdditionalSenseCode = IPS_SCSI_REQSEN_NO_SENSE;
4534         reqsen.AdditionalSenseCodeQual = IPS_SCSI_REQSEN_NO_SENSE;
4535
4536         ips_scmd_buf_write(scb->scsi_cmd, &reqsen, sizeof (reqsen));
4537
4538         return (1);
4539 }
4540
4541 /****************************************************************************/
4542 /*                                                                          */
4543 /* Routine Name: ips_free                                                   */
4544 /*                                                                          */
4545 /* Routine Description:                                                     */
4546 /*                                                                          */
4547 /*   Free any allocated space for this controller                           */
4548 /*                                                                          */
4549 /****************************************************************************/
4550 static void
4551 ips_free(ips_ha_t * ha)
4552 {
4553
4554         METHOD_TRACE("ips_free", 1);
4555
4556         if (ha) {
4557                 if (ha->enq) {
4558                         pci_free_consistent(ha->pcidev, sizeof(IPS_ENQ),
4559                                             ha->enq, ha->enq_busaddr);
4560                         ha->enq = NULL;
4561                 }
4562
4563                 if (ha->conf) {
4564                         kfree(ha->conf);
4565                         ha->conf = NULL;
4566                 }
4567
4568                 if (ha->adapt) {
4569                         pci_free_consistent(ha->pcidev,
4570                                             sizeof (IPS_ADAPTER) +
4571                                             sizeof (IPS_IO_CMD), ha->adapt,
4572                                             ha->adapt->hw_status_start);
4573                         ha->adapt = NULL;
4574                 }
4575
4576                 if (ha->logical_drive_info) {
4577                         pci_free_consistent(ha->pcidev,
4578                                             sizeof (IPS_LD_INFO),
4579                                             ha->logical_drive_info,
4580                                             ha->logical_drive_info_dma_addr);
4581                         ha->logical_drive_info = NULL;
4582                 }
4583
4584                 if (ha->nvram) {
4585                         kfree(ha->nvram);
4586                         ha->nvram = NULL;
4587                 }
4588
4589                 if (ha->subsys) {
4590                         kfree(ha->subsys);
4591                         ha->subsys = NULL;
4592                 }
4593
4594                 if (ha->ioctl_data) {
4595                         pci_free_consistent(ha->pcidev, ha->ioctl_len,
4596                                             ha->ioctl_data, ha->ioctl_busaddr);
4597                         ha->ioctl_data = NULL;
4598                         ha->ioctl_datasize = 0;
4599                         ha->ioctl_len = 0;
4600                 }
4601                 ips_deallocatescbs(ha, ha->max_cmds);
4602
4603                 /* free memory mapped (if applicable) */
4604                 if (ha->mem_ptr) {
4605                         iounmap(ha->ioremap_ptr);
4606                         ha->ioremap_ptr = NULL;
4607                         ha->mem_ptr = NULL;
4608                 }
4609
4610                 if (ha->mem_addr)
4611                         release_mem_region(ha->mem_addr, ha->mem_len);
4612                 ha->mem_addr = 0;
4613
4614         }
4615 }
4616
4617 /****************************************************************************/
4618 /*                                                                          */
4619 /* Routine Name: ips_deallocatescbs                                         */
4620 /*                                                                          */
4621 /* Routine Description:                                                     */
4622 /*                                                                          */
4623 /*   Free the command blocks                                                */
4624 /*                                                                          */
4625 /****************************************************************************/
4626 static int
4627 ips_deallocatescbs(ips_ha_t * ha, int cmds)
4628 {
4629         if (ha->scbs) {
4630                 pci_free_consistent(ha->pcidev,
4631                                     IPS_SGLIST_SIZE(ha) * IPS_MAX_SG * cmds,
4632                                     ha->scbs->sg_list.list,
4633                                     ha->scbs->sg_busaddr);
4634                 pci_free_consistent(ha->pcidev, sizeof (ips_scb_t) * cmds,
4635                                     ha->scbs, ha->scbs->scb_busaddr);
4636                 ha->scbs = NULL;
4637         }                       /* end if */
4638         return 1;
4639 }
4640
4641 /****************************************************************************/
4642 /*                                                                          */
4643 /* Routine Name: ips_allocatescbs                                           */
4644 /*                                                                          */
4645 /* Routine Description:                                                     */
4646 /*                                                                          */
4647 /*   Allocate the command blocks                                            */
4648 /*                                                                          */
4649 /****************************************************************************/
4650 static int
4651 ips_allocatescbs(ips_ha_t * ha)
4652 {
4653         ips_scb_t *scb_p;
4654         IPS_SG_LIST ips_sg;
4655         int i;
4656         dma_addr_t command_dma, sg_dma;
4657
4658         METHOD_TRACE("ips_allocatescbs", 1);
4659
4660         /* Allocate memory for the SCBs */
4661         ha->scbs =
4662             pci_alloc_consistent(ha->pcidev, ha->max_cmds * sizeof (ips_scb_t),
4663                                  &command_dma);
4664         if (ha->scbs == NULL)
4665                 return 0;
4666         ips_sg.list =
4667             pci_alloc_consistent(ha->pcidev,
4668                                  IPS_SGLIST_SIZE(ha) * IPS_MAX_SG *
4669                                  ha->max_cmds, &sg_dma);
4670         if (ips_sg.list == NULL) {
4671                 pci_free_consistent(ha->pcidev,
4672                                     ha->max_cmds * sizeof (ips_scb_t), ha->scbs,
4673                                     command_dma);
4674                 return 0;
4675         }
4676
4677         memset(ha->scbs, 0, ha->max_cmds * sizeof (ips_scb_t));
4678
4679         for (i = 0; i < ha->max_cmds; i++) {
4680                 scb_p = &ha->scbs[i];
4681                 scb_p->scb_busaddr = command_dma + sizeof (ips_scb_t) * i;
4682                 /* set up S/G list */
4683                 if (IPS_USE_ENH_SGLIST(ha)) {
4684                         scb_p->sg_list.enh_list =
4685                             ips_sg.enh_list + i * IPS_MAX_SG;
4686                         scb_p->sg_busaddr =
4687                             sg_dma + IPS_SGLIST_SIZE(ha) * IPS_MAX_SG * i;
4688                 } else {
4689                         scb_p->sg_list.std_list =
4690                             ips_sg.std_list + i * IPS_MAX_SG;
4691                         scb_p->sg_busaddr =
4692                             sg_dma + IPS_SGLIST_SIZE(ha) * IPS_MAX_SG * i;
4693                 }
4694
4695                 /* add to the free list */
4696                 if (i < ha->max_cmds - 1) {
4697                         scb_p->q_next = ha->scb_freelist;
4698                         ha->scb_freelist = scb_p;
4699                 }
4700         }
4701
4702         /* success */
4703         return (1);
4704 }
4705
4706 /****************************************************************************/
4707 /*                                                                          */
4708 /* Routine Name: ips_init_scb                                               */
4709 /*                                                                          */
4710 /* Routine Description:                                                     */
4711 /*                                                                          */
4712 /*   Initialize a CCB to default values                                     */
4713 /*                                                                          */
4714 /****************************************************************************/
4715 static void
4716 ips_init_scb(ips_ha_t * ha, ips_scb_t * scb)
4717 {
4718         IPS_SG_LIST sg_list;
4719         uint32_t cmd_busaddr, sg_busaddr;
4720         METHOD_TRACE("ips_init_scb", 1);
4721
4722         if (scb == NULL)
4723                 return;
4724
4725         sg_list.list = scb->sg_list.list;
4726         cmd_busaddr = scb->scb_busaddr;
4727         sg_busaddr = scb->sg_busaddr;
4728         /* zero fill */
4729         memset(scb, 0, sizeof (ips_scb_t));
4730         memset(ha->dummy, 0, sizeof (IPS_IO_CMD));
4731
4732         /* Initialize dummy command bucket */
4733         ha->dummy->op_code = 0xFF;
4734         ha->dummy->ccsar = cpu_to_le32(ha->adapt->hw_status_start
4735                                        + sizeof (IPS_ADAPTER));
4736         ha->dummy->command_id = IPS_MAX_CMDS;
4737
4738         /* set bus address of scb */
4739         scb->scb_busaddr = cmd_busaddr;
4740         scb->sg_busaddr = sg_busaddr;
4741         scb->sg_list.list = sg_list.list;
4742
4743         /* Neptune Fix */
4744         scb->cmd.basic_io.cccr = cpu_to_le32((uint32_t) IPS_BIT_ILE);
4745         scb->cmd.basic_io.ccsar = cpu_to_le32(ha->adapt->hw_status_start
4746                                               + sizeof (IPS_ADAPTER));
4747 }
4748
4749 /****************************************************************************/
4750 /*                                                                          */
4751 /* Routine Name: ips_get_scb                                                */
4752 /*                                                                          */
4753 /* Routine Description:                                                     */
4754 /*                                                                          */
4755 /*   Initialize a CCB to default values                                     */
4756 /*                                                                          */
4757 /* ASSUMED to be callled from within a lock                                 */
4758 /*                                                                          */
4759 /****************************************************************************/
4760 static ips_scb_t *
4761 ips_getscb(ips_ha_t * ha)
4762 {
4763         ips_scb_t *scb;
4764
4765         METHOD_TRACE("ips_getscb", 1);
4766
4767         if ((scb = ha->scb_freelist) == NULL) {
4768
4769                 return (NULL);
4770         }
4771
4772         ha->scb_freelist = scb->q_next;
4773         scb->flags = 0;
4774         scb->q_next = NULL;
4775
4776         ips_init_scb(ha, scb);
4777
4778         return (scb);
4779 }
4780
4781 /****************************************************************************/
4782 /*                                                                          */
4783 /* Routine Name: ips_free_scb                                               */
4784 /*                                                                          */
4785 /* Routine Description:                                                     */
4786 /*                                                                          */
4787 /*   Return an unused CCB back to the free list                             */
4788 /*                                                                          */
4789 /* ASSUMED to be called from within a lock                                  */
4790 /*                                                                          */
4791 /****************************************************************************/
4792 static void
4793 ips_freescb(ips_ha_t * ha, ips_scb_t * scb)
4794 {
4795
4796         METHOD_TRACE("ips_freescb", 1);
4797         if (scb->flags & IPS_SCB_MAP_SG)
4798                 pci_unmap_sg(ha->pcidev, scb->scsi_cmd->request_buffer,
4799                              scb->scsi_cmd->use_sg, IPS_DMA_DIR(scb));
4800         else if (scb->flags & IPS_SCB_MAP_SINGLE)
4801                 pci_unmap_single(ha->pcidev, scb->data_busaddr, scb->data_len,
4802                                  IPS_DMA_DIR(scb));
4803
4804         /* check to make sure this is not our "special" scb */
4805         if (IPS_COMMAND_ID(ha, scb) < (ha->max_cmds - 1)) {
4806                 scb->q_next = ha->scb_freelist;
4807                 ha->scb_freelist = scb;
4808         }
4809 }
4810
4811 /****************************************************************************/
4812 /*                                                                          */
4813 /* Routine Name: ips_isinit_copperhead                                      */
4814 /*                                                                          */
4815 /* Routine Description:                                                     */
4816 /*                                                                          */
4817 /*   Is controller initialized ?                                            */
4818 /*                                                                          */
4819 /****************************************************************************/
4820 static int
4821 ips_isinit_copperhead(ips_ha_t * ha)
4822 {
4823         uint8_t scpr;
4824         uint8_t isr;
4825
4826         METHOD_TRACE("ips_isinit_copperhead", 1);
4827
4828         isr = inb(ha->io_addr + IPS_REG_HISR);
4829         scpr = inb(ha->io_addr + IPS_REG_SCPR);
4830
4831         if (((isr & IPS_BIT_EI) == 0) && ((scpr & IPS_BIT_EBM) == 0))
4832                 return (0);
4833         else
4834                 return (1);
4835 }
4836
4837 /****************************************************************************/
4838 /*                                                                          */
4839 /* Routine Name: ips_isinit_copperhead_memio                                */
4840 /*                                                                          */
4841 /* Routine Description:                                                     */
4842 /*                                                                          */
4843 /*   Is controller initialized ?                                            */
4844 /*                                                                          */
4845 /****************************************************************************/
4846 static int
4847 ips_isinit_copperhead_memio(ips_ha_t * ha)
4848 {
4849         uint8_t isr = 0;
4850         uint8_t scpr;
4851
4852         METHOD_TRACE("ips_is_init_copperhead_memio", 1);
4853
4854         isr = readb(ha->mem_ptr + IPS_REG_HISR);
4855         scpr = readb(ha->mem_ptr + IPS_REG_SCPR);
4856
4857         if (((isr & IPS_BIT_EI) == 0) && ((scpr & IPS_BIT_EBM) == 0))
4858                 return (0);
4859         else
4860                 return (1);
4861 }
4862
4863 /****************************************************************************/
4864 /*                                                                          */
4865 /* Routine Name: ips_isinit_morpheus                                        */
4866 /*                                                                          */
4867 /* Routine Description:                                                     */
4868 /*                                                                          */
4869 /*   Is controller initialized ?                                            */
4870 /*                                                                          */
4871 /****************************************************************************/
4872 static int
4873 ips_isinit_morpheus(ips_ha_t * ha)
4874 {
4875         uint32_t post;
4876         uint32_t bits;
4877
4878         METHOD_TRACE("ips_is_init_morpheus", 1);
4879
4880         post = readl(ha->mem_ptr + IPS_REG_I960_MSG0);
4881         bits = readl(ha->mem_ptr + IPS_REG_I2O_HIR);
4882
4883         if (post == 0)
4884                 return (0);
4885         else if (bits & 0x3)
4886                 return (0);
4887         else
4888                 return (1);
4889 }
4890
4891 /****************************************************************************/
4892 /*                                                                          */
4893 /* Routine Name: ips_enable_int_copperhead                                  */
4894 /*                                                                          */
4895 /* Routine Description:                                                     */
4896 /*   Turn on interrupts                                                     */
4897 /*                                                                          */
4898 /****************************************************************************/
4899 static void
4900 ips_enable_int_copperhead(ips_ha_t * ha)
4901 {
4902         METHOD_TRACE("ips_enable_int_copperhead", 1);
4903
4904         outb(ha->io_addr + IPS_REG_HISR, IPS_BIT_EI);
4905         inb(ha->io_addr + IPS_REG_HISR);        /*Ensure PCI Posting Completes*/
4906 }
4907
4908 /****************************************************************************/
4909 /*                                                                          */
4910 /* Routine Name: ips_enable_int_copperhead_memio                            */
4911 /*                                                                          */
4912 /* Routine Description:                                                     */
4913 /*   Turn on interrupts                                                     */
4914 /*                                                                          */
4915 /****************************************************************************/
4916 static void
4917 ips_enable_int_copperhead_memio(ips_ha_t * ha)
4918 {
4919         METHOD_TRACE("ips_enable_int_copperhead_memio", 1);
4920
4921         writeb(IPS_BIT_EI, ha->mem_ptr + IPS_REG_HISR);
4922         readb(ha->mem_ptr + IPS_REG_HISR);      /*Ensure PCI Posting Completes*/
4923 }
4924
4925 /****************************************************************************/
4926 /*                                                                          */
4927 /* Routine Name: ips_enable_int_morpheus                                    */
4928 /*                                                                          */
4929 /* Routine Description:                                                     */
4930 /*   Turn on interrupts                                                     */
4931 /*                                                                          */
4932 /****************************************************************************/
4933 static void
4934 ips_enable_int_morpheus(ips_ha_t * ha)
4935 {
4936         uint32_t Oimr;
4937
4938         METHOD_TRACE("ips_enable_int_morpheus", 1);
4939
4940         Oimr = readl(ha->mem_ptr + IPS_REG_I960_OIMR);
4941         Oimr &= ~0x08;
4942         writel(Oimr, ha->mem_ptr + IPS_REG_I960_OIMR);
4943         readl(ha->mem_ptr + IPS_REG_I960_OIMR); /*Ensure PCI Posting Completes*/
4944 }
4945
4946 /****************************************************************************/
4947 /*                                                                          */
4948 /* Routine Name: ips_init_copperhead                                        */
4949 /*                                                                          */
4950 /* Routine Description:                                                     */
4951 /*                                                                          */
4952 /*   Initialize a copperhead controller                                     */
4953 /*                                                                          */
4954 /****************************************************************************/
4955 static int
4956 ips_init_copperhead(ips_ha_t * ha)
4957 {
4958         uint8_t Isr;
4959         uint8_t Cbsp;
4960         uint8_t PostByte[IPS_MAX_POST_BYTES];
4961         uint8_t ConfigByte[IPS_MAX_CONFIG_BYTES];
4962         int i, j;
4963
4964         METHOD_TRACE("ips_init_copperhead", 1);
4965
4966         for (i = 0; i < IPS_MAX_POST_BYTES; i++) {
4967                 for (j = 0; j < 45; j++) {
4968                         Isr = inb(ha->io_addr + IPS_REG_HISR);
4969                         if (Isr & IPS_BIT_GHI)
4970                                 break;
4971
4972                         /* Delay for 1 Second */
4973                         MDELAY(IPS_ONE_SEC);
4974                 }
4975
4976                 if (j >= 45)
4977                         /* error occurred */
4978                         return (0);
4979
4980                 PostByte[i] = inb(ha->io_addr + IPS_REG_ISPR);
4981                 outb(Isr, ha->io_addr + IPS_REG_HISR);
4982         }
4983
4984         if (PostByte[0] < IPS_GOOD_POST_STATUS) {
4985                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
4986                            "reset controller fails (post status %x %x).\n",
4987                            PostByte[0], PostByte[1]);
4988
4989                 return (0);
4990         }
4991
4992         for (i = 0; i < IPS_MAX_CONFIG_BYTES; i++) {
4993                 for (j = 0; j < 240; j++) {
4994                         Isr = inb(ha->io_addr + IPS_REG_HISR);
4995                         if (Isr & IPS_BIT_GHI)
4996                                 break;
4997
4998                         /* Delay for 1 Second */
4999                         MDELAY(IPS_ONE_SEC);
5000                 }
5001
5002                 if (j >= 240)
5003                         /* error occurred */
5004                         return (0);
5005
5006                 ConfigByte[i] = inb(ha->io_addr + IPS_REG_ISPR);
5007                 outb(Isr, ha->io_addr + IPS_REG_HISR);
5008         }
5009
5010         for (i = 0; i < 240; i++) {
5011                 Cbsp = inb(ha->io_addr + IPS_REG_CBSP);
5012
5013                 if ((Cbsp & IPS_BIT_OP) == 0)
5014                         break;
5015
5016                 /* Delay for 1 Second */
5017                 MDELAY(IPS_ONE_SEC);
5018         }
5019
5020         if (i >= 240)
5021                 /* reset failed */
5022                 return (0);
5023
5024         /* setup CCCR */
5025         outl(cpu_to_le32(0x1010), ha->io_addr + IPS_REG_CCCR);
5026
5027         /* Enable busmastering */
5028         outb(IPS_BIT_EBM, ha->io_addr + IPS_REG_SCPR);
5029
5030         if (ha->revision_id == IPS_REVID_TROMBONE64)
5031                 /* fix for anaconda64 */
5032                 outl(0, ha->io_addr + IPS_REG_NDAE);
5033
5034         /* Enable interrupts */
5035         outb(IPS_BIT_EI, ha->io_addr + IPS_REG_HISR);
5036
5037         return (1);
5038 }
5039
5040 /****************************************************************************/
5041 /*                                                                          */
5042 /* Routine Name: ips_init_copperhead_memio                                  */
5043 /*                                                                          */
5044 /* Routine Description:                                                     */
5045 /*                                                                          */
5046 /*   Initialize a copperhead controller with memory mapped I/O              */
5047 /*                                                                          */
5048 /****************************************************************************/
5049 static int
5050 ips_init_copperhead_memio(ips_ha_t * ha)
5051 {
5052         uint8_t Isr = 0;
5053         uint8_t Cbsp;
5054         uint8_t PostByte[IPS_MAX_POST_BYTES];
5055         uint8_t ConfigByte[IPS_MAX_CONFIG_BYTES];
5056         int i, j;
5057
5058         METHOD_TRACE("ips_init_copperhead_memio", 1);
5059
5060         for (i = 0; i < IPS_MAX_POST_BYTES; i++) {
5061                 for (j = 0; j < 45; j++) {
5062                         Isr = readb(ha->mem_ptr + IPS_REG_HISR);
5063                         if (Isr & IPS_BIT_GHI)
5064                                 break;
5065
5066                         /* Delay for 1 Second */
5067                         MDELAY(IPS_ONE_SEC);
5068                 }
5069
5070                 if (j >= 45)
5071                         /* error occurred */
5072                         return (0);
5073
5074                 PostByte[i] = readb(ha->mem_ptr + IPS_REG_ISPR);
5075                 writeb(Isr, ha->mem_ptr + IPS_REG_HISR);
5076         }
5077
5078         if (PostByte[0] < IPS_GOOD_POST_STATUS) {
5079                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
5080                            "reset controller fails (post status %x %x).\n",
5081                            PostByte[0], PostByte[1]);
5082
5083                 return (0);
5084         }
5085
5086         for (i = 0; i < IPS_MAX_CONFIG_BYTES; i++) {
5087                 for (j = 0; j < 240; j++) {
5088                         Isr = readb(ha->mem_ptr + IPS_REG_HISR);
5089                         if (Isr & IPS_BIT_GHI)
5090                                 break;
5091
5092                         /* Delay for 1 Second */
5093                         MDELAY(IPS_ONE_SEC);
5094                 }
5095
5096                 if (j >= 240)
5097                         /* error occurred */
5098                         return (0);
5099
5100                 ConfigByte[i] = readb(ha->mem_ptr + IPS_REG_ISPR);
5101                 writeb(Isr, ha->mem_ptr + IPS_REG_HISR);
5102         }
5103
5104         for (i = 0; i < 240; i++) {
5105                 Cbsp = readb(ha->mem_ptr + IPS_REG_CBSP);
5106
5107                 if ((Cbsp & IPS_BIT_OP) == 0)
5108                         break;
5109
5110                 /* Delay for 1 Second */
5111                 MDELAY(IPS_ONE_SEC);
5112         }
5113
5114         if (i >= 240)
5115                 /* error occurred */
5116                 return (0);
5117
5118         /* setup CCCR */
5119         writel(0x1010, ha->mem_ptr + IPS_REG_CCCR);
5120
5121         /* Enable busmastering */
5122         writeb(IPS_BIT_EBM, ha->mem_ptr + IPS_REG_SCPR);
5123
5124         if (ha->revision_id == IPS_REVID_TROMBONE64)
5125                 /* fix for anaconda64 */
5126                 writel(0, ha->mem_ptr + IPS_REG_NDAE);
5127
5128         /* Enable interrupts */
5129         writeb(IPS_BIT_EI, ha->mem_ptr + IPS_REG_HISR);
5130
5131         /* if we get here then everything went OK */
5132         return (1);
5133 }
5134
5135 /****************************************************************************/
5136 /*                                                                          */
5137 /* Routine Name: ips_init_morpheus                                          */
5138 /*                                                                          */
5139 /* Routine Description:                                                     */
5140 /*                                                                          */
5141 /*   Initialize a morpheus controller                                       */
5142 /*                                                                          */
5143 /****************************************************************************/
5144 static int
5145 ips_init_morpheus(ips_ha_t * ha)
5146 {
5147         uint32_t Post;
5148         uint32_t Config;
5149         uint32_t Isr;
5150         uint32_t Oimr;
5151         int i;
5152
5153         METHOD_TRACE("ips_init_morpheus", 1);
5154
5155         /* Wait up to 45 secs for Post */
5156         for (i = 0; i < 45; i++) {
5157                 Isr = readl(ha->mem_ptr + IPS_REG_I2O_HIR);
5158
5159                 if (Isr & IPS_BIT_I960_MSG0I)
5160                         break;
5161
5162                 /* Delay for 1 Second */
5163                 MDELAY(IPS_ONE_SEC);
5164         }
5165
5166         if (i >= 45) {
5167                 /* error occurred */
5168                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
5169                            "timeout waiting for post.\n");
5170
5171                 return (0);
5172         }
5173
5174         Post = readl(ha->mem_ptr + IPS_REG_I960_MSG0);
5175
5176         if (Post == 0x4F00) {   /* If Flashing the Battery PIC         */
5177                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
5178                            "Flashing Battery PIC, Please wait ...\n");
5179
5180                 /* Clear the interrupt bit */
5181                 Isr = (uint32_t) IPS_BIT_I960_MSG0I;
5182                 writel(Isr, ha->mem_ptr + IPS_REG_I2O_HIR);
5183
5184                 for (i = 0; i < 120; i++) {     /*    Wait Up to 2 Min. for Completion */
5185                         Post = readl(ha->mem_ptr + IPS_REG_I960_MSG0);
5186                         if (Post != 0x4F00)
5187                                 break;
5188                         /* Delay for 1 Second */
5189                         MDELAY(IPS_ONE_SEC);
5190                 }
5191
5192                 if (i >= 120) {
5193                         IPS_PRINTK(KERN_WARNING, ha->pcidev,
5194                                    "timeout waiting for Battery PIC Flash\n");
5195                         return (0);
5196                 }
5197
5198         }
5199
5200         /* Clear the interrupt bit */
5201         Isr = (uint32_t) IPS_BIT_I960_MSG0I;
5202         writel(Isr, ha->mem_ptr + IPS_REG_I2O_HIR);
5203
5204         if (Post < (IPS_GOOD_POST_STATUS << 8)) {
5205                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
5206                            "reset controller fails (post status %x).\n", Post);
5207
5208                 return (0);
5209         }
5210
5211         /* Wait up to 240 secs for config bytes */
5212         for (i = 0; i < 240; i++) {
5213                 Isr = readl(ha->mem_ptr + IPS_REG_I2O_HIR);
5214
5215                 if (Isr & IPS_BIT_I960_MSG1I)
5216                         break;
5217
5218                 /* Delay for 1 Second */
5219                 MDELAY(IPS_ONE_SEC);
5220         }
5221
5222         if (i >= 240) {
5223                 /* error occurred */
5224                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
5225                            "timeout waiting for config.\n");
5226
5227                 return (0);
5228         }
5229
5230         Config = readl(ha->mem_ptr + IPS_REG_I960_MSG1);
5231
5232         /* Clear interrupt bit */
5233         Isr = (uint32_t) IPS_BIT_I960_MSG1I;
5234         writel(Isr, ha->mem_ptr + IPS_REG_I2O_HIR);
5235
5236         /* Turn on the interrupts */
5237         Oimr = readl(ha->mem_ptr + IPS_REG_I960_OIMR);
5238         Oimr &= ~0x8;
5239         writel(Oimr, ha->mem_ptr + IPS_REG_I960_OIMR);
5240
5241         /* if we get here then everything went OK */
5242
5243         /* Since we did a RESET, an EraseStripeLock may be needed */
5244         if (Post == 0xEF10) {
5245                 if ((Config == 0x000F) || (Config == 0x0009))
5246                         ha->requires_esl = 1;
5247         }
5248
5249         return (1);
5250 }
5251
5252 /****************************************************************************/
5253 /*                                                                          */
5254 /* Routine Name: ips_reset_copperhead                                       */
5255 /*                                                                          */
5256 /* Routine Description:                                                     */
5257 /*                                                                          */
5258 /*   Reset the controller                                                   */
5259 /*                                                                          */
5260 /****************************************************************************/
5261 static int
5262 ips_reset_copperhead(ips_ha_t * ha)
5263 {
5264         int reset_counter;
5265
5266         METHOD_TRACE("ips_reset_copperhead", 1);
5267
5268         DEBUG_VAR(1, "(%s%d) ips_reset_copperhead: io addr: %x, irq: %d",
5269                   ips_name, ha->host_num, ha->io_addr, ha->irq);
5270
5271         reset_counter = 0;
5272
5273         while (reset_counter < 2) {
5274                 reset_counter++;
5275
5276                 outb(IPS_BIT_RST, ha->io_addr + IPS_REG_SCPR);
5277
5278                 /* Delay for 1 Second */
5279                 MDELAY(IPS_ONE_SEC);
5280
5281                 outb(0, ha->io_addr + IPS_REG_SCPR);
5282
5283                 /* Delay for 1 Second */
5284                 MDELAY(IPS_ONE_SEC);
5285
5286                 if ((*ha->func.init) (ha))
5287                         break;
5288                 else if (reset_counter >= 2) {
5289
5290                         return (0);
5291                 }
5292         }
5293
5294         return (1);
5295 }
5296
5297 /****************************************************************************/
5298 /*                                                                          */
5299 /* Routine Name: ips_reset_copperhead_memio                                 */
5300 /*                                                                          */
5301 /* Routine Description:                                                     */
5302 /*                                                                          */
5303 /*   Reset the controller                                                   */
5304 /*                                                                          */
5305 /****************************************************************************/
5306 static int
5307 ips_reset_copperhead_memio(ips_ha_t * ha)
5308 {
5309         int reset_counter;
5310
5311         METHOD_TRACE("ips_reset_copperhead_memio", 1);
5312
5313         DEBUG_VAR(1, "(%s%d) ips_reset_copperhead_memio: mem addr: %x, irq: %d",
5314                   ips_name, ha->host_num, ha->mem_addr, ha->irq);
5315
5316         reset_counter = 0;
5317
5318         while (reset_counter < 2) {
5319                 reset_counter++;
5320
5321                 writeb(IPS_BIT_RST, ha->mem_ptr + IPS_REG_SCPR);
5322
5323                 /* Delay for 1 Second */
5324                 MDELAY(IPS_ONE_SEC);
5325
5326                 writeb(0, ha->mem_ptr + IPS_REG_SCPR);
5327
5328                 /* Delay for 1 Second */
5329                 MDELAY(IPS_ONE_SEC);
5330
5331                 if ((*ha->func.init) (ha))
5332                         break;
5333                 else if (reset_counter >= 2) {
5334
5335                         return (0);
5336                 }
5337         }
5338
5339         return (1);
5340 }
5341
5342 /****************************************************************************/
5343 /*                                                                          */
5344 /* Routine Name: ips_reset_morpheus                                         */
5345 /*                                                                          */
5346 /* Routine Description:                                                     */
5347 /*                                                                          */
5348 /*   Reset the controller                                                   */
5349 /*                                                                          */
5350 /****************************************************************************/
5351 static int
5352 ips_reset_morpheus(ips_ha_t * ha)
5353 {
5354         int reset_counter;
5355         uint8_t junk;
5356
5357         METHOD_TRACE("ips_reset_morpheus", 1);
5358
5359         DEBUG_VAR(1, "(%s%d) ips_reset_morpheus: mem addr: %x, irq: %d",
5360                   ips_name, ha->host_num, ha->mem_addr, ha->irq);
5361
5362         reset_counter = 0;
5363
5364         while (reset_counter < 2) {
5365                 reset_counter++;
5366
5367                 writel(0x80000000, ha->mem_ptr + IPS_REG_I960_IDR);
5368
5369                 /* Delay for 5 Seconds */
5370                 MDELAY(5 * IPS_ONE_SEC);
5371
5372                 /* Do a PCI config read to wait for adapter */
5373                 pci_read_config_byte(ha->pcidev, 4, &junk);
5374
5375                 if ((*ha->func.init) (ha))
5376                         break;
5377                 else if (reset_counter >= 2) {
5378
5379                         return (0);
5380                 }
5381         }
5382
5383         return (1);
5384 }
5385
5386 /****************************************************************************/
5387 /*                                                                          */
5388 /* Routine Name: ips_statinit                                               */
5389 /*                                                                          */
5390 /* Routine Description:                                                     */
5391 /*                                                                          */
5392 /*   Initialize the status queues on the controller                         */
5393 /*                                                                          */
5394 /****************************************************************************/
5395 static void
5396 ips_statinit(ips_ha_t * ha)
5397 {
5398         uint32_t phys_status_start;
5399
5400         METHOD_TRACE("ips_statinit", 1);
5401
5402         ha->adapt->p_status_start = ha->adapt->status;
5403         ha->adapt->p_status_end = ha->adapt->status + IPS_MAX_CMDS;
5404         ha->adapt->p_status_tail = ha->adapt->status;
5405
5406         phys_status_start = ha->adapt->hw_status_start;
5407         outl(cpu_to_le32(phys_status_start), ha->io_addr + IPS_REG_SQSR);
5408         outl(cpu_to_le32(phys_status_start + IPS_STATUS_Q_SIZE),
5409              ha->io_addr + IPS_REG_SQER);
5410         outl(cpu_to_le32(phys_status_start + IPS_STATUS_SIZE),
5411              ha->io_addr + IPS_REG_SQHR);
5412         outl(cpu_to_le32(phys_status_start), ha->io_addr + IPS_REG_SQTR);
5413
5414         ha->adapt->hw_status_tail = phys_status_start;
5415 }
5416
5417 /****************************************************************************/
5418 /*                                                                          */
5419 /* Routine Name: ips_statinit_memio                                         */
5420 /*                                                                          */
5421 /* Routine Description:                                                     */
5422 /*                                                                          */
5423 /*   Initialize the status queues on the controller                         */
5424 /*                                                                          */
5425 /****************************************************************************/
5426 static void
5427 ips_statinit_memio(ips_ha_t * ha)
5428 {
5429         uint32_t phys_status_start;
5430
5431         METHOD_TRACE("ips_statinit_memio", 1);
5432
5433         ha->adapt->p_status_start = ha->adapt->status;
5434         ha->adapt->p_status_end = ha->adapt->status + IPS_MAX_CMDS;
5435         ha->adapt->p_status_tail = ha->adapt->status;
5436
5437         phys_status_start = ha->adapt->hw_status_start;
5438         writel(phys_status_start, ha->mem_ptr + IPS_REG_SQSR);
5439         writel(phys_status_start + IPS_STATUS_Q_SIZE,
5440                ha->mem_ptr + IPS_REG_SQER);
5441         writel(phys_status_start + IPS_STATUS_SIZE, ha->mem_ptr + IPS_REG_SQHR);
5442         writel(phys_status_start, ha->mem_ptr + IPS_REG_SQTR);
5443
5444         ha->adapt->hw_status_tail = phys_status_start;
5445 }
5446
5447 /****************************************************************************/
5448 /*                                                                          */
5449 /* Routine Name: ips_statupd_copperhead                                     */
5450 /*                                                                          */
5451 /* Routine Description:                                                     */
5452 /*                                                                          */
5453 /*   Remove an element from the status queue                                */
5454 /*                                                                          */
5455 /****************************************************************************/
5456 static uint32_t
5457 ips_statupd_copperhead(ips_ha_t * ha)
5458 {
5459         METHOD_TRACE("ips_statupd_copperhead", 1);
5460
5461         if (ha->adapt->p_status_tail != ha->adapt->p_status_end) {
5462                 ha->adapt->p_status_tail++;
5463                 ha->adapt->hw_status_tail += sizeof (IPS_STATUS);
5464         } else {
5465                 ha->adapt->p_status_tail = ha->adapt->p_status_start;
5466                 ha->adapt->hw_status_tail = ha->adapt->hw_status_start;
5467         }
5468
5469         outl(cpu_to_le32(ha->adapt->hw_status_tail),
5470              ha->io_addr + IPS_REG_SQTR);
5471
5472         return (ha->adapt->p_status_tail->value);
5473 }
5474
5475 /****************************************************************************/
5476 /*                                                                          */
5477 /* Routine Name: ips_statupd_copperhead_memio                               */
5478 /*                                                                          */
5479 /* Routine Description:                                                     */
5480 /*                                                                          */
5481 /*   Remove an element from the status queue                                */
5482 /*                                                                          */
5483 /****************************************************************************/
5484 static uint32_t
5485 ips_statupd_copperhead_memio(ips_ha_t * ha)
5486 {
5487         METHOD_TRACE("ips_statupd_copperhead_memio", 1);
5488
5489         if (ha->adapt->p_status_tail != ha->adapt->p_status_end) {
5490                 ha->adapt->p_status_tail++;
5491                 ha->adapt->hw_status_tail += sizeof (IPS_STATUS);
5492         } else {
5493                 ha->adapt->p_status_tail = ha->adapt->p_status_start;
5494                 ha->adapt->hw_status_tail = ha->adapt->hw_status_start;
5495         }
5496
5497         writel(ha->adapt->hw_status_tail, ha->mem_ptr + IPS_REG_SQTR);
5498
5499         return (ha->adapt->p_status_tail->value);
5500 }
5501
5502 /****************************************************************************/
5503 /*                                                                          */
5504 /* Routine Name: ips_statupd_morpheus                                       */
5505 /*                                                                          */
5506 /* Routine Description:                                                     */
5507 /*                                                                          */
5508 /*   Remove an element from the status queue                                */
5509 /*                                                                          */
5510 /****************************************************************************/
5511 static uint32_t
5512 ips_statupd_morpheus(ips_ha_t * ha)
5513 {
5514         uint32_t val;
5515
5516         METHOD_TRACE("ips_statupd_morpheus", 1);
5517
5518         val = readl(ha->mem_ptr + IPS_REG_I2O_OUTMSGQ);
5519
5520         return (val);
5521 }
5522
5523 /****************************************************************************/
5524 /*                                                                          */
5525 /* Routine Name: ips_issue_copperhead                                       */
5526 /*                                                                          */
5527 /* Routine Description:                                                     */
5528 /*                                                                          */
5529 /*   Send a command down to the controller                                  */
5530 /*                                                                          */
5531 /****************************************************************************/
5532 static int
5533 ips_issue_copperhead(ips_ha_t * ha, ips_scb_t * scb)
5534 {
5535         uint32_t TimeOut;
5536         uint32_t val;
5537
5538         METHOD_TRACE("ips_issue_copperhead", 1);
5539
5540         if (scb->scsi_cmd) {
5541                 DEBUG_VAR(2, "(%s%d) ips_issue: cmd 0x%X id %d (%d %d %d)",
5542                           ips_name,
5543                           ha->host_num,
5544                           scb->cdb[0],
5545                           scb->cmd.basic_io.command_id,
5546                           scb->bus, scb->target_id, scb->lun);
5547         } else {
5548                 DEBUG_VAR(2, KERN_NOTICE "(%s%d) ips_issue: logical cmd id %d",
5549                           ips_name, ha->host_num, scb->cmd.basic_io.command_id);
5550         }
5551
5552         TimeOut = 0;
5553
5554         while ((val =
5555                 le32_to_cpu(inl(ha->io_addr + IPS_REG_CCCR))) & IPS_BIT_SEM) {
5556                 udelay(1000);
5557
5558                 if (++TimeOut >= IPS_SEM_TIMEOUT) {
5559                         if (!(val & IPS_BIT_START_STOP))
5560                                 break;
5561
5562                         IPS_PRINTK(KERN_WARNING, ha->pcidev,
5563                                    "ips_issue val [0x%x].\n", val);
5564                         IPS_PRINTK(KERN_WARNING, ha->pcidev,
5565                                    "ips_issue semaphore chk timeout.\n");
5566
5567                         return (IPS_FAILURE);
5568                 }               /* end if */
5569         }                       /* end while */
5570
5571         outl(cpu_to_le32(scb->scb_busaddr), ha->io_addr + IPS_REG_CCSAR);
5572         outw(cpu_to_le32(IPS_BIT_START_CMD), ha->io_addr + IPS_REG_CCCR);
5573
5574         return (IPS_SUCCESS);
5575 }
5576
5577 /****************************************************************************/
5578 /*                                                                          */
5579 /* Routine Name: ips_issue_copperhead_memio                                 */
5580 /*                                                                          */
5581 /* Routine Description:                                                     */
5582 /*                                                                          */
5583 /*   Send a command down to the controller                                  */
5584 /*                                                                          */
5585 /****************************************************************************/
5586 static int
5587 ips_issue_copperhead_memio(ips_ha_t * ha, ips_scb_t * scb)
5588 {
5589         uint32_t TimeOut;
5590         uint32_t val;
5591
5592         METHOD_TRACE("ips_issue_copperhead_memio", 1);
5593
5594         if (scb->scsi_cmd) {
5595                 DEBUG_VAR(2, "(%s%d) ips_issue: cmd 0x%X id %d (%d %d %d)",
5596                           ips_name,
5597                           ha->host_num,
5598                           scb->cdb[0],
5599                           scb->cmd.basic_io.command_id,
5600                           scb->bus, scb->target_id, scb->lun);
5601         } else {
5602                 DEBUG_VAR(2, "(%s%d) ips_issue: logical cmd id %d",
5603                           ips_name, ha->host_num, scb->cmd.basic_io.command_id);
5604         }
5605
5606         TimeOut = 0;
5607
5608         while ((val = readl(ha->mem_ptr + IPS_REG_CCCR)) & IPS_BIT_SEM) {
5609                 udelay(1000);
5610
5611                 if (++TimeOut >= IPS_SEM_TIMEOUT) {
5612                         if (!(val & IPS_BIT_START_STOP))
5613                                 break;
5614
5615                         IPS_PRINTK(KERN_WARNING, ha->pcidev,
5616                                    "ips_issue val [0x%x].\n", val);
5617                         IPS_PRINTK(KERN_WARNING, ha->pcidev,
5618                                    "ips_issue semaphore chk timeout.\n");
5619
5620                         return (IPS_FAILURE);
5621                 }               /* end if */
5622         }                       /* end while */
5623
5624         writel(scb->scb_busaddr, ha->mem_ptr + IPS_REG_CCSAR);
5625         writel(IPS_BIT_START_CMD, ha->mem_ptr + IPS_REG_CCCR);
5626
5627         return (IPS_SUCCESS);
5628 }
5629
5630 /****************************************************************************/
5631 /*                                                                          */
5632 /* Routine Name: ips_issue_i2o                                              */
5633 /*                                                                          */
5634 /* Routine Description:                                                     */
5635 /*                                                                          */
5636 /*   Send a command down to the controller                                  */
5637 /*                                                                          */
5638 /****************************************************************************/
5639 static int
5640 ips_issue_i2o(ips_ha_t * ha, ips_scb_t * scb)
5641 {
5642
5643         METHOD_TRACE("ips_issue_i2o", 1);
5644
5645         if (scb->scsi_cmd) {
5646                 DEBUG_VAR(2, "(%s%d) ips_issue: cmd 0x%X id %d (%d %d %d)",
5647                           ips_name,
5648                           ha->host_num,
5649                           scb->cdb[0],
5650                           scb->cmd.basic_io.command_id,
5651                           scb->bus, scb->target_id, scb->lun);
5652         } else {
5653                 DEBUG_VAR(2, "(%s%d) ips_issue: logical cmd id %d",
5654                           ips_name, ha->host_num, scb->cmd.basic_io.command_id);
5655         }
5656
5657         outl(cpu_to_le32(scb->scb_busaddr), ha->io_addr + IPS_REG_I2O_INMSGQ);
5658
5659         return (IPS_SUCCESS);
5660 }
5661
5662 /****************************************************************************/
5663 /*                                                                          */
5664 /* Routine Name: ips_issue_i2o_memio                                        */
5665 /*                                                                          */
5666 /* Routine Description:                                                     */
5667 /*                                                                          */
5668 /*   Send a command down to the controller                                  */
5669 /*                                                                          */
5670 /****************************************************************************/
5671 static int
5672 ips_issue_i2o_memio(ips_ha_t * ha, ips_scb_t * scb)
5673 {
5674
5675         METHOD_TRACE("ips_issue_i2o_memio", 1);
5676
5677         if (scb->scsi_cmd) {
5678                 DEBUG_VAR(2, "(%s%d) ips_issue: cmd 0x%X id %d (%d %d %d)",
5679                           ips_name,
5680                           ha->host_num,
5681                           scb->cdb[0],
5682                           scb->cmd.basic_io.command_id,
5683                           scb->bus, scb->target_id, scb->lun);
5684         } else {
5685                 DEBUG_VAR(2, "(%s%d) ips_issue: logical cmd id %d",
5686                           ips_name, ha->host_num, scb->cmd.basic_io.command_id);
5687         }
5688
5689         writel(scb->scb_busaddr, ha->mem_ptr + IPS_REG_I2O_INMSGQ);
5690
5691         return (IPS_SUCCESS);
5692 }
5693
5694 /****************************************************************************/
5695 /*                                                                          */
5696 /* Routine Name: ips_isintr_copperhead                                      */
5697 /*                                                                          */
5698 /* Routine Description:                                                     */
5699 /*                                                                          */
5700 /*   Test to see if an interrupt is for us                                  */
5701 /*                                                                          */
5702 /****************************************************************************/
5703 static int
5704 ips_isintr_copperhead(ips_ha_t * ha)
5705 {
5706         uint8_t Isr;
5707
5708         METHOD_TRACE("ips_isintr_copperhead", 2);
5709
5710         Isr = inb(ha->io_addr + IPS_REG_HISR);
5711
5712         if (Isr == 0xFF)
5713                 /* ?!?! Nothing really there */
5714                 return (0);
5715
5716         if (Isr & IPS_BIT_SCE)
5717                 return (1);
5718         else if (Isr & (IPS_BIT_SQO | IPS_BIT_GHI)) {
5719                 /* status queue overflow or GHI */
5720                 /* just clear the interrupt */
5721                 outb(Isr, ha->io_addr + IPS_REG_HISR);
5722         }
5723
5724         return (0);
5725 }
5726
5727 /****************************************************************************/
5728 /*                                                                          */
5729 /* Routine Name: ips_isintr_copperhead_memio                                */
5730 /*                                                                          */
5731 /* Routine Description:                                                     */
5732 /*                                                                          */
5733 /*   Test to see if an interrupt is for us                                  */
5734 /*                                                                          */
5735 /****************************************************************************/
5736 static int
5737 ips_isintr_copperhead_memio(ips_ha_t * ha)
5738 {
5739         uint8_t Isr;
5740
5741         METHOD_TRACE("ips_isintr_memio", 2);
5742
5743         Isr = readb(ha->mem_ptr + IPS_REG_HISR);
5744
5745         if (Isr == 0xFF)
5746                 /* ?!?! Nothing really there */
5747                 return (0);
5748
5749         if (Isr & IPS_BIT_SCE)
5750                 return (1);
5751         else if (Isr & (IPS_BIT_SQO | IPS_BIT_GHI)) {
5752                 /* status queue overflow or GHI */
5753                 /* just clear the interrupt */
5754                 writeb(Isr, ha->mem_ptr + IPS_REG_HISR);
5755         }
5756
5757         return (0);
5758 }
5759
5760 /****************************************************************************/
5761 /*                                                                          */
5762 /* Routine Name: ips_isintr_morpheus                                        */
5763 /*                                                                          */
5764 /* Routine Description:                                                     */
5765 /*                                                                          */
5766 /*   Test to see if an interrupt is for us                                  */
5767 /*                                                                          */
5768 /****************************************************************************/
5769 static int
5770 ips_isintr_morpheus(ips_ha_t * ha)
5771 {
5772         uint32_t Isr;
5773
5774         METHOD_TRACE("ips_isintr_morpheus", 2);
5775
5776         Isr = readl(ha->mem_ptr + IPS_REG_I2O_HIR);
5777
5778         if (Isr & IPS_BIT_I2O_OPQI)
5779                 return (1);
5780         else
5781                 return (0);
5782 }
5783
5784 /****************************************************************************/
5785 /*                                                                          */
5786 /* Routine Name: ips_wait                                                   */
5787 /*                                                                          */
5788 /* Routine Description:                                                     */
5789 /*                                                                          */
5790 /*   Wait for a command to complete                                         */
5791 /*                                                                          */
5792 /****************************************************************************/
5793 static int
5794 ips_wait(ips_ha_t * ha, int time, int intr)
5795 {
5796         int ret;
5797         int done;
5798
5799         METHOD_TRACE("ips_wait", 1);
5800
5801         ret = IPS_FAILURE;
5802         done = FALSE;
5803
5804         time *= IPS_ONE_SEC;    /* convert seconds */
5805
5806         while ((time > 0) && (!done)) {
5807                 if (intr == IPS_INTR_ON) {
5808                         if (ha->waitflag == FALSE) {
5809                                 ret = IPS_SUCCESS;
5810                                 done = TRUE;
5811                                 break;
5812                         }
5813                 } else if (intr == IPS_INTR_IORL) {
5814                         if (ha->waitflag == FALSE) {
5815                                 /*
5816                                  * controller generated an interrupt to
5817                                  * acknowledge completion of the command
5818                                  * and ips_intr() has serviced the interrupt.
5819                                  */
5820                                 ret = IPS_SUCCESS;
5821                                 done = TRUE;
5822                                 break;
5823                         }
5824
5825                         /*
5826                          * NOTE: we already have the io_request_lock so
5827                          * even if we get an interrupt it won't get serviced
5828                          * until after we finish.
5829                          */
5830
5831                         (*ha->func.intr) (ha);
5832                 }
5833
5834                 /* This looks like a very evil loop, but it only does this during start-up */
5835                 udelay(1000);
5836                 time--;
5837         }
5838
5839         return (ret);
5840 }
5841
5842 /****************************************************************************/
5843 /*                                                                          */
5844 /* Routine Name: ips_write_driver_status                                    */
5845 /*                                                                          */
5846 /* Routine Description:                                                     */
5847 /*                                                                          */
5848 /*   Write OS/Driver version to Page 5 of the nvram on the controller       */
5849 /*                                                                          */
5850 /****************************************************************************/
5851 static int
5852 ips_write_driver_status(ips_ha_t * ha, int intr)
5853 {
5854         METHOD_TRACE("ips_write_driver_status", 1);
5855
5856         if (!ips_readwrite_page5(ha, FALSE, intr)) {
5857                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
5858                            "unable to read NVRAM page 5.\n");
5859
5860                 return (0);
5861         }
5862
5863         /* check to make sure the page has a valid */
5864         /* signature */
5865         if (le32_to_cpu(ha->nvram->signature) != IPS_NVRAM_P5_SIG) {
5866                 DEBUG_VAR(1,
5867                           "(%s%d) NVRAM page 5 has an invalid signature: %X.",
5868                           ips_name, ha->host_num, ha->nvram->signature);
5869                 ha->nvram->signature = IPS_NVRAM_P5_SIG;
5870         }
5871
5872         DEBUG_VAR(2,
5873                   "(%s%d) Ad Type: %d, Ad Slot: %d, BIOS: %c%c%c%c %c%c%c%c.",
5874                   ips_name, ha->host_num, le16_to_cpu(ha->nvram->adapter_type),
5875                   ha->nvram->adapter_slot, ha->nvram->bios_high[0],
5876                   ha->nvram->bios_high[1], ha->nvram->bios_high[2],
5877                   ha->nvram->bios_high[3], ha->nvram->bios_low[0],
5878                   ha->nvram->bios_low[1], ha->nvram->bios_low[2],
5879                   ha->nvram->bios_low[3]);
5880
5881         ips_get_bios_version(ha, intr);
5882
5883         /* change values (as needed) */
5884         ha->nvram->operating_system = IPS_OS_LINUX;
5885         ha->nvram->adapter_type = ha->ad_type;
5886         strncpy((char *) ha->nvram->driver_high, IPS_VERSION_HIGH, 4);
5887         strncpy((char *) ha->nvram->driver_low, IPS_VERSION_LOW, 4);
5888         strncpy((char *) ha->nvram->bios_high, ha->bios_version, 4);
5889         strncpy((char *) ha->nvram->bios_low, ha->bios_version + 4, 4);
5890
5891         ips_version_check(ha, intr);    /* Check BIOS/FW/Driver Versions */
5892
5893         /* now update the page */
5894         if (!ips_readwrite_page5(ha, TRUE, intr)) {
5895                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
5896                            "unable to write NVRAM page 5.\n");
5897
5898                 return (0);
5899         }
5900
5901         /* IF NVRAM Page 5 is OK, Use it for Slot Number Info Because Linux Doesn't Do Slots */
5902         ha->slot_num = ha->nvram->adapter_slot;
5903
5904         return (1);
5905 }
5906
5907 /****************************************************************************/
5908 /*                                                                          */
5909 /* Routine Name: ips_read_adapter_status                                    */
5910 /*                                                                          */
5911 /* Routine Description:                                                     */
5912 /*                                                                          */
5913 /*   Do an Inquiry command to the adapter                                   */
5914 /*                                                                          */
5915 /****************************************************************************/
5916 static int
5917 ips_read_adapter_status(ips_ha_t * ha, int intr)
5918 {
5919         ips_scb_t *scb;
5920         int ret;
5921
5922         METHOD_TRACE("ips_read_adapter_status", 1);
5923
5924         scb = &ha->scbs[ha->max_cmds - 1];
5925
5926         ips_init_scb(ha, scb);
5927
5928         scb->timeout = ips_cmd_timeout;
5929         scb->cdb[0] = IPS_CMD_ENQUIRY;
5930
5931         scb->cmd.basic_io.op_code = IPS_CMD_ENQUIRY;
5932         scb->cmd.basic_io.command_id = IPS_COMMAND_ID(ha, scb);
5933         scb->cmd.basic_io.sg_count = 0;
5934         scb->cmd.basic_io.lba = 0;
5935         scb->cmd.basic_io.sector_count = 0;
5936         scb->cmd.basic_io.log_drv = 0;
5937         scb->data_len = sizeof (*ha->enq);
5938         scb->cmd.basic_io.sg_addr = ha->enq_busaddr;
5939
5940         /* send command */
5941         if (((ret =
5942               ips_send_wait(ha, scb, ips_cmd_timeout, intr)) == IPS_FAILURE)
5943             || (ret == IPS_SUCCESS_IMM)
5944             || ((scb->basic_status & IPS_GSC_STATUS_MASK) > 1))
5945                 return (0);
5946
5947         return (1);
5948 }
5949
5950 /****************************************************************************/
5951 /*                                                                          */
5952 /* Routine Name: ips_read_subsystem_parameters                              */
5953 /*                                                                          */
5954 /* Routine Description:                                                     */
5955 /*                                                                          */
5956 /*   Read subsystem parameters from the adapter                             */
5957 /*                                                                          */
5958 /****************************************************************************/
5959 static int
5960 ips_read_subsystem_parameters(ips_ha_t * ha, int intr)
5961 {
5962         ips_scb_t *scb;
5963         int ret;
5964
5965         METHOD_TRACE("ips_read_subsystem_parameters", 1);
5966
5967         scb = &ha->scbs[ha->max_cmds - 1];
5968
5969         ips_init_scb(ha, scb);
5970
5971         scb->timeout = ips_cmd_timeout;
5972         scb->cdb[0] = IPS_CMD_GET_SUBSYS;
5973
5974         scb->cmd.basic_io.op_code = IPS_CMD_GET_SUBSYS;
5975         scb->cmd.basic_io.command_id = IPS_COMMAND_ID(ha, scb);
5976         scb->cmd.basic_io.sg_count = 0;
5977         scb->cmd.basic_io.lba = 0;
5978         scb->cmd.basic_io.sector_count = 0;
5979         scb->cmd.basic_io.log_drv = 0;
5980         scb->data_len = sizeof (*ha->subsys);
5981         scb->cmd.basic_io.sg_addr = ha->ioctl_busaddr;
5982
5983         /* send command */
5984         if (((ret =
5985               ips_send_wait(ha, scb, ips_cmd_timeout, intr)) == IPS_FAILURE)
5986             || (ret == IPS_SUCCESS_IMM)
5987             || ((scb->basic_status & IPS_GSC_STATUS_MASK) > 1))
5988                 return (0);
5989
5990         memcpy(ha->subsys, ha->ioctl_data, sizeof(*ha->subsys));
5991         return (1);
5992 }
5993
5994 /****************************************************************************/
5995 /*                                                                          */
5996 /* Routine Name: ips_read_config                                            */
5997 /*                                                                          */
5998 /* Routine Description:                                                     */
5999 /*                                                                          */
6000 /*   Read the configuration on the adapter                                  */
6001 /*                                                                          */
6002 /****************************************************************************/
6003 static int
6004 ips_read_config(ips_ha_t * ha, int intr)
6005 {
6006         ips_scb_t *scb;
6007         int i;
6008         int ret;
6009
6010         METHOD_TRACE("ips_read_config", 1);
6011
6012         /* set defaults for initiator IDs */
6013         for (i = 0; i < 4; i++)
6014                 ha->conf->init_id[i] = 7;
6015
6016         scb = &ha->scbs[ha->max_cmds - 1];
6017
6018         ips_init_scb(ha, scb);
6019
6020         scb->timeout = ips_cmd_timeout;
6021         scb->cdb[0] = IPS_CMD_READ_CONF;
6022
6023         scb->cmd.basic_io.op_code = IPS_CMD_READ_CONF;
6024         scb->cmd.basic_io.command_id = IPS_COMMAND_ID(ha, scb);
6025         scb->data_len = sizeof (*ha->conf);
6026         scb->cmd.basic_io.sg_addr = ha->ioctl_busaddr;
6027
6028         /* send command */
6029         if (((ret =
6030               ips_send_wait(ha, scb, ips_cmd_timeout, intr)) == IPS_FAILURE)
6031             || (ret == IPS_SUCCESS_IMM)
6032             || ((scb->basic_status & IPS_GSC_STATUS_MASK) > 1)) {
6033
6034                 memset(ha->conf, 0, sizeof (IPS_CONF));
6035
6036                 /* reset initiator IDs */
6037                 for (i = 0; i < 4; i++)
6038                         ha->conf->init_id[i] = 7;
6039
6040                 /* Allow Completed with Errors, so JCRM can access the Adapter to fix the problems */
6041                 if ((scb->basic_status & IPS_GSC_STATUS_MASK) ==
6042                     IPS_CMD_CMPLT_WERROR)
6043                         return (1);
6044
6045                 return (0);
6046         }
6047         
6048         memcpy(ha->conf, ha->ioctl_data, sizeof(*ha->conf));
6049         return (1);
6050 }
6051
6052 /****************************************************************************/
6053 /*                                                                          */
6054 /* Routine Name: ips_readwrite_page5                                        */
6055 /*                                                                          */
6056 /* Routine Description:                                                     */
6057 /*                                                                          */
6058 /*   Read nvram page 5 from the adapter                                     */
6059 /*                                                                          */
6060 /****************************************************************************/
6061 static int
6062 ips_readwrite_page5(ips_ha_t * ha, int write, int intr)
6063 {
6064         ips_scb_t *scb;
6065         int ret;
6066
6067         METHOD_TRACE("ips_readwrite_page5", 1);
6068
6069         scb = &ha->scbs[ha->max_cmds - 1];
6070
6071         ips_init_scb(ha, scb);
6072
6073         scb->timeout = ips_cmd_timeout;
6074         scb->cdb[0] = IPS_CMD_RW_NVRAM_PAGE;
6075
6076         scb->cmd.nvram.op_code = IPS_CMD_RW_NVRAM_PAGE;
6077         scb->cmd.nvram.command_id = IPS_COMMAND_ID(ha, scb);
6078         scb->cmd.nvram.page = 5;
6079         scb->cmd.nvram.write = write;
6080         scb->cmd.nvram.reserved = 0;
6081         scb->cmd.nvram.reserved2 = 0;
6082         scb->data_len = sizeof (*ha->nvram);
6083         scb->cmd.nvram.buffer_addr = ha->ioctl_busaddr;
6084         if (write)
6085                 memcpy(ha->ioctl_data, ha->nvram, sizeof(*ha->nvram));
6086         
6087         /* issue the command */
6088         if (((ret =
6089               ips_send_wait(ha, scb, ips_cmd_timeout, intr)) == IPS_FAILURE)
6090             || (ret == IPS_SUCCESS_IMM)
6091             || ((scb->basic_status & IPS_GSC_STATUS_MASK) > 1)) {
6092
6093                 memset(ha->nvram, 0, sizeof (IPS_NVRAM_P5));
6094
6095                 return (0);
6096         }
6097         if (!write)
6098                 memcpy(ha->nvram, ha->ioctl_data, sizeof(*ha->nvram));
6099         return (1);
6100 }
6101
6102 /****************************************************************************/
6103 /*                                                                          */
6104 /* Routine Name: ips_clear_adapter                                          */
6105 /*                                                                          */
6106 /* Routine Description:                                                     */
6107 /*                                                                          */
6108 /*   Clear the stripe lock tables                                           */
6109 /*                                                                          */
6110 /****************************************************************************/
6111 static int
6112 ips_clear_adapter(ips_ha_t * ha, int intr)
6113 {
6114         ips_scb_t *scb;
6115         int ret;
6116
6117         METHOD_TRACE("ips_clear_adapter", 1);
6118
6119         scb = &ha->scbs[ha->max_cmds - 1];
6120
6121         ips_init_scb(ha, scb);
6122
6123         scb->timeout = ips_reset_timeout;
6124         scb->cdb[0] = IPS_CMD_CONFIG_SYNC;
6125
6126         scb->cmd.config_sync.op_code = IPS_CMD_CONFIG_SYNC;
6127         scb->cmd.config_sync.command_id = IPS_COMMAND_ID(ha, scb);
6128         scb->cmd.config_sync.channel = 0;
6129         scb->cmd.config_sync.source_target = IPS_POCL;
6130         scb->cmd.config_sync.reserved = 0;
6131         scb->cmd.config_sync.reserved2 = 0;
6132         scb->cmd.config_sync.reserved3 = 0;
6133
6134         /* issue command */
6135         if (((ret =
6136               ips_send_wait(ha, scb, ips_reset_timeout, intr)) == IPS_FAILURE)
6137             || (ret == IPS_SUCCESS_IMM)
6138             || ((scb->basic_status & IPS_GSC_STATUS_MASK) > 1))
6139                 return (0);
6140
6141         /* send unlock stripe command */
6142         ips_init_scb(ha, scb);
6143
6144         scb->cdb[0] = IPS_CMD_ERROR_TABLE;
6145         scb->timeout = ips_reset_timeout;
6146
6147         scb->cmd.unlock_stripe.op_code = IPS_CMD_ERROR_TABLE;
6148         scb->cmd.unlock_stripe.command_id = IPS_COMMAND_ID(ha, scb);
6149         scb->cmd.unlock_stripe.log_drv = 0;
6150         scb->cmd.unlock_stripe.control = IPS_CSL;
6151         scb->cmd.unlock_stripe.reserved = 0;
6152         scb->cmd.unlock_stripe.reserved2 = 0;
6153         scb->cmd.unlock_stripe.reserved3 = 0;
6154
6155         /* issue command */
6156         if (((ret =
6157               ips_send_wait(ha, scb, ips_cmd_timeout, intr)) == IPS_FAILURE)
6158             || (ret == IPS_SUCCESS_IMM)
6159             || ((scb->basic_status & IPS_GSC_STATUS_MASK) > 1))
6160                 return (0);
6161
6162         return (1);
6163 }
6164
6165 /****************************************************************************/
6166 /*                                                                          */
6167 /* Routine Name: ips_ffdc_reset                                             */
6168 /*                                                                          */
6169 /* Routine Description:                                                     */
6170 /*                                                                          */
6171 /*   FFDC: write reset info                                                 */
6172 /*                                                                          */
6173 /****************************************************************************/
6174 static void
6175 ips_ffdc_reset(ips_ha_t * ha, int intr)
6176 {
6177         ips_scb_t *scb;
6178
6179         METHOD_TRACE("ips_ffdc_reset", 1);
6180
6181         scb = &ha->scbs[ha->max_cmds - 1];
6182
6183         ips_init_scb(ha, scb);
6184
6185         scb->timeout = ips_cmd_timeout;
6186         scb->cdb[0] = IPS_CMD_FFDC;
6187         scb->cmd.ffdc.op_code = IPS_CMD_FFDC;
6188         scb->cmd.ffdc.command_id = IPS_COMMAND_ID(ha, scb);
6189         scb->cmd.ffdc.reset_count = ha->reset_count;
6190         scb->cmd.ffdc.reset_type = 0x80;
6191
6192         /* convert time to what the card wants */
6193         ips_fix_ffdc_time(ha, scb, ha->last_ffdc);
6194
6195         /* issue command */
6196         ips_send_wait(ha, scb, ips_cmd_timeout, intr);
6197 }
6198
6199 /****************************************************************************/
6200 /*                                                                          */
6201 /* Routine Name: ips_ffdc_time                                              */
6202 /*                                                                          */
6203 /* Routine Description:                                                     */
6204 /*                                                                          */
6205 /*   FFDC: write time info                                                  */
6206 /*                                                                          */
6207 /****************************************************************************/
6208 static void
6209 ips_ffdc_time(ips_ha_t * ha)
6210 {
6211         ips_scb_t *scb;
6212
6213         METHOD_TRACE("ips_ffdc_time", 1);
6214
6215         DEBUG_VAR(1, "(%s%d) Sending time update.", ips_name, ha->host_num);
6216
6217         scb = &ha->scbs[ha->max_cmds - 1];
6218
6219         ips_init_scb(ha, scb);
6220
6221         scb->timeout = ips_cmd_timeout;
6222         scb->cdb[0] = IPS_CMD_FFDC;
6223         scb->cmd.ffdc.op_code = IPS_CMD_FFDC;
6224         scb->cmd.ffdc.command_id = IPS_COMMAND_ID(ha, scb);
6225         scb->cmd.ffdc.reset_count = 0;
6226         scb->cmd.ffdc.reset_type = 0;
6227
6228         /* convert time to what the card wants */
6229         ips_fix_ffdc_time(ha, scb, ha->last_ffdc);
6230
6231         /* issue command */
6232         ips_send_wait(ha, scb, ips_cmd_timeout, IPS_FFDC);
6233 }
6234
6235 /****************************************************************************/
6236 /*                                                                          */
6237 /* Routine Name: ips_fix_ffdc_time                                          */
6238 /*                                                                          */
6239 /* Routine Description:                                                     */
6240 /*   Adjust time_t to what the card wants                                   */
6241 /*                                                                          */
6242 /****************************************************************************/
6243 static void
6244 ips_fix_ffdc_time(ips_ha_t * ha, ips_scb_t * scb, time_t current_time)
6245 {
6246         long days;
6247         long rem;
6248         int i;
6249         int year;
6250         int yleap;
6251         int year_lengths[2] = { IPS_DAYS_NORMAL_YEAR, IPS_DAYS_LEAP_YEAR };
6252         int month_lengths[12][2] = { {31, 31},
6253         {28, 29},
6254         {31, 31},
6255         {30, 30},
6256         {31, 31},
6257         {30, 30},
6258         {31, 31},
6259         {31, 31},
6260         {30, 30},
6261         {31, 31},
6262         {30, 30},
6263         {31, 31}
6264         };
6265
6266         METHOD_TRACE("ips_fix_ffdc_time", 1);
6267
6268         days = current_time / IPS_SECS_DAY;
6269         rem = current_time % IPS_SECS_DAY;
6270
6271         scb->cmd.ffdc.hour = (rem / IPS_SECS_HOUR);
6272         rem = rem % IPS_SECS_HOUR;
6273         scb->cmd.ffdc.minute = (rem / IPS_SECS_MIN);
6274         scb->cmd.ffdc.second = (rem % IPS_SECS_MIN);
6275
6276         year = IPS_EPOCH_YEAR;
6277         while (days < 0 || days >= year_lengths[yleap = IPS_IS_LEAP_YEAR(year)]) {
6278                 int newy;
6279
6280                 newy = year + (days / IPS_DAYS_NORMAL_YEAR);
6281                 if (days < 0)
6282                         --newy;
6283                 days -= (newy - year) * IPS_DAYS_NORMAL_YEAR +
6284                     IPS_NUM_LEAP_YEARS_THROUGH(newy - 1) -
6285                     IPS_NUM_LEAP_YEARS_THROUGH(year - 1);
6286                 year = newy;
6287         }
6288
6289         scb->cmd.ffdc.yearH = year / 100;
6290         scb->cmd.ffdc.yearL = year % 100;
6291
6292         for (i = 0; days >= month_lengths[i][yleap]; ++i)
6293                 days -= month_lengths[i][yleap];
6294
6295         scb->cmd.ffdc.month = i + 1;
6296         scb->cmd.ffdc.day = days + 1;
6297 }
6298
6299 /****************************************************************************
6300  * BIOS Flash Routines                                                      *
6301  ****************************************************************************/
6302
6303 /****************************************************************************/
6304 /*                                                                          */
6305 /* Routine Name: ips_erase_bios                                             */
6306 /*                                                                          */
6307 /* Routine Description:                                                     */
6308 /*   Erase the BIOS on the adapter                                          */
6309 /*                                                                          */
6310 /****************************************************************************/
6311 static int
6312 ips_erase_bios(ips_ha_t * ha)
6313 {
6314         int timeout;
6315         uint8_t status = 0;
6316
6317         METHOD_TRACE("ips_erase_bios", 1);
6318
6319         status = 0;
6320
6321         /* Clear the status register */
6322         outl(0, ha->io_addr + IPS_REG_FLAP);
6323         if (ha->revision_id == IPS_REVID_TROMBONE64)
6324                 udelay(25);     /* 25 us */
6325
6326         outb(0x50, ha->io_addr + IPS_REG_FLDP);
6327         if (ha->revision_id == IPS_REVID_TROMBONE64)
6328                 udelay(25);     /* 25 us */
6329
6330         /* Erase Setup */
6331         outb(0x20, ha->io_addr + IPS_REG_FLDP);
6332         if (ha->revision_id == IPS_REVID_TROMBONE64)
6333                 udelay(25);     /* 25 us */
6334
6335         /* Erase Confirm */
6336         outb(0xD0, ha->io_addr + IPS_REG_FLDP);
6337         if (ha->revision_id == IPS_REVID_TROMBONE64)
6338                 udelay(25);     /* 25 us */
6339
6340         /* Erase Status */
6341         outb(0x70, ha->io_addr + IPS_REG_FLDP);
6342         if (ha->revision_id == IPS_REVID_TROMBONE64)
6343                 udelay(25);     /* 25 us */
6344
6345         timeout = 80000;        /* 80 seconds */
6346
6347         while (timeout > 0) {
6348                 if (ha->revision_id == IPS_REVID_TROMBONE64) {
6349                         outl(0, ha->io_addr + IPS_REG_FLAP);
6350                         udelay(25);     /* 25 us */
6351                 }
6352
6353                 status = inb(ha->io_addr + IPS_REG_FLDP);
6354
6355                 if (status & 0x80)
6356                         break;
6357
6358                 MDELAY(1);
6359                 timeout--;
6360         }
6361
6362         /* check for timeout */
6363         if (timeout <= 0) {
6364                 /* timeout */
6365
6366                 /* try to suspend the erase */
6367                 outb(0xB0, ha->io_addr + IPS_REG_FLDP);
6368                 if (ha->revision_id == IPS_REVID_TROMBONE64)
6369                         udelay(25);     /* 25 us */
6370
6371                 /* wait for 10 seconds */
6372                 timeout = 10000;
6373                 while (timeout > 0) {
6374                         if (ha->revision_id == IPS_REVID_TROMBONE64) {
6375                                 outl(0, ha->io_addr + IPS_REG_FLAP);
6376                                 udelay(25);     /* 25 us */
6377                         }
6378
6379                         status = inb(ha->io_addr + IPS_REG_FLDP);
6380
6381                         if (status & 0xC0)
6382                                 break;
6383
6384                         MDELAY(1);
6385                         timeout--;
6386                 }
6387
6388                 return (1);
6389         }
6390
6391         /* check for valid VPP */
6392         if (status & 0x08)
6393                 /* VPP failure */
6394                 return (1);
6395
6396         /* check for succesful flash */
6397         if (status & 0x30)
6398                 /* sequence error */
6399                 return (1);
6400
6401         /* Otherwise, we were successful */
6402         /* clear status */
6403         outb(0x50, ha->io_addr + IPS_REG_FLDP);
6404         if (ha->revision_id == IPS_REVID_TROMBONE64)
6405                 udelay(25);     /* 25 us */
6406
6407         /* enable reads */
6408         outb(0xFF, ha->io_addr + IPS_REG_FLDP);
6409         if (ha->revision_id == IPS_REVID_TROMBONE64)
6410                 udelay(25);     /* 25 us */
6411
6412         return (0);
6413 }
6414
6415 /****************************************************************************/
6416 /*                                                                          */
6417 /* Routine Name: ips_erase_bios_memio                                       */
6418 /*                                                                          */
6419 /* Routine Description:                                                     */
6420 /*   Erase the BIOS on the adapter                                          */
6421 /*                                                                          */
6422 /****************************************************************************/
6423 static int
6424 ips_erase_bios_memio(ips_ha_t * ha)
6425 {
6426         int timeout;
6427         uint8_t status;
6428
6429         METHOD_TRACE("ips_erase_bios_memio", 1);
6430
6431         status = 0;
6432
6433         /* Clear the status register */
6434         writel(0, ha->mem_ptr + IPS_REG_FLAP);
6435         if (ha->revision_id == IPS_REVID_TROMBONE64)
6436                 udelay(25);     /* 25 us */
6437
6438         writeb(0x50, ha->mem_ptr + IPS_REG_FLDP);
6439         if (ha->revision_id == IPS_REVID_TROMBONE64)
6440                 udelay(25);     /* 25 us */
6441
6442         /* Erase Setup */
6443         writeb(0x20, ha->mem_ptr + IPS_REG_FLDP);
6444         if (ha->revision_id == IPS_REVID_TROMBONE64)
6445                 udelay(25);     /* 25 us */
6446
6447         /* Erase Confirm */
6448         writeb(0xD0, ha->mem_ptr + IPS_REG_FLDP);
6449         if (ha->revision_id == IPS_REVID_TROMBONE64)
6450                 udelay(25);     /* 25 us */
6451
6452         /* Erase Status */
6453         writeb(0x70, ha->mem_ptr + IPS_REG_FLDP);
6454         if (ha->revision_id == IPS_REVID_TROMBONE64)
6455                 udelay(25);     /* 25 us */
6456
6457         timeout = 80000;        /* 80 seconds */
6458
6459         while (timeout > 0) {
6460                 if (ha->revision_id == IPS_REVID_TROMBONE64) {
6461                         writel(0, ha->mem_ptr + IPS_REG_FLAP);
6462                         udelay(25);     /* 25 us */
6463                 }
6464
6465                 status = readb(ha->mem_ptr + IPS_REG_FLDP);
6466
6467                 if (status & 0x80)
6468                         break;
6469
6470                 MDELAY(1);
6471                 timeout--;
6472         }
6473
6474         /* check for timeout */
6475         if (timeout <= 0) {
6476                 /* timeout */
6477
6478                 /* try to suspend the erase */
6479                 writeb(0xB0, ha->mem_ptr + IPS_REG_FLDP);
6480                 if (ha->revision_id == IPS_REVID_TROMBONE64)
6481                         udelay(25);     /* 25 us */
6482
6483                 /* wait for 10 seconds */
6484                 timeout = 10000;
6485                 while (timeout > 0) {
6486                         if (ha->revision_id == IPS_REVID_TROMBONE64) {
6487                                 writel(0, ha->mem_ptr + IPS_REG_FLAP);
6488                                 udelay(25);     /* 25 us */
6489                         }
6490
6491                         status = readb(ha->mem_ptr + IPS_REG_FLDP);
6492
6493                         if (status & 0xC0)
6494                                 break;
6495
6496                         MDELAY(1);
6497                         timeout--;
6498                 }
6499
6500                 return (1);
6501         }
6502
6503         /* check for valid VPP */
6504         if (status & 0x08)
6505                 /* VPP failure */
6506                 return (1);
6507
6508         /* check for succesful flash */
6509         if (status & 0x30)
6510                 /* sequence error */
6511                 return (1);
6512
6513         /* Otherwise, we were successful */
6514         /* clear status */
6515         writeb(0x50, ha->mem_ptr + IPS_REG_FLDP);
6516         if (ha->revision_id == IPS_REVID_TROMBONE64)
6517                 udelay(25);     /* 25 us */
6518
6519         /* enable reads */
6520         writeb(0xFF, ha->mem_ptr + IPS_REG_FLDP);
6521         if (ha->revision_id == IPS_REVID_TROMBONE64)
6522                 udelay(25);     /* 25 us */
6523
6524         return (0);
6525 }
6526
6527 /****************************************************************************/
6528 /*                                                                          */
6529 /* Routine Name: ips_program_bios                                           */
6530 /*                                                                          */
6531 /* Routine Description:                                                     */
6532 /*   Program the BIOS on the adapter                                        */
6533 /*                                                                          */
6534 /****************************************************************************/
6535 static int
6536 ips_program_bios(ips_ha_t * ha, char *buffer, uint32_t buffersize,
6537                  uint32_t offset)
6538 {
6539         int i;
6540         int timeout;
6541         uint8_t status = 0;
6542
6543         METHOD_TRACE("ips_program_bios", 1);
6544
6545         status = 0;
6546
6547         for (i = 0; i < buffersize; i++) {
6548                 /* write a byte */
6549                 outl(cpu_to_le32(i + offset), ha->io_addr + IPS_REG_FLAP);
6550                 if (ha->revision_id == IPS_REVID_TROMBONE64)
6551                         udelay(25);     /* 25 us */
6552
6553                 outb(0x40, ha->io_addr + IPS_REG_FLDP);
6554                 if (ha->revision_id == IPS_REVID_TROMBONE64)
6555                         udelay(25);     /* 25 us */
6556
6557                 outb(buffer[i], ha->io_addr + IPS_REG_FLDP);
6558                 if (ha->revision_id == IPS_REVID_TROMBONE64)
6559                         udelay(25);     /* 25 us */
6560
6561                 /* wait up to one second */
6562                 timeout = 1000;
6563                 while (timeout > 0) {
6564                         if (ha->revision_id == IPS_REVID_TROMBONE64) {
6565                                 outl(0, ha->io_addr + IPS_REG_FLAP);
6566                                 udelay(25);     /* 25 us */
6567                         }
6568
6569                         status = inb(ha->io_addr + IPS_REG_FLDP);
6570
6571                         if (status & 0x80)
6572                                 break;
6573
6574                         MDELAY(1);
6575                         timeout--;
6576                 }
6577
6578                 if (timeout == 0) {
6579                         /* timeout error */
6580                         outl(0, ha->io_addr + IPS_REG_FLAP);
6581                         if (ha->revision_id == IPS_REVID_TROMBONE64)
6582                                 udelay(25);     /* 25 us */
6583
6584                         outb(0xFF, ha->io_addr + IPS_REG_FLDP);
6585                         if (ha->revision_id == IPS_REVID_TROMBONE64)
6586                                 udelay(25);     /* 25 us */
6587
6588                         return (1);
6589                 }
6590
6591                 /* check the status */
6592                 if (status & 0x18) {
6593                         /* programming error */
6594                         outl(0, ha->io_addr + IPS_REG_FLAP);
6595                         if (ha->revision_id == IPS_REVID_TROMBONE64)
6596                                 udelay(25);     /* 25 us */
6597
6598                         outb(0xFF, ha->io_addr + IPS_REG_FLDP);
6599                         if (ha->revision_id == IPS_REVID_TROMBONE64)
6600                                 udelay(25);     /* 25 us */
6601
6602                         return (1);
6603                 }
6604         }                       /* end for */
6605
6606         /* Enable reading */
6607         outl(0, ha->io_addr + IPS_REG_FLAP);
6608         if (ha->revision_id == IPS_REVID_TROMBONE64)
6609                 udelay(25);     /* 25 us */
6610
6611         outb(0xFF, ha->io_addr + IPS_REG_FLDP);
6612         if (ha->revision_id == IPS_REVID_TROMBONE64)
6613                 udelay(25);     /* 25 us */
6614
6615         return (0);
6616 }
6617
6618 /****************************************************************************/
6619 /*                                                                          */
6620 /* Routine Name: ips_program_bios_memio                                     */
6621 /*                                                                          */
6622 /* Routine Description:                                                     */
6623 /*   Program the BIOS on the adapter                                        */
6624 /*                                                                          */
6625 /****************************************************************************/
6626 static int
6627 ips_program_bios_memio(ips_ha_t * ha, char *buffer, uint32_t buffersize,
6628                        uint32_t offset)
6629 {
6630         int i;
6631         int timeout;
6632         uint8_t status = 0;
6633
6634         METHOD_TRACE("ips_program_bios_memio", 1);
6635
6636         status = 0;
6637
6638         for (i = 0; i < buffersize; i++) {
6639                 /* write a byte */
6640                 writel(i + offset, ha->mem_ptr + IPS_REG_FLAP);
6641                 if (ha->revision_id == IPS_REVID_TROMBONE64)
6642                         udelay(25);     /* 25 us */
6643
6644                 writeb(0x40, ha->mem_ptr + IPS_REG_FLDP);
6645                 if (ha->revision_id == IPS_REVID_TROMBONE64)
6646                         udelay(25);     /* 25 us */
6647
6648                 writeb(buffer[i], ha->mem_ptr + IPS_REG_FLDP);
6649                 if (ha->revision_id == IPS_REVID_TROMBONE64)
6650                         udelay(25);     /* 25 us */
6651
6652                 /* wait up to one second */
6653                 timeout = 1000;
6654                 while (timeout > 0) {
6655                         if (ha->revision_id == IPS_REVID_TROMBONE64) {
6656                                 writel(0, ha->mem_ptr + IPS_REG_FLAP);
6657                                 udelay(25);     /* 25 us */
6658                         }
6659
6660                         status = readb(ha->mem_ptr + IPS_REG_FLDP);
6661
6662                         if (status & 0x80)
6663                                 break;
6664
6665                         MDELAY(1);
6666                         timeout--;
6667                 }
6668
6669                 if (timeout == 0) {
6670                         /* timeout error */
6671                         writel(0, ha->mem_ptr + IPS_REG_FLAP);
6672                         if (ha->revision_id == IPS_REVID_TROMBONE64)
6673                                 udelay(25);     /* 25 us */
6674
6675                         writeb(0xFF, ha->mem_ptr + IPS_REG_FLDP);
6676                         if (ha->revision_id == IPS_REVID_TROMBONE64)
6677                                 udelay(25);     /* 25 us */
6678
6679                         return (1);
6680                 }
6681
6682                 /* check the status */
6683                 if (status & 0x18) {
6684                         /* programming error */
6685                         writel(0, ha->mem_ptr + IPS_REG_FLAP);
6686                         if (ha->revision_id == IPS_REVID_TROMBONE64)
6687                                 udelay(25);     /* 25 us */
6688
6689                         writeb(0xFF, ha->mem_ptr + IPS_REG_FLDP);
6690                         if (ha->revision_id == IPS_REVID_TROMBONE64)
6691                                 udelay(25);     /* 25 us */
6692
6693                         return (1);
6694                 }
6695         }                       /* end for */
6696
6697         /* Enable reading */
6698         writel(0, ha->mem_ptr + IPS_REG_FLAP);
6699         if (ha->revision_id == IPS_REVID_TROMBONE64)
6700                 udelay(25);     /* 25 us */
6701
6702         writeb(0xFF, ha->mem_ptr + IPS_REG_FLDP);
6703         if (ha->revision_id == IPS_REVID_TROMBONE64)
6704                 udelay(25);     /* 25 us */
6705
6706         return (0);
6707 }
6708
6709 /****************************************************************************/
6710 /*                                                                          */
6711 /* Routine Name: ips_verify_bios                                            */
6712 /*                                                                          */
6713 /* Routine Description:                                                     */
6714 /*   Verify the BIOS on the adapter                                         */
6715 /*                                                                          */
6716 /****************************************************************************/
6717 static int
6718 ips_verify_bios(ips_ha_t * ha, char *buffer, uint32_t buffersize,
6719                 uint32_t offset)
6720 {
6721         uint8_t checksum;
6722         int i;
6723
6724         METHOD_TRACE("ips_verify_bios", 1);
6725
6726         /* test 1st byte */
6727         outl(0, ha->io_addr + IPS_REG_FLAP);
6728         if (ha->revision_id == IPS_REVID_TROMBONE64)
6729                 udelay(25);     /* 25 us */
6730
6731         if (inb(ha->io_addr + IPS_REG_FLDP) != 0x55)
6732                 return (1);
6733
6734         outl(cpu_to_le32(1), ha->io_addr + IPS_REG_FLAP);
6735         if (ha->revision_id == IPS_REVID_TROMBONE64)
6736                 udelay(25);     /* 25 us */
6737         if (inb(ha->io_addr + IPS_REG_FLDP) != 0xAA)
6738                 return (1);
6739
6740         checksum = 0xff;
6741         for (i = 2; i < buffersize; i++) {
6742
6743                 outl(cpu_to_le32(i + offset), ha->io_addr + IPS_REG_FLAP);
6744                 if (ha->revision_id == IPS_REVID_TROMBONE64)
6745                         udelay(25);     /* 25 us */
6746
6747                 checksum = (uint8_t) checksum + inb(ha->io_addr + IPS_REG_FLDP);
6748         }
6749
6750         if (checksum != 0)
6751                 /* failure */
6752                 return (1);
6753         else
6754                 /* success */
6755                 return (0);
6756 }
6757
6758 /****************************************************************************/
6759 /*                                                                          */
6760 /* Routine Name: ips_verify_bios_memio                                      */
6761 /*                                                                          */
6762 /* Routine Description:                                                     */
6763 /*   Verify the BIOS on the adapter                                         */
6764 /*                                                                          */
6765 /****************************************************************************/
6766 static int
6767 ips_verify_bios_memio(ips_ha_t * ha, char *buffer, uint32_t buffersize,
6768                       uint32_t offset)
6769 {
6770         uint8_t checksum;
6771         int i;
6772
6773         METHOD_TRACE("ips_verify_bios_memio", 1);
6774
6775         /* test 1st byte */
6776         writel(0, ha->mem_ptr + IPS_REG_FLAP);
6777         if (ha->revision_id == IPS_REVID_TROMBONE64)
6778                 udelay(25);     /* 25 us */
6779
6780         if (readb(ha->mem_ptr + IPS_REG_FLDP) != 0x55)
6781                 return (1);
6782
6783         writel(1, ha->mem_ptr + IPS_REG_FLAP);
6784         if (ha->revision_id == IPS_REVID_TROMBONE64)
6785                 udelay(25);     /* 25 us */
6786         if (readb(ha->mem_ptr + IPS_REG_FLDP) != 0xAA)
6787                 return (1);
6788
6789         checksum = 0xff;
6790         for (i = 2; i < buffersize; i++) {
6791
6792                 writel(i + offset, ha->mem_ptr + IPS_REG_FLAP);
6793                 if (ha->revision_id == IPS_REVID_TROMBONE64)
6794                         udelay(25);     /* 25 us */
6795
6796                 checksum =
6797                     (uint8_t) checksum + readb(ha->mem_ptr + IPS_REG_FLDP);
6798         }
6799
6800         if (checksum != 0)
6801                 /* failure */
6802                 return (1);
6803         else
6804                 /* success */
6805                 return (0);
6806 }
6807
6808 /*---------------------------------------------------------------------------*/
6809 /*   Routine Name: ips_version_check                                         */
6810 /*                                                                           */
6811 /*   Dependencies:                                                           */
6812 /*     Assumes that ips_read_adapter_status() is called first filling in     */
6813 /*     the data for SubSystem Parameters.                                    */
6814 /*     Called from ips_write_driver_status() so it also assumes NVRAM Page 5 */
6815 /*     Data is available.                                                    */
6816 /*                                                                           */
6817 /*---------------------------------------------------------------------------*/
6818 static void
6819 ips_version_check(ips_ha_t * ha, int intr)
6820 {
6821         IPS_VERSION_DATA *VersionInfo;
6822         uint8_t FirmwareVersion[IPS_COMPAT_ID_LENGTH + 1];
6823         uint8_t BiosVersion[IPS_COMPAT_ID_LENGTH + 1];
6824         int MatchError;
6825         int rc;
6826         char BiosString[10];
6827         char FirmwareString[10];
6828
6829         METHOD_TRACE("ips_version_check", 1);
6830
6831         VersionInfo = ( IPS_VERSION_DATA * ) ha->ioctl_data;
6832
6833         memset(FirmwareVersion, 0, IPS_COMPAT_ID_LENGTH + 1);
6834         memset(BiosVersion, 0, IPS_COMPAT_ID_LENGTH + 1);
6835
6836         /* Get the Compatible BIOS Version from NVRAM Page 5 */
6837         memcpy(BiosVersion, ha->nvram->BiosCompatibilityID,
6838                IPS_COMPAT_ID_LENGTH);
6839
6840         rc = IPS_FAILURE;
6841         if (ha->subsys->param[4] & IPS_GET_VERSION_SUPPORT) {   /* If Versioning is Supported */
6842                 /* Get the Version Info with a Get Version Command */
6843                 memset( VersionInfo, 0, sizeof (IPS_VERSION_DATA));
6844                 rc = ips_get_version_info(ha, ha->ioctl_busaddr, intr);
6845                 if (rc == IPS_SUCCESS)
6846                         memcpy(FirmwareVersion, VersionInfo->compatibilityId,
6847                                IPS_COMPAT_ID_LENGTH);
6848         }
6849
6850         if (rc != IPS_SUCCESS) {        /* If Data Not Obtainable from a GetVersion Command */
6851                 /* Get the Firmware Version from Enquiry Data */
6852                 memcpy(FirmwareVersion, ha->enq->CodeBlkVersion,
6853                        IPS_COMPAT_ID_LENGTH);
6854         }
6855
6856         /* printk(KERN_WARNING "Adapter's BIOS Version  = %s\n", BiosVersion);          */
6857         /* printk(KERN_WARNING "BIOS Compatible Version = %s\n", IPS_COMPAT_BIOS);      */
6858         /* printk(KERN_WARNING "Adapter's Firmware Version  = %s\n", FirmwareVersion);  */
6859         /* printk(KERN_WARNING "Firmware Compatible Version = %s \n", Compatable[ ha->nvram->adapter_type ]); */
6860
6861         MatchError = 0;
6862
6863         if (strncmp
6864             (FirmwareVersion, Compatable[ha->nvram->adapter_type],
6865              IPS_COMPAT_ID_LENGTH) != 0)
6866                 MatchError = 1;
6867
6868         if (strncmp(BiosVersion, IPS_COMPAT_BIOS, IPS_COMPAT_ID_LENGTH) != 0)
6869                 MatchError = 1;
6870
6871         ha->nvram->versioning = 1;      /* Indicate the Driver Supports Versioning */
6872
6873         if (MatchError) {
6874                 ha->nvram->version_mismatch = 1;
6875                 if (ips_cd_boot == 0) {
6876                         strncpy(&BiosString[0], ha->nvram->bios_high, 4);
6877                         strncpy(&BiosString[4], ha->nvram->bios_low, 4);
6878                         BiosString[8] = 0;
6879
6880                         strncpy(&FirmwareString[0], ha->enq->CodeBlkVersion, 8);
6881                         FirmwareString[8] = 0;
6882
6883                         IPS_PRINTK(KERN_WARNING, ha->pcidev,
6884                                    "Warning ! ! ! ServeRAID Version Mismatch\n");
6885                         IPS_PRINTK(KERN_WARNING, ha->pcidev,
6886                                    "Bios = %s, Firmware = %s, Device Driver = %s%s\n",
6887                                    BiosString, FirmwareString, IPS_VERSION_HIGH,
6888                                    IPS_VERSION_LOW);
6889                         IPS_PRINTK(KERN_WARNING, ha->pcidev,
6890                                    "These levels should match to avoid possible compatibility problems.\n");
6891                 }
6892         } else {
6893                 ha->nvram->version_mismatch = 0;
6894         }
6895
6896         return;
6897 }
6898
6899 /*---------------------------------------------------------------------------*/
6900 /*   Routine Name: ips_get_version_info                                      */
6901 /*                                                                           */
6902 /*   Routine Description:                                                    */
6903 /*     Issue an internal GETVERSION Command                                  */
6904 /*                                                                           */
6905 /*   Return Value:                                                           */
6906 /*     0 if Successful, else non-zero                                        */
6907 /*---------------------------------------------------------------------------*/
6908 static int
6909 ips_get_version_info(ips_ha_t * ha, dma_addr_t Buffer, int intr)
6910 {
6911         ips_scb_t *scb;
6912         int rc;
6913
6914         METHOD_TRACE("ips_get_version_info", 1);
6915
6916         scb = &ha->scbs[ha->max_cmds - 1];
6917
6918         ips_init_scb(ha, scb);
6919
6920         scb->timeout = ips_cmd_timeout;
6921         scb->cdb[0] = IPS_CMD_GET_VERSION_INFO;
6922         scb->cmd.version_info.op_code = IPS_CMD_GET_VERSION_INFO;
6923         scb->cmd.version_info.command_id = IPS_COMMAND_ID(ha, scb);
6924         scb->cmd.version_info.reserved = 0;
6925         scb->cmd.version_info.count = sizeof (IPS_VERSION_DATA);
6926         scb->cmd.version_info.reserved2 = 0;
6927         scb->data_len = sizeof (IPS_VERSION_DATA);
6928         scb->data_busaddr = Buffer;
6929         scb->cmd.version_info.buffer_addr = Buffer;
6930         scb->flags = 0;
6931
6932         /* issue command */
6933         rc = ips_send_wait(ha, scb, ips_cmd_timeout, intr);
6934         return (rc);
6935 }
6936
6937 /****************************************************************************/
6938 /*                                                                          */
6939 /* Routine Name: ips_abort_init                                             */
6940 /*                                                                          */
6941 /* Routine Description:                                                     */
6942 /*   cleanup routine for a failed adapter initialization                    */
6943 /****************************************************************************/
6944 static int
6945 ips_abort_init(ips_ha_t * ha, int index)
6946 {
6947         ha->active = 0;
6948         ips_free(ha);
6949         ips_ha[index] = 0;
6950         ips_sh[index] = 0;
6951         return -1;
6952 }
6953
6954 /****************************************************************************/
6955 /*                                                                          */
6956 /* Routine Name: ips_shift_controllers                                      */
6957 /*                                                                          */
6958 /* Routine Description:                                                     */
6959 /*   helper function for ordering adapters                                  */
6960 /****************************************************************************/
6961 static void
6962 ips_shift_controllers(int lowindex, int highindex)
6963 {
6964         ips_ha_t *ha_sav = ips_ha[highindex];
6965         struct Scsi_Host *sh_sav = ips_sh[highindex];
6966         int i;
6967
6968         for (i = highindex; i > lowindex; i--) {
6969                 ips_ha[i] = ips_ha[i - 1];
6970                 ips_sh[i] = ips_sh[i - 1];
6971                 ips_ha[i]->host_num = i;
6972         }
6973         ha_sav->host_num = lowindex;
6974         ips_ha[lowindex] = ha_sav;
6975         ips_sh[lowindex] = sh_sav;
6976 }
6977
6978 /****************************************************************************/
6979 /*                                                                          */
6980 /* Routine Name: ips_order_controllers                                      */
6981 /*                                                                          */
6982 /* Routine Description:                                                     */
6983 /*   place controllers is the "proper" boot order                           */
6984 /****************************************************************************/
6985 static void
6986 ips_order_controllers(void)
6987 {
6988         int i, j, tmp, position = 0;
6989         IPS_NVRAM_P5 *nvram;
6990         if (!ips_ha[0])
6991                 return;
6992         nvram = ips_ha[0]->nvram;
6993
6994         if (nvram->adapter_order[0]) {
6995                 for (i = 1; i <= nvram->adapter_order[0]; i++) {
6996                         for (j = position; j < ips_num_controllers; j++) {
6997                                 switch (ips_ha[j]->ad_type) {
6998                                 case IPS_ADTYPE_SERVERAID6M:
6999                                 case IPS_ADTYPE_SERVERAID7k:
7000                                 case IPS_ADTYPE_SERVERAID7M:
7001                                         if (nvram->adapter_order[i] == 'M') {
7002                                                 ips_shift_controllers(position,
7003                                                                       j);
7004                                                 position++;
7005                                         }
7006                                         break;
7007                                 case IPS_ADTYPE_SERVERAID4L:
7008                                 case IPS_ADTYPE_SERVERAID4M:
7009                                 case IPS_ADTYPE_SERVERAID4MX:
7010                                 case IPS_ADTYPE_SERVERAID4LX:
7011                                         if (nvram->adapter_order[i] == 'N') {
7012                                                 ips_shift_controllers(position,
7013                                                                       j);
7014                                                 position++;
7015                                         }
7016                                         break;
7017                                 case IPS_ADTYPE_SERVERAID6I:
7018                                 case IPS_ADTYPE_SERVERAID5I2:
7019                                 case IPS_ADTYPE_SERVERAID5I1:
7020                                         if (nvram->adapter_order[i] == 'S') {
7021                                                 ips_shift_controllers(position,
7022                                                                       j);
7023                                                 position++;
7024                                         }
7025                                         break;
7026                                 case IPS_ADTYPE_SERVERAID:
7027                                 case IPS_ADTYPE_SERVERAID2:
7028                                 case IPS_ADTYPE_NAVAJO:
7029                                 case IPS_ADTYPE_KIOWA:
7030                                 case IPS_ADTYPE_SERVERAID3L:
7031                                 case IPS_ADTYPE_SERVERAID3:
7032                                 case IPS_ADTYPE_SERVERAID4H:
7033                                         if (nvram->adapter_order[i] == 'A') {
7034                                                 ips_shift_controllers(position,
7035                                                                       j);
7036                                                 position++;
7037                                         }
7038                                         break;
7039                                 default:
7040                                         break;
7041                                 }
7042                         }
7043                 }
7044                 /* if adapter_order[0], then ordering is complete */
7045                 return;
7046         }
7047         /* old bios, use older ordering */
7048         tmp = 0;
7049         for (i = position; i < ips_num_controllers; i++) {
7050                 if (ips_ha[i]->ad_type == IPS_ADTYPE_SERVERAID5I2 ||
7051                     ips_ha[i]->ad_type == IPS_ADTYPE_SERVERAID5I1) {
7052                         ips_shift_controllers(position, i);
7053                         position++;
7054                         tmp = 1;
7055                 }
7056         }
7057         /* if there were no 5I cards, then don't do any extra ordering */
7058         if (!tmp)
7059                 return;
7060         for (i = position; i < ips_num_controllers; i++) {
7061                 if (ips_ha[i]->ad_type == IPS_ADTYPE_SERVERAID4L ||
7062                     ips_ha[i]->ad_type == IPS_ADTYPE_SERVERAID4M ||
7063                     ips_ha[i]->ad_type == IPS_ADTYPE_SERVERAID4LX ||
7064                     ips_ha[i]->ad_type == IPS_ADTYPE_SERVERAID4MX) {
7065                         ips_shift_controllers(position, i);
7066                         position++;
7067                 }
7068         }
7069
7070         return;
7071 }
7072
7073 /****************************************************************************/
7074 /*                                                                          */
7075 /* Routine Name: ips_register_scsi                                          */
7076 /*                                                                          */
7077 /* Routine Description:                                                     */
7078 /*   perform any registration and setup with the scsi layer                 */
7079 /****************************************************************************/
7080 static int
7081 ips_register_scsi(int index)
7082 {
7083         struct Scsi_Host *sh;
7084         ips_ha_t *ha, *oldha = ips_ha[index];
7085         sh = scsi_host_alloc(&ips_driver_template, sizeof (ips_ha_t));
7086         if (!sh) {
7087                 IPS_PRINTK(KERN_WARNING, oldha->pcidev,
7088                            "Unable to register controller with SCSI subsystem\n");
7089                 return -1;
7090         }
7091         ha = IPS_HA(sh);
7092         memcpy(ha, oldha, sizeof (ips_ha_t));
7093         free_irq(oldha->irq, oldha);
7094         /* Install the interrupt handler with the new ha */
7095         if (request_irq(ha->irq, do_ipsintr, SA_SHIRQ, ips_name, ha)) {
7096                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
7097                            "Unable to install interrupt handler\n");
7098                 scsi_host_put(sh);
7099                 return -1;
7100         }
7101
7102         kfree(oldha);
7103         ips_sh[index] = sh;
7104         ips_ha[index] = ha;
7105         IPS_SCSI_SET_DEVICE(sh, ha);
7106
7107         /* Store away needed values for later use */
7108         sh->io_port = ha->io_addr;
7109         sh->n_io_port = ha->io_addr ? 255 : 0;
7110         sh->unique_id = (ha->io_addr) ? ha->io_addr : ha->mem_addr;
7111         sh->irq = ha->irq;
7112         sh->sg_tablesize = sh->hostt->sg_tablesize;
7113         sh->can_queue = sh->hostt->can_queue;
7114         sh->cmd_per_lun = sh->hostt->cmd_per_lun;
7115         sh->unchecked_isa_dma = sh->hostt->unchecked_isa_dma;
7116         sh->use_clustering = sh->hostt->use_clustering;
7117
7118 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,7)
7119         sh->max_sectors = 128;
7120 #endif
7121
7122         sh->max_id = ha->ntargets;
7123         sh->max_lun = ha->nlun;
7124         sh->max_channel = ha->nbus - 1;
7125         sh->can_queue = ha->max_cmds - 1;
7126
7127         IPS_ADD_HOST(sh, NULL);
7128         return 0;
7129 }
7130
7131 /*---------------------------------------------------------------------------*/
7132 /*   Routine Name: ips_remove_device                                         */
7133 /*                                                                           */
7134 /*   Routine Description:                                                    */
7135 /*     Remove one Adapter ( Hot Plugging )                                   */
7136 /*---------------------------------------------------------------------------*/
7137 static void __devexit
7138 ips_remove_device(struct pci_dev *pci_dev)
7139 {
7140         int i;
7141         struct Scsi_Host *sh;
7142         ips_ha_t *ha;
7143
7144         for (i = 0; i < IPS_MAX_ADAPTERS; i++) {
7145                 ha = ips_ha[i];
7146                 if (ha) {
7147                         if ((pci_dev->bus->number == ha->pcidev->bus->number) &&
7148                             (pci_dev->devfn == ha->pcidev->devfn)) {
7149                                 sh = ips_sh[i];
7150                                 ips_release(sh);
7151                         }
7152                 }
7153         }
7154 }
7155
7156 /****************************************************************************/
7157 /*                                                                          */
7158 /* Routine Name: ips_module_init                                            */
7159 /*                                                                          */
7160 /* Routine Description:                                                     */
7161 /*   function called on module load                                         */
7162 /****************************************************************************/
7163 static int __init
7164 ips_module_init(void)
7165 {
7166         if (pci_module_init(&ips_pci_driver) < 0)
7167                 return -ENODEV;
7168         ips_driver_template.module = THIS_MODULE;
7169         ips_order_controllers();
7170         if (IPS_REGISTER_HOSTS(&ips_driver_template)) {
7171                 pci_unregister_driver(&ips_pci_driver);
7172                 return -ENODEV;
7173         }
7174         register_reboot_notifier(&ips_notifier);
7175         return 0;
7176 }
7177
7178 /****************************************************************************/
7179 /*                                                                          */
7180 /* Routine Name: ips_module_exit                                            */
7181 /*                                                                          */
7182 /* Routine Description:                                                     */
7183 /*   function called on module unload                                       */
7184 /****************************************************************************/
7185 static void __exit
7186 ips_module_exit(void)
7187 {
7188         IPS_UNREGISTER_HOSTS(&ips_driver_template);
7189         pci_unregister_driver(&ips_pci_driver);
7190         unregister_reboot_notifier(&ips_notifier);
7191 }
7192
7193 module_init(ips_module_init);
7194 module_exit(ips_module_exit);
7195
7196 /*---------------------------------------------------------------------------*/
7197 /*   Routine Name: ips_insert_device                                         */
7198 /*                                                                           */
7199 /*   Routine Description:                                                    */
7200 /*     Add One Adapter ( Hot Plug )                                          */
7201 /*                                                                           */
7202 /*   Return Value:                                                           */
7203 /*     0 if Successful, else non-zero                                        */
7204 /*---------------------------------------------------------------------------*/
7205 static int __devinit
7206 ips_insert_device(struct pci_dev *pci_dev, const struct pci_device_id *ent)
7207 {
7208         int index;
7209         int rc;
7210
7211         METHOD_TRACE("ips_insert_device", 1);
7212         if (pci_enable_device(pci_dev))
7213                 return -1;
7214
7215         rc = ips_init_phase1(pci_dev, &index);
7216         if (rc == SUCCESS)
7217                 rc = ips_init_phase2(index);
7218
7219         if (ips_hotplug)
7220                 if (ips_register_scsi(index)) {
7221                         ips_free(ips_ha[index]);
7222                         rc = -1;
7223                 }
7224
7225         if (rc == SUCCESS)
7226                 ips_num_controllers++;
7227
7228         ips_next_controller = ips_num_controllers;
7229         return rc;
7230 }
7231
7232 /*---------------------------------------------------------------------------*/
7233 /*   Routine Name: ips_init_phase1                                           */
7234 /*                                                                           */
7235 /*   Routine Description:                                                    */
7236 /*     Adapter Initialization                                                */
7237 /*                                                                           */
7238 /*   Return Value:                                                           */
7239 /*     0 if Successful, else non-zero                                        */
7240 /*---------------------------------------------------------------------------*/
7241 static int
7242 ips_init_phase1(struct pci_dev *pci_dev, int *indexPtr)
7243 {
7244         ips_ha_t *ha;
7245         uint32_t io_addr;
7246         uint32_t mem_addr;
7247         uint32_t io_len;
7248         uint32_t mem_len;
7249         uint8_t revision_id;
7250         uint8_t bus;
7251         uint8_t func;
7252         uint8_t irq;
7253         uint16_t subdevice_id;
7254         int j;
7255         int index;
7256         dma_addr_t dma_address;
7257         char *ioremap_ptr;
7258         char *mem_ptr;
7259         uint32_t IsDead;
7260
7261         METHOD_TRACE("ips_init_phase1", 1);
7262         index = IPS_MAX_ADAPTERS;
7263         for (j = 0; j < IPS_MAX_ADAPTERS; j++) {
7264                 if (ips_ha[j] == 0) {
7265                         index = j;
7266                         break;
7267                 }
7268         }
7269
7270         if (index >= IPS_MAX_ADAPTERS)
7271                 return -1;
7272
7273         /* stuff that we get in dev */
7274         irq = pci_dev->irq;
7275         bus = pci_dev->bus->number;
7276         func = pci_dev->devfn;
7277
7278         /* Init MEM/IO addresses to 0 */
7279         mem_addr = 0;
7280         io_addr = 0;
7281         mem_len = 0;
7282         io_len = 0;
7283
7284         for (j = 0; j < 2; j++) {
7285                 if (!pci_resource_start(pci_dev, j))
7286                         break;
7287
7288                 if (pci_resource_flags(pci_dev, j) & IORESOURCE_IO) {
7289                         io_addr = pci_resource_start(pci_dev, j);
7290                         io_len = pci_resource_len(pci_dev, j);
7291                 } else {
7292                         mem_addr = pci_resource_start(pci_dev, j);
7293                         mem_len = pci_resource_len(pci_dev, j);
7294                 }
7295         }
7296
7297         /* setup memory mapped area (if applicable) */
7298         if (mem_addr) {
7299                 uint32_t base;
7300                 uint32_t offs;
7301
7302                 if (!request_mem_region(mem_addr, mem_len, "ips")) {
7303                         IPS_PRINTK(KERN_WARNING, pci_dev,
7304                                    "Couldn't allocate IO Memory space %x len %d.\n",
7305                                    mem_addr, mem_len);
7306                         return -1;
7307                 }
7308
7309                 base = mem_addr & PAGE_MASK;
7310                 offs = mem_addr - base;
7311                 ioremap_ptr = ioremap(base, PAGE_SIZE);
7312                 mem_ptr = ioremap_ptr + offs;
7313         } else {
7314                 ioremap_ptr = NULL;
7315                 mem_ptr = NULL;
7316         }
7317
7318         /* setup I/O mapped area (if applicable) */
7319         if (io_addr) {
7320                 if (!request_region(io_addr, io_len, "ips")) {
7321                         IPS_PRINTK(KERN_WARNING, pci_dev,
7322                                    "Couldn't allocate IO space %x len %d.\n",
7323                                    io_addr, io_len);
7324                         return -1;
7325                 }
7326         }
7327
7328         /* get the revision ID */
7329         if (pci_read_config_byte(pci_dev, PCI_REVISION_ID, &revision_id)) {
7330                 IPS_PRINTK(KERN_WARNING, pci_dev, "Can't get revision id.\n");
7331                 return -1;
7332         }
7333
7334         subdevice_id = pci_dev->subsystem_device;
7335
7336         /* found a controller */
7337         ha = kmalloc(sizeof (ips_ha_t), GFP_KERNEL);
7338         if (ha == NULL) {
7339                 IPS_PRINTK(KERN_WARNING, pci_dev,
7340                            "Unable to allocate temporary ha struct\n");
7341                 return -1;
7342         }
7343
7344         memset(ha, 0, sizeof (ips_ha_t));
7345
7346         ips_sh[index] = NULL;
7347         ips_ha[index] = ha;
7348         ha->active = 1;
7349
7350         /* Store info in HA structure */
7351         ha->irq = irq;
7352         ha->io_addr = io_addr;
7353         ha->io_len = io_len;
7354         ha->mem_addr = mem_addr;
7355         ha->mem_len = mem_len;
7356         ha->mem_ptr = mem_ptr;
7357         ha->ioremap_ptr = ioremap_ptr;
7358         ha->host_num = (uint32_t) index;
7359         ha->revision_id = revision_id;
7360         ha->slot_num = PCI_SLOT(pci_dev->devfn);
7361         ha->device_id = pci_dev->device;
7362         ha->subdevice_id = subdevice_id;
7363         ha->pcidev = pci_dev;
7364
7365         /*
7366          * Set the pci_dev's dma_mask.  Not all adapters support 64bit
7367          * addressing so don't enable it if the adapter can't support
7368          * it!  Also, don't use 64bit addressing if dma addresses
7369          * are guaranteed to be < 4G.
7370          */
7371         if (IPS_ENABLE_DMA64 && IPS_HAS_ENH_SGLIST(ha) &&
7372             !pci_set_dma_mask(ha->pcidev, 0xffffffffffffffffULL)) {
7373                 (ha)->flags |= IPS_HA_ENH_SG;
7374         } else {
7375                 if (pci_set_dma_mask(ha->pcidev, 0xffffffffULL) != 0) {
7376                         printk(KERN_WARNING "Unable to set DMA Mask\n");
7377                         return ips_abort_init(ha, index);
7378                 }
7379         }
7380         if(ips_cd_boot && !ips_FlashData){
7381                 ips_FlashData = pci_alloc_consistent(pci_dev, PAGE_SIZE << 7,
7382                                                      &ips_flashbusaddr);
7383         }
7384
7385         ha->enq = pci_alloc_consistent(pci_dev, sizeof (IPS_ENQ),
7386                                        &ha->enq_busaddr);
7387         if (!ha->enq) {
7388                 IPS_PRINTK(KERN_WARNING, pci_dev,
7389                            "Unable to allocate host inquiry structure\n");
7390                 return ips_abort_init(ha, index);
7391         }
7392
7393         ha->adapt = pci_alloc_consistent(pci_dev, sizeof (IPS_ADAPTER) +
7394                                          sizeof (IPS_IO_CMD), &dma_address);
7395         if (!ha->adapt) {
7396                 IPS_PRINTK(KERN_WARNING, pci_dev,
7397                            "Unable to allocate host adapt & dummy structures\n");
7398                 return ips_abort_init(ha, index);
7399         }
7400         ha->adapt->hw_status_start = dma_address;
7401         ha->dummy = (void *) (ha->adapt + 1);
7402
7403
7404
7405         ha->logical_drive_info = pci_alloc_consistent(pci_dev, sizeof (IPS_LD_INFO), &dma_address);
7406         if (!ha->logical_drive_info) {
7407                 IPS_PRINTK(KERN_WARNING, pci_dev,
7408                            "Unable to allocate logical drive info structure\n");
7409                 return ips_abort_init(ha, index);
7410         }
7411         ha->logical_drive_info_dma_addr = dma_address;
7412
7413
7414         ha->conf = kmalloc(sizeof (IPS_CONF), GFP_KERNEL);
7415
7416         if (!ha->conf) {
7417                 IPS_PRINTK(KERN_WARNING, pci_dev,
7418                            "Unable to allocate host conf structure\n");
7419                 return ips_abort_init(ha, index);
7420         }
7421
7422         ha->nvram = kmalloc(sizeof (IPS_NVRAM_P5), GFP_KERNEL);
7423
7424         if (!ha->nvram) {
7425                 IPS_PRINTK(KERN_WARNING, pci_dev,
7426                            "Unable to allocate host NVRAM structure\n");
7427                 return ips_abort_init(ha, index);
7428         }
7429
7430         ha->subsys = kmalloc(sizeof (IPS_SUBSYS), GFP_KERNEL);
7431
7432         if (!ha->subsys) {
7433                 IPS_PRINTK(KERN_WARNING, pci_dev,
7434                            "Unable to allocate host subsystem structure\n");
7435                 return ips_abort_init(ha, index);
7436         }
7437
7438         /* the ioctl buffer is now used during adapter initialization, so its
7439          * successful allocation is now required */
7440         if (ips_ioctlsize < PAGE_SIZE)
7441                 ips_ioctlsize = PAGE_SIZE;
7442
7443         ha->ioctl_data = pci_alloc_consistent(pci_dev, ips_ioctlsize,
7444                                               &ha->ioctl_busaddr);
7445         ha->ioctl_len = ips_ioctlsize;
7446         if (!ha->ioctl_data) {
7447                 IPS_PRINTK(KERN_WARNING, pci_dev,
7448                            "Unable to allocate IOCTL data\n");
7449                 return ips_abort_init(ha, index);
7450         }
7451
7452         /*
7453          * Setup Functions
7454          */
7455         ips_setup_funclist(ha);
7456
7457         if ((IPS_IS_MORPHEUS(ha)) || (IPS_IS_MARCO(ha))) {
7458                 /* If Morpheus appears dead, reset it */
7459                 IsDead = readl(ha->mem_ptr + IPS_REG_I960_MSG1);
7460                 if (IsDead == 0xDEADBEEF) {
7461                         ips_reset_morpheus(ha);
7462                 }
7463         }
7464
7465         /*
7466          * Initialize the card if it isn't already
7467          */
7468
7469         if (!(*ha->func.isinit) (ha)) {
7470                 if (!(*ha->func.init) (ha)) {
7471                         /*
7472                          * Initialization failed
7473                          */
7474                         IPS_PRINTK(KERN_WARNING, pci_dev,
7475                                    "Unable to initialize controller\n");
7476                         return ips_abort_init(ha, index);
7477                 }
7478         }
7479
7480         *indexPtr = index;
7481         return SUCCESS;
7482 }
7483
7484 /*---------------------------------------------------------------------------*/
7485 /*   Routine Name: ips_init_phase2                                           */
7486 /*                                                                           */
7487 /*   Routine Description:                                                    */
7488 /*     Adapter Initialization Phase 2                                        */
7489 /*                                                                           */
7490 /*   Return Value:                                                           */
7491 /*     0 if Successful, else non-zero                                        */
7492 /*---------------------------------------------------------------------------*/
7493 static int
7494 ips_init_phase2(int index)
7495 {
7496         ips_ha_t *ha;
7497
7498         ha = ips_ha[index];
7499
7500         METHOD_TRACE("ips_init_phase2", 1);
7501         if (!ha->active) {
7502                 ips_ha[index] = NULL;
7503                 return -1;
7504         }
7505
7506         /* Install the interrupt handler */
7507         if (request_irq(ha->irq, do_ipsintr, SA_SHIRQ, ips_name, ha)) {
7508                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
7509                            "Unable to install interrupt handler\n");
7510                 return ips_abort_init(ha, index);
7511         }
7512
7513         /*
7514          * Allocate a temporary SCB for initialization
7515          */
7516         ha->max_cmds = 1;
7517         if (!ips_allocatescbs(ha)) {
7518                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
7519                            "Unable to allocate a CCB\n");
7520                 free_irq(ha->irq, ha);
7521                 return ips_abort_init(ha, index);
7522         }
7523
7524         if (!ips_hainit(ha)) {
7525                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
7526                            "Unable to initialize controller\n");
7527                 free_irq(ha->irq, ha);
7528                 return ips_abort_init(ha, index);
7529         }
7530         /* Free the temporary SCB */
7531         ips_deallocatescbs(ha, 1);
7532
7533         /* allocate CCBs */
7534         if (!ips_allocatescbs(ha)) {
7535                 IPS_PRINTK(KERN_WARNING, ha->pcidev,
7536                            "Unable to allocate CCBs\n");
7537                 free_irq(ha->irq, ha);
7538                 return ips_abort_init(ha, index);
7539         }
7540
7541         return SUCCESS;
7542 }
7543
7544 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,9)
7545 MODULE_LICENSE("GPL");
7546 #endif
7547
7548 /*
7549  * Overrides for Emacs so that we almost follow Linus's tabbing style.
7550  * Emacs will notice this stuff at the end of the file and automatically
7551  * adjust the settings for this buffer only.  This must remain at the end
7552  * of the file.
7553  * ---------------------------------------------------------------------------
7554  * Local variables:
7555  * c-indent-level: 2
7556  * c-brace-imaginary-offset: 0
7557  * c-brace-offset: -2
7558  * c-argdecl-indent: 2
7559  * c-label-offset: -2
7560  * c-continued-statement-offset: 2
7561  * c-continued-brace-offset: 0
7562  * indent-tabs-mode: nil
7563  * tab-width: 8
7564  * End:
7565  */