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