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