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