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