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