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