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