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