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