[SCSI] qla1280: remove dead per-host flag variables
[pandora-kernel.git] / drivers / scsi / qla1280.c
1 /******************************************************************************
2 *                  QLOGIC LINUX SOFTWARE
3 *
4 * QLogic  QLA1280 (Ultra2)  and  QLA12160 (Ultra3) SCSI driver
5 * Copyright (C) 2000 Qlogic Corporation (www.qlogic.com)
6 * Copyright (C) 2001-2004 Jes Sorensen, Wild Open Source Inc.
7 * Copyright (C) 2003-2004 Christoph Hellwig
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 ******************************************************************************/
20 #define QLA1280_VERSION      "3.25"
21 /*****************************************************************************
22     Revision History:
23     Rev  3.25.1, February 10, 2005 Christoph Hellwig
24         - use pci_map_single to map non-S/G requests
25         - remove qla1280_proc_info
26     Rev  3.25, September 28, 2004, Christoph Hellwig
27         - add support for ISP1020/1040
28         - don't include "scsi.h" anymore for 2.6.x
29     Rev  3.24.4 June 7, 2004 Christoph Hellwig
30         - restructure firmware loading, cleanup initialization code
31         - prepare support for ISP1020/1040 chips
32     Rev  3.24.3 January 19, 2004, Jes Sorensen
33         - Handle PCI DMA mask settings correctly
34         - Correct order of error handling in probe_one, free_irq should not
35           be called if request_irq failed
36     Rev  3.24.2 January 19, 2004, James Bottomley & Andrew Vasquez
37         - Big endian fixes (James)
38         - Remove bogus IOCB content on zero data transfer commands (Andrew)
39     Rev  3.24.1 January 5, 2004, Jes Sorensen
40         - Initialize completion queue to avoid OOPS on probe
41         - Handle interrupts during mailbox testing
42     Rev  3.24 November 17, 2003, Christoph Hellwig
43         - use struct list_head for completion queue
44         - avoid old Scsi_FOO typedefs
45         - cleanup 2.4 compat glue a bit
46         - use <scsi/scsi_*.h> headers on 2.6 instead of "scsi.h"
47         - make initialization for memory mapped vs port I/O more similar
48         - remove broken pci config space manipulation
49         - kill more cruft
50         - this is an almost perfect 2.6 scsi driver now! ;)
51     Rev  3.23.39 December 17, 2003, Jes Sorensen
52         - Delete completion queue from srb if mailbox command failed to
53           to avoid qla1280_done completeting qla1280_error_action's
54           obsolete context
55         - Reduce arguments for qla1280_done
56     Rev  3.23.38 October 18, 2003, Christoph Hellwig
57         - Convert to new-style hotplugable driver for 2.6
58         - Fix missing scsi_unregister/scsi_host_put on HBA removal
59         - Kill some more cruft
60     Rev  3.23.37 October 1, 2003, Jes Sorensen
61         - Make MMIO depend on CONFIG_X86_VISWS instead of yet another
62           random CONFIG option
63         - Clean up locking in probe path
64     Rev  3.23.36 October 1, 2003, Christoph Hellwig
65         - queuecommand only ever receives new commands - clear flags
66         - Reintegrate lost fixes from Linux 2.5
67     Rev  3.23.35 August 14, 2003, Jes Sorensen
68         - Build against 2.6
69     Rev  3.23.34 July 23, 2003, Jes Sorensen
70         - Remove pointless TRUE/FALSE macros
71         - Clean up vchan handling
72     Rev  3.23.33 July 3, 2003, Jes Sorensen
73         - Don't define register access macros before define determining MMIO.
74           This just happend to work out on ia64 but not elsewhere.
75         - Don't try and read from the card while it is in reset as
76           it won't respond and causes an MCA
77     Rev  3.23.32 June 23, 2003, Jes Sorensen
78         - Basic support for boot time arguments
79     Rev  3.23.31 June 8, 2003, Jes Sorensen
80         - Reduce boot time messages
81     Rev  3.23.30 June 6, 2003, Jes Sorensen
82         - Do not enable sync/wide/ppr before it has been determined
83           that the target device actually supports it
84         - Enable DMA arbitration for multi channel controllers
85     Rev  3.23.29 June 3, 2003, Jes Sorensen
86         - Port to 2.5.69
87     Rev  3.23.28 June 3, 2003, Jes Sorensen
88         - Eliminate duplicate marker commands on bus resets
89         - Handle outstanding commands appropriately on bus/device resets
90     Rev  3.23.27 May 28, 2003, Jes Sorensen
91         - Remove bogus input queue code, let the Linux SCSI layer do the work
92         - Clean up NVRAM handling, only read it once from the card
93         - Add a number of missing default nvram parameters
94     Rev  3.23.26 Beta May 28, 2003, Jes Sorensen
95         - Use completion queue for mailbox commands instead of busy wait
96     Rev  3.23.25 Beta May 27, 2003, James Bottomley
97         - Migrate to use new error handling code
98     Rev  3.23.24 Beta May 21, 2003, James Bottomley
99         - Big endian support
100         - Cleanup data direction code
101     Rev  3.23.23 Beta May 12, 2003, Jes Sorensen
102         - Switch to using MMIO instead of PIO
103     Rev  3.23.22 Beta April 15, 2003, Jes Sorensen
104         - Fix PCI parity problem with 12160 during reset.
105     Rev  3.23.21 Beta April 14, 2003, Jes Sorensen
106         - Use pci_map_page()/pci_unmap_page() instead of map_single version.
107     Rev  3.23.20 Beta April 9, 2003, Jes Sorensen
108         - Remove < 2.4.x support
109         - Introduce HOST_LOCK to make the spin lock changes portable.
110         - Remove a bunch of idiotic and unnecessary typedef's
111         - Kill all leftovers of target-mode support which never worked anyway
112     Rev  3.23.19 Beta April 11, 2002, Linus Torvalds
113         - Do qla1280_pci_config() before calling request_irq() and
114           request_region()
115         - Use pci_dma_hi32() to handle upper word of DMA addresses instead
116           of large shifts
117         - Hand correct arguments to free_irq() in case of failure
118     Rev  3.23.18 Beta April 11, 2002, Jes Sorensen
119         - Run source through Lindent and clean up the output
120     Rev  3.23.17 Beta April 11, 2002, Jes Sorensen
121         - Update SCSI firmware to qla1280 v8.15.00 and qla12160 v10.04.32
122     Rev  3.23.16 Beta March 19, 2002, Jes Sorensen
123         - Rely on mailbox commands generating interrupts - do not
124           run qla1280_isr() from ql1280_mailbox_command()
125         - Remove device_reg_t
126         - Integrate ql12160_set_target_parameters() with 1280 version
127         - Make qla1280_setup() non static
128         - Do not call qla1280_check_for_dead_scsi_bus() on every I/O request
129           sent to the card - this command pauses the firmare!!!
130     Rev  3.23.15 Beta March 19, 2002, Jes Sorensen
131         - Clean up qla1280.h - remove obsolete QL_DEBUG_LEVEL_x definitions
132         - Remove a pile of pointless and confusing (srb_t **) and
133           (scsi_lu_t *) typecasts
134         - Explicit mark that we do not use the new error handling (for now)
135         - Remove scsi_qla_host_t and use 'struct' instead
136         - Remove in_abort, watchdog_enabled, dpc, dpc_sched, bios_enabled,
137           pci_64bit_slot flags which weren't used for anything anyway
138         - Grab host->host_lock while calling qla1280_isr() from abort()
139         - Use spin_lock()/spin_unlock() in qla1280_intr_handler() - we
140           do not need to save/restore flags in the interrupt handler
141         - Enable interrupts early (before any mailbox access) in preparation
142           for cleaning up the mailbox handling
143     Rev  3.23.14 Beta March 14, 2002, Jes Sorensen
144         - Further cleanups. Remove all trace of QL_DEBUG_LEVEL_x and replace
145           it with proper use of dprintk().
146         - Make qla1280_print_scsi_cmd() and qla1280_dump_buffer() both take
147           a debug level argument to determine if data is to be printed
148         - Add KERN_* info to printk()
149     Rev  3.23.13 Beta March 14, 2002, Jes Sorensen
150         - Significant cosmetic cleanups
151         - Change debug code to use dprintk() and remove #if mess
152     Rev  3.23.12 Beta March 13, 2002, Jes Sorensen
153         - More cosmetic cleanups, fix places treating return as function
154         - use cpu_relax() in qla1280_debounce_register()
155     Rev  3.23.11 Beta March 13, 2002, Jes Sorensen
156         - Make it compile under 2.5.5
157     Rev  3.23.10 Beta October 1, 2001, Jes Sorensen
158         - Do no typecast short * to long * in QL1280BoardTbl, this
159           broke miserably on big endian boxes
160     Rev  3.23.9 Beta September 30, 2001, Jes Sorensen
161         - Remove pre 2.2 hack for checking for reentrance in interrupt handler
162         - Make data types used to receive from SCSI_{BUS,TCN,LUN}_32
163           unsigned int to match the types from struct scsi_cmnd
164     Rev  3.23.8 Beta September 29, 2001, Jes Sorensen
165         - Remove bogus timer_t typedef from qla1280.h
166         - Remove obsolete pre 2.2 PCI setup code, use proper #define's
167           for PCI_ values, call pci_set_master()
168         - Fix memleak of qla1280_buffer on module unload
169         - Only compile module parsing code #ifdef MODULE - should be
170           changed to use individual MODULE_PARM's later
171         - Remove dummy_buffer that was never modified nor printed
172         - ENTER()/LEAVE() are noops unless QL_DEBUG_LEVEL_3, hence remove
173           #ifdef QL_DEBUG_LEVEL_3/#endif around ENTER()/LEAVE() calls
174         - Remove \r from print statements, this is Linux, not DOS
175         - Remove obsolete QLA1280_{SCSILU,INTR,RING}_{LOCK,UNLOCK}
176           dummy macros
177         - Remove C++ compile hack in header file as Linux driver are not
178           supposed to be compiled as C++
179         - Kill MS_64BITS macro as it makes the code more readable
180         - Remove unnecessary flags.in_interrupts bit
181     Rev  3.23.7 Beta August 20, 2001, Jes Sorensen
182         - Dont' check for set flags on q->q_flag one by one in qla1280_next()
183         - Check whether the interrupt was generated by the QLA1280 before
184           doing any processing
185         - qla1280_status_entry(): Only zero out part of sense_buffer that
186           is not being copied into
187         - Remove more superflouous typecasts
188         - qla1280_32bit_start_scsi() replace home-brew memcpy() with memcpy()
189     Rev  3.23.6 Beta August 20, 2001, Tony Luck, Intel
190         - Don't walk the entire list in qla1280_putq_t() just to directly
191           grab the pointer to the last element afterwards
192     Rev  3.23.5 Beta August 9, 2001, Jes Sorensen
193         - Don't use SA_INTERRUPT, it's use is deprecated for this kinda driver
194     Rev  3.23.4 Beta August 8, 2001, Jes Sorensen
195         - Set dev->max_sectors to 1024
196     Rev  3.23.3 Beta August 6, 2001, Jes Sorensen
197         - Provide compat macros for pci_enable_device(), pci_find_subsys()
198           and scsi_set_pci_device()
199         - Call scsi_set_pci_device() for all devices
200         - Reduce size of kernel version dependent device probe code
201         - Move duplicate probe/init code to separate function
202         - Handle error if qla1280_mem_alloc() fails
203         - Kill OFFSET() macro and use Linux's PCI definitions instead
204         - Kill private structure defining PCI config space (struct config_reg)
205         - Only allocate I/O port region if not in MMIO mode
206         - Remove duplicate (unused) sanity check of sife of srb_t
207     Rev  3.23.2 Beta August 6, 2001, Jes Sorensen
208         - Change home-brew memset() implementations to use memset()
209         - Remove all references to COMTRACE() - accessing a PC's COM2 serial
210           port directly is not legal under Linux.
211     Rev  3.23.1 Beta April 24, 2001, Jes Sorensen
212         - Remove pre 2.2 kernel support
213         - clean up 64 bit DMA setting to use 2.4 API (provide backwards compat)
214         - Fix MMIO access to use readl/writel instead of directly
215           dereferencing pointers
216         - Nuke MSDOS debugging code
217         - Change true/false data types to int from uint8_t
218         - Use int for counters instead of uint8_t etc.
219         - Clean up size & byte order conversion macro usage
220     Rev  3.23 Beta January 11, 2001 BN Qlogic
221         - Added check of device_id when handling non
222           QLA12160s during detect().
223     Rev  3.22 Beta January 5, 2001 BN Qlogic
224         - Changed queue_task() to schedule_task()
225           for kernels 2.4.0 and higher.
226           Note: 2.4.0-testxx kernels released prior to
227                 the actual 2.4.0 kernel release on January 2001
228                 will get compile/link errors with schedule_task().
229                 Please update your kernel to released 2.4.0 level,
230                 or comment lines in this file flagged with  3.22
231                 to resolve compile/link error of schedule_task().
232         - Added -DCONFIG_SMP in addition to -D__SMP__
233           in Makefile for 2.4.0 builds of driver as module.
234     Rev  3.21 Beta January 4, 2001 BN Qlogic
235         - Changed criteria of 64/32 Bit mode of HBA
236           operation according to BITS_PER_LONG rather
237           than HBA's NVRAM setting of >4Gig memory bit;
238           so that the HBA auto-configures without the need
239           to setup each system individually.
240     Rev  3.20 Beta December 5, 2000 BN Qlogic
241         - Added priority handling to IA-64  onboard SCSI
242           ISP12160 chip for kernels greater than 2.3.18.
243         - Added irqrestore for qla1280_intr_handler.
244         - Enabled /proc/scsi/qla1280 interface.
245         - Clear /proc/scsi/qla1280 counters in detect().
246     Rev  3.19 Beta October 13, 2000 BN Qlogic
247         - Declare driver_template for new kernel
248           (2.4.0 and greater) scsi initialization scheme.
249         - Update /proc/scsi entry for 2.3.18 kernels and
250           above as qla1280
251     Rev  3.18 Beta October 10, 2000 BN Qlogic
252         - Changed scan order of adapters to map
253           the QLA12160 followed by the QLA1280.
254     Rev  3.17 Beta September 18, 2000 BN Qlogic
255         - Removed warnings for 32 bit 2.4.x compiles
256         - Corrected declared size for request and response
257           DMA addresses that are kept in each ha
258     Rev. 3.16 Beta  August 25, 2000   BN  Qlogic
259         - Corrected 64 bit addressing issue on IA-64
260           where the upper 32 bits were not properly
261           passed to the RISC engine.
262     Rev. 3.15 Beta  August 22, 2000   BN  Qlogic
263         - Modified qla1280_setup_chip to properly load
264           ISP firmware for greater that 4 Gig memory on IA-64
265     Rev. 3.14 Beta  August 16, 2000   BN  Qlogic
266         - Added setting of dma_mask to full 64 bit
267           if flags.enable_64bit_addressing is set in NVRAM
268     Rev. 3.13 Beta  August 16, 2000   BN  Qlogic
269         - Use new PCI DMA mapping APIs for 2.4.x kernel
270     Rev. 3.12       July 18, 2000    Redhat & BN Qlogic
271         - Added check of pci_enable_device to detect() for 2.3.x
272         - Use pci_resource_start() instead of
273           pdev->resource[0].start in detect() for 2.3.x
274         - Updated driver version
275     Rev. 3.11       July 14, 2000    BN  Qlogic
276         - Updated SCSI Firmware to following versions:
277           qla1x80:   8.13.08
278           qla1x160:  10.04.08
279         - Updated driver version to 3.11
280     Rev. 3.10    June 23, 2000   BN Qlogic
281         - Added filtering of AMI SubSys Vendor ID devices
282     Rev. 3.9
283         - DEBUG_QLA1280 undefined and  new version  BN Qlogic
284     Rev. 3.08b      May 9, 2000    MD Dell
285         - Added logic to check against AMI subsystem vendor ID
286         Rev. 3.08       May 4, 2000    DG  Qlogic
287         - Added logic to check for PCI subsystem ID.
288         Rev. 3.07       Apr 24, 2000    DG & BN  Qlogic
289            - Updated SCSI Firmware to following versions:
290              qla12160:   10.01.19
291                  qla1280:     8.09.00
292         Rev. 3.06       Apr 12, 2000    DG & BN  Qlogic
293            - Internal revision; not released
294     Rev. 3.05       Mar 28, 2000    DG & BN  Qlogic
295        - Edit correction for virt_to_bus and PROC.
296     Rev. 3.04       Mar 28, 2000    DG & BN  Qlogic
297        - Merge changes from ia64 port.
298     Rev. 3.03       Mar 28, 2000    BN  Qlogic
299        - Increase version to reflect new code drop with compile fix
300          of issue with inclusion of linux/spinlock for 2.3 kernels
301     Rev. 3.02       Mar 15, 2000    BN  Qlogic
302        - Merge qla1280_proc_info from 2.10 code base
303     Rev. 3.01       Feb 10, 2000    BN  Qlogic
304        - Corrected code to compile on a 2.2.x kernel.
305     Rev. 3.00       Jan 17, 2000    DG  Qlogic
306            - Added 64-bit support.
307     Rev. 2.07       Nov 9, 1999     DG  Qlogic
308            - Added new routine to set target parameters for ISP12160.
309     Rev. 2.06       Sept 10, 1999     DG  Qlogic
310        - Added support for ISP12160 Ultra 3 chip.
311     Rev. 2.03       August 3, 1999    Fred Lewis, Intel DuPont
312         - Modified code to remove errors generated when compiling with
313           Cygnus IA64 Compiler.
314         - Changed conversion of pointers to unsigned longs instead of integers.
315         - Changed type of I/O port variables from uint32_t to unsigned long.
316         - Modified OFFSET macro to work with 64-bit as well as 32-bit.
317         - Changed sprintf and printk format specifiers for pointers to %p.
318         - Changed some int to long type casts where needed in sprintf & printk.
319         - Added l modifiers to sprintf and printk format specifiers for longs.
320         - Removed unused local variables.
321     Rev. 1.20       June 8, 1999      DG,  Qlogic
322          Changes to support RedHat release 6.0 (kernel 2.2.5).
323        - Added SCSI exclusive access lock (io_request_lock) when accessing
324          the adapter.
325        - Added changes for the new LINUX interface template. Some new error
326          handling routines have been added to the template, but for now we
327          will use the old ones.
328     -   Initial Beta Release.
329 *****************************************************************************/
330
331
332 #include <linux/config.h>
333 #include <linux/module.h>
334
335 #include <linux/version.h>
336 #include <linux/types.h>
337 #include <linux/string.h>
338 #include <linux/errno.h>
339 #include <linux/kernel.h>
340 #include <linux/ioport.h>
341 #include <linux/delay.h>
342 #include <linux/timer.h>
343 #include <linux/sched.h>
344 #include <linux/pci.h>
345 #include <linux/proc_fs.h>
346 #include <linux/stat.h>
347 #include <linux/slab.h>
348 #include <linux/pci_ids.h>
349 #include <linux/interrupt.h>
350 #include <linux/init.h>
351
352 #include <asm/io.h>
353 #include <asm/irq.h>
354 #include <asm/byteorder.h>
355 #include <asm/processor.h>
356 #include <asm/types.h>
357 #include <asm/system.h>
358
359 #if LINUX_VERSION_CODE >= 0x020545
360 #include <scsi/scsi.h>
361 #include <scsi/scsi_cmnd.h>
362 #include <scsi/scsi_device.h>
363 #include <scsi/scsi_host.h>
364 #include <scsi/scsi_tcq.h>
365 #else
366 #include <linux/blk.h>
367 #include "scsi.h"
368 #include <scsi/scsi_host.h>
369 #include "sd.h"
370 #endif
371
372 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
373 #include <asm/sn/io.h>
374 #endif
375
376 #if LINUX_VERSION_CODE < 0x020407
377 #error "Kernels older than 2.4.7 are no longer supported"
378 #endif
379
380
381 /*
382  * Compile time Options:
383  *            0 - Disable and 1 - Enable
384  */
385 #define  DEBUG_QLA1280_INTR     0
386 #define  DEBUG_PRINT_NVRAM      0
387 #define  DEBUG_QLA1280          0
388
389 /*
390  * The SGI VISWS is broken and doesn't support MMIO ;-(
391  */
392 #ifdef CONFIG_X86_VISWS
393 #define MEMORY_MAPPED_IO        0
394 #else
395 #define MEMORY_MAPPED_IO        1
396 #endif
397
398 #define UNIQUE_FW_NAME
399 #include "qla1280.h"
400 #include "ql12160_fw.h"         /* ISP RISC codes */
401 #include "ql1280_fw.h"
402 #include "ql1040_fw.h"
403
404
405 /*
406  * Missing PCI ID's
407  */
408 #ifndef PCI_DEVICE_ID_QLOGIC_ISP1080
409 #define PCI_DEVICE_ID_QLOGIC_ISP1080    0x1080
410 #endif
411 #ifndef PCI_DEVICE_ID_QLOGIC_ISP1240
412 #define PCI_DEVICE_ID_QLOGIC_ISP1240    0x1240
413 #endif
414 #ifndef PCI_DEVICE_ID_QLOGIC_ISP1280
415 #define PCI_DEVICE_ID_QLOGIC_ISP1280    0x1280
416 #endif
417 #ifndef PCI_DEVICE_ID_QLOGIC_ISP10160
418 #define PCI_DEVICE_ID_QLOGIC_ISP10160   0x1016
419 #endif
420 #ifndef PCI_DEVICE_ID_QLOGIC_ISP12160
421 #define PCI_DEVICE_ID_QLOGIC_ISP12160   0x1216
422 #endif
423
424 #ifndef PCI_VENDOR_ID_AMI
425 #define PCI_VENDOR_ID_AMI               0x101e
426 #endif
427
428 #ifndef BITS_PER_LONG
429 #error "BITS_PER_LONG not defined!"
430 #endif
431 #if (BITS_PER_LONG == 64) || defined CONFIG_HIGHMEM
432 #define QLA_64BIT_PTR   1
433 #endif
434
435 #ifdef QLA_64BIT_PTR
436 #define pci_dma_hi32(a)                 ((a >> 16) >> 16)
437 #else
438 #define pci_dma_hi32(a)                 0
439 #endif
440 #define pci_dma_lo32(a)                 (a & 0xffffffff)
441
442 #define NVRAM_DELAY()                   udelay(500)     /* 2 microseconds */
443
444 #if LINUX_VERSION_CODE < 0x020500
445 #define HOST_LOCK                       &io_request_lock
446 #define irqreturn_t                     void
447 #define IRQ_RETVAL(foo)
448 #define MSG_ORDERED_TAG                 1
449
450 #define DMA_BIDIRECTIONAL       SCSI_DATA_UNKNOWN
451 #define DMA_TO_DEVICE           SCSI_DATA_WRITE
452 #define DMA_FROM_DEVICE         SCSI_DATA_READ
453 #define DMA_NONE                SCSI_DATA_NONE
454
455 #ifndef HAVE_SECTOR_T
456 typedef unsigned int sector_t;
457 #endif
458
459 static inline void
460 scsi_adjust_queue_depth(struct scsi_device *device, int tag, int depth)
461 {
462         if (tag) {
463                 device->tagged_queue = tag;
464                 device->current_tag = 0;
465         }
466         device->queue_depth = depth;
467 }
468 static inline struct Scsi_Host *scsi_host_alloc(Scsi_Host_Template *t, size_t s)
469 {
470         return scsi_register(t, s);
471 }
472 static inline void scsi_host_put(struct Scsi_Host *h)
473 {
474         scsi_unregister(h);
475 }
476 #else
477 #define HOST_LOCK                       ha->host->host_lock
478 #endif
479 #if LINUX_VERSION_CODE < 0x020600
480 #define DEV_SIMPLE_TAGS(device)         device->tagged_queue
481 /*
482  * Hack around that qla1280_remove_one is called from
483  * qla1280_release in 2.4
484  */
485 #undef __devexit
486 #define __devexit
487 #else
488 #define DEV_SIMPLE_TAGS(device)         device->simple_tags
489 #endif
490 #if defined(__ia64__) && !defined(ia64_platform_is)
491 #define ia64_platform_is(foo)           (!strcmp(x, platform_name))
492 #endif
493
494
495 #define IS_ISP1040(ha) (ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP1020)
496 #define IS_ISP1x40(ha) (ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP1020 || \
497                         ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP1240)
498 #define IS_ISP1x160(ha)        (ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP10160 || \
499                                 ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP12160)
500
501
502 static int qla1280_probe_one(struct pci_dev *, const struct pci_device_id *);
503 static void qla1280_remove_one(struct pci_dev *);
504
505 /*
506  *  QLogic Driver Support Function Prototypes.
507  */
508 static void qla1280_done(struct scsi_qla_host *);
509 #if LINUX_VERSION_CODE < 0x020545
510 static void qla1280_get_target_options(struct scsi_cmnd *, struct scsi_qla_host *);
511 #endif
512 static int qla1280_get_token(char *);
513 static int qla1280_setup(char *s) __init;
514
515 /*
516  *  QLogic ISP1280 Hardware Support Function Prototypes.
517  */
518 static int qla1280_load_firmware(struct scsi_qla_host *);
519 static int qla1280_init_rings(struct scsi_qla_host *);
520 static int qla1280_nvram_config(struct scsi_qla_host *);
521 static int qla1280_mailbox_command(struct scsi_qla_host *,
522                                    uint8_t, uint16_t *);
523 static int qla1280_bus_reset(struct scsi_qla_host *, int);
524 static int qla1280_device_reset(struct scsi_qla_host *, int, int);
525 static int qla1280_abort_device(struct scsi_qla_host *, int, int, int);
526 static int qla1280_abort_command(struct scsi_qla_host *, struct srb *, int);
527 static int qla1280_abort_isp(struct scsi_qla_host *);
528 #ifdef QLA_64BIT_PTR
529 static int qla1280_64bit_start_scsi(struct scsi_qla_host *, struct srb *);
530 #else
531 static int qla1280_32bit_start_scsi(struct scsi_qla_host *, struct srb *);
532 #endif
533 static void qla1280_nv_write(struct scsi_qla_host *, uint16_t);
534 static void qla1280_poll(struct scsi_qla_host *);
535 static void qla1280_reset_adapter(struct scsi_qla_host *);
536 static void qla1280_marker(struct scsi_qla_host *, int, int, int, u8);
537 static void qla1280_isp_cmd(struct scsi_qla_host *);
538 static void qla1280_isr(struct scsi_qla_host *, struct list_head *);
539 static void qla1280_rst_aen(struct scsi_qla_host *);
540 static void qla1280_status_entry(struct scsi_qla_host *, struct response *,
541                                  struct list_head *);
542 static void qla1280_error_entry(struct scsi_qla_host *, struct response *,
543                                 struct list_head *);
544 static uint16_t qla1280_get_nvram_word(struct scsi_qla_host *, uint32_t);
545 static uint16_t qla1280_nvram_request(struct scsi_qla_host *, uint32_t);
546 static uint16_t qla1280_debounce_register(volatile uint16_t __iomem *);
547 static request_t *qla1280_req_pkt(struct scsi_qla_host *);
548 static int qla1280_check_for_dead_scsi_bus(struct scsi_qla_host *,
549                                            unsigned int);
550 static void qla1280_get_target_parameters(struct scsi_qla_host *,
551                                            struct scsi_device *);
552 static int qla1280_set_target_parameters(struct scsi_qla_host *, int, int);
553
554
555 static struct qla_driver_setup driver_setup;
556
557 /*
558  * convert scsi data direction to request_t control flags
559  */
560 static inline uint16_t
561 qla1280_data_direction(struct scsi_cmnd *cmnd)
562 {
563         switch(cmnd->sc_data_direction) {
564         case DMA_FROM_DEVICE:
565                 return BIT_5;
566         case DMA_TO_DEVICE:
567                 return BIT_6;
568         case DMA_BIDIRECTIONAL:
569                 return BIT_5 | BIT_6;
570         /*
571          * We could BUG() on default here if one of the four cases aren't
572          * met, but then again if we receive something like that from the
573          * SCSI layer we have more serious problems. This shuts up GCC.
574          */
575         case DMA_NONE:
576         default:
577                 return 0;
578         }
579 }
580                 
581 #if DEBUG_QLA1280
582 static void __qla1280_print_scsi_cmd(struct scsi_cmnd * cmd);
583 static void __qla1280_dump_buffer(char *, int);
584 #endif
585
586
587 /*
588  * insmod needs to find the variable and make it point to something
589  */
590 #ifdef MODULE
591 static char *qla1280;
592
593 /* insmod qla1280 options=verbose" */
594 module_param(qla1280, charp, 0);
595 #else
596 __setup("qla1280=", qla1280_setup);
597 #endif
598
599
600 /*
601  * We use the scsi_pointer structure that's included with each scsi_command
602  * to overlay our struct srb over it. qla1280_init() checks that a srb is not
603  * bigger than a scsi_pointer.
604  */
605
606 #define CMD_SP(Cmnd)            &Cmnd->SCp
607 #define CMD_CDBLEN(Cmnd)        Cmnd->cmd_len
608 #define CMD_CDBP(Cmnd)          Cmnd->cmnd
609 #define CMD_SNSP(Cmnd)          Cmnd->sense_buffer
610 #define CMD_SNSLEN(Cmnd)        sizeof(Cmnd->sense_buffer)
611 #define CMD_RESULT(Cmnd)        Cmnd->result
612 #define CMD_HANDLE(Cmnd)        Cmnd->host_scribble
613 #if LINUX_VERSION_CODE < 0x020545
614 #define CMD_REQUEST(Cmnd)       Cmnd->request.cmd
615 #else
616 #define CMD_REQUEST(Cmnd)       Cmnd->request->cmd
617 #endif
618
619 #define CMD_HOST(Cmnd)          Cmnd->device->host
620 #define SCSI_BUS_32(Cmnd)       Cmnd->device->channel
621 #define SCSI_TCN_32(Cmnd)       Cmnd->device->id
622 #define SCSI_LUN_32(Cmnd)       Cmnd->device->lun
623
624
625 /*****************************************/
626 /*   ISP Boards supported by this driver */
627 /*****************************************/
628
629 struct qla_boards {
630         unsigned char name[9];  /* Board ID String */
631         int numPorts;           /* Number of SCSI ports */
632         unsigned short *fwcode; /* pointer to FW array         */
633         unsigned short *fwlen;  /* number of words in array    */
634         unsigned short *fwstart;        /* start address for F/W       */
635         unsigned char *fwver;   /* Ptr to F/W version array    */
636 };
637
638 /* NOTE: the last argument in each entry is used to index ql1280_board_tbl */
639 static struct pci_device_id qla1280_pci_tbl[] = {
640         {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP12160,
641                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
642 #ifdef CONFIG_SCSI_QLOGIC_1280_1040
643         {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1020,
644                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
645 #endif
646         {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1080,
647                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
648         {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1240,
649                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3},
650         {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1280,
651                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4},
652         {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP10160,
653                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5},
654         {0,}
655 };
656 MODULE_DEVICE_TABLE(pci, qla1280_pci_tbl);
657
658 static struct qla_boards ql1280_board_tbl[] = {
659         /* Name ,  Number of ports, FW details */
660         {"QLA12160", 2, &fw12160i_code01[0], &fw12160i_length01,
661          &fw12160i_addr01, &fw12160i_version_str[0]},
662         {"QLA1040", 1, &risc_code01[0], &risc_code_length01,
663          &risc_code_addr01, &firmware_version[0]},
664         {"QLA1080", 1, &fw1280ei_code01[0], &fw1280ei_length01,
665          &fw1280ei_addr01, &fw1280ei_version_str[0]},
666         {"QLA1240", 2, &fw1280ei_code01[0], &fw1280ei_length01,
667          &fw1280ei_addr01, &fw1280ei_version_str[0]},
668         {"QLA1280", 2, &fw1280ei_code01[0], &fw1280ei_length01,
669          &fw1280ei_addr01, &fw1280ei_version_str[0]},
670         {"QLA10160", 1, &fw12160i_code01[0], &fw12160i_length01,
671          &fw12160i_addr01, &fw12160i_version_str[0]},
672         {"        ", 0}
673 };
674
675 static int qla1280_verbose = 1;
676
677 #if DEBUG_QLA1280
678 static int ql_debug_level = 1;
679 #define dprintk(level, format, a...)    \
680         do { if (ql_debug_level >= level) printk(KERN_ERR format, ##a); } while(0)
681 #define qla1280_dump_buffer(level, buf, size)   \
682         if (ql_debug_level >= level) __qla1280_dump_buffer(buf, size)
683 #define qla1280_print_scsi_cmd(level, cmd)      \
684         if (ql_debug_level >= level) __qla1280_print_scsi_cmd(cmd)
685 #else
686 #define ql_debug_level                  0
687 #define dprintk(level, format, a...)    do{}while(0)
688 #define qla1280_dump_buffer(a, b, c)    do{}while(0)
689 #define qla1280_print_scsi_cmd(a, b)    do{}while(0)
690 #endif
691
692 #define ENTER(x)                dprintk(3, "qla1280 : Entering %s()\n", x);
693 #define LEAVE(x)                dprintk(3, "qla1280 : Leaving %s()\n", x);
694 #define ENTER_INTR(x)           dprintk(4, "qla1280 : Entering %s()\n", x);
695 #define LEAVE_INTR(x)           dprintk(4, "qla1280 : Leaving %s()\n", x);
696
697
698 static int qla1280_read_nvram(struct scsi_qla_host *ha)
699 {
700         uint16_t *wptr;
701         uint8_t chksum;
702         int cnt, i;
703         struct nvram *nv;
704
705         ENTER("qla1280_read_nvram");
706
707         if (driver_setup.no_nvram)
708                 return 1;
709
710         printk(KERN_INFO "scsi(%ld): Reading NVRAM\n", ha->host_no);
711
712         wptr = (uint16_t *)&ha->nvram;
713         nv = &ha->nvram;
714         chksum = 0;
715         for (cnt = 0; cnt < 3; cnt++) {
716                 *wptr = qla1280_get_nvram_word(ha, cnt);
717                 chksum += *wptr & 0xff;
718                 chksum += (*wptr >> 8) & 0xff;
719                 wptr++;
720         }
721
722         if (nv->id0 != 'I' || nv->id1 != 'S' ||
723             nv->id2 != 'P' || nv->id3 != ' ' || nv->version < 1) {
724                 dprintk(2, "Invalid nvram ID or version!\n");
725                 chksum = 1;
726         } else {
727                 for (; cnt < sizeof(struct nvram); cnt++) {
728                         *wptr = qla1280_get_nvram_word(ha, cnt);
729                         chksum += *wptr & 0xff;
730                         chksum += (*wptr >> 8) & 0xff;
731                         wptr++;
732                 }
733         }
734
735         dprintk(3, "qla1280_read_nvram: NVRAM Magic ID= %c %c %c %02x"
736                " version %i\n", nv->id0, nv->id1, nv->id2, nv->id3,
737                nv->version);
738
739
740         if (chksum) {
741                 if (!driver_setup.no_nvram)
742                         printk(KERN_WARNING "scsi(%ld): Unable to identify or "
743                                "validate NVRAM checksum, using default "
744                                "settings\n", ha->host_no);
745                 ha->nvram_valid = 0;
746         } else
747                 ha->nvram_valid = 1;
748
749         /* The firmware interface is, um, interesting, in that the
750          * actual firmware image on the chip is little endian, thus,
751          * the process of taking that image to the CPU would end up
752          * little endian.  However, the firmare interface requires it
753          * to be read a word (two bytes) at a time.
754          *
755          * The net result of this would be that the word (and
756          * doubleword) quantites in the firmware would be correct, but
757          * the bytes would be pairwise reversed.  Since most of the
758          * firmware quantites are, in fact, bytes, we do an extra
759          * le16_to_cpu() in the firmware read routine.
760          *
761          * The upshot of all this is that the bytes in the firmware
762          * are in the correct places, but the 16 and 32 bit quantites
763          * are still in little endian format.  We fix that up below by
764          * doing extra reverses on them */
765         nv->isp_parameter = cpu_to_le16(nv->isp_parameter);
766         nv->firmware_feature.w = cpu_to_le16(nv->firmware_feature.w);
767         for(i = 0; i < MAX_BUSES; i++) {
768                 nv->bus[i].selection_timeout = cpu_to_le16(nv->bus[i].selection_timeout);
769                 nv->bus[i].max_queue_depth = cpu_to_le16(nv->bus[i].max_queue_depth);
770         }
771         dprintk(1, "qla1280_read_nvram: Completed Reading NVRAM\n");
772         LEAVE("qla1280_read_nvram");
773
774         return chksum;
775 }
776
777 /**************************************************************************
778  *   qla1280_info
779  *     Return a string describing the driver.
780  **************************************************************************/
781 static const char *
782 qla1280_info(struct Scsi_Host *host)
783 {
784         static char qla1280_scsi_name_buffer[125];
785         char *bp;
786         struct scsi_qla_host *ha;
787         struct qla_boards *bdp;
788
789         bp = &qla1280_scsi_name_buffer[0];
790         ha = (struct scsi_qla_host *)host->hostdata;
791         bdp = &ql1280_board_tbl[ha->devnum];
792         memset(bp, 0, sizeof(qla1280_scsi_name_buffer));
793
794         sprintf (bp,
795                  "QLogic %s PCI to SCSI Host Adapter\n"
796                  "       Firmware version: %2d.%02d.%02d, Driver version %s",
797                  &bdp->name[0], bdp->fwver[0], bdp->fwver[1], bdp->fwver[2],
798                  QLA1280_VERSION);
799         return bp;
800 }
801
802 /**************************************************************************
803  *   qla1200_queuecommand
804  *     Queue a command to the controller.
805  *
806  * Note:
807  * The mid-level driver tries to ensures that queuecommand never gets invoked
808  * concurrently with itself or the interrupt handler (although the
809  * interrupt handler may call this routine as part of request-completion
810  * handling).   Unfortunely, it sometimes calls the scheduler in interrupt
811  * context which is a big NO! NO!.
812  **************************************************************************/
813 static int
814 qla1280_queuecommand(struct scsi_cmnd *cmd, void (*fn)(struct scsi_cmnd *))
815 {
816         struct Scsi_Host *host = cmd->device->host;
817         struct scsi_qla_host *ha = (struct scsi_qla_host *)host->hostdata;
818         struct srb *sp = (struct srb *)&cmd->SCp;
819         int status;
820
821         cmd->scsi_done = fn;
822         sp->cmd = cmd;
823         sp->flags = 0;
824
825         qla1280_print_scsi_cmd(5, cmd);
826
827 #ifdef QLA_64BIT_PTR
828         /*
829          * Using 64 bit commands if the PCI bridge doesn't support it is a
830          * bit wasteful, however this should really only happen if one's
831          * PCI controller is completely broken, like the BCM1250. For
832          * sane hardware this is not an issue.
833          */
834         status = qla1280_64bit_start_scsi(ha, sp);
835 #else
836         status = qla1280_32bit_start_scsi(ha, sp);
837 #endif
838         return status;
839 }
840
841 enum action {
842         ABORT_COMMAND,
843         ABORT_DEVICE,
844         DEVICE_RESET,
845         BUS_RESET,
846         ADAPTER_RESET,
847         FAIL
848 };
849
850 /* timer action for error action processor */
851 static void qla1280_error_wait_timeout(unsigned long __data)
852 {
853         struct scsi_cmnd *cmd = (struct scsi_cmnd *)__data;
854         struct srb *sp = (struct srb *)CMD_SP(cmd);
855
856         complete(sp->wait);
857 }
858
859 static void qla1280_mailbox_timeout(unsigned long __data)
860 {
861         struct scsi_qla_host *ha = (struct scsi_qla_host *)__data;
862         struct device_reg __iomem *reg;
863         reg = ha->iobase;
864
865         ha->mailbox_out[0] = RD_REG_WORD(&reg->mailbox0);
866         printk(KERN_ERR "scsi(%ld): mailbox timed out, mailbox0 %04x, "
867                "ictrl %04x, istatus %04x\n", ha->host_no, ha->mailbox_out[0],
868                RD_REG_WORD(&reg->ictrl), RD_REG_WORD(&reg->istatus));
869         complete(ha->mailbox_wait);
870 }
871
872 /**************************************************************************
873  * qla1200_error_action
874  *    The function will attempt to perform a specified error action and
875  *    wait for the results (or time out).
876  *
877  * Input:
878  *      cmd = Linux SCSI command packet of the command that cause the
879  *            bus reset.
880  *      action = error action to take (see action_t)
881  *
882  * Returns:
883  *      SUCCESS or FAILED
884  *
885  * Note:
886  *      Resetting the bus always succeeds - is has to, otherwise the
887  *      kernel will panic! Try a surgical technique - sending a BUS
888  *      DEVICE RESET message - on the offending target before pulling
889  *      the SCSI bus reset line.
890  **************************************************************************/
891 static int
892 qla1280_error_action(struct scsi_cmnd *cmd, enum action action)
893 {
894         struct scsi_qla_host *ha;
895         int bus, target, lun;
896         struct srb *sp;
897         uint16_t data;
898         unsigned char *handle;
899         int result, i;
900         DECLARE_COMPLETION(wait);
901         struct timer_list timer;
902
903         ha = (struct scsi_qla_host *)(CMD_HOST(cmd)->hostdata);
904
905         dprintk(4, "error_action %i, istatus 0x%04x\n", action,
906                 RD_REG_WORD(&ha->iobase->istatus));
907
908         dprintk(4, "host_cmd 0x%04x, ictrl 0x%04x, jiffies %li\n",
909                 RD_REG_WORD(&ha->iobase->host_cmd),
910                 RD_REG_WORD(&ha->iobase->ictrl), jiffies);
911
912         ENTER("qla1280_error_action");
913         if (qla1280_verbose)
914                 printk(KERN_INFO "scsi(%li): Resetting Cmnd=0x%p, "
915                        "Handle=0x%p, action=0x%x\n",
916                        ha->host_no, cmd, CMD_HANDLE(cmd), action);
917
918         if (cmd == NULL) {
919                 printk(KERN_WARNING "(scsi?:?:?:?) Reset called with NULL "
920                        "si_Cmnd pointer, failing.\n");
921                 LEAVE("qla1280_error_action");
922                 return FAILED;
923         }
924
925         ha = (struct scsi_qla_host *)cmd->device->host->hostdata;
926         sp = (struct srb *)CMD_SP(cmd);
927         handle = CMD_HANDLE(cmd);
928
929         /* Check for pending interrupts. */
930         data = qla1280_debounce_register(&ha->iobase->istatus);
931         /*
932          * The io_request_lock is held when the reset handler is called, hence
933          * the interrupt handler cannot be running in parallel as it also
934          * grabs the lock. /Jes
935          */
936         if (data & RISC_INT)
937                 qla1280_isr(ha, &ha->done_q);
938
939         /*
940          * Determine the suggested action that the mid-level driver wants
941          * us to perform.
942          */
943         if (handle == (unsigned char *)INVALID_HANDLE || handle == NULL) {
944                 if(action == ABORT_COMMAND) {
945                         /* we never got this command */
946                         printk(KERN_INFO "qla1280: Aborting a NULL handle\n");
947                         return SUCCESS; /* no action - we don't have command */
948                 }
949         } else {
950                 sp->wait = &wait;
951         }
952
953         bus = SCSI_BUS_32(cmd);
954         target = SCSI_TCN_32(cmd);
955         lun = SCSI_LUN_32(cmd);
956
957         /* Overloading result.  Here it means the success or fail of the
958          * *issue* of the action.  When we return from the routine, it must
959          * mean the actual success or fail of the action */
960         result = FAILED;
961         switch (action) {
962         case FAIL:
963                 break;
964
965         case ABORT_COMMAND:
966                 if ((sp->flags & SRB_ABORT_PENDING)) {
967                         printk(KERN_WARNING
968                                "scsi(): Command has a pending abort "
969                                "message - ABORT_PENDING.\n");
970                         /* This should technically be impossible since we
971                          * now wait for abort completion */
972                         break;
973                 }
974
975                 for (i = 0; i < MAX_OUTSTANDING_COMMANDS; i++) {
976                         if (sp == ha->outstanding_cmds[i]) {
977                                 dprintk(1, "qla1280: RISC aborting command\n");
978                                 if (qla1280_abort_command(ha, sp, i) == 0)
979                                         result = SUCCESS;
980                                 else {
981                                         /*
982                                          * Since we don't know what might
983                                          * have happend to the command, it
984                                          * is unsafe to remove it from the
985                                          * device's queue at this point.
986                                          * Wait and let the escalation
987                                          * process take care of it.
988                                          */
989                                         printk(KERN_WARNING
990                                                "scsi(%li:%i:%i:%i): Unable"
991                                                " to abort command!\n",
992                                                ha->host_no, bus, target, lun);
993                                 }
994                         }
995                 }
996                 break;
997
998         case ABORT_DEVICE:
999                 if (qla1280_verbose)
1000                         printk(KERN_INFO
1001                                "scsi(%ld:%d:%d:%d): Queueing abort device "
1002                                "command.\n", ha->host_no, bus, target, lun);
1003                 if (qla1280_abort_device(ha, bus, target, lun) == 0)
1004                         result = SUCCESS;
1005                 break;
1006
1007         case DEVICE_RESET:
1008                 if (qla1280_verbose)
1009                         printk(KERN_INFO
1010                                "scsi(%ld:%d:%d:%d): Queueing device reset "
1011                                "command.\n", ha->host_no, bus, target, lun);
1012                 if (qla1280_device_reset(ha, bus, target) == 0)
1013                         result = SUCCESS;
1014                 break;
1015
1016         case BUS_RESET:
1017                 if (qla1280_verbose)
1018                         printk(KERN_INFO "qla1280(%ld:%d): Issuing BUS "
1019                                "DEVICE RESET\n", ha->host_no, bus);
1020                 if (qla1280_bus_reset(ha, bus == 0))
1021                         result = SUCCESS;
1022
1023                 break;
1024
1025         case ADAPTER_RESET:
1026         default:
1027                 if (qla1280_verbose) {
1028                         printk(KERN_INFO
1029                                "scsi(%ld): Issued ADAPTER RESET\n",
1030                                ha->host_no);
1031                         printk(KERN_INFO "scsi(%ld): I/O processing will "
1032                                "continue automatically\n", ha->host_no);
1033                 }
1034                 ha->flags.reset_active = 1;
1035                 /*
1036                  * We restarted all of the commands automatically, so the
1037                  * mid-level code can expect completions momentitarily.
1038                  */
1039                 if (qla1280_abort_isp(ha) == 0)
1040                         result = SUCCESS;
1041
1042                 ha->flags.reset_active = 0;
1043         }
1044
1045         if (!list_empty(&ha->done_q))
1046                 qla1280_done(ha);
1047
1048         /* If we didn't manage to issue the action, or we have no
1049          * command to wait for, exit here */
1050         if (result == FAILED || handle == NULL ||
1051             handle == (unsigned char *)INVALID_HANDLE) {
1052                 /*
1053                  * Clear completion queue to avoid qla1280_done() trying
1054                  * to complete the command at a later stage after we
1055                  * have exited the current context
1056                  */
1057                 sp->wait = NULL;
1058                 goto leave;
1059         }
1060
1061         /* set up a timer just in case we're really jammed */
1062         init_timer(&timer);
1063         timer.expires = jiffies + 4*HZ;
1064         timer.data = (unsigned long)cmd;
1065         timer.function = qla1280_error_wait_timeout;
1066         add_timer(&timer);
1067
1068         /* wait for the action to complete (or the timer to expire) */
1069         spin_unlock_irq(HOST_LOCK);
1070         wait_for_completion(&wait);
1071         del_timer_sync(&timer);
1072         spin_lock_irq(HOST_LOCK);
1073         sp->wait = NULL;
1074
1075         /* the only action we might get a fail for is abort */
1076         if (action == ABORT_COMMAND) {
1077                 if(sp->flags & SRB_ABORTED)
1078                         result = SUCCESS;
1079                 else
1080                         result = FAILED;
1081         }
1082
1083  leave:
1084         dprintk(1, "RESET returning %d\n", result);
1085
1086         LEAVE("qla1280_error_action");
1087         return result;
1088 }
1089
1090 /**************************************************************************
1091  *   qla1280_abort
1092  *     Abort the specified SCSI command(s).
1093  **************************************************************************/
1094 static int
1095 qla1280_eh_abort(struct scsi_cmnd * cmd)
1096 {
1097         int rc;
1098
1099         spin_lock_irq(cmd->device->host->host_lock);
1100         rc = qla1280_error_action(cmd, ABORT_COMMAND);
1101         spin_unlock_irq(cmd->device->host->host_lock);
1102
1103         return rc;
1104 }
1105
1106 /**************************************************************************
1107  *   qla1280_device_reset
1108  *     Reset the specified SCSI device
1109  **************************************************************************/
1110 static int
1111 qla1280_eh_device_reset(struct scsi_cmnd *cmd)
1112 {
1113         int rc;
1114
1115         spin_lock_irq(cmd->device->host->host_lock);
1116         rc = qla1280_error_action(cmd, DEVICE_RESET);
1117         spin_unlock_irq(cmd->device->host->host_lock);
1118
1119         return rc;
1120 }
1121
1122 /**************************************************************************
1123  *   qla1280_bus_reset
1124  *     Reset the specified bus.
1125  **************************************************************************/
1126 static int
1127 qla1280_eh_bus_reset(struct scsi_cmnd *cmd)
1128 {
1129         int rc;
1130
1131         spin_lock_irq(cmd->device->host->host_lock);
1132         rc = qla1280_error_action(cmd, BUS_RESET);
1133         spin_unlock_irq(cmd->device->host->host_lock);
1134
1135         return rc;
1136 }
1137
1138 /**************************************************************************
1139  *   qla1280_adapter_reset
1140  *     Reset the specified adapter (both channels)
1141  **************************************************************************/
1142 static int
1143 qla1280_eh_adapter_reset(struct scsi_cmnd *cmd)
1144 {
1145         int rc;
1146
1147         spin_lock_irq(cmd->device->host->host_lock);
1148         rc = qla1280_error_action(cmd, ADAPTER_RESET);
1149         spin_unlock_irq(cmd->device->host->host_lock);
1150
1151         return rc;
1152 }
1153
1154 static int
1155 qla1280_biosparam(struct scsi_device *sdev, struct block_device *bdev,
1156                   sector_t capacity, int geom[])
1157 {
1158         int heads, sectors, cylinders;
1159
1160         heads = 64;
1161         sectors = 32;
1162         cylinders = (unsigned long)capacity / (heads * sectors);
1163         if (cylinders > 1024) {
1164                 heads = 255;
1165                 sectors = 63;
1166                 cylinders = (unsigned long)capacity / (heads * sectors);
1167                 /* if (cylinders > 1023)
1168                    cylinders = 1023; */
1169         }
1170
1171         geom[0] = heads;
1172         geom[1] = sectors;
1173         geom[2] = cylinders;
1174
1175         return 0;
1176 }
1177
1178 #if LINUX_VERSION_CODE < 0x020600
1179 static int
1180 qla1280_detect(Scsi_Host_Template *template)
1181 {
1182         struct pci_device_id *id = &qla1280_pci_tbl[0];
1183         struct pci_dev *pdev = NULL;
1184         int num_hosts = 0;
1185
1186         if (sizeof(struct srb) > sizeof(Scsi_Pointer)) {
1187                 printk(KERN_WARNING
1188                        "qla1280: struct srb too big, aborting\n");
1189                 return 0;
1190         }
1191
1192         if ((DMA_BIDIRECTIONAL != PCI_DMA_BIDIRECTIONAL) ||
1193             (DMA_TO_DEVICE != PCI_DMA_TODEVICE) ||
1194             (DMA_FROM_DEVICE != PCI_DMA_FROMDEVICE) ||
1195             (DMA_NONE != PCI_DMA_NONE)) {
1196                 printk(KERN_WARNING
1197                        "qla1280: dma direction bits don't match\n");
1198                 return 0;
1199         }
1200
1201 #ifdef MODULE
1202         /*
1203          * If we are called as a module, the qla1280 pointer may not be null
1204          * and it would point to our bootup string, just like on the lilo
1205          * command line.  IF not NULL, then process this config string with
1206          * qla1280_setup
1207          *
1208          * Boot time Options
1209          * To add options at boot time add a line to your lilo.conf file like:
1210          * append="qla1280=verbose,max_tags:{{255,255,255,255},{255,255,255,255}}"
1211          * which will result in the first four devices on the first two
1212          * controllers being set to a tagged queue depth of 32.
1213          */
1214         if (qla1280)
1215                 qla1280_setup(qla1280);
1216 #endif
1217
1218         /* First Initialize QLA12160 on PCI Bus 1 Dev 2 */
1219         while ((pdev = pci_find_device(id->vendor, id->device, pdev))) {
1220                 if (pdev->bus->number == 1 && PCI_SLOT(pdev->devfn) == 2) {
1221                         if (!qla1280_probe_one(pdev, id))
1222                                 num_hosts++;
1223                 }
1224         }
1225
1226         pdev = NULL;
1227         /* Try and find each different type of adapter we support */
1228         for (id = &qla1280_pci_tbl[0]; id->device; id++) {
1229                 while ((pdev = pci_find_device(id->vendor, id->device, pdev))) {
1230                         /*
1231                          * skip QLA12160 already initialized on
1232                          * PCI Bus 1 Dev 2 since we already initialized
1233                          * and presented it
1234                          */
1235                         if (id->device == PCI_DEVICE_ID_QLOGIC_ISP12160 &&
1236                             pdev->bus->number == 1 &&
1237                             PCI_SLOT(pdev->devfn) == 2)
1238                                 continue;
1239
1240                         if (!qla1280_probe_one(pdev, id))
1241                                 num_hosts++;
1242                 }
1243         }
1244
1245         return num_hosts;
1246 }
1247
1248 /*
1249  * This looks a bit ugly as we could just pass down host to
1250  * qla1280_remove_one, but I want to keep qla1280_release purely a wrapper
1251  * around pci_driver::remove as used from 2.6 onwards.
1252  */
1253 static int
1254 qla1280_release(struct Scsi_Host *host)
1255 {
1256         struct scsi_qla_host *ha = (struct scsi_qla_host *)host->hostdata;
1257
1258         qla1280_remove_one(ha->pdev);
1259         return 0;
1260 }
1261
1262 static int
1263 qla1280_biosparam_old(Disk * disk, kdev_t dev, int geom[])
1264 {
1265         return qla1280_biosparam(disk->device, NULL, disk->capacity, geom);
1266 }
1267 #endif
1268
1269 /**************************************************************************
1270  * qla1280_intr_handler
1271  *   Handles the H/W interrupt
1272  **************************************************************************/
1273 static irqreturn_t
1274 qla1280_intr_handler(int irq, void *dev_id, struct pt_regs *regs)
1275 {
1276         struct scsi_qla_host *ha;
1277         struct device_reg __iomem *reg;
1278         u16 data;
1279         int handled = 0;
1280
1281         ENTER_INTR ("qla1280_intr_handler");
1282         ha = (struct scsi_qla_host *)dev_id;
1283
1284         spin_lock(HOST_LOCK);
1285
1286         ha->isr_count++;
1287         reg = ha->iobase;
1288
1289         WRT_REG_WORD(&reg->ictrl, 0);   /* disable our interrupt. */
1290
1291         data = qla1280_debounce_register(&reg->istatus);
1292         /* Check for pending interrupts. */
1293         if (data & RISC_INT) {  
1294                 qla1280_isr(ha, &ha->done_q);
1295                 handled = 1;
1296         }
1297         if (!list_empty(&ha->done_q))
1298                 qla1280_done(ha);
1299
1300         spin_unlock(HOST_LOCK);
1301
1302         /* enable our interrupt. */
1303         WRT_REG_WORD(&reg->ictrl, (ISP_EN_INT | ISP_EN_RISC));
1304
1305         LEAVE_INTR("qla1280_intr_handler");
1306         return IRQ_RETVAL(handled);
1307 }
1308
1309
1310 static int
1311 qla1280_set_target_parameters(struct scsi_qla_host *ha, int bus, int target)
1312 {
1313         uint8_t mr;
1314         uint16_t mb[MAILBOX_REGISTER_COUNT];
1315         struct nvram *nv;
1316         int status;
1317
1318         nv = &ha->nvram;
1319
1320         mr = BIT_3 | BIT_2 | BIT_1 | BIT_0;
1321
1322         /* Set Target Parameters. */
1323         mb[0] = MBC_SET_TARGET_PARAMETERS;
1324         mb[1] = (uint16_t) (bus ? target | BIT_7 : target);
1325         mb[1] <<= 8;
1326
1327         mb[2] = (nv->bus[bus].target[target].parameter.c << 8);
1328
1329         if (IS_ISP1x160(ha)) {
1330                 mb[2] |= nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr << 5;
1331                 mb[3] = (nv->bus[bus].target[target].flags.flags1x160.sync_offset << 8) |
1332                          nv->bus[bus].target[target].sync_period;
1333                 mb[6] = (nv->bus[bus].target[target].ppr_1x160.flags.ppr_options << 8) |
1334                          nv->bus[bus].target[target].ppr_1x160.flags.ppr_bus_width;
1335                 mr |= BIT_6;
1336         } else {
1337                 mb[3] = (nv->bus[bus].target[target].flags.flags1x80.sync_offset << 8) |
1338                          nv->bus[bus].target[target].sync_period;
1339         }
1340
1341         status = qla1280_mailbox_command(ha, mr, &mb[0]);
1342
1343         if (status)
1344                 printk(KERN_WARNING "scsi(%ld:%i:%i): "
1345                        "qla1280_set_target_parameters() failed\n",
1346                        ha->host_no, bus, target);
1347         return status;
1348 }
1349
1350
1351 /**************************************************************************
1352  *   qla1280_slave_configure
1353  *
1354  * Description:
1355  *   Determines the queue depth for a given device.  There are two ways
1356  *   a queue depth can be obtained for a tagged queueing device.  One
1357  *   way is the default queue depth which is determined by whether
1358  *   If it is defined, then it is used
1359  *   as the default queue depth.  Otherwise, we use either 4 or 8 as the
1360  *   default queue depth (dependent on the number of hardware SCBs).
1361  **************************************************************************/
1362 static int
1363 qla1280_slave_configure(struct scsi_device *device)
1364 {
1365         struct scsi_qla_host *ha;
1366         int default_depth = 3;
1367         int bus = device->channel;
1368         int target = device->id;
1369         int status = 0;
1370         struct nvram *nv;
1371         unsigned long flags;
1372
1373         ha = (struct scsi_qla_host *)device->host->hostdata;
1374         nv = &ha->nvram;
1375
1376         if (qla1280_check_for_dead_scsi_bus(ha, bus))
1377                 return 1;
1378
1379         if (device->tagged_supported &&
1380             (ha->bus_settings[bus].qtag_enables & (BIT_0 << target))) {
1381                 scsi_adjust_queue_depth(device, MSG_ORDERED_TAG,
1382                                         ha->bus_settings[bus].hiwat);
1383         } else {
1384                 scsi_adjust_queue_depth(device, 0, default_depth);
1385         }
1386
1387 #if LINUX_VERSION_CODE > 0x020500
1388         nv->bus[bus].target[target].parameter.f.enable_sync = device->sdtr;
1389         nv->bus[bus].target[target].parameter.f.enable_wide = device->wdtr;
1390         nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr = device->ppr;
1391 #endif
1392
1393         if (driver_setup.no_sync ||
1394             (driver_setup.sync_mask &&
1395              (~driver_setup.sync_mask & (1 << target))))
1396                 nv->bus[bus].target[target].parameter.f.enable_sync = 0;
1397         if (driver_setup.no_wide ||
1398             (driver_setup.wide_mask &&
1399              (~driver_setup.wide_mask & (1 << target))))
1400                 nv->bus[bus].target[target].parameter.f.enable_wide = 0;
1401         if (IS_ISP1x160(ha)) {
1402                 if (driver_setup.no_ppr ||
1403                     (driver_setup.ppr_mask &&
1404                      (~driver_setup.ppr_mask & (1 << target))))
1405                         nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr = 0;
1406         }
1407
1408         spin_lock_irqsave(HOST_LOCK, flags);
1409         if (nv->bus[bus].target[target].parameter.f.enable_sync)
1410                 status = qla1280_set_target_parameters(ha, bus, target);
1411         qla1280_get_target_parameters(ha, device);
1412         spin_unlock_irqrestore(HOST_LOCK, flags);
1413         return status;
1414 }
1415
1416 #if LINUX_VERSION_CODE < 0x020545
1417 /**************************************************************************
1418  *   qla1280_select_queue_depth
1419  *
1420  *   Sets the queue depth for each SCSI device hanging off the input
1421  *   host adapter.  We use a queue depth of 2 for devices that do not
1422  *   support tagged queueing.
1423  **************************************************************************/
1424 static void
1425 qla1280_select_queue_depth(struct Scsi_Host *host, struct scsi_device *sdev_q)
1426 {
1427         struct scsi_qla_host *ha = (struct scsi_qla_host *)host->hostdata;
1428         struct scsi_device *sdev;
1429
1430         ENTER("qla1280_select_queue_depth");
1431         for (sdev = sdev_q; sdev; sdev = sdev->next)
1432                 if (sdev->host == host)
1433                         qla1280_slave_configure(sdev);
1434
1435         if (sdev_q)
1436                 qla1280_check_for_dead_scsi_bus(ha, sdev_q->channel);
1437         LEAVE("qla1280_select_queue_depth");
1438 }
1439 #endif
1440
1441 /*
1442  * qla1280_done
1443  *      Process completed commands.
1444  *
1445  * Input:
1446  *      ha           = adapter block pointer.
1447  *      done_q       = done queue.
1448  */
1449 static void
1450 qla1280_done(struct scsi_qla_host *ha)
1451 {
1452         struct srb *sp;
1453         struct list_head *done_q;
1454         int bus, target, lun;
1455         struct scsi_cmnd *cmd;
1456
1457         ENTER("qla1280_done");
1458
1459         done_q = &ha->done_q;
1460
1461         while (!list_empty(done_q)) {
1462                 sp = list_entry(done_q->next, struct srb, list);
1463
1464                 list_del(&sp->list);
1465         
1466                 cmd = sp->cmd;
1467                 bus = SCSI_BUS_32(cmd);
1468                 target = SCSI_TCN_32(cmd);
1469                 lun = SCSI_LUN_32(cmd);
1470
1471                 switch ((CMD_RESULT(cmd) >> 16)) {
1472                 case DID_RESET:
1473                         /* Issue marker command. */
1474                         qla1280_marker(ha, bus, target, 0, MK_SYNC_ID);
1475                         break;
1476                 case DID_ABORT:
1477                         sp->flags &= ~SRB_ABORT_PENDING;
1478                         sp->flags |= SRB_ABORTED;
1479                         if (sp->flags & SRB_TIMEOUT)
1480                                 CMD_RESULT(sp->cmd) = DID_TIME_OUT << 16;
1481                         break;
1482                 default:
1483                         break;
1484                 }
1485
1486                 /* Release memory used for this I/O */
1487                 if (cmd->use_sg) {
1488                         pci_unmap_sg(ha->pdev, cmd->request_buffer,
1489                                         cmd->use_sg, cmd->sc_data_direction);
1490                 } else if (cmd->request_bufflen) {
1491                         pci_unmap_single(ha->pdev, sp->saved_dma_handle,
1492                                         cmd->request_bufflen,
1493                                         cmd->sc_data_direction);
1494                 }
1495
1496                 /* Call the mid-level driver interrupt handler */
1497                 CMD_HANDLE(sp->cmd) = (unsigned char *)INVALID_HANDLE;
1498                 ha->actthreads--;
1499
1500 #if LINUX_VERSION_CODE < 0x020500
1501                 if (cmd->cmnd[0] == INQUIRY)
1502                         qla1280_get_target_options(cmd, ha);
1503 #endif
1504                 (*(cmd)->scsi_done)(cmd);
1505
1506                 if(sp->wait != NULL)
1507                         complete(sp->wait);
1508         }
1509         LEAVE("qla1280_done");
1510 }
1511
1512 /*
1513  * Translates a ISP error to a Linux SCSI error
1514  */
1515 static int
1516 qla1280_return_status(struct response * sts, struct scsi_cmnd *cp)
1517 {
1518         int host_status = DID_ERROR;
1519         uint16_t comp_status = le16_to_cpu(sts->comp_status);
1520         uint16_t state_flags = le16_to_cpu(sts->state_flags);
1521         uint16_t residual_length = le16_to_cpu(sts->residual_length);
1522         uint16_t scsi_status = le16_to_cpu(sts->scsi_status);
1523 #if DEBUG_QLA1280_INTR
1524         static char *reason[] = {
1525                 "DID_OK",
1526                 "DID_NO_CONNECT",
1527                 "DID_BUS_BUSY",
1528                 "DID_TIME_OUT",
1529                 "DID_BAD_TARGET",
1530                 "DID_ABORT",
1531                 "DID_PARITY",
1532                 "DID_ERROR",
1533                 "DID_RESET",
1534                 "DID_BAD_INTR"
1535         };
1536 #endif                          /* DEBUG_QLA1280_INTR */
1537
1538         ENTER("qla1280_return_status");
1539
1540 #if DEBUG_QLA1280_INTR
1541         /*
1542           dprintk(1, "qla1280_return_status: compl status = 0x%04x\n",
1543           comp_status);
1544         */
1545 #endif
1546
1547         switch (comp_status) {
1548         case CS_COMPLETE:
1549                 host_status = DID_OK;
1550                 break;
1551
1552         case CS_INCOMPLETE:
1553                 if (!(state_flags & SF_GOT_BUS))
1554                         host_status = DID_NO_CONNECT;
1555                 else if (!(state_flags & SF_GOT_TARGET))
1556                         host_status = DID_BAD_TARGET;
1557                 else if (!(state_flags & SF_SENT_CDB))
1558                         host_status = DID_ERROR;
1559                 else if (!(state_flags & SF_TRANSFERRED_DATA))
1560                         host_status = DID_ERROR;
1561                 else if (!(state_flags & SF_GOT_STATUS))
1562                         host_status = DID_ERROR;
1563                 else if (!(state_flags & SF_GOT_SENSE))
1564                         host_status = DID_ERROR;
1565                 break;
1566
1567         case CS_RESET:
1568                 host_status = DID_RESET;
1569                 break;
1570
1571         case CS_ABORTED:
1572                 host_status = DID_ABORT;
1573                 break;
1574
1575         case CS_TIMEOUT:
1576                 host_status = DID_TIME_OUT;
1577                 break;
1578
1579         case CS_DATA_OVERRUN:
1580                 dprintk(2, "Data overrun 0x%x\n", residual_length);
1581                 dprintk(2, "qla1280_isr: response packet data\n");
1582                 qla1280_dump_buffer(2, (char *)sts, RESPONSE_ENTRY_SIZE);
1583                 host_status = DID_ERROR;
1584                 break;
1585
1586         case CS_DATA_UNDERRUN:
1587                 if ((cp->request_bufflen - residual_length) <
1588                     cp->underflow) {
1589                         printk(KERN_WARNING
1590                                "scsi: Underflow detected - retrying "
1591                                "command.\n");
1592                         host_status = DID_ERROR;
1593                 } else
1594                         host_status = DID_OK;
1595                 break;
1596
1597         default:
1598                 host_status = DID_ERROR;
1599                 break;
1600         }
1601
1602 #if DEBUG_QLA1280_INTR
1603         dprintk(1, "qla1280 ISP status: host status (%s) scsi status %x\n",
1604                 reason[host_status], scsi_status);
1605 #endif
1606
1607         LEAVE("qla1280_return_status");
1608
1609         return (scsi_status & 0xff) | (host_status << 16);
1610 }
1611
1612 /****************************************************************************/
1613 /*                QLogic ISP1280 Hardware Support Functions.                */
1614 /****************************************************************************/
1615
1616  /*
1617   * qla2100_enable_intrs
1618   * qla2100_disable_intrs
1619   *
1620   * Input:
1621   *      ha = adapter block pointer.
1622   *
1623   * Returns:
1624   *      None
1625   */
1626 static inline void
1627 qla1280_enable_intrs(struct scsi_qla_host *ha)
1628 {
1629         struct device_reg __iomem *reg;
1630
1631         reg = ha->iobase;
1632         /* enable risc and host interrupts */
1633         WRT_REG_WORD(&reg->ictrl, (ISP_EN_INT | ISP_EN_RISC));
1634         RD_REG_WORD(&reg->ictrl);       /* PCI Posted Write flush */
1635 }
1636
1637 static inline void
1638 qla1280_disable_intrs(struct scsi_qla_host *ha)
1639 {
1640         struct device_reg __iomem *reg;
1641
1642         reg = ha->iobase;
1643         /* disable risc and host interrupts */
1644         WRT_REG_WORD(&reg->ictrl, 0);
1645         RD_REG_WORD(&reg->ictrl);       /* PCI Posted Write flush */
1646 }
1647
1648 /*
1649  * qla1280_initialize_adapter
1650  *      Initialize board.
1651  *
1652  * Input:
1653  *      ha = adapter block pointer.
1654  *
1655  * Returns:
1656  *      0 = success
1657  */
1658 static int __devinit
1659 qla1280_initialize_adapter(struct scsi_qla_host *ha)
1660 {
1661         struct device_reg __iomem *reg;
1662         int status;
1663         int bus;
1664 #if LINUX_VERSION_CODE > 0x020500
1665         unsigned long flags;
1666 #endif
1667
1668         ENTER("qla1280_initialize_adapter");
1669
1670         /* Clear adapter flags. */
1671         ha->flags.online = 0;
1672         ha->flags.disable_host_adapter = 0;
1673         ha->flags.reset_active = 0;
1674         ha->flags.abort_isp_active = 0;
1675
1676 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1677         if (ia64_platform_is("sn2")) {
1678                 printk(KERN_INFO "scsi(%li): Enabling SN2 PCI DMA "
1679                        "dual channel lockup workaround\n", ha->host_no);
1680                 ha->flags.use_pci_vchannel = 1;
1681                 driver_setup.no_nvram = 1;
1682         }
1683 #endif
1684
1685         /* TODO: implement support for the 1040 nvram format */
1686         if (IS_ISP1040(ha))
1687                 driver_setup.no_nvram = 1;
1688
1689         dprintk(1, "Configure PCI space for adapter...\n");
1690
1691         reg = ha->iobase;
1692
1693         /* Insure mailbox registers are free. */
1694         WRT_REG_WORD(&reg->semaphore, 0);
1695         WRT_REG_WORD(&reg->host_cmd, HC_CLR_RISC_INT);
1696         WRT_REG_WORD(&reg->host_cmd, HC_CLR_HOST_INT);
1697         RD_REG_WORD(&reg->host_cmd);
1698
1699         if (qla1280_read_nvram(ha)) {
1700                 dprintk(2, "qla1280_initialize_adapter: failed to read "
1701                         "NVRAM\n");
1702         }
1703
1704 #if LINUX_VERSION_CODE >= 0x020500
1705         /*
1706          * It's necessary to grab the spin here as qla1280_mailbox_command
1707          * needs to be able to drop the lock unconditionally to wait
1708          * for completion.
1709          * In 2.4 ->detect is called with the io_request_lock held.
1710          */
1711         spin_lock_irqsave(HOST_LOCK, flags);
1712 #endif
1713
1714         status = qla1280_load_firmware(ha);
1715         if (status) {
1716                 printk(KERN_ERR "scsi(%li): initialize: pci probe failed!\n",
1717                        ha->host_no);
1718                 goto out;
1719         }
1720
1721         /* Setup adapter based on NVRAM parameters. */
1722         dprintk(1, "scsi(%ld): Configure NVRAM parameters\n", ha->host_no);
1723         qla1280_nvram_config(ha);
1724
1725         if (ha->flags.disable_host_adapter) {
1726                 status = 1;
1727                 goto out;
1728         }
1729
1730         status = qla1280_init_rings(ha);
1731         if (status)
1732                 goto out;
1733
1734         /* Issue SCSI reset, if we can't reset twice then bus is dead */
1735         for (bus = 0; bus < ha->ports; bus++) {
1736                 if (!ha->bus_settings[bus].disable_scsi_reset &&
1737                     qla1280_bus_reset(ha, bus) &&
1738                     qla1280_bus_reset(ha, bus))
1739                         ha->bus_settings[bus].scsi_bus_dead = 1;
1740         }
1741
1742         ha->flags.online = 1;
1743  out:
1744 #if LINUX_VERSION_CODE >= 0x020500
1745         spin_unlock_irqrestore(HOST_LOCK, flags);
1746 #endif
1747         if (status)
1748                 dprintk(2, "qla1280_initialize_adapter: **** FAILED ****\n");
1749
1750         LEAVE("qla1280_initialize_adapter");
1751         return status;
1752 }
1753
1754
1755 /*
1756  * ISP Firmware Test
1757  *      Checks if present version of RISC firmware is older than
1758  *      driver firmware.
1759  *
1760  * Input:
1761  *      ha = adapter block pointer.
1762  *
1763  * Returns:
1764  *      0 = firmware does not need to be loaded.
1765  */
1766 static int
1767 qla1280_isp_firmware(struct scsi_qla_host *ha)
1768 {
1769         struct nvram *nv = (struct nvram *) ha->response_ring;
1770         int status = 0;         /* dg 2/27 always loads RISC */
1771         uint16_t mb[MAILBOX_REGISTER_COUNT];
1772
1773         ENTER("qla1280_isp_firmware");
1774
1775         dprintk(1, "scsi(%li): Determining if RISC is loaded\n", ha->host_no);
1776
1777         /* Bad NVRAM data, load RISC code. */
1778         if (!ha->nvram_valid) {
1779                 ha->flags.disable_risc_code_load = 0;
1780         } else
1781                 ha->flags.disable_risc_code_load =
1782                         nv->cntr_flags_1.disable_loading_risc_code;
1783
1784         if (ha->flags.disable_risc_code_load) {
1785                 dprintk(3, "qla1280_isp_firmware: Telling RISC to verify "
1786                         "checksum of loaded BIOS code.\n");
1787
1788                 /* Verify checksum of loaded RISC code. */
1789                 mb[0] = MBC_VERIFY_CHECKSUM;
1790                 /* mb[1] = ql12_risc_code_addr01; */
1791                 mb[1] = *ql1280_board_tbl[ha->devnum].fwstart;
1792
1793                 if (!(status =
1794                       qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]))) {
1795                         /* Start firmware execution. */
1796                         dprintk(3, "qla1280_isp_firmware: Startng F/W "
1797                                 "execution.\n");
1798
1799                         mb[0] = MBC_EXECUTE_FIRMWARE;
1800                         /* mb[1] = ql12_risc_code_addr01; */
1801                         mb[1] = *ql1280_board_tbl[ha->devnum].fwstart;
1802                         qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
1803                 } else
1804                         printk(KERN_INFO "qla1280: RISC checksum failed.\n");
1805         } else {
1806                 dprintk(1, "qla1280: NVRAM configured to load RISC load.\n");
1807                 status = 1;
1808         }
1809
1810         if (status)
1811                 dprintk(2, "qla1280_isp_firmware: **** Load RISC code ****\n");
1812
1813         LEAVE("qla1280_isp_firmware");
1814         return status;
1815 }
1816
1817 /*
1818  * Chip diagnostics
1819  *      Test chip for proper operation.
1820  *
1821  * Input:
1822  *      ha = adapter block pointer.
1823  *
1824  * Returns:
1825  *      0 = success.
1826  */
1827 static int
1828 qla1280_chip_diag(struct scsi_qla_host *ha)
1829 {
1830         uint16_t mb[MAILBOX_REGISTER_COUNT];
1831         struct device_reg __iomem *reg = ha->iobase;
1832         int status = 0;
1833         int cnt;
1834         uint16_t data;
1835         dprintk(3, "qla1280_chip_diag: testing device at 0x%p \n", &reg->id_l);
1836
1837         dprintk(1, "scsi(%ld): Verifying chip\n", ha->host_no);
1838
1839         /* Soft reset chip and wait for it to finish. */
1840         WRT_REG_WORD(&reg->ictrl, ISP_RESET);
1841
1842         /*
1843          * We can't do a traditional PCI write flush here by reading
1844          * back the register. The card will not respond once the reset
1845          * is in action and we end up with a machine check exception
1846          * instead. Nothing to do but wait and hope for the best.
1847          * A portable pci_write_flush(pdev) call would be very useful here.
1848          */
1849         udelay(20);
1850         data = qla1280_debounce_register(&reg->ictrl);
1851         /*
1852          * Yet another QLogic gem ;-(
1853          */
1854         for (cnt = 1000000; cnt && data & ISP_RESET; cnt--) {
1855                 udelay(5);
1856                 data = RD_REG_WORD(&reg->ictrl);
1857         }
1858
1859         if (!cnt)
1860                 goto fail;
1861
1862         /* Reset register cleared by chip reset. */
1863         dprintk(3, "qla1280_chip_diag: reset register cleared by chip reset\n");
1864
1865         WRT_REG_WORD(&reg->cfg_1, 0);
1866
1867         /* Reset RISC and disable BIOS which
1868            allows RISC to execute out of RAM. */
1869         WRT_REG_WORD(&reg->host_cmd, HC_RESET_RISC |
1870                      HC_RELEASE_RISC | HC_DISABLE_BIOS);
1871
1872         RD_REG_WORD(&reg->id_l);        /* Flush PCI write */
1873         data = qla1280_debounce_register(&reg->mailbox0);
1874
1875         /*
1876          * I *LOVE* this code!
1877          */
1878         for (cnt = 1000000; cnt && data == MBS_BUSY; cnt--) {
1879                 udelay(5);
1880                 data = RD_REG_WORD(&reg->mailbox0);
1881         }
1882
1883         if (!cnt)
1884                 goto fail;
1885
1886         /* Check product ID of chip */
1887         dprintk(3, "qla1280_chip_diag: Checking product ID of chip\n");
1888
1889         if (RD_REG_WORD(&reg->mailbox1) != PROD_ID_1 ||
1890             (RD_REG_WORD(&reg->mailbox2) != PROD_ID_2 &&
1891              RD_REG_WORD(&reg->mailbox2) != PROD_ID_2a) ||
1892             RD_REG_WORD(&reg->mailbox3) != PROD_ID_3 ||
1893             RD_REG_WORD(&reg->mailbox4) != PROD_ID_4) {
1894                 printk(KERN_INFO "qla1280: Wrong product ID = "
1895                        "0x%x,0x%x,0x%x,0x%x\n",
1896                        RD_REG_WORD(&reg->mailbox1),
1897                        RD_REG_WORD(&reg->mailbox2),
1898                        RD_REG_WORD(&reg->mailbox3),
1899                        RD_REG_WORD(&reg->mailbox4));
1900                 goto fail;
1901         }
1902
1903         /*
1904          * Enable ints early!!!
1905          */
1906         qla1280_enable_intrs(ha);
1907
1908         dprintk(1, "qla1280_chip_diag: Checking mailboxes of chip\n");
1909         /* Wrap Incoming Mailboxes Test. */
1910         mb[0] = MBC_MAILBOX_REGISTER_TEST;
1911         mb[1] = 0xAAAA;
1912         mb[2] = 0x5555;
1913         mb[3] = 0xAA55;
1914         mb[4] = 0x55AA;
1915         mb[5] = 0xA5A5;
1916         mb[6] = 0x5A5A;
1917         mb[7] = 0x2525;
1918
1919         status = qla1280_mailbox_command(ha, 0xff, mb);
1920         if (status)
1921                 goto fail;
1922
1923         if (mb[1] != 0xAAAA || mb[2] != 0x5555 || mb[3] != 0xAA55 ||
1924             mb[4] != 0x55AA || mb[5] != 0xA5A5 || mb[6] != 0x5A5A ||
1925             mb[7] != 0x2525) {
1926                 printk(KERN_INFO "qla1280: Failed mbox check\n");
1927                 goto fail;
1928         }
1929
1930         dprintk(3, "qla1280_chip_diag: exiting normally\n");
1931         return 0;
1932  fail:
1933         dprintk(2, "qla1280_chip_diag: **** FAILED ****\n");
1934         return status;
1935 }
1936
1937 static int
1938 qla1280_load_firmware_pio(struct scsi_qla_host *ha)
1939 {
1940         uint16_t risc_address, *risc_code_address, risc_code_size;
1941         uint16_t mb[MAILBOX_REGISTER_COUNT], i;
1942         int err;
1943
1944         /* Load RISC code. */
1945         risc_address = *ql1280_board_tbl[ha->devnum].fwstart;
1946         risc_code_address = ql1280_board_tbl[ha->devnum].fwcode;
1947         risc_code_size = *ql1280_board_tbl[ha->devnum].fwlen;
1948
1949         for (i = 0; i < risc_code_size; i++) {
1950                 mb[0] = MBC_WRITE_RAM_WORD;
1951                 mb[1] = risc_address + i;
1952                 mb[2] = risc_code_address[i];
1953
1954                 err = qla1280_mailbox_command(ha, BIT_0 | BIT_1 | BIT_2, mb);
1955                 if (err) {
1956                         printk(KERN_ERR "scsi(%li): Failed to load firmware\n",
1957                                         ha->host_no);
1958                         return err;
1959                 }
1960         }
1961
1962         return 0;
1963 }
1964
1965 #define DUMP_IT_BACK 0          /* for debug of RISC loading */
1966 static int
1967 qla1280_load_firmware_dma(struct scsi_qla_host *ha)
1968 {
1969         uint16_t risc_address, *risc_code_address, risc_code_size;
1970         uint16_t mb[MAILBOX_REGISTER_COUNT], cnt;
1971         int err = 0, num, i;
1972 #if DUMP_IT_BACK
1973         uint8_t *sp, *tbuf;
1974         dma_addr_t p_tbuf;
1975
1976         tbuf = pci_alloc_consistent(ha->pdev, 8000, &p_tbuf);
1977         if (!tbuf)
1978                 return -ENOMEM;
1979 #endif
1980
1981         /* Load RISC code. */
1982         risc_address = *ql1280_board_tbl[ha->devnum].fwstart;
1983         risc_code_address = ql1280_board_tbl[ha->devnum].fwcode;
1984         risc_code_size = *ql1280_board_tbl[ha->devnum].fwlen;
1985
1986         dprintk(1, "%s: DMA RISC code (%i) words\n",
1987                         __FUNCTION__, risc_code_size);
1988
1989         num = 0;
1990         while (risc_code_size > 0) {
1991                 int warn __attribute__((unused)) = 0;
1992
1993                 cnt = 2000 >> 1;
1994
1995                 if (cnt > risc_code_size)
1996                         cnt = risc_code_size;
1997
1998                 dprintk(2, "qla1280_setup_chip:  loading risc @ =(0x%p),"
1999                         "%d,%d(0x%x)\n",
2000                         risc_code_address, cnt, num, risc_address);
2001                 for(i = 0; i < cnt; i++)
2002                         ((uint16_t *)ha->request_ring)[i] =
2003                                 cpu_to_le16(risc_code_address[i]);
2004
2005                 mb[0] = MBC_LOAD_RAM;
2006                 mb[1] = risc_address;
2007                 mb[4] = cnt;
2008                 mb[3] = ha->request_dma & 0xffff;
2009                 mb[2] = (ha->request_dma >> 16) & 0xffff;
2010                 mb[7] = pci_dma_hi32(ha->request_dma) & 0xffff;
2011                 mb[6] = pci_dma_hi32(ha->request_dma) >> 16;
2012                 dprintk(2, "%s: op=%d  0x%p = 0x%4x,0x%4x,0x%4x,0x%4x\n",
2013                                 __FUNCTION__, mb[0],
2014                                 (void *)(long)ha->request_dma,
2015                                 mb[6], mb[7], mb[2], mb[3]);
2016                 err = qla1280_mailbox_command(ha, BIT_4 | BIT_3 | BIT_2 |
2017                                 BIT_1 | BIT_0, mb);
2018                 if (err) {
2019                         printk(KERN_ERR "scsi(%li): Failed to load partial "
2020                                "segment of f\n", ha->host_no);
2021                         goto out;
2022                 }
2023
2024 #if DUMP_IT_BACK
2025                 mb[0] = MBC_DUMP_RAM;
2026                 mb[1] = risc_address;
2027                 mb[4] = cnt;
2028                 mb[3] = p_tbuf & 0xffff;
2029                 mb[2] = (p_tbuf >> 16) & 0xffff;
2030                 mb[7] = pci_dma_hi32(p_tbuf) & 0xffff;
2031                 mb[6] = pci_dma_hi32(p_tbuf) >> 16;
2032
2033                 err = qla1280_mailbox_command(ha, BIT_4 | BIT_3 | BIT_2 |
2034                                 BIT_1 | BIT_0, mb);
2035                 if (err) {
2036                         printk(KERN_ERR
2037                                "Failed to dump partial segment of f/w\n");
2038                         goto out;
2039                 }
2040                 sp = (uint8_t *)ha->request_ring;
2041                 for (i = 0; i < (cnt << 1); i++) {
2042                         if (tbuf[i] != sp[i] && warn++ < 10) {
2043                                 printk(KERN_ERR "%s: FW compare error @ "
2044                                                 "byte(0x%x) loop#=%x\n",
2045                                                 __FUNCTION__, i, num);
2046                                 printk(KERN_ERR "%s: FWbyte=%x  "
2047                                                 "FWfromChip=%x\n",
2048                                                 __FUNCTION__, sp[i], tbuf[i]);
2049                                 /*break; */
2050                         }
2051                 }
2052 #endif
2053                 risc_address += cnt;
2054                 risc_code_size = risc_code_size - cnt;
2055                 risc_code_address = risc_code_address + cnt;
2056                 num++;
2057         }
2058
2059  out:
2060 #if DUMP_IT_BACK
2061         pci_free_consistent(ha->pdev, 8000, tbuf, p_tbuf);
2062 #endif
2063         return err;
2064 }
2065
2066 static int
2067 qla1280_start_firmware(struct scsi_qla_host *ha)
2068 {
2069         uint16_t mb[MAILBOX_REGISTER_COUNT];
2070         int err;
2071
2072         dprintk(1, "%s: Verifying checksum of loaded RISC code.\n",
2073                         __FUNCTION__);
2074
2075         /* Verify checksum of loaded RISC code. */
2076         mb[0] = MBC_VERIFY_CHECKSUM;
2077         /* mb[1] = ql12_risc_code_addr01; */
2078         mb[1] = *ql1280_board_tbl[ha->devnum].fwstart;
2079         err = qla1280_mailbox_command(ha, BIT_1 | BIT_0, mb);
2080         if (err) {
2081                 printk(KERN_ERR "scsi(%li): Failed checksum\n", ha->host_no);
2082                 return err;
2083         }
2084
2085         /* Start firmware execution. */
2086         dprintk(1, "%s: start firmware running.\n", __FUNCTION__);
2087         mb[0] = MBC_EXECUTE_FIRMWARE;
2088         mb[1] = *ql1280_board_tbl[ha->devnum].fwstart;
2089         err = qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
2090         if (err) {
2091                 printk(KERN_ERR "scsi(%li): Failed to start firmware\n",
2092                                 ha->host_no);
2093         }
2094
2095         return err;
2096 }
2097
2098 static int
2099 qla1280_load_firmware(struct scsi_qla_host *ha)
2100 {
2101         int err = -ENODEV;
2102
2103         /* If firmware needs to be loaded */
2104         if (!qla1280_isp_firmware(ha)) {
2105                 printk(KERN_ERR "scsi(%li): isp_firmware() failed!\n",
2106                                 ha->host_no);
2107                 goto out;
2108         }
2109
2110         err = qla1280_chip_diag(ha);
2111         if (err)
2112                 goto out;
2113         if (IS_ISP1040(ha))
2114                 err = qla1280_load_firmware_pio(ha);
2115         else
2116                 err = qla1280_load_firmware_dma(ha);
2117         if (err)
2118                 goto out;
2119         err = qla1280_start_firmware(ha);
2120  out:
2121         return err;
2122 }
2123
2124 /*
2125  * Initialize rings
2126  *
2127  * Input:
2128  *      ha                = adapter block pointer.
2129  *      ha->request_ring  = request ring virtual address
2130  *      ha->response_ring = response ring virtual address
2131  *      ha->request_dma   = request ring physical address
2132  *      ha->response_dma  = response ring physical address
2133  *
2134  * Returns:
2135  *      0 = success.
2136  */
2137 static int
2138 qla1280_init_rings(struct scsi_qla_host *ha)
2139 {
2140         uint16_t mb[MAILBOX_REGISTER_COUNT];
2141         int status = 0;
2142
2143         ENTER("qla1280_init_rings");
2144
2145         /* Clear outstanding commands array. */
2146         memset(ha->outstanding_cmds, 0,
2147                sizeof(struct srb *) * MAX_OUTSTANDING_COMMANDS);
2148
2149         /* Initialize request queue. */
2150         ha->request_ring_ptr = ha->request_ring;
2151         ha->req_ring_index = 0;
2152         ha->req_q_cnt = REQUEST_ENTRY_CNT;
2153         /* mb[0] = MBC_INIT_REQUEST_QUEUE; */
2154         mb[0] = MBC_INIT_REQUEST_QUEUE_A64;
2155         mb[1] = REQUEST_ENTRY_CNT;
2156         mb[3] = ha->request_dma & 0xffff;
2157         mb[2] = (ha->request_dma >> 16) & 0xffff;
2158         mb[4] = 0;
2159         mb[7] = pci_dma_hi32(ha->request_dma) & 0xffff;
2160         mb[6] = pci_dma_hi32(ha->request_dma) >> 16;
2161         if (!(status = qla1280_mailbox_command(ha, BIT_7 | BIT_6 | BIT_4 |
2162                                                BIT_3 | BIT_2 | BIT_1 | BIT_0,
2163                                                &mb[0]))) {
2164                 /* Initialize response queue. */
2165                 ha->response_ring_ptr = ha->response_ring;
2166                 ha->rsp_ring_index = 0;
2167                 /* mb[0] = MBC_INIT_RESPONSE_QUEUE; */
2168                 mb[0] = MBC_INIT_RESPONSE_QUEUE_A64;
2169                 mb[1] = RESPONSE_ENTRY_CNT;
2170                 mb[3] = ha->response_dma & 0xffff;
2171                 mb[2] = (ha->response_dma >> 16) & 0xffff;
2172                 mb[5] = 0;
2173                 mb[7] = pci_dma_hi32(ha->response_dma) & 0xffff;
2174                 mb[6] = pci_dma_hi32(ha->response_dma) >> 16;
2175                 status = qla1280_mailbox_command(ha, BIT_7 | BIT_6 | BIT_5 |
2176                                                  BIT_3 | BIT_2 | BIT_1 | BIT_0,
2177                                                  &mb[0]);
2178         }
2179
2180         if (status)
2181                 dprintk(2, "qla1280_init_rings: **** FAILED ****\n");
2182
2183         LEAVE("qla1280_init_rings");
2184         return status;
2185 }
2186
2187 static void
2188 qla1280_print_settings(struct nvram *nv)
2189 {
2190         dprintk(1, "qla1280 : initiator scsi id bus[0]=%d\n",
2191                 nv->bus[0].config_1.initiator_id);
2192         dprintk(1, "qla1280 : initiator scsi id bus[1]=%d\n",
2193                 nv->bus[1].config_1.initiator_id);
2194
2195         dprintk(1, "qla1280 : bus reset delay[0]=%d\n",
2196                 nv->bus[0].bus_reset_delay);
2197         dprintk(1, "qla1280 : bus reset delay[1]=%d\n",
2198                 nv->bus[1].bus_reset_delay);
2199
2200         dprintk(1, "qla1280 : retry count[0]=%d\n", nv->bus[0].retry_count);
2201         dprintk(1, "qla1280 : retry delay[0]=%d\n", nv->bus[0].retry_delay);
2202         dprintk(1, "qla1280 : retry count[1]=%d\n", nv->bus[1].retry_count);
2203         dprintk(1, "qla1280 : retry delay[1]=%d\n", nv->bus[1].retry_delay);
2204
2205         dprintk(1, "qla1280 : async data setup time[0]=%d\n",
2206                 nv->bus[0].config_2.async_data_setup_time);
2207         dprintk(1, "qla1280 : async data setup time[1]=%d\n",
2208                 nv->bus[1].config_2.async_data_setup_time);
2209
2210         dprintk(1, "qla1280 : req/ack active negation[0]=%d\n",
2211                 nv->bus[0].config_2.req_ack_active_negation);
2212         dprintk(1, "qla1280 : req/ack active negation[1]=%d\n",
2213                 nv->bus[1].config_2.req_ack_active_negation);
2214
2215         dprintk(1, "qla1280 : data line active negation[0]=%d\n",
2216                 nv->bus[0].config_2.data_line_active_negation);
2217         dprintk(1, "qla1280 : data line active negation[1]=%d\n",
2218                 nv->bus[1].config_2.data_line_active_negation);
2219
2220         dprintk(1, "qla1280 : disable loading risc code=%d\n",
2221                 nv->cntr_flags_1.disable_loading_risc_code);
2222
2223         dprintk(1, "qla1280 : enable 64bit addressing=%d\n",
2224                 nv->cntr_flags_1.enable_64bit_addressing);
2225
2226         dprintk(1, "qla1280 : selection timeout limit[0]=%d\n",
2227                 nv->bus[0].selection_timeout);
2228         dprintk(1, "qla1280 : selection timeout limit[1]=%d\n",
2229                 nv->bus[1].selection_timeout);
2230
2231         dprintk(1, "qla1280 : max queue depth[0]=%d\n",
2232                 nv->bus[0].max_queue_depth);
2233         dprintk(1, "qla1280 : max queue depth[1]=%d\n",
2234                 nv->bus[1].max_queue_depth);
2235 }
2236
2237 static void
2238 qla1280_set_target_defaults(struct scsi_qla_host *ha, int bus, int target)
2239 {
2240         struct nvram *nv = &ha->nvram;
2241
2242         nv->bus[bus].target[target].parameter.f.renegotiate_on_error = 1;
2243         nv->bus[bus].target[target].parameter.f.auto_request_sense = 1;
2244         nv->bus[bus].target[target].parameter.f.tag_queuing = 1;
2245         nv->bus[bus].target[target].parameter.f.enable_sync = 1;
2246 #if 1   /* Some SCSI Processors do not seem to like this */
2247         nv->bus[bus].target[target].parameter.f.enable_wide = 1;
2248 #endif
2249         nv->bus[bus].target[target].parameter.f.parity_checking = 1;
2250         nv->bus[bus].target[target].parameter.f.disconnect_allowed = 1;
2251         nv->bus[bus].target[target].execution_throttle =
2252                 nv->bus[bus].max_queue_depth - 1;
2253
2254         if (IS_ISP1x160(ha)) {
2255                 nv->bus[bus].target[target].flags.flags1x160.device_enable = 1;
2256                 nv->bus[bus].target[target].flags.flags1x160.sync_offset = 0x0e;
2257                 nv->bus[bus].target[target].sync_period = 9;
2258                 nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr = 1;
2259                 nv->bus[bus].target[target].ppr_1x160.flags.ppr_options = 2;
2260                 nv->bus[bus].target[target].ppr_1x160.flags.ppr_bus_width = 1;
2261         } else {
2262                 nv->bus[bus].target[target].flags.flags1x80.device_enable = 1;
2263                 nv->bus[bus].target[target].flags.flags1x80.sync_offset = 12;
2264                 nv->bus[bus].target[target].sync_period = 10;
2265         }
2266 }
2267
2268 static void
2269 qla1280_set_defaults(struct scsi_qla_host *ha)
2270 {
2271         struct nvram *nv = &ha->nvram;
2272         int bus, target;
2273
2274         dprintk(1, "Using defaults for NVRAM: \n");
2275         memset(nv, 0, sizeof(struct nvram));
2276
2277         /* nv->cntr_flags_1.disable_loading_risc_code = 1; */
2278         nv->firmware_feature.f.enable_fast_posting = 1;
2279         nv->firmware_feature.f.disable_synchronous_backoff = 1;
2280         nv->termination.f.scsi_bus_0_control = 3;
2281         nv->termination.f.scsi_bus_1_control = 3;
2282         nv->termination.f.auto_term_support = 1;
2283
2284         /*
2285          * Set default FIFO magic - What appropriate values would be here
2286          * is unknown. This is what I have found testing with 12160s.
2287          *
2288          * Now, I would love the magic decoder ring for this one, the
2289          * header file provided by QLogic seems to be bogus or incomplete
2290          * at best.
2291          */
2292         nv->isp_config.c = ISP_CFG1_BENAB|ISP_CFG1_F128;
2293         if (IS_ISP1x160(ha))
2294                 nv->isp_parameter = 0x01; /* fast memory enable */
2295
2296         for (bus = 0; bus < MAX_BUSES; bus++) {
2297                 nv->bus[bus].config_1.initiator_id = 7;
2298                 nv->bus[bus].config_2.req_ack_active_negation = 1;
2299                 nv->bus[bus].config_2.data_line_active_negation = 1;
2300                 nv->bus[bus].selection_timeout = 250;
2301                 nv->bus[bus].max_queue_depth = 256;
2302
2303                 if (IS_ISP1040(ha)) {
2304                         nv->bus[bus].bus_reset_delay = 3;
2305                         nv->bus[bus].config_2.async_data_setup_time = 6;
2306                         nv->bus[bus].retry_delay = 1;
2307                 } else {
2308                         nv->bus[bus].bus_reset_delay = 5;
2309                         nv->bus[bus].config_2.async_data_setup_time = 8;
2310                 }
2311
2312                 for (target = 0; target < MAX_TARGETS; target++)
2313                         qla1280_set_target_defaults(ha, bus, target);
2314         }
2315 }
2316
2317 static int
2318 qla1280_config_target(struct scsi_qla_host *ha, int bus, int target)
2319 {
2320         struct nvram *nv = &ha->nvram;
2321         uint16_t mb[MAILBOX_REGISTER_COUNT];
2322         int status, lun;
2323
2324         /* Set Target Parameters. */
2325         mb[0] = MBC_SET_TARGET_PARAMETERS;
2326         mb[1] = (uint16_t) (bus ? target | BIT_7 : target);
2327         mb[1] <<= 8;
2328
2329         /*
2330          * Do not enable wide, sync, and ppr for the initial
2331          * INQUIRY run. We enable this later if we determine
2332          * the target actually supports it.
2333          */
2334         nv->bus[bus].target[target].parameter.f.
2335                 auto_request_sense = 1;
2336         nv->bus[bus].target[target].parameter.f.
2337                 stop_queue_on_check = 0;
2338
2339         if (IS_ISP1x160(ha))
2340                 nv->bus[bus].target[target].ppr_1x160.
2341                         flags.enable_ppr = 0;
2342
2343         /*
2344          * No sync, wide, etc. while probing
2345          */
2346         mb[2] = (nv->bus[bus].target[target].parameter.c << 8) &
2347                 ~(TP_SYNC /*| TP_WIDE | TP_PPR*/);
2348
2349         if (IS_ISP1x160(ha))
2350                 mb[3] = nv->bus[bus].target[target].flags.flags1x160.sync_offset << 8;
2351         else
2352                 mb[3] = nv->bus[bus].target[target].flags.flags1x80.sync_offset << 8;
2353         mb[3] |= nv->bus[bus].target[target].sync_period;
2354
2355         status = qla1280_mailbox_command(ha, BIT_3 | BIT_2 | BIT_1 | BIT_0, &mb[0]);
2356
2357         /* Save Tag queuing enable flag. */
2358         mb[0] = BIT_0 << target;
2359         if (nv->bus[bus].target[target].parameter.f.tag_queuing)
2360                 ha->bus_settings[bus].qtag_enables |= mb[0];
2361
2362         /* Save Device enable flag. */
2363         if (IS_ISP1x160(ha)) {
2364                 if (nv->bus[bus].target[target].flags.flags1x160.device_enable)
2365                         ha->bus_settings[bus].device_enables |= mb[0];
2366                 ha->bus_settings[bus].lun_disables |= 0;
2367         } else {
2368                 if (nv->bus[bus].target[target].flags.flags1x80.device_enable)
2369                         ha->bus_settings[bus].device_enables |= mb[0];
2370                 /* Save LUN disable flag. */
2371                 if (nv->bus[bus].target[target].flags.flags1x80.lun_disable)
2372                         ha->bus_settings[bus].lun_disables |= mb[0];
2373         }
2374
2375         /* Set Device Queue Parameters. */
2376         for (lun = 0; lun < MAX_LUNS; lun++) {
2377                 mb[0] = MBC_SET_DEVICE_QUEUE;
2378                 mb[1] = (uint16_t)(bus ? target | BIT_7 : target);
2379                 mb[1] = mb[1] << 8 | lun;
2380                 mb[2] = nv->bus[bus].max_queue_depth;
2381                 mb[3] = nv->bus[bus].target[target].execution_throttle;
2382                 status |= qla1280_mailbox_command(ha, 0x0f, &mb[0]);
2383         }
2384
2385         return status;
2386 }
2387
2388 static int
2389 qla1280_config_bus(struct scsi_qla_host *ha, int bus)
2390 {
2391         struct nvram *nv = &ha->nvram;
2392         uint16_t mb[MAILBOX_REGISTER_COUNT];
2393         int target, status;
2394
2395         /* SCSI Reset Disable. */
2396         ha->bus_settings[bus].disable_scsi_reset =
2397                 nv->bus[bus].config_1.scsi_reset_disable;
2398
2399         /* Initiator ID. */
2400         ha->bus_settings[bus].id = nv->bus[bus].config_1.initiator_id;
2401         mb[0] = MBC_SET_INITIATOR_ID;
2402         mb[1] = bus ? ha->bus_settings[bus].id | BIT_7 :
2403                 ha->bus_settings[bus].id;
2404         status = qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
2405
2406         /* Reset Delay. */
2407         ha->bus_settings[bus].bus_reset_delay =
2408                 nv->bus[bus].bus_reset_delay;
2409
2410         /* Command queue depth per device. */
2411         ha->bus_settings[bus].hiwat = nv->bus[bus].max_queue_depth - 1;
2412
2413         /* Set target parameters. */
2414         for (target = 0; target < MAX_TARGETS; target++)
2415                 status |= qla1280_config_target(ha, bus, target);
2416
2417         return status;
2418 }
2419
2420 static int
2421 qla1280_nvram_config(struct scsi_qla_host *ha)
2422 {
2423         struct device_reg __iomem *reg = ha->iobase;
2424         struct nvram *nv = &ha->nvram;
2425         int bus, target, status = 0;
2426         uint16_t mb[MAILBOX_REGISTER_COUNT];
2427         uint16_t mask;
2428
2429         ENTER("qla1280_nvram_config");
2430
2431         if (ha->nvram_valid) {
2432                 /* Always force AUTO sense for LINUX SCSI */
2433                 for (bus = 0; bus < MAX_BUSES; bus++)
2434                         for (target = 0; target < MAX_TARGETS; target++) {
2435                                 nv->bus[bus].target[target].parameter.f.
2436                                         auto_request_sense = 1;
2437                         }
2438         } else {
2439                 qla1280_set_defaults(ha);
2440         }
2441
2442         qla1280_print_settings(nv);
2443
2444         /* Disable RISC load of firmware. */
2445         ha->flags.disable_risc_code_load =
2446                 nv->cntr_flags_1.disable_loading_risc_code;
2447
2448         if (IS_ISP1040(ha)) {
2449                 uint16_t hwrev, cfg1, cdma_conf, ddma_conf;
2450
2451                 hwrev = RD_REG_WORD(&reg->cfg_0) & ISP_CFG0_HWMSK;
2452
2453                 cfg1 = RD_REG_WORD(&reg->cfg_1);
2454                 cdma_conf = RD_REG_WORD(&reg->cdma_cfg);
2455                 ddma_conf = RD_REG_WORD(&reg->ddma_cfg);
2456
2457                 /* Busted fifo, says mjacob. */
2458                 if (hwrev == ISP_CFG0_1040A)
2459                         WRT_REG_WORD(&reg->cfg_1, cfg1 | ISP_CFG1_F64);
2460                 else
2461                         WRT_REG_WORD(&reg->cfg_1, cfg1 | ISP_CFG1_F64 | ISP_CFG1_BENAB);
2462
2463                 WRT_REG_WORD(&reg->cdma_cfg, cdma_conf | CDMA_CONF_BENAB);
2464                 WRT_REG_WORD(&reg->ddma_cfg, cdma_conf | DDMA_CONF_BENAB);
2465         } else {
2466                 /* Set ISP hardware DMA burst */
2467                 mb[0] = nv->isp_config.c;
2468                 /* Enable DMA arbitration on dual channel controllers */
2469                 if (ha->ports > 1)
2470                         mb[0] |= BIT_13;
2471                 WRT_REG_WORD(&reg->cfg_1, mb[0]);
2472
2473                 /* Set SCSI termination. */
2474                 WRT_REG_WORD(&reg->gpio_enable, (BIT_3 + BIT_2 + BIT_1 + BIT_0));
2475                 mb[0] = nv->termination.c & (BIT_3 + BIT_2 + BIT_1 + BIT_0);
2476                 WRT_REG_WORD(&reg->gpio_data, mb[0]);
2477         }
2478
2479         /* ISP parameter word. */
2480         mb[0] = MBC_SET_SYSTEM_PARAMETER;
2481         mb[1] = nv->isp_parameter;
2482         status |= qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
2483
2484         if (IS_ISP1x40(ha)) {
2485                 /* clock rate - for qla1240 and older, only */
2486                 mb[0] = MBC_SET_CLOCK_RATE;
2487                 mb[1] = 40;
2488                 status |= qla1280_mailbox_command(ha, BIT_1 | BIT_0, mb);
2489         }
2490
2491         /* Firmware feature word. */
2492         mb[0] = MBC_SET_FIRMWARE_FEATURES;
2493         mask = BIT_5 | BIT_1 | BIT_0;
2494         mb[1] = le16_to_cpu(nv->firmware_feature.w) & (mask);
2495 #if defined(CONFIG_IA64_GENERIC) || defined (CONFIG_IA64_SGI_SN2)
2496         if (ia64_platform_is("sn2")) {
2497                 printk(KERN_INFO "scsi(%li): Enabling SN2 PCI DMA "
2498                        "workaround\n", ha->host_no);
2499                 mb[1] |= BIT_9;
2500         }
2501 #endif
2502         status |= qla1280_mailbox_command(ha, mask, &mb[0]);
2503
2504         /* Retry count and delay. */
2505         mb[0] = MBC_SET_RETRY_COUNT;
2506         mb[1] = nv->bus[0].retry_count;
2507         mb[2] = nv->bus[0].retry_delay;
2508         mb[6] = nv->bus[1].retry_count;
2509         mb[7] = nv->bus[1].retry_delay;
2510         status |= qla1280_mailbox_command(ha, BIT_7 | BIT_6 | BIT_2 |
2511                                           BIT_1 | BIT_0, &mb[0]);
2512
2513         /* ASYNC data setup time. */
2514         mb[0] = MBC_SET_ASYNC_DATA_SETUP;
2515         mb[1] = nv->bus[0].config_2.async_data_setup_time;
2516         mb[2] = nv->bus[1].config_2.async_data_setup_time;
2517         status |= qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
2518
2519         /* Active negation states. */
2520         mb[0] = MBC_SET_ACTIVE_NEGATION;
2521         mb[1] = 0;
2522         if (nv->bus[0].config_2.req_ack_active_negation)
2523                 mb[1] |= BIT_5;
2524         if (nv->bus[0].config_2.data_line_active_negation)
2525                 mb[1] |= BIT_4;
2526         mb[2] = 0;
2527         if (nv->bus[1].config_2.req_ack_active_negation)
2528                 mb[2] |= BIT_5;
2529         if (nv->bus[1].config_2.data_line_active_negation)
2530                 mb[2] |= BIT_4;
2531         status |= qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
2532
2533         mb[0] = MBC_SET_DATA_OVERRUN_RECOVERY;
2534         mb[1] = 2;      /* Reset SCSI bus and return all outstanding IO */
2535         status |= qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
2536
2537         /* thingy */
2538         mb[0] = MBC_SET_PCI_CONTROL;
2539         mb[1] = 2;      /* Data DMA Channel Burst Enable */
2540         mb[2] = 2;      /* Command DMA Channel Burst Enable */
2541         status |= qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
2542
2543         mb[0] = MBC_SET_TAG_AGE_LIMIT;
2544         mb[1] = 8;
2545         status |= qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
2546
2547         /* Selection timeout. */
2548         mb[0] = MBC_SET_SELECTION_TIMEOUT;
2549         mb[1] = nv->bus[0].selection_timeout;
2550         mb[2] = nv->bus[1].selection_timeout;
2551         status |= qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
2552
2553         for (bus = 0; bus < ha->ports; bus++)
2554                 status |= qla1280_config_bus(ha, bus);
2555
2556         if (status)
2557                 dprintk(2, "qla1280_nvram_config: **** FAILED ****\n");
2558
2559         LEAVE("qla1280_nvram_config");
2560         return status;
2561 }
2562
2563 /*
2564  * Get NVRAM data word
2565  *      Calculates word position in NVRAM and calls request routine to
2566  *      get the word from NVRAM.
2567  *
2568  * Input:
2569  *      ha      = adapter block pointer.
2570  *      address = NVRAM word address.
2571  *
2572  * Returns:
2573  *      data word.
2574  */
2575 static uint16_t
2576 qla1280_get_nvram_word(struct scsi_qla_host *ha, uint32_t address)
2577 {
2578         uint32_t nv_cmd;
2579         uint16_t data;
2580
2581         nv_cmd = address << 16;
2582         nv_cmd |= NV_READ_OP;
2583
2584         data = le16_to_cpu(qla1280_nvram_request(ha, nv_cmd));
2585
2586         dprintk(8, "qla1280_get_nvram_word: exiting normally NVRAM data = "
2587                 "0x%x", data);
2588
2589         return data;
2590 }
2591
2592 /*
2593  * NVRAM request
2594  *      Sends read command to NVRAM and gets data from NVRAM.
2595  *
2596  * Input:
2597  *      ha     = adapter block pointer.
2598  *      nv_cmd = Bit 26     = start bit
2599  *               Bit 25, 24 = opcode
2600  *               Bit 23-16  = address
2601  *               Bit 15-0   = write data
2602  *
2603  * Returns:
2604  *      data word.
2605  */
2606 static uint16_t
2607 qla1280_nvram_request(struct scsi_qla_host *ha, uint32_t nv_cmd)
2608 {
2609         struct device_reg __iomem *reg = ha->iobase;
2610         int cnt;
2611         uint16_t data = 0;
2612         uint16_t reg_data;
2613
2614         /* Send command to NVRAM. */
2615
2616         nv_cmd <<= 5;
2617         for (cnt = 0; cnt < 11; cnt++) {
2618                 if (nv_cmd & BIT_31)
2619                         qla1280_nv_write(ha, NV_DATA_OUT);
2620                 else
2621                         qla1280_nv_write(ha, 0);
2622                 nv_cmd <<= 1;
2623         }
2624
2625         /* Read data from NVRAM. */
2626
2627         for (cnt = 0; cnt < 16; cnt++) {
2628                 WRT_REG_WORD(&reg->nvram, (NV_SELECT | NV_CLOCK));
2629                 RD_REG_WORD(&reg->id_l);        /* Flush PCI write */
2630                 NVRAM_DELAY();
2631                 data <<= 1;
2632                 reg_data = RD_REG_WORD(&reg->nvram);
2633                 if (reg_data & NV_DATA_IN)
2634                         data |= BIT_0;
2635                 WRT_REG_WORD(&reg->nvram, NV_SELECT);
2636                 RD_REG_WORD(&reg->id_l);        /* Flush PCI write */
2637                 NVRAM_DELAY();
2638         }
2639
2640         /* Deselect chip. */
2641
2642         WRT_REG_WORD(&reg->nvram, NV_DESELECT);
2643         RD_REG_WORD(&reg->id_l);        /* Flush PCI write */
2644         NVRAM_DELAY();
2645
2646         return data;
2647 }
2648
2649 static void
2650 qla1280_nv_write(struct scsi_qla_host *ha, uint16_t data)
2651 {
2652         struct device_reg __iomem *reg = ha->iobase;
2653
2654         WRT_REG_WORD(&reg->nvram, data | NV_SELECT);
2655         RD_REG_WORD(&reg->id_l);        /* Flush PCI write */
2656         NVRAM_DELAY();
2657         WRT_REG_WORD(&reg->nvram, data | NV_SELECT | NV_CLOCK);
2658         RD_REG_WORD(&reg->id_l);        /* Flush PCI write */
2659         NVRAM_DELAY();
2660         WRT_REG_WORD(&reg->nvram, data | NV_SELECT);
2661         RD_REG_WORD(&reg->id_l);        /* Flush PCI write */
2662         NVRAM_DELAY();
2663 }
2664
2665 /*
2666  * Mailbox Command
2667  *      Issue mailbox command and waits for completion.
2668  *
2669  * Input:
2670  *      ha = adapter block pointer.
2671  *      mr = mailbox registers to load.
2672  *      mb = data pointer for mailbox registers.
2673  *
2674  * Output:
2675  *      mb[MAILBOX_REGISTER_COUNT] = returned mailbox data.
2676  *
2677  * Returns:
2678  *      0 = success
2679  */
2680 static int
2681 qla1280_mailbox_command(struct scsi_qla_host *ha, uint8_t mr, uint16_t *mb)
2682 {
2683         struct device_reg __iomem *reg = ha->iobase;
2684 #if 0
2685         LIST_HEAD(done_q);
2686 #endif
2687         int status = 0;
2688         int cnt;
2689         uint16_t *optr, *iptr;
2690         uint16_t __iomem *mptr;
2691         uint16_t data;
2692         DECLARE_COMPLETION(wait);
2693         struct timer_list timer;
2694
2695         ENTER("qla1280_mailbox_command");
2696
2697         if (ha->mailbox_wait) {
2698                 printk(KERN_ERR "Warning mailbox wait already in use!\n");
2699         }
2700         ha->mailbox_wait = &wait;
2701
2702         /*
2703          * We really should start out by verifying that the mailbox is
2704          * available before starting sending the command data
2705          */
2706         /* Load mailbox registers. */
2707         mptr = (uint16_t __iomem *) &reg->mailbox0;
2708         iptr = mb;
2709         for (cnt = 0; cnt < MAILBOX_REGISTER_COUNT; cnt++) {
2710                 if (mr & BIT_0) {
2711                         WRT_REG_WORD(mptr, (*iptr));
2712                 }
2713
2714                 mr >>= 1;
2715                 mptr++;
2716                 iptr++;
2717         }
2718
2719         /* Issue set host interrupt command. */
2720
2721         /* set up a timer just in case we're really jammed */
2722         init_timer(&timer);
2723         timer.expires = jiffies + 20*HZ;
2724         timer.data = (unsigned long)ha;
2725         timer.function = qla1280_mailbox_timeout;
2726         add_timer(&timer);
2727
2728         spin_unlock_irq(HOST_LOCK);
2729         WRT_REG_WORD(&reg->host_cmd, HC_SET_HOST_INT);
2730         data = qla1280_debounce_register(&reg->istatus);
2731
2732         wait_for_completion(&wait);
2733         del_timer_sync(&timer);
2734
2735         spin_lock_irq(HOST_LOCK);
2736
2737         ha->mailbox_wait = NULL;
2738
2739         /* Check for mailbox command timeout. */
2740         if (ha->mailbox_out[0] != MBS_CMD_CMP) {
2741                 printk(KERN_WARNING "qla1280_mailbox_command: Command failed, "
2742                        "mailbox0 = 0x%04x, mailbox_out0 = 0x%04x, istatus = "
2743                        "0x%04x\n", 
2744                        mb[0], ha->mailbox_out[0], RD_REG_WORD(&reg->istatus));
2745                 printk(KERN_WARNING "m0 %04x, m1 %04x, m2 %04x, m3 %04x\n",
2746                        RD_REG_WORD(&reg->mailbox0), RD_REG_WORD(&reg->mailbox1),
2747                        RD_REG_WORD(&reg->mailbox2), RD_REG_WORD(&reg->mailbox3));
2748                 printk(KERN_WARNING "m4 %04x, m5 %04x, m6 %04x, m7 %04x\n",
2749                        RD_REG_WORD(&reg->mailbox4), RD_REG_WORD(&reg->mailbox5),
2750                        RD_REG_WORD(&reg->mailbox6), RD_REG_WORD(&reg->mailbox7));
2751                 status = 1;
2752         }
2753
2754         /* Load return mailbox registers. */
2755         optr = mb;
2756         iptr = (uint16_t *) &ha->mailbox_out[0];
2757         mr = MAILBOX_REGISTER_COUNT;
2758         memcpy(optr, iptr, MAILBOX_REGISTER_COUNT * sizeof(uint16_t));
2759
2760 #if 0
2761         /* Go check for any response interrupts pending. */
2762         qla1280_isr(ha, &done_q);
2763 #endif
2764
2765         if (ha->flags.reset_marker)
2766                 qla1280_rst_aen(ha);
2767
2768 #if 0
2769         if (!list_empty(&done_q))
2770                 qla1280_done(ha, &done_q);
2771 #endif
2772
2773         if (status)
2774                 dprintk(2, "qla1280_mailbox_command: **** FAILED, mailbox0 = "
2775                         "0x%x ****\n", mb[0]);
2776
2777         LEAVE("qla1280_mailbox_command");
2778         return status;
2779 }
2780
2781 /*
2782  * qla1280_poll
2783  *      Polls ISP for interrupts.
2784  *
2785  * Input:
2786  *      ha = adapter block pointer.
2787  */
2788 static void
2789 qla1280_poll(struct scsi_qla_host *ha)
2790 {
2791         struct device_reg __iomem *reg = ha->iobase;
2792         uint16_t data;
2793         LIST_HEAD(done_q);
2794
2795         /* ENTER("qla1280_poll"); */
2796
2797         /* Check for pending interrupts. */
2798         data = RD_REG_WORD(&reg->istatus);
2799         if (data & RISC_INT)
2800                 qla1280_isr(ha, &done_q);
2801
2802         if (!ha->mailbox_wait) {
2803                 if (ha->flags.reset_marker)
2804                         qla1280_rst_aen(ha);
2805         }
2806
2807         if (!list_empty(&done_q))
2808                 qla1280_done(ha);
2809
2810         /* LEAVE("qla1280_poll"); */
2811 }
2812
2813 /*
2814  * qla1280_bus_reset
2815  *      Issue SCSI bus reset.
2816  *
2817  * Input:
2818  *      ha  = adapter block pointer.
2819  *      bus = SCSI bus number.
2820  *
2821  * Returns:
2822  *      0 = success
2823  */
2824 static int
2825 qla1280_bus_reset(struct scsi_qla_host *ha, int bus)
2826 {
2827         uint16_t mb[MAILBOX_REGISTER_COUNT];
2828         uint16_t reset_delay;
2829         int status;
2830
2831         dprintk(3, "qla1280_bus_reset: entered\n");
2832
2833         if (qla1280_verbose)
2834                 printk(KERN_INFO "scsi(%li:%i): Resetting SCSI BUS\n",
2835                        ha->host_no, bus);
2836
2837         reset_delay = ha->bus_settings[bus].bus_reset_delay;
2838         mb[0] = MBC_BUS_RESET;
2839         mb[1] = reset_delay;
2840         mb[2] = (uint16_t) bus;
2841         status = qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
2842
2843         if (status) {
2844                 if (ha->bus_settings[bus].failed_reset_count > 2)
2845                         ha->bus_settings[bus].scsi_bus_dead = 1;
2846                 ha->bus_settings[bus].failed_reset_count++;
2847         } else {
2848                 spin_unlock_irq(HOST_LOCK);
2849                 ssleep(reset_delay);
2850                 spin_lock_irq(HOST_LOCK);
2851
2852                 ha->bus_settings[bus].scsi_bus_dead = 0;
2853                 ha->bus_settings[bus].failed_reset_count = 0;
2854                 ha->bus_settings[bus].reset_marker = 0;
2855                 /* Issue marker command. */
2856                 qla1280_marker(ha, bus, 0, 0, MK_SYNC_ALL);
2857         }
2858
2859         /*
2860          * We should probably call qla1280_set_target_parameters()
2861          * here as well for all devices on the bus.
2862          */
2863
2864         if (status)
2865                 dprintk(2, "qla1280_bus_reset: **** FAILED ****\n");
2866         else
2867                 dprintk(3, "qla1280_bus_reset: exiting normally\n");
2868
2869         return status;
2870 }
2871
2872 /*
2873  * qla1280_device_reset
2874  *      Issue bus device reset message to the target.
2875  *
2876  * Input:
2877  *      ha      = adapter block pointer.
2878  *      bus     = SCSI BUS number.
2879  *      target  = SCSI ID.
2880  *
2881  * Returns:
2882  *      0 = success
2883  */
2884 static int
2885 qla1280_device_reset(struct scsi_qla_host *ha, int bus, int target)
2886 {
2887         uint16_t mb[MAILBOX_REGISTER_COUNT];
2888         int status;
2889
2890         ENTER("qla1280_device_reset");
2891
2892         mb[0] = MBC_ABORT_TARGET;
2893         mb[1] = (bus ? (target | BIT_7) : target) << 8;
2894         mb[2] = 1;
2895         status = qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
2896
2897         /* Issue marker command. */
2898         qla1280_marker(ha, bus, target, 0, MK_SYNC_ID);
2899
2900         if (status)
2901                 dprintk(2, "qla1280_device_reset: **** FAILED ****\n");
2902
2903         LEAVE("qla1280_device_reset");
2904         return status;
2905 }
2906
2907 /*
2908  * qla1280_abort_device
2909  *      Issue an abort message to the device
2910  *
2911  * Input:
2912  *      ha     = adapter block pointer.
2913  *      bus    = SCSI BUS.
2914  *      target = SCSI ID.
2915  *      lun    = SCSI LUN.
2916  *
2917  * Returns:
2918  *      0 = success
2919  */
2920 static int
2921 qla1280_abort_device(struct scsi_qla_host *ha, int bus, int target, int lun)
2922 {
2923         uint16_t mb[MAILBOX_REGISTER_COUNT];
2924         int status;
2925
2926         ENTER("qla1280_abort_device");
2927
2928         mb[0] = MBC_ABORT_DEVICE;
2929         mb[1] = (bus ? target | BIT_7 : target) << 8 | lun;
2930         status = qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
2931
2932         /* Issue marker command. */
2933         qla1280_marker(ha, bus, target, lun, MK_SYNC_ID_LUN);
2934
2935         if (status)
2936                 dprintk(2, "qla1280_abort_device: **** FAILED ****\n");
2937
2938         LEAVE("qla1280_abort_device");
2939         return status;
2940 }
2941
2942 /*
2943  * qla1280_abort_command
2944  *      Abort command aborts a specified IOCB.
2945  *
2946  * Input:
2947  *      ha = adapter block pointer.
2948  *      sp = SB structure pointer.
2949  *
2950  * Returns:
2951  *      0 = success
2952  */
2953 static int
2954 qla1280_abort_command(struct scsi_qla_host *ha, struct srb * sp, int handle)
2955 {
2956         uint16_t mb[MAILBOX_REGISTER_COUNT];
2957         unsigned int bus, target, lun;
2958         int status;
2959
2960         ENTER("qla1280_abort_command");
2961
2962         bus = SCSI_BUS_32(sp->cmd);
2963         target = SCSI_TCN_32(sp->cmd);
2964         lun = SCSI_LUN_32(sp->cmd);
2965
2966         sp->flags |= SRB_ABORT_PENDING;
2967
2968         mb[0] = MBC_ABORT_COMMAND;
2969         mb[1] = (bus ? target | BIT_7 : target) << 8 | lun;
2970         mb[2] = handle >> 16;
2971         mb[3] = handle & 0xffff;
2972         status = qla1280_mailbox_command(ha, 0x0f, &mb[0]);
2973
2974         if (status) {
2975                 dprintk(2, "qla1280_abort_command: **** FAILED ****\n");
2976                 sp->flags &= ~SRB_ABORT_PENDING;
2977         }
2978
2979
2980         LEAVE("qla1280_abort_command");
2981         return status;
2982 }
2983
2984 /*
2985  * qla1280_reset_adapter
2986  *      Reset adapter.
2987  *
2988  * Input:
2989  *      ha = adapter block pointer.
2990  */
2991 static void
2992 qla1280_reset_adapter(struct scsi_qla_host *ha)
2993 {
2994         struct device_reg __iomem *reg = ha->iobase;
2995
2996         ENTER("qla1280_reset_adapter");
2997
2998         /* Disable ISP chip */
2999         ha->flags.online = 0;
3000         WRT_REG_WORD(&reg->ictrl, ISP_RESET);
3001         WRT_REG_WORD(&reg->host_cmd,
3002                      HC_RESET_RISC | HC_RELEASE_RISC | HC_DISABLE_BIOS);
3003         RD_REG_WORD(&reg->id_l);        /* Flush PCI write */
3004
3005         LEAVE("qla1280_reset_adapter");
3006 }
3007
3008 /*
3009  *  Issue marker command.
3010  *      Function issues marker IOCB.
3011  *
3012  * Input:
3013  *      ha   = adapter block pointer.
3014  *      bus  = SCSI BUS number
3015  *      id   = SCSI ID
3016  *      lun  = SCSI LUN
3017  *      type = marker modifier
3018  */
3019 static void
3020 qla1280_marker(struct scsi_qla_host *ha, int bus, int id, int lun, u8 type)
3021 {
3022         struct mrk_entry *pkt;
3023
3024         ENTER("qla1280_marker");
3025
3026         /* Get request packet. */
3027         if ((pkt = (struct mrk_entry *) qla1280_req_pkt(ha))) {
3028                 pkt->entry_type = MARKER_TYPE;
3029                 pkt->lun = (uint8_t) lun;
3030                 pkt->target = (uint8_t) (bus ? (id | BIT_7) : id);
3031                 pkt->modifier = type;
3032                 pkt->entry_status = 0;
3033
3034                 /* Issue command to ISP */
3035                 qla1280_isp_cmd(ha);
3036         }
3037
3038         LEAVE("qla1280_marker");
3039 }
3040
3041
3042 /*
3043  * qla1280_64bit_start_scsi
3044  *      The start SCSI is responsible for building request packets on
3045  *      request ring and modifying ISP input pointer.
3046  *
3047  * Input:
3048  *      ha = adapter block pointer.
3049  *      sp = SB structure pointer.
3050  *
3051  * Returns:
3052  *      0 = success, was able to issue command.
3053  */
3054 #ifdef QLA_64BIT_PTR
3055 static int
3056 qla1280_64bit_start_scsi(struct scsi_qla_host *ha, struct srb * sp)
3057 {
3058         struct device_reg __iomem *reg = ha->iobase;
3059         struct scsi_cmnd *cmd = sp->cmd;
3060         cmd_a64_entry_t *pkt;
3061         struct scatterlist *sg = NULL;
3062         u32 *dword_ptr;
3063         dma_addr_t dma_handle;
3064         int status = 0;
3065         int cnt;
3066         int req_cnt;
3067         u16 seg_cnt;
3068         u8 dir;
3069
3070         ENTER("qla1280_64bit_start_scsi:");
3071
3072         /* Calculate number of entries and segments required. */
3073         req_cnt = 1;
3074         if (cmd->use_sg) {
3075                 sg = (struct scatterlist *) cmd->request_buffer;
3076                 seg_cnt = pci_map_sg(ha->pdev, sg, cmd->use_sg,
3077                                      cmd->sc_data_direction);
3078
3079                 if (seg_cnt > 2) {
3080                         req_cnt += (seg_cnt - 2) / 5;
3081                         if ((seg_cnt - 2) % 5)
3082                                 req_cnt++;
3083                 }
3084         } else if (cmd->request_bufflen) {      /* If data transfer. */
3085                 seg_cnt = 1;
3086         } else {
3087                 seg_cnt = 0;
3088         }
3089
3090         if ((req_cnt + 2) >= ha->req_q_cnt) {
3091                 /* Calculate number of free request entries. */
3092                 cnt = RD_REG_WORD(&reg->mailbox4);
3093                 if (ha->req_ring_index < cnt)
3094                         ha->req_q_cnt = cnt - ha->req_ring_index;
3095                 else
3096                         ha->req_q_cnt =
3097                                 REQUEST_ENTRY_CNT - (ha->req_ring_index - cnt);
3098         }
3099
3100         /* If room for request in request ring. */
3101         if ((req_cnt + 2) >= ha->req_q_cnt) {
3102                 status = 1;
3103                 dprintk(2, "qla1280_64bit_start_scsi: in-ptr=0x%x  req_q_cnt="
3104                         "0x%xreq_cnt=0x%x", ha->req_ring_index, ha->req_q_cnt,
3105                         req_cnt);
3106                 goto out;
3107         }
3108
3109         /* Check for room in outstanding command list. */
3110         for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS &&
3111                      ha->outstanding_cmds[cnt] != 0; cnt++);
3112
3113         if (cnt >= MAX_OUTSTANDING_COMMANDS) {
3114                 status = 1;
3115                 dprintk(2, "qla1280_64bit_start_scsi: NO ROOM IN "
3116                         "OUTSTANDING ARRAY, req_q_cnt=0x%x", ha->req_q_cnt);
3117                 goto out;
3118         }
3119
3120         ha->outstanding_cmds[cnt] = sp;
3121         ha->req_q_cnt -= req_cnt;
3122         CMD_HANDLE(sp->cmd) = (unsigned char *)(unsigned long)(cnt + 1);
3123
3124         dprintk(2, "64bit_start: cmd=%p sp=%p CDB=%xm, handle %lx\n", cmd, sp,
3125                 cmd->cmnd[0], (long)CMD_HANDLE(sp->cmd));
3126         dprintk(2, "             bus %i, target %i, lun %i\n",
3127                 SCSI_BUS_32(cmd), SCSI_TCN_32(cmd), SCSI_LUN_32(cmd));
3128         qla1280_dump_buffer(2, cmd->cmnd, MAX_COMMAND_SIZE);
3129
3130         /*
3131          * Build command packet.
3132          */
3133         pkt = (cmd_a64_entry_t *) ha->request_ring_ptr;
3134
3135         pkt->entry_type = COMMAND_A64_TYPE;
3136         pkt->entry_count = (uint8_t) req_cnt;
3137         pkt->sys_define = (uint8_t) ha->req_ring_index;
3138         pkt->entry_status = 0;
3139         pkt->handle = cpu_to_le32(cnt);
3140
3141         /* Zero out remaining portion of packet. */
3142         memset(((char *)pkt + 8), 0, (REQUEST_ENTRY_SIZE - 8));
3143
3144         /* Set ISP command timeout. */
3145         pkt->timeout = cpu_to_le16(30);
3146
3147         /* Set device target ID and LUN */
3148         pkt->lun = SCSI_LUN_32(cmd);
3149         pkt->target = SCSI_BUS_32(cmd) ?
3150                 (SCSI_TCN_32(cmd) | BIT_7) : SCSI_TCN_32(cmd);
3151
3152         /* Enable simple tag queuing if device supports it. */
3153         if (DEV_SIMPLE_TAGS(cmd->device))
3154                 pkt->control_flags |= cpu_to_le16(BIT_3);
3155
3156         /* Load SCSI command packet. */
3157         pkt->cdb_len = cpu_to_le16(CMD_CDBLEN(cmd));
3158         memcpy(pkt->scsi_cdb, &(CMD_CDBP(cmd)), CMD_CDBLEN(cmd));
3159         /* dprintk(1, "Build packet for command[0]=0x%x\n",pkt->scsi_cdb[0]); */
3160
3161         /* Set transfer direction. */
3162         dir = qla1280_data_direction(cmd);
3163         pkt->control_flags |= cpu_to_le16(dir);
3164
3165         /* Set total data segment count. */
3166         pkt->dseg_count = cpu_to_le16(seg_cnt);
3167
3168         /*
3169          * Load data segments.
3170          */
3171         if (seg_cnt) {  /* If data transfer. */
3172                 /* Setup packet address segment pointer. */
3173                 dword_ptr = (u32 *)&pkt->dseg_0_address;
3174
3175                 if (cmd->use_sg) {      /* If scatter gather */
3176                         /* Load command entry data segments. */
3177                         for (cnt = 0; cnt < 2 && seg_cnt; cnt++, seg_cnt--) {
3178                                 dma_handle = sg_dma_address(sg);
3179 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
3180                                 if (ha->flags.use_pci_vchannel)
3181                                         sn_pci_set_vchan(ha->pdev,
3182                                                         (unsigned long *)&dma_handle,
3183                                                          SCSI_BUS_32(cmd));
3184 #endif
3185                                 *dword_ptr++ =
3186                                         cpu_to_le32(pci_dma_lo32(dma_handle));
3187                                 *dword_ptr++ =
3188                                         cpu_to_le32(pci_dma_hi32(dma_handle));
3189                                 *dword_ptr++ = cpu_to_le32(sg_dma_len(sg));
3190                                 sg++;
3191                                 dprintk(3, "S/G Segment phys_addr=%x %x, len=0x%x\n",
3192                                         cpu_to_le32(pci_dma_hi32(dma_handle)),
3193                                         cpu_to_le32(pci_dma_lo32(dma_handle)),
3194                                         cpu_to_le32(sg_dma_len(sg)));
3195                         }
3196                         dprintk(5, "qla1280_64bit_start_scsi: Scatter/gather "
3197                                 "command packet data - b %i, t %i, l %i \n",
3198                                 SCSI_BUS_32(cmd), SCSI_TCN_32(cmd),
3199                                 SCSI_LUN_32(cmd));
3200                         qla1280_dump_buffer(5, (char *)pkt,
3201                                             REQUEST_ENTRY_SIZE);
3202
3203                         /*
3204                          * Build continuation packets.
3205                          */
3206                         dprintk(3, "S/G Building Continuation...seg_cnt=0x%x "
3207                                 "remains\n", seg_cnt);
3208
3209                         while (seg_cnt > 0) {
3210                                 /* Adjust ring index. */
3211                                 ha->req_ring_index++;
3212                                 if (ha->req_ring_index == REQUEST_ENTRY_CNT) {
3213                                         ha->req_ring_index = 0;
3214                                         ha->request_ring_ptr =
3215                                                 ha->request_ring;
3216                                 } else
3217                                                 ha->request_ring_ptr++;
3218
3219                                 pkt = (cmd_a64_entry_t *)ha->request_ring_ptr;
3220
3221                                 /* Zero out packet. */
3222                                 memset(pkt, 0, REQUEST_ENTRY_SIZE);
3223
3224                                 /* Load packet defaults. */
3225                                 ((struct cont_a64_entry *) pkt)->entry_type =
3226                                         CONTINUE_A64_TYPE;
3227                                 ((struct cont_a64_entry *) pkt)->entry_count = 1;
3228                                 ((struct cont_a64_entry *) pkt)->sys_define =
3229                                         (uint8_t)ha->req_ring_index;
3230                                 /* Setup packet address segment pointer. */
3231                                 dword_ptr =
3232                                         (u32 *)&((struct cont_a64_entry *) pkt)->dseg_0_address;
3233
3234                                 /* Load continuation entry data segments. */
3235                                 for (cnt = 0; cnt < 5 && seg_cnt;
3236                                      cnt++, seg_cnt--) {
3237                                         dma_handle = sg_dma_address(sg);
3238 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
3239                                 if (ha->flags.use_pci_vchannel)
3240                                         sn_pci_set_vchan(ha->pdev, 
3241                                                         (unsigned long *)&dma_handle,
3242                                                          SCSI_BUS_32(cmd));
3243 #endif
3244                                         *dword_ptr++ =
3245                                                 cpu_to_le32(pci_dma_lo32(dma_handle));
3246                                         *dword_ptr++ =
3247                                                 cpu_to_le32(pci_dma_hi32(dma_handle));
3248                                         *dword_ptr++ =
3249                                                 cpu_to_le32(sg_dma_len(sg));
3250                                         dprintk(3, "S/G Segment Cont. phys_addr=%x %x, len=0x%x\n",
3251                                                 cpu_to_le32(pci_dma_hi32(dma_handle)),
3252                                                 cpu_to_le32(pci_dma_lo32(dma_handle)),
3253                                                 cpu_to_le32(sg_dma_len(sg)));
3254                                         sg++;
3255                                 }
3256                                 dprintk(5, "qla1280_64bit_start_scsi: "
3257                                         "continuation packet data - b %i, t "
3258                                         "%i, l %i \n", SCSI_BUS_32(cmd),
3259                                         SCSI_TCN_32(cmd), SCSI_LUN_32(cmd));
3260                                 qla1280_dump_buffer(5, (char *)pkt,
3261                                                     REQUEST_ENTRY_SIZE);
3262                         }
3263                 } else {        /* No scatter gather data transfer */
3264                         dma_handle = pci_map_single(ha->pdev,
3265                                         cmd->request_buffer,
3266                                         cmd->request_bufflen,
3267                                         cmd->sc_data_direction);
3268
3269                         sp->saved_dma_handle = dma_handle;
3270 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
3271                         if (ha->flags.use_pci_vchannel)
3272                                 sn_pci_set_vchan(ha->pdev, 
3273                                                 (unsigned long *)&dma_handle,
3274                                                  SCSI_BUS_32(cmd));
3275 #endif
3276                         *dword_ptr++ = cpu_to_le32(pci_dma_lo32(dma_handle));
3277                         *dword_ptr++ = cpu_to_le32(pci_dma_hi32(dma_handle));
3278                         *dword_ptr = cpu_to_le32(cmd->request_bufflen);
3279
3280                         dprintk(5, "qla1280_64bit_start_scsi: No scatter/"
3281                                 "gather command packet data - b %i, t %i, "
3282                                 "l %i \n", SCSI_BUS_32(cmd), SCSI_TCN_32(cmd),
3283                                 SCSI_LUN_32(cmd));
3284                         qla1280_dump_buffer(5, (char *)pkt,
3285                                             REQUEST_ENTRY_SIZE);
3286                 }
3287         } else {        /* No data transfer */
3288                 dprintk(5, "qla1280_64bit_start_scsi: No data, command "
3289                         "packet data - b %i, t %i, l %i \n",
3290                         SCSI_BUS_32(cmd), SCSI_TCN_32(cmd), SCSI_LUN_32(cmd));
3291                 qla1280_dump_buffer(5, (char *)pkt, REQUEST_ENTRY_SIZE);
3292         }
3293         /* Adjust ring index. */
3294         ha->req_ring_index++;
3295         if (ha->req_ring_index == REQUEST_ENTRY_CNT) {
3296                 ha->req_ring_index = 0;
3297                 ha->request_ring_ptr = ha->request_ring;
3298         } else
3299                 ha->request_ring_ptr++;
3300
3301         /* Set chip new ring index. */
3302         dprintk(2,
3303                 "qla1280_64bit_start_scsi: Wakeup RISC for pending command\n");
3304         sp->flags |= SRB_SENT;
3305         ha->actthreads++;
3306         WRT_REG_WORD(&reg->mailbox4, ha->req_ring_index);
3307         /* Enforce mmio write ordering; see comment in qla1280_isp_cmd(). */
3308         mmiowb();
3309
3310  out:
3311         if (status)
3312                 dprintk(2, "qla1280_64bit_start_scsi: **** FAILED ****\n");
3313         else
3314                 dprintk(3, "qla1280_64bit_start_scsi: exiting normally\n");
3315
3316         return status;
3317 }
3318 #else /* !QLA_64BIT_PTR */
3319
3320 /*
3321  * qla1280_32bit_start_scsi
3322  *      The start SCSI is responsible for building request packets on
3323  *      request ring and modifying ISP input pointer.
3324  *
3325  *      The Qlogic firmware interface allows every queue slot to have a SCSI
3326  *      command and up to 4 scatter/gather (SG) entries.  If we need more
3327  *      than 4 SG entries, then continuation entries are used that can
3328  *      hold another 7 entries each.  The start routine determines if there
3329  *      is eought empty slots then build the combination of requests to
3330  *      fulfill the OS request.
3331  *
3332  * Input:
3333  *      ha = adapter block pointer.
3334  *      sp = SCSI Request Block structure pointer.
3335  *
3336  * Returns:
3337  *      0 = success, was able to issue command.
3338  */
3339 static int
3340 qla1280_32bit_start_scsi(struct scsi_qla_host *ha, struct srb * sp)
3341 {
3342         struct device_reg __iomem *reg = ha->iobase;
3343         struct scsi_cmnd *cmd = sp->cmd;
3344         struct cmd_entry *pkt;
3345         struct scatterlist *sg = NULL;
3346         uint32_t *dword_ptr;
3347         int status = 0;
3348         int cnt;
3349         int req_cnt;
3350         uint16_t seg_cnt;
3351         dma_addr_t dma_handle;
3352         u8 dir;
3353
3354         ENTER("qla1280_32bit_start_scsi");
3355
3356         dprintk(1, "32bit_start: cmd=%p sp=%p CDB=%x\n", cmd, sp,
3357                 cmd->cmnd[0]);
3358
3359         /* Calculate number of entries and segments required. */
3360         req_cnt = 1;
3361         if (cmd->use_sg) {
3362                 /*
3363                  * We must build an SG list in adapter format, as the kernel's
3364                  * SG list cannot be used directly because of data field size
3365                  * (__alpha__) differences and the kernel SG list uses virtual
3366                  * addresses where we need physical addresses.
3367                  */
3368                 sg = (struct scatterlist *) cmd->request_buffer;
3369                 seg_cnt = pci_map_sg(ha->pdev, sg, cmd->use_sg,
3370                                      cmd->sc_data_direction);
3371
3372                 /*
3373                  * if greater than four sg entries then we need to allocate
3374                  * continuation entries
3375                  */
3376                 if (seg_cnt > 4) {
3377                         req_cnt += (seg_cnt - 4) / 7;
3378                         if ((seg_cnt - 4) % 7)
3379                                 req_cnt++;
3380                 }
3381                 dprintk(3, "S/G Transfer cmd=%p seg_cnt=0x%x, req_cnt=%x\n",
3382                         cmd, seg_cnt, req_cnt);
3383         } else if (cmd->request_bufflen) {      /* If data transfer. */
3384                 dprintk(3, "No S/G transfer t=%x cmd=%p len=%x CDB=%x\n",
3385                         SCSI_TCN_32(cmd), cmd, cmd->request_bufflen,
3386                         cmd->cmnd[0]);
3387                 seg_cnt = 1;
3388         } else {
3389                 /* dprintk(1, "No data transfer \n"); */
3390                 seg_cnt = 0;
3391         }
3392
3393         if ((req_cnt + 2) >= ha->req_q_cnt) {
3394                 /* Calculate number of free request entries. */
3395                 cnt = RD_REG_WORD(&reg->mailbox4);
3396                 if (ha->req_ring_index < cnt)
3397                         ha->req_q_cnt = cnt - ha->req_ring_index;
3398                 else
3399                         ha->req_q_cnt =
3400                                 REQUEST_ENTRY_CNT - (ha->req_ring_index - cnt);
3401         }
3402
3403         dprintk(3, "Number of free entries=(%d) seg_cnt=0x%x\n",
3404                 ha->req_q_cnt, seg_cnt);
3405         /* If room for request in request ring. */
3406         if ((req_cnt + 2) >= ha->req_q_cnt) {
3407                 status = 1;
3408                 dprintk(2, "qla1280_32bit_start_scsi: in-ptr=0x%x, "
3409                         "req_q_cnt=0x%x, req_cnt=0x%x", ha->req_ring_index,
3410                         ha->req_q_cnt, req_cnt);
3411                 goto out;
3412         }
3413
3414         /* Check for empty slot in outstanding command list. */
3415         for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS &&
3416                      (ha->outstanding_cmds[cnt] != 0); cnt++) ;
3417
3418         if (cnt >= MAX_OUTSTANDING_COMMANDS) {
3419                 status = 1;
3420                 dprintk(2, "qla1280_32bit_start_scsi: NO ROOM IN OUTSTANDING "
3421                         "ARRAY, req_q_cnt=0x%x\n", ha->req_q_cnt);
3422                 goto out;
3423         }
3424
3425         CMD_HANDLE(sp->cmd) = (unsigned char *) (unsigned long)(cnt + 1);
3426         ha->outstanding_cmds[cnt] = sp;
3427         ha->req_q_cnt -= req_cnt;
3428
3429         /*
3430          * Build command packet.
3431          */
3432         pkt = (struct cmd_entry *) ha->request_ring_ptr;
3433
3434         pkt->entry_type = COMMAND_TYPE;
3435         pkt->entry_count = (uint8_t) req_cnt;
3436         pkt->sys_define = (uint8_t) ha->req_ring_index;
3437         pkt->entry_status = 0;
3438         pkt->handle = cpu_to_le32(cnt);
3439
3440         /* Zero out remaining portion of packet. */
3441         memset(((char *)pkt + 8), 0, (REQUEST_ENTRY_SIZE - 8));
3442
3443         /* Set ISP command timeout. */
3444         pkt->timeout = cpu_to_le16(30);
3445
3446         /* Set device target ID and LUN */
3447         pkt->lun = SCSI_LUN_32(cmd);
3448         pkt->target = SCSI_BUS_32(cmd) ?
3449                 (SCSI_TCN_32(cmd) | BIT_7) : SCSI_TCN_32(cmd);
3450
3451         /* Enable simple tag queuing if device supports it. */
3452         if (DEV_SIMPLE_TAGS(cmd->device))
3453                 pkt->control_flags |= cpu_to_le16(BIT_3);
3454
3455         /* Load SCSI command packet. */
3456         pkt->cdb_len = cpu_to_le16(CMD_CDBLEN(cmd));
3457         memcpy(pkt->scsi_cdb, &(CMD_CDBP(cmd)), CMD_CDBLEN(cmd));
3458
3459         /*dprintk(1, "Build packet for command[0]=0x%x\n",pkt->scsi_cdb[0]); */
3460         /* Set transfer direction. */
3461         dir = qla1280_data_direction(cmd);
3462         pkt->control_flags |= cpu_to_le16(dir);
3463
3464         /* Set total data segment count. */
3465         pkt->dseg_count = cpu_to_le16(seg_cnt);
3466
3467         /*
3468          * Load data segments.
3469          */
3470         if (seg_cnt) {
3471                 /* Setup packet address segment pointer. */
3472                 dword_ptr = &pkt->dseg_0_address;
3473
3474                 if (cmd->use_sg) {      /* If scatter gather */
3475                         dprintk(3, "Building S/G data segments..\n");
3476                         qla1280_dump_buffer(1, (char *)sg, 4 * 16);
3477
3478                         /* Load command entry data segments. */
3479                         for (cnt = 0; cnt < 4 && seg_cnt; cnt++, seg_cnt--) {
3480                                 *dword_ptr++ =
3481                                         cpu_to_le32(pci_dma_lo32(sg_dma_address(sg)));
3482                                 *dword_ptr++ =
3483                                         cpu_to_le32(sg_dma_len(sg));
3484                                 dprintk(3, "S/G Segment phys_addr=0x%lx, len=0x%x\n",
3485                                         (pci_dma_lo32(sg_dma_address(sg))),
3486                                         (sg_dma_len(sg)));
3487                                 sg++;
3488                         }
3489                         /*
3490                          * Build continuation packets.
3491                          */
3492                         dprintk(3, "S/G Building Continuation"
3493                                 "...seg_cnt=0x%x remains\n", seg_cnt);
3494                         while (seg_cnt > 0) {
3495                                 /* Adjust ring index. */
3496                                 ha->req_ring_index++;
3497                                 if (ha->req_ring_index == REQUEST_ENTRY_CNT) {
3498                                         ha->req_ring_index = 0;
3499                                         ha->request_ring_ptr =
3500                                                 ha->request_ring;
3501                                 } else
3502                                         ha->request_ring_ptr++;
3503
3504                                 pkt = (struct cmd_entry *)ha->request_ring_ptr;
3505
3506                                 /* Zero out packet. */
3507                                 memset(pkt, 0, REQUEST_ENTRY_SIZE);
3508
3509                                 /* Load packet defaults. */
3510                                 ((struct cont_entry *) pkt)->
3511                                         entry_type = CONTINUE_TYPE;
3512                                 ((struct cont_entry *) pkt)->entry_count = 1;
3513
3514                                 ((struct cont_entry *) pkt)->sys_define =
3515                                         (uint8_t) ha->req_ring_index;
3516
3517                                 /* Setup packet address segment pointer. */
3518                                 dword_ptr =
3519                                         &((struct cont_entry *) pkt)->dseg_0_address;
3520
3521                                 /* Load continuation entry data segments. */
3522                                 for (cnt = 0; cnt < 7 && seg_cnt;
3523                                      cnt++, seg_cnt--) {
3524                                         *dword_ptr++ =
3525                                                 cpu_to_le32(pci_dma_lo32(sg_dma_address(sg)));
3526                                         *dword_ptr++ =
3527                                                 cpu_to_le32(sg_dma_len(sg));
3528                                         dprintk(1,
3529                                                 "S/G Segment Cont. phys_addr=0x%x, "
3530                                                 "len=0x%x\n",
3531                                                 cpu_to_le32(pci_dma_lo32(sg_dma_address(sg))),
3532                                                 cpu_to_le32(sg_dma_len(sg)));
3533                                         sg++;
3534                                 }
3535                                 dprintk(5, "qla1280_32bit_start_scsi: "
3536                                         "continuation packet data - "
3537                                         "scsi(%i:%i:%i)\n", SCSI_BUS_32(cmd),
3538                                         SCSI_TCN_32(cmd), SCSI_LUN_32(cmd));
3539                                 qla1280_dump_buffer(5, (char *)pkt,
3540                                                     REQUEST_ENTRY_SIZE);
3541                         }
3542                 } else {        /* No S/G data transfer */
3543                         dma_handle = pci_map_single(ha->pdev,
3544                                         cmd->request_buffer,
3545                                         cmd->request_bufflen,
3546                                         cmd->sc_data_direction);
3547                         sp->saved_dma_handle = dma_handle;
3548
3549                         *dword_ptr++ = cpu_to_le32(pci_dma_lo32(dma_handle));
3550                         *dword_ptr = cpu_to_le32(cmd->request_bufflen);
3551                 }
3552         } else {        /* No data transfer at all */
3553                 dprintk(5, "qla1280_32bit_start_scsi: No data, command "
3554                         "packet data - \n");
3555                 qla1280_dump_buffer(5, (char *)pkt, REQUEST_ENTRY_SIZE);
3556         }
3557         dprintk(5, "qla1280_32bit_start_scsi: First IOCB block:\n");
3558         qla1280_dump_buffer(5, (char *)ha->request_ring_ptr,
3559                             REQUEST_ENTRY_SIZE);
3560
3561         /* Adjust ring index. */
3562         ha->req_ring_index++;
3563         if (ha->req_ring_index == REQUEST_ENTRY_CNT) {
3564                 ha->req_ring_index = 0;
3565                 ha->request_ring_ptr = ha->request_ring;
3566         } else
3567                 ha->request_ring_ptr++;
3568
3569         /* Set chip new ring index. */
3570         dprintk(2, "qla1280_32bit_start_scsi: Wakeup RISC "
3571                 "for pending command\n");
3572         sp->flags |= SRB_SENT;
3573         ha->actthreads++;
3574         WRT_REG_WORD(&reg->mailbox4, ha->req_ring_index);
3575         /* Enforce mmio write ordering; see comment in qla1280_isp_cmd(). */
3576         mmiowb();
3577
3578 out:
3579         if (status)
3580                 dprintk(2, "qla1280_32bit_start_scsi: **** FAILED ****\n");
3581
3582         LEAVE("qla1280_32bit_start_scsi");
3583
3584         return status;
3585 }
3586 #endif
3587
3588 /*
3589  * qla1280_req_pkt
3590  *      Function is responsible for locking ring and
3591  *      getting a zeroed out request packet.
3592  *
3593  * Input:
3594  *      ha  = adapter block pointer.
3595  *
3596  * Returns:
3597  *      0 = failed to get slot.
3598  */
3599 static request_t *
3600 qla1280_req_pkt(struct scsi_qla_host *ha)
3601 {
3602         struct device_reg __iomem *reg = ha->iobase;
3603         request_t *pkt = NULL;
3604         int cnt;
3605         uint32_t timer;
3606
3607         ENTER("qla1280_req_pkt");
3608
3609         /*
3610          * This can be called from interrupt context, damn it!!!
3611          */
3612         /* Wait for 30 seconds for slot. */
3613         for (timer = 15000000; timer; timer--) {
3614                 if (ha->req_q_cnt > 0) {
3615                         /* Calculate number of free request entries. */
3616                         cnt = RD_REG_WORD(&reg->mailbox4);
3617                         if (ha->req_ring_index < cnt)
3618                                 ha->req_q_cnt = cnt - ha->req_ring_index;
3619                         else
3620                                 ha->req_q_cnt =
3621                                         REQUEST_ENTRY_CNT - (ha->req_ring_index - cnt);
3622                 }
3623
3624                 /* Found empty request ring slot? */
3625                 if (ha->req_q_cnt > 0) {
3626                         ha->req_q_cnt--;
3627                         pkt = ha->request_ring_ptr;
3628
3629                         /* Zero out packet. */
3630                         memset(pkt, 0, REQUEST_ENTRY_SIZE);
3631
3632                         /*
3633                          * How can this be right when we have a ring
3634                          * size of 512???
3635                          */
3636                         /* Set system defined field. */
3637                         pkt->sys_define = (uint8_t) ha->req_ring_index;
3638
3639                         /* Set entry count. */
3640                         pkt->entry_count = 1;
3641
3642                         break;
3643                 }
3644
3645                 udelay(2);      /* 10 */
3646
3647                 /* Check for pending interrupts. */
3648                 qla1280_poll(ha);
3649         }
3650
3651         if (!pkt)
3652                 dprintk(2, "qla1280_req_pkt: **** FAILED ****\n");
3653         else
3654                 dprintk(3, "qla1280_req_pkt: exiting normally\n");
3655
3656         return pkt;
3657 }
3658
3659 /*
3660  * qla1280_isp_cmd
3661  *      Function is responsible for modifying ISP input pointer.
3662  *      Releases ring lock.
3663  *
3664  * Input:
3665  *      ha  = adapter block pointer.
3666  */
3667 static void
3668 qla1280_isp_cmd(struct scsi_qla_host *ha)
3669 {
3670         struct device_reg __iomem *reg = ha->iobase;
3671
3672         ENTER("qla1280_isp_cmd");
3673
3674         dprintk(5, "qla1280_isp_cmd: IOCB data:\n");
3675         qla1280_dump_buffer(5, (char *)ha->request_ring_ptr,
3676                             REQUEST_ENTRY_SIZE);
3677
3678         /* Adjust ring index. */
3679         ha->req_ring_index++;
3680         if (ha->req_ring_index == REQUEST_ENTRY_CNT) {
3681                 ha->req_ring_index = 0;
3682                 ha->request_ring_ptr = ha->request_ring;
3683         } else
3684                 ha->request_ring_ptr++;
3685
3686         /*
3687          * Update request index to mailbox4 (Request Queue In).
3688          * The mmiowb() ensures that this write is ordered with writes by other
3689          * CPUs.  Without the mmiowb(), it is possible for the following:
3690          *    CPUA posts write of index 5 to mailbox4
3691          *    CPUA releases host lock
3692          *    CPUB acquires host lock
3693          *    CPUB posts write of index 6 to mailbox4
3694          *    On PCI bus, order reverses and write of 6 posts, then index 5,
3695          *       causing chip to issue full queue of stale commands
3696          * The mmiowb() prevents future writes from crossing the barrier.
3697          * See Documentation/DocBook/deviceiobook.tmpl for more information.
3698          */
3699         WRT_REG_WORD(&reg->mailbox4, ha->req_ring_index);
3700         mmiowb();
3701
3702         LEAVE("qla1280_isp_cmd");
3703 }
3704
3705 /****************************************************************************/
3706 /*                        Interrupt Service Routine.                        */
3707 /****************************************************************************/
3708
3709 /****************************************************************************
3710  *  qla1280_isr
3711  *      Calls I/O done on command completion.
3712  *
3713  * Input:
3714  *      ha           = adapter block pointer.
3715  *      done_q       = done queue.
3716  ****************************************************************************/
3717 static void
3718 qla1280_isr(struct scsi_qla_host *ha, struct list_head *done_q)
3719 {
3720         struct device_reg __iomem *reg = ha->iobase;
3721         struct response *pkt;
3722         struct srb *sp = NULL;
3723         uint16_t mailbox[MAILBOX_REGISTER_COUNT];
3724         uint16_t *wptr;
3725         uint32_t index;
3726         u16 istatus;
3727
3728         ENTER("qla1280_isr");
3729
3730         istatus = RD_REG_WORD(&reg->istatus);
3731         if (!(istatus & (RISC_INT | PCI_INT)))
3732                 return;
3733
3734         /* Save mailbox register 5 */
3735         mailbox[5] = RD_REG_WORD(&reg->mailbox5);
3736
3737         /* Check for mailbox interrupt. */
3738
3739         mailbox[0] = RD_REG_WORD_dmasync(&reg->semaphore);
3740
3741         if (mailbox[0] & BIT_0) {
3742                 /* Get mailbox data. */
3743                 /* dprintk(1, "qla1280_isr: In Get mailbox data \n"); */
3744
3745                 wptr = &mailbox[0];
3746                 *wptr++ = RD_REG_WORD(&reg->mailbox0);
3747                 *wptr++ = RD_REG_WORD(&reg->mailbox1);
3748                 *wptr = RD_REG_WORD(&reg->mailbox2);
3749                 if (mailbox[0] != MBA_SCSI_COMPLETION) {
3750                         wptr++;
3751                         *wptr++ = RD_REG_WORD(&reg->mailbox3);
3752                         *wptr++ = RD_REG_WORD(&reg->mailbox4);
3753                         wptr++;
3754                         *wptr++ = RD_REG_WORD(&reg->mailbox6);
3755                         *wptr = RD_REG_WORD(&reg->mailbox7);
3756                 }
3757
3758                 /* Release mailbox registers. */
3759
3760                 WRT_REG_WORD(&reg->semaphore, 0);
3761                 WRT_REG_WORD(&reg->host_cmd, HC_CLR_RISC_INT);
3762
3763                 dprintk(5, "qla1280_isr: mailbox interrupt mailbox[0] = 0x%x",
3764                         mailbox[0]);
3765
3766                 /* Handle asynchronous event */
3767                 switch (mailbox[0]) {
3768                 case MBA_SCSI_COMPLETION:       /* Response completion */
3769                         dprintk(5, "qla1280_isr: mailbox SCSI response "
3770                                 "completion\n");
3771
3772                         if (ha->flags.online) {
3773                                 /* Get outstanding command index. */
3774                                 index = mailbox[2] << 16 | mailbox[1];
3775
3776                                 /* Validate handle. */
3777                                 if (index < MAX_OUTSTANDING_COMMANDS)
3778                                         sp = ha->outstanding_cmds[index];
3779                                 else
3780                                         sp = NULL;
3781
3782                                 if (sp) {
3783                                         /* Free outstanding command slot. */
3784                                         ha->outstanding_cmds[index] = NULL;
3785
3786                                         /* Save ISP completion status */
3787                                         CMD_RESULT(sp->cmd) = 0;
3788
3789                                         /* Place block on done queue */
3790                                         list_add_tail(&sp->list, done_q);
3791                                 } else {
3792                                         /*
3793                                          * If we get here we have a real problem!
3794                                          */
3795                                         printk(KERN_WARNING
3796                                                "qla1280: ISP invalid handle");
3797                                 }
3798                         }
3799                         break;
3800
3801                 case MBA_BUS_RESET:     /* SCSI Bus Reset */
3802                         ha->flags.reset_marker = 1;
3803                         index = mailbox[6] & BIT_0;
3804                         ha->bus_settings[index].reset_marker = 1;
3805
3806                         printk(KERN_DEBUG "qla1280_isr(): index %i "
3807                                "asynchronous BUS_RESET\n", index);
3808                         break;
3809
3810                 case MBA_SYSTEM_ERR:    /* System Error */
3811                         printk(KERN_WARNING
3812                                "qla1280: ISP System Error - mbx1=%xh, mbx2="
3813                                "%xh, mbx3=%xh\n", mailbox[1], mailbox[2],
3814                                mailbox[3]);
3815                         break;
3816
3817                 case MBA_REQ_TRANSFER_ERR:      /* Request Transfer Error */
3818                         printk(KERN_WARNING
3819                                "qla1280: ISP Request Transfer Error\n");
3820                         break;
3821
3822                 case MBA_RSP_TRANSFER_ERR:      /* Response Transfer Error */
3823                         printk(KERN_WARNING
3824                                "qla1280: ISP Response Transfer Error\n");
3825                         break;
3826
3827                 case MBA_WAKEUP_THRES:  /* Request Queue Wake-up */
3828                         dprintk(2, "qla1280_isr: asynchronous WAKEUP_THRES\n");
3829                         break;
3830
3831                 case MBA_TIMEOUT_RESET: /* Execution Timeout Reset */
3832                         dprintk(2,
3833                                 "qla1280_isr: asynchronous TIMEOUT_RESET\n");
3834                         break;
3835
3836                 case MBA_DEVICE_RESET:  /* Bus Device Reset */
3837                         printk(KERN_INFO "qla1280_isr(): asynchronous "
3838                                "BUS_DEVICE_RESET\n");
3839
3840                         ha->flags.reset_marker = 1;
3841                         index = mailbox[6] & BIT_0;
3842                         ha->bus_settings[index].reset_marker = 1;
3843                         break;
3844
3845                 case MBA_BUS_MODE_CHANGE:
3846                         dprintk(2,
3847                                 "qla1280_isr: asynchronous BUS_MODE_CHANGE\n");
3848                         break;
3849
3850                 default:
3851                         /* dprintk(1, "qla1280_isr: default case of switch MB \n"); */
3852                         if (mailbox[0] < MBA_ASYNC_EVENT) {
3853                                 wptr = &mailbox[0];
3854                                 memcpy((uint16_t *) ha->mailbox_out, wptr,
3855                                        MAILBOX_REGISTER_COUNT *
3856                                        sizeof(uint16_t));
3857
3858                                 if(ha->mailbox_wait != NULL)
3859                                         complete(ha->mailbox_wait);
3860                         }
3861                         break;
3862                 }
3863         } else {
3864                 WRT_REG_WORD(&reg->host_cmd, HC_CLR_RISC_INT);
3865         }
3866
3867         /*
3868          * We will receive interrupts during mailbox testing prior to
3869          * the card being marked online, hence the double check.
3870          */
3871         if (!(ha->flags.online && !ha->mailbox_wait)) {
3872                 dprintk(2, "qla1280_isr: Response pointer Error\n");
3873                 goto out;
3874         }
3875
3876         if (mailbox[5] >= RESPONSE_ENTRY_CNT)
3877                 goto out;
3878
3879         while (ha->rsp_ring_index != mailbox[5]) {
3880                 pkt = ha->response_ring_ptr;
3881
3882                 dprintk(5, "qla1280_isr: ha->rsp_ring_index = 0x%x, mailbox[5]"
3883                         " = 0x%x\n", ha->rsp_ring_index, mailbox[5]);
3884                 dprintk(5,"qla1280_isr: response packet data\n");
3885                 qla1280_dump_buffer(5, (char *)pkt, RESPONSE_ENTRY_SIZE);
3886
3887                 if (pkt->entry_type == STATUS_TYPE) {
3888                         if ((le16_to_cpu(pkt->scsi_status) & 0xff)
3889                             || pkt->comp_status || pkt->entry_status) {
3890                                 dprintk(2, "qla1280_isr: ha->rsp_ring_index = "
3891                                         "0x%x mailbox[5] = 0x%x, comp_status "
3892                                         "= 0x%x, scsi_status = 0x%x\n",
3893                                         ha->rsp_ring_index, mailbox[5],
3894                                         le16_to_cpu(pkt->comp_status),
3895                                         le16_to_cpu(pkt->scsi_status));
3896                         }
3897                 } else {
3898                         dprintk(2, "qla1280_isr: ha->rsp_ring_index = "
3899                                 "0x%x, mailbox[5] = 0x%x\n",
3900                                 ha->rsp_ring_index, mailbox[5]);
3901                         dprintk(2, "qla1280_isr: response packet data\n");
3902                         qla1280_dump_buffer(2, (char *)pkt,
3903                                             RESPONSE_ENTRY_SIZE);
3904                 }
3905
3906                 if (pkt->entry_type == STATUS_TYPE || pkt->entry_status) {
3907                         dprintk(2, "status: Cmd %p, handle %i\n",
3908                                 ha->outstanding_cmds[pkt->handle]->cmd,
3909                                 pkt->handle);
3910                         if (pkt->entry_type == STATUS_TYPE)
3911                                 qla1280_status_entry(ha, pkt, done_q);
3912                         else
3913                                 qla1280_error_entry(ha, pkt, done_q);
3914                         /* Adjust ring index. */
3915                         ha->rsp_ring_index++;
3916                         if (ha->rsp_ring_index == RESPONSE_ENTRY_CNT) {
3917                                 ha->rsp_ring_index = 0;
3918                                 ha->response_ring_ptr = ha->response_ring;
3919                         } else
3920                                 ha->response_ring_ptr++;
3921                         WRT_REG_WORD(&reg->mailbox5, ha->rsp_ring_index);
3922                 }
3923         }
3924         
3925  out:
3926         LEAVE("qla1280_isr");
3927 }
3928
3929 /*
3930  *  qla1280_rst_aen
3931  *      Processes asynchronous reset.
3932  *
3933  * Input:
3934  *      ha  = adapter block pointer.
3935  */
3936 static void
3937 qla1280_rst_aen(struct scsi_qla_host *ha)
3938 {
3939         uint8_t bus;
3940
3941         ENTER("qla1280_rst_aen");
3942
3943         if (ha->flags.online && !ha->flags.reset_active &&
3944             !ha->flags.abort_isp_active) {
3945                 ha->flags.reset_active = 1;
3946                 while (ha->flags.reset_marker) {
3947                         /* Issue marker command. */
3948                         ha->flags.reset_marker = 0;
3949                         for (bus = 0; bus < ha->ports &&
3950                                      !ha->flags.reset_marker; bus++) {
3951                                 if (ha->bus_settings[bus].reset_marker) {
3952                                         ha->bus_settings[bus].reset_marker = 0;
3953                                         qla1280_marker(ha, bus, 0, 0,
3954                                                        MK_SYNC_ALL);
3955                                 }
3956                         }
3957                 }
3958         }
3959
3960         LEAVE("qla1280_rst_aen");
3961 }
3962
3963
3964 #if LINUX_VERSION_CODE < 0x020500
3965 /*
3966  *
3967  */
3968 static void
3969 qla1280_get_target_options(struct scsi_cmnd *cmd, struct scsi_qla_host *ha)
3970 {
3971         unsigned char *result;
3972         struct nvram *n;
3973         int bus, target, lun;
3974
3975         bus = SCSI_BUS_32(cmd);
3976         target = SCSI_TCN_32(cmd);
3977         lun = SCSI_LUN_32(cmd);
3978
3979         /*
3980          * Make sure to not touch anything if someone is using the
3981          * sg interface.
3982          */
3983         if (cmd->use_sg || (CMD_RESULT(cmd) >> 16) != DID_OK || lun)
3984                 return;
3985
3986         result = cmd->request_buffer;
3987         n = &ha->nvram;
3988
3989         n->bus[bus].target[target].parameter.f.enable_wide = 0;
3990         n->bus[bus].target[target].parameter.f.enable_sync = 0;
3991         n->bus[bus].target[target].ppr_1x160.flags.enable_ppr = 0;
3992
3993         if (result[7] & 0x60)
3994                 n->bus[bus].target[target].parameter.f.enable_wide = 1;
3995         if (result[7] & 0x10)
3996                 n->bus[bus].target[target].parameter.f.enable_sync = 1;
3997         if ((result[2] >= 3) && (result[4] + 5 > 56) &&
3998             (result[56] & 0x4))
3999                 n->bus[bus].target[target].ppr_1x160.flags.enable_ppr = 1;
4000
4001         dprintk(2, "get_target_options(): wide %i, sync %i, ppr %i\n",
4002                 n->bus[bus].target[target].parameter.f.enable_wide,
4003                 n->bus[bus].target[target].parameter.f.enable_sync,
4004                 n->bus[bus].target[target].ppr_1x160.flags.enable_ppr);
4005 }
4006 #endif
4007
4008 /*
4009  *  qla1280_status_entry
4010  *      Processes received ISP status entry.
4011  *
4012  * Input:
4013  *      ha           = adapter block pointer.
4014  *      pkt          = entry pointer.
4015  *      done_q       = done queue.
4016  */
4017 static void
4018 qla1280_status_entry(struct scsi_qla_host *ha, struct response *pkt,
4019                      struct list_head *done_q)
4020 {
4021         unsigned int bus, target, lun;
4022         int sense_sz;
4023         struct srb *sp;
4024         struct scsi_cmnd *cmd;
4025         uint32_t handle = le32_to_cpu(pkt->handle);
4026         uint16_t scsi_status = le16_to_cpu(pkt->scsi_status);
4027         uint16_t comp_status = le16_to_cpu(pkt->comp_status);
4028
4029         ENTER("qla1280_status_entry");
4030
4031         /* Validate handle. */
4032         if (handle < MAX_OUTSTANDING_COMMANDS)
4033                 sp = ha->outstanding_cmds[handle];
4034         else
4035                 sp = NULL;
4036
4037         if (!sp) {
4038                 printk(KERN_WARNING "qla1280: Status Entry invalid handle\n");
4039                 goto out;
4040         }
4041
4042         /* Free outstanding command slot. */
4043         ha->outstanding_cmds[handle] = NULL;
4044
4045         cmd = sp->cmd;
4046
4047         /* Generate LU queue on cntrl, target, LUN */
4048         bus = SCSI_BUS_32(cmd);
4049         target = SCSI_TCN_32(cmd);
4050         lun = SCSI_LUN_32(cmd);
4051
4052         if (comp_status || scsi_status) {
4053                 dprintk(3, "scsi: comp_status = 0x%x, scsi_status = "
4054                         "0x%x, handle = 0x%x\n", comp_status,
4055                         scsi_status, handle);
4056         }
4057
4058         /* Target busy or queue full */
4059         if ((scsi_status & 0xFF) == SAM_STAT_TASK_SET_FULL ||
4060             (scsi_status & 0xFF) == SAM_STAT_BUSY) {
4061                 CMD_RESULT(cmd) = scsi_status & 0xff;
4062         } else {
4063
4064                 /* Save ISP completion status */
4065                 CMD_RESULT(cmd) = qla1280_return_status(pkt, cmd);
4066
4067                 if (scsi_status & SS_CHECK_CONDITION) {
4068                         if (comp_status != CS_ARS_FAILED) {
4069                                 uint16_t req_sense_length =
4070                                         le16_to_cpu(pkt->req_sense_length);
4071                                 if (req_sense_length < CMD_SNSLEN(cmd))
4072                                         sense_sz = req_sense_length;
4073                                 else
4074                                         /*
4075                                          * scsi_cmnd->sense_buffer is
4076                                          * 64 bytes, why only copy 63?
4077                                          * This looks wrong! /Jes
4078                                          */
4079                                         sense_sz = CMD_SNSLEN(cmd) - 1;
4080
4081                                 memcpy(cmd->sense_buffer,
4082                                        &pkt->req_sense_data, sense_sz);
4083                         } else
4084                                 sense_sz = 0;
4085                         memset(cmd->sense_buffer + sense_sz, 0,
4086                                sizeof(cmd->sense_buffer) - sense_sz);
4087
4088                         dprintk(2, "qla1280_status_entry: Check "
4089                                 "condition Sense data, b %i, t %i, "
4090                                 "l %i\n", bus, target, lun);
4091                         if (sense_sz)
4092                                 qla1280_dump_buffer(2,
4093                                                     (char *)cmd->sense_buffer,
4094                                                     sense_sz);
4095                 }
4096         }
4097
4098         /* Place command on done queue. */
4099         list_add_tail(&sp->list, done_q);
4100  out:
4101         LEAVE("qla1280_status_entry");
4102 }
4103
4104 /*
4105  *  qla1280_error_entry
4106  *      Processes error entry.
4107  *
4108  * Input:
4109  *      ha           = adapter block pointer.
4110  *      pkt          = entry pointer.
4111  *      done_q       = done queue.
4112  */
4113 static void
4114 qla1280_error_entry(struct scsi_qla_host *ha, struct response *pkt,
4115                     struct list_head *done_q)
4116 {
4117         struct srb *sp;
4118         uint32_t handle = le32_to_cpu(pkt->handle);
4119
4120         ENTER("qla1280_error_entry");
4121
4122         if (pkt->entry_status & BIT_3)
4123                 dprintk(2, "qla1280_error_entry: BAD PAYLOAD flag error\n");
4124         else if (pkt->entry_status & BIT_2)
4125                 dprintk(2, "qla1280_error_entry: BAD HEADER flag error\n");
4126         else if (pkt->entry_status & BIT_1)
4127                 dprintk(2, "qla1280_error_entry: FULL flag error\n");
4128         else
4129                 dprintk(2, "qla1280_error_entry: UNKNOWN flag error\n");
4130
4131         /* Validate handle. */
4132         if (handle < MAX_OUTSTANDING_COMMANDS)
4133                 sp = ha->outstanding_cmds[handle];
4134         else
4135                 sp = NULL;
4136
4137         if (sp) {
4138                 /* Free outstanding command slot. */
4139                 ha->outstanding_cmds[handle] = NULL;
4140
4141                 /* Bad payload or header */
4142                 if (pkt->entry_status & (BIT_3 + BIT_2)) {
4143                         /* Bad payload or header, set error status. */
4144                         /* CMD_RESULT(sp->cmd) = CS_BAD_PAYLOAD; */
4145                         CMD_RESULT(sp->cmd) = DID_ERROR << 16;
4146                 } else if (pkt->entry_status & BIT_1) { /* FULL flag */
4147                         CMD_RESULT(sp->cmd) = DID_BUS_BUSY << 16;
4148                 } else {
4149                         /* Set error status. */
4150                         CMD_RESULT(sp->cmd) = DID_ERROR << 16;
4151                 }
4152
4153                 /* Place command on done queue. */
4154                 list_add_tail(&sp->list, done_q);
4155         }
4156 #ifdef QLA_64BIT_PTR
4157         else if (pkt->entry_type == COMMAND_A64_TYPE) {
4158                 printk(KERN_WARNING "!qla1280: Error Entry invalid handle");
4159         }
4160 #endif
4161
4162         LEAVE("qla1280_error_entry");
4163 }
4164
4165 /*
4166  *  qla1280_abort_isp
4167  *      Resets ISP and aborts all outstanding commands.
4168  *
4169  * Input:
4170  *      ha           = adapter block pointer.
4171  *
4172  * Returns:
4173  *      0 = success
4174  */
4175 static int
4176 qla1280_abort_isp(struct scsi_qla_host *ha)
4177 {
4178         struct device_reg __iomem *reg = ha->iobase;
4179         struct srb *sp;
4180         int status = 0;
4181         int cnt;
4182         int bus;
4183
4184         ENTER("qla1280_abort_isp");
4185
4186         if (ha->flags.abort_isp_active || !ha->flags.online)
4187                 goto out;
4188         
4189         ha->flags.abort_isp_active = 1;
4190
4191         /* Disable ISP interrupts. */
4192         qla1280_disable_intrs(ha);
4193         WRT_REG_WORD(&reg->host_cmd, HC_PAUSE_RISC);
4194         RD_REG_WORD(&reg->id_l);
4195
4196         printk(KERN_INFO "scsi(%li): dequeuing outstanding commands\n",
4197                ha->host_no);
4198         /* Dequeue all commands in outstanding command list. */
4199         for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
4200                 struct scsi_cmnd *cmd;
4201                 sp = ha->outstanding_cmds[cnt];
4202                 if (sp) {
4203
4204                         cmd = sp->cmd;
4205                         CMD_RESULT(cmd) = DID_RESET << 16;
4206
4207                         sp->cmd = NULL;
4208                         ha->outstanding_cmds[cnt] = NULL;
4209
4210                         (*cmd->scsi_done)(cmd);
4211
4212                         sp->flags = 0;
4213                 }
4214         }
4215
4216         status = qla1280_load_firmware(ha);
4217         if (status)
4218                 goto out;
4219
4220         /* Setup adapter based on NVRAM parameters. */
4221         qla1280_nvram_config (ha);
4222
4223         status = qla1280_init_rings(ha);
4224         if (status)
4225                 goto out;
4226                 
4227         /* Issue SCSI reset. */
4228         for (bus = 0; bus < ha->ports; bus++)
4229                 qla1280_bus_reset(ha, bus);
4230                 
4231         ha->flags.abort_isp_active = 0;
4232  out:
4233         if (status) {
4234                 printk(KERN_WARNING
4235                        "qla1280: ISP error recovery failed, board disabled");
4236                 qla1280_reset_adapter(ha);
4237                 dprintk(2, "qla1280_abort_isp: **** FAILED ****\n");
4238         }
4239
4240         LEAVE("qla1280_abort_isp");
4241         return status;
4242 }
4243
4244
4245 /*
4246  * qla1280_debounce_register
4247  *      Debounce register.
4248  *
4249  * Input:
4250  *      port = register address.
4251  *
4252  * Returns:
4253  *      register value.
4254  */
4255 static u16
4256 qla1280_debounce_register(volatile u16 __iomem * addr)
4257 {
4258         volatile u16 ret;
4259         volatile u16 ret2;
4260
4261         ret = RD_REG_WORD(addr);
4262         ret2 = RD_REG_WORD(addr);
4263
4264         if (ret == ret2)
4265                 return ret;
4266
4267         do {
4268                 cpu_relax();
4269                 ret = RD_REG_WORD(addr);
4270                 ret2 = RD_REG_WORD(addr);
4271         } while (ret != ret2);
4272
4273         return ret;
4274 }
4275
4276
4277 /************************************************************************
4278  * qla1280_check_for_dead_scsi_bus                                      *
4279  *                                                                      *
4280  *    This routine checks for a dead SCSI bus                           *
4281  ************************************************************************/
4282 #define SET_SXP_BANK            0x0100
4283 #define SCSI_PHASE_INVALID      0x87FF
4284 static int
4285 qla1280_check_for_dead_scsi_bus(struct scsi_qla_host *ha, unsigned int bus)
4286 {
4287         uint16_t config_reg, scsi_control;
4288         struct device_reg __iomem *reg = ha->iobase;
4289
4290         if (ha->bus_settings[bus].scsi_bus_dead) {
4291                 WRT_REG_WORD(&reg->host_cmd, HC_PAUSE_RISC);
4292                 config_reg = RD_REG_WORD(&reg->cfg_1);
4293                 WRT_REG_WORD(&reg->cfg_1, SET_SXP_BANK);
4294                 scsi_control = RD_REG_WORD(&reg->scsiControlPins);
4295                 WRT_REG_WORD(&reg->cfg_1, config_reg);
4296                 WRT_REG_WORD(&reg->host_cmd, HC_RELEASE_RISC);
4297
4298                 if (scsi_control == SCSI_PHASE_INVALID) {
4299                         ha->bus_settings[bus].scsi_bus_dead = 1;
4300 #if 0
4301                         CMD_RESULT(cp) = DID_NO_CONNECT << 16;
4302                         CMD_HANDLE(cp) = INVALID_HANDLE;
4303                         /* ha->actthreads--; */
4304
4305                         (*(cp)->scsi_done)(cp);
4306 #endif
4307                         return 1;       /* bus is dead */
4308                 } else {
4309                         ha->bus_settings[bus].scsi_bus_dead = 0;
4310                         ha->bus_settings[bus].failed_reset_count = 0;
4311                 }
4312         }
4313         return 0;               /* bus is not dead */
4314 }
4315
4316 static void
4317 qla1280_get_target_parameters(struct scsi_qla_host *ha,
4318                               struct scsi_device *device)
4319 {
4320         uint16_t mb[MAILBOX_REGISTER_COUNT];
4321         int bus, target, lun;
4322
4323         bus = device->channel;
4324         target = device->id;
4325         lun = device->lun;
4326
4327
4328         mb[0] = MBC_GET_TARGET_PARAMETERS;
4329         mb[1] = (uint16_t) (bus ? target | BIT_7 : target);
4330         mb[1] <<= 8;
4331         qla1280_mailbox_command(ha, BIT_6 | BIT_3 | BIT_2 | BIT_1 | BIT_0,
4332                                 &mb[0]);
4333
4334         printk(KERN_INFO "scsi(%li:%d:%d:%d):", ha->host_no, bus, target, lun);
4335
4336         if (mb[3] != 0) {
4337                 printk(" Sync: period %d, offset %d",
4338                        (mb[3] & 0xff), (mb[3] >> 8));
4339                 if (mb[2] & BIT_13)
4340                         printk(", Wide");
4341                 if ((mb[2] & BIT_5) && ((mb[6] >> 8) & 0xff) >= 2)
4342                         printk(", DT");
4343         } else
4344                 printk(" Async");
4345
4346         if (DEV_SIMPLE_TAGS(device))
4347                 printk(", Tagged queuing: depth %d", device->queue_depth);
4348         printk("\n");
4349 }
4350
4351
4352 #if DEBUG_QLA1280
4353 static void
4354 __qla1280_dump_buffer(char *b, int size)
4355 {
4356         int cnt;
4357         u8 c;
4358
4359         printk(KERN_DEBUG " 0   1   2   3   4   5   6   7   8   9   Ah  "
4360                "Bh  Ch  Dh  Eh  Fh\n");
4361         printk(KERN_DEBUG "---------------------------------------------"
4362                "------------------\n");
4363
4364         for (cnt = 0; cnt < size;) {
4365                 c = *b++;
4366
4367                 printk("0x%02x", c);
4368                 cnt++;
4369                 if (!(cnt % 16))
4370                         printk("\n");
4371                 else
4372                         printk(" ");
4373         }
4374         if (cnt % 16)
4375                 printk("\n");
4376 }
4377
4378 /**************************************************************************
4379  *   ql1280_print_scsi_cmd
4380  *
4381  **************************************************************************/
4382 static void
4383 __qla1280_print_scsi_cmd(struct scsi_cmnd *cmd)
4384 {
4385         struct scsi_qla_host *ha;
4386         struct Scsi_Host *host = CMD_HOST(cmd);
4387         struct srb *sp;
4388         /* struct scatterlist *sg; */
4389
4390         int i;
4391         ha = (struct scsi_qla_host *)host->hostdata;
4392
4393         sp = (struct srb *)CMD_SP(cmd);
4394         printk("SCSI Command @= 0x%p, Handle=0x%p\n", cmd, CMD_HANDLE(cmd));
4395         printk("  chan=%d, target = 0x%02x, lun = 0x%02x, cmd_len = 0x%02x\n",
4396                SCSI_BUS_32(cmd), SCSI_TCN_32(cmd), SCSI_LUN_32(cmd),
4397                CMD_CDBLEN(cmd));
4398         printk(" CDB = ");
4399         for (i = 0; i < cmd->cmd_len; i++) {
4400                 printk("0x%02x ", cmd->cmnd[i]);
4401         }
4402         printk("  seg_cnt =%d\n", cmd->use_sg);
4403         printk("  request buffer=0x%p, request buffer len=0x%x\n",
4404                cmd->request_buffer, cmd->request_bufflen);
4405         /* if (cmd->use_sg)
4406            {
4407            sg = (struct scatterlist *) cmd->request_buffer;
4408            printk("  SG buffer: \n");
4409            qla1280_dump_buffer(1, (char *)sg, (cmd->use_sg*sizeof(struct scatterlist)));
4410            } */
4411         printk("  tag=%d, transfersize=0x%x \n",
4412                cmd->tag, cmd->transfersize);
4413         printk("  Pid=%li, SP=0x%p\n", cmd->pid, CMD_SP(cmd));
4414         printk(" underflow size = 0x%x, direction=0x%x\n",
4415                cmd->underflow, cmd->sc_data_direction);
4416 }
4417
4418 /**************************************************************************
4419  *   ql1280_dump_device
4420  *
4421  **************************************************************************/
4422 static void
4423 ql1280_dump_device(struct scsi_qla_host *ha)
4424 {
4425
4426         struct scsi_cmnd *cp;
4427         struct srb *sp;
4428         int i;
4429
4430         printk(KERN_DEBUG "Outstanding Commands on controller:\n");
4431
4432         for (i = 0; i < MAX_OUTSTANDING_COMMANDS; i++) {
4433                 if ((sp = ha->outstanding_cmds[i]) == NULL)
4434                         continue;
4435                 if ((cp = sp->cmd) == NULL)
4436                         continue;
4437                 qla1280_print_scsi_cmd(1, cp);
4438         }
4439 }
4440 #endif
4441
4442
4443 enum tokens {
4444         TOKEN_NVRAM,
4445         TOKEN_SYNC,
4446         TOKEN_WIDE,
4447         TOKEN_PPR,
4448         TOKEN_VERBOSE,
4449         TOKEN_DEBUG,
4450 };
4451
4452 struct setup_tokens {
4453         char *token;
4454         int val;
4455 };
4456
4457 static struct setup_tokens setup_token[] __initdata = 
4458 {
4459         { "nvram", TOKEN_NVRAM },
4460         { "sync", TOKEN_SYNC },
4461         { "wide", TOKEN_WIDE },
4462         { "ppr", TOKEN_PPR },
4463         { "verbose", TOKEN_VERBOSE },
4464         { "debug", TOKEN_DEBUG },
4465 };
4466
4467
4468 /**************************************************************************
4469  *   qla1280_setup
4470  *
4471  *   Handle boot parameters. This really needs to be changed so one
4472  *   can specify per adapter parameters.
4473  **************************************************************************/
4474 static int __init
4475 qla1280_setup(char *s)
4476 {
4477         char *cp, *ptr;
4478         unsigned long val;
4479         int toke;
4480
4481         cp = s;
4482
4483         while (cp && (ptr = strchr(cp, ':'))) {
4484                 ptr++;
4485                 if (!strcmp(ptr, "yes")) {
4486                         val = 0x10000;
4487                         ptr += 3;
4488                 } else if (!strcmp(ptr, "no")) {
4489                         val = 0;
4490                         ptr += 2;
4491                 } else
4492                         val = simple_strtoul(ptr, &ptr, 0);
4493
4494                 switch ((toke = qla1280_get_token(cp))) {
4495                 case TOKEN_NVRAM:
4496                         if (!val)
4497                                 driver_setup.no_nvram = 1;
4498                         break;
4499                 case TOKEN_SYNC:
4500                         if (!val)
4501                                 driver_setup.no_sync = 1;
4502                         else if (val != 0x10000)
4503                                 driver_setup.sync_mask = val;
4504                         break;
4505                 case TOKEN_WIDE:
4506                         if (!val)
4507                                 driver_setup.no_wide = 1;
4508                         else if (val != 0x10000)
4509                                 driver_setup.wide_mask = val;
4510                         break;
4511                 case TOKEN_PPR:
4512                         if (!val)
4513                                 driver_setup.no_ppr = 1;
4514                         else if (val != 0x10000)
4515                                 driver_setup.ppr_mask = val;
4516                         break;
4517                 case TOKEN_VERBOSE:
4518                         qla1280_verbose = val;
4519                         break;
4520                 default:
4521                         printk(KERN_INFO "qla1280: unknown boot option %s\n",
4522                                cp);
4523                 }
4524
4525                 cp = strchr(ptr, ';');
4526                 if (cp)
4527                         cp++;
4528                 else {
4529                         break;
4530                 }
4531         }
4532         return 1;
4533 }
4534
4535
4536 static int
4537 qla1280_get_token(char *str)
4538 {
4539         char *sep;
4540         long ret = -1;
4541         int i, len;
4542
4543         len = sizeof(setup_token)/sizeof(struct setup_tokens);
4544
4545         sep = strchr(str, ':');
4546
4547         if (sep) {
4548                 for (i = 0; i < len; i++){
4549
4550                         if (!strncmp(setup_token[i].token, str, (sep - str))) {
4551                                 ret =  setup_token[i].val;
4552                                 break;
4553                         }
4554                 }
4555         }
4556
4557         return ret;
4558 }
4559
4560 #if LINUX_VERSION_CODE >= 0x020600
4561 static struct scsi_host_template qla1280_driver_template = {
4562         .module                 = THIS_MODULE,
4563         .proc_name              = "qla1280",
4564         .name                   = "Qlogic ISP 1280/12160",
4565         .info                   = qla1280_info,
4566         .slave_configure        = qla1280_slave_configure,
4567         .queuecommand           = qla1280_queuecommand,
4568         .eh_abort_handler       = qla1280_eh_abort,
4569         .eh_device_reset_handler= qla1280_eh_device_reset,
4570         .eh_bus_reset_handler   = qla1280_eh_bus_reset,
4571         .eh_host_reset_handler  = qla1280_eh_adapter_reset,
4572         .bios_param             = qla1280_biosparam,
4573         .can_queue              = 0xfffff,
4574         .this_id                = -1,
4575         .sg_tablesize           = SG_ALL,
4576         .cmd_per_lun            = 1,
4577         .use_clustering         = ENABLE_CLUSTERING,
4578 };
4579 #else
4580 static Scsi_Host_Template qla1280_driver_template = {
4581         .proc_name              = "qla1280",
4582         .name                   = "Qlogic ISP 1280/12160",
4583         .detect                 = qla1280_detect,
4584         .release                = qla1280_release,
4585         .info                   = qla1280_info,
4586         .queuecommand           = qla1280_queuecommand,
4587         .eh_abort_handler       = qla1280_eh_abort,
4588         .eh_device_reset_handler= qla1280_eh_device_reset,
4589         .eh_bus_reset_handler   = qla1280_eh_bus_reset,
4590         .eh_host_reset_handler  = qla1280_eh_adapter_reset,
4591         .bios_param             = qla1280_biosparam_old,
4592         .can_queue              = 0xfffff,
4593         .this_id                = -1,
4594         .sg_tablesize           = SG_ALL,
4595         .cmd_per_lun            = 1,
4596         .use_clustering         = ENABLE_CLUSTERING,
4597         .use_new_eh_code        = 1,
4598 };
4599 #endif
4600
4601 static int __devinit
4602 qla1280_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
4603 {
4604         int devnum = id->driver_data;
4605         struct qla_boards *bdp = &ql1280_board_tbl[devnum];
4606         struct Scsi_Host *host;
4607         struct scsi_qla_host *ha;
4608         int error = -ENODEV;
4609
4610         /* Bypass all AMI SUBSYS VENDOR IDs */
4611         if (pdev->subsystem_vendor == PCI_VENDOR_ID_AMI) {
4612                 printk(KERN_INFO
4613                        "qla1280: Skipping AMI SubSys Vendor ID Chip\n");
4614                 goto error;
4615         }
4616
4617         printk(KERN_INFO "qla1280: %s found on PCI bus %i, dev %i\n",
4618                bdp->name, pdev->bus->number, PCI_SLOT(pdev->devfn));
4619         
4620         if (pci_enable_device(pdev)) {
4621                 printk(KERN_WARNING
4622                        "qla1280: Failed to enabled pci device, aborting.\n");
4623                 goto error;
4624         }
4625
4626         pci_set_master(pdev);
4627
4628         error = -ENOMEM;
4629         host = scsi_host_alloc(&qla1280_driver_template, sizeof(*ha));
4630         if (!host) {
4631                 printk(KERN_WARNING
4632                        "qla1280: Failed to register host, aborting.\n");
4633                 goto error_disable_device;
4634         }
4635
4636         ha = (struct scsi_qla_host *)host->hostdata;
4637         memset(ha, 0, sizeof(struct scsi_qla_host));
4638
4639         ha->pdev = pdev;
4640         ha->devnum = devnum;    /* specifies microcode load address */
4641
4642 #ifdef QLA_64BIT_PTR
4643         if (pci_set_dma_mask(ha->pdev, (dma_addr_t) ~ 0ULL)) {
4644                 if (pci_set_dma_mask(ha->pdev, 0xffffffff)) {
4645                         printk(KERN_WARNING "scsi(%li): Unable to set a "
4646                                " suitable DMA mask - aboring\n", ha->host_no);
4647                         error = -ENODEV;
4648                         goto error_free_irq;
4649                 }
4650         } else
4651                 dprintk(2, "scsi(%li): 64 Bit PCI Addressing Enabled\n",
4652                         ha->host_no);
4653 #else
4654         if (pci_set_dma_mask(ha->pdev, 0xffffffff)) {
4655                 printk(KERN_WARNING "scsi(%li): Unable to set a "
4656                        " suitable DMA mask - aboring\n", ha->host_no);
4657                 error = -ENODEV;
4658                 goto error_free_irq;
4659         }
4660 #endif
4661
4662         ha->request_ring = pci_alloc_consistent(ha->pdev,
4663                         ((REQUEST_ENTRY_CNT + 1) * (sizeof(request_t))),
4664                         &ha->request_dma);
4665         if (!ha->request_ring) {
4666                 printk(KERN_INFO "qla1280: Failed to get request memory\n");
4667                 goto error_put_host;
4668         }
4669
4670         ha->response_ring = pci_alloc_consistent(ha->pdev,
4671                         ((RESPONSE_ENTRY_CNT + 1) * (sizeof(struct response))),
4672                         &ha->response_dma);
4673         if (!ha->response_ring) {
4674                 printk(KERN_INFO "qla1280: Failed to get response memory\n");
4675                 goto error_free_request_ring;
4676         }
4677
4678         ha->ports = bdp->numPorts;
4679
4680         ha->host = host;
4681         ha->host_no = host->host_no;
4682
4683         host->irq = pdev->irq;
4684         host->max_channel = bdp->numPorts - 1;
4685         host->max_lun = MAX_LUNS - 1;
4686         host->max_id = MAX_TARGETS;
4687         host->max_sectors = 1024;
4688         host->unique_id = host->host_no;
4689
4690 #if LINUX_VERSION_CODE < 0x020545
4691         host->select_queue_depths = qla1280_select_queue_depth;
4692 #endif
4693
4694         error = -ENODEV;
4695
4696 #if MEMORY_MAPPED_IO
4697         ha->mmpbase = ioremap(pci_resource_start(ha->pdev, 1),
4698                               pci_resource_len(ha->pdev, 1));
4699         if (!ha->mmpbase) {
4700                 printk(KERN_INFO "qla1280: Unable to map I/O memory\n");
4701                 goto error_free_response_ring;
4702         }
4703
4704         host->base = (unsigned long)ha->mmpbase;
4705         ha->iobase = (struct device_reg __iomem *)ha->mmpbase;
4706 #else
4707         host->io_port = pci_resource_start(ha->pdev, 0);
4708         if (!request_region(host->io_port, 0xff, "qla1280")) {
4709                 printk(KERN_INFO "qla1280: Failed to reserve i/o region "
4710                                  "0x%04lx-0x%04lx - already in use\n",
4711                        host->io_port, host->io_port + 0xff);
4712                 goto error_free_response_ring;
4713         }
4714
4715         ha->iobase = (struct device_reg *)host->io_port;
4716 #endif
4717
4718         INIT_LIST_HEAD(&ha->done_q);
4719
4720         /* Disable ISP interrupts. */
4721         qla1280_disable_intrs(ha);
4722
4723         if (request_irq(pdev->irq, qla1280_intr_handler, SA_SHIRQ,
4724                                 "qla1280", ha)) {
4725                 printk("qla1280 : Failed to reserve interrupt %d already "
4726                        "in use\n", pdev->irq);
4727                 goto error_release_region;
4728         }
4729
4730         /* load the F/W, read paramaters, and init the H/W */
4731         if (qla1280_initialize_adapter(ha)) {
4732                 printk(KERN_INFO "qla1x160: Failed to initialize adapter\n");
4733                 goto error_free_irq;
4734         }
4735
4736         /* set our host ID  (need to do something about our two IDs) */
4737         host->this_id = ha->bus_settings[0].id;
4738
4739         pci_set_drvdata(pdev, host);
4740
4741 #if LINUX_VERSION_CODE >= 0x020600
4742         error = scsi_add_host(host, &pdev->dev);
4743         if (error)
4744                 goto error_disable_adapter;
4745         scsi_scan_host(host);
4746 #else
4747         scsi_set_pci_device(host, pdev);
4748 #endif
4749
4750         return 0;
4751
4752 #if LINUX_VERSION_CODE >= 0x020600
4753  error_disable_adapter:
4754         WRT_REG_WORD(&ha->iobase->ictrl, 0);
4755 #endif
4756  error_free_irq:
4757         free_irq(pdev->irq, ha);
4758  error_release_region:
4759 #if MEMORY_MAPPED_IO
4760         iounmap(ha->mmpbase);
4761 #else
4762         release_region(host->io_port, 0xff);
4763 #endif
4764  error_free_response_ring:
4765         pci_free_consistent(ha->pdev,
4766                         ((RESPONSE_ENTRY_CNT + 1) * (sizeof(struct response))),
4767                         ha->response_ring, ha->response_dma);
4768  error_free_request_ring:
4769         pci_free_consistent(ha->pdev,
4770                         ((REQUEST_ENTRY_CNT + 1) * (sizeof(request_t))),
4771                         ha->request_ring, ha->request_dma);
4772  error_put_host:
4773         scsi_host_put(host);
4774  error_disable_device:
4775         pci_disable_device(pdev);
4776  error:
4777         return error;
4778 }
4779
4780
4781 static void __devexit
4782 qla1280_remove_one(struct pci_dev *pdev)
4783 {
4784         struct Scsi_Host *host = pci_get_drvdata(pdev);
4785         struct scsi_qla_host *ha = (struct scsi_qla_host *)host->hostdata;
4786
4787 #if LINUX_VERSION_CODE >= 0x020600
4788         scsi_remove_host(host);
4789 #endif
4790
4791         WRT_REG_WORD(&ha->iobase->ictrl, 0);
4792
4793         free_irq(pdev->irq, ha);
4794
4795 #if MEMORY_MAPPED_IO
4796         iounmap(ha->mmpbase);
4797 #else
4798         release_region(host->io_port, 0xff);
4799 #endif
4800
4801         pci_free_consistent(ha->pdev,
4802                         ((REQUEST_ENTRY_CNT + 1) * (sizeof(request_t))),
4803                         ha->request_ring, ha->request_dma);
4804         pci_free_consistent(ha->pdev,
4805                         ((RESPONSE_ENTRY_CNT + 1) * (sizeof(struct response))),
4806                         ha->response_ring, ha->response_dma);
4807
4808         pci_disable_device(pdev);
4809
4810         scsi_host_put(host);
4811 }
4812
4813 #if LINUX_VERSION_CODE >= 0x020600
4814 static struct pci_driver qla1280_pci_driver = {
4815         .name           = "qla1280",
4816         .id_table       = qla1280_pci_tbl,
4817         .probe          = qla1280_probe_one,
4818         .remove         = __devexit_p(qla1280_remove_one),
4819 };
4820
4821 static int __init
4822 qla1280_init(void)
4823 {
4824         if (sizeof(struct srb) > sizeof(struct scsi_pointer)) {
4825                 printk(KERN_WARNING
4826                        "qla1280: struct srb too big, aborting\n");
4827                 return -EINVAL;
4828         }
4829
4830 #ifdef MODULE
4831         /*
4832          * If we are called as a module, the qla1280 pointer may not be null
4833          * and it would point to our bootup string, just like on the lilo
4834          * command line.  IF not NULL, then process this config string with
4835          * qla1280_setup
4836          *
4837          * Boot time Options
4838          * To add options at boot time add a line to your lilo.conf file like:
4839          * append="qla1280=verbose,max_tags:{{255,255,255,255},{255,255,255,255}}"
4840          * which will result in the first four devices on the first two
4841          * controllers being set to a tagged queue depth of 32.
4842          */
4843         if (qla1280)
4844                 qla1280_setup(qla1280);
4845 #endif
4846
4847         return pci_module_init(&qla1280_pci_driver);
4848 }
4849
4850 static void __exit
4851 qla1280_exit(void)
4852 {
4853         pci_unregister_driver(&qla1280_pci_driver);
4854 }
4855
4856 module_init(qla1280_init);
4857 module_exit(qla1280_exit);
4858
4859 #else
4860 # define driver_template qla1280_driver_template
4861 # include "scsi_module.c"
4862 #endif
4863
4864 MODULE_AUTHOR("Qlogic & Jes Sorensen");
4865 MODULE_DESCRIPTION("Qlogic ISP SCSI (qla1x80/qla1x160) driver");
4866 MODULE_LICENSE("GPL");
4867 MODULE_VERSION(QLA1280_VERSION);
4868
4869 /*
4870  * Overrides for Emacs so that we almost follow Linus's tabbing style.
4871  * Emacs will notice this stuff at the end of the file and automatically
4872  * adjust the settings for this buffer only.  This must remain at the end
4873  * of the file.
4874  * ---------------------------------------------------------------------------
4875  * Local variables:
4876  * c-basic-offset: 8
4877  * tab-width: 8
4878  * End:
4879  */