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