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