[SCSI] ServeRAID V7.12.02
[linux-2.6] / 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                 ha->flags.in_reset = 1;
1000                 if (qla1280_verbose)
1001                         printk(KERN_INFO
1002                                "scsi(%ld:%d:%d:%d): Queueing abort device "
1003                                "command.\n", ha->host_no, bus, target, lun);
1004                 if (qla1280_abort_device(ha, bus, target, lun) == 0)
1005                         result = SUCCESS;
1006                 break;
1007
1008         case DEVICE_RESET:
1009                 if (qla1280_verbose)
1010                         printk(KERN_INFO
1011                                "scsi(%ld:%d:%d:%d): Queueing device reset "
1012                                "command.\n", ha->host_no, bus, target, lun);
1013                 ha->flags.in_reset = 1;
1014                 if (qla1280_device_reset(ha, bus, target) == 0)
1015                         result = SUCCESS;
1016                 break;
1017
1018         case BUS_RESET:
1019                 if (qla1280_verbose)
1020                         printk(KERN_INFO "qla1280(%ld:%d): Issuing BUS "
1021                                "DEVICE RESET\n", ha->host_no, bus);
1022                 ha->flags.in_reset = 1;
1023                 if (qla1280_bus_reset(ha, bus == 0))
1024                         result = SUCCESS;
1025
1026                 break;
1027
1028         case ADAPTER_RESET:
1029         default:
1030                 if (qla1280_verbose) {
1031                         printk(KERN_INFO
1032                                "scsi(%ld): Issued ADAPTER RESET\n",
1033                                ha->host_no);
1034                         printk(KERN_INFO "scsi(%ld): I/O processing will "
1035                                "continue automatically\n", ha->host_no);
1036                 }
1037                 ha->flags.reset_active = 1;
1038                 /*
1039                  * We restarted all of the commands automatically, so the
1040                  * mid-level code can expect completions momentitarily.
1041                  */
1042                 if (qla1280_abort_isp(ha) == 0)
1043                         result = SUCCESS;
1044
1045                 ha->flags.reset_active = 0;
1046         }
1047
1048         if (!list_empty(&ha->done_q))
1049                 qla1280_done(ha);
1050         ha->flags.in_reset = 0;
1051
1052         /* If we didn't manage to issue the action, or we have no
1053          * command to wait for, exit here */
1054         if (result == FAILED || handle == NULL ||
1055             handle == (unsigned char *)INVALID_HANDLE) {
1056                 /*
1057                  * Clear completion queue to avoid qla1280_done() trying
1058                  * to complete the command at a later stage after we
1059                  * have exited the current context
1060                  */
1061                 sp->wait = NULL;
1062                 goto leave;
1063         }
1064
1065         /* set up a timer just in case we're really jammed */
1066         init_timer(&timer);
1067         timer.expires = jiffies + 4*HZ;
1068         timer.data = (unsigned long)cmd;
1069         timer.function = qla1280_error_wait_timeout;
1070         add_timer(&timer);
1071
1072         /* wait for the action to complete (or the timer to expire) */
1073         spin_unlock_irq(HOST_LOCK);
1074         wait_for_completion(&wait);
1075         del_timer_sync(&timer);
1076         spin_lock_irq(HOST_LOCK);
1077         sp->wait = NULL;
1078
1079         /* the only action we might get a fail for is abort */
1080         if (action == ABORT_COMMAND) {
1081                 if(sp->flags & SRB_ABORTED)
1082                         result = SUCCESS;
1083                 else
1084                         result = FAILED;
1085         }
1086
1087  leave:
1088         dprintk(1, "RESET returning %d\n", result);
1089
1090         LEAVE("qla1280_error_action");
1091         return result;
1092 }
1093
1094 /**************************************************************************
1095  *   qla1280_abort
1096  *     Abort the specified SCSI command(s).
1097  **************************************************************************/
1098 static int
1099 qla1280_eh_abort(struct scsi_cmnd * cmd)
1100 {
1101         int rc;
1102
1103         spin_lock_irq(cmd->device->host->host_lock);
1104         rc = qla1280_error_action(cmd, ABORT_COMMAND);
1105         spin_unlock_irq(cmd->device->host->host_lock);
1106
1107         return rc;
1108 }
1109
1110 /**************************************************************************
1111  *   qla1280_device_reset
1112  *     Reset the specified SCSI device
1113  **************************************************************************/
1114 static int
1115 qla1280_eh_device_reset(struct scsi_cmnd *cmd)
1116 {
1117         int rc;
1118
1119         spin_lock_irq(cmd->device->host->host_lock);
1120         rc = qla1280_error_action(cmd, DEVICE_RESET);
1121         spin_unlock_irq(cmd->device->host->host_lock);
1122
1123         return rc;
1124 }
1125
1126 /**************************************************************************
1127  *   qla1280_bus_reset
1128  *     Reset the specified bus.
1129  **************************************************************************/
1130 static int
1131 qla1280_eh_bus_reset(struct scsi_cmnd *cmd)
1132 {
1133         int rc;
1134
1135         spin_lock_irq(cmd->device->host->host_lock);
1136         rc = qla1280_error_action(cmd, BUS_RESET);
1137         spin_unlock_irq(cmd->device->host->host_lock);
1138
1139         return rc;
1140 }
1141
1142 /**************************************************************************
1143  *   qla1280_adapter_reset
1144  *     Reset the specified adapter (both channels)
1145  **************************************************************************/
1146 static int
1147 qla1280_eh_adapter_reset(struct scsi_cmnd *cmd)
1148 {
1149         int rc;
1150
1151         spin_lock_irq(cmd->device->host->host_lock);
1152         rc = qla1280_error_action(cmd, ADAPTER_RESET);
1153         spin_unlock_irq(cmd->device->host->host_lock);
1154
1155         return rc;
1156 }
1157
1158 static int
1159 qla1280_biosparam(struct scsi_device *sdev, struct block_device *bdev,
1160                   sector_t capacity, int geom[])
1161 {
1162         int heads, sectors, cylinders;
1163
1164         heads = 64;
1165         sectors = 32;
1166         cylinders = (unsigned long)capacity / (heads * sectors);
1167         if (cylinders > 1024) {
1168                 heads = 255;
1169                 sectors = 63;
1170                 cylinders = (unsigned long)capacity / (heads * sectors);
1171                 /* if (cylinders > 1023)
1172                    cylinders = 1023; */
1173         }
1174
1175         geom[0] = heads;
1176         geom[1] = sectors;
1177         geom[2] = cylinders;
1178
1179         return 0;
1180 }
1181
1182 #if LINUX_VERSION_CODE < 0x020600
1183 static int
1184 qla1280_detect(Scsi_Host_Template *template)
1185 {
1186         struct pci_device_id *id = &qla1280_pci_tbl[0];
1187         struct pci_dev *pdev = NULL;
1188         int num_hosts = 0;
1189
1190         if (sizeof(struct srb) > sizeof(Scsi_Pointer)) {
1191                 printk(KERN_WARNING
1192                        "qla1280: struct srb too big, aborting\n");
1193                 return 0;
1194         }
1195
1196         if ((DMA_BIDIRECTIONAL != PCI_DMA_BIDIRECTIONAL) ||
1197             (DMA_TO_DEVICE != PCI_DMA_TODEVICE) ||
1198             (DMA_FROM_DEVICE != PCI_DMA_FROMDEVICE) ||
1199             (DMA_NONE != PCI_DMA_NONE)) {
1200                 printk(KERN_WARNING
1201                        "qla1280: dma direction bits don't match\n");
1202                 return 0;
1203         }
1204
1205 #ifdef MODULE
1206         /*
1207          * If we are called as a module, the qla1280 pointer may not be null
1208          * and it would point to our bootup string, just like on the lilo
1209          * command line.  IF not NULL, then process this config string with
1210          * qla1280_setup
1211          *
1212          * Boot time Options
1213          * To add options at boot time add a line to your lilo.conf file like:
1214          * append="qla1280=verbose,max_tags:{{255,255,255,255},{255,255,255,255}}"
1215          * which will result in the first four devices on the first two
1216          * controllers being set to a tagged queue depth of 32.
1217          */
1218         if (qla1280)
1219                 qla1280_setup(qla1280);
1220 #endif
1221
1222         /* First Initialize QLA12160 on PCI Bus 1 Dev 2 */
1223         while ((pdev = pci_find_device(id->vendor, id->device, pdev))) {
1224                 if (pdev->bus->number == 1 && PCI_SLOT(pdev->devfn) == 2) {
1225                         if (!qla1280_probe_one(pdev, id))
1226                                 num_hosts++;
1227                 }
1228         }
1229
1230         pdev = NULL;
1231         /* Try and find each different type of adapter we support */
1232         for (id = &qla1280_pci_tbl[0]; id->device; id++) {
1233                 while ((pdev = pci_find_device(id->vendor, id->device, pdev))) {
1234                         /*
1235                          * skip QLA12160 already initialized on
1236                          * PCI Bus 1 Dev 2 since we already initialized
1237                          * and presented it
1238                          */
1239                         if (id->device == PCI_DEVICE_ID_QLOGIC_ISP12160 &&
1240                             pdev->bus->number == 1 &&
1241                             PCI_SLOT(pdev->devfn) == 2)
1242                                 continue;
1243
1244                         if (!qla1280_probe_one(pdev, id))
1245                                 num_hosts++;
1246                 }
1247         }
1248
1249         return num_hosts;
1250 }
1251
1252 /*
1253  * This looks a bit ugly as we could just pass down host to
1254  * qla1280_remove_one, but I want to keep qla1280_release purely a wrapper
1255  * around pci_driver::remove as used from 2.6 onwards.
1256  */
1257 static int
1258 qla1280_release(struct Scsi_Host *host)
1259 {
1260         struct scsi_qla_host *ha = (struct scsi_qla_host *)host->hostdata;
1261
1262         qla1280_remove_one(ha->pdev);
1263         return 0;
1264 }
1265
1266 static int
1267 qla1280_biosparam_old(Disk * disk, kdev_t dev, int geom[])
1268 {
1269         return qla1280_biosparam(disk->device, NULL, disk->capacity, geom);
1270 }
1271 #endif
1272
1273 /**************************************************************************
1274  * qla1280_intr_handler
1275  *   Handles the H/W interrupt
1276  **************************************************************************/
1277 static irqreturn_t
1278 qla1280_intr_handler(int irq, void *dev_id, struct pt_regs *regs)
1279 {
1280         struct scsi_qla_host *ha;
1281         struct device_reg __iomem *reg;
1282         u16 data;
1283         int handled = 0;
1284
1285         ENTER_INTR ("qla1280_intr_handler");
1286         ha = (struct scsi_qla_host *)dev_id;
1287
1288         spin_lock(HOST_LOCK);
1289
1290         ha->isr_count++;
1291         reg = ha->iobase;
1292
1293         WRT_REG_WORD(&reg->ictrl, 0);   /* disable our interrupt. */
1294
1295         data = qla1280_debounce_register(&reg->istatus);
1296         /* Check for pending interrupts. */
1297         if (data & RISC_INT) {  
1298                 qla1280_isr(ha, &ha->done_q);
1299                 handled = 1;
1300         }
1301         if (!list_empty(&ha->done_q))
1302                 qla1280_done(ha);
1303
1304         spin_unlock(HOST_LOCK);
1305
1306         /* enable our interrupt. */
1307         WRT_REG_WORD(&reg->ictrl, (ISP_EN_INT | ISP_EN_RISC));
1308
1309         LEAVE_INTR("qla1280_intr_handler");
1310         return IRQ_RETVAL(handled);
1311 }
1312
1313
1314 static int
1315 qla1280_set_target_parameters(struct scsi_qla_host *ha, int bus, int target)
1316 {
1317         uint8_t mr;
1318         uint16_t mb[MAILBOX_REGISTER_COUNT];
1319         struct nvram *nv;
1320         int status;
1321
1322         nv = &ha->nvram;
1323
1324         mr = BIT_3 | BIT_2 | BIT_1 | BIT_0;
1325
1326         /* Set Target Parameters. */
1327         mb[0] = MBC_SET_TARGET_PARAMETERS;
1328         mb[1] = (uint16_t) (bus ? target | BIT_7 : target);
1329         mb[1] <<= 8;
1330
1331         mb[2] = (nv->bus[bus].target[target].parameter.c << 8);
1332
1333         if (IS_ISP1x160(ha)) {
1334                 mb[2] |= nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr << 5;
1335                 mb[3] = (nv->bus[bus].target[target].flags.flags1x160.sync_offset << 8) |
1336                          nv->bus[bus].target[target].sync_period;
1337                 mb[6] = (nv->bus[bus].target[target].ppr_1x160.flags.ppr_options << 8) |
1338                          nv->bus[bus].target[target].ppr_1x160.flags.ppr_bus_width;
1339                 mr |= BIT_6;
1340         } else {
1341                 mb[3] = (nv->bus[bus].target[target].flags.flags1x80.sync_offset << 8) |
1342                          nv->bus[bus].target[target].sync_period;
1343         }
1344
1345         status = qla1280_mailbox_command(ha, mr, &mb[0]);
1346
1347         if (status)
1348                 printk(KERN_WARNING "scsi(%ld:%i:%i): "
1349                        "qla1280_set_target_parameters() failed\n",
1350                        ha->host_no, bus, target);
1351         return status;
1352 }
1353
1354
1355 /**************************************************************************
1356  *   qla1280_slave_configure
1357  *
1358  * Description:
1359  *   Determines the queue depth for a given device.  There are two ways
1360  *   a queue depth can be obtained for a tagged queueing device.  One
1361  *   way is the default queue depth which is determined by whether
1362  *   If it is defined, then it is used
1363  *   as the default queue depth.  Otherwise, we use either 4 or 8 as the
1364  *   default queue depth (dependent on the number of hardware SCBs).
1365  **************************************************************************/
1366 static int
1367 qla1280_slave_configure(struct scsi_device *device)
1368 {
1369         struct scsi_qla_host *ha;
1370         int default_depth = 3;
1371         int bus = device->channel;
1372         int target = device->id;
1373         int status = 0;
1374         struct nvram *nv;
1375         unsigned long flags;
1376
1377         ha = (struct scsi_qla_host *)device->host->hostdata;
1378         nv = &ha->nvram;
1379
1380         if (qla1280_check_for_dead_scsi_bus(ha, bus))
1381                 return 1;
1382
1383         if (device->tagged_supported &&
1384             (ha->bus_settings[bus].qtag_enables & (BIT_0 << target))) {
1385                 scsi_adjust_queue_depth(device, MSG_ORDERED_TAG,
1386                                         ha->bus_settings[bus].hiwat);
1387         } else {
1388                 scsi_adjust_queue_depth(device, 0, default_depth);
1389         }
1390
1391 #if LINUX_VERSION_CODE > 0x020500
1392         nv->bus[bus].target[target].parameter.f.enable_sync = device->sdtr;
1393         nv->bus[bus].target[target].parameter.f.enable_wide = device->wdtr;
1394         nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr = device->ppr;
1395 #endif
1396
1397         if (driver_setup.no_sync ||
1398             (driver_setup.sync_mask &&
1399              (~driver_setup.sync_mask & (1 << target))))
1400                 nv->bus[bus].target[target].parameter.f.enable_sync = 0;
1401         if (driver_setup.no_wide ||
1402             (driver_setup.wide_mask &&
1403              (~driver_setup.wide_mask & (1 << target))))
1404                 nv->bus[bus].target[target].parameter.f.enable_wide = 0;
1405         if (IS_ISP1x160(ha)) {
1406                 if (driver_setup.no_ppr ||
1407                     (driver_setup.ppr_mask &&
1408                      (~driver_setup.ppr_mask & (1 << target))))
1409                         nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr = 0;
1410         }
1411
1412         spin_lock_irqsave(HOST_LOCK, flags);
1413         if (nv->bus[bus].target[target].parameter.f.enable_sync)
1414                 status = qla1280_set_target_parameters(ha, bus, target);
1415         qla1280_get_target_parameters(ha, device);
1416         spin_unlock_irqrestore(HOST_LOCK, flags);
1417         return status;
1418 }
1419
1420 #if LINUX_VERSION_CODE < 0x020545
1421 /**************************************************************************
1422  *   qla1280_select_queue_depth
1423  *
1424  *   Sets the queue depth for each SCSI device hanging off the input
1425  *   host adapter.  We use a queue depth of 2 for devices that do not
1426  *   support tagged queueing.
1427  **************************************************************************/
1428 static void
1429 qla1280_select_queue_depth(struct Scsi_Host *host, struct scsi_device *sdev_q)
1430 {
1431         struct scsi_qla_host *ha = (struct scsi_qla_host *)host->hostdata;
1432         struct scsi_device *sdev;
1433
1434         ENTER("qla1280_select_queue_depth");
1435         for (sdev = sdev_q; sdev; sdev = sdev->next)
1436                 if (sdev->host == host)
1437                         qla1280_slave_configure(sdev);
1438
1439         if (sdev_q)
1440                 qla1280_check_for_dead_scsi_bus(ha, sdev_q->channel);
1441         LEAVE("qla1280_select_queue_depth");
1442 }
1443 #endif
1444
1445 /*
1446  * qla1280_done
1447  *      Process completed commands.
1448  *
1449  * Input:
1450  *      ha           = adapter block pointer.
1451  *      done_q       = done queue.
1452  */
1453 static void
1454 qla1280_done(struct scsi_qla_host *ha)
1455 {
1456         struct srb *sp;
1457         struct list_head *done_q;
1458         int bus, target, lun;
1459         struct scsi_cmnd *cmd;
1460
1461         ENTER("qla1280_done");
1462
1463         done_q = &ha->done_q;
1464
1465         while (!list_empty(done_q)) {
1466                 sp = list_entry(done_q->next, struct srb, list);
1467
1468                 list_del(&sp->list);
1469         
1470                 cmd = sp->cmd;
1471                 bus = SCSI_BUS_32(cmd);
1472                 target = SCSI_TCN_32(cmd);
1473                 lun = SCSI_LUN_32(cmd);
1474
1475                 switch ((CMD_RESULT(cmd) >> 16)) {
1476                 case DID_RESET:
1477                         /* Issue marker command. */
1478                         qla1280_marker(ha, bus, target, 0, MK_SYNC_ID);
1479                         break;
1480                 case DID_ABORT:
1481                         sp->flags &= ~SRB_ABORT_PENDING;
1482                         sp->flags |= SRB_ABORTED;
1483                         if (sp->flags & SRB_TIMEOUT)
1484                                 CMD_RESULT(sp->cmd) = DID_TIME_OUT << 16;
1485                         break;
1486                 default:
1487                         break;
1488                 }
1489
1490                 /* Release memory used for this I/O */
1491                 if (cmd->use_sg) {
1492                         pci_unmap_sg(ha->pdev, cmd->request_buffer,
1493                                         cmd->use_sg, cmd->sc_data_direction);
1494                 } else if (cmd->request_bufflen) {
1495                         pci_unmap_single(ha->pdev, sp->saved_dma_handle,
1496                                         cmd->request_bufflen,
1497                                         cmd->sc_data_direction);
1498                 }
1499
1500                 /* Call the mid-level driver interrupt handler */
1501                 CMD_HANDLE(sp->cmd) = (unsigned char *)INVALID_HANDLE;
1502                 ha->actthreads--;
1503
1504 #if LINUX_VERSION_CODE < 0x020500
1505                 if (cmd->cmnd[0] == INQUIRY)
1506                         qla1280_get_target_options(cmd, ha);
1507 #endif
1508                 (*(cmd)->scsi_done)(cmd);
1509
1510                 if(sp->wait != NULL)
1511                         complete(sp->wait);
1512         }
1513         LEAVE("qla1280_done");
1514 }
1515
1516 /*
1517  * Translates a ISP error to a Linux SCSI error
1518  */
1519 static int
1520 qla1280_return_status(struct response * sts, struct scsi_cmnd *cp)
1521 {
1522         int host_status = DID_ERROR;
1523         uint16_t comp_status = le16_to_cpu(sts->comp_status);
1524         uint16_t state_flags = le16_to_cpu(sts->state_flags);
1525         uint16_t residual_length = le16_to_cpu(sts->residual_length);
1526         uint16_t scsi_status = le16_to_cpu(sts->scsi_status);
1527 #if DEBUG_QLA1280_INTR
1528         static char *reason[] = {
1529                 "DID_OK",
1530                 "DID_NO_CONNECT",
1531                 "DID_BUS_BUSY",
1532                 "DID_TIME_OUT",
1533                 "DID_BAD_TARGET",
1534                 "DID_ABORT",
1535                 "DID_PARITY",
1536                 "DID_ERROR",
1537                 "DID_RESET",
1538                 "DID_BAD_INTR"
1539         };
1540 #endif                          /* DEBUG_QLA1280_INTR */
1541
1542         ENTER("qla1280_return_status");
1543
1544 #if DEBUG_QLA1280_INTR
1545         /*
1546           dprintk(1, "qla1280_return_status: compl status = 0x%04x\n",
1547           comp_status);
1548         */
1549 #endif
1550
1551         switch (comp_status) {
1552         case CS_COMPLETE:
1553                 host_status = DID_OK;
1554                 break;
1555
1556         case CS_INCOMPLETE:
1557                 if (!(state_flags & SF_GOT_BUS))
1558                         host_status = DID_NO_CONNECT;
1559                 else if (!(state_flags & SF_GOT_TARGET))
1560                         host_status = DID_BAD_TARGET;
1561                 else if (!(state_flags & SF_SENT_CDB))
1562                         host_status = DID_ERROR;
1563                 else if (!(state_flags & SF_TRANSFERRED_DATA))
1564                         host_status = DID_ERROR;
1565                 else if (!(state_flags & SF_GOT_STATUS))
1566                         host_status = DID_ERROR;
1567                 else if (!(state_flags & SF_GOT_SENSE))
1568                         host_status = DID_ERROR;
1569                 break;
1570
1571         case CS_RESET:
1572                 host_status = DID_RESET;
1573                 break;
1574
1575         case CS_ABORTED:
1576                 host_status = DID_ABORT;
1577                 break;
1578
1579         case CS_TIMEOUT:
1580                 host_status = DID_TIME_OUT;
1581                 break;
1582
1583         case CS_DATA_OVERRUN:
1584                 dprintk(2, "Data overrun 0x%x\n", residual_length);
1585                 dprintk(2, "qla1280_isr: response packet data\n");
1586                 qla1280_dump_buffer(2, (char *)sts, RESPONSE_ENTRY_SIZE);
1587                 host_status = DID_ERROR;
1588                 break;
1589
1590         case CS_DATA_UNDERRUN:
1591                 if ((cp->request_bufflen - residual_length) <
1592                     cp->underflow) {
1593                         printk(KERN_WARNING
1594                                "scsi: Underflow detected - retrying "
1595                                "command.\n");
1596                         host_status = DID_ERROR;
1597                 } else
1598                         host_status = DID_OK;
1599                 break;
1600
1601         default:
1602                 host_status = DID_ERROR;
1603                 break;
1604         }
1605
1606 #if DEBUG_QLA1280_INTR
1607         dprintk(1, "qla1280 ISP status: host status (%s) scsi status %x\n",
1608                 reason[host_status], scsi_status);
1609 #endif
1610
1611         LEAVE("qla1280_return_status");
1612
1613         return (scsi_status & 0xff) | (host_status << 16);
1614 }
1615
1616 /****************************************************************************/
1617 /*                QLogic ISP1280 Hardware Support Functions.                */
1618 /****************************************************************************/
1619
1620  /*
1621   * qla2100_enable_intrs
1622   * qla2100_disable_intrs
1623   *
1624   * Input:
1625   *      ha = adapter block pointer.
1626   *
1627   * Returns:
1628   *      None
1629   */
1630 static inline void
1631 qla1280_enable_intrs(struct scsi_qla_host *ha)
1632 {
1633         struct device_reg __iomem *reg;
1634
1635         reg = ha->iobase;
1636         /* enable risc and host interrupts */
1637         WRT_REG_WORD(&reg->ictrl, (ISP_EN_INT | ISP_EN_RISC));
1638         RD_REG_WORD(&reg->ictrl);       /* PCI Posted Write flush */
1639         ha->flags.ints_enabled = 1;
1640 }
1641
1642 static inline void
1643 qla1280_disable_intrs(struct scsi_qla_host *ha)
1644 {
1645         struct device_reg __iomem *reg;
1646
1647         reg = ha->iobase;
1648         /* disable risc and host interrupts */
1649         WRT_REG_WORD(&reg->ictrl, 0);
1650         RD_REG_WORD(&reg->ictrl);       /* PCI Posted Write flush */
1651         ha->flags.ints_enabled = 0;
1652 }
1653
1654 /*
1655  * qla1280_initialize_adapter
1656  *      Initialize board.
1657  *
1658  * Input:
1659  *      ha = adapter block pointer.
1660  *
1661  * Returns:
1662  *      0 = success
1663  */
1664 static int __devinit
1665 qla1280_initialize_adapter(struct scsi_qla_host *ha)
1666 {
1667         struct device_reg __iomem *reg;
1668         int status;
1669         int bus;
1670 #if LINUX_VERSION_CODE > 0x020500
1671         unsigned long flags;
1672 #endif
1673
1674         ENTER("qla1280_initialize_adapter");
1675
1676         /* Clear adapter flags. */
1677         ha->flags.online = 0;
1678         ha->flags.disable_host_adapter = 0;
1679         ha->flags.reset_active = 0;
1680         ha->flags.abort_isp_active = 0;
1681
1682         ha->flags.ints_enabled = 0;
1683 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1684         if (ia64_platform_is("sn2")) {
1685                 printk(KERN_INFO "scsi(%li): Enabling SN2 PCI DMA "
1686                        "dual channel lockup workaround\n", ha->host_no);
1687                 ha->flags.use_pci_vchannel = 1;
1688                 driver_setup.no_nvram = 1;
1689         }
1690 #endif
1691
1692         /* TODO: implement support for the 1040 nvram format */
1693         if (IS_ISP1040(ha))
1694                 driver_setup.no_nvram = 1;
1695
1696         dprintk(1, "Configure PCI space for adapter...\n");
1697
1698         reg = ha->iobase;
1699
1700         /* Insure mailbox registers are free. */
1701         WRT_REG_WORD(&reg->semaphore, 0);
1702         WRT_REG_WORD(&reg->host_cmd, HC_CLR_RISC_INT);
1703         WRT_REG_WORD(&reg->host_cmd, HC_CLR_HOST_INT);
1704         RD_REG_WORD(&reg->host_cmd);
1705
1706         if (qla1280_read_nvram(ha)) {
1707                 dprintk(2, "qla1280_initialize_adapter: failed to read "
1708                         "NVRAM\n");
1709         }
1710
1711 #if LINUX_VERSION_CODE >= 0x020500
1712         /*
1713          * It's necessary to grab the spin here as qla1280_mailbox_command
1714          * needs to be able to drop the lock unconditionally to wait
1715          * for completion.
1716          * In 2.4 ->detect is called with the io_request_lock held.
1717          */
1718         spin_lock_irqsave(HOST_LOCK, flags);
1719 #endif
1720
1721         status = qla1280_load_firmware(ha);
1722         if (status) {
1723                 printk(KERN_ERR "scsi(%li): initialize: pci probe failed!\n",
1724                        ha->host_no);
1725                 goto out;
1726         }
1727
1728         /* Setup adapter based on NVRAM parameters. */
1729         dprintk(1, "scsi(%ld): Configure NVRAM parameters\n", ha->host_no);
1730         qla1280_nvram_config(ha);
1731
1732         if (ha->flags.disable_host_adapter) {
1733                 status = 1;
1734                 goto out;
1735         }
1736
1737         status = qla1280_init_rings(ha);
1738         if (status)
1739                 goto out;
1740
1741         /* Issue SCSI reset, if we can't reset twice then bus is dead */
1742         for (bus = 0; bus < ha->ports; bus++) {
1743                 if (!ha->bus_settings[bus].disable_scsi_reset &&
1744                     qla1280_bus_reset(ha, bus) &&
1745                     qla1280_bus_reset(ha, bus))
1746                         ha->bus_settings[bus].scsi_bus_dead = 1;
1747         }
1748
1749         ha->flags.online = 1;
1750  out:
1751 #if LINUX_VERSION_CODE >= 0x020500
1752         spin_unlock_irqrestore(HOST_LOCK, flags);
1753 #endif
1754         if (status)
1755                 dprintk(2, "qla1280_initialize_adapter: **** FAILED ****\n");
1756
1757         LEAVE("qla1280_initialize_adapter");
1758         return status;
1759 }
1760
1761
1762 /*
1763  * ISP Firmware Test
1764  *      Checks if present version of RISC firmware is older than
1765  *      driver firmware.
1766  *
1767  * Input:
1768  *      ha = adapter block pointer.
1769  *
1770  * Returns:
1771  *      0 = firmware does not need to be loaded.
1772  */
1773 static int
1774 qla1280_isp_firmware(struct scsi_qla_host *ha)
1775 {
1776         struct nvram *nv = (struct nvram *) ha->response_ring;
1777         int status = 0;         /* dg 2/27 always loads RISC */
1778         uint16_t mb[MAILBOX_REGISTER_COUNT];
1779
1780         ENTER("qla1280_isp_firmware");
1781
1782         dprintk(1, "scsi(%li): Determining if RISC is loaded\n", ha->host_no);
1783
1784         /* Bad NVRAM data, load RISC code. */
1785         if (!ha->nvram_valid) {
1786                 ha->flags.disable_risc_code_load = 0;
1787         } else
1788                 ha->flags.disable_risc_code_load =
1789                         nv->cntr_flags_1.disable_loading_risc_code;
1790
1791         if (ha->flags.disable_risc_code_load) {
1792                 dprintk(3, "qla1280_isp_firmware: Telling RISC to verify "
1793                         "checksum of loaded BIOS code.\n");
1794
1795                 /* Verify checksum of loaded RISC code. */
1796                 mb[0] = MBC_VERIFY_CHECKSUM;
1797                 /* mb[1] = ql12_risc_code_addr01; */
1798                 mb[1] = *ql1280_board_tbl[ha->devnum].fwstart;
1799
1800                 if (!(status =
1801                       qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]))) {
1802                         /* Start firmware execution. */
1803                         dprintk(3, "qla1280_isp_firmware: Startng F/W "
1804                                 "execution.\n");
1805
1806                         mb[0] = MBC_EXECUTE_FIRMWARE;
1807                         /* mb[1] = ql12_risc_code_addr01; */
1808                         mb[1] = *ql1280_board_tbl[ha->devnum].fwstart;
1809                         qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
1810                 } else
1811                         printk(KERN_INFO "qla1280: RISC checksum failed.\n");
1812         } else {
1813                 dprintk(1, "qla1280: NVRAM configured to load RISC load.\n");
1814                 status = 1;
1815         }
1816
1817         if (status)
1818                 dprintk(2, "qla1280_isp_firmware: **** Load RISC code ****\n");
1819
1820         LEAVE("qla1280_isp_firmware");
1821         return status;
1822 }
1823
1824 /*
1825  * Chip diagnostics
1826  *      Test chip for proper operation.
1827  *
1828  * Input:
1829  *      ha = adapter block pointer.
1830  *
1831  * Returns:
1832  *      0 = success.
1833  */
1834 static int
1835 qla1280_chip_diag(struct scsi_qla_host *ha)
1836 {
1837         uint16_t mb[MAILBOX_REGISTER_COUNT];
1838         struct device_reg __iomem *reg = ha->iobase;
1839         int status = 0;
1840         int cnt;
1841         uint16_t data;
1842         dprintk(3, "qla1280_chip_diag: testing device at 0x%p \n", &reg->id_l);
1843
1844         dprintk(1, "scsi(%ld): Verifying chip\n", ha->host_no);
1845
1846         /* Soft reset chip and wait for it to finish. */
1847         WRT_REG_WORD(&reg->ictrl, ISP_RESET);
1848
1849         /*
1850          * We can't do a traditional PCI write flush here by reading
1851          * back the register. The card will not respond once the reset
1852          * is in action and we end up with a machine check exception
1853          * instead. Nothing to do but wait and hope for the best.
1854          * A portable pci_write_flush(pdev) call would be very useful here.
1855          */
1856         udelay(20);
1857         data = qla1280_debounce_register(&reg->ictrl);
1858         /*
1859          * Yet another QLogic gem ;-(
1860          */
1861         for (cnt = 1000000; cnt && data & ISP_RESET; cnt--) {
1862                 udelay(5);
1863                 data = RD_REG_WORD(&reg->ictrl);
1864         }
1865
1866         if (!cnt)
1867                 goto fail;
1868
1869         /* Reset register cleared by chip reset. */
1870         dprintk(3, "qla1280_chip_diag: reset register cleared by chip reset\n");
1871
1872         WRT_REG_WORD(&reg->cfg_1, 0);
1873
1874         /* Reset RISC and disable BIOS which
1875            allows RISC to execute out of RAM. */
1876         WRT_REG_WORD(&reg->host_cmd, HC_RESET_RISC |
1877                      HC_RELEASE_RISC | HC_DISABLE_BIOS);
1878
1879         RD_REG_WORD(&reg->id_l);        /* Flush PCI write */
1880         data = qla1280_debounce_register(&reg->mailbox0);
1881
1882         /*
1883          * I *LOVE* this code!
1884          */
1885         for (cnt = 1000000; cnt && data == MBS_BUSY; cnt--) {
1886                 udelay(5);
1887                 data = RD_REG_WORD(&reg->mailbox0);
1888         }
1889
1890         if (!cnt)
1891                 goto fail;
1892
1893         /* Check product ID of chip */
1894         dprintk(3, "qla1280_chip_diag: Checking product ID of chip\n");
1895
1896         if (RD_REG_WORD(&reg->mailbox1) != PROD_ID_1 ||
1897             (RD_REG_WORD(&reg->mailbox2) != PROD_ID_2 &&
1898              RD_REG_WORD(&reg->mailbox2) != PROD_ID_2a) ||
1899             RD_REG_WORD(&reg->mailbox3) != PROD_ID_3 ||
1900             RD_REG_WORD(&reg->mailbox4) != PROD_ID_4) {
1901                 printk(KERN_INFO "qla1280: Wrong product ID = "
1902                        "0x%x,0x%x,0x%x,0x%x\n",
1903                        RD_REG_WORD(&reg->mailbox1),
1904                        RD_REG_WORD(&reg->mailbox2),
1905                        RD_REG_WORD(&reg->mailbox3),
1906                        RD_REG_WORD(&reg->mailbox4));
1907                 goto fail;
1908         }
1909
1910         /*
1911          * Enable ints early!!!
1912          */
1913         qla1280_enable_intrs(ha);
1914
1915         dprintk(1, "qla1280_chip_diag: Checking mailboxes of chip\n");
1916         /* Wrap Incoming Mailboxes Test. */
1917         mb[0] = MBC_MAILBOX_REGISTER_TEST;
1918         mb[1] = 0xAAAA;
1919         mb[2] = 0x5555;
1920         mb[3] = 0xAA55;
1921         mb[4] = 0x55AA;
1922         mb[5] = 0xA5A5;
1923         mb[6] = 0x5A5A;
1924         mb[7] = 0x2525;
1925
1926         status = qla1280_mailbox_command(ha, 0xff, mb);
1927         if (status)
1928                 goto fail;
1929
1930         if (mb[1] != 0xAAAA || mb[2] != 0x5555 || mb[3] != 0xAA55 ||
1931             mb[4] != 0x55AA || mb[5] != 0xA5A5 || mb[6] != 0x5A5A ||
1932             mb[7] != 0x2525) {
1933                 printk(KERN_INFO "qla1280: Failed mbox check\n");
1934                 goto fail;
1935         }
1936
1937         dprintk(3, "qla1280_chip_diag: exiting normally\n");
1938         return 0;
1939  fail:
1940         dprintk(2, "qla1280_chip_diag: **** FAILED ****\n");
1941         return status;
1942 }
1943
1944 static int
1945 qla1280_load_firmware_pio(struct scsi_qla_host *ha)
1946 {
1947         uint16_t risc_address, *risc_code_address, risc_code_size;
1948         uint16_t mb[MAILBOX_REGISTER_COUNT], i;
1949         int err;
1950
1951         /* Load RISC code. */
1952         risc_address = *ql1280_board_tbl[ha->devnum].fwstart;
1953         risc_code_address = ql1280_board_tbl[ha->devnum].fwcode;
1954         risc_code_size = *ql1280_board_tbl[ha->devnum].fwlen;
1955
1956         for (i = 0; i < risc_code_size; i++) {
1957                 mb[0] = MBC_WRITE_RAM_WORD;
1958                 mb[1] = risc_address + i;
1959                 mb[2] = risc_code_address[i];
1960
1961                 err = qla1280_mailbox_command(ha, BIT_0 | BIT_1 | BIT_2, mb);
1962                 if (err) {
1963                         printk(KERN_ERR "scsi(%li): Failed to load firmware\n",
1964                                         ha->host_no);
1965                         return err;
1966                 }
1967         }
1968
1969         return 0;
1970 }
1971
1972 #define DUMP_IT_BACK 0          /* for debug of RISC loading */
1973 static int
1974 qla1280_load_firmware_dma(struct scsi_qla_host *ha)
1975 {
1976         uint16_t risc_address, *risc_code_address, risc_code_size;
1977         uint16_t mb[MAILBOX_REGISTER_COUNT], cnt;
1978         int err = 0, num, i;
1979 #if DUMP_IT_BACK
1980         uint8_t *sp, *tbuf;
1981         dma_addr_t p_tbuf;
1982
1983         tbuf = pci_alloc_consistent(ha->pdev, 8000, &p_tbuf);
1984         if (!tbuf)
1985                 return -ENOMEM;
1986 #endif
1987
1988         /* Load RISC code. */
1989         risc_address = *ql1280_board_tbl[ha->devnum].fwstart;
1990         risc_code_address = ql1280_board_tbl[ha->devnum].fwcode;
1991         risc_code_size = *ql1280_board_tbl[ha->devnum].fwlen;
1992
1993         dprintk(1, "%s: DMA RISC code (%i) words\n",
1994                         __FUNCTION__, risc_code_size);
1995
1996         num = 0;
1997         while (risc_code_size > 0) {
1998                 int warn __attribute__((unused)) = 0;
1999
2000                 cnt = 2000 >> 1;
2001
2002                 if (cnt > risc_code_size)
2003                         cnt = risc_code_size;
2004
2005                 dprintk(2, "qla1280_setup_chip:  loading risc @ =(0x%p),"
2006                         "%d,%d(0x%x)\n",
2007                         risc_code_address, cnt, num, risc_address);
2008                 for(i = 0; i < cnt; i++)
2009                         ((uint16_t *)ha->request_ring)[i] =
2010                                 cpu_to_le16(risc_code_address[i]);
2011
2012                 mb[0] = MBC_LOAD_RAM;
2013                 mb[1] = risc_address;
2014                 mb[4] = cnt;
2015                 mb[3] = ha->request_dma & 0xffff;
2016                 mb[2] = (ha->request_dma >> 16) & 0xffff;
2017                 mb[7] = pci_dma_hi32(ha->request_dma) & 0xffff;
2018                 mb[6] = pci_dma_hi32(ha->request_dma) >> 16;
2019                 dprintk(2, "%s: op=%d  0x%p = 0x%4x,0x%4x,0x%4x,0x%4x\n",
2020                                 __FUNCTION__, mb[0],
2021                                 (void *)(long)ha->request_dma,
2022                                 mb[6], mb[7], mb[2], mb[3]);
2023                 err = qla1280_mailbox_command(ha, BIT_4 | BIT_3 | BIT_2 |
2024                                 BIT_1 | BIT_0, mb);
2025                 if (err) {
2026                         printk(KERN_ERR "scsi(%li): Failed to load partial "
2027                                "segment of f\n", ha->host_no);
2028                         goto out;
2029                 }
2030
2031 #if DUMP_IT_BACK
2032                 mb[0] = MBC_DUMP_RAM;
2033                 mb[1] = risc_address;
2034                 mb[4] = cnt;
2035                 mb[3] = p_tbuf & 0xffff;
2036                 mb[2] = (p_tbuf >> 16) & 0xffff;
2037                 mb[7] = pci_dma_hi32(p_tbuf) & 0xffff;
2038                 mb[6] = pci_dma_hi32(p_tbuf) >> 16;
2039
2040                 err = qla1280_mailbox_command(ha, BIT_4 | BIT_3 | BIT_2 |
2041                                 BIT_1 | BIT_0, mb);
2042                 if (err) {
2043                         printk(KERN_ERR
2044                                "Failed to dump partial segment of f/w\n");
2045                         goto out;
2046                 }
2047                 sp = (uint8_t *)ha->request_ring;
2048                 for (i = 0; i < (cnt << 1); i++) {
2049                         if (tbuf[i] != sp[i] && warn++ < 10) {
2050                                 printk(KERN_ERR "%s: FW compare error @ "
2051                                                 "byte(0x%x) loop#=%x\n",
2052                                                 __FUNCTION__, i, num);
2053                                 printk(KERN_ERR "%s: FWbyte=%x  "
2054                                                 "FWfromChip=%x\n",
2055                                                 __FUNCTION__, sp[i], tbuf[i]);
2056                                 /*break; */
2057                         }
2058                 }
2059 #endif
2060                 risc_address += cnt;
2061                 risc_code_size = risc_code_size - cnt;
2062                 risc_code_address = risc_code_address + cnt;
2063                 num++;
2064         }
2065
2066  out:
2067 #if DUMP_IT_BACK
2068         pci_free_consistent(ha->pdev, 8000, tbuf, p_tbuf);
2069 #endif
2070         return err;
2071 }
2072
2073 static int
2074 qla1280_start_firmware(struct scsi_qla_host *ha)
2075 {
2076         uint16_t mb[MAILBOX_REGISTER_COUNT];
2077         int err;
2078
2079         dprintk(1, "%s: Verifying checksum of loaded RISC code.\n",
2080                         __FUNCTION__);
2081
2082         /* Verify checksum of loaded RISC code. */
2083         mb[0] = MBC_VERIFY_CHECKSUM;
2084         /* mb[1] = ql12_risc_code_addr01; */
2085         mb[1] = *ql1280_board_tbl[ha->devnum].fwstart;
2086         err = qla1280_mailbox_command(ha, BIT_1 | BIT_0, mb);
2087         if (err) {
2088                 printk(KERN_ERR "scsi(%li): Failed checksum\n", ha->host_no);
2089                 return err;
2090         }
2091
2092         /* Start firmware execution. */
2093         dprintk(1, "%s: start firmware running.\n", __FUNCTION__);
2094         mb[0] = MBC_EXECUTE_FIRMWARE;
2095         mb[1] = *ql1280_board_tbl[ha->devnum].fwstart;
2096         err = qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
2097         if (err) {
2098                 printk(KERN_ERR "scsi(%li): Failed to start firmware\n",
2099                                 ha->host_no);
2100         }
2101
2102         return err;
2103 }
2104
2105 static int
2106 qla1280_load_firmware(struct scsi_qla_host *ha)
2107 {
2108         int err = -ENODEV;
2109
2110         /* If firmware needs to be loaded */
2111         if (!qla1280_isp_firmware(ha)) {
2112                 printk(KERN_ERR "scsi(%li): isp_firmware() failed!\n",
2113                                 ha->host_no);
2114                 goto out;
2115         }
2116
2117         err = qla1280_chip_diag(ha);
2118         if (err)
2119                 goto out;
2120         if (IS_ISP1040(ha))
2121                 err = qla1280_load_firmware_pio(ha);
2122         else
2123                 err = qla1280_load_firmware_dma(ha);
2124         if (err)
2125                 goto out;
2126         err = qla1280_start_firmware(ha);
2127  out:
2128         return err;
2129 }
2130
2131 /*
2132  * Initialize rings
2133  *
2134  * Input:
2135  *      ha                = adapter block pointer.
2136  *      ha->request_ring  = request ring virtual address
2137  *      ha->response_ring = response ring virtual address
2138  *      ha->request_dma   = request ring physical address
2139  *      ha->response_dma  = response ring physical address
2140  *
2141  * Returns:
2142  *      0 = success.
2143  */
2144 static int
2145 qla1280_init_rings(struct scsi_qla_host *ha)
2146 {
2147         uint16_t mb[MAILBOX_REGISTER_COUNT];
2148         int status = 0;
2149
2150         ENTER("qla1280_init_rings");
2151
2152         /* Clear outstanding commands array. */
2153         memset(ha->outstanding_cmds, 0,
2154                sizeof(struct srb *) * MAX_OUTSTANDING_COMMANDS);
2155
2156         /* Initialize request queue. */
2157         ha->request_ring_ptr = ha->request_ring;
2158         ha->req_ring_index = 0;
2159         ha->req_q_cnt = REQUEST_ENTRY_CNT;
2160         /* mb[0] = MBC_INIT_REQUEST_QUEUE; */
2161         mb[0] = MBC_INIT_REQUEST_QUEUE_A64;
2162         mb[1] = REQUEST_ENTRY_CNT;
2163         mb[3] = ha->request_dma & 0xffff;
2164         mb[2] = (ha->request_dma >> 16) & 0xffff;
2165         mb[4] = 0;
2166         mb[7] = pci_dma_hi32(ha->request_dma) & 0xffff;
2167         mb[6] = pci_dma_hi32(ha->request_dma) >> 16;
2168         if (!(status = qla1280_mailbox_command(ha, BIT_7 | BIT_6 | BIT_4 |
2169                                                BIT_3 | BIT_2 | BIT_1 | BIT_0,
2170                                                &mb[0]))) {
2171                 /* Initialize response queue. */
2172                 ha->response_ring_ptr = ha->response_ring;
2173                 ha->rsp_ring_index = 0;
2174                 /* mb[0] = MBC_INIT_RESPONSE_QUEUE; */
2175                 mb[0] = MBC_INIT_RESPONSE_QUEUE_A64;
2176                 mb[1] = RESPONSE_ENTRY_CNT;
2177                 mb[3] = ha->response_dma & 0xffff;
2178                 mb[2] = (ha->response_dma >> 16) & 0xffff;
2179                 mb[5] = 0;
2180                 mb[7] = pci_dma_hi32(ha->response_dma) & 0xffff;
2181                 mb[6] = pci_dma_hi32(ha->response_dma) >> 16;
2182                 status = qla1280_mailbox_command(ha, BIT_7 | BIT_6 | BIT_5 |
2183                                                  BIT_3 | BIT_2 | BIT_1 | BIT_0,
2184                                                  &mb[0]);
2185         }
2186
2187         if (status)
2188                 dprintk(2, "qla1280_init_rings: **** FAILED ****\n");
2189
2190         LEAVE("qla1280_init_rings");
2191         return status;
2192 }
2193
2194 static void
2195 qla1280_print_settings(struct nvram *nv)
2196 {
2197         dprintk(1, "qla1280 : initiator scsi id bus[0]=%d\n",
2198                 nv->bus[0].config_1.initiator_id);
2199         dprintk(1, "qla1280 : initiator scsi id bus[1]=%d\n",
2200                 nv->bus[1].config_1.initiator_id);
2201
2202         dprintk(1, "qla1280 : bus reset delay[0]=%d\n",
2203                 nv->bus[0].bus_reset_delay);
2204         dprintk(1, "qla1280 : bus reset delay[1]=%d\n",
2205                 nv->bus[1].bus_reset_delay);
2206
2207         dprintk(1, "qla1280 : retry count[0]=%d\n", nv->bus[0].retry_count);
2208         dprintk(1, "qla1280 : retry delay[0]=%d\n", nv->bus[0].retry_delay);
2209         dprintk(1, "qla1280 : retry count[1]=%d\n", nv->bus[1].retry_count);
2210         dprintk(1, "qla1280 : retry delay[1]=%d\n", nv->bus[1].retry_delay);
2211
2212         dprintk(1, "qla1280 : async data setup time[0]=%d\n",
2213                 nv->bus[0].config_2.async_data_setup_time);
2214         dprintk(1, "qla1280 : async data setup time[1]=%d\n",
2215                 nv->bus[1].config_2.async_data_setup_time);
2216
2217         dprintk(1, "qla1280 : req/ack active negation[0]=%d\n",
2218                 nv->bus[0].config_2.req_ack_active_negation);
2219         dprintk(1, "qla1280 : req/ack active negation[1]=%d\n",
2220                 nv->bus[1].config_2.req_ack_active_negation);
2221
2222         dprintk(1, "qla1280 : data line active negation[0]=%d\n",
2223                 nv->bus[0].config_2.data_line_active_negation);
2224         dprintk(1, "qla1280 : data line active negation[1]=%d\n",
2225                 nv->bus[1].config_2.data_line_active_negation);
2226
2227         dprintk(1, "qla1280 : disable loading risc code=%d\n",
2228                 nv->cntr_flags_1.disable_loading_risc_code);
2229
2230         dprintk(1, "qla1280 : enable 64bit addressing=%d\n",
2231                 nv->cntr_flags_1.enable_64bit_addressing);
2232
2233         dprintk(1, "qla1280 : selection timeout limit[0]=%d\n",
2234                 nv->bus[0].selection_timeout);
2235         dprintk(1, "qla1280 : selection timeout limit[1]=%d\n",
2236                 nv->bus[1].selection_timeout);
2237
2238         dprintk(1, "qla1280 : max queue depth[0]=%d\n",
2239                 nv->bus[0].max_queue_depth);
2240         dprintk(1, "qla1280 : max queue depth[1]=%d\n",
2241                 nv->bus[1].max_queue_depth);
2242 }
2243
2244 static void
2245 qla1280_set_target_defaults(struct scsi_qla_host *ha, int bus, int target)
2246 {
2247         struct nvram *nv = &ha->nvram;
2248
2249         nv->bus[bus].target[target].parameter.f.renegotiate_on_error = 1;
2250         nv->bus[bus].target[target].parameter.f.auto_request_sense = 1;
2251         nv->bus[bus].target[target].parameter.f.tag_queuing = 1;
2252         nv->bus[bus].target[target].parameter.f.enable_sync = 1;
2253 #if 1   /* Some SCSI Processors do not seem to like this */
2254         nv->bus[bus].target[target].parameter.f.enable_wide = 1;
2255 #endif
2256         nv->bus[bus].target[target].parameter.f.parity_checking = 1;
2257         nv->bus[bus].target[target].parameter.f.disconnect_allowed = 1;
2258         nv->bus[bus].target[target].execution_throttle =
2259                 nv->bus[bus].max_queue_depth - 1;
2260
2261         if (IS_ISP1x160(ha)) {
2262                 nv->bus[bus].target[target].flags.flags1x160.device_enable = 1;
2263                 nv->bus[bus].target[target].flags.flags1x160.sync_offset = 0x0e;
2264                 nv->bus[bus].target[target].sync_period = 9;
2265                 nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr = 1;
2266                 nv->bus[bus].target[target].ppr_1x160.flags.ppr_options = 2;
2267                 nv->bus[bus].target[target].ppr_1x160.flags.ppr_bus_width = 1;
2268         } else {
2269                 nv->bus[bus].target[target].flags.flags1x80.device_enable = 1;
2270                 nv->bus[bus].target[target].flags.flags1x80.sync_offset = 12;
2271                 nv->bus[bus].target[target].sync_period = 10;
2272         }
2273 }
2274
2275 static void
2276 qla1280_set_defaults(struct scsi_qla_host *ha)
2277 {
2278         struct nvram *nv = &ha->nvram;
2279         int bus, target;
2280
2281         dprintk(1, "Using defaults for NVRAM: \n");
2282         memset(nv, 0, sizeof(struct nvram));
2283
2284         /* nv->cntr_flags_1.disable_loading_risc_code = 1; */
2285         nv->firmware_feature.f.enable_fast_posting = 1;
2286         nv->firmware_feature.f.disable_synchronous_backoff = 1;
2287         nv->termination.f.scsi_bus_0_control = 3;
2288         nv->termination.f.scsi_bus_1_control = 3;
2289         nv->termination.f.auto_term_support = 1;
2290
2291         /*
2292          * Set default FIFO magic - What appropriate values would be here
2293          * is unknown. This is what I have found testing with 12160s.
2294          *
2295          * Now, I would love the magic decoder ring for this one, the
2296          * header file provided by QLogic seems to be bogus or incomplete
2297          * at best.
2298          */
2299         nv->isp_config.c = ISP_CFG1_BENAB|ISP_CFG1_F128;
2300         if (IS_ISP1x160(ha))
2301                 nv->isp_parameter = 0x01; /* fast memory enable */
2302
2303         for (bus = 0; bus < MAX_BUSES; bus++) {
2304                 nv->bus[bus].config_1.initiator_id = 7;
2305                 nv->bus[bus].config_2.req_ack_active_negation = 1;
2306                 nv->bus[bus].config_2.data_line_active_negation = 1;
2307                 nv->bus[bus].selection_timeout = 250;
2308                 nv->bus[bus].max_queue_depth = 256;
2309
2310                 if (IS_ISP1040(ha)) {
2311                         nv->bus[bus].bus_reset_delay = 3;
2312                         nv->bus[bus].config_2.async_data_setup_time = 6;
2313                         nv->bus[bus].retry_delay = 1;
2314                 } else {
2315                         nv->bus[bus].bus_reset_delay = 5;
2316                         nv->bus[bus].config_2.async_data_setup_time = 8;
2317                 }
2318
2319                 for (target = 0; target < MAX_TARGETS; target++)
2320                         qla1280_set_target_defaults(ha, bus, target);
2321         }
2322 }
2323
2324 static int
2325 qla1280_config_target(struct scsi_qla_host *ha, int bus, int target)
2326 {
2327         struct nvram *nv = &ha->nvram;
2328         uint16_t mb[MAILBOX_REGISTER_COUNT];
2329         int status, lun;
2330
2331         /* Set Target Parameters. */
2332         mb[0] = MBC_SET_TARGET_PARAMETERS;
2333         mb[1] = (uint16_t) (bus ? target | BIT_7 : target);
2334         mb[1] <<= 8;
2335
2336         /*
2337          * Do not enable wide, sync, and ppr for the initial
2338          * INQUIRY run. We enable this later if we determine
2339          * the target actually supports it.
2340          */
2341         nv->bus[bus].target[target].parameter.f.
2342                 auto_request_sense = 1;
2343         nv->bus[bus].target[target].parameter.f.
2344                 stop_queue_on_check = 0;
2345
2346         if (IS_ISP1x160(ha))
2347                 nv->bus[bus].target[target].ppr_1x160.
2348                         flags.enable_ppr = 0;
2349
2350         /*
2351          * No sync, wide, etc. while probing
2352          */
2353         mb[2] = (nv->bus[bus].target[target].parameter.c << 8) &
2354                 ~(TP_SYNC /*| TP_WIDE | TP_PPR*/);
2355
2356         if (IS_ISP1x160(ha))
2357                 mb[3] = nv->bus[bus].target[target].flags.flags1x160.sync_offset << 8;
2358         else
2359                 mb[3] = nv->bus[bus].target[target].flags.flags1x80.sync_offset << 8;
2360         mb[3] |= nv->bus[bus].target[target].sync_period;
2361
2362         status = qla1280_mailbox_command(ha, BIT_3 | BIT_2 | BIT_1 | BIT_0, &mb[0]);
2363
2364         /* Save Tag queuing enable flag. */
2365         mb[0] = BIT_0 << target;
2366         if (nv->bus[bus].target[target].parameter.f.tag_queuing)
2367                 ha->bus_settings[bus].qtag_enables |= mb[0];
2368
2369         /* Save Device enable flag. */
2370         if (IS_ISP1x160(ha)) {
2371                 if (nv->bus[bus].target[target].flags.flags1x160.device_enable)
2372                         ha->bus_settings[bus].device_enables |= mb[0];
2373                 ha->bus_settings[bus].lun_disables |= 0;
2374         } else {
2375                 if (nv->bus[bus].target[target].flags.flags1x80.device_enable)
2376                         ha->bus_settings[bus].device_enables |= mb[0];
2377                 /* Save LUN disable flag. */
2378                 if (nv->bus[bus].target[target].flags.flags1x80.lun_disable)
2379                         ha->bus_settings[bus].lun_disables |= mb[0];
2380         }
2381
2382         /* Set Device Queue Parameters. */
2383         for (lun = 0; lun < MAX_LUNS; lun++) {
2384                 mb[0] = MBC_SET_DEVICE_QUEUE;
2385                 mb[1] = (uint16_t)(bus ? target | BIT_7 : target);
2386                 mb[1] = mb[1] << 8 | lun;
2387                 mb[2] = nv->bus[bus].max_queue_depth;
2388                 mb[3] = nv->bus[bus].target[target].execution_throttle;
2389                 status |= qla1280_mailbox_command(ha, 0x0f, &mb[0]);
2390         }
2391
2392         return status;
2393 }
2394
2395 static int
2396 qla1280_config_bus(struct scsi_qla_host *ha, int bus)
2397 {
2398         struct nvram *nv = &ha->nvram;
2399         uint16_t mb[MAILBOX_REGISTER_COUNT];
2400         int target, status;
2401
2402         /* SCSI Reset Disable. */
2403         ha->bus_settings[bus].disable_scsi_reset =
2404                 nv->bus[bus].config_1.scsi_reset_disable;
2405
2406         /* Initiator ID. */
2407         ha->bus_settings[bus].id = nv->bus[bus].config_1.initiator_id;
2408         mb[0] = MBC_SET_INITIATOR_ID;
2409         mb[1] = bus ? ha->bus_settings[bus].id | BIT_7 :
2410                 ha->bus_settings[bus].id;
2411         status = qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
2412
2413         /* Reset Delay. */
2414         ha->bus_settings[bus].bus_reset_delay =
2415                 nv->bus[bus].bus_reset_delay;
2416
2417         /* Command queue depth per device. */
2418         ha->bus_settings[bus].hiwat = nv->bus[bus].max_queue_depth - 1;
2419
2420         /* Set target parameters. */
2421         for (target = 0; target < MAX_TARGETS; target++)
2422                 status |= qla1280_config_target(ha, bus, target);
2423
2424         return status;
2425 }
2426
2427 static int
2428 qla1280_nvram_config(struct scsi_qla_host *ha)
2429 {
2430         struct device_reg __iomem *reg = ha->iobase;
2431         struct nvram *nv = &ha->nvram;
2432         int bus, target, status = 0;
2433         uint16_t mb[MAILBOX_REGISTER_COUNT];
2434         uint16_t mask;
2435
2436         ENTER("qla1280_nvram_config");
2437
2438         if (ha->nvram_valid) {
2439                 /* Always force AUTO sense for LINUX SCSI */
2440                 for (bus = 0; bus < MAX_BUSES; bus++)
2441                         for (target = 0; target < MAX_TARGETS; target++) {
2442                                 nv->bus[bus].target[target].parameter.f.
2443                                         auto_request_sense = 1;
2444                         }
2445         } else {
2446                 qla1280_set_defaults(ha);
2447         }
2448
2449         qla1280_print_settings(nv);
2450
2451         /* Disable RISC load of firmware. */
2452         ha->flags.disable_risc_code_load =
2453                 nv->cntr_flags_1.disable_loading_risc_code;
2454
2455         if (IS_ISP1040(ha)) {
2456                 uint16_t hwrev, cfg1, cdma_conf, ddma_conf;
2457
2458                 hwrev = RD_REG_WORD(&reg->cfg_0) & ISP_CFG0_HWMSK;
2459
2460                 cfg1 = RD_REG_WORD(&reg->cfg_1);
2461                 cdma_conf = RD_REG_WORD(&reg->cdma_cfg);
2462                 ddma_conf = RD_REG_WORD(&reg->ddma_cfg);
2463
2464                 /* Busted fifo, says mjacob. */
2465                 if (hwrev == ISP_CFG0_1040A)
2466                         WRT_REG_WORD(&reg->cfg_1, cfg1 | ISP_CFG1_F64);
2467                 else
2468                         WRT_REG_WORD(&reg->cfg_1, cfg1 | ISP_CFG1_F64 | ISP_CFG1_BENAB);
2469
2470                 WRT_REG_WORD(&reg->cdma_cfg, cdma_conf | CDMA_CONF_BENAB);
2471                 WRT_REG_WORD(&reg->ddma_cfg, cdma_conf | DDMA_CONF_BENAB);
2472         } else {
2473                 /* Set ISP hardware DMA burst */
2474                 mb[0] = nv->isp_config.c;
2475                 /* Enable DMA arbitration on dual channel controllers */
2476                 if (ha->ports > 1)
2477                         mb[0] |= BIT_13;
2478                 WRT_REG_WORD(&reg->cfg_1, mb[0]);
2479
2480                 /* Set SCSI termination. */
2481                 WRT_REG_WORD(&reg->gpio_enable, (BIT_3 + BIT_2 + BIT_1 + BIT_0));
2482                 mb[0] = nv->termination.c & (BIT_3 + BIT_2 + BIT_1 + BIT_0);
2483                 WRT_REG_WORD(&reg->gpio_data, mb[0]);
2484         }
2485
2486         /* ISP parameter word. */
2487         mb[0] = MBC_SET_SYSTEM_PARAMETER;
2488         mb[1] = nv->isp_parameter;
2489         status |= qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
2490
2491         if (IS_ISP1x40(ha)) {
2492                 /* clock rate - for qla1240 and older, only */
2493                 mb[0] = MBC_SET_CLOCK_RATE;
2494                 mb[1] = 40;
2495                 status |= qla1280_mailbox_command(ha, BIT_1 | BIT_0, mb);
2496         }
2497
2498         /* Firmware feature word. */
2499         mb[0] = MBC_SET_FIRMWARE_FEATURES;
2500         mask = BIT_5 | BIT_1 | BIT_0;
2501         mb[1] = le16_to_cpu(nv->firmware_feature.w) & (mask);
2502 #if defined(CONFIG_IA64_GENERIC) || defined (CONFIG_IA64_SGI_SN2)
2503         if (ia64_platform_is("sn2")) {
2504                 printk(KERN_INFO "scsi(%li): Enabling SN2 PCI DMA "
2505                        "workaround\n", ha->host_no);
2506                 mb[1] |= BIT_9;
2507         }
2508 #endif
2509         status |= qla1280_mailbox_command(ha, mask, &mb[0]);
2510
2511         /* Retry count and delay. */
2512         mb[0] = MBC_SET_RETRY_COUNT;
2513         mb[1] = nv->bus[0].retry_count;
2514         mb[2] = nv->bus[0].retry_delay;
2515         mb[6] = nv->bus[1].retry_count;
2516         mb[7] = nv->bus[1].retry_delay;
2517         status |= qla1280_mailbox_command(ha, BIT_7 | BIT_6 | BIT_2 |
2518                                           BIT_1 | BIT_0, &mb[0]);
2519
2520         /* ASYNC data setup time. */
2521         mb[0] = MBC_SET_ASYNC_DATA_SETUP;
2522         mb[1] = nv->bus[0].config_2.async_data_setup_time;
2523         mb[2] = nv->bus[1].config_2.async_data_setup_time;
2524         status |= qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
2525
2526         /* Active negation states. */
2527         mb[0] = MBC_SET_ACTIVE_NEGATION;
2528         mb[1] = 0;
2529         if (nv->bus[0].config_2.req_ack_active_negation)
2530                 mb[1] |= BIT_5;
2531         if (nv->bus[0].config_2.data_line_active_negation)
2532                 mb[1] |= BIT_4;
2533         mb[2] = 0;
2534         if (nv->bus[1].config_2.req_ack_active_negation)
2535                 mb[2] |= BIT_5;
2536         if (nv->bus[1].config_2.data_line_active_negation)
2537                 mb[2] |= BIT_4;
2538         status |= qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
2539
2540         mb[0] = MBC_SET_DATA_OVERRUN_RECOVERY;
2541         mb[1] = 2;      /* Reset SCSI bus and return all outstanding IO */
2542         status |= qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
2543
2544         /* thingy */
2545         mb[0] = MBC_SET_PCI_CONTROL;
2546         mb[1] = 2;      /* Data DMA Channel Burst Enable */
2547         mb[2] = 2;      /* Command DMA Channel Burst Enable */
2548         status |= qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
2549
2550         mb[0] = MBC_SET_TAG_AGE_LIMIT;
2551         mb[1] = 8;
2552         status |= qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
2553
2554         /* Selection timeout. */
2555         mb[0] = MBC_SET_SELECTION_TIMEOUT;
2556         mb[1] = nv->bus[0].selection_timeout;
2557         mb[2] = nv->bus[1].selection_timeout;
2558         status |= qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
2559
2560         for (bus = 0; bus < ha->ports; bus++)
2561                 status |= qla1280_config_bus(ha, bus);
2562
2563         if (status)
2564                 dprintk(2, "qla1280_nvram_config: **** FAILED ****\n");
2565
2566         LEAVE("qla1280_nvram_config");
2567         return status;
2568 }
2569
2570 /*
2571  * Get NVRAM data word
2572  *      Calculates word position in NVRAM and calls request routine to
2573  *      get the word from NVRAM.
2574  *
2575  * Input:
2576  *      ha      = adapter block pointer.
2577  *      address = NVRAM word address.
2578  *
2579  * Returns:
2580  *      data word.
2581  */
2582 static uint16_t
2583 qla1280_get_nvram_word(struct scsi_qla_host *ha, uint32_t address)
2584 {
2585         uint32_t nv_cmd;
2586         uint16_t data;
2587
2588         nv_cmd = address << 16;
2589         nv_cmd |= NV_READ_OP;
2590
2591         data = le16_to_cpu(qla1280_nvram_request(ha, nv_cmd));
2592
2593         dprintk(8, "qla1280_get_nvram_word: exiting normally NVRAM data = "
2594                 "0x%x", data);
2595
2596         return data;
2597 }
2598
2599 /*
2600  * NVRAM request
2601  *      Sends read command to NVRAM and gets data from NVRAM.
2602  *
2603  * Input:
2604  *      ha     = adapter block pointer.
2605  *      nv_cmd = Bit 26     = start bit
2606  *               Bit 25, 24 = opcode
2607  *               Bit 23-16  = address
2608  *               Bit 15-0   = write data
2609  *
2610  * Returns:
2611  *      data word.
2612  */
2613 static uint16_t
2614 qla1280_nvram_request(struct scsi_qla_host *ha, uint32_t nv_cmd)
2615 {
2616         struct device_reg __iomem *reg = ha->iobase;
2617         int cnt;
2618         uint16_t data = 0;
2619         uint16_t reg_data;
2620
2621         /* Send command to NVRAM. */
2622
2623         nv_cmd <<= 5;
2624         for (cnt = 0; cnt < 11; cnt++) {
2625                 if (nv_cmd & BIT_31)
2626                         qla1280_nv_write(ha, NV_DATA_OUT);
2627                 else
2628                         qla1280_nv_write(ha, 0);
2629                 nv_cmd <<= 1;
2630         }
2631
2632         /* Read data from NVRAM. */
2633
2634         for (cnt = 0; cnt < 16; cnt++) {
2635                 WRT_REG_WORD(&reg->nvram, (NV_SELECT | NV_CLOCK));
2636                 RD_REG_WORD(&reg->id_l);        /* Flush PCI write */
2637                 NVRAM_DELAY();
2638                 data <<= 1;
2639                 reg_data = RD_REG_WORD(&reg->nvram);
2640                 if (reg_data & NV_DATA_IN)
2641                         data |= BIT_0;
2642                 WRT_REG_WORD(&reg->nvram, NV_SELECT);
2643                 RD_REG_WORD(&reg->id_l);        /* Flush PCI write */
2644                 NVRAM_DELAY();
2645         }
2646
2647         /* Deselect chip. */
2648
2649         WRT_REG_WORD(&reg->nvram, NV_DESELECT);
2650         RD_REG_WORD(&reg->id_l);        /* Flush PCI write */
2651         NVRAM_DELAY();
2652
2653         return data;
2654 }
2655
2656 static void
2657 qla1280_nv_write(struct scsi_qla_host *ha, uint16_t data)
2658 {
2659         struct device_reg __iomem *reg = ha->iobase;
2660
2661         WRT_REG_WORD(&reg->nvram, data | NV_SELECT);
2662         RD_REG_WORD(&reg->id_l);        /* Flush PCI write */
2663         NVRAM_DELAY();
2664         WRT_REG_WORD(&reg->nvram, data | NV_SELECT | NV_CLOCK);
2665         RD_REG_WORD(&reg->id_l);        /* Flush PCI write */
2666         NVRAM_DELAY();
2667         WRT_REG_WORD(&reg->nvram, data | NV_SELECT);
2668         RD_REG_WORD(&reg->id_l);        /* Flush PCI write */
2669         NVRAM_DELAY();
2670 }
2671
2672 /*
2673  * Mailbox Command
2674  *      Issue mailbox command and waits for completion.
2675  *
2676  * Input:
2677  *      ha = adapter block pointer.
2678  *      mr = mailbox registers to load.
2679  *      mb = data pointer for mailbox registers.
2680  *
2681  * Output:
2682  *      mb[MAILBOX_REGISTER_COUNT] = returned mailbox data.
2683  *
2684  * Returns:
2685  *      0 = success
2686  */
2687 static int
2688 qla1280_mailbox_command(struct scsi_qla_host *ha, uint8_t mr, uint16_t *mb)
2689 {
2690         struct device_reg __iomem *reg = ha->iobase;
2691 #if 0
2692         LIST_HEAD(done_q);
2693 #endif
2694         int status = 0;
2695         int cnt;
2696         uint16_t *optr, *iptr;
2697         uint16_t __iomem *mptr;
2698         uint16_t data;
2699         DECLARE_COMPLETION(wait);
2700         struct timer_list timer;
2701
2702         ENTER("qla1280_mailbox_command");
2703
2704         if (ha->mailbox_wait) {
2705                 printk(KERN_ERR "Warning mailbox wait already in use!\n");
2706         }
2707         ha->mailbox_wait = &wait;
2708
2709         /*
2710          * We really should start out by verifying that the mailbox is
2711          * available before starting sending the command data
2712          */
2713         /* Load mailbox registers. */
2714         mptr = (uint16_t __iomem *) &reg->mailbox0;
2715         iptr = mb;
2716         for (cnt = 0; cnt < MAILBOX_REGISTER_COUNT; cnt++) {
2717                 if (mr & BIT_0) {
2718                         WRT_REG_WORD(mptr, (*iptr));
2719                 }
2720
2721                 mr >>= 1;
2722                 mptr++;
2723                 iptr++;
2724         }
2725
2726         /* Issue set host interrupt command. */
2727
2728         /* set up a timer just in case we're really jammed */
2729         init_timer(&timer);
2730         timer.expires = jiffies + 20*HZ;
2731         timer.data = (unsigned long)ha;
2732         timer.function = qla1280_mailbox_timeout;
2733         add_timer(&timer);
2734
2735         spin_unlock_irq(HOST_LOCK);
2736         WRT_REG_WORD(&reg->host_cmd, HC_SET_HOST_INT);
2737         data = qla1280_debounce_register(&reg->istatus);
2738
2739         wait_for_completion(&wait);
2740         del_timer_sync(&timer);
2741
2742         spin_lock_irq(HOST_LOCK);
2743
2744         ha->mailbox_wait = NULL;
2745
2746         /* Check for mailbox command timeout. */
2747         if (ha->mailbox_out[0] != MBS_CMD_CMP) {
2748                 printk(KERN_WARNING "qla1280_mailbox_command: Command failed, "
2749                        "mailbox0 = 0x%04x, mailbox_out0 = 0x%04x, istatus = "
2750                        "0x%04x\n", 
2751                        mb[0], ha->mailbox_out[0], RD_REG_WORD(&reg->istatus));
2752                 printk(KERN_WARNING "m0 %04x, m1 %04x, m2 %04x, m3 %04x\n",
2753                        RD_REG_WORD(&reg->mailbox0), RD_REG_WORD(&reg->mailbox1),
2754                        RD_REG_WORD(&reg->mailbox2), RD_REG_WORD(&reg->mailbox3));
2755                 printk(KERN_WARNING "m4 %04x, m5 %04x, m6 %04x, m7 %04x\n",
2756                        RD_REG_WORD(&reg->mailbox4), RD_REG_WORD(&reg->mailbox5),
2757                        RD_REG_WORD(&reg->mailbox6), RD_REG_WORD(&reg->mailbox7));
2758                 status = 1;
2759         }
2760
2761         /* Load return mailbox registers. */
2762         optr = mb;
2763         iptr = (uint16_t *) &ha->mailbox_out[0];
2764         mr = MAILBOX_REGISTER_COUNT;
2765         memcpy(optr, iptr, MAILBOX_REGISTER_COUNT * sizeof(uint16_t));
2766
2767 #if 0
2768         /* Go check for any response interrupts pending. */
2769         qla1280_isr(ha, &done_q);
2770 #endif
2771
2772         if (ha->flags.reset_marker)
2773                 qla1280_rst_aen(ha);
2774
2775 #if 0
2776         if (!list_empty(&done_q))
2777                 qla1280_done(ha, &done_q);
2778 #endif
2779
2780         if (status)
2781                 dprintk(2, "qla1280_mailbox_command: **** FAILED, mailbox0 = "
2782                         "0x%x ****\n", mb[0]);
2783
2784         LEAVE("qla1280_mailbox_command");
2785         return status;
2786 }
2787
2788 /*
2789  * qla1280_poll
2790  *      Polls ISP for interrupts.
2791  *
2792  * Input:
2793  *      ha = adapter block pointer.
2794  */
2795 static void
2796 qla1280_poll(struct scsi_qla_host *ha)
2797 {
2798         struct device_reg __iomem *reg = ha->iobase;
2799         uint16_t data;
2800         LIST_HEAD(done_q);
2801
2802         /* ENTER("qla1280_poll"); */
2803
2804         /* Check for pending interrupts. */
2805         data = RD_REG_WORD(&reg->istatus);
2806         if (data & RISC_INT)
2807                 qla1280_isr(ha, &done_q);
2808
2809         if (!ha->mailbox_wait) {
2810                 if (ha->flags.reset_marker)
2811                         qla1280_rst_aen(ha);
2812         }
2813
2814         if (!list_empty(&done_q))
2815                 qla1280_done(ha);
2816
2817         /* LEAVE("qla1280_poll"); */
2818 }
2819
2820 /*
2821  * qla1280_bus_reset
2822  *      Issue SCSI bus reset.
2823  *
2824  * Input:
2825  *      ha  = adapter block pointer.
2826  *      bus = SCSI bus number.
2827  *
2828  * Returns:
2829  *      0 = success
2830  */
2831 static int
2832 qla1280_bus_reset(struct scsi_qla_host *ha, int bus)
2833 {
2834         uint16_t mb[MAILBOX_REGISTER_COUNT];
2835         uint16_t reset_delay;
2836         int status;
2837
2838         dprintk(3, "qla1280_bus_reset: entered\n");
2839
2840         if (qla1280_verbose)
2841                 printk(KERN_INFO "scsi(%li:%i): Resetting SCSI BUS\n",
2842                        ha->host_no, bus);
2843
2844         reset_delay = ha->bus_settings[bus].bus_reset_delay;
2845         mb[0] = MBC_BUS_RESET;
2846         mb[1] = reset_delay;
2847         mb[2] = (uint16_t) bus;
2848         status = qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
2849
2850         if (status) {
2851                 if (ha->bus_settings[bus].failed_reset_count > 2)
2852                         ha->bus_settings[bus].scsi_bus_dead = 1;
2853                 ha->bus_settings[bus].failed_reset_count++;
2854         } else {
2855                 spin_unlock_irq(HOST_LOCK);
2856                 ssleep(reset_delay);
2857                 spin_lock_irq(HOST_LOCK);
2858
2859                 ha->bus_settings[bus].scsi_bus_dead = 0;
2860                 ha->bus_settings[bus].failed_reset_count = 0;
2861                 ha->bus_settings[bus].reset_marker = 0;
2862                 /* Issue marker command. */
2863                 qla1280_marker(ha, bus, 0, 0, MK_SYNC_ALL);
2864         }
2865
2866         /*
2867          * We should probably call qla1280_set_target_parameters()
2868          * here as well for all devices on the bus.
2869          */
2870
2871         if (status)
2872                 dprintk(2, "qla1280_bus_reset: **** FAILED ****\n");
2873         else
2874                 dprintk(3, "qla1280_bus_reset: exiting normally\n");
2875
2876         return status;
2877 }
2878
2879 /*
2880  * qla1280_device_reset
2881  *      Issue bus device reset message to the target.
2882  *
2883  * Input:
2884  *      ha      = adapter block pointer.
2885  *      bus     = SCSI BUS number.
2886  *      target  = SCSI ID.
2887  *
2888  * Returns:
2889  *      0 = success
2890  */
2891 static int
2892 qla1280_device_reset(struct scsi_qla_host *ha, int bus, int target)
2893 {
2894         uint16_t mb[MAILBOX_REGISTER_COUNT];
2895         int status;
2896
2897         ENTER("qla1280_device_reset");
2898
2899         mb[0] = MBC_ABORT_TARGET;
2900         mb[1] = (bus ? (target | BIT_7) : target) << 8;
2901         mb[2] = 1;
2902         status = qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
2903
2904         /* Issue marker command. */
2905         qla1280_marker(ha, bus, target, 0, MK_SYNC_ID);
2906
2907         if (status)
2908                 dprintk(2, "qla1280_device_reset: **** FAILED ****\n");
2909
2910         LEAVE("qla1280_device_reset");
2911         return status;
2912 }
2913
2914 /*
2915  * qla1280_abort_device
2916  *      Issue an abort message to the device
2917  *
2918  * Input:
2919  *      ha     = adapter block pointer.
2920  *      bus    = SCSI BUS.
2921  *      target = SCSI ID.
2922  *      lun    = SCSI LUN.
2923  *
2924  * Returns:
2925  *      0 = success
2926  */
2927 static int
2928 qla1280_abort_device(struct scsi_qla_host *ha, int bus, int target, int lun)
2929 {
2930         uint16_t mb[MAILBOX_REGISTER_COUNT];
2931         int status;
2932
2933         ENTER("qla1280_abort_device");
2934
2935         mb[0] = MBC_ABORT_DEVICE;
2936         mb[1] = (bus ? target | BIT_7 : target) << 8 | lun;
2937         status = qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
2938
2939         /* Issue marker command. */
2940         qla1280_marker(ha, bus, target, lun, MK_SYNC_ID_LUN);
2941
2942         if (status)
2943                 dprintk(2, "qla1280_abort_device: **** FAILED ****\n");
2944
2945         LEAVE("qla1280_abort_device");
2946         return status;
2947 }
2948
2949 /*
2950  * qla1280_abort_command
2951  *      Abort command aborts a specified IOCB.
2952  *
2953  * Input:
2954  *      ha = adapter block pointer.
2955  *      sp = SB structure pointer.
2956  *
2957  * Returns:
2958  *      0 = success
2959  */
2960 static int
2961 qla1280_abort_command(struct scsi_qla_host *ha, struct srb * sp, int handle)
2962 {
2963         uint16_t mb[MAILBOX_REGISTER_COUNT];
2964         unsigned int bus, target, lun;
2965         int status;
2966
2967         ENTER("qla1280_abort_command");
2968
2969         bus = SCSI_BUS_32(sp->cmd);
2970         target = SCSI_TCN_32(sp->cmd);
2971         lun = SCSI_LUN_32(sp->cmd);
2972
2973         sp->flags |= SRB_ABORT_PENDING;
2974
2975         mb[0] = MBC_ABORT_COMMAND;
2976         mb[1] = (bus ? target | BIT_7 : target) << 8 | lun;
2977         mb[2] = handle >> 16;
2978         mb[3] = handle & 0xffff;
2979         status = qla1280_mailbox_command(ha, 0x0f, &mb[0]);
2980
2981         if (status) {
2982                 dprintk(2, "qla1280_abort_command: **** FAILED ****\n");
2983                 sp->flags &= ~SRB_ABORT_PENDING;
2984         }
2985
2986
2987         LEAVE("qla1280_abort_command");
2988         return status;
2989 }
2990
2991 /*
2992  * qla1280_reset_adapter
2993  *      Reset adapter.
2994  *
2995  * Input:
2996  *      ha = adapter block pointer.
2997  */
2998 static void
2999 qla1280_reset_adapter(struct scsi_qla_host *ha)
3000 {
3001         struct device_reg __iomem *reg = ha->iobase;
3002
3003         ENTER("qla1280_reset_adapter");
3004
3005         /* Disable ISP chip */
3006         ha->flags.online = 0;
3007         WRT_REG_WORD(&reg->ictrl, ISP_RESET);
3008         WRT_REG_WORD(&reg->host_cmd,
3009                      HC_RESET_RISC | HC_RELEASE_RISC | HC_DISABLE_BIOS);
3010         RD_REG_WORD(&reg->id_l);        /* Flush PCI write */
3011
3012         LEAVE("qla1280_reset_adapter");
3013 }
3014
3015 /*
3016  *  Issue marker command.
3017  *      Function issues marker IOCB.
3018  *
3019  * Input:
3020  *      ha   = adapter block pointer.
3021  *      bus  = SCSI BUS number
3022  *      id   = SCSI ID
3023  *      lun  = SCSI LUN
3024  *      type = marker modifier
3025  */
3026 static void
3027 qla1280_marker(struct scsi_qla_host *ha, int bus, int id, int lun, u8 type)
3028 {
3029         struct mrk_entry *pkt;
3030
3031         ENTER("qla1280_marker");
3032
3033         /* Get request packet. */
3034         if ((pkt = (struct mrk_entry *) qla1280_req_pkt(ha))) {
3035                 pkt->entry_type = MARKER_TYPE;
3036                 pkt->lun = (uint8_t) lun;
3037                 pkt->target = (uint8_t) (bus ? (id | BIT_7) : id);
3038                 pkt->modifier = type;
3039                 pkt->entry_status = 0;
3040
3041                 /* Issue command to ISP */
3042                 qla1280_isp_cmd(ha);
3043         }
3044
3045         LEAVE("qla1280_marker");
3046 }
3047
3048
3049 /*
3050  * qla1280_64bit_start_scsi
3051  *      The start SCSI is responsible for building request packets on
3052  *      request ring and modifying ISP input pointer.
3053  *
3054  * Input:
3055  *      ha = adapter block pointer.
3056  *      sp = SB structure pointer.
3057  *
3058  * Returns:
3059  *      0 = success, was able to issue command.
3060  */
3061 #ifdef QLA_64BIT_PTR
3062 static int
3063 qla1280_64bit_start_scsi(struct scsi_qla_host *ha, struct srb * sp)
3064 {
3065         struct device_reg __iomem *reg = ha->iobase;
3066         struct scsi_cmnd *cmd = sp->cmd;
3067         cmd_a64_entry_t *pkt;
3068         struct scatterlist *sg = NULL;
3069         u32 *dword_ptr;
3070         dma_addr_t dma_handle;
3071         int status = 0;
3072         int cnt;
3073         int req_cnt;
3074         u16 seg_cnt;
3075         u8 dir;
3076
3077         ENTER("qla1280_64bit_start_scsi:");
3078
3079         /* Calculate number of entries and segments required. */
3080         req_cnt = 1;
3081         if (cmd->use_sg) {
3082                 sg = (struct scatterlist *) cmd->request_buffer;
3083                 seg_cnt = pci_map_sg(ha->pdev, sg, cmd->use_sg,
3084                                      cmd->sc_data_direction);
3085
3086                 if (seg_cnt > 2) {
3087                         req_cnt += (seg_cnt - 2) / 5;
3088                         if ((seg_cnt - 2) % 5)
3089                                 req_cnt++;
3090                 }
3091         } else if (cmd->request_bufflen) {      /* If data transfer. */
3092                 seg_cnt = 1;
3093         } else {
3094                 seg_cnt = 0;
3095         }
3096
3097         if ((req_cnt + 2) >= ha->req_q_cnt) {
3098                 /* Calculate number of free request entries. */
3099                 cnt = RD_REG_WORD(&reg->mailbox4);
3100                 if (ha->req_ring_index < cnt)
3101                         ha->req_q_cnt = cnt - ha->req_ring_index;
3102                 else
3103                         ha->req_q_cnt =
3104                                 REQUEST_ENTRY_CNT - (ha->req_ring_index - cnt);
3105         }
3106
3107         /* If room for request in request ring. */
3108         if ((req_cnt + 2) >= ha->req_q_cnt) {
3109                 status = 1;
3110                 dprintk(2, "qla1280_64bit_start_scsi: in-ptr=0x%x  req_q_cnt="
3111                         "0x%xreq_cnt=0x%x", ha->req_ring_index, ha->req_q_cnt,
3112                         req_cnt);
3113                 goto out;
3114         }
3115
3116         /* Check for room in outstanding command list. */
3117         for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS &&
3118                      ha->outstanding_cmds[cnt] != 0; cnt++);
3119
3120         if (cnt >= MAX_OUTSTANDING_COMMANDS) {
3121                 status = 1;
3122                 dprintk(2, "qla1280_64bit_start_scsi: NO ROOM IN "
3123                         "OUTSTANDING ARRAY, req_q_cnt=0x%x", ha->req_q_cnt);
3124                 goto out;
3125         }
3126
3127         ha->outstanding_cmds[cnt] = sp;
3128         ha->req_q_cnt -= req_cnt;
3129         CMD_HANDLE(sp->cmd) = (unsigned char *)(unsigned long)(cnt + 1);
3130
3131         dprintk(2, "64bit_start: cmd=%p sp=%p CDB=%xm, handle %lx\n", cmd, sp,
3132                 cmd->cmnd[0], (long)CMD_HANDLE(sp->cmd));
3133         dprintk(2, "             bus %i, target %i, lun %i\n",
3134                 SCSI_BUS_32(cmd), SCSI_TCN_32(cmd), SCSI_LUN_32(cmd));
3135         qla1280_dump_buffer(2, cmd->cmnd, MAX_COMMAND_SIZE);
3136
3137         /*
3138          * Build command packet.
3139          */
3140         pkt = (cmd_a64_entry_t *) ha->request_ring_ptr;
3141
3142         pkt->entry_type = COMMAND_A64_TYPE;
3143         pkt->entry_count = (uint8_t) req_cnt;
3144         pkt->sys_define = (uint8_t) ha->req_ring_index;
3145         pkt->entry_status = 0;
3146         pkt->handle = cpu_to_le32(cnt);
3147
3148         /* Zero out remaining portion of packet. */
3149         memset(((char *)pkt + 8), 0, (REQUEST_ENTRY_SIZE - 8));
3150
3151         /* Set ISP command timeout. */
3152         pkt->timeout = cpu_to_le16(30);
3153
3154         /* Set device target ID and LUN */
3155         pkt->lun = SCSI_LUN_32(cmd);
3156         pkt->target = SCSI_BUS_32(cmd) ?
3157                 (SCSI_TCN_32(cmd) | BIT_7) : SCSI_TCN_32(cmd);
3158
3159         /* Enable simple tag queuing if device supports it. */
3160         if (DEV_SIMPLE_TAGS(cmd->device))
3161                 pkt->control_flags |= cpu_to_le16(BIT_3);
3162
3163         /* Load SCSI command packet. */
3164         pkt->cdb_len = cpu_to_le16(CMD_CDBLEN(cmd));
3165         memcpy(pkt->scsi_cdb, &(CMD_CDBP(cmd)), CMD_CDBLEN(cmd));
3166         /* dprintk(1, "Build packet for command[0]=0x%x\n",pkt->scsi_cdb[0]); */
3167
3168         /* Set transfer direction. */
3169         dir = qla1280_data_direction(cmd);
3170         pkt->control_flags |= cpu_to_le16(dir);
3171
3172         /* Set total data segment count. */
3173         pkt->dseg_count = cpu_to_le16(seg_cnt);
3174
3175         /*
3176          * Load data segments.
3177          */
3178         if (seg_cnt) {  /* If data transfer. */
3179                 /* Setup packet address segment pointer. */
3180                 dword_ptr = (u32 *)&pkt->dseg_0_address;
3181
3182                 if (cmd->use_sg) {      /* If scatter gather */
3183                         /* Load command entry data segments. */
3184                         for (cnt = 0; cnt < 2 && seg_cnt; cnt++, seg_cnt--) {
3185                                 dma_handle = sg_dma_address(sg);
3186 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
3187                                 if (ha->flags.use_pci_vchannel)
3188                                         sn_pci_set_vchan(ha->pdev,
3189                                                         (unsigned long *)&dma_handle,
3190                                                          SCSI_BUS_32(cmd));
3191 #endif
3192                                 *dword_ptr++ =
3193                                         cpu_to_le32(pci_dma_lo32(dma_handle));
3194                                 *dword_ptr++ =
3195                                         cpu_to_le32(pci_dma_hi32(dma_handle));
3196                                 *dword_ptr++ = cpu_to_le32(sg_dma_len(sg));
3197                                 sg++;
3198                                 dprintk(3, "S/G Segment phys_addr=%x %x, len=0x%x\n",
3199                                         cpu_to_le32(pci_dma_hi32(dma_handle)),
3200                                         cpu_to_le32(pci_dma_lo32(dma_handle)),
3201                                         cpu_to_le32(sg_dma_len(sg)));
3202                         }
3203                         dprintk(5, "qla1280_64bit_start_scsi: Scatter/gather "
3204                                 "command packet data - b %i, t %i, l %i \n",
3205                                 SCSI_BUS_32(cmd), SCSI_TCN_32(cmd),
3206                                 SCSI_LUN_32(cmd));
3207                         qla1280_dump_buffer(5, (char *)pkt,
3208                                             REQUEST_ENTRY_SIZE);
3209
3210                         /*
3211                          * Build continuation packets.
3212                          */
3213                         dprintk(3, "S/G Building Continuation...seg_cnt=0x%x "
3214                                 "remains\n", seg_cnt);
3215
3216                         while (seg_cnt > 0) {
3217                                 /* Adjust ring index. */
3218                                 ha->req_ring_index++;
3219                                 if (ha->req_ring_index == REQUEST_ENTRY_CNT) {
3220                                         ha->req_ring_index = 0;
3221                                         ha->request_ring_ptr =
3222                                                 ha->request_ring;
3223                                 } else
3224                                                 ha->request_ring_ptr++;
3225
3226                                 pkt = (cmd_a64_entry_t *)ha->request_ring_ptr;
3227
3228                                 /* Zero out packet. */
3229                                 memset(pkt, 0, REQUEST_ENTRY_SIZE);
3230
3231                                 /* Load packet defaults. */
3232                                 ((struct cont_a64_entry *) pkt)->entry_type =
3233                                         CONTINUE_A64_TYPE;
3234                                 ((struct cont_a64_entry *) pkt)->entry_count = 1;
3235                                 ((struct cont_a64_entry *) pkt)->sys_define =
3236                                         (uint8_t)ha->req_ring_index;
3237                                 /* Setup packet address segment pointer. */
3238                                 dword_ptr =
3239                                         (u32 *)&((struct cont_a64_entry *) pkt)->dseg_0_address;
3240
3241                                 /* Load continuation entry data segments. */
3242                                 for (cnt = 0; cnt < 5 && seg_cnt;
3243                                      cnt++, seg_cnt--) {
3244                                         dma_handle = sg_dma_address(sg);
3245 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
3246                                 if (ha->flags.use_pci_vchannel)
3247                                         sn_pci_set_vchan(ha->pdev, 
3248                                                         (unsigned long *)&dma_handle,
3249                                                          SCSI_BUS_32(cmd));
3250 #endif
3251                                         *dword_ptr++ =
3252                                                 cpu_to_le32(pci_dma_lo32(dma_handle));
3253                                         *dword_ptr++ =
3254                                                 cpu_to_le32(pci_dma_hi32(dma_handle));
3255                                         *dword_ptr++ =
3256                                                 cpu_to_le32(sg_dma_len(sg));
3257                                         dprintk(3, "S/G Segment Cont. phys_addr=%x %x, len=0x%x\n",
3258                                                 cpu_to_le32(pci_dma_hi32(dma_handle)),
3259                                                 cpu_to_le32(pci_dma_lo32(dma_handle)),
3260                                                 cpu_to_le32(sg_dma_len(sg)));
3261                                         sg++;
3262                                 }
3263                                 dprintk(5, "qla1280_64bit_start_scsi: "
3264                                         "continuation packet data - b %i, t "
3265                                         "%i, l %i \n", SCSI_BUS_32(cmd),
3266                                         SCSI_TCN_32(cmd), SCSI_LUN_32(cmd));
3267                                 qla1280_dump_buffer(5, (char *)pkt,
3268                                                     REQUEST_ENTRY_SIZE);
3269                         }
3270                 } else {        /* No scatter gather data transfer */
3271                         dma_handle = pci_map_single(ha->pdev,
3272                                         cmd->request_buffer,
3273                                         cmd->request_bufflen,
3274                                         cmd->sc_data_direction);
3275
3276                         sp->saved_dma_handle = dma_handle;
3277 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
3278                         if (ha->flags.use_pci_vchannel)
3279                                 sn_pci_set_vchan(ha->pdev, 
3280                                                 (unsigned long *)&dma_handle,
3281                                                  SCSI_BUS_32(cmd));
3282 #endif
3283                         *dword_ptr++ = cpu_to_le32(pci_dma_lo32(dma_handle));
3284                         *dword_ptr++ = cpu_to_le32(pci_dma_hi32(dma_handle));
3285                         *dword_ptr = cpu_to_le32(cmd->request_bufflen);
3286
3287                         dprintk(5, "qla1280_64bit_start_scsi: No scatter/"
3288                                 "gather command packet data - b %i, t %i, "
3289                                 "l %i \n", SCSI_BUS_32(cmd), SCSI_TCN_32(cmd),
3290                                 SCSI_LUN_32(cmd));
3291                         qla1280_dump_buffer(5, (char *)pkt,
3292                                             REQUEST_ENTRY_SIZE);
3293                 }
3294         } else {        /* No data transfer */
3295                 dprintk(5, "qla1280_64bit_start_scsi: No data, command "
3296                         "packet data - b %i, t %i, l %i \n",
3297                         SCSI_BUS_32(cmd), SCSI_TCN_32(cmd), SCSI_LUN_32(cmd));
3298                 qla1280_dump_buffer(5, (char *)pkt, REQUEST_ENTRY_SIZE);
3299         }
3300         /* Adjust ring index. */
3301         ha->req_ring_index++;
3302         if (ha->req_ring_index == REQUEST_ENTRY_CNT) {
3303                 ha->req_ring_index = 0;
3304                 ha->request_ring_ptr = ha->request_ring;
3305         } else
3306                 ha->request_ring_ptr++;
3307
3308         /* Set chip new ring index. */
3309         dprintk(2,
3310                 "qla1280_64bit_start_scsi: Wakeup RISC for pending command\n");
3311         sp->flags |= SRB_SENT;
3312         ha->actthreads++;
3313         WRT_REG_WORD(&reg->mailbox4, ha->req_ring_index);
3314         /* Enforce mmio write ordering; see comment in qla1280_isp_cmd(). */
3315         mmiowb();
3316
3317  out:
3318         if (status)
3319                 dprintk(2, "qla1280_64bit_start_scsi: **** FAILED ****\n");
3320         else
3321                 dprintk(3, "qla1280_64bit_start_scsi: exiting normally\n");
3322
3323         return status;
3324 }
3325 #else /* !QLA_64BIT_PTR */
3326
3327 /*
3328  * qla1280_32bit_start_scsi
3329  *      The start SCSI is responsible for building request packets on
3330  *      request ring and modifying ISP input pointer.
3331  *
3332  *      The Qlogic firmware interface allows every queue slot to have a SCSI
3333  *      command and up to 4 scatter/gather (SG) entries.  If we need more
3334  *      than 4 SG entries, then continuation entries are used that can
3335  *      hold another 7 entries each.  The start routine determines if there
3336  *      is eought empty slots then build the combination of requests to
3337  *      fulfill the OS request.
3338  *
3339  * Input:
3340  *      ha = adapter block pointer.
3341  *      sp = SCSI Request Block structure pointer.
3342  *
3343  * Returns:
3344  *      0 = success, was able to issue command.
3345  */
3346 static int
3347 qla1280_32bit_start_scsi(struct scsi_qla_host *ha, struct srb * sp)
3348 {
3349         struct device_reg __iomem *reg = ha->iobase;
3350         struct scsi_cmnd *cmd = sp->cmd;
3351         struct cmd_entry *pkt;
3352         struct scatterlist *sg = NULL;
3353         uint32_t *dword_ptr;
3354         int status = 0;
3355         int cnt;
3356         int req_cnt;
3357         uint16_t seg_cnt;
3358         dma_addr_t dma_handle;
3359         u8 dir;
3360
3361         ENTER("qla1280_32bit_start_scsi");
3362
3363         dprintk(1, "32bit_start: cmd=%p sp=%p CDB=%x\n", cmd, sp,
3364                 cmd->cmnd[0]);
3365
3366         /* Calculate number of entries and segments required. */
3367         req_cnt = 1;
3368         if (cmd->use_sg) {
3369                 /*
3370                  * We must build an SG list in adapter format, as the kernel's
3371                  * SG list cannot be used directly because of data field size
3372                  * (__alpha__) differences and the kernel SG list uses virtual
3373                  * addresses where we need physical addresses.
3374                  */
3375                 sg = (struct scatterlist *) cmd->request_buffer;
3376                 seg_cnt = pci_map_sg(ha->pdev, sg, cmd->use_sg,
3377                                      cmd->sc_data_direction);
3378
3379                 /*
3380                  * if greater than four sg entries then we need to allocate
3381                  * continuation entries
3382                  */
3383                 if (seg_cnt > 4) {
3384                         req_cnt += (seg_cnt - 4) / 7;
3385                         if ((seg_cnt - 4) % 7)
3386                                 req_cnt++;
3387                 }
3388                 dprintk(3, "S/G Transfer cmd=%p seg_cnt=0x%x, req_cnt=%x\n",
3389                         cmd, seg_cnt, req_cnt);
3390         } else if (cmd->request_bufflen) {      /* If data transfer. */
3391                 dprintk(3, "No S/G transfer t=%x cmd=%p len=%x CDB=%x\n",
3392                         SCSI_TCN_32(cmd), cmd, cmd->request_bufflen,
3393                         cmd->cmnd[0]);
3394                 seg_cnt = 1;
3395         } else {
3396                 /* dprintk(1, "No data transfer \n"); */
3397                 seg_cnt = 0;
3398         }
3399
3400         if ((req_cnt + 2) >= ha->req_q_cnt) {
3401                 /* Calculate number of free request entries. */
3402                 cnt = RD_REG_WORD(&reg->mailbox4);
3403                 if (ha->req_ring_index < cnt)
3404                         ha->req_q_cnt = cnt - ha->req_ring_index;
3405                 else
3406                         ha->req_q_cnt =
3407                                 REQUEST_ENTRY_CNT - (ha->req_ring_index - cnt);
3408         }
3409
3410         dprintk(3, "Number of free entries=(%d) seg_cnt=0x%x\n",
3411                 ha->req_q_cnt, seg_cnt);
3412         /* If room for request in request ring. */
3413         if ((req_cnt + 2) >= ha->req_q_cnt) {
3414                 status = 1;
3415                 dprintk(2, "qla1280_32bit_start_scsi: in-ptr=0x%x, "
3416                         "req_q_cnt=0x%x, req_cnt=0x%x", ha->req_ring_index,
3417                         ha->req_q_cnt, req_cnt);
3418                 goto out;
3419         }
3420
3421         /* Check for empty slot in outstanding command list. */
3422         for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS &&
3423                      (ha->outstanding_cmds[cnt] != 0); cnt++) ;
3424
3425         if (cnt >= MAX_OUTSTANDING_COMMANDS) {
3426                 status = 1;
3427                 dprintk(2, "qla1280_32bit_start_scsi: NO ROOM IN OUTSTANDING "
3428                         "ARRAY, req_q_cnt=0x%x\n", ha->req_q_cnt);
3429                 goto out;
3430         }
3431
3432         CMD_HANDLE(sp->cmd) = (unsigned char *) (unsigned long)(cnt + 1);
3433         ha->outstanding_cmds[cnt] = sp;
3434         ha->req_q_cnt -= req_cnt;
3435
3436         /*
3437          * Build command packet.
3438          */
3439         pkt = (struct cmd_entry *) ha->request_ring_ptr;
3440
3441         pkt->entry_type = COMMAND_TYPE;
3442         pkt->entry_count = (uint8_t) req_cnt;
3443         pkt->sys_define = (uint8_t) ha->req_ring_index;
3444         pkt->entry_status = 0;
3445         pkt->handle = cpu_to_le32(cnt);
3446
3447         /* Zero out remaining portion of packet. */
3448         memset(((char *)pkt + 8), 0, (REQUEST_ENTRY_SIZE - 8));
3449
3450         /* Set ISP command timeout. */
3451         pkt->timeout = cpu_to_le16(30);
3452
3453         /* Set device target ID and LUN */
3454         pkt->lun = SCSI_LUN_32(cmd);
3455         pkt->target = SCSI_BUS_32(cmd) ?
3456                 (SCSI_TCN_32(cmd) | BIT_7) : SCSI_TCN_32(cmd);
3457
3458         /* Enable simple tag queuing if device supports it. */
3459         if (DEV_SIMPLE_TAGS(cmd->device))
3460                 pkt->control_flags |= cpu_to_le16(BIT_3);
3461
3462         /* Load SCSI command packet. */
3463         pkt->cdb_len = cpu_to_le16(CMD_CDBLEN(cmd));
3464         memcpy(pkt->scsi_cdb, &(CMD_CDBP(cmd)), CMD_CDBLEN(cmd));
3465
3466         /*dprintk(1, "Build packet for command[0]=0x%x\n",pkt->scsi_cdb[0]); */
3467         /* Set transfer direction. */
3468         dir = qla1280_data_direction(cmd);
3469         pkt->control_flags |= cpu_to_le16(dir);
3470
3471         /* Set total data segment count. */
3472         pkt->dseg_count = cpu_to_le16(seg_cnt);
3473
3474         /*
3475          * Load data segments.
3476          */
3477         if (seg_cnt) {
3478                 /* Setup packet address segment pointer. */
3479                 dword_ptr = &pkt->dseg_0_address;
3480
3481                 if (cmd->use_sg) {      /* If scatter gather */
3482                         dprintk(3, "Building S/G data segments..\n");
3483                         qla1280_dump_buffer(1, (char *)sg, 4 * 16);
3484
3485                         /* Load command entry data segments. */
3486                         for (cnt = 0; cnt < 4 && seg_cnt; cnt++, seg_cnt--) {
3487                                 *dword_ptr++ =
3488                                         cpu_to_le32(pci_dma_lo32(sg_dma_address(sg)));
3489                                 *dword_ptr++ =
3490                                         cpu_to_le32(sg_dma_len(sg));
3491                                 dprintk(3, "S/G Segment phys_addr=0x%lx, len=0x%x\n",
3492                                         (pci_dma_lo32(sg_dma_address(sg))),
3493                                         (sg_dma_len(sg)));
3494                                 sg++;
3495                         }
3496                         /*
3497                          * Build continuation packets.
3498                          */
3499                         dprintk(3, "S/G Building Continuation"
3500                                 "...seg_cnt=0x%x remains\n", seg_cnt);
3501                         while (seg_cnt > 0) {
3502                                 /* Adjust ring index. */
3503                                 ha->req_ring_index++;
3504                                 if (ha->req_ring_index == REQUEST_ENTRY_CNT) {
3505                                         ha->req_ring_index = 0;
3506                                         ha->request_ring_ptr =
3507                                                 ha->request_ring;
3508                                 } else
3509                                         ha->request_ring_ptr++;
3510
3511                                 pkt = (struct cmd_entry *)ha->request_ring_ptr;
3512
3513                                 /* Zero out packet. */
3514                                 memset(pkt, 0, REQUEST_ENTRY_SIZE);
3515
3516                                 /* Load packet defaults. */
3517                                 ((struct cont_entry *) pkt)->
3518                                         entry_type = CONTINUE_TYPE;
3519                                 ((struct cont_entry *) pkt)->entry_count = 1;
3520
3521                                 ((struct cont_entry *) pkt)->sys_define =
3522                                         (uint8_t) ha->req_ring_index;
3523
3524                                 /* Setup packet address segment pointer. */
3525                                 dword_ptr =
3526                                         &((struct cont_entry *) pkt)->dseg_0_address;
3527
3528                                 /* Load continuation entry data segments. */
3529                                 for (cnt = 0; cnt < 7 && seg_cnt;
3530                                      cnt++, seg_cnt--) {
3531                                         *dword_ptr++ =
3532                                                 cpu_to_le32(pci_dma_lo32(sg_dma_address(sg)));
3533                                         *dword_ptr++ =
3534                                                 cpu_to_le32(sg_dma_len(sg));
3535                                         dprintk(1,
3536                                                 "S/G Segment Cont. phys_addr=0x%x, "
3537                                                 "len=0x%x\n",
3538                                                 cpu_to_le32(pci_dma_lo32(sg_dma_address(sg))),
3539                                                 cpu_to_le32(sg_dma_len(sg)));
3540                                         sg++;
3541                                 }
3542                                 dprintk(5, "qla1280_32bit_start_scsi: "
3543                                         "continuation packet data - "
3544                                         "scsi(%i:%i:%i)\n", SCSI_BUS_32(cmd),
3545                                         SCSI_TCN_32(cmd), SCSI_LUN_32(cmd));
3546                                 qla1280_dump_buffer(5, (char *)pkt,
3547                                                     REQUEST_ENTRY_SIZE);
3548                         }
3549                 } else {        /* No S/G data transfer */
3550                         dma_handle = pci_map_single(ha->pdev,
3551                                         cmd->request_buffer,
3552                                         cmd->request_bufflen,
3553                                         cmd->sc_data_direction);
3554                         sp->saved_dma_handle = dma_handle;
3555
3556                         *dword_ptr++ = cpu_to_le32(pci_dma_lo32(dma_handle));
3557                         *dword_ptr = cpu_to_le32(cmd->request_bufflen);
3558                 }
3559         } else {        /* No data transfer at all */
3560                 dprintk(5, "qla1280_32bit_start_scsi: No data, command "
3561                         "packet data - \n");
3562                 qla1280_dump_buffer(5, (char *)pkt, REQUEST_ENTRY_SIZE);
3563         }
3564         dprintk(5, "qla1280_32bit_start_scsi: First IOCB block:\n");
3565         qla1280_dump_buffer(5, (char *)ha->request_ring_ptr,
3566                             REQUEST_ENTRY_SIZE);
3567
3568         /* Adjust ring index. */
3569         ha->req_ring_index++;
3570         if (ha->req_ring_index == REQUEST_ENTRY_CNT) {
3571                 ha->req_ring_index = 0;
3572                 ha->request_ring_ptr = ha->request_ring;
3573         } else
3574                 ha->request_ring_ptr++;
3575
3576         /* Set chip new ring index. */
3577         dprintk(2, "qla1280_32bit_start_scsi: Wakeup RISC "
3578                 "for pending command\n");
3579         sp->flags |= SRB_SENT;
3580         ha->actthreads++;
3581         WRT_REG_WORD(&reg->mailbox4, ha->req_ring_index);
3582         /* Enforce mmio write ordering; see comment in qla1280_isp_cmd(). */
3583         mmiowb();
3584
3585 out:
3586         if (status)
3587                 dprintk(2, "qla1280_32bit_start_scsi: **** FAILED ****\n");
3588
3589         LEAVE("qla1280_32bit_start_scsi");
3590
3591         return status;
3592 }
3593 #endif
3594
3595 /*
3596  * qla1280_req_pkt
3597  *      Function is responsible for locking ring and
3598  *      getting a zeroed out request packet.
3599  *
3600  * Input:
3601  *      ha  = adapter block pointer.
3602  *
3603  * Returns:
3604  *      0 = failed to get slot.
3605  */
3606 static request_t *
3607 qla1280_req_pkt(struct scsi_qla_host *ha)
3608 {
3609         struct device_reg __iomem *reg = ha->iobase;
3610         request_t *pkt = NULL;
3611         int cnt;
3612         uint32_t timer;
3613
3614         ENTER("qla1280_req_pkt");
3615
3616         /*
3617          * This can be called from interrupt context, damn it!!!
3618          */
3619         /* Wait for 30 seconds for slot. */
3620         for (timer = 15000000; timer; timer--) {
3621                 if (ha->req_q_cnt > 0) {
3622                         /* Calculate number of free request entries. */
3623                         cnt = RD_REG_WORD(&reg->mailbox4);
3624                         if (ha->req_ring_index < cnt)
3625                                 ha->req_q_cnt = cnt - ha->req_ring_index;
3626                         else
3627                                 ha->req_q_cnt =
3628                                         REQUEST_ENTRY_CNT - (ha->req_ring_index - cnt);
3629                 }
3630
3631                 /* Found empty request ring slot? */
3632                 if (ha->req_q_cnt > 0) {
3633                         ha->req_q_cnt--;
3634                         pkt = ha->request_ring_ptr;
3635
3636                         /* Zero out packet. */
3637                         memset(pkt, 0, REQUEST_ENTRY_SIZE);
3638
3639                         /*
3640                          * How can this be right when we have a ring
3641                          * size of 512???
3642                          */
3643                         /* Set system defined field. */
3644                         pkt->sys_define = (uint8_t) ha->req_ring_index;
3645
3646                         /* Set entry count. */
3647                         pkt->entry_count = 1;
3648
3649                         break;
3650                 }
3651
3652                 udelay(2);      /* 10 */
3653
3654                 /* Check for pending interrupts. */
3655                 qla1280_poll(ha);
3656         }
3657
3658         if (!pkt)
3659                 dprintk(2, "qla1280_req_pkt: **** FAILED ****\n");
3660         else
3661                 dprintk(3, "qla1280_req_pkt: exiting normally\n");
3662
3663         return pkt;
3664 }
3665
3666 /*
3667  * qla1280_isp_cmd
3668  *      Function is responsible for modifying ISP input pointer.
3669  *      Releases ring lock.
3670  *
3671  * Input:
3672  *      ha  = adapter block pointer.
3673  */
3674 static void
3675 qla1280_isp_cmd(struct scsi_qla_host *ha)
3676 {
3677         struct device_reg __iomem *reg = ha->iobase;
3678
3679         ENTER("qla1280_isp_cmd");
3680
3681         dprintk(5, "qla1280_isp_cmd: IOCB data:\n");
3682         qla1280_dump_buffer(5, (char *)ha->request_ring_ptr,
3683                             REQUEST_ENTRY_SIZE);
3684
3685         /* Adjust ring index. */
3686         ha->req_ring_index++;
3687         if (ha->req_ring_index == REQUEST_ENTRY_CNT) {
3688                 ha->req_ring_index = 0;
3689                 ha->request_ring_ptr = ha->request_ring;
3690         } else
3691                 ha->request_ring_ptr++;
3692
3693         /*
3694          * Update request index to mailbox4 (Request Queue In).
3695          * The mmiowb() ensures that this write is ordered with writes by other
3696          * CPUs.  Without the mmiowb(), it is possible for the following:
3697          *    CPUA posts write of index 5 to mailbox4
3698          *    CPUA releases host lock
3699          *    CPUB acquires host lock
3700          *    CPUB posts write of index 6 to mailbox4
3701          *    On PCI bus, order reverses and write of 6 posts, then index 5,
3702          *       causing chip to issue full queue of stale commands
3703          * The mmiowb() prevents future writes from crossing the barrier.
3704          * See Documentation/DocBook/deviceiobook.tmpl for more information.
3705          */
3706         WRT_REG_WORD(&reg->mailbox4, ha->req_ring_index);
3707         mmiowb();
3708
3709         LEAVE("qla1280_isp_cmd");
3710 }
3711
3712 /****************************************************************************/
3713 /*                        Interrupt Service Routine.                        */
3714 /****************************************************************************/
3715
3716 /****************************************************************************
3717  *  qla1280_isr
3718  *      Calls I/O done on command completion.
3719  *
3720  * Input:
3721  *      ha           = adapter block pointer.
3722  *      done_q       = done queue.
3723  ****************************************************************************/
3724 static void
3725 qla1280_isr(struct scsi_qla_host *ha, struct list_head *done_q)
3726 {
3727         struct device_reg __iomem *reg = ha->iobase;
3728         struct response *pkt;
3729         struct srb *sp = NULL;
3730         uint16_t mailbox[MAILBOX_REGISTER_COUNT];
3731         uint16_t *wptr;
3732         uint32_t index;
3733         u16 istatus;
3734
3735         ENTER("qla1280_isr");
3736
3737         istatus = RD_REG_WORD(&reg->istatus);
3738         if (!(istatus & (RISC_INT | PCI_INT)))
3739                 return;
3740
3741         /* Save mailbox register 5 */
3742         mailbox[5] = RD_REG_WORD(&reg->mailbox5);
3743
3744         /* Check for mailbox interrupt. */
3745
3746         mailbox[0] = RD_REG_WORD_dmasync(&reg->semaphore);
3747
3748         if (mailbox[0] & BIT_0) {
3749                 /* Get mailbox data. */
3750                 /* dprintk(1, "qla1280_isr: In Get mailbox data \n"); */
3751
3752                 wptr = &mailbox[0];
3753                 *wptr++ = RD_REG_WORD(&reg->mailbox0);
3754                 *wptr++ = RD_REG_WORD(&reg->mailbox1);
3755                 *wptr = RD_REG_WORD(&reg->mailbox2);
3756                 if (mailbox[0] != MBA_SCSI_COMPLETION) {
3757                         wptr++;
3758                         *wptr++ = RD_REG_WORD(&reg->mailbox3);
3759                         *wptr++ = RD_REG_WORD(&reg->mailbox4);
3760                         wptr++;
3761                         *wptr++ = RD_REG_WORD(&reg->mailbox6);
3762                         *wptr = RD_REG_WORD(&reg->mailbox7);
3763                 }
3764
3765                 /* Release mailbox registers. */
3766
3767                 WRT_REG_WORD(&reg->semaphore, 0);
3768                 WRT_REG_WORD(&reg->host_cmd, HC_CLR_RISC_INT);
3769
3770                 dprintk(5, "qla1280_isr: mailbox interrupt mailbox[0] = 0x%x",
3771                         mailbox[0]);
3772
3773                 /* Handle asynchronous event */
3774                 switch (mailbox[0]) {
3775                 case MBA_SCSI_COMPLETION:       /* Response completion */
3776                         dprintk(5, "qla1280_isr: mailbox SCSI response "
3777                                 "completion\n");
3778
3779                         if (ha->flags.online) {
3780                                 /* Get outstanding command index. */
3781                                 index = mailbox[2] << 16 | mailbox[1];
3782
3783                                 /* Validate handle. */
3784                                 if (index < MAX_OUTSTANDING_COMMANDS)
3785                                         sp = ha->outstanding_cmds[index];
3786                                 else
3787                                         sp = NULL;
3788
3789                                 if (sp) {
3790                                         /* Free outstanding command slot. */
3791                                         ha->outstanding_cmds[index] = NULL;
3792
3793                                         /* Save ISP completion status */
3794                                         CMD_RESULT(sp->cmd) = 0;
3795
3796                                         /* Place block on done queue */
3797                                         list_add_tail(&sp->list, done_q);
3798                                 } else {
3799                                         /*
3800                                          * If we get here we have a real problem!
3801                                          */
3802                                         printk(KERN_WARNING
3803                                                "qla1280: ISP invalid handle");
3804                                 }
3805                         }
3806                         break;
3807
3808                 case MBA_BUS_RESET:     /* SCSI Bus Reset */
3809                         ha->flags.reset_marker = 1;
3810                         index = mailbox[6] & BIT_0;
3811                         ha->bus_settings[index].reset_marker = 1;
3812
3813                         printk(KERN_DEBUG "qla1280_isr(): index %i "
3814                                "asynchronous BUS_RESET\n", index);
3815                         break;
3816
3817                 case MBA_SYSTEM_ERR:    /* System Error */
3818                         printk(KERN_WARNING
3819                                "qla1280: ISP System Error - mbx1=%xh, mbx2="
3820                                "%xh, mbx3=%xh\n", mailbox[1], mailbox[2],
3821                                mailbox[3]);
3822                         break;
3823
3824                 case MBA_REQ_TRANSFER_ERR:      /* Request Transfer Error */
3825                         printk(KERN_WARNING
3826                                "qla1280: ISP Request Transfer Error\n");
3827                         break;
3828
3829                 case MBA_RSP_TRANSFER_ERR:      /* Response Transfer Error */
3830                         printk(KERN_WARNING
3831                                "qla1280: ISP Response Transfer Error\n");
3832                         break;
3833
3834                 case MBA_WAKEUP_THRES:  /* Request Queue Wake-up */
3835                         dprintk(2, "qla1280_isr: asynchronous WAKEUP_THRES\n");
3836                         break;
3837
3838                 case MBA_TIMEOUT_RESET: /* Execution Timeout Reset */
3839                         dprintk(2,
3840                                 "qla1280_isr: asynchronous TIMEOUT_RESET\n");
3841                         break;
3842
3843                 case MBA_DEVICE_RESET:  /* Bus Device Reset */
3844                         printk(KERN_INFO "qla1280_isr(): asynchronous "
3845                                "BUS_DEVICE_RESET\n");
3846
3847                         ha->flags.reset_marker = 1;
3848                         index = mailbox[6] & BIT_0;
3849                         ha->bus_settings[index].reset_marker = 1;
3850                         break;
3851
3852                 case MBA_BUS_MODE_CHANGE:
3853                         dprintk(2,
3854                                 "qla1280_isr: asynchronous BUS_MODE_CHANGE\n");
3855                         break;
3856
3857                 default:
3858                         /* dprintk(1, "qla1280_isr: default case of switch MB \n"); */
3859                         if (mailbox[0] < MBA_ASYNC_EVENT) {
3860                                 wptr = &mailbox[0];
3861                                 memcpy((uint16_t *) ha->mailbox_out, wptr,
3862                                        MAILBOX_REGISTER_COUNT *
3863                                        sizeof(uint16_t));
3864
3865                                 if(ha->mailbox_wait != NULL)
3866                                         complete(ha->mailbox_wait);
3867                         }
3868                         break;
3869                 }
3870         } else {
3871                 WRT_REG_WORD(&reg->host_cmd, HC_CLR_RISC_INT);
3872         }
3873
3874         /*
3875          * We will receive interrupts during mailbox testing prior to
3876          * the card being marked online, hence the double check.
3877          */
3878         if (!(ha->flags.online && !ha->mailbox_wait)) {
3879                 dprintk(2, "qla1280_isr: Response pointer Error\n");
3880                 goto out;
3881         }
3882
3883         if (mailbox[5] >= RESPONSE_ENTRY_CNT)
3884                 goto out;
3885
3886         while (ha->rsp_ring_index != mailbox[5]) {
3887                 pkt = ha->response_ring_ptr;
3888
3889                 dprintk(5, "qla1280_isr: ha->rsp_ring_index = 0x%x, mailbox[5]"
3890                         " = 0x%x\n", ha->rsp_ring_index, mailbox[5]);
3891                 dprintk(5,"qla1280_isr: response packet data\n");
3892                 qla1280_dump_buffer(5, (char *)pkt, RESPONSE_ENTRY_SIZE);
3893
3894                 if (pkt->entry_type == STATUS_TYPE) {
3895                         if ((le16_to_cpu(pkt->scsi_status) & 0xff)
3896                             || pkt->comp_status || pkt->entry_status) {
3897                                 dprintk(2, "qla1280_isr: ha->rsp_ring_index = "
3898                                         "0x%x mailbox[5] = 0x%x, comp_status "
3899                                         "= 0x%x, scsi_status = 0x%x\n",
3900                                         ha->rsp_ring_index, mailbox[5],
3901                                         le16_to_cpu(pkt->comp_status),
3902                                         le16_to_cpu(pkt->scsi_status));
3903                         }
3904                 } else {
3905                         dprintk(2, "qla1280_isr: ha->rsp_ring_index = "
3906                                 "0x%x, mailbox[5] = 0x%x\n",
3907                                 ha->rsp_ring_index, mailbox[5]);
3908                         dprintk(2, "qla1280_isr: response packet data\n");
3909                         qla1280_dump_buffer(2, (char *)pkt,
3910                                             RESPONSE_ENTRY_SIZE);
3911                 }
3912
3913                 if (pkt->entry_type == STATUS_TYPE || pkt->entry_status) {
3914                         dprintk(2, "status: Cmd %p, handle %i\n",
3915                                 ha->outstanding_cmds[pkt->handle]->cmd,
3916                                 pkt->handle);
3917                         if (pkt->entry_type == STATUS_TYPE)
3918                                 qla1280_status_entry(ha, pkt, done_q);
3919                         else
3920                                 qla1280_error_entry(ha, pkt, done_q);
3921                         /* Adjust ring index. */
3922                         ha->rsp_ring_index++;
3923                         if (ha->rsp_ring_index == RESPONSE_ENTRY_CNT) {
3924                                 ha->rsp_ring_index = 0;
3925                                 ha->response_ring_ptr = ha->response_ring;
3926                         } else
3927                                 ha->response_ring_ptr++;
3928                         WRT_REG_WORD(&reg->mailbox5, ha->rsp_ring_index);
3929                 }
3930         }
3931         
3932  out:
3933         LEAVE("qla1280_isr");
3934 }
3935
3936 /*
3937  *  qla1280_rst_aen
3938  *      Processes asynchronous reset.
3939  *
3940  * Input:
3941  *      ha  = adapter block pointer.
3942  */
3943 static void
3944 qla1280_rst_aen(struct scsi_qla_host *ha)
3945 {
3946         uint8_t bus;
3947
3948         ENTER("qla1280_rst_aen");
3949
3950         if (ha->flags.online && !ha->flags.reset_active &&
3951             !ha->flags.abort_isp_active) {
3952                 ha->flags.reset_active = 1;
3953                 while (ha->flags.reset_marker) {
3954                         /* Issue marker command. */
3955                         ha->flags.reset_marker = 0;
3956                         for (bus = 0; bus < ha->ports &&
3957                                      !ha->flags.reset_marker; bus++) {
3958                                 if (ha->bus_settings[bus].reset_marker) {
3959                                         ha->bus_settings[bus].reset_marker = 0;
3960                                         qla1280_marker(ha, bus, 0, 0,
3961                                                        MK_SYNC_ALL);
3962                                 }
3963                         }
3964                 }
3965         }
3966
3967         LEAVE("qla1280_rst_aen");
3968 }
3969
3970
3971 #if LINUX_VERSION_CODE < 0x020500
3972 /*
3973  *
3974  */
3975 static void
3976 qla1280_get_target_options(struct scsi_cmnd *cmd, struct scsi_qla_host *ha)
3977 {
3978         unsigned char *result;
3979         struct nvram *n;
3980         int bus, target, lun;
3981
3982         bus = SCSI_BUS_32(cmd);
3983         target = SCSI_TCN_32(cmd);
3984         lun = SCSI_LUN_32(cmd);
3985
3986         /*
3987          * Make sure to not touch anything if someone is using the
3988          * sg interface.
3989          */
3990         if (cmd->use_sg || (CMD_RESULT(cmd) >> 16) != DID_OK || lun)
3991                 return;
3992
3993         result = cmd->request_buffer;
3994         n = &ha->nvram;
3995
3996         n->bus[bus].target[target].parameter.f.enable_wide = 0;
3997         n->bus[bus].target[target].parameter.f.enable_sync = 0;
3998         n->bus[bus].target[target].ppr_1x160.flags.enable_ppr = 0;
3999
4000         if (result[7] & 0x60)
4001                 n->bus[bus].target[target].parameter.f.enable_wide = 1;
4002         if (result[7] & 0x10)
4003                 n->bus[bus].target[target].parameter.f.enable_sync = 1;
4004         if ((result[2] >= 3) && (result[4] + 5 > 56) &&
4005             (result[56] & 0x4))
4006                 n->bus[bus].target[target].ppr_1x160.flags.enable_ppr = 1;
4007
4008         dprintk(2, "get_target_options(): wide %i, sync %i, ppr %i\n",
4009                 n->bus[bus].target[target].parameter.f.enable_wide,
4010                 n->bus[bus].target[target].parameter.f.enable_sync,
4011                 n->bus[bus].target[target].ppr_1x160.flags.enable_ppr);
4012 }
4013 #endif
4014
4015 /*
4016  *  qla1280_status_entry
4017  *      Processes received ISP status entry.
4018  *
4019  * Input:
4020  *      ha           = adapter block pointer.
4021  *      pkt          = entry pointer.
4022  *      done_q       = done queue.
4023  */
4024 static void
4025 qla1280_status_entry(struct scsi_qla_host *ha, struct response *pkt,
4026                      struct list_head *done_q)
4027 {
4028         unsigned int bus, target, lun;
4029         int sense_sz;
4030         struct srb *sp;
4031         struct scsi_cmnd *cmd;
4032         uint32_t handle = le32_to_cpu(pkt->handle);
4033         uint16_t scsi_status = le16_to_cpu(pkt->scsi_status);
4034         uint16_t comp_status = le16_to_cpu(pkt->comp_status);
4035
4036         ENTER("qla1280_status_entry");
4037
4038         /* Validate handle. */
4039         if (handle < MAX_OUTSTANDING_COMMANDS)
4040                 sp = ha->outstanding_cmds[handle];
4041         else
4042                 sp = NULL;
4043
4044         if (!sp) {
4045                 printk(KERN_WARNING "qla1280: Status Entry invalid handle\n");
4046                 goto out;
4047         }
4048
4049         /* Free outstanding command slot. */
4050         ha->outstanding_cmds[handle] = NULL;
4051
4052         cmd = sp->cmd;
4053
4054         /* Generate LU queue on cntrl, target, LUN */
4055         bus = SCSI_BUS_32(cmd);
4056         target = SCSI_TCN_32(cmd);
4057         lun = SCSI_LUN_32(cmd);
4058
4059         if (comp_status || scsi_status) {
4060                 dprintk(3, "scsi: comp_status = 0x%x, scsi_status = "
4061                         "0x%x, handle = 0x%x\n", comp_status,
4062                         scsi_status, handle);
4063         }
4064
4065         /* Target busy or queue full */
4066         if ((scsi_status & 0xFF) == SAM_STAT_TASK_SET_FULL ||
4067             (scsi_status & 0xFF) == SAM_STAT_BUSY) {
4068                 CMD_RESULT(cmd) = scsi_status & 0xff;
4069         } else {
4070
4071                 /* Save ISP completion status */
4072                 CMD_RESULT(cmd) = qla1280_return_status(pkt, cmd);
4073
4074                 if (scsi_status & SS_CHECK_CONDITION) {
4075                         if (comp_status != CS_ARS_FAILED) {
4076                                 uint16_t req_sense_length =
4077                                         le16_to_cpu(pkt->req_sense_length);
4078                                 if (req_sense_length < CMD_SNSLEN(cmd))
4079                                         sense_sz = req_sense_length;
4080                                 else
4081                                         /*
4082                                          * scsi_cmnd->sense_buffer is
4083                                          * 64 bytes, why only copy 63?
4084                                          * This looks wrong! /Jes
4085                                          */
4086                                         sense_sz = CMD_SNSLEN(cmd) - 1;
4087
4088                                 memcpy(cmd->sense_buffer,
4089                                        &pkt->req_sense_data, sense_sz);
4090                         } else
4091                                 sense_sz = 0;
4092                         memset(cmd->sense_buffer + sense_sz, 0,
4093                                sizeof(cmd->sense_buffer) - sense_sz);
4094
4095                         dprintk(2, "qla1280_status_entry: Check "
4096                                 "condition Sense data, b %i, t %i, "
4097                                 "l %i\n", bus, target, lun);
4098                         if (sense_sz)
4099                                 qla1280_dump_buffer(2,
4100                                                     (char *)cmd->sense_buffer,
4101                                                     sense_sz);
4102                 }
4103         }
4104
4105         /* Place command on done queue. */
4106         list_add_tail(&sp->list, done_q);
4107  out:
4108         LEAVE("qla1280_status_entry");
4109 }
4110
4111 /*
4112  *  qla1280_error_entry
4113  *      Processes error entry.
4114  *
4115  * Input:
4116  *      ha           = adapter block pointer.
4117  *      pkt          = entry pointer.
4118  *      done_q       = done queue.
4119  */
4120 static void
4121 qla1280_error_entry(struct scsi_qla_host *ha, struct response *pkt,
4122                     struct list_head *done_q)
4123 {
4124         struct srb *sp;
4125         uint32_t handle = le32_to_cpu(pkt->handle);
4126
4127         ENTER("qla1280_error_entry");
4128
4129         if (pkt->entry_status & BIT_3)
4130                 dprintk(2, "qla1280_error_entry: BAD PAYLOAD flag error\n");
4131         else if (pkt->entry_status & BIT_2)
4132                 dprintk(2, "qla1280_error_entry: BAD HEADER flag error\n");
4133         else if (pkt->entry_status & BIT_1)
4134                 dprintk(2, "qla1280_error_entry: FULL flag error\n");
4135         else
4136                 dprintk(2, "qla1280_error_entry: UNKNOWN flag error\n");
4137
4138         /* Validate handle. */
4139         if (handle < MAX_OUTSTANDING_COMMANDS)
4140                 sp = ha->outstanding_cmds[handle];
4141         else
4142                 sp = NULL;
4143
4144         if (sp) {
4145                 /* Free outstanding command slot. */
4146                 ha->outstanding_cmds[handle] = NULL;
4147
4148                 /* Bad payload or header */
4149                 if (pkt->entry_status & (BIT_3 + BIT_2)) {
4150                         /* Bad payload or header, set error status. */
4151                         /* CMD_RESULT(sp->cmd) = CS_BAD_PAYLOAD; */
4152                         CMD_RESULT(sp->cmd) = DID_ERROR << 16;
4153                 } else if (pkt->entry_status & BIT_1) { /* FULL flag */
4154                         CMD_RESULT(sp->cmd) = DID_BUS_BUSY << 16;
4155                 } else {
4156                         /* Set error status. */
4157                         CMD_RESULT(sp->cmd) = DID_ERROR << 16;
4158                 }
4159
4160                 /* Place command on done queue. */
4161                 list_add_tail(&sp->list, done_q);
4162         }
4163 #ifdef QLA_64BIT_PTR
4164         else if (pkt->entry_type == COMMAND_A64_TYPE) {
4165                 printk(KERN_WARNING "!qla1280: Error Entry invalid handle");
4166         }
4167 #endif
4168
4169         LEAVE("qla1280_error_entry");
4170 }
4171
4172 /*
4173  *  qla1280_abort_isp
4174  *      Resets ISP and aborts all outstanding commands.
4175  *
4176  * Input:
4177  *      ha           = adapter block pointer.
4178  *
4179  * Returns:
4180  *      0 = success
4181  */
4182 static int
4183 qla1280_abort_isp(struct scsi_qla_host *ha)
4184 {
4185         struct device_reg __iomem *reg = ha->iobase;
4186         struct srb *sp;
4187         int status = 0;
4188         int cnt;
4189         int bus;
4190
4191         ENTER("qla1280_abort_isp");
4192
4193         if (ha->flags.abort_isp_active || !ha->flags.online)
4194                 goto out;
4195         
4196         ha->flags.abort_isp_active = 1;
4197
4198         /* Disable ISP interrupts. */
4199         qla1280_disable_intrs(ha);
4200         WRT_REG_WORD(&reg->host_cmd, HC_PAUSE_RISC);
4201         RD_REG_WORD(&reg->id_l);
4202
4203         printk(KERN_INFO "scsi(%li): dequeuing outstanding commands\n",
4204                ha->host_no);
4205         /* Dequeue all commands in outstanding command list. */
4206         for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
4207                 struct scsi_cmnd *cmd;
4208                 sp = ha->outstanding_cmds[cnt];
4209                 if (sp) {
4210
4211                         cmd = sp->cmd;
4212                         CMD_RESULT(cmd) = DID_RESET << 16;
4213
4214                         sp->cmd = NULL;
4215                         ha->outstanding_cmds[cnt] = NULL;
4216
4217                         (*cmd->scsi_done)(cmd);
4218
4219                         sp->flags = 0;
4220                 }
4221         }
4222
4223         status = qla1280_load_firmware(ha);
4224         if (status)
4225                 goto out;
4226
4227         /* Setup adapter based on NVRAM parameters. */
4228         qla1280_nvram_config (ha);
4229
4230         status = qla1280_init_rings(ha);
4231         if (status)
4232                 goto out;
4233                 
4234         /* Issue SCSI reset. */
4235         for (bus = 0; bus < ha->ports; bus++)
4236                 qla1280_bus_reset(ha, bus);
4237                 
4238         ha->flags.abort_isp_active = 0;
4239  out:
4240         if (status) {
4241                 printk(KERN_WARNING
4242                        "qla1280: ISP error recovery failed, board disabled");
4243                 qla1280_reset_adapter(ha);
4244                 dprintk(2, "qla1280_abort_isp: **** FAILED ****\n");
4245         }
4246
4247         LEAVE("qla1280_abort_isp");
4248         return status;
4249 }
4250
4251
4252 /*
4253  * qla1280_debounce_register
4254  *      Debounce register.
4255  *
4256  * Input:
4257  *      port = register address.
4258  *
4259  * Returns:
4260  *      register value.
4261  */
4262 static u16
4263 qla1280_debounce_register(volatile u16 __iomem * addr)
4264 {
4265         volatile u16 ret;
4266         volatile u16 ret2;
4267
4268         ret = RD_REG_WORD(addr);
4269         ret2 = RD_REG_WORD(addr);
4270
4271         if (ret == ret2)
4272                 return ret;
4273
4274         do {
4275                 cpu_relax();
4276                 ret = RD_REG_WORD(addr);
4277                 ret2 = RD_REG_WORD(addr);
4278         } while (ret != ret2);
4279
4280         return ret;
4281 }
4282
4283
4284 /************************************************************************
4285  * qla1280_check_for_dead_scsi_bus                                      *
4286  *                                                                      *
4287  *    This routine checks for a dead SCSI bus                           *
4288  ************************************************************************/
4289 #define SET_SXP_BANK            0x0100
4290 #define SCSI_PHASE_INVALID      0x87FF
4291 static int
4292 qla1280_check_for_dead_scsi_bus(struct scsi_qla_host *ha, unsigned int bus)
4293 {
4294         uint16_t config_reg, scsi_control;
4295         struct device_reg __iomem *reg = ha->iobase;
4296
4297         if (ha->bus_settings[bus].scsi_bus_dead) {
4298                 WRT_REG_WORD(&reg->host_cmd, HC_PAUSE_RISC);
4299                 config_reg = RD_REG_WORD(&reg->cfg_1);
4300                 WRT_REG_WORD(&reg->cfg_1, SET_SXP_BANK);
4301                 scsi_control = RD_REG_WORD(&reg->scsiControlPins);
4302                 WRT_REG_WORD(&reg->cfg_1, config_reg);
4303                 WRT_REG_WORD(&reg->host_cmd, HC_RELEASE_RISC);
4304
4305                 if (scsi_control == SCSI_PHASE_INVALID) {
4306                         ha->bus_settings[bus].scsi_bus_dead = 1;
4307 #if 0
4308                         CMD_RESULT(cp) = DID_NO_CONNECT << 16;
4309                         CMD_HANDLE(cp) = INVALID_HANDLE;
4310                         /* ha->actthreads--; */
4311
4312                         (*(cp)->scsi_done)(cp);
4313 #endif
4314                         return 1;       /* bus is dead */
4315                 } else {
4316                         ha->bus_settings[bus].scsi_bus_dead = 0;
4317                         ha->bus_settings[bus].failed_reset_count = 0;
4318                 }
4319         }
4320         return 0;               /* bus is not dead */
4321 }
4322
4323 static void
4324 qla1280_get_target_parameters(struct scsi_qla_host *ha,
4325                               struct scsi_device *device)
4326 {
4327         uint16_t mb[MAILBOX_REGISTER_COUNT];
4328         int bus, target, lun;
4329
4330         bus = device->channel;
4331         target = device->id;
4332         lun = device->lun;
4333
4334
4335         mb[0] = MBC_GET_TARGET_PARAMETERS;
4336         mb[1] = (uint16_t) (bus ? target | BIT_7 : target);
4337         mb[1] <<= 8;
4338         qla1280_mailbox_command(ha, BIT_6 | BIT_3 | BIT_2 | BIT_1 | BIT_0,
4339                                 &mb[0]);
4340
4341         printk(KERN_INFO "scsi(%li:%d:%d:%d):", ha->host_no, bus, target, lun);
4342
4343         if (mb[3] != 0) {
4344                 printk(" Sync: period %d, offset %d",
4345                        (mb[3] & 0xff), (mb[3] >> 8));
4346                 if (mb[2] & BIT_13)
4347                         printk(", Wide");
4348                 if ((mb[2] & BIT_5) && ((mb[6] >> 8) & 0xff) >= 2)
4349                         printk(", DT");
4350         } else
4351                 printk(" Async");
4352
4353         if (DEV_SIMPLE_TAGS(device))
4354                 printk(", Tagged queuing: depth %d", device->queue_depth);
4355         printk("\n");
4356 }
4357
4358
4359 #if DEBUG_QLA1280
4360 static void
4361 __qla1280_dump_buffer(char *b, int size)
4362 {
4363         int cnt;
4364         u8 c;
4365
4366         printk(KERN_DEBUG " 0   1   2   3   4   5   6   7   8   9   Ah  "
4367                "Bh  Ch  Dh  Eh  Fh\n");
4368         printk(KERN_DEBUG "---------------------------------------------"
4369                "------------------\n");
4370
4371         for (cnt = 0; cnt < size;) {
4372                 c = *b++;
4373
4374                 printk("0x%02x", c);
4375                 cnt++;
4376                 if (!(cnt % 16))
4377                         printk("\n");
4378                 else
4379                         printk(" ");
4380         }
4381         if (cnt % 16)
4382                 printk("\n");
4383 }
4384
4385 /**************************************************************************
4386  *   ql1280_print_scsi_cmd
4387  *
4388  **************************************************************************/
4389 static void
4390 __qla1280_print_scsi_cmd(struct scsi_cmnd *cmd)
4391 {
4392         struct scsi_qla_host *ha;
4393         struct Scsi_Host *host = CMD_HOST(cmd);
4394         struct srb *sp;
4395         /* struct scatterlist *sg; */
4396
4397         int i;
4398         ha = (struct scsi_qla_host *)host->hostdata;
4399
4400         sp = (struct srb *)CMD_SP(cmd);
4401         printk("SCSI Command @= 0x%p, Handle=0x%p\n", cmd, CMD_HANDLE(cmd));
4402         printk("  chan=%d, target = 0x%02x, lun = 0x%02x, cmd_len = 0x%02x\n",
4403                SCSI_BUS_32(cmd), SCSI_TCN_32(cmd), SCSI_LUN_32(cmd),
4404                CMD_CDBLEN(cmd));
4405         printk(" CDB = ");
4406         for (i = 0; i < cmd->cmd_len; i++) {
4407                 printk("0x%02x ", cmd->cmnd[i]);
4408         }
4409         printk("  seg_cnt =%d\n", cmd->use_sg);
4410         printk("  request buffer=0x%p, request buffer len=0x%x\n",
4411                cmd->request_buffer, cmd->request_bufflen);
4412         /* if (cmd->use_sg)
4413            {
4414            sg = (struct scatterlist *) cmd->request_buffer;
4415            printk("  SG buffer: \n");
4416            qla1280_dump_buffer(1, (char *)sg, (cmd->use_sg*sizeof(struct scatterlist)));
4417            } */
4418         printk("  tag=%d, transfersize=0x%x \n",
4419                cmd->tag, cmd->transfersize);
4420         printk("  Pid=%li, SP=0x%p\n", cmd->pid, CMD_SP(cmd));
4421         printk(" underflow size = 0x%x, direction=0x%x\n",
4422                cmd->underflow, cmd->sc_data_direction);
4423 }
4424
4425 /**************************************************************************
4426  *   ql1280_dump_device
4427  *
4428  **************************************************************************/
4429 static void
4430 ql1280_dump_device(struct scsi_qla_host *ha)
4431 {
4432
4433         struct scsi_cmnd *cp;
4434         struct srb *sp;
4435         int i;
4436
4437         printk(KERN_DEBUG "Outstanding Commands on controller:\n");
4438
4439         for (i = 0; i < MAX_OUTSTANDING_COMMANDS; i++) {
4440                 if ((sp = ha->outstanding_cmds[i]) == NULL)
4441                         continue;
4442                 if ((cp = sp->cmd) == NULL)
4443                         continue;
4444                 qla1280_print_scsi_cmd(1, cp);
4445         }
4446 }
4447 #endif
4448
4449
4450 enum tokens {
4451         TOKEN_NVRAM,
4452         TOKEN_SYNC,
4453         TOKEN_WIDE,
4454         TOKEN_PPR,
4455         TOKEN_VERBOSE,
4456         TOKEN_DEBUG,
4457 };
4458
4459 struct setup_tokens {
4460         char *token;
4461         int val;
4462 };
4463
4464 static struct setup_tokens setup_token[] __initdata = 
4465 {
4466         { "nvram", TOKEN_NVRAM },
4467         { "sync", TOKEN_SYNC },
4468         { "wide", TOKEN_WIDE },
4469         { "ppr", TOKEN_PPR },
4470         { "verbose", TOKEN_VERBOSE },
4471         { "debug", TOKEN_DEBUG },
4472 };
4473
4474
4475 /**************************************************************************
4476  *   qla1280_setup
4477  *
4478  *   Handle boot parameters. This really needs to be changed so one
4479  *   can specify per adapter parameters.
4480  **************************************************************************/
4481 static int __init
4482 qla1280_setup(char *s)
4483 {
4484         char *cp, *ptr;
4485         unsigned long val;
4486         int toke;
4487
4488         cp = s;
4489
4490         while (cp && (ptr = strchr(cp, ':'))) {
4491                 ptr++;
4492                 if (!strcmp(ptr, "yes")) {
4493                         val = 0x10000;
4494                         ptr += 3;
4495                 } else if (!strcmp(ptr, "no")) {
4496                         val = 0;
4497                         ptr += 2;
4498                 } else
4499                         val = simple_strtoul(ptr, &ptr, 0);
4500
4501                 switch ((toke = qla1280_get_token(cp))) {
4502                 case TOKEN_NVRAM:
4503                         if (!val)
4504                                 driver_setup.no_nvram = 1;
4505                         break;
4506                 case TOKEN_SYNC:
4507                         if (!val)
4508                                 driver_setup.no_sync = 1;
4509                         else if (val != 0x10000)
4510                                 driver_setup.sync_mask = val;
4511                         break;
4512                 case TOKEN_WIDE:
4513                         if (!val)
4514                                 driver_setup.no_wide = 1;
4515                         else if (val != 0x10000)
4516                                 driver_setup.wide_mask = val;
4517                         break;
4518                 case TOKEN_PPR:
4519                         if (!val)
4520                                 driver_setup.no_ppr = 1;
4521                         else if (val != 0x10000)
4522                                 driver_setup.ppr_mask = val;
4523                         break;
4524                 case TOKEN_VERBOSE:
4525                         qla1280_verbose = val;
4526                         break;
4527                 default:
4528                         printk(KERN_INFO "qla1280: unknown boot option %s\n",
4529                                cp);
4530                 }
4531
4532                 cp = strchr(ptr, ';');
4533                 if (cp)
4534                         cp++;
4535                 else {
4536                         break;
4537                 }
4538         }
4539         return 1;
4540 }
4541
4542
4543 static int
4544 qla1280_get_token(char *str)
4545 {
4546         char *sep;
4547         long ret = -1;
4548         int i, len;
4549
4550         len = sizeof(setup_token)/sizeof(struct setup_tokens);
4551
4552         sep = strchr(str, ':');
4553
4554         if (sep) {
4555                 for (i = 0; i < len; i++){
4556
4557                         if (!strncmp(setup_token[i].token, str, (sep - str))) {
4558                                 ret =  setup_token[i].val;
4559                                 break;
4560                         }
4561                 }
4562         }
4563
4564         return ret;
4565 }
4566
4567 #if LINUX_VERSION_CODE >= 0x020600
4568 static struct scsi_host_template qla1280_driver_template = {
4569         .module                 = THIS_MODULE,
4570         .proc_name              = "qla1280",
4571         .name                   = "Qlogic ISP 1280/12160",
4572         .info                   = qla1280_info,
4573         .slave_configure        = qla1280_slave_configure,
4574         .queuecommand           = qla1280_queuecommand,
4575         .eh_abort_handler       = qla1280_eh_abort,
4576         .eh_device_reset_handler= qla1280_eh_device_reset,
4577         .eh_bus_reset_handler   = qla1280_eh_bus_reset,
4578         .eh_host_reset_handler  = qla1280_eh_adapter_reset,
4579         .bios_param             = qla1280_biosparam,
4580         .can_queue              = 0xfffff,
4581         .this_id                = -1,
4582         .sg_tablesize           = SG_ALL,
4583         .cmd_per_lun            = 1,
4584         .use_clustering         = ENABLE_CLUSTERING,
4585 };
4586 #else
4587 static Scsi_Host_Template qla1280_driver_template = {
4588         .proc_name              = "qla1280",
4589         .name                   = "Qlogic ISP 1280/12160",
4590         .detect                 = qla1280_detect,
4591         .release                = qla1280_release,
4592         .info                   = qla1280_info,
4593         .queuecommand           = qla1280_queuecommand,
4594         .eh_abort_handler       = qla1280_eh_abort,
4595         .eh_device_reset_handler= qla1280_eh_device_reset,
4596         .eh_bus_reset_handler   = qla1280_eh_bus_reset,
4597         .eh_host_reset_handler  = qla1280_eh_adapter_reset,
4598         .bios_param             = qla1280_biosparam_old,
4599         .can_queue              = 0xfffff,
4600         .this_id                = -1,
4601         .sg_tablesize           = SG_ALL,
4602         .cmd_per_lun            = 1,
4603         .use_clustering         = ENABLE_CLUSTERING,
4604         .use_new_eh_code        = 1,
4605 };
4606 #endif
4607
4608 static int __devinit
4609 qla1280_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
4610 {
4611         int devnum = id->driver_data;
4612         struct qla_boards *bdp = &ql1280_board_tbl[devnum];
4613         struct Scsi_Host *host;
4614         struct scsi_qla_host *ha;
4615         int error = -ENODEV;
4616
4617         /* Bypass all AMI SUBSYS VENDOR IDs */
4618         if (pdev->subsystem_vendor == PCI_VENDOR_ID_AMI) {
4619                 printk(KERN_INFO
4620                        "qla1280: Skipping AMI SubSys Vendor ID Chip\n");
4621                 goto error;
4622         }
4623
4624         printk(KERN_INFO "qla1280: %s found on PCI bus %i, dev %i\n",
4625                bdp->name, pdev->bus->number, PCI_SLOT(pdev->devfn));
4626         
4627         if (pci_enable_device(pdev)) {
4628                 printk(KERN_WARNING
4629                        "qla1280: Failed to enabled pci device, aborting.\n");
4630                 goto error;
4631         }
4632
4633         pci_set_master(pdev);
4634
4635         error = -ENOMEM;
4636         host = scsi_host_alloc(&qla1280_driver_template, sizeof(*ha));
4637         if (!host) {
4638                 printk(KERN_WARNING
4639                        "qla1280: Failed to register host, aborting.\n");
4640                 goto error_disable_device;
4641         }
4642
4643         ha = (struct scsi_qla_host *)host->hostdata;
4644         memset(ha, 0, sizeof(struct scsi_qla_host));
4645
4646         ha->pdev = pdev;
4647         ha->devnum = devnum;    /* specifies microcode load address */
4648
4649 #ifdef QLA_64BIT_PTR
4650         if (pci_set_dma_mask(ha->pdev, (dma_addr_t) ~ 0ULL)) {
4651                 if (pci_set_dma_mask(ha->pdev, 0xffffffff)) {
4652                         printk(KERN_WARNING "scsi(%li): Unable to set a "
4653                                " suitable DMA mask - aboring\n", ha->host_no);
4654                         error = -ENODEV;
4655                         goto error_free_irq;
4656                 }
4657         } else
4658                 dprintk(2, "scsi(%li): 64 Bit PCI Addressing Enabled\n",
4659                         ha->host_no);
4660 #else
4661         if (pci_set_dma_mask(ha->pdev, 0xffffffff)) {
4662                 printk(KERN_WARNING "scsi(%li): Unable to set a "
4663                        " suitable DMA mask - aboring\n", ha->host_no);
4664                 error = -ENODEV;
4665                 goto error_free_irq;
4666         }
4667 #endif
4668
4669         ha->request_ring = pci_alloc_consistent(ha->pdev,
4670                         ((REQUEST_ENTRY_CNT + 1) * (sizeof(request_t))),
4671                         &ha->request_dma);
4672         if (!ha->request_ring) {
4673                 printk(KERN_INFO "qla1280: Failed to get request memory\n");
4674                 goto error_put_host;
4675         }
4676
4677         ha->response_ring = pci_alloc_consistent(ha->pdev,
4678                         ((RESPONSE_ENTRY_CNT + 1) * (sizeof(struct response))),
4679                         &ha->response_dma);
4680         if (!ha->response_ring) {
4681                 printk(KERN_INFO "qla1280: Failed to get response memory\n");
4682                 goto error_free_request_ring;
4683         }
4684
4685         ha->ports = bdp->numPorts;
4686
4687         ha->host = host;
4688         ha->host_no = host->host_no;
4689
4690         host->irq = pdev->irq;
4691         host->max_channel = bdp->numPorts - 1;
4692         host->max_lun = MAX_LUNS - 1;
4693         host->max_id = MAX_TARGETS;
4694         host->max_sectors = 1024;
4695         host->unique_id = host->host_no;
4696
4697 #if LINUX_VERSION_CODE < 0x020545
4698         host->select_queue_depths = qla1280_select_queue_depth;
4699 #endif
4700
4701         error = -ENODEV;
4702
4703 #if MEMORY_MAPPED_IO
4704         ha->mmpbase = ioremap(pci_resource_start(ha->pdev, 1),
4705                               pci_resource_len(ha->pdev, 1));
4706         if (!ha->mmpbase) {
4707                 printk(KERN_INFO "qla1280: Unable to map I/O memory\n");
4708                 goto error_free_response_ring;
4709         }
4710
4711         host->base = (unsigned long)ha->mmpbase;
4712         ha->iobase = (struct device_reg __iomem *)ha->mmpbase;
4713 #else
4714         host->io_port = pci_resource_start(ha->pdev, 0);
4715         if (!request_region(host->io_port, 0xff, "qla1280")) {
4716                 printk(KERN_INFO "qla1280: Failed to reserve i/o region "
4717                                  "0x%04lx-0x%04lx - already in use\n",
4718                        host->io_port, host->io_port + 0xff);
4719                 goto error_free_response_ring;
4720         }
4721
4722         ha->iobase = (struct device_reg *)host->io_port;
4723 #endif
4724
4725         INIT_LIST_HEAD(&ha->done_q);
4726
4727         /* Disable ISP interrupts. */
4728         qla1280_disable_intrs(ha);
4729
4730         if (request_irq(pdev->irq, qla1280_intr_handler, SA_SHIRQ,
4731                                 "qla1280", ha)) {
4732                 printk("qla1280 : Failed to reserve interrupt %d already "
4733                        "in use\n", pdev->irq);
4734                 goto error_release_region;
4735         }
4736
4737         /* load the F/W, read paramaters, and init the H/W */
4738         if (qla1280_initialize_adapter(ha)) {
4739                 printk(KERN_INFO "qla1x160: Failed to initialize adapter\n");
4740                 goto error_free_irq;
4741         }
4742
4743         /* set our host ID  (need to do something about our two IDs) */
4744         host->this_id = ha->bus_settings[0].id;
4745
4746         pci_set_drvdata(pdev, host);
4747
4748 #if LINUX_VERSION_CODE >= 0x020600
4749         error = scsi_add_host(host, &pdev->dev);
4750         if (error)
4751                 goto error_disable_adapter;
4752         scsi_scan_host(host);
4753 #else
4754         scsi_set_pci_device(host, pdev);
4755 #endif
4756
4757         return 0;
4758
4759 #if LINUX_VERSION_CODE >= 0x020600
4760  error_disable_adapter:
4761         WRT_REG_WORD(&ha->iobase->ictrl, 0);
4762 #endif
4763  error_free_irq:
4764         free_irq(pdev->irq, ha);
4765  error_release_region:
4766 #if MEMORY_MAPPED_IO
4767         iounmap(ha->mmpbase);
4768 #else
4769         release_region(host->io_port, 0xff);
4770 #endif
4771  error_free_response_ring:
4772         pci_free_consistent(ha->pdev,
4773                         ((RESPONSE_ENTRY_CNT + 1) * (sizeof(struct response))),
4774                         ha->response_ring, ha->response_dma);
4775  error_free_request_ring:
4776         pci_free_consistent(ha->pdev,
4777                         ((REQUEST_ENTRY_CNT + 1) * (sizeof(request_t))),
4778                         ha->request_ring, ha->request_dma);
4779  error_put_host:
4780         scsi_host_put(host);
4781  error_disable_device:
4782         pci_disable_device(pdev);
4783  error:
4784         return error;
4785 }
4786
4787
4788 static void __devexit
4789 qla1280_remove_one(struct pci_dev *pdev)
4790 {
4791         struct Scsi_Host *host = pci_get_drvdata(pdev);
4792         struct scsi_qla_host *ha = (struct scsi_qla_host *)host->hostdata;
4793
4794 #if LINUX_VERSION_CODE >= 0x020600
4795         scsi_remove_host(host);
4796 #endif
4797
4798         WRT_REG_WORD(&ha->iobase->ictrl, 0);
4799
4800         free_irq(pdev->irq, ha);
4801
4802 #if MEMORY_MAPPED_IO
4803         iounmap(ha->mmpbase);
4804 #else
4805         release_region(host->io_port, 0xff);
4806 #endif
4807
4808         pci_free_consistent(ha->pdev,
4809                         ((REQUEST_ENTRY_CNT + 1) * (sizeof(request_t))),
4810                         ha->request_ring, ha->request_dma);
4811         pci_free_consistent(ha->pdev,
4812                         ((RESPONSE_ENTRY_CNT + 1) * (sizeof(struct response))),
4813                         ha->response_ring, ha->response_dma);
4814
4815         pci_disable_device(pdev);
4816
4817         scsi_host_put(host);
4818 }
4819
4820 #if LINUX_VERSION_CODE >= 0x020600
4821 static struct pci_driver qla1280_pci_driver = {
4822         .name           = "qla1280",
4823         .id_table       = qla1280_pci_tbl,
4824         .probe          = qla1280_probe_one,
4825         .remove         = __devexit_p(qla1280_remove_one),
4826 };
4827
4828 static int __init
4829 qla1280_init(void)
4830 {
4831         if (sizeof(struct srb) > sizeof(struct scsi_pointer)) {
4832                 printk(KERN_WARNING
4833                        "qla1280: struct srb too big, aborting\n");
4834                 return -EINVAL;
4835         }
4836
4837 #ifdef MODULE
4838         /*
4839          * If we are called as a module, the qla1280 pointer may not be null
4840          * and it would point to our bootup string, just like on the lilo
4841          * command line.  IF not NULL, then process this config string with
4842          * qla1280_setup
4843          *
4844          * Boot time Options
4845          * To add options at boot time add a line to your lilo.conf file like:
4846          * append="qla1280=verbose,max_tags:{{255,255,255,255},{255,255,255,255}}"
4847          * which will result in the first four devices on the first two
4848          * controllers being set to a tagged queue depth of 32.
4849          */
4850         if (qla1280)
4851                 qla1280_setup(qla1280);
4852 #endif
4853
4854         return pci_module_init(&qla1280_pci_driver);
4855 }
4856
4857 static void __exit
4858 qla1280_exit(void)
4859 {
4860         pci_unregister_driver(&qla1280_pci_driver);
4861 }
4862
4863 module_init(qla1280_init);
4864 module_exit(qla1280_exit);
4865
4866 #else
4867 # define driver_template qla1280_driver_template
4868 # include "scsi_module.c"
4869 #endif
4870
4871 MODULE_AUTHOR("Qlogic & Jes Sorensen");
4872 MODULE_DESCRIPTION("Qlogic ISP SCSI (qla1x80/qla1x160) driver");
4873 MODULE_LICENSE("GPL");
4874 MODULE_VERSION(QLA1280_VERSION);
4875
4876 /*
4877  * Overrides for Emacs so that we almost follow Linus's tabbing style.
4878  * Emacs will notice this stuff at the end of the file and automatically
4879  * adjust the settings for this buffer only.  This must remain at the end
4880  * of the file.
4881  * ---------------------------------------------------------------------------
4882  * Local variables:
4883  * c-basic-offset: 8
4884  * tab-width: 8
4885  * End:
4886  */