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