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