[PATCH] sata_sil: disable hotplug interrupts on two ATI IXPs
[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 SA_INTERRUPT, 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/config.h>
335 #include <linux/module.h>
336
337 #include <linux/version.h>
338 #include <linux/types.h>
339 #include <linux/string.h>
340 #include <linux/errno.h>
341 #include <linux/kernel.h>
342 #include <linux/ioport.h>
343 #include <linux/delay.h>
344 #include <linux/timer.h>
345 #include <linux/sched.h>
346 #include <linux/pci.h>
347 #include <linux/proc_fs.h>
348 #include <linux/stat.h>
349 #include <linux/slab.h>
350 #include <linux/pci_ids.h>
351 #include <linux/interrupt.h>
352 #include <linux/init.h>
353 #include <linux/dma-mapping.h>
354
355 #include <asm/io.h>
356 #include <asm/irq.h>
357 #include <asm/byteorder.h>
358 #include <asm/processor.h>
359 #include <asm/types.h>
360 #include <asm/system.h>
361
362 #include <scsi/scsi.h>
363 #include <scsi/scsi_cmnd.h>
364 #include <scsi/scsi_device.h>
365 #include <scsi/scsi_host.h>
366 #include <scsi/scsi_tcq.h>
367
368 #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
369 #include <asm/sn/io.h>
370 #endif
371
372 #if LINUX_VERSION_CODE < 0x020600
373 #error "Kernels older than 2.6.0 are no longer supported"
374 #endif
375
376
377 /*
378  * Compile time Options:
379  *            0 - Disable and 1 - Enable
380  */
381 #define  DEBUG_QLA1280_INTR     0
382 #define  DEBUG_PRINT_NVRAM      0
383 #define  DEBUG_QLA1280          0
384
385 /*
386  * The SGI VISWS is broken and doesn't support MMIO ;-(
387  */
388 #ifdef CONFIG_X86_VISWS
389 #define MEMORY_MAPPED_IO        0
390 #else
391 #define MEMORY_MAPPED_IO        1
392 #endif
393
394 #define UNIQUE_FW_NAME
395 #include "qla1280.h"
396 #include "ql12160_fw.h"         /* ISP RISC codes */
397 #include "ql1280_fw.h"
398 #include "ql1040_fw.h"
399
400 #ifndef BITS_PER_LONG
401 #error "BITS_PER_LONG not defined!"
402 #endif
403 #if (BITS_PER_LONG == 64) || defined CONFIG_HIGHMEM
404 #define QLA_64BIT_PTR   1
405 #endif
406
407 #ifdef QLA_64BIT_PTR
408 #define pci_dma_hi32(a)                 ((a >> 16) >> 16)
409 #else
410 #define pci_dma_hi32(a)                 0
411 #endif
412 #define pci_dma_lo32(a)                 (a & 0xffffffff)
413
414 #define NVRAM_DELAY()                   udelay(500)     /* 2 microseconds */
415
416 #if defined(__ia64__) && !defined(ia64_platform_is)
417 #define ia64_platform_is(foo)           (!strcmp(x, platform_name))
418 #endif
419
420
421 #define IS_ISP1040(ha) (ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP1020)
422 #define IS_ISP1x40(ha) (ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP1020 || \
423                         ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP1240)
424 #define IS_ISP1x160(ha)        (ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP10160 || \
425                                 ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP12160)
426
427
428 static int qla1280_probe_one(struct pci_dev *, const struct pci_device_id *);
429 static void qla1280_remove_one(struct pci_dev *);
430
431 /*
432  *  QLogic Driver Support Function Prototypes.
433  */
434 static void qla1280_done(struct scsi_qla_host *);
435 static int qla1280_get_token(char *);
436 static int qla1280_setup(char *s) __init;
437
438 /*
439  *  QLogic ISP1280 Hardware Support Function Prototypes.
440  */
441 static int qla1280_load_firmware(struct scsi_qla_host *);
442 static int qla1280_init_rings(struct scsi_qla_host *);
443 static int qla1280_nvram_config(struct scsi_qla_host *);
444 static int qla1280_mailbox_command(struct scsi_qla_host *,
445                                    uint8_t, uint16_t *);
446 static int qla1280_bus_reset(struct scsi_qla_host *, int);
447 static int qla1280_device_reset(struct scsi_qla_host *, int, int);
448 static int qla1280_abort_device(struct scsi_qla_host *, int, int, int);
449 static int qla1280_abort_command(struct scsi_qla_host *, struct srb *, int);
450 static int qla1280_abort_isp(struct scsi_qla_host *);
451 #ifdef QLA_64BIT_PTR
452 static int qla1280_64bit_start_scsi(struct scsi_qla_host *, struct srb *);
453 #else
454 static int qla1280_32bit_start_scsi(struct scsi_qla_host *, struct srb *);
455 #endif
456 static void qla1280_nv_write(struct scsi_qla_host *, uint16_t);
457 static void qla1280_poll(struct scsi_qla_host *);
458 static void qla1280_reset_adapter(struct scsi_qla_host *);
459 static void qla1280_marker(struct scsi_qla_host *, int, int, int, u8);
460 static void qla1280_isp_cmd(struct scsi_qla_host *);
461 static void qla1280_isr(struct scsi_qla_host *, struct list_head *);
462 static void qla1280_rst_aen(struct scsi_qla_host *);
463 static void qla1280_status_entry(struct scsi_qla_host *, struct response *,
464                                  struct list_head *);
465 static void qla1280_error_entry(struct scsi_qla_host *, struct response *,
466                                 struct list_head *);
467 static uint16_t qla1280_get_nvram_word(struct scsi_qla_host *, uint32_t);
468 static uint16_t qla1280_nvram_request(struct scsi_qla_host *, uint32_t);
469 static uint16_t qla1280_debounce_register(volatile uint16_t __iomem *);
470 static request_t *qla1280_req_pkt(struct scsi_qla_host *);
471 static int qla1280_check_for_dead_scsi_bus(struct scsi_qla_host *,
472                                            unsigned int);
473 static void qla1280_get_target_parameters(struct scsi_qla_host *,
474                                            struct scsi_device *);
475 static int qla1280_set_target_parameters(struct scsi_qla_host *, int, int);
476
477
478 static struct qla_driver_setup driver_setup;
479
480 /*
481  * convert scsi data direction to request_t control flags
482  */
483 static inline uint16_t
484 qla1280_data_direction(struct scsi_cmnd *cmnd)
485 {
486         switch(cmnd->sc_data_direction) {
487         case DMA_FROM_DEVICE:
488                 return BIT_5;
489         case DMA_TO_DEVICE:
490                 return BIT_6;
491         case DMA_BIDIRECTIONAL:
492                 return BIT_5 | BIT_6;
493         /*
494          * We could BUG() on default here if one of the four cases aren't
495          * met, but then again if we receive something like that from the
496          * SCSI layer we have more serious problems. This shuts up GCC.
497          */
498         case DMA_NONE:
499         default:
500                 return 0;
501         }
502 }
503                 
504 #if DEBUG_QLA1280
505 static void __qla1280_print_scsi_cmd(struct scsi_cmnd * cmd);
506 static void __qla1280_dump_buffer(char *, int);
507 #endif
508
509
510 /*
511  * insmod needs to find the variable and make it point to something
512  */
513 #ifdef MODULE
514 static char *qla1280;
515
516 /* insmod qla1280 options=verbose" */
517 module_param(qla1280, charp, 0);
518 #else
519 __setup("qla1280=", qla1280_setup);
520 #endif
521
522
523 /*
524  * We use the scsi_pointer structure that's included with each scsi_command
525  * to overlay our struct srb over it. qla1280_init() checks that a srb is not
526  * bigger than a scsi_pointer.
527  */
528
529 #define CMD_SP(Cmnd)            &Cmnd->SCp
530 #define CMD_CDBLEN(Cmnd)        Cmnd->cmd_len
531 #define CMD_CDBP(Cmnd)          Cmnd->cmnd
532 #define CMD_SNSP(Cmnd)          Cmnd->sense_buffer
533 #define CMD_SNSLEN(Cmnd)        sizeof(Cmnd->sense_buffer)
534 #define CMD_RESULT(Cmnd)        Cmnd->result
535 #define CMD_HANDLE(Cmnd)        Cmnd->host_scribble
536 #define CMD_REQUEST(Cmnd)       Cmnd->request->cmd
537
538 #define CMD_HOST(Cmnd)          Cmnd->device->host
539 #define SCSI_BUS_32(Cmnd)       Cmnd->device->channel
540 #define SCSI_TCN_32(Cmnd)       Cmnd->device->id
541 #define SCSI_LUN_32(Cmnd)       Cmnd->device->lun
542
543
544 /*****************************************/
545 /*   ISP Boards supported by this driver */
546 /*****************************************/
547
548 struct qla_boards {
549         unsigned char name[9];  /* Board ID String */
550         int numPorts;           /* Number of SCSI ports */
551         unsigned short *fwcode; /* pointer to FW array         */
552         unsigned short *fwlen;  /* number of words in array    */
553         unsigned short *fwstart;        /* start address for F/W       */
554         unsigned char *fwver;   /* Ptr to F/W version array    */
555 };
556
557 /* NOTE: the last argument in each entry is used to index ql1280_board_tbl */
558 static struct pci_device_id qla1280_pci_tbl[] = {
559         {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP12160,
560                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
561         {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1020,
562                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
563         {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1080,
564                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
565         {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1240,
566                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3},
567         {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1280,
568                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4},
569         {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP10160,
570                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5},
571         {0,}
572 };
573 MODULE_DEVICE_TABLE(pci, qla1280_pci_tbl);
574
575 static struct qla_boards ql1280_board_tbl[] = {
576         /* Name ,  Number of ports, FW details */
577         {"QLA12160", 2, &fw12160i_code01[0], &fw12160i_length01,
578          &fw12160i_addr01, &fw12160i_version_str[0]},
579         {"QLA1040", 1, &risc_code01[0], &risc_code_length01,
580          &risc_code_addr01, &firmware_version[0]},
581         {"QLA1080", 1, &fw1280ei_code01[0], &fw1280ei_length01,
582          &fw1280ei_addr01, &fw1280ei_version_str[0]},
583         {"QLA1240", 2, &fw1280ei_code01[0], &fw1280ei_length01,
584          &fw1280ei_addr01, &fw1280ei_version_str[0]},
585         {"QLA1280", 2, &fw1280ei_code01[0], &fw1280ei_length01,
586          &fw1280ei_addr01, &fw1280ei_version_str[0]},
587         {"QLA10160", 1, &fw12160i_code01[0], &fw12160i_length01,
588          &fw12160i_addr01, &fw12160i_version_str[0]},
589         {"        ", 0}
590 };
591
592 static int qla1280_verbose = 1;
593
594 #if DEBUG_QLA1280
595 static int ql_debug_level = 1;
596 #define dprintk(level, format, a...)    \
597         do { if (ql_debug_level >= level) printk(KERN_ERR format, ##a); } while(0)
598 #define qla1280_dump_buffer(level, buf, size)   \
599         if (ql_debug_level >= level) __qla1280_dump_buffer(buf, size)
600 #define qla1280_print_scsi_cmd(level, cmd)      \
601         if (ql_debug_level >= level) __qla1280_print_scsi_cmd(cmd)
602 #else
603 #define ql_debug_level                  0
604 #define dprintk(level, format, a...)    do{}while(0)
605 #define qla1280_dump_buffer(a, b, c)    do{}while(0)
606 #define qla1280_print_scsi_cmd(a, b)    do{}while(0)
607 #endif
608
609 #define ENTER(x)                dprintk(3, "qla1280 : Entering %s()\n", x);
610 #define LEAVE(x)                dprintk(3, "qla1280 : Leaving %s()\n", x);
611 #define ENTER_INTR(x)           dprintk(4, "qla1280 : Entering %s()\n", x);
612 #define LEAVE_INTR(x)           dprintk(4, "qla1280 : Leaving %s()\n", x);
613
614
615 static int qla1280_read_nvram(struct scsi_qla_host *ha)
616 {
617         uint16_t *wptr;
618         uint8_t chksum;
619         int cnt, i;
620         struct nvram *nv;
621
622         ENTER("qla1280_read_nvram");
623
624         if (driver_setup.no_nvram)
625                 return 1;
626
627         printk(KERN_INFO "scsi(%ld): Reading NVRAM\n", ha->host_no);
628
629         wptr = (uint16_t *)&ha->nvram;
630         nv = &ha->nvram;
631         chksum = 0;
632         for (cnt = 0; cnt < 3; cnt++) {
633                 *wptr = qla1280_get_nvram_word(ha, cnt);
634                 chksum += *wptr & 0xff;
635                 chksum += (*wptr >> 8) & 0xff;
636                 wptr++;
637         }
638
639         if (nv->id0 != 'I' || nv->id1 != 'S' ||
640             nv->id2 != 'P' || nv->id3 != ' ' || nv->version < 1) {
641                 dprintk(2, "Invalid nvram ID or version!\n");
642                 chksum = 1;
643         } else {
644                 for (; cnt < sizeof(struct nvram); cnt++) {
645                         *wptr = qla1280_get_nvram_word(ha, cnt);
646                         chksum += *wptr & 0xff;
647                         chksum += (*wptr >> 8) & 0xff;
648                         wptr++;
649                 }
650         }
651
652         dprintk(3, "qla1280_read_nvram: NVRAM Magic ID= %c %c %c %02x"
653                " version %i\n", nv->id0, nv->id1, nv->id2, nv->id3,
654                nv->version);
655
656
657         if (chksum) {
658                 if (!driver_setup.no_nvram)
659                         printk(KERN_WARNING "scsi(%ld): Unable to identify or "
660                                "validate NVRAM checksum, using default "
661                                "settings\n", ha->host_no);
662                 ha->nvram_valid = 0;
663         } else
664                 ha->nvram_valid = 1;
665
666         /* The firmware interface is, um, interesting, in that the
667          * actual firmware image on the chip is little endian, thus,
668          * the process of taking that image to the CPU would end up
669          * little endian.  However, the firmare interface requires it
670          * to be read a word (two bytes) at a time.
671          *
672          * The net result of this would be that the word (and
673          * doubleword) quantites in the firmware would be correct, but
674          * the bytes would be pairwise reversed.  Since most of the
675          * firmware quantites are, in fact, bytes, we do an extra
676          * le16_to_cpu() in the firmware read routine.
677          *
678          * The upshot of all this is that the bytes in the firmware
679          * are in the correct places, but the 16 and 32 bit quantites
680          * are still in little endian format.  We fix that up below by
681          * doing extra reverses on them */
682         nv->isp_parameter = cpu_to_le16(nv->isp_parameter);
683         nv->firmware_feature.w = cpu_to_le16(nv->firmware_feature.w);
684         for(i = 0; i < MAX_BUSES; i++) {
685                 nv->bus[i].selection_timeout = cpu_to_le16(nv->bus[i].selection_timeout);
686                 nv->bus[i].max_queue_depth = cpu_to_le16(nv->bus[i].max_queue_depth);
687         }
688         dprintk(1, "qla1280_read_nvram: Completed Reading NVRAM\n");
689         LEAVE("qla1280_read_nvram");
690
691         return chksum;
692 }
693
694 /**************************************************************************
695  *   qla1280_info
696  *     Return a string describing the driver.
697  **************************************************************************/
698 static const char *
699 qla1280_info(struct Scsi_Host *host)
700 {
701         static char qla1280_scsi_name_buffer[125];
702         char *bp;
703         struct scsi_qla_host *ha;
704         struct qla_boards *bdp;
705
706         bp = &qla1280_scsi_name_buffer[0];
707         ha = (struct scsi_qla_host *)host->hostdata;
708         bdp = &ql1280_board_tbl[ha->devnum];
709         memset(bp, 0, sizeof(qla1280_scsi_name_buffer));
710
711         sprintf (bp,
712                  "QLogic %s PCI to SCSI Host Adapter\n"
713                  "       Firmware version: %2d.%02d.%02d, Driver version %s",
714                  &bdp->name[0], bdp->fwver[0], bdp->fwver[1], bdp->fwver[2],
715                  QLA1280_VERSION);
716         return bp;
717 }
718
719 /**************************************************************************
720  *   qla1200_queuecommand
721  *     Queue a command to the controller.
722  *
723  * Note:
724  * The mid-level driver tries to ensures that queuecommand never gets invoked
725  * concurrently with itself or the interrupt handler (although the
726  * interrupt handler may call this routine as part of request-completion
727  * handling).   Unfortunely, it sometimes calls the scheduler in interrupt
728  * context which is a big NO! NO!.
729  **************************************************************************/
730 static int
731 qla1280_queuecommand(struct scsi_cmnd *cmd, void (*fn)(struct scsi_cmnd *))
732 {
733         struct Scsi_Host *host = cmd->device->host;
734         struct scsi_qla_host *ha = (struct scsi_qla_host *)host->hostdata;
735         struct srb *sp = (struct srb *)&cmd->SCp;
736         int status;
737
738         cmd->scsi_done = fn;
739         sp->cmd = cmd;
740         sp->flags = 0;
741
742         qla1280_print_scsi_cmd(5, cmd);
743
744 #ifdef QLA_64BIT_PTR
745         /*
746          * Using 64 bit commands if the PCI bridge doesn't support it is a
747          * bit wasteful, however this should really only happen if one's
748          * PCI controller is completely broken, like the BCM1250. For
749          * sane hardware this is not an issue.
750          */
751         status = qla1280_64bit_start_scsi(ha, sp);
752 #else
753         status = qla1280_32bit_start_scsi(ha, sp);
754 #endif
755         return status;
756 }
757
758 enum action {
759         ABORT_COMMAND,
760         ABORT_DEVICE,
761         DEVICE_RESET,
762         BUS_RESET,
763         ADAPTER_RESET,
764         FAIL
765 };
766
767 /* timer action for error action processor */
768 static void qla1280_error_wait_timeout(unsigned long __data)
769 {
770         struct scsi_cmnd *cmd = (struct scsi_cmnd *)__data;
771         struct srb *sp = (struct srb *)CMD_SP(cmd);
772
773         complete(sp->wait);
774 }
775
776 static void qla1280_mailbox_timeout(unsigned long __data)
777 {
778         struct scsi_qla_host *ha = (struct scsi_qla_host *)__data;
779         struct device_reg __iomem *reg;
780         reg = ha->iobase;
781
782         ha->mailbox_out[0] = RD_REG_WORD(&reg->mailbox0);
783         printk(KERN_ERR "scsi(%ld): mailbox timed out, mailbox0 %04x, "
784                "ictrl %04x, istatus %04x\n", ha->host_no, ha->mailbox_out[0],
785                RD_REG_WORD(&reg->ictrl), RD_REG_WORD(&reg->istatus));
786         complete(ha->mailbox_wait);
787 }
788
789 /**************************************************************************
790  * qla1200_error_action
791  *    The function will attempt to perform a specified error action and
792  *    wait for the results (or time out).
793  *
794  * Input:
795  *      cmd = Linux SCSI command packet of the command that cause the
796  *            bus reset.
797  *      action = error action to take (see action_t)
798  *
799  * Returns:
800  *      SUCCESS or FAILED
801  *
802  * Note:
803  *      Resetting the bus always succeeds - is has to, otherwise the
804  *      kernel will panic! Try a surgical technique - sending a BUS
805  *      DEVICE RESET message - on the offending target before pulling
806  *      the SCSI bus reset line.
807  **************************************************************************/
808 static int
809 qla1280_error_action(struct scsi_cmnd *cmd, enum action action)
810 {
811         struct scsi_qla_host *ha;
812         int bus, target, lun;
813         struct srb *sp;
814         uint16_t data;
815         unsigned char *handle;
816         int result, i;
817         DECLARE_COMPLETION(wait);
818         struct timer_list timer;
819
820         ha = (struct scsi_qla_host *)(CMD_HOST(cmd)->hostdata);
821
822         dprintk(4, "error_action %i, istatus 0x%04x\n", action,
823                 RD_REG_WORD(&ha->iobase->istatus));
824
825         dprintk(4, "host_cmd 0x%04x, ictrl 0x%04x, jiffies %li\n",
826                 RD_REG_WORD(&ha->iobase->host_cmd),
827                 RD_REG_WORD(&ha->iobase->ictrl), jiffies);
828
829         ENTER("qla1280_error_action");
830         if (qla1280_verbose)
831                 printk(KERN_INFO "scsi(%li): Resetting Cmnd=0x%p, "
832                        "Handle=0x%p, action=0x%x\n",
833                        ha->host_no, cmd, CMD_HANDLE(cmd), action);
834
835         if (cmd == NULL) {
836                 printk(KERN_WARNING "(scsi?:?:?:?) Reset called with NULL "
837                        "si_Cmnd pointer, failing.\n");
838                 LEAVE("qla1280_error_action");
839                 return FAILED;
840         }
841
842         ha = (struct scsi_qla_host *)cmd->device->host->hostdata;
843         sp = (struct srb *)CMD_SP(cmd);
844         handle = CMD_HANDLE(cmd);
845
846         /* Check for pending interrupts. */
847         data = qla1280_debounce_register(&ha->iobase->istatus);
848         /*
849          * The io_request_lock is held when the reset handler is called, hence
850          * the interrupt handler cannot be running in parallel as it also
851          * grabs the lock. /Jes
852          */
853         if (data & RISC_INT)
854                 qla1280_isr(ha, &ha->done_q);
855
856         /*
857          * Determine the suggested action that the mid-level driver wants
858          * us to perform.
859          */
860         if (handle == (unsigned char *)INVALID_HANDLE || handle == NULL) {
861                 if(action == ABORT_COMMAND) {
862                         /* we never got this command */
863                         printk(KERN_INFO "qla1280: Aborting a NULL handle\n");
864                         return SUCCESS; /* no action - we don't have command */
865                 }
866         } else {
867                 sp->wait = &wait;
868         }
869
870         bus = SCSI_BUS_32(cmd);
871         target = SCSI_TCN_32(cmd);
872         lun = SCSI_LUN_32(cmd);
873
874         /* Overloading result.  Here it means the success or fail of the
875          * *issue* of the action.  When we return from the routine, it must
876          * mean the actual success or fail of the action */
877         result = FAILED;
878         switch (action) {
879         case FAIL:
880                 break;
881
882         case ABORT_COMMAND:
883                 if ((sp->flags & SRB_ABORT_PENDING)) {
884                         printk(KERN_WARNING
885                                "scsi(): Command has a pending abort "
886                                "message - ABORT_PENDING.\n");
887                         /* This should technically be impossible since we
888                          * now wait for abort completion */
889                         break;
890                 }
891
892                 for (i = 0; i < MAX_OUTSTANDING_COMMANDS; i++) {
893                         if (sp == ha->outstanding_cmds[i]) {
894                                 dprintk(1, "qla1280: RISC aborting command\n");
895                                 if (qla1280_abort_command(ha, sp, i) == 0)
896                                         result = SUCCESS;
897                                 else {
898                                         /*
899                                          * Since we don't know what might
900                                          * have happend to the command, it
901                                          * is unsafe to remove it from the
902                                          * device's queue at this point.
903                                          * Wait and let the escalation
904                                          * process take care of it.
905                                          */
906                                         printk(KERN_WARNING
907                                                "scsi(%li:%i:%i:%i): Unable"
908                                                " to abort command!\n",
909                                                ha->host_no, bus, target, lun);
910                                 }
911                         }
912                 }
913                 break;
914
915         case ABORT_DEVICE:
916                 if (qla1280_verbose)
917                         printk(KERN_INFO
918                                "scsi(%ld:%d:%d:%d): Queueing abort device "
919                                "command.\n", ha->host_no, bus, target, lun);
920                 if (qla1280_abort_device(ha, bus, target, lun) == 0)
921                         result = SUCCESS;
922                 break;
923
924         case DEVICE_RESET:
925                 if (qla1280_verbose)
926                         printk(KERN_INFO
927                                "scsi(%ld:%d:%d:%d): Queueing device reset "
928                                "command.\n", ha->host_no, bus, target, lun);
929                 if (qla1280_device_reset(ha, bus, target) == 0)
930                         result = SUCCESS;
931                 break;
932
933         case BUS_RESET:
934                 if (qla1280_verbose)
935                         printk(KERN_INFO "qla1280(%ld:%d): Issuing BUS "
936                                "DEVICE RESET\n", ha->host_no, bus);
937                 if (qla1280_bus_reset(ha, bus == 0))
938                         result = SUCCESS;
939
940                 break;
941
942         case ADAPTER_RESET:
943         default:
944                 if (qla1280_verbose) {
945                         printk(KERN_INFO
946                                "scsi(%ld): Issued ADAPTER RESET\n",
947                                ha->host_no);
948                         printk(KERN_INFO "scsi(%ld): I/O processing will "
949                                "continue automatically\n", ha->host_no);
950                 }
951                 ha->flags.reset_active = 1;
952                 /*
953                  * We restarted all of the commands automatically, so the
954                  * mid-level code can expect completions momentitarily.
955                  */
956                 if (qla1280_abort_isp(ha) == 0)
957                         result = SUCCESS;
958
959                 ha->flags.reset_active = 0;
960         }
961
962         if (!list_empty(&ha->done_q))
963                 qla1280_done(ha);
964
965         /* If we didn't manage to issue the action, or we have no
966          * command to wait for, exit here */
967         if (result == FAILED || handle == NULL ||
968             handle == (unsigned char *)INVALID_HANDLE) {
969                 /*
970                  * Clear completion queue to avoid qla1280_done() trying
971                  * to complete the command at a later stage after we
972                  * have exited the current context
973                  */
974                 sp->wait = NULL;
975                 goto leave;
976         }
977
978         /* set up a timer just in case we're really jammed */
979         init_timer(&timer);
980         timer.expires = jiffies + 4*HZ;
981         timer.data = (unsigned long)cmd;
982         timer.function = qla1280_error_wait_timeout;
983         add_timer(&timer);
984
985         /* wait for the action to complete (or the timer to expire) */
986         spin_unlock_irq(ha->host->host_lock);
987         wait_for_completion(&wait);
988         del_timer_sync(&timer);
989         spin_lock_irq(ha->host->host_lock);
990         sp->wait = NULL;
991
992         /* the only action we might get a fail for is abort */
993         if (action == ABORT_COMMAND) {
994                 if(sp->flags & SRB_ABORTED)
995                         result = SUCCESS;
996                 else
997                         result = FAILED;
998         }
999
1000  leave:
1001         dprintk(1, "RESET returning %d\n", result);
1002
1003         LEAVE("qla1280_error_action");
1004         return result;
1005 }
1006
1007 /**************************************************************************
1008  *   qla1280_abort
1009  *     Abort the specified SCSI command(s).
1010  **************************************************************************/
1011 static int
1012 qla1280_eh_abort(struct scsi_cmnd * cmd)
1013 {
1014         int rc;
1015
1016         spin_lock_irq(cmd->device->host->host_lock);
1017         rc = qla1280_error_action(cmd, ABORT_COMMAND);
1018         spin_unlock_irq(cmd->device->host->host_lock);
1019
1020         return rc;
1021 }
1022
1023 /**************************************************************************
1024  *   qla1280_device_reset
1025  *     Reset the specified SCSI device
1026  **************************************************************************/
1027 static int
1028 qla1280_eh_device_reset(struct scsi_cmnd *cmd)
1029 {
1030         int rc;
1031
1032         spin_lock_irq(cmd->device->host->host_lock);
1033         rc = qla1280_error_action(cmd, DEVICE_RESET);
1034         spin_unlock_irq(cmd->device->host->host_lock);
1035
1036         return rc;
1037 }
1038
1039 /**************************************************************************
1040  *   qla1280_bus_reset
1041  *     Reset the specified bus.
1042  **************************************************************************/
1043 static int
1044 qla1280_eh_bus_reset(struct scsi_cmnd *cmd)
1045 {
1046         int rc;
1047
1048         spin_lock_irq(cmd->device->host->host_lock);
1049         rc = qla1280_error_action(cmd, BUS_RESET);
1050         spin_unlock_irq(cmd->device->host->host_lock);
1051
1052         return rc;
1053 }
1054
1055 /**************************************************************************
1056  *   qla1280_adapter_reset
1057  *     Reset the specified adapter (both channels)
1058  **************************************************************************/
1059 static int
1060 qla1280_eh_adapter_reset(struct scsi_cmnd *cmd)
1061 {
1062         int rc;
1063
1064         spin_lock_irq(cmd->device->host->host_lock);
1065         rc = qla1280_error_action(cmd, ADAPTER_RESET);
1066         spin_unlock_irq(cmd->device->host->host_lock);
1067
1068         return rc;
1069 }
1070
1071 static int
1072 qla1280_biosparam(struct scsi_device *sdev, struct block_device *bdev,
1073                   sector_t capacity, int geom[])
1074 {
1075         int heads, sectors, cylinders;
1076
1077         heads = 64;
1078         sectors = 32;
1079         cylinders = (unsigned long)capacity / (heads * sectors);
1080         if (cylinders > 1024) {
1081                 heads = 255;
1082                 sectors = 63;
1083                 cylinders = (unsigned long)capacity / (heads * sectors);
1084                 /* if (cylinders > 1023)
1085                    cylinders = 1023; */
1086         }
1087
1088         geom[0] = heads;
1089         geom[1] = sectors;
1090         geom[2] = cylinders;
1091
1092         return 0;
1093 }
1094
1095  
1096 /* disable risc and host interrupts */
1097 static inline void
1098 qla1280_disable_intrs(struct scsi_qla_host *ha)
1099 {
1100         WRT_REG_WORD(&ha->iobase->ictrl, 0);
1101         RD_REG_WORD(&ha->iobase->ictrl);        /* PCI Posted Write flush */
1102 }
1103
1104 /* enable risc and host interrupts */
1105 static inline void
1106 qla1280_enable_intrs(struct scsi_qla_host *ha)
1107 {
1108         WRT_REG_WORD(&ha->iobase->ictrl, (ISP_EN_INT | ISP_EN_RISC));
1109         RD_REG_WORD(&ha->iobase->ictrl);        /* PCI Posted Write flush */
1110 }
1111
1112 /**************************************************************************
1113  * qla1280_intr_handler
1114  *   Handles the H/W interrupt
1115  **************************************************************************/
1116 static irqreturn_t
1117 qla1280_intr_handler(int irq, void *dev_id, struct pt_regs *regs)
1118 {
1119         struct scsi_qla_host *ha;
1120         struct device_reg __iomem *reg;
1121         u16 data;
1122         int handled = 0;
1123
1124         ENTER_INTR ("qla1280_intr_handler");
1125         ha = (struct scsi_qla_host *)dev_id;
1126
1127         spin_lock(ha->host->host_lock);
1128
1129         ha->isr_count++;
1130         reg = ha->iobase;
1131
1132         qla1280_disable_intrs(ha);
1133
1134         data = qla1280_debounce_register(&reg->istatus);
1135         /* Check for pending interrupts. */
1136         if (data & RISC_INT) {  
1137                 qla1280_isr(ha, &ha->done_q);
1138                 handled = 1;
1139         }
1140         if (!list_empty(&ha->done_q))
1141                 qla1280_done(ha);
1142
1143         spin_unlock(ha->host->host_lock);
1144
1145         qla1280_enable_intrs(ha);
1146
1147         LEAVE_INTR("qla1280_intr_handler");
1148         return IRQ_RETVAL(handled);
1149 }
1150
1151
1152 static int
1153 qla1280_set_target_parameters(struct scsi_qla_host *ha, int bus, int target)
1154 {
1155         uint8_t mr;
1156         uint16_t mb[MAILBOX_REGISTER_COUNT];
1157         struct nvram *nv;
1158         int status, lun;
1159
1160         nv = &ha->nvram;
1161
1162         mr = BIT_3 | BIT_2 | BIT_1 | BIT_0;
1163
1164         /* Set Target Parameters. */
1165         mb[0] = MBC_SET_TARGET_PARAMETERS;
1166         mb[1] = (uint16_t)((bus ? target | BIT_7 : target) << 8);
1167         mb[2] = nv->bus[bus].target[target].parameter.renegotiate_on_error << 8;
1168         mb[2] |= nv->bus[bus].target[target].parameter.stop_queue_on_check << 9;
1169         mb[2] |= nv->bus[bus].target[target].parameter.auto_request_sense << 10;
1170         mb[2] |= nv->bus[bus].target[target].parameter.tag_queuing << 11;
1171         mb[2] |= nv->bus[bus].target[target].parameter.enable_sync << 12;
1172         mb[2] |= nv->bus[bus].target[target].parameter.enable_wide << 13;
1173         mb[2] |= nv->bus[bus].target[target].parameter.parity_checking << 14;
1174         mb[2] |= nv->bus[bus].target[target].parameter.disconnect_allowed << 15;
1175
1176         if (IS_ISP1x160(ha)) {
1177                 mb[2] |= nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr << 5;
1178                 mb[3] = (nv->bus[bus].target[target].flags.flags1x160.sync_offset << 8);
1179                 mb[6] = (nv->bus[bus].target[target].ppr_1x160.flags.ppr_options << 8) |
1180                          nv->bus[bus].target[target].ppr_1x160.flags.ppr_bus_width;
1181                 mr |= BIT_6;
1182         } else {
1183                 mb[3] = (nv->bus[bus].target[target].flags.flags1x80.sync_offset << 8);
1184         }
1185         mb[3] |= nv->bus[bus].target[target].sync_period;
1186
1187         status = qla1280_mailbox_command(ha, mr, mb);
1188
1189         /* Set Device Queue Parameters. */
1190         for (lun = 0; lun < MAX_LUNS; lun++) {
1191                 mb[0] = MBC_SET_DEVICE_QUEUE;
1192                 mb[1] = (uint16_t)((bus ? target | BIT_7 : target) << 8);
1193                 mb[1] |= lun;
1194                 mb[2] = nv->bus[bus].max_queue_depth;
1195                 mb[3] = nv->bus[bus].target[target].execution_throttle;
1196                 status |= qla1280_mailbox_command(ha, 0x0f, mb);
1197         }
1198
1199         if (status)
1200                 printk(KERN_WARNING "scsi(%ld:%i:%i): "
1201                        "qla1280_set_target_parameters() failed\n",
1202                        ha->host_no, bus, target);
1203         return status;
1204 }
1205
1206
1207 /**************************************************************************
1208  *   qla1280_slave_configure
1209  *
1210  * Description:
1211  *   Determines the queue depth for a given device.  There are two ways
1212  *   a queue depth can be obtained for a tagged queueing device.  One
1213  *   way is the default queue depth which is determined by whether
1214  *   If it is defined, then it is used
1215  *   as the default queue depth.  Otherwise, we use either 4 or 8 as the
1216  *   default queue depth (dependent on the number of hardware SCBs).
1217  **************************************************************************/
1218 static int
1219 qla1280_slave_configure(struct scsi_device *device)
1220 {
1221         struct scsi_qla_host *ha;
1222         int default_depth = 3;
1223         int bus = device->channel;
1224         int target = device->id;
1225         int status = 0;
1226         struct nvram *nv;
1227         unsigned long flags;
1228
1229         ha = (struct scsi_qla_host *)device->host->hostdata;
1230         nv = &ha->nvram;
1231
1232         if (qla1280_check_for_dead_scsi_bus(ha, bus))
1233                 return 1;
1234
1235         if (device->tagged_supported &&
1236             (ha->bus_settings[bus].qtag_enables & (BIT_0 << target))) {
1237                 scsi_adjust_queue_depth(device, MSG_ORDERED_TAG,
1238                                         ha->bus_settings[bus].hiwat);
1239         } else {
1240                 scsi_adjust_queue_depth(device, 0, default_depth);
1241         }
1242
1243         nv->bus[bus].target[target].parameter.enable_sync = device->sdtr;
1244         nv->bus[bus].target[target].parameter.enable_wide = device->wdtr;
1245         nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr = device->ppr;
1246
1247         if (driver_setup.no_sync ||
1248             (driver_setup.sync_mask &&
1249              (~driver_setup.sync_mask & (1 << target))))
1250                 nv->bus[bus].target[target].parameter.enable_sync = 0;
1251         if (driver_setup.no_wide ||
1252             (driver_setup.wide_mask &&
1253              (~driver_setup.wide_mask & (1 << target))))
1254                 nv->bus[bus].target[target].parameter.enable_wide = 0;
1255         if (IS_ISP1x160(ha)) {
1256                 if (driver_setup.no_ppr ||
1257                     (driver_setup.ppr_mask &&
1258                      (~driver_setup.ppr_mask & (1 << target))))
1259                         nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr = 0;
1260         }
1261
1262         spin_lock_irqsave(ha->host->host_lock, flags);
1263         if (nv->bus[bus].target[target].parameter.enable_sync)
1264                 status = qla1280_set_target_parameters(ha, bus, target);
1265         qla1280_get_target_parameters(ha, device);
1266         spin_unlock_irqrestore(ha->host->host_lock, flags);
1267         return status;
1268 }
1269
1270
1271 /*
1272  * qla1280_done
1273  *      Process completed commands.
1274  *
1275  * Input:
1276  *      ha           = adapter block pointer.
1277  */
1278 static void
1279 qla1280_done(struct scsi_qla_host *ha)
1280 {
1281         struct srb *sp;
1282         struct list_head *done_q;
1283         int bus, target, lun;
1284         struct scsi_cmnd *cmd;
1285
1286         ENTER("qla1280_done");
1287
1288         done_q = &ha->done_q;
1289
1290         while (!list_empty(done_q)) {
1291                 sp = list_entry(done_q->next, struct srb, list);
1292
1293                 list_del(&sp->list);
1294         
1295                 cmd = sp->cmd;
1296                 bus = SCSI_BUS_32(cmd);
1297                 target = SCSI_TCN_32(cmd);
1298                 lun = SCSI_LUN_32(cmd);
1299
1300                 switch ((CMD_RESULT(cmd) >> 16)) {
1301                 case DID_RESET:
1302                         /* Issue marker command. */
1303                         qla1280_marker(ha, bus, target, 0, MK_SYNC_ID);
1304                         break;
1305                 case DID_ABORT:
1306                         sp->flags &= ~SRB_ABORT_PENDING;
1307                         sp->flags |= SRB_ABORTED;
1308                         if (sp->flags & SRB_TIMEOUT)
1309                                 CMD_RESULT(sp->cmd) = DID_TIME_OUT << 16;
1310                         break;
1311                 default:
1312                         break;
1313                 }
1314
1315                 /* Release memory used for this I/O */
1316                 if (cmd->use_sg) {
1317                         pci_unmap_sg(ha->pdev, cmd->request_buffer,
1318                                         cmd->use_sg, cmd->sc_data_direction);
1319                 } else if (cmd->request_bufflen) {
1320                         pci_unmap_single(ha->pdev, sp->saved_dma_handle,
1321                                         cmd->request_bufflen,
1322                                         cmd->sc_data_direction);
1323                 }
1324
1325                 /* Call the mid-level driver interrupt handler */
1326                 CMD_HANDLE(sp->cmd) = (unsigned char *)INVALID_HANDLE;
1327                 ha->actthreads--;
1328
1329                 (*(cmd)->scsi_done)(cmd);
1330
1331                 if(sp->wait != NULL)
1332                         complete(sp->wait);
1333         }
1334         LEAVE("qla1280_done");
1335 }
1336
1337 /*
1338  * Translates a ISP error to a Linux SCSI error
1339  */
1340 static int
1341 qla1280_return_status(struct response * sts, struct scsi_cmnd *cp)
1342 {
1343         int host_status = DID_ERROR;
1344         uint16_t comp_status = le16_to_cpu(sts->comp_status);
1345         uint16_t state_flags = le16_to_cpu(sts->state_flags);
1346         uint16_t residual_length = le32_to_cpu(sts->residual_length);
1347         uint16_t scsi_status = le16_to_cpu(sts->scsi_status);
1348 #if DEBUG_QLA1280_INTR
1349         static char *reason[] = {
1350                 "DID_OK",
1351                 "DID_NO_CONNECT",
1352                 "DID_BUS_BUSY",
1353                 "DID_TIME_OUT",
1354                 "DID_BAD_TARGET",
1355                 "DID_ABORT",
1356                 "DID_PARITY",
1357                 "DID_ERROR",
1358                 "DID_RESET",
1359                 "DID_BAD_INTR"
1360         };
1361 #endif                          /* DEBUG_QLA1280_INTR */
1362
1363         ENTER("qla1280_return_status");
1364
1365 #if DEBUG_QLA1280_INTR
1366         /*
1367           dprintk(1, "qla1280_return_status: compl status = 0x%04x\n",
1368           comp_status);
1369         */
1370 #endif
1371
1372         switch (comp_status) {
1373         case CS_COMPLETE:
1374                 host_status = DID_OK;
1375                 break;
1376
1377         case CS_INCOMPLETE:
1378                 if (!(state_flags & SF_GOT_BUS))
1379                         host_status = DID_NO_CONNECT;
1380                 else if (!(state_flags & SF_GOT_TARGET))
1381                         host_status = DID_BAD_TARGET;
1382                 else if (!(state_flags & SF_SENT_CDB))
1383                         host_status = DID_ERROR;
1384                 else if (!(state_flags & SF_TRANSFERRED_DATA))
1385                         host_status = DID_ERROR;
1386                 else if (!(state_flags & SF_GOT_STATUS))
1387                         host_status = DID_ERROR;
1388                 else if (!(state_flags & SF_GOT_SENSE))
1389                         host_status = DID_ERROR;
1390                 break;
1391
1392         case CS_RESET:
1393                 host_status = DID_RESET;
1394                 break;
1395
1396         case CS_ABORTED:
1397                 host_status = DID_ABORT;
1398                 break;
1399
1400         case CS_TIMEOUT:
1401                 host_status = DID_TIME_OUT;
1402                 break;
1403
1404         case CS_DATA_OVERRUN:
1405                 dprintk(2, "Data overrun 0x%x\n", residual_length);
1406                 dprintk(2, "qla1280_return_status: response packet data\n");
1407                 qla1280_dump_buffer(2, (char *)sts, RESPONSE_ENTRY_SIZE);
1408                 host_status = DID_ERROR;
1409                 break;
1410
1411         case CS_DATA_UNDERRUN:
1412                 if ((cp->request_bufflen - residual_length) <
1413                     cp->underflow) {
1414                         printk(KERN_WARNING
1415                                "scsi: Underflow detected - retrying "
1416                                "command.\n");
1417                         host_status = DID_ERROR;
1418                 } else
1419                         host_status = DID_OK;
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(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(30);
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(30);
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
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, SA_SHIRQ,
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_module_init(&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  */