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