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