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