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