File Index
Files and directories
Sign in
FILE

qla1280.c

File preview with syntax highlighting.

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