Linux Audio

Check our new training course

Buildroot integration, development and maintenance

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