Update ia64 patch to 2.5.72-030619
[linux-flexiantxendom0-3.2.10.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-2002 Jes Sorensen, Wild Open Source Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * General Public License for more details.
17 *
18 ******************************************************************************/
19 #define QLA1280_VERSION      "3.23.19 Beta"
20 /******************************************************************************
21     Revision History:
22     Rev  3.23.19 Beta April 11, 2002, Linus Torvalds
23         - Do qla1280_pci_config() before calling request_irq() and
24           request_region()
25         - Use pci_dma_hi32() to handle upper word of DMA addresses instead
26           of large shifts
27         - Hand correct arguments to free_irq() in case of failure
28     Rev  3.23.18 Beta April 11, 2002, Jes Sorensen
29         - Run source through Lindent and clean up the output
30     Rev  3.23.17 Beta April 11, 2002, Jes Sorensen
31         - Update SCSI firmware to qla1280 v8.15.00 and qla12160 v10.04.32
32     Rev  3.23.16 Beta March 19, 2002, Jes Sorensen
33         - Rely on mailbox commands generating interrupts - do not
34           run qla1280_isr() from ql1280_mailbox_command()
35         - Remove device_reg_t
36         - Integrate ql12160_set_target_parameters() with 1280 version
37         - Make qla1280_setup() non static
38         - Do not call qla1280_check_for_dead_scsi_bus() on every I/O request
39           sent to the card - this command pauses the firmare!!!
40     Rev  3.23.15 Beta March 19, 2002, Jes Sorensen
41         - Clean up qla1280.h - remove obsolete QL_DEBUG_LEVEL_x definitions
42         - Remove a pile of pointless and confusing (srb_t **) and
43           (scsi_lu_t *) typecasts
44         - Explicit mark that we do not use the new error handling (for now)
45         - Remove scsi_qla_host_t and use 'struct' instead
46         - Remove in_abort, watchdog_enabled, dpc, dpc_sched, bios_enabled,
47           pci_64bit_slot flags which weren't used for anything anyway
48         - Grab host->host_lock while calling qla1280_isr() from abort()
49         - Use spin_lock()/spin_unlock() in qla1280_intr_handler() - we
50           do not need to save/restore flags in the interrupt handler
51         - Enable interrupts early (before any mailbox access) in preparation
52           for cleaning up the mailbox handling
53     Rev  3.23.14 Beta March 14, 2002, Jes Sorensen
54         - Further cleanups. Remove all trace of QL_DEBUG_LEVEL_x and replace
55           it with proper use of dprintk().
56         - Make qla1280_print_scsi_cmd() and qla1280_dump_buffer() both take
57           a debug level argument to determine if data is to be printed
58         - Add KERN_* info to printk()
59     Rev  3.23.13 Beta March 14, 2002, Jes Sorensen
60         - Significant cosmetic cleanups
61         - Change debug code to use dprintk() and remove #if mess
62     Rev  3.23.12 Beta March 13, 2002, Jes Sorensen
63         - More cosmetic cleanups, fix places treating return as function
64         - use cpu_relax() in qla1280_debounce_register()
65     Rev  3.23.11 Beta March 13, 2002, Jes Sorensen
66         - Make it compile under 2.5.5
67     Rev  3.23.10 Beta October 1, 2001, Jes Sorensen
68         - Do no typecast short * to long * in QL1280BoardTbl, this
69           broke miserably on big endian boxes
70     Rev  3.23.9 Beta September 30, 2001, Jes Sorensen
71         - Remove pre 2.2 hack for checking for reentrance in interrupt handler
72         - Make data types used to receive from SCSI_{BUS,TCN,LUN}_32
73           unsigned int to match the types from struct scsi_cmnd
74     Rev  3.23.8 Beta September 29, 2001, Jes Sorensen
75         - Remove bogus timer_t typedef from qla1280.h
76         - Remove obsolete pre 2.2 PCI setup code, use proper #define's
77           for PCI_ values, call pci_set_master()
78         - Fix memleak of qla1280_buffer on module unload
79         - Only compile module parsing code #ifdef MODULE - should be
80           changed to use individual MODULE_PARM's later
81         - Remove dummy_buffer that was never modified nor printed
82         - ENTER()/LEAVE() are noops unless QL_DEBUG_LEVEL_3, hence remove
83           #ifdef QL_DEBUG_LEVEL_3/#endif around ENTER()/LEAVE() calls
84         - Remove \r from print statements, this is Linux, not DOS
85         - Remove obsolete QLA1280_{SCSILU,INTR,RING}_{LOCK,UNLOCK}
86           dummy macros
87         - Remove C++ compile hack in header file as Linux driver are not
88           supposed to be compiled as C++
89         - Kill MS_64BITS macro as it makes the code more readable
90         - Remove unnecessary flags.in_interrupts bit
91     Rev  3.23.7 Beta August 20, 2001, Jes Sorensen
92         - Dont' check for set flags on q->q_flag one by one in qla1280_next()
93         - Check whether the interrupt was generated by the QLA1280 before
94           doing any processing
95         - qla1280_status_entry(): Only zero out part of sense_buffer that
96           is not being copied into
97         - Remove more superflouous typecasts
98         - qla1280_32bit_start_scsi() replace home-brew memcpy() with memcpy()
99     Rev  3.23.6 Beta August 20, 2001, Tony Luck, Intel
100         - Don't walk the entire list in qla1280_putq_t() just to directly
101           grab the pointer to the last element afterwards
102     Rev  3.23.5 Beta August 9, 2001, Jes Sorensen
103         - Don't use SA_INTERRUPT, it's use is deprecated for this kinda driver
104     Rev  3.23.4 Beta August 8, 2001, Jes Sorensen
105         - Set dev->max_sectors to 1024
106     Rev  3.23.3 Beta August 6, 2001, Jes Sorensen
107         - Provide compat macros for pci_enable_device(), pci_find_subsys()
108           and scsi_set_pci_device()
109         - Call scsi_set_pci_device() for all devices
110         - Reduce size of kernel version dependent device probe code
111         - Move duplicate probe/init code to separate function
112         - Handle error if qla1280_mem_alloc() fails
113         - Kill OFFSET() macro and use Linux's PCI definitions instead
114         - Kill private structure defining PCI config space (struct config_reg)
115         - Only allocate I/O port region if not in MMIO mode
116         - Remove duplicate (unused) sanity check of sife of srb_t
117     Rev  3.23.2 Beta August 6, 2001, Jes Sorensen
118         - Change home-brew memset() implementations to use memset()
119         - Remove all references to COMTRACE() - accessing a PC's COM2 serial
120           port directly is not legal under Linux.
121     Rev  3.23.1 Beta April 24, 2001, Jes Sorensen
122         - Remove pre 2.2 kernel support
123         - clean up 64 bit DMA setting to use 2.4 API (provide backwards compat)
124         - Fix MMIO access to use readl/writel instead of directly
125           dereferencing pointers
126         - Nuke MSDOS debugging code
127         - Change true/false data types to int from uint8_t
128         - Use int for counters instead of uint8_t etc.
129         - Clean up size & byte order conversion macro usage
130     Rev  3.23 Beta January 11, 2001 BN Qlogic
131         - Added check of device_id when handling non
132           QLA12160s during detect().
133     Rev  3.22 Beta January 5, 2001 BN Qlogic
134         - Changed queue_task() to schedule_work()
135           for kernels 2.4.0 and higher.
136           Note: 2.4.0-testxx kernels released prior to
137                 the actual 2.4.0 kernel release on January 2001
138                 will get compile/link errors with schedule_work().
139                 Please update your kernel to released 2.4.0 level,
140                 or comment lines in this file flagged with  3.22
141                 to resolve compile/link error of schedule_work().
142         - Added -DCONFIG_SMP in addition to -D__SMP__
143           in Makefile for 2.4.0 builds of driver as module.
144     Rev  3.21 Beta January 4, 2001 BN Qlogic
145         - Changed criteria of 64/32 Bit mode of HBA
146           operation according to BITS_PER_LONG rather
147           than HBA's NVRAM setting of >4Gig memory bit;
148           so that the HBA auto-configures without the need
149           to setup each system individually.
150     Rev  3.20 Beta December 5, 2000 BN Qlogic
151         - Added priority handling to IA-64  onboard SCSI
152           ISP12160 chip for kernels greater than 2.3.18.
153         - Added irqrestore for qla1280_intr_handler.
154         - Enabled /proc/scsi/qla1280 interface.
155         - Clear /proc/scsi/qla1280 counters in detect().
156     Rev  3.19 Beta October 13, 2000 BN Qlogic
157         - Declare driver_template for new kernel
158           (2.4.0 and greater) scsi initialization scheme.
159         - Update /proc/scsi entry for 2.3.18 kernels and
160           above as qla1280
161     Rev  3.18 Beta October 10, 2000 BN Qlogic
162         - Changed scan order of adapters to map
163           the QLA12160 followed by the QLA1280.
164     Rev  3.17 Beta September 18, 2000 BN Qlogic
165         - Removed warnings for 32 bit 2.4.x compiles
166         - Corrected declared size for request and response
167           DMA addresses that are kept in each ha
168     Rev. 3.16 Beta  August 25, 2000   BN  Qlogic
169         - Corrected 64 bit addressing issue on IA-64
170           where the upper 32 bits were not properly
171           passed to the RISC engine.
172     Rev. 3.15 Beta  August 22, 2000   BN  Qlogic
173         - Modified qla1280_setup_chip to properly load
174           ISP firmware for greater that 4 Gig memory on IA-64
175     Rev. 3.14 Beta  August 16, 2000   BN  Qlogic
176         - Added setting of dma_mask to full 64 bit
177           if flags.enable_64bit_addressing is set in NVRAM
178     Rev. 3.13 Beta  August 16, 2000   BN  Qlogic
179         - Use new PCI DMA mapping APIs for 2.4.x kernel
180     Rev. 3.12       July 18, 2000    Redhat & BN Qlogic
181         - Added check of pci_enable_device to detect() for 2.3.x
182         - Use pci_resource_start() instead of
183           pdev->resource[0].start in detect() for 2.3.x
184         - Updated driver version
185     Rev. 3.11       July 14, 2000    BN  Qlogic
186         - Updated SCSI Firmware to following versions:
187           qla1x80:   8.13.08
188           qla1x160:  10.04.08
189         - Updated driver version to 3.11
190     Rev. 3.10    June 23, 2000   BN Qlogic
191         - Added filtering of AMI SubSys Vendor ID devices
192     Rev. 3.9
193         - DEBUG_QLA1280 undefined and  new version  BN Qlogic
194     Rev. 3.08b      May 9, 2000    MD Dell
195         - Added logic to check against AMI subsystem vendor ID
196         Rev. 3.08       May 4, 2000    DG  Qlogic
197         - Added logic to check for PCI subsystem ID.
198         Rev. 3.07       Apr 24, 2000    DG & BN  Qlogic
199            - Updated SCSI Firmware to following versions:
200              qla12160:   10.01.19
201                  qla1280:     8.09.00
202         Rev. 3.06       Apr 12, 2000    DG & BN  Qlogic
203            - Internal revision; not released
204     Rev. 3.05       Mar 28, 2000    DG & BN  Qlogic
205        - Edit correction for virt_to_bus and PROC.
206     Rev. 3.04       Mar 28, 2000    DG & BN  Qlogic
207        - Merge changes from ia64 port.
208     Rev. 3.03       Mar 28, 2000    BN  Qlogic
209        - Increase version to reflect new code drop with compile fix
210          of issue with inclusion of linux/spinlock for 2.3 kernels
211     Rev. 3.02       Mar 15, 2000    BN  Qlogic
212        - Merge qla1280_proc_info from 2.10 code base
213     Rev. 3.01       Feb 10, 2000    BN  Qlogic
214        - Corrected code to compile on a 2.2.x kernel.
215     Rev. 3.00       Jan 17, 2000    DG  Qlogic
216            - Added 64-bit support.
217     Rev. 2.07       Nov 9, 1999     DG  Qlogic
218            - Added new routine to set target parameters for ISP12160.
219     Rev. 2.06       Sept 10, 1999     DG  Qlogic
220        - Added support for ISP12160 Ultra 3 chip.
221     Rev. 2.03       August 3, 1999    Fred Lewis, Intel DuPont
222         - Modified code to remove errors generated when compiling with
223           Cygnus IA64 Compiler.
224         - Changed conversion of pointers to unsigned longs instead of integers.
225         - Changed type of I/O port variables from uint32_t to unsigned long.
226         - Modified OFFSET macro to work with 64-bit as well as 32-bit.
227         - Changed sprintf and printk format specifiers for pointers to %p.
228         - Changed some int to long type casts where needed in sprintf & printk.
229         - Added l modifiers to sprintf and printk format specifiers for longs.
230         - Removed unused local variables.
231     Rev. 1.20       June 8, 1999      DG,  Qlogic
232          Changes to support RedHat release 6.0 (kernel 2.2.5).
233        - Added SCSI exclusive access lock (io_request_lock) when accessing
234          the adapter.
235        - Added changes for the new LINUX interface template. Some new error
236          handling routines have been added to the template, but for now we
237          will use the old ones.
238     -   Initial Beta Release.
239 *****************************************************************************/
240
241 #include <linux/config.h>
242 #include <linux/module.h>
243
244 #include <linux/version.h>
245 #include <linux/types.h>
246 #include <linux/string.h>
247 #include <linux/errno.h>
248 #include <linux/kernel.h>
249 #include <linux/interrupt.h>
250 #include <linux/ioport.h>
251 #include <linux/delay.h>
252 #include <linux/timer.h>
253 #include <linux/pci.h>
254 #include <linux/proc_fs.h>
255 #include <linux/blk.h>
256 #include <linux/workqueue.h>
257 #include <linux/stat.h>
258 #include <linux/slab.h>
259
260 #include <asm/io.h>
261 #include <asm/irq.h>
262 #include <asm/byteorder.h>
263 #include <asm/processor.h>
264
265 #ifndef KERNEL_VERSION
266 #define KERNEL_VERSION(x,y,z) (((x)<<16)+((y)<<8)+(z))
267 #endif
268
269 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,18)
270 #include <linux/pci_ids.h>
271 #endif
272
273 #include "scsi.h"
274 #include "hosts.h"
275 #define UNIQUE_FW_NAME
276 #include "qla1280.h"
277 #include "ql12160_fw.h"         /* ISP RISC codes */
278 #include "ql1280_fw.h"
279
280 /*
281  * Compile time Options:
282  *            0 - Disable and 1 - Enable
283  */
284 #define  QL1280_TARGET_MODE_SUPPORT    0        /* Target mode support */
285 #define  QL1280_LUN_SUPPORT            0
286 #define  WATCHDOGTIMER                 0
287 #define  MEMORY_MAPPED_IO              1
288 #define  DEBUG_QLA1280_INTR            0
289 #define  USE_NVRAM_DEFAULTS            0
290 #define  DEBUG_PRINT_NVRAM             0
291 #define  LOADING_RISC_ACTIVITY         0
292 #define  AUTO_ESCALATE_RESET           0        /* Automatically escalate resets */
293 #define  AUTO_ESCALATE_ABORT           0        /* Automatically escalate aborts */
294 #define  STOP_ON_ERROR                 0        /* Stop on aborts and resets  */
295 #define  STOP_ON_RESET                 0
296 #define  STOP_ON_ABORT                 0
297 #define  QLA1280_PROFILE               1        /* 3.20 */
298 #define  DEBUG_QLA1280                 0
299
300 /*
301  * Missing PCI ID's
302  */
303 #ifndef PCI_DEVICE_ID_QLOGIC_ISP1080
304 #define PCI_DEVICE_ID_QLOGIC_ISP1080    0x1080
305 #endif
306 #ifndef PCI_DEVICE_ID_QLOGIC_ISP1240
307 #define PCI_DEVICE_ID_QLOGIC_ISP1240    0x1240
308 #endif
309 #ifndef PCI_DEVICE_ID_QLOGIC_ISP1280
310 #define PCI_DEVICE_ID_QLOGIC_ISP1280    0x1280
311 #endif
312 #ifndef PCI_DEVICE_ID_QLOGIC_ISP10160
313 #define PCI_DEVICE_ID_QLOGIC_ISP10160   0x1016
314 #endif
315 #ifndef PCI_DEVICE_ID_QLOGIC_ISP12160
316 #define PCI_DEVICE_ID_QLOGIC_ISP12160   0x1216
317 #endif
318
319 #ifndef PCI_VENDOR_ID_AMI
320 #define PCI_VENDOR_ID_AMI               0x101e
321 #endif
322
323 #if (BITS_PER_LONG == 64) || defined CONFIG_HIGHMEM
324 #define QLA_64BIT_PTR   1
325 #endif
326
327 /* 3.16 */
328 #ifdef QLA_64BIT_PTR
329 #define pci_dma_lo32(a)         (a & 0xffffffff)
330 #define pci_dma_hi32(a)         (a >> 32)
331 #else
332 #define pci_dma_lo32(a)         (a & 0xffffffff)
333 #define pci_dma_hi32(a)         0
334 #endif
335 /* MACROS for managing the endian addresses */
336 static inline uint16_t qla1280_addr0_15(dma_addr_t dma)
337 {
338         return ((uint16_t)(dma & 0xffff));
339 }
340 static inline uint16_t qla1280_addr16_31(dma_addr_t dma)
341 {
342         return ((uint16_t)((dma >> 16) & 0xffff));
343 }
344 static inline uint16_t qla1280_addr32_47(dma_addr_t dma)
345 {
346         return ((uint16_t)(pci_dma_hi32(dma) & 0xffff));
347 }
348 static inline uint16_t qla1280_addr48_63(dma_addr_t dma)
349 {
350         return ((uint16_t)((pci_dma_hi32(dma) >> 16) & 0xffff));
351 }
352
353 #define NVRAM_DELAY()           udelay(500)     /* 2 microsecond delay */
354
355 #define CACHE_FLUSH(a)          RD_REG_WORD(a)
356 #define INVALID_HANDLE          (MAX_OUTSTANDING_COMMANDS + 1)
357
358 /*
359  * Compat macros
360  */
361 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0)
362 #define pci_set_dma_mask(dev, mask)             dev->dma_mask = mask;
363 #define pci_enable_device(pdev)                 0
364 #define pci_find_subsys(id, dev, sid, sdev, pdev) pci_find_device(id,dev,pdev)
365 #define scsi_set_pci_device(host, pdev)
366 #endif
367
368 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,18)
369 typedef unsigned long dma_addr_t;
370
371 static inline void *
372 pci_alloc_consistent(struct pci_dev *hwdev, size_t size,
373                      dma_addr_t * dma_handle)
374 {
375         void *virt_ptr;
376
377         virt_ptr = kmalloc(size, GFP_KERNEL);
378         if (!virt_ptr)
379                 return NULL;
380         *dma_handle = virt_to_bus(virt_ptr);
381         return virt_ptr;
382 }
383
384 #define pci_free_consistent(cookie, size, ptr, dma_ptr) kfree(ptr)
385 #define pci_map_single(cookie, address, size, dir)      virt_to_bus(address)
386 #define pci_map_sg(cookie, scatter, ents, dir)          ents
387 #define pci_unmap_single(cookie, address, size, dir)
388 #define pci_unmap_sg(cookie, scatter, ents, dir)
389
390 #define pci_resource_start(dev, i)                      dev->base_address[i]
391 #endif
392
393 /*
394  *  QLogic Driver Support Function Prototypes.
395  */
396 static void qla1280_done(struct scsi_qla_host *, srb_t **, srb_t **);
397 static void qla1280_next(struct scsi_qla_host *, scsi_lu_t *, int);
398 static void qla1280_putq_t(scsi_lu_t *, srb_t *);
399 static void qla1280_done_q_put(srb_t *, srb_t **, srb_t **);
400 static int qla1280_slave_configure(Scsi_Device *);
401 #if STOP_ON_ERROR
402 static void qla1280_panic(char *, struct Scsi_Host *host);
403 #endif
404 static void qla1280_abort_queue_single(struct scsi_qla_host *, int, int,
405                                        int, uint32_t);
406
407 static int qla1280_return_status(sts_entry_t * sts, Scsi_Cmnd * cp);
408 static void qla1280_removeq(scsi_lu_t * q, srb_t * sp);
409 static void qla1280_mem_free(struct scsi_qla_host *ha);
410 void qla1280_do_dpc(void *p);
411 #ifdef MODULE
412 static char *qla1280_get_token(char *, char *);
413 #endif
414 static inline void qla1280_enable_intrs(struct scsi_qla_host *);
415 static inline void qla1280_disable_intrs(struct scsi_qla_host *);
416
417 /*
418  *  QLogic ISP1280 Hardware Support Function Prototypes.
419  */
420 static int qla1280_initialize_adapter(struct scsi_qla_host *ha);
421 static int qla1280_enable_tgt(struct scsi_qla_host *, int);
422 static int qla1280_isp_firmware(struct scsi_qla_host *);
423 static int qla1280_pci_config(struct scsi_qla_host *);
424 static int qla1280_chip_diag(struct scsi_qla_host *);
425 static int qla1280_setup_chip(struct scsi_qla_host *);
426 static int qla1280_init_rings(struct scsi_qla_host *);
427 static int qla1280_nvram_config(struct scsi_qla_host *);
428 static int qla1280_mailbox_command(struct scsi_qla_host *,
429                                    uint8_t, uint16_t *);
430 static int qla1280_bus_reset(struct scsi_qla_host *, int);
431 static int qla1280_device_reset(struct scsi_qla_host *, int, int);
432 static int qla1280_abort_device(struct scsi_qla_host *, int, int, int);
433 static int qla1280_abort_command(struct scsi_qla_host *, srb_t *);
434 static int qla1280_abort_isp(struct scsi_qla_host *);
435 static int qla1280_64bit_start_scsi(struct scsi_qla_host *, srb_t *);
436 static int qla1280_32bit_start_scsi(struct scsi_qla_host *, srb_t *);
437 static void qla1280_nv_write(struct scsi_qla_host *, uint16_t);
438 static void qla1280_poll(struct scsi_qla_host *);
439 static void qla1280_reset_adapter(struct scsi_qla_host *);
440 static void qla1280_marker(struct scsi_qla_host *, int, int, int, u8);
441 static void qla1280_isp_cmd(struct scsi_qla_host *);
442 static void qla1280_isr(struct scsi_qla_host *, srb_t **, srb_t **);
443 static void qla1280_rst_aen(struct scsi_qla_host *);
444 static void qla1280_status_entry(struct scsi_qla_host *, sts_entry_t *,
445                                  srb_t **, srb_t **);
446 static void qla1280_error_entry(struct scsi_qla_host *, response_t *,
447                                 srb_t **, srb_t **);
448 static void qla1280_restart_queues(struct scsi_qla_host *);
449 static void qla1280_abort_queues(struct scsi_qla_host *);
450 static uint16_t qla1280_get_nvram_word(struct scsi_qla_host *, uint32_t);
451 static uint16_t qla1280_nvram_request(struct scsi_qla_host *, uint32_t);
452 static uint16_t qla1280_debounce_register(volatile uint16_t *);
453 static request_t *qla1280_req_pkt(struct scsi_qla_host *);
454 static int qla1280_check_for_dead_scsi_bus(struct scsi_qla_host *,
455                                            unsigned int);
456 static int qla1280_mem_alloc(struct scsi_qla_host *ha);
457
458 static void qla12160_get_target_parameters(struct scsi_qla_host *,
459                                            uint32_t, uint32_t, uint32_t);
460
461 /* convert scsi data direction to request_t control flags
462  */
463 static inline uint16_t
464 qla1280_data_direction(struct scsi_cmnd *cmnd)
465 {
466         uint16_t flags = 0;
467
468         switch(cmnd->sc_data_direction) {
469
470         case SCSI_DATA_NONE:
471                 flags = 0;
472                 break;
473
474         case SCSI_DATA_READ:
475                 flags = BIT_5;
476                 break;
477
478         case SCSI_DATA_WRITE:
479                 flags = BIT_6;
480                 break;
481
482         case SCSI_DATA_UNKNOWN:
483         default:
484                 flags = BIT_5 | BIT_6;
485                 break;
486         }
487         return flags;
488 }
489                 
490 #if QL1280_LUN_SUPPORT
491 static void qla1280_enable_lun(struct scsi_qla_host *, int, int);
492 #endif
493
494 #if QL1280_TARGET_MODE_SUPPORT
495 static void qla1280_notify_ack(struct scsi_qla_host *, notify_entry_t *);
496 static void qla1280_immed_notify(struct scsi_qla_host *, notify_entry_t *);
497 static void qla1280_accept_io(struct scsi_qla_host *, ctio_ret_entry_t *);
498 static void qla1280_64bit_continue_io(struct scsi_qla_host *, atio_entry_t *,
499                                       uint32_t, paddr32_t *);
500 static void qla1280_32bit_continue_io(struct scsi_qla_host *, atio_entry_t *,
501                                       uint32_t, paddr32_t *);
502 static void qla1280_atio_entry(struct scsi_qla_host *, atio_entry_t *);
503 static void qla1280_notify_entry(struct scsi_qla_host *, notify_entry_t *);
504 #endif                          /* QLA1280_TARGET_MODE_SUPPORT */
505
506 #ifdef QL_DEBUG_ROUTINES
507 /*
508  *  Driver Debug Function Prototypes.
509  */
510 static u8 qla1280_getbyte(u8 *);
511 static u16 qla1280_getword(u16 *);
512 static u32 qla1280_getdword(u32 *);
513 static void qla1280_putbyte(u8 *, u8);
514 static void qla1280_putword(u16 *, u8);
515 static void qla1280_putdword(u32 *, u32);
516 static void __qla1280_print_scsi_cmd(Scsi_Cmnd * cmd);
517 static void __qla1280_dump_buffer(char *, u32);
518 #endif
519
520 /*
521  * insmod needs to find the variable and make it point to something
522  */
523 #ifdef MODULE
524 static char *options = NULL;
525
526 /* insmod qla1280 options=verbose" */
527 MODULE_PARM(options, "s");
528 #endif
529
530 MODULE_LICENSE("GPL");
531
532 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,18)
533 /*
534  * Our directory Entry in /proc/scsi for the user to
535  * access the driver.
536  */
537 /* Need to add in proc_fs.h     PROC_SCSI_QL1280 */
538 #define PROC_SCSI_QL1280  PROC_SCSI_QLOGICISP
539
540 struct proc_dir_entry proc_scsi_qla1280 = {
541         PROC_SCSI_QL1280, 7, "qla1280",
542         S_IFDIR | S_IRUGO | S_IXUGO, 2,
543         0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL
544 };
545 #endif
546
547 /* We use the Scsi_Pointer structure that's included with each command
548  * SCSI_Cmnd as a scratchpad for our SRB.
549  *
550  * SCp will always point to the SRB structure (defined in qla1280.h).
551  * It is define as follows:
552  *  - SCp.ptr  -- > pointer back to the cmd
553  *  - SCp.this_residual --> used as forward pointer to next srb
554  *  - SCp.buffer --> used as backward pointer to next srb
555  *  - SCp.buffers_residual --> used as flags field
556  *  - SCp.have_data_in --> not used
557  *  - SCp.sent_command --> not used
558  *  - SCp.phase --> not used
559  */
560
561 #define CMD_SP(Cmnd)            &Cmnd->SCp
562 #define CMD_CDBLEN(Cmnd)        Cmnd->cmd_len
563 #define CMD_CDBP(Cmnd)          Cmnd->cmnd
564 #define CMD_SNSP(Cmnd)          Cmnd->sense_buffer
565 #define CMD_SNSLEN(Cmnd)        sizeof(Cmnd->sense_buffer)
566 #define CMD_RESULT(Cmnd)        Cmnd->result
567 #define CMD_HANDLE(Cmnd)        Cmnd->host_scribble
568
569 /*****************************************/
570 /*   ISP Boards supported by this driver */
571 /*****************************************/
572
573 #define NUM_OF_ISP_DEVICES      6
574
575 struct qla_boards {
576         unsigned char bdName[9];        /* Board ID String */
577         unsigned long device_id;        /* Device PCI ID   */
578         int numPorts;           /* Number of SCSI ports */
579         unsigned short *fwcode; /* pointer to FW array         */
580         unsigned short *fwlen;  /* number of words in array    */
581         unsigned short *fwstart;        /* start address for F/W       */
582         unsigned char *fwver;   /* Ptr to F/W version array    */
583 };
584
585 struct qla_boards ql1280_board_tbl[NUM_OF_ISP_DEVICES] = {
586         /* Name ,  Board PCI Device ID,         Number of ports */
587         {"QLA12160 ", PCI_DEVICE_ID_QLOGIC_ISP12160, 2,
588          &fw12160i_code01[0], &fw12160i_length01,
589          &fw12160i_addr01, &fw12160i_version_str[0]},
590         {"QLA1080 ", PCI_DEVICE_ID_QLOGIC_ISP1080, 1,
591          &fw1280ei_code01[0], &fw1280ei_length01,
592          &fw1280ei_addr01, &fw1280ei_version_str[0]},
593         {"QLA1240 ", PCI_DEVICE_ID_QLOGIC_ISP1240, 2,
594          &fw1280ei_code01[0], &fw1280ei_length01,
595          &fw1280ei_addr01, &fw1280ei_version_str[0]},
596         {"QLA1280 ", PCI_DEVICE_ID_QLOGIC_ISP1280, 2,
597          &fw1280ei_code01[0], &fw1280ei_length01,
598          &fw1280ei_addr01, &fw1280ei_version_str[0]},
599         {"QLA10160 ", PCI_DEVICE_ID_QLOGIC_ISP10160, 1,
600          &fw12160i_code01[0], &fw12160i_length01,
601          &fw12160i_addr01, &fw12160i_version_str[0]},
602         {"        ", 0, 0}
603 };
604
605 static int qla1280_verbose = 1;
606 static struct scsi_qla_host *qla1280_hostlist = NULL;
607 #if QLA1280_PROFILE
608 static int qla1280_buffer_size = 0;
609 static char *qla1280_buffer = NULL;
610 #endif
611
612 #if DEBUG_QLA1280
613 static int ql_debug_print = 1;
614 char debug_buff[80];
615 #define DEBUG(x)        x
616 static int ql_debug_level = 0;
617 #define dprintk(level, format, a...)    \
618         if ((ql_debug_level >= level) && ql_debug_print) printk(KERN_DEBUG format, ##a)
619 #define qla1280_dump_buffer(level, buf, size)   \
620         if (ql_debug_level >= level) __qla1280_dump_buffer(buf, size)
621 #define qla1280_dump_print_cmd(level, cmd)      \
622         if (ql_debug_level >= level) __qla1280_print_scsi_cmd(cmd)
623 #else
624 #define DEBUG(x)
625 #define ql_debug_level                  0
626 #define dprintk(level, format, a...)    do{}while(0)
627 #define qla1280_dump_buffer(a, b, c)    do{}while(0)
628 #define qla1280_print_scsi_cmd(a, b)    do{}while(0)
629 #endif
630
631 #define ENTER(x)                dprintk(3, "qla1280 : Entering %s()\n", x);
632 #define LEAVE(x)                dprintk(3, "qla1280 : Leaving %s()\n", x);
633 #define ENTER_INTR(x)           dprintk(3, "qla1280 : Entering %s()\n", x);
634 #define LEAVE_INTR(x)           dprintk(3, "qla1280 : Leaving %s()\n", x);
635
636 #define SCSI_BUS_32(scp)        scp->device->channel
637 #define SCSI_TCN_32(scp)        scp->device->id
638 #define SCSI_LUN_32(scp)        scp->device->lun
639
640 /****************************************************************************/
641 /*  LINUX -  Loadable Module Functions.                                     */
642 /****************************************************************************/
643
644 /*************************************************************************
645  *   qla1280_set_info
646  *
647  * Description:
648  *   Set parameters for the driver from the /proc filesystem.
649  *
650  * Returns:
651  *************************************************************************/
652 int
653 qla1280_set_info(char *buffer, int length, struct Scsi_Host *HBAptr)
654 {
655         return -ENOSYS;         /* Currently this is a no-op */
656 }
657
658 /*************************************************************************
659  * qla1280_proc_info
660  *
661  * Description:
662  *   Return information to handle /proc support for the driver.
663  *
664  * buffer - ptrs to a page buffer
665  *
666  * Returns:
667  *************************************************************************/
668 #define PROC_BUF        &qla1280_buffer[len]
669
670 int
671 qla1280_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset, int length,
672                   int inout)
673 {
674 #if QLA1280_PROFILE
675         struct scsi_qla_host *ha;
676         int size = 0;
677         scsi_lu_t *up;
678         int len = 0;
679         struct qla_boards *bdp;
680         uint32_t b, t, l;
681         host = NULL;
682
683         /* Find the host that was specified */
684         for (ha = qla1280_hostlist; (ha != NULL) && ha->host != host;
685              ha = ha->next) ;
686
687         if (inout == TRUE) {    /* Has data been written to the file? */
688                 printk(KERN_INFO
689                        "qla1280_proc: has data been written to the file.\n");
690                 return qla1280_set_info(buffer, length, host);
691         }
692
693         /*
694          * if our old buffer is the right size use it otherwise
695          * allocate a new one.
696          */
697         if (qla1280_buffer_size != PAGE_SIZE) {
698                 /* deallocate this buffer and get a new one */
699                 if (qla1280_buffer != NULL) {
700                         free_page((unsigned long)qla1280_buffer);
701                         qla1280_buffer_size = 0;
702                 }
703                 qla1280_buffer = (char *)get_zeroed_page(GFP_KERNEL);
704         }
705         if (qla1280_buffer == NULL) {
706                 size = sprintf(buffer, "qla1280 - kmalloc error at line %d\n",
707                                __LINE__);
708                 return size;
709         }
710         /* save the size of our buffer */
711         qla1280_buffer_size = PAGE_SIZE;
712
713         /* 3.20 clear the buffer we use for proc display */
714         memset(qla1280_buffer, 0, PAGE_SIZE);
715
716         /* start building the print buffer */
717         bdp = &ql1280_board_tbl[ha->devnum];
718         size = sprintf(PROC_BUF,
719                        "QLogic PCI to SCSI Adapter for ISP 1280/12160:\n"
720                        "        Firmware version: %2d.%02d.%02d, Driver version %s\n",
721                        bdp->fwver[0], bdp->fwver[1], bdp->fwver[2],
722                        QLA1280_VERSION);
723
724         len += size;
725
726         size = sprintf(PROC_BUF, "SCSI Host Adapter Information: %s\n",
727                        bdp->bdName);
728         len += size;
729         size = sprintf(PROC_BUF, "Request Queue = 0x%p, Response Queue = 0x%p\n",
730                        (void *)ha->request_dma, (void *)ha->response_dma);
731         len += size;
732         size = sprintf(PROC_BUF, "Request Queue count= 0x%x, Response "
733                        "Queue count= 0x%x\n",
734                        REQUEST_ENTRY_CNT, RESPONSE_ENTRY_CNT);
735         len += size;
736         size = sprintf(PROC_BUF, "Number of pending commands = 0x%lx\n",
737                        ha->actthreads);
738         len += size;
739         size = sprintf(PROC_BUF, "Number of queued commands = 0x%lx\n",
740                        ha->qthreads);
741         len += size;
742         size = sprintf(PROC_BUF, "Number of free request entries = %d\n",
743                        ha->req_q_cnt);
744         len += size;
745         size = sprintf(PROC_BUF, "\n"); /* 1       */
746         len += size;
747
748         size = sprintf(PROC_BUF, "SCSI device Information:\n");
749         len += size;
750         /* scan for all equipment stats */
751         for (b = 0; b < MAX_BUSES; b++)
752                 for (t = 0; t < MAX_TARGETS; t++) {
753                         for (l = 0; l < MAX_LUNS; l++) {
754                                 up = LU_Q(ha, b, t, l);
755                                 if (up == NULL)
756                                         continue;
757                                 /* unused device/lun */
758                                 if (up->io_cnt == 0 || up->io_cnt < 2)
759                                         continue;
760                                 /* total reads since boot */
761                                 /* total writes since boot */
762                                 /* total requests since boot  */
763                                 size = sprintf (PROC_BUF,
764                                                 "(%2d:%2d:%2d): Total reqs %ld,",
765                                                 b, t, l, up->io_cnt);
766                                 len += size;
767                                 /* current number of pending requests */
768                                 size =  sprintf(PROC_BUF, " Pend reqs %d,",
769                                                 up->q_outcnt);
770                                 len += size;
771 #if 0
772                                 /* avg response time */
773                                 size = sprintf(PROC_BUF, " Avg resp time %ld%%,",
774                                                (up->resp_time / up->io_cnt) *
775                                                100);
776                                 len += size;
777
778                                 /* avg active time */
779                                 size = sprintf(PROC_BUF,
780                                                " Avg active time %ld%%\n",
781                                                (up->act_time / up->io_cnt) * 100);
782 #else
783                                 size = sprintf(PROC_BUF, "\n");
784 #endif
785                                 len += size;
786                         }
787                         if (len >= qla1280_buffer_size)
788                                 break;
789                 }
790
791         if (len >= qla1280_buffer_size) {
792                 printk(KERN_WARNING
793                        "qla1280: Overflow buffer in qla1280_proc.c\n");
794         }
795
796         if (offset > len - 1) {
797                 free_page((unsigned long) qla1280_buffer);
798                 qla1280_buffer = NULL;
799                 qla1280_buffer_size = length = 0;
800                 *start = NULL;
801         } else {
802                 *start = &qla1280_buffer[offset];       /* Start of wanted data */
803                 if (len - offset < length) {
804                         length = len - offset;
805                 }
806         }
807         return length;
808 #else
809         return 0;
810 #endif
811 }
812
813 /**************************************************************************
814  * qla1280_do_device_init
815  *    This routine will register the device with the SCSI subsystem,
816  *    initialize the host adapter structure and call the device init
817  *    routines.
818  *
819  * Input:
820  *     pdev      - pointer to struct pci_dev for adapter
821  *     template  - pointer to SCSI template
822  *     devnum    - the device number
823  *     bdp       - pointer to struct _qlaboards
824  *     num_hosts - the host number
825  *
826  * Returns:
827  *  host - pointer to SCSI host structure
828  **************************************************************************/
829 struct Scsi_Host *
830 qla1280_do_device_init(struct pci_dev *pdev,
831                        Scsi_Host_Template * template,
832                        int devnum, struct qla_boards *bdp, int num_hosts)
833 {
834         struct Scsi_Host *host;
835         struct scsi_qla_host *ha;
836         struct device_reg *reg;
837
838         printk("qla1x160: Initializing ISP12160 on PCI bus %i, dev %i, irq %i\n",
839                pdev->bus->number, PCI_SLOT(pdev->devfn), pdev->irq);
840
841         host = scsi_register(template, sizeof(struct scsi_qla_host));
842         if (!host) {
843                 printk(KERN_WARNING
844                        "qla1280: Failed to register host, aborting.\n");
845                 goto error;
846         }
847
848         scsi_set_device(host, &pdev->dev);
849         ha = (struct scsi_qla_host *)host->hostdata;
850         /* Clear our data area */
851         memset(ha, 0, sizeof(struct scsi_qla_host));
852         /* Sanitize the information from PCI BIOS.  */
853         host->irq = pdev->irq;
854         ha->pci_bus = pdev->bus->number;
855         ha->pci_device_fn = pdev->devfn;
856         ha->pdev = pdev;
857         ha->device_id = bdp->device_id;
858         ha->devnum = devnum;    /* specifies microcode load address */
859
860         if (qla1280_mem_alloc(ha)) {
861                 printk(KERN_INFO "qla1x160: Failed to get memory\n");
862                 goto error;
863         }
864
865         ha->ports = bdp->numPorts;
866         /* following needed for all cases of OS versions */
867         ha->host = host;
868         ha->host_no = host->host_no;
869
870         host->can_queue = 0xfffff;      /* unlimited  */
871         host->cmd_per_lun = 1;
872 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,18)
873         host->base = (unsigned char *)ha->mmpbase;
874 #else
875         host->base = (unsigned long)ha->mmpbase;
876 #endif
877         host->max_channel = bdp->numPorts - 1;
878         host->max_lun = MAX_LUNS - 1;
879         host->max_id = MAX_TARGETS;
880 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,7)
881         host->max_sectors = 1024;
882 #endif
883
884         ha->instance = num_hosts;
885         host->unique_id = ha->instance;
886
887         if (qla1280_pci_config(ha)) {
888                 printk(KERN_INFO "qla1x160: Unable to configure PCI\n");
889                 goto error_mem_alloced;
890         }
891
892         /* Disable ISP interrupts. */
893         qla1280_disable_intrs(ha);
894
895         /* Register the IRQ with Linux (sharable) */
896         if (request_irq(host->irq, qla1280_intr_handler, SA_SHIRQ,
897                         "qla1280", ha)) {
898                 printk("qla1280 : Failed to reserve interrupt %d already "
899                        "in use\n", host->irq);
900                 goto error_unmap;
901         }
902 #if !MEMORY_MAPPED_IO
903         /* Register the I/O space with Linux */
904         if (!request_region(host->io_port, 0xff, "qla1280")) {
905                 printk("qla1280 : Failed to reserve i/o region 0x%04lx-0x%04lx"
906                        " already in use\n",
907                        host->io_port, host->io_port + 0xff);
908                 goto error_irq;
909         }
910
911 #endif
912
913         reg = ha->iobase;
914
915         /* load the F/W, read paramaters, and init the H/W */
916         if (qla1280_initialize_adapter(ha)) {
917                 printk(KERN_INFO "qla1x160:Failed to initialize adapter\n");
918                 goto error_region;
919         }
920
921         /* set our host ID  (need to do something about our two IDs) */
922         host->this_id = ha->bus_settings[0].id;
923
924         return host;
925
926  error_region:
927 #if !MEMORY_MAPPED_IO
928         release_region(host->io_port, 0xff);
929 #endif
930
931  error_irq:
932         free_irq(host->irq, ha);
933
934  error_unmap:
935 #if MEMORY_MAPPED_IO
936         if (ha->mmpbase)
937                 iounmap((void *)(((unsigned long) ha->mmpbase) & PAGE_MASK));
938 #endif
939
940
941  error_mem_alloced:
942         qla1280_mem_free(ha);
943
944  error:
945         if (host) {
946                 scsi_unregister(host);
947         }
948         return NULL;
949 }
950
951 /**************************************************************************
952  * qla1280_detect
953  *    This routine will probe for Qlogic 1280 SCSI host adapters.
954  *    It returns the number of host adapters of a particular
955  *    type that were found.      It also initialize all data necessary for
956  *    the driver.  It is passed-in the host number, so that it
957  *    knows where its first entry is in the scsi_hosts[] array.
958  *
959  * Input:
960  *     template - pointer to SCSI template
961  *
962  * Returns:
963  *  num - number of host adapters found.
964  **************************************************************************/
965 int
966 qla1280_detect(Scsi_Host_Template * template)
967 {
968         struct pci_dev *pdev = NULL;
969         struct Scsi_Host *host;
970         struct scsi_qla_host *ha, *cur_ha;
971         struct qla_boards *bdp;
972         uint16_t subsys_vendor, subsys_device;
973         int num_hosts = 0;
974         int devnum = 0;
975
976         ENTER("qla1280_detect");
977
978         if (sizeof(srb_t) > sizeof(Scsi_Pointer)) {
979                 printk(KERN_WARNING
980                        "qla1280_detect: [WARNING] srb_t too big\n");
981                 return 0;
982         }
983 #ifdef MODULE
984         dprintk(1, "DEBUG: qla1280_detect starts at address = %p\n",
985                 qla1280_detect);
986         /*
987          * If we are called as a module, the qla1280 pointer may not be null
988          * and it would point to our bootup string, just like on the lilo
989          * command line.  IF not NULL, then process this config string with
990          * qla1280_setup
991          *
992          * Boot time Options
993          * To add options at boot time add a line to your lilo.conf file like:
994          * append="qla1280=verbose,max_tags:{{255,255,255,255},{255,255,255,255}}"
995          * which will result in the first four devices on the first two
996          * controllers being set to a tagged queue depth of 32.
997          */
998         if (options)
999                 qla1280_setup(options, NULL);
1000
1001         printk(KERN_WARNING
1002                "qla1280: Please read the file /usr/src/linux/Documentation"
1003                "/scsi/qla1280.txt\n"
1004                "qla1280: to see the proper way to specify options to the qla1280 "
1005                "module\n"
1006                "qla1280: Specifically, don't use any commas when passing "
1007                "arguments to\n"
1008                "qla1280: insmod or else it might trash certain memory areas.\n");
1009 #endif
1010
1011         bdp = &ql1280_board_tbl[0];
1012         qla1280_hostlist = NULL;
1013 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,18)
1014         template->proc_dir = &proc_scsi_qla1280;
1015 #else
1016         template->proc_name = "qla1280";
1017 #endif
1018
1019         /* 3.20 */
1020         /* First Initialize QLA12160 on PCI Bus 1 Dev 2 */
1021         while ((pdev = pci_find_subsys(PCI_VENDOR_ID_QLOGIC, bdp->device_id,
1022                                        PCI_ANY_ID, PCI_ANY_ID, pdev))) {
1023
1024                 /* find QLA12160 device on PCI bus=1 slot=2 */
1025                 if ((pdev->bus->number != 1) || (PCI_SLOT(pdev->devfn) != 2))
1026                         continue;
1027
1028                 /* Bypass all AMI SUBSYS VENDOR IDs */
1029                 if (pdev->subsystem_vendor == PCI_VENDOR_ID_AMI) {
1030                         printk(KERN_INFO
1031                                "qla1x160: Skip AMI SubSys Vendor ID Chip\n");
1032                         continue;
1033                 }
1034
1035                 if (pci_enable_device(pdev))
1036                         goto find_devices;
1037
1038                 host = qla1280_do_device_init(pdev, template, devnum,
1039                                               bdp, num_hosts);
1040                 if (!host)
1041                         continue;
1042                 ha = (struct scsi_qla_host *)host->hostdata;
1043
1044                 /* this preferred device will always be the first one found */
1045                 cur_ha = qla1280_hostlist = ha;
1046                 num_hosts++;
1047         }
1048
1049  find_devices:
1050
1051         pdev = NULL;
1052         /* Try and find each different type of adapter we support */
1053         for (devnum = 0; bdp->device_id != 0 && devnum < NUM_OF_ISP_DEVICES;
1054              devnum++, bdp++) {
1055                 /* PCI_SUBSYSTEM_IDS supported */
1056                 while ((pdev = pci_find_subsys(PCI_VENDOR_ID_QLOGIC,
1057                                                bdp->device_id, PCI_ANY_ID,
1058                                                PCI_ANY_ID, pdev))) {
1059                         if (pci_enable_device(pdev))
1060                                 continue;
1061                         /* found an adapter */
1062 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,18)
1063                         subsys_vendor = pdev->subsystem_vendor;
1064                         subsys_device = pdev->subsystem_device;
1065 #else
1066                         pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID,
1067                                              &subsys_vendor);
1068                         pci_read_config_word(pdev, PCI_SUBSYSTEM_ID,
1069                                              &subsys_device);
1070 #endif
1071                         /*
1072                          * skip QLA12160 already initialized on
1073                          * PCI Bus 1 Dev 2 since we already initialized
1074                          * and presented it
1075                          */
1076                         if ((bdp->device_id == PCI_DEVICE_ID_QLOGIC_ISP12160)&&
1077                             (pdev->bus->number == 1) &&
1078                             (PCI_SLOT(pdev->devfn) == 2))
1079                                 continue;
1080
1081                         /* Bypass all AMI SUBSYS VENDOR IDs */
1082                         if (subsys_vendor == PCI_VENDOR_ID_AMI) {
1083                                 printk(KERN_INFO
1084                                        "qla1x160: Skip AMI SubSys Vendor ID Chip\n");
1085                                 continue;
1086                         }
1087                         printk(KERN_INFO
1088                                "qla1x160: Supported Device Found VID=%x "
1089                                "DID=%x SSVID=%x SSDID=%x\n", pdev->vendor,
1090                                pdev->device, subsys_vendor, subsys_device);
1091
1092                         host = qla1280_do_device_init(pdev, template,
1093                                                       devnum, bdp, num_hosts);
1094                         if (!host)
1095                                 continue;
1096                         ha = (struct scsi_qla_host *)host->hostdata;
1097
1098                         if (qla1280_hostlist == NULL) {
1099                                 cur_ha = qla1280_hostlist = ha;
1100                         } else {
1101                                 cur_ha = qla1280_hostlist;
1102                                 while (cur_ha->next != NULL)
1103                                         cur_ha = cur_ha->next;
1104                                 cur_ha->next = ha;
1105                         }
1106                         num_hosts++;
1107                 }               /* end of WHILE */
1108         }                       /* end of FOR */
1109
1110         LEAVE("qla1280_detect");
1111         return num_hosts;
1112 }
1113
1114 /**************************************************************************
1115  *   qla1280_release
1116  *   Free the passed in Scsi_Host memory structures prior to unloading the
1117  *   module.
1118  **************************************************************************/
1119 int
1120 qla1280_release(struct Scsi_Host *host)
1121 {
1122         struct scsi_qla_host *ha = (struct scsi_qla_host *)host->hostdata;
1123
1124         ENTER("qla1280_release");
1125
1126         if (!ha->flags.online)
1127                 return 0;
1128
1129         /* turn-off interrupts on the card */
1130         WRT_REG_WORD(&ha->iobase->ictrl, 0);
1131
1132         /* Detach interrupts */
1133         if (host->irq)
1134                 free_irq(host->irq, ha);
1135
1136 #if MEMORY_MAPPED_IO
1137         if (ha->mmpbase)
1138                 iounmap((void *)(((unsigned long) ha->mmpbase) & PAGE_MASK));
1139 #else
1140         /* release io space registers  */
1141         if (host->io_port)
1142                 release_region(host->io_port, 0xff);
1143 #endif                          /* MEMORY_MAPPED_IO */
1144
1145         qla1280_mem_free(ha);
1146
1147         ENTER("qla1280_release");
1148         return 0;
1149 }
1150
1151 /**************************************************************************
1152  *   qla1280_info
1153  *     Return a string describing the driver.
1154  **************************************************************************/
1155 const char *
1156 qla1280_info(struct Scsi_Host *host)
1157 {
1158         static char qla1280_scsi_name_buffer[125];
1159         char *bp;
1160         struct scsi_qla_host *ha;
1161         struct qla_boards *bdp;
1162
1163         bp = &qla1280_scsi_name_buffer[0];
1164         ha = (struct scsi_qla_host *)host->hostdata;
1165         bdp = &ql1280_board_tbl[ha->devnum];
1166         memset(bp, 0, sizeof(qla1280_scsi_name_buffer));
1167         sprintf (bp,
1168                  "QLogic %s PCI to SCSI Host Adapter: bus %d device %d irq %d\n"
1169                  "       Firmware version: %2d.%02d.%02d, Driver version %s",
1170                  &bdp->bdName[0], ha->pci_bus, (ha->pci_device_fn & 0xf8) >> 3,
1171                  host->irq, bdp->fwver[0], bdp->fwver[1], bdp->fwver[2],
1172                  QLA1280_VERSION);
1173         return bp;
1174 }
1175
1176 /**************************************************************************
1177  *   qla1200_queuecommand
1178  *     Queue a command to the controller.
1179  *
1180  * Note:
1181  * The mid-level driver tries to ensures that queuecommand never gets invoked
1182  * concurrently with itself or the interrupt handler (although the
1183  * interrupt handler may call this routine as part of request-completion
1184  * handling).   Unfortunely, it sometimes calls the scheduler in interrupt
1185  * context which is a big NO! NO!.
1186  **************************************************************************/
1187 int
1188 qla1280_queuecommand(Scsi_Cmnd * cmd, void (*fn) (Scsi_Cmnd *))
1189 {
1190         struct scsi_qla_host *ha;
1191         srb_t *sp;
1192         struct Scsi_Host *host;
1193         int bus, target, lun;
1194         scsi_lu_t *q;
1195
1196         /*ENTER("qla1280_queuecommand");
1197          */
1198
1199         host = cmd->device->host;
1200         ha = (struct scsi_qla_host *)host->hostdata;
1201
1202         /* send command to adapter */
1203         sp = (srb_t *)CMD_SP(cmd);
1204         sp->cmd = cmd;
1205         cmd->scsi_done = fn;
1206         if (cmd->flags == 0) {  /* new command */
1207                 sp->flags = 0;
1208         }
1209
1210         qla1280_print_scsi_cmd(5, cmd);
1211
1212         /* Generate LU queue on bus, target, LUN */
1213         bus = SCSI_BUS_32(cmd);
1214         target = SCSI_TCN_32(cmd);
1215         lun = SCSI_LUN_32(cmd);
1216         if ((q = LU_Q(ha, bus, target, lun)) == NULL) {
1217                 if ((q = (scsi_lu_t *)kmalloc(sizeof(struct scsi_lu),
1218                                           GFP_ATOMIC))) {
1219                         LU_Q(ha, bus, target, lun) = q;
1220                         memset(q, 0, sizeof(struct scsi_lu));
1221                         dprintk(1, "Allocate new device queue 0x%p\n",
1222                                 (void *)q);
1223                 } else {
1224                         CMD_RESULT(cmd) = DID_BUS_BUSY << 16;
1225                         qla1280_done_q_put(sp, &ha->done_q_first,
1226                                            &ha->done_q_last);
1227 /* 3.22 */
1228 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)  /* 3.22 */
1229                         schedule_work(&ha->run_qla_bh);
1230 #else                           /* 3.22 */
1231                         schedule_work(&ha->run_qla_bh); /* 3.22 */
1232 #endif                          /* 3.22 */
1233                         return 0;
1234                 }
1235         }
1236         /* Set an invalid handle until we issue the command to ISP */
1237         /* then we will set the real handle value.                 */
1238         CMD_HANDLE(cmd) = (unsigned char *)INVALID_HANDLE;
1239
1240         /* add the command to our queue */
1241         ha->qthreads++;
1242         qla1280_putq_t(q, sp);
1243
1244         dprintk(1, "qla1280_QC: t=%x CDB=%x I/OSize=0x%x haQueueCount=0x%lx\n",
1245                 target, cmd->cmnd[0], cmd->request_bufflen, ha->qthreads);
1246
1247         /* send command to adapter */
1248         if (q->q_outcnt == 0)
1249                 qla1280_restart_queues(ha);
1250
1251         /*LEAVE("qla1280_queuecommand"); */
1252         return 0;
1253 }
1254
1255 typedef enum {
1256         ABORT_COMMAND,
1257         ABORT_DEVICE,
1258         DEVICE_RESET,
1259         BUS_RESET,
1260         ADAPTER_RESET,
1261         FAIL
1262 } action_t;
1263
1264 /* timer action for error action processor */
1265 static void qla1280_error_wait_timeout(unsigned long __data)
1266 {
1267         struct scsi_cmnd *cmd = (struct scsi_cmnd *)__data;
1268         srb_t *sp = (srb_t *)CMD_SP(cmd);
1269
1270         complete(sp->wait);
1271 }
1272
1273 /**************************************************************************
1274  * qla1200_error_action
1275  *    The function will attempt to perform a specified error action and
1276  *    wait for the results (or time out).
1277  *
1278  * Input:
1279  *      cmd = Linux SCSI command packet of the command that cause the
1280  *            bus reset.
1281  *      action = error action to take (see action_t)
1282  *
1283  * Returns:
1284  *      SUCCESS or FAIL
1285  *
1286  * Note:
1287  *      Resetting the bus always succeeds - is has to, otherwise the
1288  *      kernel will panic! Try a surgical technique - sending a BUS
1289  *      DEVICE RESET message - on the offending target before pulling
1290  *      the SCSI bus reset line.
1291  **************************************************************************/
1292 int
1293 qla1280_error_action(Scsi_Cmnd * cmd, action_t action)
1294 {
1295         struct scsi_qla_host *ha;
1296         int bus, target, lun;
1297         srb_t *sp;
1298         uint16_t data;
1299         unsigned char *handle;
1300         scsi_lu_t *q;
1301         int result;
1302         DECLARE_COMPLETION(wait);
1303         struct timer_list timer;
1304
1305         ENTER("qla1280_error_action");
1306         if (qla1280_verbose)
1307                 printk(KERN_INFO "scsi(): Resetting Cmnd=0x%p, Handle=0x%p, "
1308                        "action=0x%x\n", cmd, CMD_HANDLE(cmd), action);
1309
1310         if (cmd == NULL) {
1311                 printk(KERN_WARNING
1312                        "(scsi?:?:?:?) Reset called with NULL Scsi_Cmnd "
1313                        "pointer, failing.\n");
1314                 LEAVE("qla1280_error_action");
1315                 return FAIL;
1316         }
1317
1318         ha = (struct scsi_qla_host *)cmd->device->host->hostdata;
1319         sp = (srb_t *)CMD_SP(cmd);
1320         handle = CMD_HANDLE(cmd);
1321
1322 #if STOP_ON_RESET
1323         qla1280_panic("qla1280_reset", ha->host);
1324 #endif
1325
1326         /* Check for pending interrupts. */
1327         data = qla1280_debounce_register(&ha->iobase->istatus);
1328         /*
1329          * The io_request_lock is held when the reset handler is called, hence
1330          * the interrupt handler cannot be running in parallel as it also
1331          * grabs the lock. /Jes
1332          */
1333         if (data & RISC_INT)
1334                 qla1280_isr(ha, &ha->done_q_first, &ha->done_q_last);
1335
1336         /*
1337          * Determine the suggested action that the mid-level driver wants
1338          * us to perform.
1339          */
1340         if (handle == NULL) {
1341                 if(action == ABORT_COMMAND) {
1342                         /* we never got this command */
1343                         printk(KERN_INFO "qla1280: Aborting a NULL handle\n");
1344                         return SUCCESS; /* no action - we don't have command */
1345                 }
1346         } else {
1347                 sp->wait = &wait;
1348         }
1349
1350         bus = SCSI_BUS_32(cmd);
1351         target = SCSI_TCN_32(cmd);
1352         lun = SCSI_LUN_32(cmd);
1353         q = LU_Q(ha, bus, target, lun);
1354
1355         /* Overloading result.  Here it means the success or fail of the
1356          * *issue* of the action.  When we return from the routine, it must
1357          * mean the actual success or fail of the action */
1358         result = FAIL;
1359         switch (action) {
1360         case FAIL:
1361                 break;
1362
1363         case ABORT_COMMAND:
1364                 if (q == NULL) {
1365                         /* No lun queue -- command must not be active */
1366                         printk(KERN_WARNING "qla1280 (%d:%d:%d): No LUN queue for the "
1367                                "specified device\n", bus, target, lun);
1368                         break;
1369                 }
1370                 if ((sp->flags & SRB_ABORT_PENDING)) {
1371                         printk(KERN_WARNING
1372                                "scsi(): Command has a pending abort "
1373                                "message - ABORT_PENDING.\n");
1374                         /* This should technically be impossible since we
1375                          * now wait for abort completion */
1376                         break;
1377                 }
1378
1379                 /*
1380                  * Normally, would would need to search our queue for
1381                  * the specified command but; since our sp contains
1382                  * the cmd ptr, we can just remove it from our LUN
1383                  * queue.
1384                  */
1385                 if (!(sp->flags & SRB_SENT)) {
1386                         if (qla1280_verbose)
1387                                 printk(KERN_WARNING
1388                                        "scsi(): Command returned from queue "
1389                                        "aborted.\n");
1390                         
1391                         /* Remove srb from SCSI LU queue. */
1392                         qla1280_removeq(q, sp);
1393                         sp->flags |= SRB_ABORTED;
1394                         CMD_RESULT(cmd) = DID_ABORT << 16;
1395                         qla1280_done_q_put(sp, &ha->done_q_first, &ha->done_q_last);
1396                         if (ha->done_q_first)
1397                                 qla1280_done(ha, &ha->done_q_first, &ha->done_q_last);
1398                         
1399                         qla1280_restart_queues(ha);
1400                         
1401                 } else {        /* find the command in our active list */
1402                         int i;
1403
1404                         for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
1405                                 if (sp == ha->outstanding_cmds[i]) {
1406                                         dprintk(1,
1407                                                 "qla1280: RISC aborting command.\n");
1408                                         qla1280_abort_command(ha, sp);
1409                                 }
1410                         }
1411                 }
1412                 break;
1413
1414
1415
1416         case ABORT_DEVICE:
1417                 ha->flags.in_reset = TRUE;
1418                 if (qla1280_verbose)
1419                         printk(KERN_INFO
1420                                "scsi(%ld:%d:%d:%d): Queueing abort device "
1421                                "command.\n", ha->host_no, bus, target, lun);
1422                 qla1280_abort_queue_single(ha, bus, target, lun, DID_ABORT);
1423                 if (qla1280_abort_device(ha, bus, target, lun) == 0)
1424                         result = SUCCESS;
1425                 break;
1426
1427         case DEVICE_RESET:
1428                 if (qla1280_verbose)
1429                         printk(KERN_INFO
1430                                "scsi(%ld:%d:%d:%d): Queueing device reset "
1431                                "command.\n", ha->host_no, bus, target, lun);
1432                 ha->flags.in_reset = TRUE;
1433                 for (lun = 0; lun < MAX_LUNS; lun++)
1434                         qla1280_abort_queue_single(ha, bus, target, lun,
1435                                                    DID_ABORT);
1436                 if (qla1280_device_reset(ha, bus, target) == 0)
1437                         result = SUCCESS;
1438                 q->q_flag |= QLA1280_QRESET;
1439                 break;
1440
1441         case BUS_RESET:
1442                 if (qla1280_verbose)
1443                         printk(KERN_INFO "qla1280(%ld:%d:%d:%d): Issuing BUS "
1444                                "DEVICE RESET.\n", ha->host_no, bus, target,
1445                                lun);
1446                 ha->flags.in_reset = TRUE;
1447                 for (target = 0; target < MAX_TARGETS; target++)
1448                         for (lun = 0; lun < MAX_LUNS; lun++)
1449                                 qla1280_abort_queue_single(ha, bus, target,
1450                                                            lun, DID_RESET);
1451                 qla1280_bus_reset(ha, bus);
1452
1453                 /* wait 4 seconds */
1454                 schedule_timeout(4*HZ);
1455
1456                 result = SUCCESS;
1457
1458                 break;
1459
1460         case ADAPTER_RESET:
1461         default:
1462                 if (qla1280_verbose) {
1463                         printk(KERN_INFO
1464                                "scsi(%ld:%d:%d:%d): Issued an ADAPTER "
1465                                "RESET.\n", ha->host_no, bus, target, lun);
1466                         printk(KERN_INFO
1467                                "scsi(%ld:%d:%d:%d): I/O processing will "
1468                                "continue automatically.\n", ha->host_no, bus,
1469                                target, lun);
1470                 }
1471                 ha->flags.reset_active = TRUE;
1472                 /*
1473                  * We restarted all of the commands automatically, so the
1474                  * mid-level code can expect completions momentitarily.
1475                  */
1476                 if (qla1280_abort_isp(ha) == 0)
1477                         result = SUCCESS;
1478
1479                 ha->flags.reset_active = FALSE;
1480         }
1481
1482         if (ha->done_q_first)
1483                 qla1280_done(ha, &ha->done_q_first, &ha->done_q_last);
1484         qla1280_restart_queues(ha);
1485         ha->flags.in_reset = FALSE;
1486
1487         /* If we didn't manage to issue the action, or we have no
1488          * command to wait for, exit here */
1489         if(result == FAIL || handle == NULL)
1490                 goto leave;
1491
1492         /* set up a timer just in case we're really jammed */
1493         init_timer(&timer);
1494         timer.expires = jiffies + 4*HZ;
1495         timer.data = (unsigned long)cmd;
1496         timer.function = qla1280_error_wait_timeout;
1497         add_timer(&timer);
1498
1499         /* wait for the action to complete (or the timer to expire) */
1500         spin_unlock_irq(ha->host->host_lock);
1501         wait_for_completion(&wait);
1502         del_timer_sync(&timer);
1503         spin_lock_irq(ha->host->host_lock);
1504         sp->wait = NULL;
1505         
1506         /* the only action we might get a fail for is abort */
1507         if(action == ABORT_COMMAND) {
1508                 if(sp->flags & SRB_ABORTED)
1509                         result = SUCCESS;
1510                 else
1511                         result = FAILED;
1512         }
1513
1514  leave:
1515         dprintk(1, "RESET returning %d\n", result);
1516
1517         LEAVE("qla1280_error_action");
1518         return result;
1519 }
1520
1521 /**************************************************************************
1522  *   qla1200_abort
1523  *     Abort the specified SCSI command(s).
1524  **************************************************************************/
1525 int
1526 qla1280_eh_abort(struct scsi_cmnd * cmd)
1527 {
1528         return qla1280_error_action(cmd, ABORT_COMMAND);
1529 }
1530
1531 /**************************************************************************
1532  *   qla1200_device_reset
1533  *     Reset the specified SCSI device
1534  **************************************************************************/
1535 int
1536 qla1280_eh_device_reset(struct scsi_cmnd *cmd)
1537 {
1538         return qla1280_error_action(cmd, DEVICE_RESET);
1539 }
1540
1541 /**************************************************************************
1542  *   qla1200_bus_reset
1543  *     Reset the specified bus.
1544  **************************************************************************/
1545 int
1546 qla1280_eh_bus_reset(struct scsi_cmnd *cmd)
1547 {
1548         return qla1280_error_action(cmd, BUS_RESET);
1549 }
1550
1551 /**************************************************************************
1552  *   qla1200_adapter_reset
1553  *     Reset the specified adapter (both channels)
1554  **************************************************************************/
1555 int
1556 qla1280_eh_adapter_reset(struct scsi_cmnd *cmd)
1557 {
1558         return qla1280_error_action(cmd, ADAPTER_RESET);
1559 }
1560
1561 /**************************************************************************
1562  * qla1280_biosparam
1563  *   Return the disk geometry for the given SCSI device.
1564  **************************************************************************/
1565 int
1566 qla1280_biosparam(struct scsi_device *sdev, struct block_device *bdev,
1567                 sector_t capacity, int geom[])
1568 {
1569         int heads, sectors, cylinders;
1570
1571         heads = 64;
1572         sectors = 32;
1573         cylinders = (unsigned long)capacity / (heads * sectors);
1574         if (cylinders > 1024) {
1575                 heads = 255;
1576                 sectors = 63;
1577                 cylinders = (unsigned long)capacity / (heads * sectors);
1578                 /* if (cylinders > 1023)
1579                    cylinders = 1023; */
1580         }
1581
1582         geom[0] = heads;
1583         geom[1] = sectors;
1584         geom[2] = cylinders;
1585
1586         return 0;
1587 }
1588
1589 /**************************************************************************
1590  * qla1280_intr_handler
1591  *   Handles the H/W interrupt
1592  **************************************************************************/
1593 irqreturn_t
1594 qla1280_intr_handler(int irq, void *dev_id, struct pt_regs *regs)
1595 {
1596         struct scsi_qla_host *ha;
1597         struct device_reg *reg;
1598         int handled = 0;
1599         u16 data;
1600
1601         ENTER_INTR ("qla1280_intr_handler");
1602         ha = (struct scsi_qla_host *)dev_id;
1603
1604         spin_lock(ha->host->host_lock);
1605
1606         ha->isr_count++;
1607         reg = ha->iobase;
1608
1609         WRT_REG_WORD(&reg->ictrl, 0);   /* disable our interrupt. */
1610
1611         data = qla1280_debounce_register(&reg->istatus);
1612         /* Check for pending interrupts. */
1613         if (data & RISC_INT) {
1614                 qla1280_isr(ha, &ha->done_q_first, &ha->done_q_last);
1615                 handled = 1;
1616         } else {
1617                 /* spurious interrupts can happen legally */
1618                 dprintk(1, "scsi(%ld): Spurious interrupt - ignoring\n",
1619                         ha->host_no);
1620         }
1621
1622         if (ha->done_q_first)
1623                 qla1280_done(ha, &ha->done_q_first, &ha->done_q_last);
1624
1625         spin_unlock(ha->host->host_lock);
1626
1627         /* enable our interrupt. */
1628         WRT_REG_WORD(&reg->ictrl, (ISP_EN_INT | ISP_EN_RISC));
1629
1630         LEAVE_INTR("qla1280_intr_handler");
1631         return IRQ_RETVAL(handled);
1632 }
1633
1634 /**************************************************************************
1635  *   qla1280_do_dpc
1636  *
1637  * Description:
1638  * This routine is a task that is schedule by the interrupt handler
1639  * to perform the background processing for interrupts.  We put it
1640  * on a task queue that is consumed whenever the scheduler runs; that's
1641  * so you can do anything (i.e. put the process to sleep etc).  In fact, the
1642  * mid-level tries to sleep when it reaches the driver threshold
1643  * "host->can_queue". This can cause a panic if we were in our interrupt
1644  * code .
1645  **************************************************************************/
1646 void
1647 qla1280_do_dpc(void *p)
1648 {
1649         struct scsi_qla_host *ha = (struct scsi_qla_host *) p;
1650         unsigned long cpu_flags;
1651
1652         spin_lock_irqsave(ha->host->host_lock, cpu_flags);
1653
1654         if (ha->flags.isp_abort_needed)
1655                 qla1280_abort_isp(ha);
1656
1657         if (ha->flags.reset_marker)
1658                 qla1280_rst_aen(ha);
1659
1660         if (ha->done_q_first)
1661                 qla1280_done(ha, &ha->done_q_first, &ha->done_q_last);
1662
1663         spin_unlock_irqrestore(ha->host->host_lock, cpu_flags);
1664 }
1665
1666 /**************************************************************************
1667  *   qla1280_slave_configure
1668  *
1669  * Description:
1670  *   Determines the queue depth for a given device.  There are two ways
1671  *   a queue depth can be obtained for a tagged queueing device.  One
1672  *   way is the default queue depth which is determined by whether
1673  *   If it is defined, then it is used
1674  *   as the default queue depth.  Otherwise, we use either 4 or 8 as the
1675  *   default queue depth (dependent on the number of hardware SCBs).
1676  **************************************************************************/
1677 static int
1678 qla1280_slave_configure(Scsi_Device * device)
1679 {
1680         struct scsi_qla_host *p = (struct scsi_qla_host *)device->host->hostdata;
1681         int bus = device->channel;
1682         int target = device->id;
1683
1684         if (qla1280_check_for_dead_scsi_bus(p, bus))
1685                 return 1;
1686         if (device->tagged_supported &&
1687             (p->bus_settings[bus].qtag_enables & (BIT_0 << target))) {
1688                 scsi_adjust_queue_depth(device, MSG_ORDERED_TAG,
1689                                 p->bus_settings[bus].hiwat);
1690                 /* device->queue_depth = 20; */
1691                 printk(KERN_INFO "scsi(%li:%d:%d:%d): Enabled tagged queuing, "
1692                        "queue depth %d.\n", p->host_no, device->channel,
1693                        device->id, device->lun, device->queue_depth);
1694         } else {
1695                 scsi_adjust_queue_depth(device, 0 /* TCQ off */, 3);
1696         }
1697         qla12160_get_target_parameters(p, bus, target, device->lun);
1698         return 0;
1699 }
1700
1701 /*
1702  * Driver Support Routines
1703  */
1704
1705 /*
1706  * qla1280_done
1707  *      Process completed commands.
1708  *
1709  * Input:
1710  *      ha           = adapter block pointer.
1711  *      done_q_first = done queue first pointer.
1712  *      done_q_last  = done queue last pointer.
1713  */
1714 static void
1715 qla1280_done(struct scsi_qla_host *ha, srb_t ** done_q_first,
1716              srb_t ** done_q_last)
1717 {
1718         srb_t *sp;
1719         scsi_lu_t *q;
1720         int bus, target, lun;
1721         Scsi_Cmnd *cmd;
1722
1723         ENTER("qla1280_done");
1724
1725         while (*done_q_first != NULL) {
1726                 /* remove command from done list */
1727                 sp = *done_q_first;
1728                 if (!(*done_q_first = sp->s_next))
1729                         *done_q_last = NULL;
1730                 else
1731                         (*done_q_first)->s_prev = NULL;
1732
1733                 cmd = sp->cmd;
1734                 bus = SCSI_BUS_32(cmd);
1735                 target = SCSI_TCN_32(cmd);
1736                 lun = SCSI_LUN_32(cmd);
1737                 q = LU_Q(ha, bus, target, lun);
1738
1739                 /* Decrement outstanding commands on device. */
1740                 if (q->q_outcnt)
1741                         q->q_outcnt--;
1742                 if (q->q_outcnt < ha->bus_settings[bus].hiwat) {
1743                         q->q_flag &= ~QLA1280_QBUSY;
1744                 }
1745
1746                 q->io_cnt++;
1747                 if (sp->dir & BIT_5)
1748                         q->r_cnt++;
1749                 else
1750                         q->w_cnt++;
1751
1752                 switch ((CMD_RESULT(cmd) >> 16)) {
1753                 case DID_RESET:
1754                         q->q_flag &= ~QLA1280_QRESET;
1755                         /* Issue marker command. */
1756                         qla1280_marker(ha, bus, target, 0, MK_SYNC_ID);
1757                         break;
1758                 case DID_ABORT:
1759                         sp->flags &= ~SRB_ABORT_PENDING;
1760                         sp->flags |= SRB_ABORTED;
1761                         if (sp->flags & SRB_TIMEOUT)
1762                                 CMD_RESULT(sp->cmd) = DID_TIME_OUT << 16;
1763                         break;
1764                 default:
1765                         break;
1766                 }
1767
1768                 /* 3.13   64 and 32 bit */
1769                 /* Release memory used for this I/O */
1770                 if (cmd->use_sg) {
1771                         dprintk(1, "S/G unmap_sg cmd=%p\n", cmd);
1772
1773                         pci_unmap_sg(ha->pdev, cmd->request_buffer,
1774                                      cmd->use_sg,
1775                                      scsi_to_pci_dma_dir(cmd->sc_data_direction));
1776                 } else if (cmd->request_bufflen) {
1777                         /*dprintk(1, "No S/G unmap_single cmd=%x saved_dma_handle=%lx\n",
1778                           cmd, sp->saved_dma_handle); */
1779
1780                         pci_unmap_single(ha->pdev, sp->saved_dma_handle,
1781                                          cmd->request_bufflen,
1782                                          scsi_to_pci_dma_dir(cmd->sc_data_direction));
1783                 }
1784
1785                 /* Call the mid-level driver interrupt handler */
1786                 CMD_HANDLE(sp->cmd) = NULL;
1787                 ha->actthreads--;
1788
1789                 (*(cmd)->scsi_done)(cmd);
1790
1791                 if(sp->wait != NULL)
1792                         complete(sp->wait);
1793
1794                 qla1280_next(ha, q, bus);
1795         }
1796         LEAVE("qla1280_done");
1797 }
1798
1799 /*
1800  * Translates a ISP error to a Linux SCSI error
1801  */
1802 static int
1803 qla1280_return_status(sts_entry_t * sts, Scsi_Cmnd * cp)
1804 {
1805         int host_status = DID_ERROR;
1806 #if DEBUG_QLA1280_INTR
1807         static char *reason[] = {
1808                 "DID_OK",
1809                 "DID_NO_CONNECT",
1810                 "DID_BUS_BUSY",
1811                 "DID_TIME_OUT",
1812                 "DID_BAD_TARGET",
1813                 "DID_ABORT",
1814                 "DID_PARITY",
1815                 "DID_ERROR",
1816                 "DID_RESET",
1817                 "DID_BAD_INTR"
1818         };
1819 #endif                          /* DEBUG_QLA1280_INTR */
1820
1821         ENTER("qla1280_return_status");
1822
1823 #if DEBUG_QLA1280_INTR
1824         /*
1825           dprintk(1, "qla1280_return_status: compl status = 0x%04x\n",
1826           sts->comp_status);
1827         */
1828 #endif
1829         switch (sts->comp_status) {
1830         case CS_COMPLETE:
1831                 host_status = DID_OK;
1832                 break;
1833
1834         case CS_INCOMPLETE:
1835                 if (!(sts->state_flags & SF_GOT_BUS))
1836                         host_status = DID_NO_CONNECT;
1837                 else if (!(sts->state_flags & SF_GOT_TARGET))
1838                         host_status = DID_BAD_TARGET;
1839                 else if (!(sts->state_flags & SF_SENT_CDB))
1840                         host_status = DID_ERROR;
1841                 else if (!(sts->state_flags & SF_TRANSFERRED_DATA))
1842                         host_status = DID_ERROR;
1843                 else if (!(sts->state_flags & SF_GOT_STATUS))
1844                         host_status = DID_ERROR;
1845                 else if (!(sts->state_flags & SF_GOT_SENSE))
1846                         host_status = DID_ERROR;
1847                 break;
1848
1849         case CS_RESET:
1850                 host_status = DID_RESET;
1851                 break;
1852
1853         case CS_ABORTED:
1854                 host_status = DID_ABORT;
1855                 break;
1856
1857         case CS_TIMEOUT:
1858                 host_status = DID_TIME_OUT;
1859                 break;
1860
1861         case CS_DATA_OVERRUN:
1862                 dprintk(2, "Data overrun 0x%x\n", sts->residual_length);
1863                 dprintk(2, "qla1280_isr: response packet data\n");
1864                 qla1280_dump_buffer(2, (char *)sts, RESPONSE_ENTRY_SIZE);
1865                 host_status = DID_ERROR;
1866                 break;
1867
1868         case CS_DATA_UNDERRUN:
1869                 if ((cp->request_bufflen - sts->residual_length) <
1870                     cp->underflow) {
1871                         printk(KERN_WARNING
1872                                "scsi: Underflow detected - retrying "
1873                                "command.\n");
1874                         host_status = DID_ERROR;
1875                 } else
1876                         host_status = DID_OK;
1877                 break;
1878
1879         default:
1880                 host_status = DID_ERROR;
1881                 break;
1882         }
1883
1884 #if DEBUG_QLA1280_INTR
1885         dprintk(1, "qla1280 ISP status: host status (%s) scsi status %x\n",
1886                 reason[host_status], sts->scsi_status);
1887 #endif
1888
1889         LEAVE("qla1280_return_status");
1890
1891         return (sts->scsi_status & 0xff) | (host_status << 16);
1892 }
1893
1894 /*
1895  * qla1280_done_q_put
1896  *      Place SRB command on done queue.
1897  *
1898  * Input:
1899  *      sp           = srb pointer.
1900  *      done_q_first = done queue first pointer.
1901  *      done_q_last  = done queue last pointer.
1902  */
1903 static void
1904 qla1280_done_q_put(srb_t * sp, srb_t ** done_q_first, srb_t ** done_q_last)
1905 {
1906         ENTER("qla1280_put_done_q");
1907
1908         /* Place block on done queue */
1909         sp->s_next = NULL;
1910         sp->s_prev = *done_q_last;
1911         if (!*done_q_first)
1912                 *done_q_first = sp;
1913         else
1914                 (*done_q_last)->s_next = sp;
1915         *done_q_last = sp;
1916
1917         LEAVE("qla1280_put_done_q");
1918 }
1919
1920 /*
1921  * qla1280_next
1922  *      Retrieve and process next job in the queue.
1923  *
1924  * Input:
1925  *      ha   = adapter block pointer.
1926  *      q    = SCSI LU pointer.
1927  *      bus  = SCSI bus number.
1928  *      SCSI_LU_Qlock must be already obtained and no other locks.
1929  *
1930  * Output:
1931  *      Releases SCSI_LU_Qupon exit.
1932  */
1933 static void
1934 qla1280_next(struct scsi_qla_host *ha, scsi_lu_t * q, int bus)
1935 {
1936         srb_t *sp;
1937         int cnt, status;
1938
1939         ENTER("qla1280_next");
1940
1941         while (((sp = q->q_first) != NULL) &&   /* we have a queue pending */
1942                /* device not busy/suspended */
1943                !(q->q_flag & (QLA1280_QBUSY | QLA1280_QSUSP)) && !ha->flags.abort_isp_active) { /* not resetting the adapter */
1944                 /* Remove srb from SCSI LU queue. */
1945                 qla1280_removeq(q, sp);
1946
1947                 dprintk(1, "starting request 0x%p<-(0x%p)\n", q, sp);
1948                 {
1949                         /* Set busy flag if reached high water mark. */
1950                         q->q_outcnt++;
1951                         if (q->q_outcnt >= ha->bus_settings[bus].hiwat)
1952                                 q->q_flag |= QLA1280_QBUSY;
1953
1954 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,18)
1955                         if (ha->flags.enable_64bit_addressing)
1956                                 status = qla1280_64bit_start_scsi(ha, sp);
1957                         else
1958 #endif
1959                                 status = qla1280_32bit_start_scsi(ha, sp);
1960
1961                         if (status) {   /* if couldn't start the request */
1962                                 if (q->q_outcnt == 1) {
1963                                         /* Wait for 30 sec for command to be accepted. */
1964                                         for (cnt = 6000000; cnt; cnt--) {
1965 #if QLA_64BIT_PTR
1966                                                 if (ha->flags.enable_64bit_addressing)
1967                                                         status =
1968                                                                 qla1280_64bit_start_scsi(ha, sp);
1969                                                 else
1970 #endif
1971                                                         status =
1972                                                                 qla1280_32bit_start_scsi(ha, sp);
1973
1974                                                 if (!status)
1975                                                         break;
1976
1977                                                 /* Go check for pending interrupts. */
1978                                                 qla1280_poll(ha);
1979
1980                                                 udelay(5);      /* 10 */
1981                                         }
1982                                         if (!cnt) {
1983                                                 /* Set timeout status */
1984                                                 CMD_RESULT(sp->cmd) =
1985                                                         DID_TIME_OUT << 16;
1986
1987 #if WATCHDOGTIMER
1988                                                 /* Remove command from watchdog queue. */
1989                                                 if (sp->flags & SRB_WATCHDOG)
1990                                                         qla1280_timeout_remove
1991                                                                 (ha, sp);
1992 #endif
1993                                                 CMD_HANDLE(sp->cmd) = NULL;
1994
1995                                                 /* Call the mid-level driver interrupt handler */
1996                                                 (*(sp->cmd)->scsi_done)(sp->cmd);
1997
1998                                                 if (q->q_outcnt)
1999                                                         q->q_outcnt--;
2000                                         }
2001                                 } else {        /* Place request back on top of device queue. */
2002                                         qla1280_putq_t(q, sp);
2003
2004                                         if (q->q_outcnt)
2005                                                 q->q_outcnt--;
2006                                         if (q->q_outcnt <
2007                                             ha->bus_settings[bus].hiwat)
2008                                                 q->q_flag &= ~QLA1280_QBUSY;
2009                                         break;
2010                                 }
2011                         }
2012                 }
2013         }
2014
2015         LEAVE("qla1280_next");
2016 }
2017
2018 /*
2019  * qla1280_putq_t
2020  *      Add the standard SCB job to the top of standard SCB commands.
2021  *
2022  * Input:
2023  *      q  = SCSI LU pointer.
2024  *      sp = srb pointer.
2025  *      SCSI_LU_Qlock must be already obtained.
2026  */
2027 static void
2028 qla1280_putq_t(scsi_lu_t * q, srb_t * sp)
2029 {
2030         ENTER("qla1280_putq_t");
2031
2032         dprintk(1, "Adding to device q=0x%p<-(0x%p)sp\n", (void *) q,
2033                 (void *) sp);
2034
2035         sp->s_next = NULL;
2036         if (!q->q_first) {      /* If queue empty */
2037                 sp->s_prev = NULL;
2038                 q->q_first = sp;
2039                 q->q_last = sp;
2040         } else {
2041                 sp->s_prev = q->q_last;
2042                 q->q_last->s_next = sp;
2043                 q->q_last = sp;
2044         }
2045
2046         LEAVE("qla1280_putq_t");
2047 }
2048
2049 /*
2050  * qla1280_removeq
2051  *      Function used to remove a command block from the
2052  *      LU queue.
2053  *
2054  * Input:
2055  *      q  = SCSI LU pointer.
2056  *      sp = srb pointer.
2057  *      SCSI_LU_Qlock must be already obtained.
2058  */
2059 static void
2060 qla1280_removeq(scsi_lu_t * q, srb_t * sp)
2061 {
2062         dprintk(1, "Removing from device_q (0x%p)->(0x%p)\n", q, sp);
2063
2064         if (sp->s_prev) {
2065                 if ((sp->s_prev->s_next = sp->s_next) != NULL)
2066                         sp->s_next->s_prev = sp->s_prev;
2067                 else
2068                         q->q_last = sp->s_prev;
2069         } else if (!(q->q_first = sp->s_next))
2070                 q->q_last = NULL;
2071         else
2072                 q->q_first->s_prev = NULL;
2073 }
2074
2075 /*
2076 * qla1280_mem_alloc
2077 *      Allocates adapter memory.
2078 *
2079 * Returns:
2080 *      0  = success.
2081 *      1  = failure.
2082 */
2083 static int
2084 qla1280_mem_alloc(struct scsi_qla_host *ha)
2085 {
2086         int status = 1;
2087         dma_addr_t dma_handle;
2088
2089         ENTER("qla1280_mem_alloc");
2090
2091         /* 3.13 */
2092         /* get consistent memory allocated for request and response rings */
2093         ha->request_ring = pci_alloc_consistent(ha->pdev,
2094                                                 ((REQUEST_ENTRY_CNT + 1) *
2095                                                  (sizeof(request_t))),
2096                                                 &dma_handle);
2097         if (!ha->request_ring)
2098                 goto error;
2099         ha->request_dma = dma_handle;
2100         ha->response_ring = pci_alloc_consistent(ha->pdev,
2101                                                  ((RESPONSE_ENTRY_CNT + 1) *
2102                                                   (sizeof(response_t))),
2103                                                  &dma_handle);
2104         if (!ha->request_ring)
2105                 goto error;
2106         ha->response_dma = dma_handle;
2107         status = 0;
2108
2109  error:
2110         if (status)
2111                 dprintk(2, "qla1280_mem_alloc: **** FAILED ****\n");
2112
2113         LEAVE("qla1280_mem_alloc");
2114         return status;
2115 }
2116
2117 /*
2118  * qla1280_mem_free
2119  *      Frees adapter allocated memory.
2120  *
2121  * Input:
2122  *      ha = adapter block pointer.
2123  */
2124 static void
2125 qla1280_mem_free(struct scsi_qla_host *ha)
2126 {
2127         scsi_lu_t *q;
2128         int bus, target, lun;
2129
2130         ENTER("qlc1280_mem_free");
2131         if (ha) {
2132                 /* Free device queues. */
2133                 for (bus = 0; bus < MAX_BUSES; bus++) {
2134                         q = LU_Q(ha, bus, ha->bus_settings[bus].id, 0);
2135                         for (target = 0; target < MAX_TARGETS; target++)
2136                                 for (lun = 0; lun < MAX_LUNS; lun++)
2137                                         if (LU_Q(ha, bus, target, lun) != NULL
2138                                             && LU_Q(ha, bus, target, lun) != q)
2139                                                 kfree(LU_Q(ha, bus, target, lun));
2140                         kfree(q);
2141                 }
2142                 for (bus = 0; bus < MAX_EQ; bus++)
2143                         ha->dev[bus] = NULL;
2144         }
2145
2146         /* 3.13 */
2147         /* free consistent memory allocated for request and response rings */
2148         if (ha->request_ring)
2149                 pci_free_consistent(ha->pdev,
2150                                     ((REQUEST_ENTRY_CNT + 1) *
2151                                      (sizeof(request_t))),
2152                                     ha->request_ring, ha->request_dma);
2153
2154         if (ha->response_ring)
2155                 pci_free_consistent(ha->pdev,
2156                                     ((RESPONSE_ENTRY_CNT + 1) *
2157                                      (sizeof(response_t))),
2158                                     ha->response_ring, ha->response_dma);
2159
2160         if (qla1280_buffer) {
2161                 free_page((unsigned long) qla1280_buffer);
2162                 qla1280_buffer = NULL;
2163         }
2164
2165         LEAVE("qlc1280_mem_free");
2166 }
2167
2168 /****************************************************************************/
2169 /*                QLogic ISP1280 Hardware Support Functions.                */
2170 /****************************************************************************/
2171
2172  /*
2173     * qla2100_enable_intrs
2174     * qla2100_disable_intrs
2175     *
2176     * Input:
2177     *      ha = adapter block pointer.
2178     *
2179     * Returns:
2180     *      None
2181   */
2182 static inline void
2183 qla1280_enable_intrs(struct scsi_qla_host *ha)
2184 {
2185         struct device_reg *reg;
2186
2187         reg = ha->iobase;
2188         /* enable risc and host interrupts */
2189         WRT_REG_WORD(&reg->ictrl, (ISP_EN_INT | ISP_EN_RISC));
2190         ha->flags.ints_enabled = 1;
2191 #if 0
2192         printk("Enabling ints\n");
2193 #endif
2194 }
2195
2196 static inline void
2197 qla1280_disable_intrs(struct scsi_qla_host *ha)
2198 {
2199         struct device_reg *reg;
2200
2201         reg = ha->iobase;
2202         /* disable risc and host interrupts */
2203         WRT_REG_WORD(&reg->ictrl, 0);
2204         ha->flags.ints_enabled = 0;
2205 #if 0
2206         printk("Disabling ints\n");
2207 #endif
2208 }
2209
2210 /*
2211  * qla1280_initialize_adapter
2212  *      Initialize board.
2213  *
2214  * Input:
2215  *      ha = adapter block pointer.
2216  *
2217  * Returns:
2218  *      0 = success
2219  */
2220 static int
2221 qla1280_initialize_adapter(struct scsi_qla_host *ha)
2222 {
2223         struct device_reg *reg;
2224         int status;
2225         int bus;
2226
2227         ENTER("qla1280_initialize_adapter");
2228
2229         /* Clear adapter flags. */
2230         ha->flags.online = FALSE;
2231         ha->flags.isp_abort_needed = FALSE;
2232         ha->flags.disable_host_adapter = FALSE;
2233         ha->flags.reset_active = FALSE;
2234         ha->flags.abort_isp_active = FALSE;
2235
2236         ha->flags.ints_enabled = FALSE;
2237
2238         dprintk(1, "Configure PCI space for adapter...\n");
2239
2240         reg = ha->iobase;
2241
2242         /* Insure mailbox registers are free. */
2243         WRT_REG_WORD(&reg->semaphore, 0);
2244         WRT_REG_WORD(&reg->host_cmd, HC_CLR_RISC_INT);
2245         WRT_REG_WORD(&reg->host_cmd, HC_CLR_HOST_INT);
2246
2247         /* If firmware needs to be loaded */
2248         if (qla1280_verbose)
2249                 printk(KERN_INFO "scsi(%li): Determining if RISC is "
2250                        "loaded...\n", ha->host_no);
2251         if (qla1280_isp_firmware(ha)) {
2252                 if (qla1280_verbose)
2253                         printk(KERN_INFO "scsi(%ld): Verifying chip...\n",
2254                                ha->host_no);
2255                 if (!(status = qla1280_chip_diag (ha))) {
2256                         if (qla1280_verbose)
2257                                 printk(KERN_INFO "scsi(%ld): Setup chip...\n",
2258                                        ha->host_no);
2259                         status = qla1280_setup_chip(ha);
2260                 }
2261         } else {
2262                 printk(KERN_ERR "initialize: isp_firmware() failed!\n");
2263                 status = 1;
2264         }
2265
2266         if (!status) {
2267                 /* Setup adapter based on NVRAM parameters. */
2268                 if (qla1280_verbose)
2269                         printk(KERN_INFO
2270                                "scsi(%ld): Configure NVRAM parameters...\n",
2271                                ha->host_no);
2272                 qla1280_nvram_config(ha);
2273
2274                 if (!ha->flags.disable_host_adapter
2275                     && !qla1280_init_rings(ha)) {
2276                         /* Issue SCSI reset. */
2277                         /* dg 03/13 if we can't reset twice then bus is dead */
2278                         for (bus = 0; bus < ha->ports; bus++) {
2279                                 if (!ha->bus_settings[bus].disable_scsi_reset){
2280                                         if (qla1280_bus_reset(ha, bus)) {
2281                                                 if (qla1280_bus_reset(ha, bus)) {
2282                                                         ha->bus_settings[bus].scsi_bus_dead = TRUE;
2283                                                 }
2284                                         }
2285                                 }
2286                         }
2287                         do {
2288                                 /* Issue marker command. */
2289                                 ha->flags.reset_marker = FALSE;
2290                                 for (bus = 0; bus < ha->ports; bus++) {
2291                                         ha->bus_settings[bus].reset_marker = FALSE;
2292                                         qla1280_marker(ha, bus, 0, 0,
2293                                                        MK_SYNC_ALL);
2294                                 }
2295                         } while (ha->flags.reset_marker);
2296
2297                         ha->flags.online = TRUE;
2298
2299                         /* Enable host adapter target mode. */
2300                         for (bus = 0; bus < ha->ports; bus++) {
2301                                 if (!(status = qla1280_enable_tgt(ha, bus))) {
2302 #if 0
2303                                         int cnt;
2304                                         for (cnt = 0; cnt < MAX_LUNS; cnt++) {
2305                                                 qla1280_enable_lun(ha, bus,
2306                                                                    cnt);
2307                                                 qla1280_poll(ha);
2308                                         }
2309 #endif
2310                                 } else
2311                                         break;
2312                         }
2313                 } else
2314                         status = 1;
2315         } else
2316                 printk(KERN_ERR "scsi(%li): initialize: pci probe failed!\n",
2317                        ha->host_no);
2318
2319         if (status)
2320                 dprintk(2, "qla1280_initialize_adapter: **** FAILED ****\n");
2321
2322         LEAVE("qla1280_initialize_adapter");
2323         return status;
2324 }
2325
2326 /*
2327  * qla1280_enable_tgt
2328  *      Enable target mode.
2329  *
2330  * Input:
2331  *      ha   = adapter block pointer.
2332  *      bus  = SCSI bus number.
2333  *
2334  * Returns:
2335  *      0 = success.
2336  */
2337 static int
2338 qla1280_enable_tgt(struct scsi_qla_host *ha, int bus)
2339 {
2340         int status = 0;
2341         /*  uint16_t    mb[MAILBOX_REGISTER_COUNT]; */
2342
2343         dprintk(3, "qla1280_enable_tgt: entered\n");
2344
2345         /* Enable target mode. */
2346 #if 0
2347         mb[0] = MBC_ENABLE_TARGET_MODE;
2348         mb[1] = BIT_15;
2349         mb[2] = (uint16_t) (bus << 15);
2350         status = qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
2351 #endif
2352         if (status)
2353                 dprintk(2, "qla1280_enable_tgt: **** FAILED ****\n");
2354         else
2355                 dprintk(3, "qla1280_enable_tgt: exiting normally\n");
2356
2357         return status;
2358 }
2359
2360 /*
2361  * ISP Firmware Test
2362  *      Checks if present version of RISC firmware is older than
2363  *      driver firmware.
2364  *
2365  * Input:
2366  *      ha = adapter block pointer.
2367  *
2368  * Returns:
2369  *      0 = firmware does not need to be loaded.
2370  */
2371 static int
2372 qla1280_isp_firmware(struct scsi_qla_host *ha)
2373 {
2374         nvram_t *nv = (nvram_t *) ha->response_ring;
2375         uint16_t *wptr;
2376         int status = 0;         /* dg 2/27 always loads RISC */
2377         int cnt;
2378         uint8_t chksum;
2379         uint16_t mb[MAILBOX_REGISTER_COUNT];
2380
2381         ENTER("qla1280_isp_firmware");
2382
2383         /* Verify valid NVRAM checksum. */
2384         wptr = (uint16_t *) ha->response_ring;
2385         dprintk(1, "qla1280_isp_firmware: Reading NVRAM\n");
2386
2387         chksum = 0;
2388         for (cnt = 0; cnt < sizeof(nvram_t) / 2; cnt++) {
2389                 *wptr = qla1280_get_nvram_word (ha, cnt);
2390                 chksum += (uint8_t) * wptr;
2391                 chksum += (uint8_t) (*wptr >> 8);
2392                 wptr++;
2393         }
2394         dprintk(1, "qla1280_isp_firmware: Completed Reading NVRAM\n");
2395
2396         dprintk(3, "qla1280_isp_firmware: NVRAM Magic ID= %c %c %c\n",
2397                 nv->id0, nv->id1, nv->id2);
2398
2399         /* Bad NVRAM data, load RISC code. */
2400         if (chksum || nv->id0 != 'I' || nv->id1 != 'S' ||
2401             nv->id2 != 'P' || nv->id3 != ' ' || nv->version < 1) {
2402                 printk(KERN_INFO "qla1280_isp_firmware: Bad checksum or magic "
2403                        "number or version in NVRAM.\n");
2404                 ha->flags.disable_risc_code_load = FALSE;
2405         } else
2406                 ha->flags.disable_risc_code_load =
2407                         nv->cntr_flags_1.disable_loading_risc_code;
2408
2409         if (ha->flags.disable_risc_code_load) {
2410                 dprintk(3,
2411                         "qla1280_isp_firmware: Telling RISC to verify checksum "
2412                         "of loaded BIOS code.\n");
2413
2414                 /* Verify checksum of loaded RISC code. */
2415                 mb[0] = MBC_VERIFY_CHECKSUM;
2416                 /* mb[1] = ql12_risc_code_addr01; */
2417                 mb[1] = *ql1280_board_tbl[ha->devnum].fwstart;
2418
2419                 if (!
2420                     (status =
2421                      qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]))) {
2422                         /* Start firmware execution. */
2423                         dprintk(3, "qla1280_isp_firmware: Startng F/W "
2424                                 "execution.\n");
2425
2426                         mb[0] = MBC_EXECUTE_FIRMWARE;
2427                         /* mb[1] = ql12_risc_code_addr01; */
2428                         mb[1] = *ql1280_board_tbl[ha->devnum].fwstart;
2429                         qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
2430                 } else
2431                         printk(KERN_INFO "qla1280: RISC checksum failed.\n");
2432         } else {
2433                 dprintk(1, "qla1280: NVRAM configured to load RISC load.\n");
2434                 status = 1;
2435         }
2436
2437         if (status)
2438                 dprintk(2, "qla1280_isp_firmware: **** Load RISC code ****\n");
2439
2440         LEAVE("qla1280_isp_firmware");
2441         return status;
2442 }
2443
2444 /*
2445  * PCI configuration
2446  *      Setup device PCI configuration registers.
2447  *
2448  * Input:
2449  *      ha = adapter block pointer.
2450  *
2451  * Returns:
2452  *      0 = success.
2453  */
2454 static int
2455 qla1280_pci_config(struct scsi_qla_host *ha)
2456 {
2457 #if MEMORY_MAPPED_IO
2458         uint32_t page_offset, base;
2459         uint32_t mmapbase;
2460 #endif
2461         uint16_t buf_wd;
2462         int status = 1;
2463
2464         ENTER("qla1280_pci_config");
2465
2466         pci_set_master(ha->pdev);
2467         /*
2468          * Set Bus Master Enable, Memory Address Space Enable and
2469          * reset any error bits, in the command register.
2470          */
2471         pci_read_config_word (ha->pdev, PCI_COMMAND, &buf_wd);
2472 #if MEMORY_MAPPED_IO
2473         dprintk(1, "qla1280: MEMORY MAPPED IO is enabled.\n");
2474         buf_wd |= PCI_COMMAND_MEMORY + PCI_COMMAND_IO;
2475 #else
2476         buf_wd |= PCI_COMMAND_IO;
2477 #endif
2478         pci_write_config_word (ha->pdev, PCI_COMMAND, buf_wd);
2479         /*
2480          * Reset expansion ROM address decode enable.
2481          */
2482         pci_read_config_word (ha->pdev, PCI_ROM_ADDRESS, &buf_wd);
2483         buf_wd &= ~PCI_ROM_ADDRESS_ENABLE;
2484         pci_write_config_word (ha->pdev, PCI_ROM_ADDRESS, buf_wd);
2485
2486         ha->host->io_port = pci_resource_start(ha->pdev, 0);
2487         ha->host->io_port &= PCI_BASE_ADDRESS_IO_MASK;
2488         ha->iobase = (struct device_reg *) ha->host->io_port;
2489
2490 #if MEMORY_MAPPED_IO
2491         /*
2492          * Get memory mapped I/O address.
2493          */
2494         pci_read_config_dword (ha->pdev, PCI_BASE_ADDRESS_1, &mmapbase);
2495         mmapbase &= PCI_BASE_ADDRESS_MEM_MASK;
2496
2497         /*
2498          * Find proper memory chunk for memory map I/O reg.
2499          */
2500         base = mmapbase & PAGE_MASK;
2501         page_offset = mmapbase - base;
2502         /*
2503          * Get virtual address for I/O registers.
2504          */
2505         ha->mmpbase = ioremap(base, page_offset + 256);
2506         if (ha->mmpbase) {
2507                 ha->mmpbase += page_offset;
2508                 /* ha->iobase = ha->mmpbase; */
2509                 status = 0;
2510         }
2511 #else                           /* MEMORY_MAPPED_IO */
2512         status = 0;
2513 #endif                          /* MEMORY_MAPPED_IO */
2514
2515         LEAVE("qla1280_pci_config");
2516         return status;
2517 }
2518
2519 /*
2520  * Chip diagnostics
2521  *      Test chip for proper operation.
2522  *
2523  * Input:
2524  *      ha = adapter block pointer.
2525  *
2526  * Returns:
2527  *      0 = success.
2528  */
2529 static int
2530 qla1280_chip_diag(struct scsi_qla_host *ha)
2531 {
2532         uint16_t mb[MAILBOX_REGISTER_COUNT];
2533         struct device_reg *reg = ha->iobase;
2534         int status = 0;
2535         int cnt;
2536         uint16_t data;
2537
2538         dprintk(3, "qla1280_chip_diag: testing device at 0x%p \n", &reg->id_l);
2539
2540         /* Soft reset chip and wait for it to finish. */
2541         WRT_REG_WORD(&reg->ictrl, ISP_RESET);
2542         data = qla1280_debounce_register(&reg->ictrl);
2543         /*
2544          * This is *AWESOME*
2545          */
2546         for (cnt = 6000000; cnt && data & ISP_RESET; cnt--) {
2547                 udelay(5);
2548                 data = RD_REG_WORD(&reg->ictrl);
2549         }
2550         if (cnt) {
2551                 /* Reset register not cleared by chip reset. */
2552                 dprintk(3,
2553                         "qla1280_chip_diag: reset register cleared by chip reset\n");
2554
2555                 WRT_REG_WORD(&reg->cfg_1, 0);
2556
2557                 /* Reset RISC and disable BIOS which
2558                    allows RISC to execute out of RAM. */
2559                 WRT_REG_WORD(&reg->host_cmd, HC_RESET_RISC);
2560                 WRT_REG_WORD(&reg->host_cmd, HC_RELEASE_RISC);
2561                 WRT_REG_WORD(&reg->host_cmd, HC_DISABLE_BIOS);
2562                 data = qla1280_debounce_register(&reg->mailbox0);
2563                 /*
2564                  * I *LOVE* this code!
2565                  */
2566                 for (cnt = 6000000; cnt && data == MBS_BUSY; cnt--) {
2567                         udelay(5);
2568                         data = RD_REG_WORD(&reg->mailbox0);
2569                 }
2570
2571                 if (cnt) {
2572                         /* Check product ID of chip */
2573                         dprintk(3,
2574                                 "qla1280_chip_diag: Checking product ID of chip\n");
2575
2576                         if (RD_REG_WORD(&reg->mailbox1) != PROD_ID_1 ||
2577                             (RD_REG_WORD(&reg->mailbox2) != PROD_ID_2 &&
2578                              RD_REG_WORD(&reg->mailbox2) != PROD_ID_2a) ||
2579                             RD_REG_WORD(&reg->mailbox3) != PROD_ID_3 ||
2580                             RD_REG_WORD(&reg->mailbox4) != PROD_ID_4) {
2581                                 printk(KERN_INFO
2582                                        "qla1280: Wrong product ID = 0x%x,0x%x,0x%x,"
2583                                        "0x%x\n", RD_REG_WORD(&reg->mailbox1),
2584                                        RD_REG_WORD(&reg->mailbox2),
2585                                        RD_REG_WORD(&reg->mailbox3),
2586                                        RD_REG_WORD(&reg->mailbox4));
2587                                 status = 1;
2588                         } else {
2589                                 /*
2590                                  * Enable ints early!!!
2591                                  */
2592                                 qla1280_enable_intrs(ha);
2593                                 dprintk(1,
2594                                         "qla1280_chip_diag: Checking mailboxes of chip\n");
2595                                 /* Wrap Incoming Mailboxes Test. */
2596                                 mb[0] = MBC_MAILBOX_REGISTER_TEST;
2597                                 mb[1] = 0xAAAA;
2598                                 mb[2] = 0x5555;
2599                                 mb[3] = 0xAA55;
2600                                 mb[4] = 0x55AA;
2601                                 mb[5] = 0xA5A5;
2602                                 mb[6] = 0x5A5A;
2603                                 mb[7] = 0x2525;
2604                                 if (!(status = qla1280_mailbox_command(ha,
2605                                                                        BIT_7 |
2606                                                                        BIT_6 |
2607                                                                        BIT_5 |
2608                                                                        BIT_4 |
2609                                                                        BIT_3 |
2610                                                                        BIT_2 |
2611                                                                        BIT_1 |
2612                                                                        BIT_0,
2613                                                                        &mb
2614                                                                        [0]))) {
2615                                         if (mb[1] != 0xAAAA ||
2616                                             mb[2] != 0x5555 ||
2617                                             mb[3] != 0xAA55 ||
2618                                             mb[4] != 0x55AA ||
2619                                             mb[5] != 0xA5A5 ||
2620                                             mb[6] != 0x5A5A ||
2621                                             mb[7] != 0x2525)
2622                                                 status = 1;
2623                                         if (status == 1)
2624                                                 printk(KERN_INFO
2625                                                        "qla1280: Failed mailbox check\n");
2626                                 }
2627                         }
2628                 } else
2629                         status = 1;
2630         } else
2631                 status = 1;
2632
2633         if (status)
2634                 dprintk(2, "qla1280_chip_diag: **** FAILED ****\n");
2635         else
2636                 dprintk(3, "qla1280_chip_diag: exiting normally\n");
2637
2638         return status;
2639 }
2640
2641 /*
2642  * Setup chip
2643  *      Load and start RISC firmware.
2644  *
2645  * Input:
2646  *      ha = adapter block pointer.
2647  *
2648  * Returns:
2649  *      0 = success.
2650  */
2651 #define DUMP_IT_BACK 1          /* for debug of RISC loading */
2652 static int
2653 qla1280_setup_chip(struct scsi_qla_host *ha)
2654 {
2655         int status = 0;
2656         uint16_t risc_address;
2657         uint16_t *risc_code_address;
2658         int risc_code_size;
2659         uint16_t mb[MAILBOX_REGISTER_COUNT];
2660         uint16_t cnt;
2661         int num;
2662 #if DUMP_IT_BACK
2663         int i;
2664         uint8_t *sp;
2665         uint8_t *tbuf;
2666         dma_addr_t p_tbuf;
2667 #endif
2668
2669         ENTER("qla1280_setup_chip");
2670
2671         /* 3.13 */
2672 #if DUMP_IT_BACK
2673         /* get consistent memory allocated for setup_chip */
2674         tbuf = pci_alloc_consistent(ha->pdev, 8000, &p_tbuf);
2675 #endif
2676
2677         /* Load RISC code. */
2678         risc_address = *ql1280_board_tbl[ha->devnum].fwstart;
2679         risc_code_address = ql1280_board_tbl[ha->devnum].fwcode;
2680         risc_code_size = (int) *ql1280_board_tbl[ha->devnum].fwlen;
2681
2682         dprintk(1, "qla1280_setup_chip: DMA RISC code (%i) words\n",
2683                 risc_code_size);
2684
2685         num = 0;
2686         while (risc_code_size > 0 && !status) {
2687                 int warn __attribute__((unused)) = 0;
2688
2689                 cnt = 2000 >> 1;
2690
2691                 if (cnt > risc_code_size)
2692                         cnt = risc_code_size;
2693
2694                 dprintk(1, "qla1280_setup_chip:  loading risc @ =(0x%p),"
2695                         "%d,%d(0x%x)\n",
2696                         risc_code_address, cnt, num, risc_address);
2697                 for(i = 0; i < cnt; i++)
2698                         ((uint16_t *)ha->request_ring)[i] =
2699                                 cpu_to_le16(risc_code_address[i]);
2700
2701                 flush_cache_all();
2702
2703                 mb[0] = MBC_LOAD_RAM;
2704                 mb[1] = risc_address;
2705                 mb[4] = cnt;
2706                 mb[3] = qla1280_addr0_15(ha->request_dma);
2707                 mb[2] = qla1280_addr16_31(ha->request_dma);
2708                 mb[7] = qla1280_addr32_47(ha->request_dma);
2709                 mb[6] = qla1280_addr48_63(ha->request_dma);
2710                 dprintk(1, "qla1280_setup_chip: op=%d  0x%p = 0x%4x,0x%4x,"
2711                         "0x%4x,0x%4x\n",
2712                         mb[0], (void *)ha->request_dma, mb[6], mb[7], mb[2], mb[3]);
2713                 if ((status = qla1280_mailbox_command(ha, BIT_4 | BIT_3 |
2714                                                       BIT_2 | BIT_1 | BIT_0,
2715                                                       &mb[0]))) {
2716                         printk(KERN_ERR
2717                                "Failed to load partial segment of f/w\n");
2718                         break;
2719                 }
2720
2721 #if DUMP_IT_BACK
2722                 mb[0] = MBC_DUMP_RAM;
2723                 mb[1] = risc_address;
2724                 mb[4] = cnt;
2725                 mb[3] = qla1280_addr0_15(p_tbuf);
2726                 mb[2] = qla1280_addr16_31(p_tbuf);
2727                 mb[7] = qla1280_addr32_47(p_tbuf);
2728                 mb[6] = qla1280_addr48_63(p_tbuf);
2729
2730                 if ((status = qla1280_mailbox_command(ha,
2731                                                       BIT_4 | BIT_3 | BIT_2 |
2732                                                       BIT_1 | BIT_0,
2733                                                       &mb[0]))) {
2734                         printk(KERN_ERR
2735                                "Failed to dump partial segment of f/w\n");
2736                         break;
2737                 }
2738                 sp = (uint8_t *)ha->request_ring;
2739                 for (i = 0; i < (cnt << 1); i++) {
2740                         if (tbuf[i] != sp[i] &&warn++ < 10) {
2741                                 printk(KERN_ERR "qla1280_setup_chip: FW "
2742                                        "compare error @ byte(0x%x) loop#=%x\n",
2743                                        i, num);
2744                                 printk(KERN_ERR "setup_chip: FWbyte=%x  "
2745                                        "FWfromChip=%x\n", sp[i], tbuf[i]);
2746                                 /*break; */
2747                         }
2748                 }
2749 #endif
2750                 risc_address += cnt;
2751                 risc_code_size = risc_code_size - cnt;
2752                 risc_code_address = risc_code_address + cnt;
2753                 num++;
2754         }
2755
2756         /* Verify checksum of loaded RISC code. */
2757         if (!status) {
2758                 dprintk(1, "qla1280_setup_chip: Verifying checksum of "
2759                         "loaded RISC code.\n");
2760                 mb[0] = MBC_VERIFY_CHECKSUM;
2761                 /* mb[1] = ql12_risc_code_addr01; */
2762                 mb[1] = *ql1280_board_tbl[ha->devnum].fwstart;
2763
2764                 if (!(status =
2765                       qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]))) {
2766                         /* Start firmware execution. */
2767                         dprintk(1,
2768                                 "qla1280_setup_chip: start firmware running.\n");
2769                         mb[0] = MBC_EXECUTE_FIRMWARE;
2770                         mb[1] = *ql1280_board_tbl[ha->devnum].fwstart;
2771                         qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
2772                 } else
2773                         printk(KERN_ERR
2774                                "qla1280_setup_chip: Failed checksum.\n");
2775         }
2776
2777         /* 3.13 */
2778 #if DUMP_IT_BACK
2779         /* free consistent memory allocated for setup_chip */
2780         pci_free_consistent(ha->pdev, 8000, tbuf, p_tbuf);
2781 #endif
2782
2783         if (status)
2784                 dprintk(2, "qla1280_setup_chip: **** FAILED ****\n");
2785
2786         LEAVE("qla1280_setup_chip");
2787         return status;
2788 }
2789
2790 /*
2791  * Initialize rings
2792  *
2793  * Input:
2794  *      ha                = adapter block pointer.
2795  *      ha->request_ring  = request ring virtual address
2796  *      ha->response_ring = response ring virtual address
2797  *      ha->request_dma   = request ring physical address
2798  *      ha->response_dma  = response ring physical address
2799  *
2800  * Returns:
2801  *      0 = success.
2802  */
2803 static int
2804 qla1280_init_rings(struct scsi_qla_host *ha)
2805 {
2806         uint16_t mb[MAILBOX_REGISTER_COUNT];
2807         int status = 0;
2808         int cnt;
2809
2810         ENTER("qla1280_init_rings");
2811
2812         /* Clear outstanding commands array. */
2813         for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
2814                 ha->outstanding_cmds[cnt] = 0;
2815
2816         /* Initialize request queue. */
2817         ha->request_ring_ptr = ha->request_ring;
2818         ha->req_ring_index = 0;
2819         ha->req_q_cnt = REQUEST_ENTRY_CNT;
2820         /* mb[0] = MBC_INIT_REQUEST_QUEUE; */
2821         mb[0] = MBC_INIT_REQUEST_QUEUE_A64;
2822         mb[1] = REQUEST_ENTRY_CNT;
2823         mb[3] = ha->request_dma & 0xffff;
2824         mb[2] = (ha->request_dma >> 16) & 0xffff;
2825         mb[4] = 0;
2826         mb[7] = pci_dma_hi32(ha->request_dma) & 0xffff;
2827         mb[6] = pci_dma_hi32(ha->request_dma) >> 16;
2828         if (!(status = qla1280_mailbox_command(ha, BIT_7 | BIT_6 | BIT_4 |
2829                                                BIT_3 | BIT_2 | BIT_1 | BIT_0,
2830                                                &mb[0]))) {
2831                 /* Initialize response queue. */
2832                 ha->response_ring_ptr = ha->response_ring;
2833                 ha->rsp_ring_index = 0;
2834                 /* mb[0] = MBC_INIT_RESPONSE_QUEUE; */
2835                 mb[0] = MBC_INIT_RESPONSE_QUEUE_A64;
2836                 mb[1] = RESPONSE_ENTRY_CNT;
2837                 mb[3] = ha->response_dma & 0xffff;
2838                 mb[2] = (ha->response_dma >> 16) & 0xffff;
2839                 mb[5] = 0;
2840                 mb[7] = pci_dma_hi32(ha->response_dma) & 0xffff;
2841                 mb[6] = pci_dma_hi32(ha->response_dma) >> 16;
2842                 status = qla1280_mailbox_command(ha, BIT_7 | BIT_6 | BIT_5 |
2843                                                  BIT_3 | BIT_2 | BIT_1 | BIT_0,
2844                                                  &mb[0]);
2845         }
2846
2847         if (status)
2848                 dprintk(2, "qla1280_init_rings: **** FAILED ****\n");
2849
2850         LEAVE("qla1280_init_rings");
2851         return status;
2852 }
2853
2854 /*
2855  * NVRAM configuration.
2856  *
2857  * Input:
2858  *      ha                = adapter block pointer.
2859  *      ha->request_ring  = request ring virtual address
2860  *
2861  * Output:
2862  *      host adapters parameters in host adapter block
2863  *
2864  * Returns:
2865  *      0 = success.
2866  */
2867 static int
2868 qla1280_nvram_config(struct scsi_qla_host *ha)
2869 {
2870         struct device_reg *reg = ha->iobase;
2871         nvram_t *nv = (nvram_t *)ha->response_ring;
2872         int status = 0;
2873         int cnt;
2874         int bus, target, lun;
2875         uint16_t *wptr;
2876         uint16_t mb[MAILBOX_REGISTER_COUNT];
2877         uint8_t chksum;
2878         int nvsize;
2879
2880 #if DEBUG_PRINT_NVRAM
2881         int saved_print_status = ql_debug_print;
2882 #endif
2883         ENTER("qla1280_nvram_config");
2884
2885         /* Verify valid NVRAM checksum. */
2886 #if USE_NVRAM_DEFAULTS
2887         chksum = 1;
2888 #else
2889         wptr = (uint16_t *) ha->response_ring;
2890         chksum = 0;
2891         if (ha->device_id == PCI_DEVICE_ID_QLOGIC_ISP12160 ||
2892             ha->device_id == PCI_DEVICE_ID_QLOGIC_ISP10160)
2893                 nvsize = sizeof(nvram160_t) / 2;
2894         else
2895                 nvsize = sizeof(nvram_t) / 2;
2896         for (cnt = 0; cnt < nvsize; cnt++) {
2897                 *wptr = qla1280_get_nvram_word(ha, cnt);
2898                 chksum += (uint8_t) * wptr;
2899                 chksum += (uint8_t) (*wptr >> 8);
2900                 wptr++;
2901         }
2902 #endif
2903
2904         /* Bad NVRAM data, set defaults parameters. */
2905         if (chksum || nv->id0 != 'I' || nv->id1 != 'S' ||
2906             nv->id2 != 'P' || nv->id3 != ' ' || nv->version < 1) {
2907 #if USE_NVRAM_DEFAULTS
2908                 dprintk(1, "Using defaults for NVRAM\n");
2909 #else
2910                 dprintk(1, "Using defaults for NVRAM: \n");
2911                 dprintk(1, "checksum=0x%x, Id=%c, version=0x%x\n",
2912                         chksum, nv->id[0], nv->version);
2913                 memset(ha->response_ring, 0, sizeof(nvram_t));
2914 #endif
2915
2916                 /* nv->cntr_flags_1.disable_loading_risc_code = 1; */
2917                 nv->firmware_feature.w = BIT_0;
2918                 nv->termination.f.scsi_bus_0_control = 3;
2919                 nv->termination.f.scsi_bus_1_control = 3;
2920                 nv->termination.f.auto_term_support = 1;
2921
2922                 for (bus = 0; bus < MAX_BUSES; bus++) {
2923                         nv->bus[bus].config_1.initiator_id = 7;
2924                         nv->bus[bus].bus_reset_delay = 5;
2925                         nv->bus[bus].config_2.async_data_setup_time = 9;
2926                         nv->bus[bus].config_2.req_ack_active_negation = 1;
2927                         nv->bus[bus].config_2.data_line_active_negation = 1;
2928                         nv->bus[bus].selection_timeout = 250;
2929                         nv->bus[bus].max_queue_depth = 256;
2930
2931                         for (target = 0; target < MAX_TARGETS; target++) {
2932                                 nv->bus[bus].target[target].parameter.f.
2933                                         auto_request_sense = 1;
2934                                 nv->bus[bus].target[target].parameter.f.
2935                                         disconnect_allowed = 1;
2936                                 nv->bus[bus].target[target].parameter.f.
2937                                         tag_queuing = 1;
2938                                 nv->bus[bus].target[target].flags.
2939                                         device_enable = 1;
2940                         }
2941                 }
2942
2943 #if USE_NVRAM_DEFAULTS
2944                 status = 0;
2945 #else
2946                 status = 1;
2947 #endif
2948         } else {
2949                 /* Always force AUTO sense for LINUX SCSI */
2950                 for (bus = 0; bus < MAX_BUSES; bus++)
2951                         for (target = 0; target < MAX_TARGETS; target++) {
2952                                 nv->bus[bus].target[target].parameter.f.
2953                                         auto_request_sense = 1;
2954                         }
2955         }
2956 #if DEBUG_PRINT_NVRAM
2957         ql_debug_print = 1;
2958 #endif
2959         dprintk(1, "qla1280 : initiator scsi id bus[0]=%d\n",
2960                 nv->bus[0].config_1.initiator_id);
2961         dprintk(1, "qla1280 : initiator scsi id bus[1]=%d\n",
2962                 nv->bus[1].config_1.initiator_id);
2963
2964         dprintk(1, "qla1280 : bus reset delay[0]=%d\n",
2965                 nv->bus[0].bus_reset_delay);
2966         dprintk(1, "qla1280 : bus reset delay[1]=%d\n",
2967                 nv->bus[1].bus_reset_delay);
2968
2969         dprintk(1, "qla1280 : retry count[0]=%d\n", nv->bus[0].retry_count);
2970         dprintk(1, "qla1280 : retry delay[0]=%d\n", nv->bus[0].retry_delay);
2971         dprintk(1, "qla1280 : retry count[1]=%d\n", nv->bus[1].retry_count);
2972         dprintk(1, "qla1280 : retry delay[1]=%d\n", nv->bus[1].retry_delay);
2973
2974         dprintk(1, "qla1280 : async data setup time[0]=%d\n",
2975                 nv->bus[0].config_2.async_data_setup_time);
2976         dprintk(1, "qla1280 : async data setup time[1]=%d\n",
2977                 nv->bus[1].config_2.async_data_setup_time);
2978
2979         dprintk(1, "qla1280 : req/ack active negation[0]=%d\n",
2980                 nv->bus[0].config_2.req_ack_active_negation);
2981         dprintk(1, "qla1280 : req/ack active negation[1]=%d\n",
2982                 nv->bus[1].config_2.req_ack_active_negation);
2983
2984         dprintk(1, "qla1280 : data line active negation[0]=%d\n",
2985                 nv->bus[0].config_2.data_line_active_negation);
2986         dprintk(1, "qla1280 : data line active negation[1]=%d\n",
2987                 nv->bus[1].config_2.data_line_active_negation);
2988
2989         dprintk(1, "qla1280 : disable loading risc code=%d\n",
2990                 nv->cntr_flags_1.disable_loading_risc_code);
2991
2992         dprintk(1, "qla1280 : enable 64bit addressing=%d\n",
2993                 nv->cntr_flags_1.enable_64bit_addressing);
2994
2995         dprintk(1, "qla1280 : selection timeout limit[0]=%d\n",
2996                 nv->bus[0].selection_timeout);
2997         dprintk(1, "qla1280 : selection timeout limit[1]=%d\n",
2998                 nv->bus[1].selection_timeout);
2999
3000         dprintk(1, "qla1280 : max queue depth[0]=%d\n",
3001                 nv->bus[0].max_queue_depth);
3002         dprintk(1, "qla1280 : max queue depth[1]=%d\n",
3003                 nv->bus[1].max_queue_depth);
3004
3005         /* Disable RISC load of firmware. */
3006         ha->flags.disable_risc_code_load =
3007                 nv->cntr_flags_1.disable_loading_risc_code;
3008
3009 #ifdef QLA_64BIT_PTR
3010         /* Enable 64bit addressing for OS/System combination supporting it   */
3011         /* actual NVRAM bit is: nv->cntr_flags_1.enable_64bit_addressing     */
3012         /* but we will ignore it and use BITS_PER_LONG macro to setup for    */
3013         /* 64 or 32 bit access of host memory in all x86/ia-64/Alpha systems */
3014         ha->flags.enable_64bit_addressing = 1;
3015 #else
3016         ha->flags.enable_64bit_addressing = 0;
3017 #endif
3018
3019 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,18)
3020         if (ha->flags.enable_64bit_addressing) {
3021                 printk(KERN_INFO "scsi(%li): 64 Bit PCI Addressing Enabled\n",
3022                        ha->host_no);
3023
3024                 pci_set_dma_mask(ha->pdev, (dma_addr_t) ~ 0ULL);
3025         }
3026 #endif
3027
3028         /* Set ISP hardware DMA burst */
3029         mb[0] = nv->isp_config.c;
3030         WRT_REG_WORD(&reg->cfg_1, mb[0]);
3031
3032         /* Set SCSI termination. */
3033         WRT_REG_WORD(&reg->gpio_enable, (BIT_3 + BIT_2 + BIT_1 + BIT_0));
3034         mb[0] = nv->termination.c & (BIT_3 + BIT_2 + BIT_1 + BIT_0);
3035         WRT_REG_WORD(&reg->gpio_data, mb[0]);
3036
3037         /* ISP parameter word. */
3038         mb[0] = MBC_SET_SYSTEM_PARAMETER;
3039         mb[1] = nv->isp_parameter;
3040         status |= qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
3041
3042         /* Firmware feature word. */
3043         mb[0] = MBC_SET_FIRMWARE_FEATURES;
3044         mb[1] = nv->firmware_feature.w & (BIT_1 | BIT_0);
3045         status |= qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
3046
3047         /* Retry count and delay. */
3048         mb[0] = MBC_SET_RETRY_COUNT;
3049         mb[1] = nv->bus[0].retry_count;
3050         mb[2] = nv->bus[0].retry_delay;
3051         mb[6] = nv->bus[1].retry_count;
3052         mb[7] = nv->bus[1].retry_delay;
3053         status |=
3054                 qla1280_mailbox_command(ha, BIT_7 | BIT_6 | BIT_2 | BIT_1 | BIT_0,
3055                                         &mb[0]);
3056
3057         /* ASYNC data setup time. */
3058         mb[0] = MBC_SET_ASYNC_DATA_SETUP;
3059         mb[1] = nv->bus[0].config_2.async_data_setup_time;
3060         mb[2] = nv->bus[1].config_2.async_data_setup_time;
3061         status |= qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
3062
3063         /* Active negation states. */
3064         mb[0] = MBC_SET_ACTIVE_NEGATION;
3065         mb[1] = 0;
3066         if (nv->bus[0].config_2.req_ack_active_negation)
3067                 mb[1] |= BIT_5;
3068         if (nv->bus[0].config_2.data_line_active_negation)
3069                 mb[1] |= BIT_4;
3070         mb[2] = 0;
3071         if (nv->bus[1].config_2.req_ack_active_negation)
3072                 mb[2] |= BIT_5;
3073         if (nv->bus[1].config_2.data_line_active_negation)
3074                 mb[2] |= BIT_4;
3075         status |= qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
3076
3077         /* Selection timeout. */
3078         mb[0] = MBC_SET_SELECTION_TIMEOUT;
3079         mb[1] = nv->bus[0].selection_timeout;
3080         mb[2] = nv->bus[1].selection_timeout;
3081         status |= qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
3082
3083         for (bus = 0; bus < ha->ports; bus++) {
3084                 /* SCSI Reset Disable. */
3085                 ha->bus_settings[bus].disable_scsi_reset =
3086                         nv->bus[bus].config_1.scsi_reset_disable;
3087
3088                 /* Initiator ID. */
3089                 ha->bus_settings[bus].id = nv->bus[bus].config_1.initiator_id;
3090                 mb[0] = MBC_SET_INITIATOR_ID;
3091                 mb[1] = bus ? ha->bus_settings[bus].id | BIT_7 :
3092                         ha->bus_settings[bus].id;
3093                 status |= qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
3094
3095                 /* Reset Delay. */
3096                 ha->bus_settings[bus].bus_reset_delay =
3097                         nv->bus[bus].bus_reset_delay;
3098
3099                 /* Command queue depth per device. */
3100                 ha->bus_settings[bus].hiwat = nv->bus[bus].max_queue_depth - 1;
3101
3102                 /* Set target parameters. */
3103                 for (target = 0; target < MAX_TARGETS; target++) {
3104                         uint8_t mr = BIT_3 | BIT_2 | BIT_1 | BIT_0;
3105
3106                         /* Set Target Parameters. */
3107                         mb[0] = MBC_SET_TARGET_PARAMETERS;
3108                         mb[1] = (uint16_t) (bus ? target | BIT_7 : target);
3109                         mb[1] <<= 8;
3110
3111                         mb[2] = nv->bus[bus].target[target].parameter.c << 8;
3112                         mb[2] |= TP_AUTO_REQUEST_SENSE;
3113                         mb[2] &= ~TP_STOP_QUEUE;
3114
3115                         mb[3] =
3116                                 nv->bus[bus].target[target].flags.sync_offset << 8;
3117                         mb[3] |= nv->bus[bus].target[target].sync_period;
3118
3119                         if (ha->device_id == PCI_DEVICE_ID_QLOGIC_ISP12160 ||
3120                             ha->device_id == PCI_DEVICE_ID_QLOGIC_ISP10160) {
3121                                 nvram160_t *nv2 = (nvram160_t *) nv;
3122                                 mb[2] |=
3123                                         nv2->bus[bus].target[target].flags2.
3124                                         enable_ppr << 5;
3125
3126                                 mb[6] =
3127                                         nv2->bus[bus].target[target].flags2.
3128                                         ppr_options << 8;
3129                                 mb[6] |=
3130                                         nv2->bus[bus].target[target].flags2.
3131                                         ppr_bus_width;
3132                                 mr |= BIT_6;
3133                         }
3134                         status = qla1280_mailbox_command(ha, mr, &mb[0]);
3135
3136                         /* Save Tag queuing enable flag. */
3137                         mb[0] = BIT_0 << target;
3138                         if (nv->bus[bus].target[target].parameter.f.tag_queuing)
3139                                 ha->bus_settings[bus].qtag_enables |= mb[0];
3140
3141                         /* Save Device enable flag. */
3142                         if (nv->bus[bus].target[target].flags.device_enable)
3143                                 ha->bus_settings[bus].device_enables |= mb[0];
3144
3145                         /* Save LUN disable flag. */
3146                         if (nv->bus[bus].target[target].flags.lun_disable)
3147                                 ha->bus_settings[bus].lun_disables |= mb[0];
3148
3149                         /* Set Device Queue Parameters. */
3150                         for (lun = 0; lun < MAX_LUNS; lun++) {
3151                                 mb[0] = MBC_SET_DEVICE_QUEUE;
3152                                 mb[1] = (uint16_t)(bus ? target | BIT_7 : target);
3153                                 mb[1] = mb[1] << 8 | lun;
3154                                 mb[2] = nv->bus[bus].max_queue_depth;
3155                                 mb[3] = nv->bus[bus].target[target].execution_throttle;
3156                                 status |=
3157                                         qla1280_mailbox_command(ha,
3158                                                                 BIT_3 | BIT_2 |
3159                                                                 BIT_1 | BIT_0,
3160                                                                 &mb[0]);
3161                         }
3162                 }
3163         }
3164
3165 #if DEBUG_PRINT_NVRAM
3166         ql_debug_print = saved_print_status;
3167 #endif
3168
3169         if (status)
3170                 dprintk(2, "qla1280_nvram_config: **** FAILED ****\n");
3171
3172         LEAVE("qla1280_nvram_config");
3173         return status;
3174 }
3175
3176 /*
3177  * Get NVRAM data word
3178  *      Calculates word position in NVRAM and calls request routine to
3179  *      get the word from NVRAM.
3180  *
3181  * Input:
3182  *      ha      = adapter block pointer.
3183  *      address = NVRAM word address.
3184  *
3185  * Returns:
3186  *      data word.
3187  */
3188 static uint16_t
3189 qla1280_get_nvram_word(struct scsi_qla_host *ha, uint32_t address)
3190 {
3191         uint32_t nv_cmd;
3192         uint16_t data;
3193
3194 #ifdef QL_DEBUG_ROUTINES
3195         int saved_print_status = ql_debug_print;
3196 #endif
3197
3198         nv_cmd = address << 16;
3199         nv_cmd |= NV_READ_OP;
3200
3201 #ifdef QL_DEBUG_ROUTINES
3202         ql_debug_print = FALSE;
3203 #endif
3204         data = qla1280_nvram_request(ha, nv_cmd);
3205 #ifdef QL_DEBUG_ROUTINES
3206         ql_debug_print = saved_print_status;
3207 #endif
3208
3209         dprintk(4,
3210                 "qla1280_get_nvram_word: exiting normally NVRAM data = 0x%x",
3211                 data);
3212
3213         return data;
3214 }
3215
3216 /*
3217  * NVRAM request
3218  *      Sends read command to NVRAM and gets data from NVRAM.
3219  *
3220  * Input:
3221  *      ha     = adapter block pointer.
3222  *      nv_cmd = Bit 26     = start bit
3223  *               Bit 25, 24 = opcode
3224  *               Bit 23-16  = address
3225  *               Bit 15-0   = write data
3226  *
3227  * Returns:
3228  *      data word.
3229  */
3230 static uint16_t
3231 qla1280_nvram_request(struct scsi_qla_host *ha, uint32_t nv_cmd)
3232 {
3233         struct device_reg *reg = ha->iobase;
3234         int cnt;
3235         uint16_t data = 0;
3236         uint16_t reg_data;
3237
3238         /* Send command to NVRAM. */
3239
3240         nv_cmd <<= 5;
3241         for (cnt = 0; cnt < 11; cnt++) {
3242                 if (nv_cmd & BIT_31)
3243                         qla1280_nv_write(ha, NV_DATA_OUT);
3244                 else
3245                         qla1280_nv_write(ha, 0);
3246                 nv_cmd <<= 1;
3247         }
3248
3249         /* Read data from NVRAM. */
3250
3251         for (cnt = 0; cnt < 16; cnt++) {
3252                 WRT_REG_WORD(&reg->nvram, (NV_SELECT | NV_CLOCK));
3253                 NVRAM_DELAY();
3254                 data <<= 1;
3255                 reg_data = RD_REG_WORD(&reg->nvram);
3256                 if (reg_data & NV_DATA_IN)
3257                         data |= BIT_0;
3258                 WRT_REG_WORD(&reg->nvram, NV_SELECT);
3259                 NVRAM_DELAY();
3260         }
3261
3262         /* Deselect chip. */
3263
3264         WRT_REG_WORD(&reg->nvram, NV_DESELECT);
3265         NVRAM_DELAY();
3266
3267         return data;
3268 }
3269
3270 static void
3271 qla1280_nv_write(struct scsi_qla_host *ha, uint16_t data)
3272 {
3273         struct device_reg *reg = ha->iobase;
3274
3275         WRT_REG_WORD(&reg->nvram, data | NV_SELECT);
3276         NVRAM_DELAY();
3277         WRT_REG_WORD(&reg->nvram, data | NV_SELECT | NV_CLOCK);
3278         NVRAM_DELAY();
3279         WRT_REG_WORD(&reg->nvram, data | NV_SELECT);
3280         NVRAM_DELAY();
3281 }
3282
3283 /*
3284  * Mailbox Command
3285  *      Issue mailbox command and waits for completion.
3286  *
3287  * Input:
3288  *      ha = adapter block pointer.
3289  *      mr = mailbox registers to load.
3290  *      mb = data pointer for mailbox registers.
3291  *
3292  * Output:
3293  *      mb[MAILBOX_REGISTER_COUNT] = returned mailbox data.
3294  *
3295  * Returns:
3296  *      0 = success
3297  */
3298 static int
3299 qla1280_mailbox_command(struct scsi_qla_host *ha, uint8_t mr, uint16_t * mb)
3300 {
3301         struct device_reg *reg = ha->iobase;
3302 #if 0
3303         srb_t *done_q_first = 0;
3304         srb_t *done_q_last = 0;
3305 #endif
3306         int status = 0;
3307         int cnt;
3308         uint16_t *optr, *iptr;
3309         uint16_t data;
3310
3311         ENTER("qla1280_mailbox_command");
3312
3313         ha->flags.mbox_busy = TRUE;
3314
3315         if (!ha->flags.ints_enabled)
3316                 printk(KERN_DEBUG
3317                        "Running qla1280_mailbox_command() with interrupts "
3318                        "disabled!\n");
3319         /*
3320          * We really should start out by verifying that the mailbox is available
3321          * before starting sending the command data
3322          */
3323         /* Load mailbox registers. */
3324         optr = (uint16_t *) &reg->mailbox0;
3325         iptr = mb;
3326         for (cnt = 0; cnt < MAILBOX_REGISTER_COUNT; cnt++) {
3327                 if (mr & BIT_0) {
3328                         WRT_REG_WORD(optr, (*iptr));
3329                 }
3330
3331                 mr >>= 1;
3332                 optr++;
3333                 iptr++;
3334         }
3335
3336         /* Issue set host interrupt command. */
3337         ha->flags.mbox_int = FALSE;
3338         ha->flags.mbox_busy = FALSE;
3339         WRT_REG_WORD(&reg->host_cmd, HC_SET_HOST_INT);
3340         data = qla1280_debounce_register(&reg->istatus);
3341
3342         /*
3343          * This is insane - instead of looping to wait for the interrupt 
3344          * to appear and run the handler (this is insane!!), use a waitqueue
3345          * and go to sleep.
3346          *
3347          * We are never called here from interrupt context anyway!    /Jes
3348          */
3349         /* Wait for 30 seconds for command to finish. */
3350         for (cnt = 30000000; cnt > 0 && !ha->flags.mbox_int; cnt--) {
3351                 /* Check for pending interrupts. */
3352 #if 0
3353                 if (data & RISC_INT) {
3354                         qla1280_isr(ha, &done_q_first, &done_q_last);
3355                 } else
3356 #endif
3357                         udelay(1);
3358
3359                 data = RD_REG_WORD(&reg->istatus);
3360         }
3361
3362         /* Check for mailbox command timeout. */
3363         if (!cnt) {
3364                 printk(KERN_WARNING
3365                        "qla1280_mailbox_command: **** Command Timeout, "
3366                        "mailbox0 = 0x%x****\n", mb[0]);
3367
3368                 ha->flags.isp_abort_needed = TRUE;
3369                 status = 1;
3370         } else if (ha->mailbox_out[0] != MBS_CMD_CMP)
3371                 status = 1;
3372
3373         /* Load return mailbox registers. */
3374         optr = mb;
3375         iptr = (uint16_t *) &ha->mailbox_out[0];
3376         mr = MAILBOX_REGISTER_COUNT;
3377         memcpy(optr, iptr, MAILBOX_REGISTER_COUNT * sizeof(uint16_t));
3378
3379 #if 0
3380         /* Go check for any response interrupts pending. */
3381         qla1280_isr(ha, &done_q_first, &done_q_last);
3382 #endif
3383
3384         if (ha->flags.isp_abort_needed)
3385                 qla1280_abort_isp(ha);
3386
3387         if (ha->flags.reset_marker)
3388                 qla1280_rst_aen(ha);
3389
3390 #if 0
3391         if (done_q_first)
3392                 qla1280_done (ha, &done_q_first, &done_q_last);
3393 #endif
3394
3395         if (status)
3396                 dprintk(2,
3397                         "qla1280_mailbox_command: **** FAILED, mailbox0 = 0x%x "
3398                         "****n", mb[0]);
3399
3400         LEAVE("qla1280_mailbox_command");
3401         return status;
3402 }
3403
3404 /*
3405  * qla1280_poll
3406  *      Polls ISP for interrupts.
3407  *
3408  * Input:
3409  *      ha = adapter block pointer.
3410  */
3411 static void
3412 qla1280_poll(struct scsi_qla_host *ha)
3413 {
3414         struct device_reg *reg = ha->iobase;
3415         uint16_t data;
3416         srb_t *done_q_first = 0;
3417         srb_t *done_q_last = 0;
3418
3419         /* ENTER("qla1280_poll"); */
3420
3421         /* Check for pending interrupts. */
3422         data = RD_REG_WORD(&reg->istatus);
3423         if (data & RISC_INT)
3424                 qla1280_isr(ha, &done_q_first, &done_q_last);
3425
3426         if (!ha->flags.mbox_busy) {
3427                 if (ha->flags.isp_abort_needed)
3428                         qla1280_abort_isp(ha);
3429                 if (ha->flags.reset_marker)
3430                         qla1280_rst_aen(ha);
3431         }
3432
3433         if (done_q_first)
3434                 qla1280_done(ha, &done_q_first, &done_q_last);
3435
3436         /* LEAVE("qla1280_poll"); */
3437 }
3438
3439 /*
3440  * qla1280_bus_reset
3441  *      Issue SCSI bus reset.
3442  *
3443  * Input:
3444  *      ha  = adapter block pointer.
3445  *      bus = SCSI bus number.
3446  *
3447  * Returns:
3448  *      0 = success
3449  */
3450 static int
3451 qla1280_bus_reset(struct scsi_qla_host *ha, int bus)
3452 {
3453         uint16_t mb[MAILBOX_REGISTER_COUNT];
3454         int status;
3455
3456         dprintk(3, "qla1280_bus_reset: entered\n");
3457
3458         if (qla1280_verbose)
3459                 printk(KERN_INFO "scsi(%li): Resetting SCSI BUS (%i)\n",
3460                        ha->host_no, bus);
3461
3462         mb[0] = MBC_BUS_RESET;
3463         mb[1] = ha->bus_settings[bus].bus_reset_delay;
3464         mb[2] = (uint16_t) bus;
3465         status = qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
3466
3467         if (status) {
3468                 if (ha->bus_settings[bus].failed_reset_count > 2)       /* dg - 03/13/99 */
3469                         ha->bus_settings[bus].scsi_bus_dead = TRUE;
3470                 ha->bus_settings[bus].failed_reset_count++;
3471         } else {
3472                 /*
3473                  * Eeeeep! This is evil! /Jes
3474                  */
3475 #if 0
3476                 mdelay(4000);
3477 #else
3478                 schedule_timeout(4 * HZ);
3479 #endif
3480                 ha->bus_settings[bus].scsi_bus_dead = FALSE;    /* dg - 03/13/99 */
3481                 ha->bus_settings[bus].failed_reset_count = 0;
3482                 /* Issue marker command. */
3483                 qla1280_marker(ha, bus, 0, 0, MK_SYNC_ALL);
3484         }
3485
3486         if (status)
3487                 dprintk(2, "qla1280_bus_reset: **** FAILED ****\n");
3488         else
3489                 dprintk(3, "qla1280_bus_reset: exiting normally\n");
3490
3491         return status;
3492 }
3493
3494 /*
3495  * qla1280_device_reset
3496  *      Issue bus device reset message to the target.
3497  *
3498  * Input:
3499  *      ha      = adapter block pointer.
3500  *      bus     = SCSI BUS number.
3501  *      target  = SCSI ID.
3502  *
3503  * Returns:
3504  *      0 = success
3505  */
3506 static int
3507 qla1280_device_reset(struct scsi_qla_host *ha, int bus, int target)
3508 {
3509         uint16_t mb[MAILBOX_REGISTER_COUNT];
3510         int status;
3511
3512         ENTER("qla1280_device_reset");
3513
3514         mb[0] = MBC_ABORT_TARGET;
3515         mb[1] = (bus ? (target | BIT_7) : target) << 8;
3516         mb[2] = 1;
3517         status = qla1280_mailbox_command(ha, BIT_2 | BIT_1 | BIT_0, &mb[0]);
3518
3519         /* Issue marker command. */
3520         qla1280_marker(ha, bus, target, 0, MK_SYNC_ID);
3521
3522         if (status)
3523                 dprintk(2, "qla1280_device_reset: **** FAILED ****\n");
3524
3525         LEAVE("qla1280_device_reset");
3526         return status;
3527 }
3528
3529 /*
3530  * qla1280_abort_device
3531  *      Issue an abort message to the device
3532  *
3533  * Input:
3534  *      ha     = adapter block pointer.
3535  *      bus    = SCSI BUS.
3536  *      target = SCSI ID.
3537  *      lun    = SCSI LUN.
3538  *
3539  * Returns:
3540  *      0 = success
3541  */
3542 static int
3543 qla1280_abort_device(struct scsi_qla_host *ha, int bus, int target, int lun)
3544 {
3545         uint16_t mb[MAILBOX_REGISTER_COUNT];
3546         int status;
3547
3548         ENTER("qla1280_abort_device");
3549
3550         mb[0] = MBC_ABORT_DEVICE;
3551         mb[1] = (bus ? target | BIT_7 : target) << 8 | lun;
3552         status = qla1280_mailbox_command(ha, BIT_1 | BIT_0, &mb[0]);
3553
3554         /* Issue marker command. */
3555         qla1280_marker(ha, bus, target, lun, MK_SYNC_ID_LUN);
3556
3557         if (status)
3558                 dprintk(2, "qla1280_abort_device: **** FAILED ****\n");
3559
3560         LEAVE("qla1280_abort_device");
3561         return status;
3562 }
3563
3564 /*
3565  * qla1280_abort_command
3566  *      Abort command aborts a specified IOCB.
3567  *
3568  * Input:
3569  *      ha = adapter block pointer.
3570  *      sp = SB structure pointer.
3571  *
3572  * Returns:
3573  *      0 = success
3574  */
3575 static int
3576 qla1280_abort_command(struct scsi_qla_host *ha, srb_t * sp)
3577 {
3578         uint16_t mb[MAILBOX_REGISTER_COUNT];
3579         unsigned int bus, target, lun;
3580         uint32_t handle;
3581         int status;
3582
3583         ENTER("qla1280_abort_command");
3584
3585         /* Locate handle number. */
3586         for (handle = 0; handle < MAX_OUTSTANDING_COMMANDS; handle++)
3587                 if (ha->outstanding_cmds[handle] == sp)
3588                         break;
3589
3590         bus = SCSI_BUS_32(sp->cmd);
3591         target = SCSI_TCN_32(sp->cmd);
3592         lun = SCSI_LUN_32(sp->cmd);
3593
3594         mb[0] = MBC_ABORT_COMMAND;
3595         mb[1] = (bus ? target | BIT_7 : target) << 8 | lun;
3596         mb[2] = handle >> 16;
3597         mb[3] = handle & 0xffff;
3598         status =
3599                 qla1280_mailbox_command(ha, BIT_3 | BIT_2 | BIT_1 | BIT_0, &mb[0]);
3600
3601         if (status)
3602                 dprintk(2, "qla1280_abort_command: **** FAILED ****\n");
3603
3604         sp->flags |= SRB_ABORT_PENDING;
3605
3606         LEAVE("qla1280_abort_command");
3607         return status;
3608 }
3609
3610 /*
3611  * qla1280_reset_adapter
3612  *      Reset adapter.
3613  *
3614  * Input:
3615  *      ha = adapter block pointer.
3616  */
3617 static void
3618 qla1280_reset_adapter(struct scsi_qla_host *ha)
3619 {
3620         struct device_reg *reg = ha->iobase;
3621
3622         ENTER("qla1280_reset_adapter");
3623
3624         /* Disable ISP chip */
3625         ha->flags.online = FALSE;
3626         WRT_REG_WORD(&reg->ictrl, ISP_RESET);
3627         WRT_REG_WORD(&reg->host_cmd, HC_RESET_RISC);
3628         WRT_REG_WORD(&reg->host_cmd, HC_RELEASE_RISC);
3629         WRT_REG_WORD(&reg->host_cmd, HC_DISABLE_BIOS);
3630
3631         LEAVE("qla1280_reset_adapter");
3632 }
3633
3634 /*
3635  *  Issue marker command.
3636  *      Function issues marker IOCB.
3637  *
3638  * Input:
3639  *      ha   = adapter block pointer.
3640  *      bus  = SCSI BUS number
3641  *      id   = SCSI ID
3642  *      lun  = SCSI LUN
3643  *      type = marker modifier
3644  */
3645 static void
3646 qla1280_marker(struct scsi_qla_host *ha, int bus, int id, int lun, u8 type)
3647 {
3648         mrk_entry_t *pkt;
3649
3650         ENTER("qla1280_marker");
3651
3652         /* Get request packet. */
3653         if ((pkt = (mrk_entry_t *) qla1280_req_pkt(ha))) {
3654                 pkt->entry_type = MARKER_TYPE;
3655                 pkt->lun = (uint8_t) lun;
3656                 pkt->target = (uint8_t) (bus ? (id | BIT_7) : id);
3657                 pkt->modifier = type;
3658
3659                 /* Issue command to ISP */
3660                 qla1280_isp_cmd(ha);
3661         }
3662
3663         LEAVE("qla1280_marker");
3664 }
3665
3666 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,18)
3667 /*
3668  * qla1280_64bit_start_scsi
3669  *      The start SCSI is responsible for building request packets on
3670  *      request ring and modifying ISP input pointer.
3671  *
3672  * Input:
3673  *      ha = adapter block pointer.
3674  *      sp = SB structure pointer.
3675  *
3676  * Returns:
3677  *      0 = success, was able to issue command.
3678  */
3679 static int
3680 qla1280_64bit_start_scsi(struct scsi_qla_host *ha, srb_t * sp)
3681 {
3682         struct device_reg *reg = ha->iobase;
3683         Scsi_Cmnd *cmd = sp->cmd;
3684         cmd_a64_entry_t *pkt;
3685         struct scatterlist *sg = NULL;
3686         u32 *dword_ptr;
3687         dma_addr_t dma_handle;
3688         int status = 0;
3689         int cnt;
3690         int req_cnt;
3691         u16 seg_cnt;
3692
3693         ENTER("qla1280_64bit_start_scsi:");
3694
3695         dprintk(1, "64bit_start: cmd=%p sp=%p CDB=%x\n", cmd, sp,
3696                 cmd->cmnd[0]);
3697
3698         /* Calculate number of entries and segments required. */
3699         req_cnt = 1;
3700         if (cmd->use_sg) {      /* 3.13 64 bit */
3701                 sg = (struct scatterlist *) cmd->request_buffer;
3702                 seg_cnt = pci_map_sg (ha->pdev, sg, cmd->use_sg,
3703                                       scsi_to_pci_dma_dir(cmd->sc_data_direction));
3704
3705                 if (seg_cnt > 2) {
3706                         req_cnt += (seg_cnt - 2) / 5;
3707                         if ((seg_cnt - 2) % 5)
3708                                 req_cnt++;
3709                 }
3710         } else if (cmd->request_bufflen) {      /* If data transfer. */
3711                 seg_cnt = 1;
3712         } else {
3713                 seg_cnt = 0;
3714         }
3715
3716         if ((req_cnt + 2) >= ha->req_q_cnt) {
3717                 /* Calculate number of free request entries. */
3718                 cnt = RD_REG_WORD(&reg->mailbox4);
3719                 if (ha->req_ring_index < cnt)
3720                         ha->req_q_cnt = cnt - ha->req_ring_index;
3721                 else
3722                         ha->req_q_cnt =
3723                                 REQUEST_ENTRY_CNT - (ha->req_ring_index - cnt);
3724         }
3725
3726         /* If room for request in request ring. */
3727         if ((req_cnt + 2) < ha->req_q_cnt) {
3728                 /* Check for room in outstanding command list. */
3729                 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS &&
3730                              ha->outstanding_cmds[cnt] != 0; cnt++) ;
3731
3732                 if (cnt < MAX_OUTSTANDING_COMMANDS) {
3733                         ha->outstanding_cmds[cnt] = sp;
3734                         ha->req_q_cnt -= req_cnt;
3735                         CMD_HANDLE(sp->cmd) =
3736                                 (unsigned char *)(unsigned long)cnt;
3737
3738                         /*
3739                          * Build command packet.
3740                          */
3741                         pkt = (cmd_a64_entry_t *) ha->request_ring_ptr;
3742
3743                         pkt->entry_type = COMMAND_A64_TYPE;
3744                         pkt->entry_count = (uint8_t) req_cnt;
3745                         pkt->sys_define = (uint8_t) ha->req_ring_index;
3746                         pkt->handle = cpu_to_le32(cnt);
3747
3748                         /* Zero out remaining portion of packet. */
3749                         memset(((char *)pkt + 8), 0, (REQUEST_ENTRY_SIZE - 8));
3750
3751                         /* Set ISP command timeout. */
3752                         pkt->timeout = cpu_to_le16(30);
3753
3754                         /* Set device target ID and LUN */
3755                         pkt->lun = SCSI_LUN_32(cmd);
3756                         pkt->target = SCSI_BUS_32(cmd) ?
3757                                 (SCSI_TCN_32(cmd) | BIT_7) : SCSI_TCN_32(cmd);
3758
3759                         /* Enable simple tag queuing if device supports it. */
3760                         if (cmd->device->tagged_queue)
3761                                 pkt->control_flags |= cpu_to_le16(BIT_3);
3762
3763                         /* Load SCSI command packet. */
3764                         pkt->cdb_len = cpu_to_le16(CMD_CDBLEN(cmd));
3765                         memcpy(pkt->scsi_cdb, &(CMD_CDBP(cmd)), CMD_CDBLEN(cmd));
3766                         /* dprintk(1, "Build packet for command[0]=0x%x\n",pkt->scsi_cdb[0]); */
3767
3768                         /* Set transfer direction. */
3769                         sp->dir = qla1280_data_direction(cmd);
3770                         pkt->control_flags |= cpu_to_le16(sp->dir);
3771                         
3772                         /* Set total data segment count. */
3773                         pkt->dseg_count = cpu_to_le16(seg_cnt);
3774
3775                         /*
3776                          * Load data segments.
3777                          */
3778                         if (seg_cnt) {  /* If data transfer. */
3779                                 /* Setup packet address segment pointer. */
3780                                 dword_ptr = (u32 *)&pkt->dseg_0_address;
3781
3782                                 if (cmd->use_sg) {      /* If scatter gather */
3783                                         /* Load command entry data segments. */
3784                                         for (cnt = 0; cnt < 2 && seg_cnt;
3785                                              cnt++, seg_cnt--) {
3786                                                 /* 3.13 64 bit */
3787                                                 *dword_ptr++ =
3788                                                         cpu_to_le32(pci_dma_lo32(sg_dma_address(sg)));
3789                                                 *dword_ptr++ =
3790                                                         cpu_to_le32(pci_dma_hi32(sg_dma_address(sg)));
3791                                                 *dword_ptr++ =
3792                                                         cpu_to_le32(sg_dma_len(sg));
3793                                                 sg++;
3794                                                 dprintk(1,
3795                                                         "S/G Segment phys_addr=%x %x, len=0x%x\n",
3796                                                         cpu_to_le32(pci_dma_hi32(sg_dma_address(sg))),
3797                                                         cpu_to_le32(pci_dma_lo32(sg_dma_address(sg))),
3798                                                         cpu_to_le32(sg_dma_len(sg)));
3799                                         }
3800                                         dprintk(5,
3801                                                 "qla1280_64bit_start_scsi: Scatter/gather "
3802                                                 "command packet data - b %i, t %i, l %i \n",
3803                                                 SCSI_BUS_32(cmd),
3804                                                 SCSI_TCN_32(cmd),
3805                                                 SCSI_LUN_32(cmd));
3806                                         qla1280_dump_buffer(5, (char *)pkt,
3807                                                             REQUEST_ENTRY_SIZE);
3808
3809                                         /*
3810                                          * Build continuation packets.
3811                                          */
3812                                         dprintk(1,
3813                                                 "S/G Building Continuation...seg_cnt=0x%x "
3814                                                 "remains\n", seg_cnt);
3815
3816                                         while (seg_cnt > 0) {
3817                                                 /* Adjust ring index. */
3818                                                 ha->req_ring_index++;
3819                                                 if (ha->req_ring_index ==
3820                                                     REQUEST_ENTRY_CNT) {
3821                                                         ha->req_ring_index = 0;
3822                                                         ha->request_ring_ptr =
3823                                                                 ha->request_ring;
3824                                                 } else
3825                                                         ha->request_ring_ptr++;
3826
3827                                                 pkt = (cmd_a64_entry_t *)ha->request_ring_ptr;
3828
3829                                                 /* Zero out packet. */
3830                                                 memset(pkt, 0,
3831                                                        REQUEST_ENTRY_SIZE);
3832
3833                                                 /* Load packet defaults. */
3834                                                 ((cont_a64_entry_t *) pkt)->entry_type =
3835                                                         CONTINUE_A64_TYPE;
3836                                                 ((cont_a64_entry_t *) pkt)->entry_count = 1;
3837                                                 ((cont_a64_entry_t *) pkt)->sys_define =
3838                                                         (uint8_t)ha->req_ring_index;
3839                                                 /* Setup packet address segment pointer. */
3840                                                 dword_ptr =
3841                                                         (u32 *)&((cont_a64_entry_t *) pkt)->dseg_0_address;
3842
3843                                                 /* Load continuation entry data segments. */
3844                                                 for (cnt = 0;
3845                                                      cnt < 5 && seg_cnt;
3846                                                      cnt++, seg_cnt--) {
3847                                                         /* 3.13 64 bit */
3848                                                         *dword_ptr++ =
3849                                                                 cpu_to_le32(pci_dma_lo32(sg_dma_address(sg)));
3850                                                         *dword_ptr++ =
3851                                                                 cpu_to_le32(pci_dma_hi32(sg_dma_address(sg)));
3852                                                         *dword_ptr++ =
3853                                                                 cpu_to_le32(sg_dma_len(sg));
3854                                                         dprintk(1,
3855                                                                 "S/G Segment Cont. phys_addr=%x %x, len=0x%x\n",
3856                                                                 cpu_to_le32(pci_dma_hi32(sg_dma_address(sg))),
3857                                                                 cpu_to_le32(pci_dma_lo32(sg_dma_address(sg))),
3858                                                                 cpu_to_le32(sg_dma_len(sg)));
3859                                                         sg++;
3860                                                 }
3861                                                 dprintk(5,
3862                                                         "qla1280_64bit_start_scsi: continuation "
3863                                                         "packet data - b %i, t %i, l %i \n",
3864                                                         SCSI_BUS_32(cmd),
3865                                                         SCSI_TCN_32(cmd),
3866                                                         SCSI_LUN_32(cmd));
3867                                                 qla1280_dump_buffer(5,
3868                                                                     (char *)pkt,
3869                                                                     REQUEST_ENTRY_SIZE);
3870                                         }
3871                                 } else {        /* No scatter gather data transfer */
3872                                         /* 3.13 64 bit */
3873                                         dma_handle = pci_map_single(ha->pdev,
3874                                                                     cmd->request_buffer,
3875                                                                     cmd->request_bufflen,
3876                                                                     scsi_to_pci_dma_dir(cmd->sc_data_direction));
3877                                         /* save dma_handle for pci_unmap_single */
3878                                         sp->saved_dma_handle = dma_handle;
3879
3880                                         *dword_ptr++ =
3881                                                 cpu_to_le32(pci_dma_lo32(dma_handle));
3882                                         *dword_ptr++ =
3883                                                 cpu_to_le32(pci_dma_hi32(dma_handle));
3884                                         *dword_ptr =
3885                                                 (uint32_t)cmd->request_bufflen;
3886                                         /* dprintk(1,
3887                                            "No S/G map_single saved_dma_handle=%lx\n",dma_handle);
3888                                         */
3889                                         dprintk(5,
3890                                                 "qla1280_64bit_start_scsi: No scatter/gather "
3891                                                 "command packet data - b %i, t %i, l %i \n",
3892                                                 SCSI_BUS_32(cmd),
3893                                                 SCSI_TCN_32(cmd),
3894                                                 SCSI_LUN_32(cmd));
3895                                         qla1280_dump_buffer(5, (char *)pkt,
3896                                                             REQUEST_ENTRY_SIZE);
3897                                 }
3898                         } else {        /* No data transfer */
3899
3900                                 dword_ptr = (uint32_t *)(pkt + 1);
3901                                 *dword_ptr++ = 0;
3902                                 *dword_ptr++ = 0;
3903                                 *dword_ptr = 0;
3904                                 dprintk(5,
3905                                         "qla1280_64bit_start_scsi: No data, command "
3906                                         "packet data - b %i, t %i, l %i \n",
3907                                         SCSI_BUS_32(cmd), SCSI_TCN_32(cmd),
3908                                         SCSI_LUN_32(cmd));
3909                                 qla1280_dump_buffer(5, (char *)pkt,
3910                                                     REQUEST_ENTRY_SIZE);
3911                         }
3912                         /* Adjust ring index. */
3913                         ha->req_ring_index++;
3914                         if (ha->req_ring_index == REQUEST_ENTRY_CNT) {
3915                                 ha->req_ring_index = 0;
3916                                 ha->request_ring_ptr = ha->request_ring;
3917                         } else
3918                                 ha->request_ring_ptr++;
3919
3920                         /* Set chip new ring index. */
3921                         dprintk(1,
3922                                 "qla1280_64bit_start_scsi: Wakeup RISC for pending command\n");
3923                         ha->qthreads--;
3924                         sp->flags |= SRB_SENT;
3925                         ha->actthreads++;
3926                         WRT_REG_WORD(&reg->mailbox4, ha->req_ring_index);
3927                 } else {
3928                         status = 1;
3929                         dprintk(2, "qla1280_64bit_start_scsi: NO ROOM IN "
3930                                 "OUTSTANDING ARRAY, req_q_cnt=0x%x",
3931                                 ha->req_q_cnt);
3932                 }
3933         } else {
3934                 status = 1;
3935                 dprintk(2,
3936                         "qla1280_64bit_start_scsi: in-ptr=0x%x  req_q_cnt=0x%x"
3937                         "req_cnt=0x%x", ha->req_ring_index, ha->req_q_cnt,
3938                         req_cnt);
3939         }
3940
3941         if (status)
3942                 dprintk(2, "qla1280_64bit_start_scsi: **** FAILED ****\n");
3943         else
3944                 dprintk(3, "qla1280_64bit_start_scsi: exiting normally\n");
3945
3946         return status;
3947 }
3948 #endif
3949
3950 /*
3951  * qla1280_32bit_start_scsi
3952  *      The start SCSI is responsible for building request packets on
3953  *      request ring and modifying ISP input pointer.
3954  *
3955  *      The Qlogic firmware interface allows every queue slot to have a SCSI
3956  *      command and up to 4 scatter/gather (SG) entries.  If we need more
3957  *      than 4 SG entries, then continuation entries are used that can
3958  *      hold another 7 entries each.  The start routine determines if there
3959  *      is eought empty slots then build the combination of requests to
3960  *      fulfill the OS request.
3961  *
3962  * Input:
3963  *      ha = adapter block pointer.
3964  *      sp = SCSI Request Block structure pointer.
3965  *
3966  * Returns:
3967  *      0 = success, was able to issue command.
3968  */
3969 static int
3970 qla1280_32bit_start_scsi(struct scsi_qla_host *ha, srb_t * sp)
3971 {
3972         struct device_reg *reg = ha->iobase;
3973         Scsi_Cmnd *cmd = sp->cmd;
3974         cmd_entry_t *pkt;
3975         struct scatterlist *sg = NULL;
3976         uint32_t *dword_ptr;
3977         int status = 0;
3978         int cnt;
3979         int req_cnt;
3980         uint16_t seg_cnt;
3981         dma_addr_t dma_handle;
3982
3983         ENTER("qla1280_32bit_start_scsi");
3984
3985         dprintk(1, "32bit_start: cmd=%p sp=%p CDB=%x\n", cmd, sp,
3986                 cmd->cmnd[0]);
3987
3988         /* Calculate number of entries and segments required. */
3989         req_cnt = 1;
3990         if (cmd->use_sg) {
3991                 /*
3992                  * We must build an SG list in adapter format, as the kernel's SG list
3993                  * cannot be used directly because of data field size (__alpha__)
3994                  * differences and the kernel SG list uses virtual addresses where
3995                  * we need physical addresses.
3996                  */
3997                 sg = (struct scatterlist *) cmd->request_buffer;
3998                 /* 3.13 32 bit */
3999                 seg_cnt = pci_map_sg (ha->pdev, sg, cmd->use_sg,
4000                                       scsi_to_pci_dma_dir(cmd->sc_data_direction));
4001
4002                 /*
4003                  * if greater than four sg entries then we need to allocate
4004                  * continuation entries
4005                  */
4006                 if (seg_cnt > 4) {
4007                         req_cnt += (seg_cnt - 4) / 7;
4008                         if ((seg_cnt - 4) % 7)
4009                                 req_cnt++;
4010                 }
4011                 dprintk(1, "S/G Transfer cmd=%p seg_cnt=0x%x, req_cnt=%x\n",
4012                         cmd, seg_cnt, req_cnt);
4013         } else if (cmd->request_bufflen) {      /* If data transfer. */
4014                 dprintk(1, "No S/G transfer t=%x cmd=%p len=%x CDB=%x\n",
4015                         SCSI_TCN_32(cmd), cmd, cmd->request_bufflen,
4016                         cmd->cmnd[0]);
4017                 seg_cnt = 1;
4018         } else {
4019                 /* dprintk(1, "No data transfer \n"); */
4020                 seg_cnt = 0;
4021         }
4022
4023         if ((req_cnt + 2) >= ha->req_q_cnt) {
4024                 /* Calculate number of free request entries. */
4025                 cnt = RD_REG_WORD(&reg->mailbox4);
4026                 if (ha->req_ring_index < cnt)
4027                         ha->req_q_cnt = cnt - ha->req_ring_index;
4028                 else
4029                         ha->req_q_cnt =
4030                                 REQUEST_ENTRY_CNT - (ha->req_ring_index - cnt);
4031         }
4032
4033         dprintk(1, "Number of free entries=(%d) seg_cnt=0x%x\n",
4034                 ha->req_q_cnt, seg_cnt);
4035         /* If room for request in request ring. */
4036         if ((req_cnt + 2) < ha->req_q_cnt) {
4037                 /* Check for empty slot in outstanding command list. */
4038                 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS &&
4039                              (ha->outstanding_cmds[cnt] != 0); cnt++) ;
4040
4041                 if (cnt < MAX_OUTSTANDING_COMMANDS) {
4042                         CMD_HANDLE(sp->cmd) =
4043                                 (unsigned char *) (unsigned long) cnt;
4044                         ha->outstanding_cmds[cnt] = sp;
4045                         ha->req_q_cnt -= req_cnt;
4046
4047                         /*
4048                          * Build command packet.
4049                          */
4050                         pkt = (cmd_entry_t *) ha->request_ring_ptr;
4051
4052                         pkt->entry_type = COMMAND_TYPE;
4053                         pkt->entry_count = (uint8_t) req_cnt;
4054                         pkt->sys_define = (uint8_t) ha->req_ring_index;
4055                         pkt->handle = cpu_to_le32(cnt);
4056
4057                         /* Zero out remaining portion of packet. */
4058                         memset(((char *)pkt + 8), 0, (REQUEST_ENTRY_SIZE - 8));
4059
4060                         /* Set ISP command timeout. */
4061                         pkt->timeout = cpu_to_le16(30);
4062
4063                         /* Set device target ID and LUN */
4064                         pkt->lun = SCSI_LUN_32(cmd);
4065                         pkt->target = SCSI_BUS_32(cmd) ?
4066                                 (SCSI_TCN_32(cmd) | BIT_7) : SCSI_TCN_32(cmd);
4067
4068                         /* Enable simple tag queuing if device supports it. */
4069                         if (cmd->device->tagged_queue)
4070                                 pkt->control_flags |= cpu_to_le16(BIT_3);
4071
4072                         /* Load SCSI command packet. */
4073                         pkt->cdb_len = cpu_to_le16(CMD_CDBLEN(cmd));
4074                         memcpy(pkt->scsi_cdb, &(CMD_CDBP(cmd)), CMD_CDBLEN(cmd));
4075
4076                         /*dprintk(1, "Build packet for command[0]=0x%x\n",pkt->scsi_cdb[0]); */
4077                         /* Set transfer direction. */
4078                         sp->dir = qla1280_data_direction(cmd);
4079                         pkt->control_flags |= cpu_to_le16(sp->dir);
4080                         
4081                         /* Set total data segment count. */
4082                         pkt->dseg_count = cpu_to_le16(seg_cnt);
4083
4084                         /*
4085                          * Load data segments.
4086                          */
4087                         if (seg_cnt) {
4088                                 /* Setup packet address segment pointer. */
4089                                 dword_ptr = &pkt->dseg_0_address;
4090
4091                                 if (cmd->use_sg) {      /* If scatter gather */
4092                                         dprintk(1, "Building S/G data "
4093                                                 "segments..\n");
4094                                         qla1280_dump_buffer(1, (char *)sg,
4095                                                             4 * 16);
4096
4097                                         /* Load command entry data segments. */
4098                                         for (cnt = 0; cnt < 4 && seg_cnt;
4099                                              cnt++, seg_cnt--) {
4100                                                 /* 3.13 32 bit */
4101 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,18)
4102                                                 *dword_ptr++ =
4103                                                         cpu_to_le32(virt_to_bus(sg->address));
4104                                                 *dword_ptr++ = cpu_to_le32(sg->length);
4105                                                 dprintk(1,
4106                                                         "S/G Segment phys_addr=0x%x, len=0x%x\n",
4107                                                         cpu_to_le32(virt_to_bus(sg->address)),
4108                                                         sg->length);
4109 #else
4110                                                 *dword_ptr++ =
4111                                                         cpu_to_le32(pci_dma_lo32(sg_dma_address(sg)));
4112                                                 *dword_ptr++ =
4113                                                         cpu_to_le32(sg_dma_len(sg));
4114                                                 dprintk(1, "S/G Segment phys_addr=0x%x, len=0x%x\n",
4115                                                         (pci_dma_lo32(sg_dma_address(sg))),
4116                                                         (sg_dma_len(sg)));
4117 #endif
4118                                                 sg++;
4119                                         }
4120                                         /*
4121                                          * Build continuation packets.
4122                                          */
4123                                         dprintk(1, "S/G Building Continuation"
4124                                                 "...seg_cnt=0x%x remains\n",
4125                                                 seg_cnt);
4126                                         while (seg_cnt > 0) {
4127                                                 /* Adjust ring index. */
4128                                                 ha->req_ring_index++;
4129                                                 if (ha->req_ring_index ==
4130                                                     REQUEST_ENTRY_CNT) {
4131                                                         ha->req_ring_index = 0;
4132                                                         ha->request_ring_ptr =
4133                                                                 ha->request_ring;
4134                                                 } else
4135                                                         ha->request_ring_ptr++;
4136
4137                                                 pkt = (cmd_entry_t *)
4138                                                         ha->request_ring_ptr;
4139
4140                                                 /* Zero out packet. */
4141                                                 memset(pkt, 0,
4142                                                        REQUEST_ENTRY_SIZE);
4143
4144                                                 /* Load packet defaults. */
4145                                                 ((cont_entry_t *) pkt)->
4146                                                         entry_type = CONTINUE_TYPE;
4147                                                 ((cont_entry_t *) pkt)->
4148                                                         entry_count = 1;
4149
4150                                                 ((cont_entry_t *) pkt)->
4151                                                         sys_define =
4152                                                         (uint8_t) ha->
4153                                                         req_ring_index;
4154
4155                                                 /* Setup packet address segment pointer. */
4156                                                 dword_ptr =
4157                                                         &((cont_entry_t *) pkt)->dseg_0_address;
4158
4159                                                 /* Load continuation entry data segments. */
4160                                                 for (cnt = 0;
4161                                                      cnt < 7 && seg_cnt;
4162                                                      cnt++, seg_cnt--) {
4163                                                         /* 3.13 32 bit */
4164 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,18)
4165                                                         *dword_ptr++ =
4166                                                                 cpu_to_le32(virt_to_bus(sg->address));
4167                                                         *dword_ptr++ = cpu_to_le32(sg->length);
4168                                                         dprintk(1,
4169                                                                 "S/G Segment Cont. phys_addr=0x%x, len=0x%x\n",
4170                                                                 cpu_to_le32(pci_dma_lo32(virt_to_bus(sg->address))), sg->length);
4171 #else
4172                                                         *dword_ptr++ =
4173                                                                 cpu_to_le32(pci_dma_lo32(sg_dma_address(sg)));
4174                                                         *dword_ptr++ =
4175                                                                 cpu_to_le32(sg_dma_len(sg));
4176                                                         dprintk(1,
4177                                                                 "S/G Segment Cont. phys_addr=0x%x, "
4178                                                                 "len=0x%x\n",
4179                                                                 cpu_to_le32(pci_dma_lo32(sg_dma_address(sg))),
4180                                                                 cpu_to_le32(sg_dma_len(sg)));
4181 #endif
4182                                                         sg++;
4183                                                 }
4184                                                 dprintk(5,
4185                                                         "qla1280_32bit_start_scsi: continuation "
4186                                                         "packet data - scsi(%i:%i:%i)\n",
4187                                                         SCSI_BUS_32(cmd),
4188                                                         SCSI_TCN_32(cmd),
4189                                                         SCSI_LUN_32(cmd));
4190                                                 qla1280_dump_buffer(5,
4191                                                                     (char *)pkt,
4192                                                                     REQUEST_ENTRY_SIZE);
4193                                         }
4194                                 } else {        /* No S/G data transfer */
4195
4196                                         /* 3.13 32 bit */
4197 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,18)
4198                                         *dword_ptr++ =
4199                                                 cpu_to_le32 (virt_to_bus
4200                                                              (cmd->request_buffer));
4201 #else
4202                                         dma_handle = pci_map_single(ha->pdev,
4203                                                                     cmd->request_buffer,
4204                                                                     cmd->request_bufflen,
4205                                                                     scsi_to_pci_dma_dir(cmd->sc_data_direction));
4206                                         sp->saved_dma_handle = dma_handle;
4207
4208                                         *dword_ptr++ =
4209                                                 cpu_to_le32(pci_dma_lo32(dma_handle));
4210 #endif
4211                                         *dword_ptr =
4212                                                 cpu_to_le32(cmd->request_bufflen);
4213                                         qla1280_dump_buffer(1,(char *)pkt,
4214                                                               REQUEST_ENTRY_SIZE);
4215                                 }
4216                         } else {        /* No data transfer at all */
4217
4218                                 //dword_ptr = (uint32_t *)(pkt + 1);
4219                                 //*dword_ptr++ = 0;
4220                                 //*dword_ptr = 0;
4221                                 dprintk(5,
4222                                         "qla1280_32bit_start_scsi: No data, command "
4223                                         "packet data - \n");
4224                                 qla1280_dump_buffer(5, (char *)pkt,
4225                                                     REQUEST_ENTRY_SIZE);
4226                         }
4227                         dprintk(5,
4228                                 "qla1280_32bit_start_scsi: First IOCB block:\n");
4229                         qla1280_dump_buffer(5, (char *)ha->request_ring_ptr,
4230                                             REQUEST_ENTRY_SIZE);
4231
4232                         /* Adjust ring index. */
4233                         ha->req_ring_index++;
4234                         if (ha->req_ring_index == REQUEST_ENTRY_CNT) {
4235                                 ha->req_ring_index = 0;
4236                                 ha->request_ring_ptr = ha->request_ring;
4237                         } else
4238                                 ha->request_ring_ptr++;
4239
4240                         /* Set chip new ring index. */
4241                         dprintk(1, "qla1280_32bit_start_scsi: Wakeup RISC "
4242                                 "for pending command\n");
4243                         ha->qthreads--;
4244                         sp->flags |= SRB_SENT;
4245                         ha->actthreads++;
4246                         WRT_REG_WORD(&reg->mailbox4, ha->req_ring_index);
4247                 } else {
4248                         status = 1;
4249                         dprintk(2,
4250                                 "qla1280_32bit_start_scsi: NO ROOM IN OUTSTANDING "
4251                                 "ARRAY, req_q_cnt=0x%x\n", ha->req_q_cnt);
4252                 }
4253         } else {
4254                 status = 1;
4255                 dprintk(2,
4256                         "qla1280_32bit_start_scsi: in-ptr=0x%x, req_q_cnt=0x%x, "
4257                         "req_cnt=0x%x", ha->req_ring_index, ha->req_q_cnt,
4258                         req_cnt);
4259         }
4260
4261         if (status)
4262                 dprintk(2, "qla1280_32bit_start_scsi: **** FAILED ****\n");
4263
4264         LEAVE("qla1280_32bit_start_scsi");
4265
4266         return status;
4267 }
4268
4269 /*
4270  * qla1280_req_pkt
4271  *      Function is responsible for locking ring and
4272  *      getting a zeroed out request packet.
4273  *
4274  * Input:
4275  *      ha  = adapter block pointer.
4276  *
4277  * Returns:
4278  *      0 = failed to get slot.
4279  */
4280 static request_t *
4281 qla1280_req_pkt(struct scsi_qla_host *ha)
4282 {
4283         struct device_reg *reg = ha->iobase;
4284         request_t *pkt = 0;
4285         int cnt;
4286         uint32_t timer;
4287
4288         ENTER("qla1280_req_pkt");
4289
4290         /*
4291          * This can be called from interrupt context, damn it!!!
4292          */
4293         /* Wait for 30 seconds for slot. */
4294         for (timer = 15000000; timer; timer--) {
4295                 if (ha->req_q_cnt > 0) {
4296                         /* Calculate number of free request entries. */
4297                         cnt = RD_REG_WORD(&reg->mailbox4);
4298                         if (ha->req_ring_index < cnt)
4299                                 ha->req_q_cnt = cnt - ha->req_ring_index;
4300                         else
4301                                 ha->req_q_cnt =
4302                                         REQUEST_ENTRY_CNT - (ha->req_ring_index - cnt);
4303                 }
4304
4305                 /* Found empty request ring slot? */
4306                 if (ha->req_q_cnt > 0) {
4307                         ha->req_q_cnt--;
4308                         pkt = ha->request_ring_ptr;
4309
4310                         /* Zero out packet. */
4311                         memset(pkt, 0, REQUEST_ENTRY_SIZE);
4312
4313                         /* Set system defined field. */
4314                         pkt->sys_define = (uint8_t) ha->req_ring_index;
4315
4316                         /* Set entry count. */
4317                         pkt->entry_count = 1;
4318
4319                         break;
4320                 }
4321
4322                 udelay(2);      /* 10 */
4323
4324                 /* Check for pending interrupts. */
4325                 qla1280_poll(ha);
4326         }
4327
4328         if (!pkt)
4329                 dprintk(2, "qla1280_req_pkt: **** FAILED ****\n");
4330         else
4331                 dprintk(3, "qla1280_req_pkt: exiting normally\n");
4332
4333         return pkt;
4334 }
4335
4336 /*
4337  * qla1280_isp_cmd
4338  *      Function is responsible for modifying ISP input pointer.
4339  *      Releases ring lock.
4340  *
4341  * Input:
4342  *      ha  = adapter block pointer.
4343  */
4344 static void
4345 qla1280_isp_cmd(struct scsi_qla_host *ha)
4346 {
4347         struct device_reg *reg = ha->iobase;
4348
4349         ENTER("qla1280_isp_cmd");
4350
4351         dprintk(5, "qla1280_isp_cmd: IOCB data:\n");
4352         qla1280_dump_buffer(5, (char *)ha->request_ring_ptr,
4353                             REQUEST_ENTRY_SIZE);
4354
4355         /* Adjust ring index. */
4356         ha->req_ring_index++;
4357         if (ha->req_ring_index == REQUEST_ENTRY_CNT) {
4358                 ha->req_ring_index = 0;
4359                 ha->request_ring_ptr = ha->request_ring;
4360         } else
4361                 ha->request_ring_ptr++;
4362
4363         /* Set chip new ring index. */
4364         WRT_REG_WORD(&reg->mailbox4, ha->req_ring_index);
4365
4366         LEAVE("qla1280_isp_cmd");
4367 }
4368
4369 #if QL1280_LUN_SUPPORT
4370 /*
4371  * qla1280_enable_lun
4372  *      Issue enable LUN entry IOCB.
4373  *
4374  * Input:
4375  *      ha  = adapter block pointer.
4376  *      bus = SCSI BUS number.
4377  *      lun  = LUN number.
4378  */
4379 static void
4380 qla1280_enable_lun(struct scsi_qla_host *ha, int bus, int lun)
4381 {
4382         elun_entry_t *pkt;
4383
4384         ENTER("qla1280_enable_lun");
4385
4386         /* Get request packet. */
4387         /*
4388           if (pkt = (elun_entry_t *)qla1280_req_pkt(ha))
4389           {
4390           pkt->entry_type = ENABLE_LUN_TYPE;
4391           pkt->lun = cpu_to_le16(bus ? lun | BIT_15 : lun);
4392           pkt->command_count = 32;
4393           pkt->immed_notify_count = 1;
4394           pkt->group_6_length = MAX_CMDSZ;
4395           pkt->group_7_length = MAX_CMDSZ;
4396           pkt->timeout = cpu_to_le16(0x30);
4397
4398           qla1280_isp_cmd(ha);
4399           }
4400         */
4401         pkt = (elun_entry_t *) 1;
4402
4403         if (!pkt)
4404                 dprintk(2, "qla1280_enable_lun: **** FAILED ****\n");
4405         else
4406                 dprintk(3, "qla1280_enable_lun: exiting normally\n");
4407 }
4408 #endif
4409
4410 #if QL1280_TARGET_MODE_SUPPORT
4411 /****************************************************************************/
4412 /*                      Target Mode Support Functions.                      */
4413 /****************************************************************************/
4414
4415 /*
4416  * qla1280_notify_ack
4417  *      Issue notify acknowledge IOCB.
4418  *      If sequence ID is zero, acknowledgement of
4419  *      SCSI bus reset or bus device reset is assumed.
4420  *
4421  * Input:
4422  *      ha      = adapter block pointer.
4423  *      inotify = immediate notify entry pointer.
4424  */
4425 static void
4426 qla1280_notify_ack(struct scsi_qla_host *ha, notify_entry_t * inotify)
4427 {
4428         nack_entry_t *pkt;
4429
4430         dprintk(3, "qla1280_notify_ack: entered\n");
4431
4432         /* Get request packet. */
4433         if (pkt = (nack_entry_t *) qla1280_req_pkt(ha)) {
4434                 pkt->entry_type = NOTIFY_ACK_TYPE;
4435                 pkt->lun = inotify->lun;
4436                 pkt->initiator_id = inotify->initiator_id;
4437                 pkt->target_id = inotify->target_id;
4438                 if (inotify->seq_id == 0)
4439                         pkt->event = BIT_7;
4440                 else
4441                         pkt->seq_id = cpu_to_le16(inotify->seq_id);
4442
4443                 /* Issue command to ISP */
4444                 qla1280_isp_cmd(ha);
4445         }
4446
4447         if (!pkt)
4448                 dprintk(2, "qla1280_notify_ack: **** FAILED ****\n");
4449         else
4450                 dprintk(3, "qla1280_notify_ack: exiting normally\n");
4451 }
4452
4453 /*
4454  * qla1280_immed_notify
4455  *      Issue immediate notify IOCB for LUN 0.
4456  *
4457  * Input:
4458  *      ha      = adapter block pointer.
4459  *      inotify = immediate notify entry pointer.
4460  */
4461 static void
4462 qla1280_immed_notify(struct scsi_qla_host *ha, notify_entry_t * inotify)
4463 {
4464         notify_entry_t *pkt;
4465
4466         dprintk(3, "qla1280_immed_notify: entered\n");
4467
4468         /* Get request packet. */
4469         if (pkt = (notify_entry_t *) qla1280_req_pkt(ha)) {
4470                 pkt->entry_type = IMMED_NOTIFY_TYPE;
4471                 pkt->lun = inotify->lun;
4472                 pkt->initiator_id = inotify->initiator_id;
4473                 pkt->target_id = inotify->target_id;
4474                 pkt->status = 1;
4475
4476                 /* Issue command to ISP */
4477                 qla1280_isp_cmd(ha);
4478         }
4479
4480         if (!pkt)
4481                 dprintk(2, "qla1280_immed_notify: **** FAILED ****\n");
4482         else
4483                 dprintk(3, "qla1280_immed_notify: exiting normally\n");
4484 }
4485
4486 /*
4487  * qla1280_accept_io
4488  *      Issue accept target I/O IOCB for LUN 0.
4489  *
4490  * Input:
4491  *      ha = adapter block pointer.
4492  *      ctio = ctio returned entry pointer.
4493  */
4494 static void
4495 qla1280_accept_io(struct scsi_qla_host *ha, ctio_ret_entry_t * ctio)
4496 {
4497         atio_entry_t *pkt;
4498
4499         dprintk(3, "qla1280_accept_io: entered\n");
4500
4501         /* Get request packet. */
4502         if (pkt = (atio_entry_t *) qla1280_req_pkt(ha)) {
4503                 pkt->entry_type = ACCEPT_TGT_IO_TYPE;
4504                 pkt->lun = ctio->lun;
4505                 pkt->initiator_id = ctio->initiator_id;
4506                 pkt->target_id = ctio->target_id;
4507                 pkt->tag_value = ctio->tag_value;
4508                 pkt->status = 1;
4509
4510                 /* Issue command to ISP */
4511                 qla1280_isp_cmd(ha);
4512         }
4513
4514         if (!pkt)
4515                 dprintk(2, "qla1280_accept_io: **** FAILED ****\n");
4516         else
4517                 dprintk(3, "qla1280_accept_io: exiting normally\n");
4518 }
4519
4520 /*
4521  * qla1280_64bit_continue_io
4522  *      Issue continue target I/O IOCB.
4523  *
4524  * Input:
4525  *      ha   = adapter block pointer.
4526  *      atio = atio pointer.
4527  *      len  = total bytecount.
4528  *      addr = physical address pointer.
4529  */
4530 static void
4531 qla1280_64bit_continue_io(struct scsi_qla_host *ha, atio_entry_t * atio,
4532                           uint32_t len, paddr32_t * addr)
4533 {
4534         ctio_a64_entry_t *pkt;
4535         uint32_t *dword_ptr;
4536
4537         dprintk(3, "qla1280_64bit_continue_io: entered\n");
4538
4539         /* Get request packet. */
4540         if (pkt = (ctio_a64_entry_t *) qla1280_req_pkt(ha)) {
4541                 pkt->entry_type = CTIO_A64_TYPE;
4542                 pkt->lun = atio->lun;
4543                 pkt->initiator_id = atio->initiator_id;
4544                 pkt->target_id = atio->target_id;
4545                 pkt->option_flags = cpu_to_le32(atio->option_flags);
4546                 pkt->tag_value = atio->tag_value;
4547                 pkt->scsi_status = atio->scsi_status;
4548
4549                 if (len) {
4550                         pkt->dseg_count = cpu_to_le16(1);
4551                         pkt->transfer_length = cpu_to_le32(len);
4552                         pkt->dseg_0_length = cpu_to_le32(len);
4553                         dword_ptr = (uint32_t *) addr;
4554                         pkt->dseg_0_address[0] = cpu_to_le32(*dword_ptr++);
4555                         pkt->dseg_0_address[1] = cpu_to_le32(*dword_ptr);
4556                 }
4557
4558                 /* Issue command to ISP */
4559                 qla1280_isp_cmd(ha);
4560         }
4561
4562         if (!pkt)
4563                 dprintk(2, "qla1280_64bit_continue_io: **** FAILED ****\n");
4564         else
4565                 dprintk(3, "qla1280_64bit_continue_io: exiting normally\n");
4566 }
4567
4568 /*
4569  * qla1280_32bit_continue_io
4570  *      Issue continue target I/O IOCB.
4571  *
4572  * Input:
4573  *      ha   = adapter block pointer.
4574  *      atio = atio pointer.
4575  *      len  = total bytecount.
4576  *      addr = physical address pointer.
4577  */
4578 static void
4579 qla1280_32bit_continue_io(struct scsi_qla_host *ha, atio_entry_t * atio,
4580                           uint32_t len, paddr32_t * addr)
4581 {
4582         ctio_entry_t *pkt;
4583         uint32_t *dword_ptr;
4584
4585         dprintk(3, "qla1280_32bit_continue_io: entered\n");
4586
4587         /* Get request packet. */
4588         if (pkt = (ctio_entry_t *) qla1280_req_pkt(ha)) {
4589                 pkt->entry_type = CONTINUE_TGT_IO_TYPE;
4590                 pkt->lun = atio->lun;
4591                 pkt->initiator_id = atio->initiator_id;
4592                 pkt->target_id = atio->target_id;
4593                 pkt->option_flags = cpu_to_le32(atio->option_flags);
4594                 pkt->tag_value = atio->tag_value;
4595                 pkt->scsi_status = atio->scsi_status;
4596
4597                 if (len) {
4598                         pkt->dseg_count = cpu_to_le16(1);
4599                         pkt->transfer_length = cpu_to_le32(len);
4600                         pkt->dseg_0_length = cpu_to_le32(len);
4601                         dword_ptr = (uint32_t *)addr;
4602                         pkt->dseg_0_address = cpu_to_le32(*dword_ptr);
4603                 }
4604
4605                 /* Issue command to ISP */
4606                 qla1280_isp_cmd(ha);
4607         }
4608
4609         if (!pkt)
4610                 dprintk(2, "qla1280_32bit_continue_io: **** FAILED ****\n");
4611         else
4612                 dprintk(3, "qla1280_32bit_continue_io: exiting normally\n");
4613 }
4614 #endif                          /* QL1280_TARGET_MODE_SUPPORT */
4615
4616 /****************************************************************************/
4617 /*                        Interrupt Service Routine.                        */
4618 /****************************************************************************/
4619
4620 /****************************************************************************
4621  *  qla1280_isr
4622  *      Calls I/O done on command completion.
4623  *
4624  * Input:
4625  *      ha           = adapter block pointer.
4626  *      done_q_first = done queue first pointer.
4627  *      done_q_last  = done queue last pointer.
4628  ****************************************************************************/
4629 static void
4630 qla1280_isr(struct scsi_qla_host *ha, srb_t ** done_q_first,
4631             srb_t ** done_q_last)
4632 {
4633         struct device_reg *reg = ha->iobase;
4634         response_t *pkt;
4635         srb_t *sp = 0;
4636         uint16_t mailbox[MAILBOX_REGISTER_COUNT];
4637         uint16_t *wptr;
4638         uint32_t index;
4639         u16 istatus;
4640
4641         ENTER("qla1280_isr");
4642
4643         istatus = RD_REG_WORD(&reg->istatus);
4644         if (!(istatus & (RISC_INT | PCI_INT)))
4645                 return;
4646
4647         /* Save mailbox register 5 */
4648         mailbox[5] = RD_REG_WORD(&reg->mailbox5);
4649
4650         /* Check for mailbox interrupt. */
4651
4652         mailbox[0] = RD_REG_WORD(&reg->semaphore);
4653
4654         if (mailbox[0] & BIT_0) {
4655                 /* Get mailbox data. */
4656                 /* dprintk(1, "qla1280_isr: In Get mailbox data \n"); */
4657
4658                 wptr = &mailbox[0];
4659                 *wptr++ = RD_REG_WORD(&reg->mailbox0);
4660                 *wptr++ = RD_REG_WORD(&reg->mailbox1);
4661                 *wptr = RD_REG_WORD(&reg->mailbox2);
4662                 if (mailbox[0] != MBA_SCSI_COMPLETION) {
4663                         wptr++;
4664                         *wptr++ = RD_REG_WORD(&reg->mailbox3);
4665                         *wptr++ = RD_REG_WORD(&reg->mailbox4);
4666                         wptr++;
4667                         *wptr++ = RD_REG_WORD(&reg->mailbox6);
4668                         *wptr = RD_REG_WORD(&reg->mailbox7);
4669                 }
4670
4671                 /* Release mailbox registers. */
4672
4673                 WRT_REG_WORD(&reg->semaphore, 0);
4674                 WRT_REG_WORD(&reg->host_cmd, HC_CLR_RISC_INT);
4675
4676                 dprintk(5, "qla1280_isr: mailbox interrupt mailbox[0] = 0x%x",
4677                         mailbox[0]);
4678
4679                 /* Handle asynchronous event */
4680                 switch (mailbox[0]) {
4681                 case MBA_SCSI_COMPLETION:       /* Response completion */
4682                         dprintk(5,
4683                                 "qla1280_isr: mailbox SCSI response completion\n");
4684
4685                         if (ha->flags.online) {
4686                                 /* Get outstanding command index. */
4687                                 index = mailbox[2] << 16 | mailbox[1];
4688
4689                                 /* Validate handle. */
4690                                 if (index < MAX_OUTSTANDING_COMMANDS)
4691                                         sp = ha->outstanding_cmds[index];
4692                                 else
4693                                         sp = 0;
4694
4695                                 if (sp) {
4696                                         /* Free outstanding command slot. */
4697                                         ha->outstanding_cmds[index] = 0;
4698
4699                                         /* Save ISP completion status */
4700                                         CMD_RESULT(sp->cmd) = 0;
4701
4702                                         /* Place block on done queue */
4703                                         sp->s_next = NULL;
4704                                         sp->s_prev = *done_q_last;
4705                                         if (!*done_q_first)
4706                                                 *done_q_first = sp;
4707                                         else
4708                                                 (*done_q_last)->s_next = sp;
4709                                         *done_q_last = sp;
4710                                 } else {
4711                                         /*
4712                                          * If we get here we have a real problem!
4713                                          */
4714                                         printk(KERN_WARNING
4715                                                "qla1280: ISP invalid handle");
4716                                         ha->flags.isp_abort_needed = TRUE;
4717                                 }
4718                         }
4719                         break;
4720
4721                 case MBA_BUS_RESET:     /* SCSI Bus Reset */
4722                         ha->flags.reset_marker = TRUE;
4723                         index = mailbox[6] & BIT_0;
4724                         ha->bus_settings[index].reset_marker = TRUE;
4725
4726                         printk(KERN_DEBUG
4727                                "qla1280_isr(): index %i asynchronous "
4728                                "BUS_RESET\n", index);
4729                         break;
4730
4731                 case MBA_SYSTEM_ERR:    /* System Error */
4732                         printk(KERN_WARNING
4733                                "qla1280: ISP System Error - mbx1=%xh, mbx2=%xh, "
4734                                "mbx3=%xh\n", mailbox[1], mailbox[2],
4735                                mailbox[3]);
4736                         ha->flags.isp_abort_needed = TRUE;
4737                         break;
4738
4739                 case MBA_REQ_TRANSFER_ERR:      /* Request Transfer Error */
4740                         printk(KERN_WARNING
4741                                "qla1280: ISP Request Transfer Error\n");
4742                         ha->flags.isp_abort_needed = TRUE;
4743                         break;
4744
4745                 case MBA_RSP_TRANSFER_ERR:      /* Response Transfer Error */
4746                         printk(KERN_WARNING
4747                                "qla1280: ISP Response Transfer Error\n");
4748                         ha->flags.isp_abort_needed = TRUE;
4749                         break;
4750
4751                 case MBA_WAKEUP_THRES:  /* Request Queue Wake-up */
4752                         dprintk(2, "qla1280_isr: asynchronous WAKEUP_THRES\n");
4753                         break;
4754
4755                 case MBA_TIMEOUT_RESET: /* Execution Timeout Reset */
4756                         dprintk(2,
4757                                 "qla1280_isr: asynchronous TIMEOUT_RESET\n");
4758                         break;
4759
4760                 case MBA_DEVICE_RESET:  /* Bus Device Reset */
4761                         dprintk(2,
4762                                 "qla1280_isr: asynchronous BUS_DEVICE_RESET\n");
4763                         printk(KERN_INFO "qla1280_isr(): asynchronous "
4764                                "BUS_DEVICE_RESET\n");
4765
4766                         ha->flags.reset_marker = TRUE;
4767                         index = mailbox[6] & BIT_0;
4768                         ha->bus_settings[index].reset_marker = TRUE;
4769                         break;
4770
4771                 case MBA_BUS_MODE_CHANGE:
4772                         dprintk(2,
4773                                 "qla1280_isr: asynchronous BUS_MODE_CHANGE\n");
4774                         break;
4775
4776                 default:
4777                         /* dprintk(1, "qla1280_isr: default case of switch MB \n"); */
4778                         if (mailbox[0] < MBA_ASYNC_EVENT) {
4779                                 wptr = &mailbox[0];
4780                                 memcpy((uint16_t *) ha->mailbox_out, wptr,
4781                                        MAILBOX_REGISTER_COUNT *
4782                                        sizeof(uint16_t));
4783                                 ha->flags.mbox_int = TRUE;
4784                         }
4785                         break;
4786                 }
4787         } else {
4788                 WRT_REG_WORD(&reg->host_cmd, HC_CLR_RISC_INT);
4789         }
4790
4791         /*
4792          * Response ring - waiting for the mbox_busy flag here seems
4793          * unnecessary as the mailbox data has been copied to ha->mailbox_out
4794          * by the time we actually get here!
4795          */
4796         if (ha->flags.online
4797 #if 0
4798             && !ha->flags.mbox_busy
4799 #endif
4800                 ) {
4801                 if (mailbox[5] < RESPONSE_ENTRY_CNT) {
4802                         while (ha->rsp_ring_index != mailbox[5]) {
4803                                 pkt = ha->response_ring_ptr;
4804
4805                                 dprintk(5,
4806                                         "qla1280_isr: ha->rsp_ring_index = 0x%x, "
4807                                         "mailbox[5] = 0x%x\n",
4808                                         ha->rsp_ring_index, mailbox[5]);
4809                                 dprintk(5,
4810                                         "qla1280_isr: response packet data\n");
4811                                 qla1280_dump_buffer(5, (char *)pkt,
4812                                                     RESPONSE_ENTRY_SIZE);
4813
4814                                 if (pkt->entry_type == STATUS_TYPE) {
4815                                         if ((le16_to_cpu(pkt->scsi_status) & 0xff)
4816                                             || pkt->comp_status
4817                                             || pkt->entry_status) {
4818                                                 dprintk(2,
4819                                                         "qla1280_isr: ha->rsp_ring_index = 0x%x"
4820                                                         "mailbox[5] = 0x%x, comp_status = 0x%x, "
4821                                                         "scsi_status = 0x%x\n",
4822                                                         ha->rsp_ring_index,
4823                                                         mailbox[5],
4824                                                         le16_to_cpu(pkt->comp_status),
4825                                                         le16_to_cpu(pkt->scsi_status));
4826                                         }
4827                                 } else {
4828                                         dprintk(2,
4829                                                 "qla1280_isr: ha->rsp_ring_index = 0x%x, "
4830                                                 "mailbox[5] = 0x%x\n",
4831                                                 ha->rsp_ring_index,
4832                                                 mailbox[5]);
4833                                         dprintk(2,
4834                                                 "qla1280_isr: response packet data\n");
4835                                         qla1280_dump_buffer(2, (char *)pkt,
4836                                                             RESPONSE_ENTRY_SIZE);
4837                                 }
4838
4839                                 if (pkt->entry_type == STATUS_TYPE
4840                                     || pkt->entry_status) {
4841                                         if (pkt->entry_type == STATUS_TYPE)
4842                                                 qla1280_status_entry(ha,
4843                                                                      (sts_entry_t *) pkt,
4844                                                                      done_q_first,
4845                                                                      done_q_last);
4846                                         else
4847                                                 qla1280_error_entry(ha, pkt,
4848                                                                     done_q_first,
4849                                                                     done_q_last);
4850
4851                                         /* Adjust ring index. */
4852                                         ha->rsp_ring_index++;
4853                                         if (ha->rsp_ring_index ==
4854                                             RESPONSE_ENTRY_CNT) {
4855                                                 ha->rsp_ring_index = 0;
4856                                                 ha->response_ring_ptr =
4857                                                         ha->response_ring;
4858                                         } else
4859                                                 ha->response_ring_ptr++;
4860                                         WRT_REG_WORD(&reg->mailbox5,
4861                                                      ha->rsp_ring_index);
4862                                 }
4863 #if QLA1280_TARGET_MODE_SUPPORT
4864                                 else {
4865                                         pkt = &response_entry;
4866
4867                                         /* Copy packet. */
4868                                         dptr1 =
4869                                                 (uint32_t *)ha->response_ring_ptr;
4870                                         dptr2 = (uint32_t *) pkt;
4871                                         for (index = 0;
4872                                              index < RESPONSE_ENTRY_SIZE / 4;
4873                                              index++)
4874                                                 *dptr2++ = *dptr1++;
4875
4876                                         /* Adjust ring index. */
4877                                         ha->rsp_ring_index++;
4878                                         if (ha->rsp_ring_index ==
4879                                             RESPONSE_ENTRY_CNT) {
4880                                                 ha->rsp_ring_index = 0;
4881                                                 ha->response_ring_ptr =
4882                                                         ha->response_ring;
4883                                         } else
4884                                                 ha->response_ring_ptr++;
4885                                         WRT_REG_WORD(&reg->mailbox5,
4886                                                      ha->rsp_ring_index);
4887
4888                                         switch (pkt->entry_type) {
4889                                         case ACCEPT_TGT_IO_TYPE:
4890                                                 qla1280_atio_entry(ha,
4891                                                                    (atio_entry_t *) pkt);
4892                                                 break;
4893                                         case IMMED_NOTIFY_TYPE:
4894                                                 qla1280_notify_entry(ha,
4895                                                                      (notify_entry_t *) pkt);
4896                                                 break;
4897                                         case CTIO_RET_TYPE:
4898                                                 qla1280_accept_io(ha,
4899                                                                   (ctio_ret_entry_t *) pkt);
4900                                                 break;
4901                                         default:
4902                                                 break;
4903                                         }
4904                                 }
4905 #endif
4906                         }
4907                 } else {
4908                         ha->flags.isp_abort_needed = TRUE;
4909                         dprintk(2, "qla1280_isr: Response pointer Error\n");
4910                 }
4911         }
4912
4913         LEAVE("qla1280_isr");
4914 }
4915
4916 /*
4917  *  qla1280_rst_aen
4918  *      Processes asynchronous reset.
4919  *
4920  * Input:
4921  *      ha  = adapter block pointer.
4922  */
4923 static void
4924 qla1280_rst_aen(struct scsi_qla_host *ha)
4925 {
4926 #if QL1280_TARGET_MODE_SUPPORT
4927         notify_entry_t nentry;
4928 #endif
4929         uint8_t bus;
4930
4931         ENTER("qla1280_rst_aen");
4932
4933         if (ha->flags.online && !ha->flags.reset_active &&
4934             !ha->flags.abort_isp_active) {
4935                 ha->flags.reset_active = TRUE;
4936                 while (ha->flags.reset_marker) {
4937                         /* Issue marker command. */
4938                         ha->flags.reset_marker = FALSE;
4939                         for (bus = 0; bus < ha->ports &&
4940                                      !ha->flags.reset_marker; bus++) {
4941                                 if (ha->bus_settings[bus].reset_marker) {
4942                                         ha->bus_settings[bus].reset_marker =
4943                                                 FALSE;
4944                                         qla1280_marker(ha, bus, 0, 0,
4945                                                        MK_SYNC_ALL);
4946
4947                                         if (!ha->flags.reset_marker) {
4948 #if QL1280_TARGET_MODE_SUPPORT
4949                                                 /* Issue notify acknowledgement command. */
4950                                                 memset(&nentry, 0,
4951                                                        sizeof(notify_entry_t));
4952
4953                                                 nentry.initiator_id =
4954                                                         nentry.target_id =
4955                                                         bus ? ha->bus_settings[bus].id | BIT_7 :
4956                                                         ha->bus_settings[bus].id;
4957                                                 qla1280_notify_entry(ha,
4958                                                                      &nentry);
4959 #endif
4960                                                 /* Asynchronous event notification */
4961                                         }
4962                                 }
4963                         }
4964                 }
4965         }
4966
4967         LEAVE("qla1280_rst_aen");
4968 }
4969
4970 #if QL1280_TARGET_MODE_SUPPORT
4971 /*
4972  *  qla1280_atio_entry
4973  *      Processes received ISP accept target I/O entry.
4974  *
4975  * Input:
4976  *      ha  = adapter block pointer.
4977  *      pkt = entry pointer.
4978  */
4979 static void
4980 qla1280_atio_entry(struct scsi_qla_host *ha, atio_entry_t * pkt)
4981 {
4982         uint64_t *a64;
4983         uint64_t *end_a64;
4984         paddr32_t phy_addr[2];
4985         paddr32_t end_addr[2];
4986         uint32_t len;
4987         uint32_t offset;
4988         uint8_t t;
4989         uint8_t *sense_ptr;
4990
4991         dprintk(3, "qla1280_atio_entry: entered\n");
4992
4993         t = pkt->initiator_id;
4994         sense_ptr = ha->tsense + t * TARGET_SENSE_SIZE;
4995         a64 = (uint64_t *)&phy_addr[0];
4996         end_a64 = (uint64_t *)&end_addr[0];
4997
4998         switch (pkt->status & ~BIT_7) {
4999         case 7:         /* Path invalid */
5000                 dprintk(2, "qla1280_atio_entry: Path invalid\n");
5001                 break;
5002
5003         case 0x14:              /* Target Bus Phase Sequence Failure */
5004                 dprintk(2, "qla1280_atio_entry: Target Bus Phase "
5005                         "Sequence Failure\n");
5006
5007                 if (pkt->status & BIT_7) {
5008                         memcpy(sense_ptr, &pkt->sense_data, TARGET_SENSE_SIZE);
5009                 } else {
5010                         memset(sense_ptr, 0, TARGET_SENSE_SIZE);
5011                         *sense_ptr = 0x70;
5012                         *(sense_ptr + 2) = SD_HARDERR;
5013                         *(sense_ptr + 7) = TARGET_SENSE_SIZE - 8;
5014                         *(sense_ptr + 12) = SC_SELFAIL;
5015                 }
5016                 pkt->scsi_status = S_CKCON;
5017                 pkt->option_flags |= cpu_to_le32(OF_SSTS | OF_NO_DATA);
5018
5019 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,18)
5020                 if (ha->flags.enable_64bit_addressing)
5021                         qla1280_64bit_continue_io(ha, pkt, 0, 0);
5022                 else
5023 #endif
5024                         qla1280_32bit_continue_io(ha, pkt, 0, 0);
5025                 break;
5026
5027         case 0x16:              /* Requested Capability Not Available */
5028                 dprintk(2, "qla1280_atio_entry: Target Bus Phase "
5029                         "Sequence Failure\n");
5030                 break;
5031
5032         case 0x17:              /* Bus Device Reset Message Received */
5033                 dprintk(2, "qla1280_atio_entry: Target Bus Phase "
5034                         "Sequence Failure\n");
5035                 break;
5036
5037         case 0x3D:              /* CDB Received */
5038
5039                 /* Check for invalid LUN */
5040                 if (pkt->lun && pkt->cdb[0] != SS_INQUIR &&
5041                     pkt->cdb[0] != SS_REQSEN)
5042                         pkt->cdb[0] = SS_TEST;
5043
5044                 switch (pkt->cdb[0]) {
5045                 case SS_TEST:
5046                         dprintk(3, "qla1280_atio_entry: SS_TEST\n");
5047
5048                         memset(sense_ptr, 0, TARGET_SENSE_SIZE);
5049                         len = 0;
5050                         if (pkt->lun == 0)
5051                                 pkt->scsi_status = S_GOOD;
5052                         else {
5053                                 *sense_ptr = 0x70;
5054                                 *(sense_ptr + 2) = SD_ILLREQ;
5055                                 *(sense_ptr + 7) = TARGET_SENSE_SIZE - 8;
5056                                 *(sense_ptr + 12) = SC_INVLUN;
5057                                 pkt->scsi_status = S_CKCON;
5058                         }
5059
5060                         pkt->option_flags |= cpu_to_le32(OF_SSTS | OF_NO_DATA);
5061                         break;
5062
5063                 case SS_REQSEN:
5064                         dprintk(3, "qla1280_atio_entry: SS_REQSEN\n");
5065
5066                         phy_addr[0] = ha->tsense_dma;
5067                         phy_addr[1] = 0;
5068                         *a64 += t * TARGET_SENSE_SIZE;
5069                         if (pkt->cdb[4] > TARGET_SENSE_SIZE)
5070                                 len = TARGET_SENSE_SIZE;
5071                         else
5072                                 len = pkt->cdb[4];
5073                         pkt->scsi_status = S_GOOD;
5074                         pkt->option_flags |= cpu_to_le32(OF_SSTS | OF_DATA_IN);
5075                         break;
5076
5077                 case SS_INQUIR:
5078                         dprintk(3, "qla1280_atio_entry: SS_INQUIR\n");
5079
5080                         memset(sense_ptr, 0, TARGET_SENSE_SIZE);
5081                         phy_addr[0] = ha->tbuf_dma;
5082                         phy_addr[1] = 0;
5083                         *a64 += TARGET_INQ_OFFSET;
5084
5085                         if (pkt->lun == 0) {
5086                                 ha->tbuf->inq.id_type = ID_PROCESOR;
5087                                 ha->tbuf->inq.id_pqual = ID_QOK;
5088                         } else {
5089                                 ha->tbuf->inq.id_type = ID_NODEV;
5090                                 ha->tbuf->inq.id_pqual = ID_QNOLU;
5091                         }
5092
5093                         if (pkt->cdb[4] > sizeof(struct ident))
5094                                 len = sizeof(struct ident);
5095                         else
5096                                 len = pkt->cdb[4];
5097                         pkt->scsi_status = S_GOOD;
5098                         pkt->option_flags |= cpu_to_le32(OF_SSTS | OF_DATA_IN);
5099                         break;
5100
5101                 case SM_WRDB:
5102                         memset(sense_ptr, 0, TARGET_SENSE_SIZE);
5103                         offset = pkt->cdb[5];
5104                         offset |= pkt->cdb[4] << 8;
5105                         offset |= pkt->cdb[3] << 16;
5106                         len = pkt->cdb[8];
5107                         len |= pkt->cdb[7] << 8;
5108                         len |= pkt->cdb[6] << 16;
5109                         end_addr[0] = phy_addr[0] = ha->tbuf_dma;
5110                         end_addr[1] = phy_addr[1] = 0;
5111                         *end_a64 += TARGET_DATA_OFFSET + TARGET_DATA_SIZE;
5112                         switch (pkt->cdb[1] & 7) {
5113                         case RW_BUF_HDATA:
5114                                 dprintk(3, "qla1280_atio_entry: SM_WRDB, "
5115                                         "RW_BUF_HDATA\n");
5116
5117                                 if (len > TARGET_DATA_SIZE + 4) {
5118                                         dprintk(2,
5119                                                 "qla1280_atio_entry: SM_WRDB, "
5120                                                 "length > buffer size\n");
5121
5122                                         *sense_ptr = 0x70;
5123                                         *(sense_ptr + 2) = SD_ILLREQ;
5124                                         *(sense_ptr + 7) =
5125                                                 TARGET_SENSE_SIZE - 8;
5126                                         *(sense_ptr + 12) = SC_ILLCDB;
5127                                         pkt->scsi_status = S_CKCON;
5128                                         pkt->option_flags |=
5129                                                 cpu_to_le32(OF_SSTS | OF_NO_DATA);
5130                                         len = 0;
5131                                 } else if (len) {
5132                                         pkt->scsi_status = S_GOOD;
5133                                         pkt->option_flags |=
5134                                                 cpu_to_le32(OF_SSTS | OF_DATA_OUT);
5135                                         dprintk(3,
5136                                                 "qla1280_atio_entry: Issuing "
5137                                                 "SDI_TARMOD_WRCOMP\n");
5138
5139                                         sdi_xaen(SDI_TARMOD_WRCOMP, ha->cntlr,
5140                                                  pkt->target_id, pkt->lun, 0,
5141                                                  offset);
5142                                 } else {
5143                                         dprintk(2,
5144                                                 "qla1280_atio_entry: SM_WRDB, "
5145                                                 "zero length\n");
5146
5147                                         pkt->scsi_status = S_GOOD;
5148                                         pkt->option_flags |=
5149                                                 cpu_to_le32(OF_SSTS | OF_NO_DATA);
5150                                 }
5151
5152                                 break;
5153                         case RW_BUF_DATA:
5154                                 dprintk(3, "qla1280_atio_entry: SM_WRDB, "
5155                                         "RW_BUF_DATA\n");
5156
5157                                 *a64 += offset + TARGET_DATA_OFFSET;
5158                                 if (pkt->cdb[2] != 0 || *a64 >= *end_a64 ||
5159                                     *a64 + len > *end_a64) {
5160                                         dprintk(2,
5161                                                 "qla1280_atio_entry: SM_WRDB, "
5162                                                 "RW_BUF_DATA BAD\n");
5163                                         dprintk(2, "buf_id=0x%x, offset=0x%x, "
5164                                                 "length=0x%x\n", pkt->cdb[2],
5165                                                 offset, len);
5166
5167                                         *sense_ptr = 0x70;
5168                                         *(sense_ptr + 2) = SD_ILLREQ;
5169                                         *(sense_ptr + 7) =
5170                                                 TARGET_SENSE_SIZE - 8;
5171                                         *(sense_ptr + 12) = SC_ILLCDB;
5172                                         len = 0;
5173                                         pkt->scsi_status = S_CKCON;
5174                                         pkt->option_flags |=
5175                                                 cpu_to_le32(OF_SSTS | OF_NO_DATA);
5176                                 } else if (len) {
5177                                         pkt->scsi_status = S_GOOD;
5178                                         pkt->option_flags |=
5179                                                 cpu_to_le32(OF_SSTS | OF_DATA_OUT);
5180                                         dprintk(3,
5181                                                 "qla1280_atio_entry: Issuing "
5182                                                 "SDI_TARMOD_WRCOMP\n");
5183
5184                                         sdi_xaen(SDI_TARMOD_WRCOMP, ha->cntlr,
5185                                                  pkt->target_id, pkt->lun, 0,
5186                                                  offset);
5187                                 } else {
5188                                         dprintk(2,
5189                                                 "qla1280_atio_entry: SM_WRDB, "
5190                                                 "zero length\n");
5191
5192                                         pkt->scsi_status = S_GOOD;
5193                                         pkt->option_flags |=
5194                                                 cpu_to_le32(OF_SSTS | OF_NO_DATA);
5195                                 }
5196                                 break;
5197
5198                         default:
5199                                 dprintk(2, "qla1280_atio_entry: SM_WRDB "
5200                                         "unknown mode\n");
5201
5202                                 *sense_ptr = 0x70;
5203                                 *(sense_ptr + 2) = SD_ILLREQ;
5204                                 *(sense_ptr + 7) = TARGET_SENSE_SIZE - 8;
5205                                 *(sense_ptr + 12) = SC_ILLCDB;
5206                                 len = 0;
5207                                 pkt->scsi_status = S_CKCON;
5208                                 pkt->option_flags |= cpu_to_le32(OF_SSTS | OF_NO_DATA);
5209                                 break;
5210                         }
5211                         break;
5212
5213                 case SM_RDDB:
5214                         memset(sense_ptr, 0, TARGET_SENSE_SIZE);
5215                         offset = pkt->cdb[5];
5216                         offset |= pkt->cdb[4] << 8;
5217                         offset |= pkt->cdb[3] << 16;
5218                         len = pkt->cdb[8];
5219                         len |= pkt->cdb[7] << 8;
5220                         len |= pkt->cdb[6] << 16;
5221                         end_addr[0] = phy_addr[0] = ha->tbuf_dma;
5222                         end_addr[1] = phy_addr[1] = 0;
5223                         *end_a64 += TARGET_DATA_OFFSET + TARGET_DATA_SIZE;
5224                         switch (pkt->cdb[1] & 7) {
5225                         case RW_BUF_HDATA:
5226                                 dprintk(3, "qla1280_atio_entry: SM_RDDB, "
5227                                         "RW_BUF_HDATA\n");
5228
5229                                 if (len) {
5230                                         ha->tbuf->hdr[0] = 0;
5231                                         ha->tbuf->hdr[1] =
5232                                                 (TARGET_DATA_SIZE >> 16) & 0xff;
5233                                         ha->tbuf->hdr[2] =
5234                                                 (TARGET_DATA_SIZE >> 8) & 0xff;
5235                                         ha->tbuf->hdr[3] =
5236                                                 TARGET_DATA_SIZE & 0xff;
5237                                         if (len > TARGET_DATA_SIZE + 4)
5238                                                 len = TARGET_DATA_SIZE + 4;
5239                                         pkt->scsi_status = S_GOOD;
5240                                         pkt->option_flags |=
5241                                                 cpu_to_le32(OF_SSTS | OF_DATA_IN);
5242                                 } else {
5243                                         dprintk(2,
5244                                                 "qla1280_atio_entry: SM_RDDB, "
5245                                                 "zero length\n");
5246
5247                                         pkt->scsi_status = S_GOOD;
5248                                         pkt->option_flags |=
5249                                                 cpu_to_le32(OF_SSTS | OF_NO_DATA);
5250                                 }
5251                                 break;
5252                         case RW_BUF_DATA:
5253                                 dprintk(3, "qla1280_atio_entry: SM_RDDB, "
5254                                         "RW_BUF_DATA\n");
5255
5256                                 *a64 += offset + TARGET_DATA_OFFSET;
5257                                 if (pkt->cdb[2] != 0 || *a64 >= *end_a64) {
5258                                         dprintk(2,
5259                                                 "qla1280_atio_entry: SM_RDDB, "
5260                                                 "RW_BUF_DATA BAD\n");
5261                                         dprintk(2, "buf_id=0x%x, offset=0x%x\n"
5262                                                 pkt->cdb[2], offset);
5263
5264                                         *sense_ptr = 0x70;
5265                                         *(sense_ptr + 2) = SD_ILLREQ;
5266                                         *(sense_ptr + 7) =
5267                                                 TARGET_SENSE_SIZE - 8;
5268                                         *(sense_ptr + 12) = SC_ILLCDB;
5269                                         len = 0;
5270                                         pkt->scsi_status = S_CKCON;
5271                                         pkt->option_flags |=
5272                                                 cpu_to_le32(OF_SSTS | OF_NO_DATA);
5273                                 } else {
5274                                         if (*a64 + len > *end_a64)
5275                                                 len = *end_a64 - *a64;
5276                                         if (len) {
5277                                                 pkt->scsi_status = S_GOOD;
5278                                                 pkt->option_flags |=
5279                                                         (OF_SSTS | OF_DATA_IN);
5280                                         } else {
5281                                                 dprintk(2,
5282                                                         "qla1280_atio_entry: SM_RDDB, "
5283                                                         "zero length\n");
5284
5285                                                 pkt->scsi_status = S_GOOD;
5286                                                 pkt->option_flags |=
5287                                                         cpu_to_le32(OF_SSTS | OF_NO_DATA);
5288                                         }
5289                                 }
5290                                 break;
5291                         case RW_BUF_DESC:
5292                                 dprintk(3, "qla1280_atio_entry: SM_RDDB, "
5293                                         "RW_BUF_DESC\n");
5294
5295                                 if (len) {
5296                                         if (len > 4)
5297                                                 len = 4;
5298
5299                                         ha->tbuf->hdr[0] = 0;
5300                                         if (pkt->cdb[2] != 0) {
5301                                                 ha->tbuf->hdr[1] = 0;
5302                                                 ha->tbuf->hdr[2] = 0;
5303                                                 ha->tbuf->hdr[3] = 0;
5304                                         } else {
5305                                                 ha->tbuf->hdr[1] =
5306                                                         (TARGET_DATA_SIZE >> 16) &
5307                                                         0xff;
5308                                                 ha->tbuf->hdr[2] =
5309                                                         (TARGET_DATA_SIZE >> 8) &
5310                                                         0xff;
5311                                                 ha->tbuf->hdr[3] =
5312                                                         TARGET_DATA_SIZE & 0xff;
5313                                         }
5314                                         pkt->scsi_status = S_GOOD;
5315                                         pkt->option_flags |=
5316                                                 cpu_to_le32(OF_SSTS | OF_DATA_IN);
5317                                 } else {
5318                                         dprintk(2,
5319                                                 "qla1280_atio_entry: SM_RDDB,"
5320                                                 " zero length\n");
5321
5322                                         pkt->scsi_status = S_GOOD;
5323                                         pkt->option_flags |=
5324                                                 cpu_to_le32(OF_SSTS | OF_NO_DATA);
5325                                 }
5326                                 break;
5327                         default:
5328                                 dprintk(2, "qla1280_atio_entry: SM_RDDB "
5329                                         "unknown mode\n");
5330
5331                                 *sense_ptr = 0x70;
5332                                 *(sense_ptr + 2) = SD_ILLREQ;
5333                                 *(sense_ptr + 7) = TARGET_SENSE_SIZE - 8;
5334                                 *(sense_ptr + 12) = SC_ILLCDB;
5335                                 len = 0;
5336                                 pkt->scsi_status = S_CKCON;
5337                                 pkt->option_flags |= cpu_to_le32(OF_SSTS | OF_NO_DATA);
5338                                 break;
5339                         }
5340                         break;
5341
5342                 default:
5343                         dprintk(2,
5344                                 "qla1280_atio_entry: Unknown SCSI command\n");
5345                         qla1280_dump_buffer(2, &pkt->cdb[0], pkt->cdb_len);
5346
5347                         memset(sense_ptr, 0, TARGET_SENSE_SIZE);
5348                         *sense_ptr = 0x70;
5349                         *(sense_ptr + 2) = SD_ILLREQ;
5350                         *(sense_ptr + 7) = TARGET_SENSE_SIZE - 8;
5351                         *(sense_ptr + 12) = SC_INVOPCODE;
5352                         len = 0;
5353                         pkt->scsi_status = S_CKCON;
5354                         pkt->option_flags |= cpu_to_le32(OF_SSTS | OF_NO_DATA);
5355                         break;
5356                 }
5357 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,18)
5358                 if (ha->flags.enable_64bit_addressing)
5359                         qla1280_64bit_continue_io(ha, pkt, len,
5360                                                   (paddr32_t *)&phy_addr);
5361                 else
5362 #endif
5363                         qla1280_32bit_continue_io(ha, pkt, len,
5364                                                   (paddr32_t *)&phy_addr);
5365                 break;
5366
5367         default:
5368                 break;
5369         }
5370
5371         dprintk(3, "qla1280_atio_entry: exiting normally\n");
5372 }
5373
5374 /*
5375  *  qla1280_notify_entry
5376  *      Processes received ISP immediate notify entry.
5377  *
5378  * Input:
5379  *      ha  = adapter block pointer.
5380  *      pkt = entry pointer.
5381  */
5382 static void
5383 qla1280_notify_entry(struct scsi_qla_host *ha, notify_entry_t * pkt)
5384 {
5385         dprintk(3, "qla1280_notify_entry: entered\n");
5386
5387         /* Acknowledge immediate notify */
5388         qla1280_notify_ack(ha, pkt);
5389
5390         /* Issue notify entry to increment resource count */
5391         qla1280_immed_notify(ha, pkt);
5392
5393         dprintk(3, "qla1280_notify_entry: exiting normally\n");
5394 }
5395 #endif                          /* QLA1280_TARGET_MODE_SUPPORT */
5396
5397 /*
5398  *  qla1280_status_entry
5399  *      Processes received ISP status entry.
5400  *
5401  * Input:
5402  *      ha           = adapter block pointer.
5403  *      pkt          = entry pointer.
5404  *      done_q_first = done queue first pointer.
5405  *      done_q_last  = done queue last pointer.
5406  */
5407 static void
5408 qla1280_status_entry(struct scsi_qla_host *ha, sts_entry_t * pkt,
5409                      srb_t ** done_q_first, srb_t ** done_q_last)
5410 {
5411         unsigned int bus, target, lun;
5412         int sense_sz;
5413         srb_t *sp;
5414         scsi_lu_t *q;
5415         Scsi_Cmnd *cmd;
5416         uint32_t handle = le32_to_cpu(pkt->handle);
5417         uint16_t scsi_status = le16_to_cpu(pkt->scsi_status);
5418         uint16_t comp_status = le16_to_cpu(pkt->comp_status);
5419
5420         ENTER("qla1280_status_entry");
5421
5422         /* Validate handle. */
5423         if (handle < MAX_OUTSTANDING_COMMANDS)
5424                 sp = ha->outstanding_cmds[handle];
5425         else
5426                 sp = 0;
5427
5428         if (sp) {
5429                 /* Free outstanding command slot. */
5430                 ha->outstanding_cmds[handle] = 0;
5431
5432                 cmd = sp->cmd;
5433
5434                 /* Generate LU queue on cntrl, target, LUN */
5435                 bus = SCSI_BUS_32(cmd);
5436                 target = SCSI_TCN_32(cmd);
5437                 lun = SCSI_LUN_32(cmd);
5438                 q = LU_Q(ha, bus, target, lun);
5439
5440                 if (comp_status || scsi_status) {
5441                         dprintk(1, "scsi: comp_status = 0x%x, scsi_status = "
5442                                 "0x%x, handle = 0x%lx\n", comp_status,
5443                                 scsi_status, handle);
5444                 }
5445
5446                 /* Target busy */
5447                 if (scsi_status & SS_BUSY_CONDITION &&
5448                     scsi_status != SS_RESERVE_CONFLICT) {
5449                         CMD_RESULT(cmd) =
5450                                 DID_BUS_BUSY << 16 | (scsi_status & 0xff);
5451                 } else {
5452
5453                         /* Save ISP completion status */
5454                         CMD_RESULT(cmd) = qla1280_return_status(pkt, cmd);
5455
5456                         if (scsi_status & SS_CHECK_CONDITION) {
5457                                 if (comp_status != CS_ARS_FAILED) {
5458                                         uint16_t req_sense_length =
5459                                                 le16_to_cpu(pkt->req_sense_length);
5460                                         if (req_sense_length <
5461                                             CMD_SNSLEN(cmd))
5462                                                 sense_sz = req_sense_length;
5463                                         else
5464                                                 /*
5465                                                  * Scsi_Cmnd->sense_buffer is
5466                                                  * 64 bytes, why only copy 63?
5467                                                  * This looks wrong! /Jes
5468                                                  */
5469                                                 sense_sz = CMD_SNSLEN(cmd) - 1;
5470
5471                                         memcpy(cmd->sense_buffer,
5472                                                &pkt->req_sense_data, sense_sz);
5473                                 } else
5474                                         sense_sz = 0;
5475                                 memset(cmd->sense_buffer + sense_sz, 0,
5476                                        sizeof(cmd->sense_buffer) - sense_sz);
5477
5478                                 dprintk(2, "qla1280_status_entry: Check "
5479                                         "condition Sense data, b %i, t %i, "
5480                                         "l %i\n", bus, target, lun);
5481                                 if (sense_sz)
5482                                         qla1280_dump_buffer(2, (char *)cmd->sense_buffer,
5483                                                             sense_sz);
5484                         }
5485                 }
5486                 /* Place command on done queue. */
5487                 qla1280_done_q_put(sp, done_q_first, done_q_last);
5488         } else {
5489                 printk(KERN_WARNING "qla1280: Status Entry invalid handle\n");
5490                 ha->flags.isp_abort_needed = TRUE;
5491         }
5492
5493         LEAVE("qla1280_status_entry");
5494 }
5495
5496 /*
5497  *  qla1280_error_entry
5498  *      Processes error entry.
5499  *
5500  * Input:
5501  *      ha           = adapter block pointer.
5502  *      pkt          = entry pointer.
5503  *      done_q_first = done queue first pointer.
5504  *      done_q_last  = done queue last pointer.
5505  */
5506 static void
5507 qla1280_error_entry(struct scsi_qla_host *ha, response_t * pkt,
5508                     srb_t ** done_q_first, srb_t ** done_q_last)
5509 {
5510         srb_t *sp;
5511         uint32_t handle = le32_to_cpu(pkt->handle);
5512
5513         ENTER("qla1280_error_entry");
5514
5515         if (pkt->entry_status & BIT_3)
5516                 dprintk(2, "qla1280_error_entry: BAD PAYLOAD flag error\n");
5517         else if (pkt->entry_status & BIT_2)
5518                 dprintk(2, "qla1280_error_entry: BAD HEADER flag error\n");
5519         else if (pkt->entry_status & BIT_1)
5520                 dprintk(2, "qla1280_error_entry: FULL flag error\n");
5521         else
5522                 dprintk(2, "qla1280_error_entry: UNKNOWN flag error\n");
5523
5524         /* Validate handle. */
5525         if (handle < MAX_OUTSTANDING_COMMANDS)
5526                 sp = ha->outstanding_cmds[handle];
5527         else
5528                 sp = 0;
5529
5530         if (sp) {
5531                 /* Free outstanding command slot. */
5532                 ha->outstanding_cmds[handle] = 0;
5533
5534                 /* Bad payload or header */
5535                 if (pkt->entry_status & (BIT_3 + BIT_2)) {
5536                         /* Bad payload or header, set error status. */
5537                         /* CMD_RESULT(sp->cmd) = CS_BAD_PAYLOAD; */
5538                         CMD_RESULT(sp->cmd) = DID_ERROR << 16;
5539                 } else if (pkt->entry_status & BIT_1) { /* FULL flag */
5540                         CMD_RESULT(sp->cmd) = DID_BUS_BUSY << 16;
5541                 } else {
5542                         /* Set error status. */
5543                         CMD_RESULT(sp->cmd) = DID_ERROR << 16;
5544                 }
5545                 /* Place command on done queue. */
5546                 qla1280_done_q_put(sp, done_q_first, done_q_last);
5547         }
5548 #ifdef QLA_64BIT_PTR
5549         else if (pkt->entry_type == COMMAND_A64_TYPE) {
5550                 printk(KERN_WARNING "!qla1280: Error Entry invalid handle");
5551                 ha->flags.isp_abort_needed = TRUE;
5552         }
5553 #endif
5554
5555         LEAVE("qla1280_error_entry");
5556 }
5557
5558 /*
5559  *  qla1280_abort_isp
5560  *      Resets ISP and aborts all outstanding commands.
5561  *
5562  * Input:
5563  *      ha           = adapter block pointer.
5564  *
5565  * Returns:
5566  *      0 = success
5567  */
5568 static int
5569 qla1280_abort_isp(struct scsi_qla_host *ha)
5570 {
5571 #if 0
5572         struct device_reg *reg = ha->iobase;
5573 #endif
5574         srb_t *sp;
5575         scsi_lu_t *q;
5576         int status = 0;
5577         int cnt;
5578         int bus, target, lun;
5579
5580         ENTER("qla1280_abort_isp");
5581
5582         ha->flags.isp_abort_needed = FALSE;
5583         if (!ha->flags.abort_isp_active && ha->flags.online) {
5584                 ha->flags.abort_isp_active = TRUE;
5585
5586                 /* Disable ISP interrupts. */
5587                 qla1280_disable_intrs(ha);
5588
5589                 /* Dequeue all commands in outstanding command list. */
5590                 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
5591                         sp = ha->outstanding_cmds[cnt];
5592                         if (sp) {
5593                                 ha->outstanding_cmds[cnt] = 0;
5594
5595                                 /* Generate LU queue on controller, target, LUN */
5596                                 bus = SCSI_BUS_32(sp->cmd);
5597                                 target = SCSI_TCN_32(sp->cmd);
5598                                 lun = SCSI_LUN_32(sp->cmd);
5599
5600                                 q = LU_Q(ha, bus, target, lun);
5601
5602                                 /* Reset outstanding command count. */
5603                                 q->q_outcnt = 0;
5604                                 q->q_flag &= ~QLA1280_QBUSY;
5605                                 q->q_flag = 0;
5606
5607                                 /* Adjust watchdog timer for command. */
5608                                 /* if (sp->flags & SRB_WATCHDOG)
5609                                    sp->timeout += 2; */
5610
5611                                 /* Place request back on top of device queue. */
5612                                 sp->flags = 0;
5613                                 qla1280_putq_t(q, sp);
5614                         }
5615                 }
5616
5617                 /* If firmware needs to be loaded */
5618                 if (qla1280_isp_firmware (ha)) {
5619                         if (!(status = qla1280_chip_diag(ha)))
5620                                 status = qla1280_setup_chip(ha);
5621                 }
5622
5623                 if (!status) {
5624                         /* Setup adapter based on NVRAM parameters. */
5625                         qla1280_nvram_config (ha);
5626
5627                         if (!(status = qla1280_init_rings(ha))) {
5628                                 /* Issue SCSI reset. */
5629                                 for (bus = 0; bus < ha->ports; bus++) {
5630                                         qla1280_bus_reset(ha, bus);
5631                                 }
5632                                 do {
5633                                         /* Issue marker command. */
5634                                         ha->flags.reset_marker = FALSE;
5635                                         for (bus = 0; bus < ha->ports; bus++) {
5636                                                 ha->bus_settings[bus].
5637                                                         reset_marker = FALSE;
5638                                                 qla1280_marker(ha, bus, 0, 0,
5639                                                                MK_SYNC_ALL);
5640                                         }
5641                                 } while (ha->flags.reset_marker);
5642
5643                                 /* Enable host adapter target mode. */
5644                                 for (bus = 0; bus < ha->ports; bus++) {
5645                                         if (!
5646                                             (status =
5647                                              qla1280_enable_tgt(ha, bus))) {
5648                                                 for (cnt = 0; cnt < MAX_LUNS;
5649                                                      cnt++) {
5650                                                         /* qla1280_enable_lun(ha, bus, cnt); */
5651                                                         qla1280_poll(ha);
5652                                                 }
5653                                         } else
5654                                                 break;
5655                                 }
5656
5657                                 if (!status) {
5658                                         /* Enable ISP interrupts. */
5659                                         qla1280_enable_intrs(ha);
5660                                         ha->flags.abort_isp_active = FALSE;
5661                                         /* Restart queues that may have been stopped. */
5662                                         qla1280_restart_queues(ha);
5663                                 }
5664                         }
5665                 }
5666         }
5667
5668         if (status) {
5669                 printk(KERN_WARNING
5670                        "qla1280: ISP error recovery failed, board disabled");
5671                 qla1280_reset_adapter(ha);
5672                 qla1280_abort_queues(ha);
5673
5674                 dprintk(2, "qla1280_abort_isp: **** FAILED ****\n");
5675         }
5676
5677         LEAVE("qla1280_abort_isp");
5678         return status;
5679 }
5680
5681 /*
5682  *  qla1280_restart_queues
5683  *      Restart all device queues.
5684  *
5685  * Input:
5686  *      ha = adapter block pointer.
5687  */
5688 static void
5689 qla1280_restart_queues(struct scsi_qla_host *ha)
5690 {
5691         scsi_lu_t *q;
5692         int bus, target, lun;
5693
5694         ENTER("qla1280_restart_queues");
5695
5696         for (bus = 0; bus < ha->ports; bus++)
5697                 for (target = 0; target < MAX_TARGETS; target++)
5698                         for (lun = 0; lun < MAX_LUNS; lun++) {
5699                                 q = LU_Q(ha, bus, target, lun);
5700                                 if (q != NULL) {
5701                                         if (q->q_first)
5702                                                 qla1280_next(ha, q, bus);
5703                                 }
5704                         }
5705         dprintk(3, "qla1280_restart_queues: exiting normally\n");
5706 }
5707
5708 /*
5709  *  qla1280_abort_queue_single
5710  *      Abort all commands on a device queues.
5711  *
5712  * Input:
5713  *      ha = adapter block pointer.
5714  */
5715 static void
5716 qla1280_abort_queue_single(struct scsi_qla_host *ha, int bus,
5717                            int target, int lun, uint32_t stat)
5718 {
5719         scsi_lu_t *q;
5720         srb_t *sp, *sp_next;
5721
5722         ENTER("qla1280_abort_queue_single");
5723         q = LU_Q(ha, bus, target, lun);
5724         if (q != NULL) {
5725                 sp = q->q_first;
5726                 q->q_first = q->q_last = NULL;
5727
5728                 while (sp) {
5729                         sp_next = sp->s_next;
5730                         CMD_RESULT(sp->cmd) = stat;
5731                         qla1280_done_q_put(sp, &ha->done_q_first,
5732                                            &ha->done_q_last);
5733                         sp = sp_next;
5734                 }
5735         }
5736         LEAVE("qla1280_abort_queue_single");
5737 }
5738
5739 /*
5740  *  qla1280_abort_queues
5741  *      Abort all commands on device queues.
5742  *
5743  * Input:
5744  *      ha = adapter block pointer.
5745  */
5746 static void
5747 qla1280_abort_queues(struct scsi_qla_host *ha)
5748 {
5749         uint32_t bus, target, lun;
5750
5751         ENTER("qla1280_abort_queues");
5752
5753         for (bus = 0; bus < ha->ports; bus++)
5754                 for (target = 0; target < MAX_TARGETS; target++)
5755                         for (lun = 0; lun < MAX_LUNS; lun++)
5756                                 qla1280_abort_queue_single(ha, bus, target,
5757                                                            lun, DID_RESET);
5758
5759         LEAVE("qla1280_abort_queues");
5760 }
5761
5762 /*
5763  * qla1280_debounce_register
5764  *      Debounce register.
5765  *
5766  * Input:
5767  *      port = register address.
5768  *
5769  * Returns:
5770  *      register value.
5771  */
5772 static u16
5773 qla1280_debounce_register(volatile u16 * addr)
5774 {
5775         volatile u16 ret;
5776         volatile u16 ret2;
5777
5778         ret = RD_REG_WORD(addr);
5779         ret2 = RD_REG_WORD(addr);
5780
5781         if (ret == ret2)
5782                 return ret;
5783
5784         do {
5785                 cpu_relax();
5786                 ret = RD_REG_WORD(addr);
5787                 ret2 = RD_REG_WORD(addr);
5788         } while (ret != ret2);
5789
5790         return ret;
5791 }
5792
5793 static Scsi_Host_Template driver_template = {
5794         .proc_info              = qla1280_proc_info,
5795         .name                   = "Qlogic ISP 1280/12160",
5796         .detect                 = qla1280_detect,
5797         .release                = qla1280_release,
5798         .info                   = qla1280_info,
5799         .queuecommand           = qla1280_queuecommand,
5800         .eh_abort_handler       = qla1280_eh_abort,
5801         .eh_device_reset_handler = qla1280_eh_device_reset,
5802         .eh_bus_reset_handler   = qla1280_eh_bus_reset,
5803         .eh_host_reset_handler  = qla1280_eh_adapter_reset,
5804         .slave_configure        = qla1280_slave_configure,
5805         .bios_param             = qla1280_biosparam,
5806         .can_queue              = 255,
5807         .this_id                = -1,
5808         .sg_tablesize           = SG_ALL,
5809         .cmd_per_lun            = 3,
5810         .use_clustering         = ENABLE_CLUSTERING,                    \
5811 };
5812 #include "scsi_module.c"
5813
5814 /************************************************************************
5815  * qla1280_check_for_dead_scsi_bus                                      *
5816  *                                                                      *
5817  *    This routine checks for a dead SCSI bus                           *
5818  ************************************************************************/
5819 #define SET_SXP_BANK            0x0100
5820 #define SCSI_PHASE_INVALID      0x87FF
5821 static int
5822 qla1280_check_for_dead_scsi_bus(struct scsi_qla_host *ha, unsigned int bus)
5823 {
5824         uint16_t config_reg, scsi_control;
5825         struct device_reg *reg = ha->iobase;
5826 #if 0
5827         unsigned int bus;
5828         Scsi_Cmnd *cp;
5829
5830         /*
5831          * If SCSI Bus is Dead because of bad termination,
5832          * we will return a status of Selection timeout.
5833          */
5834
5835         cp = sp->cmd;
5836         bus = SCSI_BUS_32(cp);
5837 #endif
5838         if (ha->bus_settings[bus].scsi_bus_dead) {
5839                 WRT_REG_WORD(&reg->host_cmd, HC_PAUSE_RISC);
5840                 config_reg = RD_REG_WORD(&reg->cfg_1);
5841                 WRT_REG_WORD(&reg->cfg_1, SET_SXP_BANK);
5842                 scsi_control = RD_REG_WORD(&reg->scsiControlPins);
5843                 WRT_REG_WORD(&reg->cfg_1, config_reg);
5844                 WRT_REG_WORD(&reg->host_cmd, HC_RELEASE_RISC);
5845
5846                 if (scsi_control == SCSI_PHASE_INVALID) {
5847                         ha->bus_settings[bus].scsi_bus_dead = TRUE;
5848 #if 0
5849                         CMD_RESULT(cp) = DID_NO_CONNECT << 16;
5850                         CMD_HANDLE(cp) = NULL;
5851                         /* ha->actthreads--; */
5852
5853                         (*(cp)->scsi_done)(cp);
5854 #endif
5855                         return TRUE;    /* bus is dead */
5856                 } else {
5857                         ha->bus_settings[bus].scsi_bus_dead = FALSE;
5858                         ha->bus_settings[bus].failed_reset_count = 0;
5859                 }
5860         }
5861         return FALSE;           /* bus is not dead */
5862 }
5863
5864 static void
5865 qla12160_get_target_parameters(struct scsi_qla_host *ha, uint32_t bus,
5866                                uint32_t target, uint32_t lun)
5867 {
5868         uint16_t mb[MAILBOX_REGISTER_COUNT];
5869
5870         mb[0] = MBC_GET_TARGET_PARAMETERS;
5871         mb[1] = (uint16_t) (bus ? target | BIT_7 : target);
5872         mb[1] <<= 8;
5873         qla1280_mailbox_command(ha, BIT_6 | BIT_3 | BIT_2 | BIT_1 | BIT_0,
5874                                 &mb[0]);
5875         if (mb[3] != 0) {
5876                 printk(KERN_INFO
5877                        "scsi(%ld:%d:%d:%d): Synchronous transfer at period "
5878                        "%d, offset %d. \n", ha->host_no, bus, target, lun,
5879                        (mb[3] & 0xff), (mb[3] >> 8));
5880         }
5881
5882         if ((mb[2] & BIT_5) && ((mb[6] >> 8) & 0xff) >= 2) {
5883                 printk(KERN_INFO
5884                        "scsi(%ld:%d:%d:%d): Dual Transition enabled.\n",
5885                        ha->host_no, bus, target, lun);
5886         }
5887 }
5888
5889 #ifdef QL_DEBUG_ROUTINES
5890 /****************************************************************************/
5891 /*                         Driver Debug Functions.                          */
5892 /****************************************************************************/
5893
5894 /*
5895  *  Get byte from I/O port
5896  */
5897 static u8
5898 qla1280_getbyte (u8 * port)
5899 {
5900         u8 ret;
5901
5902 #if MEMORY_MAPPED_IO
5903         ret = readb((unsigned long) port);
5904 #else
5905         ret = inb((unsigned long) port);
5906 #endif
5907
5908         if (ql_debug_print)
5909                 printk(KERN_DEBUG
5910                        "qla1280_getbyte: address = 0x%p, data = 0x%x", port,
5911                        ret);
5912
5913         return ret;
5914 }
5915
5916 /*
5917  *  Get word from I/O port
5918  */
5919 static u16
5920 qla1280_getword (u16 * port)
5921 {
5922         u16 ret;
5923
5924 #if MEMORY_MAPPED_IO
5925         ret = readw(unsigned long) port;
5926 #else
5927         ret = inw((unsigned long) port);
5928 #endif
5929
5930         if (ql_debug_print)
5931                 printk(KERN_DEBUG
5932                        "qla1280_getbyte: address = 0x%p, data = 0x%x", port,
5933                        ret);
5934
5935         return ret;
5936 }
5937
5938 /*
5939  *  Get double word from I/O port
5940  */
5941 static u32
5942 qla1280_getdword (u32 * port)
5943 {
5944         u32 ret;
5945
5946 #if MEMORY_MAPPED_IO
5947         ret = readl((unsigned long) port);
5948 #else
5949         ret = inl((unsigned long) port);
5950 #endif
5951
5952         if (ql_debug_print)
5953                 printk(KERN_DEBUG
5954                        "qla1280_getbyte: address = 0x%p, data = 0x%x", port,
5955                        ret);
5956
5957         return ret;
5958 }
5959
5960 /*
5961  *  Send byte to I/O port
5962  */
5963 static void
5964 qla1280_putbyte(u8 * port, u8 data)
5965 {
5966 #if MEMORY_MAPPED_IO
5967         writeb(data, (unsigned long) port);
5968 #else
5969         outb(data, (unsigned long) port);
5970 #endif
5971
5972         if (ql_debug_print)
5973                 printk(KERN_DEBUG
5974                        "qla1280_getbyte: address = 0x%p, data = 0x%x", port,
5975                        data);
5976 }
5977
5978 /*
5979  *  Send word to I/O port
5980  */
5981 static void
5982 qla1280_putword(u16 * port, u16 data)
5983 {
5984 #if MEMORY_MAPPED_IO
5985         writew(data, (unsigned long) port);
5986 #else
5987         outw(data, (unsigned long) port);
5988 #endif
5989
5990         if (ql_debug_print)
5991                 printk(KERN_DEBUG
5992                        "qla1280_getbyte: address = 0x%p, data = 0x%x", port,
5993                        data);
5994 }
5995
5996 /*
5997  *  Send double word to I/O port
5998  */
5999 static void
6000 qla1280_putdword(u32 * port, u32 data)
6001 {
6002 #if MEMORY_MAPPED_IO
6003         writel(data, (unsigned long) port);
6004 #else
6005         outl(data, (unsigned long) port);
6006 #endif
6007
6008         if (ql_debug_print)
6009                 printk(KERN_DEBUG
6010                        "qla1280_getbyte: address = 0x%p, data = 0x%x", port,
6011                        data);
6012 }
6013
6014 /*
6015  * Dummy function to prevent warnings for
6016  * declared and unused debug functions
6017  */
6018 static void
6019 qla1280_debug(void)
6020 {
6021         qla1280_getbyte(0);
6022         qla1280_getword(0);
6023         qla1280_getdword(0);
6024         qla1280_putbyte(0, 0);
6025         qla1280_putword(0, 0);
6026         qla1280_putdword(0, 0);
6027 }
6028
6029 static void
6030 __qla1280_dump_buffer(char *b, int size)
6031 {
6032         int cnt;
6033         u8 c;
6034
6035         if (ql_debug_print) {
6036                 printk(KERN_DEBUG
6037                        " 0   1   2   3   4   5   6   7   8   9   Ah  "
6038                        "Bh  Ch  Dh  Eh  Fh\n");
6039                 printk(KERN_DEBUG
6040                        "---------------------------------------------"
6041                        "------------------\n");
6042
6043                 for (cnt = 0; cnt < size;) {
6044                         c = *b++;
6045                         if (c < 16)
6046                                 printk(' ');
6047                         printk("0x%x", c);
6048                         cnt++;
6049                         if (!(cnt % 16))
6050                                 printk("\n");
6051                         else if (c < 10)
6052                                 printk("  ");
6053                         else
6054                                 printk(' ');
6055                 }
6056                 if (cnt % 16)
6057                         printk("\n");
6058         }
6059 }
6060
6061 /**************************************************************************
6062  *   ql1280_print_scsi_cmd
6063  *
6064  **************************************************************************/
6065 static void
6066 qla1280_print_scsi_cmd(Scsi_Cmnd * cmd)
6067 {
6068         struct scsi_qla_host *ha;
6069         struct Scsi_Host *host = cmd->host;
6070         srb_t *sp;
6071         /* struct scatterlist *sg; */
6072
6073         int i;
6074         ha = (struct scsi_qla_host *)host->hostdata;
6075
6076         sp = (srb_t *)CMD_SP(cmd);
6077         printk("SCSI Command @= 0x%p, Handle=0x%p\n", cmd, CMD_HANDLE(cmd));
6078         printk("  chan=%d, target = 0x%02x, lun = 0x%02x, cmd_len = 0x%02x\n",
6079                cmd->channel, cmd->target, cmd->lun, cmd->cmd_len);
6080         printk(" CDB = ");
6081         for (i = 0; i < cmd->cmd_len; i++) {
6082                 printk("0x%02x ", cmd->cmnd[i]);
6083         }
6084         printk("  seg_cnt =%d\n", cmd->use_sg);
6085         printk("  request buffer=0x%p, request buffer len=0x%x\n",
6086                cmd->request_buffer, cmd->request_bufflen);
6087         /* if (cmd->use_sg)
6088            {
6089            sg = (struct scatterlist *) cmd->request_buffer;
6090            printk("  SG buffer: \n");
6091            qla1280_dump_buffer(1, (char *)sg, (cmd->use_sg*sizeof(struct scatterlist)));
6092            } */
6093         printk("  tag=%d, flags=0x%x, transfersize=0x%x \n",
6094                cmd->tag, cmd->flags, cmd->transfersize);
6095         printk("  Pid=%li, SP=0x%p\n", cmd->pid, CMD_SP(cmd));
6096         printk(" underflow size = 0x%x, direction=0x%x, req.cmd=0x%x \n",
6097                cmd->underflow, sp->dir, cmd->request->cmd);
6098 }
6099
6100 /**************************************************************************
6101  *   ql1280_dump_device
6102  *
6103  **************************************************************************/
6104 void
6105 ql1280_dump_device(struct scsi_qla_host *ha)
6106 {
6107
6108         Scsi_Cmnd *cp;
6109         srb_t *sp;
6110         int i;
6111         if (ql_debug_print) {
6112                 printk(KERN_DEBUG "Outstanding Commands on controller:\n");
6113
6114                 for (i = 0; i < MAX_OUTSTANDING_COMMANDS; i++) {
6115                         if ((sp = ha->outstanding_cmds[i]) == NULL)
6116                                 continue;
6117                         if ((cp = sp->cmd) == NULL)
6118                                 continue;
6119                         qla1280_print_scsi_cmd(1, cp);
6120                 }
6121         }
6122 }
6123 #endif
6124
6125 #if STOP_ON_ERROR
6126 /**************************************************************************
6127  *   ql1280_panic
6128  *
6129  **************************************************************************/
6130 static void
6131 qla1280_panic(char *cp, struct Scsi_Host *host)
6132 {
6133         struct scsi_qla_host *ha;
6134         long *fp;
6135
6136         ha = (struct scsi_qla_host *)host->hostdata;
6137         printk(KERN_ERR "qla1280 - PANIC:  %s\n", cp);
6138         printk(KERN_ERR "Current time=0x%lx\n", jiffies);
6139         printk(KERN_ERR "Number of pending commands =0x%lx\n", ha->actthreads);
6140         printk(KERN_ERR "Number of SCSI queued commands =0x%lx\n",
6141                ha->qthreads);
6142         printk(KERN_ERR "Number of free entries = (%d)\n", ha->req_q_cnt);
6143         printk(KERN_ERR "Request Queue @ 0x%lx, Response Queue @ 0x%lx\n",
6144                ha->request_dma, ha->response_dma);
6145         printk(KERN_ERR "Request In Ptr %d\n", ha->req_ring_index);
6146         fp = (long *) &ha->flags;
6147         printk(KERN_ERR "HA flags =0x%lx\n", *fp);
6148         if (ql_debug_level >= 2) {
6149                 ql_debug_print = 1;
6150 #if 0
6151                 ql1280_dump_device((struct scsi_qla_host *)host->hostdata);
6152 #endif
6153         }
6154         sti();
6155         panic("Ooops");
6156         /* cli();
6157            for(;;)
6158            {
6159            barrier();
6160            sti();
6161            }
6162         */
6163 }
6164 #endif
6165
6166 #ifdef MODULE
6167 /**************************************************************************
6168  *   qla1280_setup
6169  *
6170  *   Handle Linux boot parameters. This routine allows for assigning a value
6171  *   to a parameter with a ':' between the parameter and the value.
6172  *   ie. qla1280=max_reqs:0x0A,verbose
6173  **************************************************************************/
6174 void
6175 qla1280_setup(char *s, int *dummy)
6176 {
6177         char *end, *str, *cp;
6178
6179         printk(KERN_INFO "scsi: Processing Option str = %s\n", s);
6180         end = strchr (s, '\0');
6181         /* locate command */
6182         str = s;
6183         for (cp = s; *cp && cp != end; cp++) {
6184                 cp = qla1280_get_token(cp, str);
6185                 printk(KERN_INFO "scsi: token str = %s\n", str);
6186                 /* if found execute routine */
6187         }
6188 }
6189
6190 static char *
6191 qla1280_get_token(char *cmdline, char *str)
6192 {
6193         register char *cp = cmdline;
6194
6195         /* skip preceeding spaces */
6196         while (strchr (cp, ' '))
6197                 ++cp;
6198         /* symbol starts here */
6199         str = cp;
6200         /* skip char if not a space or : */
6201         while (*cp && !(strchr (cp, ' ') || strchr (cp, ':')))
6202                 cp++;
6203         *cp = '\0';
6204         return cp;
6205 }
6206 #endif
6207
6208 /*
6209  * Overrides for Emacs so that we almost follow Linus's tabbing style.
6210  * Emacs will notice this stuff at the end of the file and automatically
6211  * adjust the settings for this buffer only.  This must remain at the end
6212  * of the file.
6213  * ---------------------------------------------------------------------------
6214  * Local variables:
6215  * c-basic-offset: 8
6216  * tab-width: 8
6217  * End:
6218  */