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