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