PageRenderTime 85ms CodeModel.GetById 17ms app.highlight 51ms RepoModel.GetById 1ms app.codeStats 1ms

/drivers/scsi/qla1280.c

http://github.com/mirrors/linux
C | 4425 lines | 2753 code | 630 blank | 1042 comment | 401 complexity | f8ede82520affdcda53b6acedfcd0c18 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   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/*
 481 * We use the scsi_pointer structure that's included with each scsi_command
 482 * to overlay our struct srb over it. qla1280_init() checks that a srb is not
 483 * bigger than a scsi_pointer.
 484 */
 485
 486#define	CMD_SP(Cmnd)		&Cmnd->SCp
 487#define	CMD_CDBLEN(Cmnd)	Cmnd->cmd_len
 488#define	CMD_CDBP(Cmnd)		Cmnd->cmnd
 489#define	CMD_SNSP(Cmnd)		Cmnd->sense_buffer
 490#define	CMD_SNSLEN(Cmnd)	SCSI_SENSE_BUFFERSIZE
 491#define	CMD_RESULT(Cmnd)	Cmnd->result
 492#define	CMD_HANDLE(Cmnd)	Cmnd->host_scribble
 493#define CMD_REQUEST(Cmnd)	Cmnd->request->cmd
 494
 495#define CMD_HOST(Cmnd)		Cmnd->device->host
 496#define SCSI_BUS_32(Cmnd)	Cmnd->device->channel
 497#define SCSI_TCN_32(Cmnd)	Cmnd->device->id
 498#define SCSI_LUN_32(Cmnd)	Cmnd->device->lun
 499
 500
 501/*****************************************/
 502/*   ISP Boards supported by this driver */
 503/*****************************************/
 504
 505struct qla_boards {
 506	char *name;		/* Board ID String */
 507	int numPorts;		/* Number of SCSI ports */
 508	int fw_index;		/* index into qla1280_fw_tbl for firmware */
 509};
 510
 511/* NOTE: the last argument in each entry is used to index ql1280_board_tbl */
 512static struct pci_device_id qla1280_pci_tbl[] = {
 513	{PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP12160,
 514		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
 515	{PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1020,
 516		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
 517	{PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1080,
 518		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
 519	{PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1240,
 520		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3},
 521	{PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1280,
 522		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4},
 523	{PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP10160,
 524		PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5},
 525	{0,}
 526};
 527MODULE_DEVICE_TABLE(pci, qla1280_pci_tbl);
 528
 529DEFINE_MUTEX(qla1280_firmware_mutex);
 530
 531struct qla_fw {
 532	char *fwname;
 533	const struct firmware *fw;
 534};
 535
 536#define QL_NUM_FW_IMAGES 3
 537
 538struct qla_fw qla1280_fw_tbl[QL_NUM_FW_IMAGES] = {
 539	{"qlogic/1040.bin",  NULL},	/* image 0 */
 540	{"qlogic/1280.bin",  NULL},	/* image 1 */
 541	{"qlogic/12160.bin", NULL},	/* image 2 */
 542};
 543
 544/* NOTE: Order of boards in this table must match order in qla1280_pci_tbl */
 545static struct qla_boards ql1280_board_tbl[] = {
 546	{.name = "QLA12160", .numPorts = 2, .fw_index = 2},
 547	{.name = "QLA1040" , .numPorts = 1, .fw_index = 0},
 548	{.name = "QLA1080" , .numPorts = 1, .fw_index = 1},
 549	{.name = "QLA1240" , .numPorts = 2, .fw_index = 1},
 550	{.name = "QLA1280" , .numPorts = 2, .fw_index = 1},
 551	{.name = "QLA10160", .numPorts = 1, .fw_index = 2},
 552	{.name = "        ", .numPorts = 0, .fw_index = -1},
 553};
 554
 555static int qla1280_verbose = 1;
 556
 557#if DEBUG_QLA1280
 558static int ql_debug_level = 1;
 559#define dprintk(level, format, a...)	\
 560	do { if (ql_debug_level >= level) printk(KERN_ERR format, ##a); } while(0)
 561#define qla1280_dump_buffer(level, buf, size)	\
 562	if (ql_debug_level >= level) __qla1280_dump_buffer(buf, size)
 563#define qla1280_print_scsi_cmd(level, cmd)	\
 564	if (ql_debug_level >= level) __qla1280_print_scsi_cmd(cmd)
 565#else
 566#define ql_debug_level			0
 567#define dprintk(level, format, a...)	do{}while(0)
 568#define qla1280_dump_buffer(a, b, c)	do{}while(0)
 569#define qla1280_print_scsi_cmd(a, b)	do{}while(0)
 570#endif
 571
 572#define ENTER(x)		dprintk(3, "qla1280 : Entering %s()\n", x);
 573#define LEAVE(x)		dprintk(3, "qla1280 : Leaving %s()\n", x);
 574#define ENTER_INTR(x)		dprintk(4, "qla1280 : Entering %s()\n", x);
 575#define LEAVE_INTR(x)		dprintk(4, "qla1280 : Leaving %s()\n", x);
 576
 577
 578static int qla1280_read_nvram(struct scsi_qla_host *ha)
 579{
 580	uint16_t *wptr;
 581	uint8_t chksum;
 582	int cnt, i;
 583	struct nvram *nv;
 584
 585	ENTER("qla1280_read_nvram");
 586
 587	if (driver_setup.no_nvram)
 588		return 1;
 589
 590	printk(KERN_INFO "scsi(%ld): Reading NVRAM\n", ha->host_no);
 591
 592	wptr = (uint16_t *)&ha->nvram;
 593	nv = &ha->nvram;
 594	chksum = 0;
 595	for (cnt = 0; cnt < 3; cnt++) {
 596		*wptr = qla1280_get_nvram_word(ha, cnt);
 597		chksum += *wptr & 0xff;
 598		chksum += (*wptr >> 8) & 0xff;
 599		wptr++;
 600	}
 601
 602	if (nv->id0 != 'I' || nv->id1 != 'S' ||
 603	    nv->id2 != 'P' || nv->id3 != ' ' || nv->version < 1) {
 604		dprintk(2, "Invalid nvram ID or version!\n");
 605		chksum = 1;
 606	} else {
 607		for (; cnt < sizeof(struct nvram); cnt++) {
 608			*wptr = qla1280_get_nvram_word(ha, cnt);
 609			chksum += *wptr & 0xff;
 610			chksum += (*wptr >> 8) & 0xff;
 611			wptr++;
 612		}
 613	}
 614
 615	dprintk(3, "qla1280_read_nvram: NVRAM Magic ID= %c %c %c %02x"
 616	       " version %i\n", nv->id0, nv->id1, nv->id2, nv->id3,
 617	       nv->version);
 618
 619
 620	if (chksum) {
 621		if (!driver_setup.no_nvram)
 622			printk(KERN_WARNING "scsi(%ld): Unable to identify or "
 623			       "validate NVRAM checksum, using default "
 624			       "settings\n", ha->host_no);
 625		ha->nvram_valid = 0;
 626	} else
 627		ha->nvram_valid = 1;
 628
 629	/* The firmware interface is, um, interesting, in that the
 630	 * actual firmware image on the chip is little endian, thus,
 631	 * the process of taking that image to the CPU would end up
 632	 * little endian.  However, the firmware interface requires it
 633	 * to be read a word (two bytes) at a time.
 634	 *
 635	 * The net result of this would be that the word (and
 636	 * doubleword) quantites in the firmware would be correct, but
 637	 * the bytes would be pairwise reversed.  Since most of the
 638	 * firmware quantites are, in fact, bytes, we do an extra
 639	 * le16_to_cpu() in the firmware read routine.
 640	 *
 641	 * The upshot of all this is that the bytes in the firmware
 642	 * are in the correct places, but the 16 and 32 bit quantites
 643	 * are still in little endian format.  We fix that up below by
 644	 * doing extra reverses on them */
 645	nv->isp_parameter = cpu_to_le16(nv->isp_parameter);
 646	nv->firmware_feature.w = cpu_to_le16(nv->firmware_feature.w);
 647	for(i = 0; i < MAX_BUSES; i++) {
 648		nv->bus[i].selection_timeout = cpu_to_le16(nv->bus[i].selection_timeout);
 649		nv->bus[i].max_queue_depth = cpu_to_le16(nv->bus[i].max_queue_depth);
 650	}
 651	dprintk(1, "qla1280_read_nvram: Completed Reading NVRAM\n");
 652	LEAVE("qla1280_read_nvram");
 653
 654	return chksum;
 655}
 656
 657/**************************************************************************
 658 *   qla1280_info
 659 *     Return a string describing the driver.
 660 **************************************************************************/
 661static const char *
 662qla1280_info(struct Scsi_Host *host)
 663{
 664	static char qla1280_scsi_name_buffer[125];
 665	char *bp;
 666	struct scsi_qla_host *ha;
 667	struct qla_boards *bdp;
 668
 669	bp = &qla1280_scsi_name_buffer[0];
 670	ha = (struct scsi_qla_host *)host->hostdata;
 671	bdp = &ql1280_board_tbl[ha->devnum];
 672	memset(bp, 0, sizeof(qla1280_scsi_name_buffer));
 673
 674	sprintf (bp,
 675		 "QLogic %s PCI to SCSI Host Adapter\n"
 676		 "       Firmware version: %2d.%02d.%02d, Driver version %s",
 677		 &bdp->name[0], ha->fwver1, ha->fwver2, ha->fwver3,
 678		 QLA1280_VERSION);
 679	return bp;
 680}
 681
 682/**************************************************************************
 683 *   qla1280_queuecommand
 684 *     Queue a command to the controller.
 685 *
 686 * Note:
 687 * The mid-level driver tries to ensures that queuecommand never gets invoked
 688 * concurrently with itself or the interrupt handler (although the
 689 * interrupt handler may call this routine as part of request-completion
 690 * handling).   Unfortunely, it sometimes calls the scheduler in interrupt
 691 * context which is a big NO! NO!.
 692 **************************************************************************/
 693static int
 694qla1280_queuecommand_lck(struct scsi_cmnd *cmd, void (*fn)(struct scsi_cmnd *))
 695{
 696	struct Scsi_Host *host = cmd->device->host;
 697	struct scsi_qla_host *ha = (struct scsi_qla_host *)host->hostdata;
 698	struct srb *sp = (struct srb *)CMD_SP(cmd);
 699	int status;
 700
 701	cmd->scsi_done = fn;
 702	sp->cmd = cmd;
 703	sp->flags = 0;
 704	sp->wait = NULL;
 705	CMD_HANDLE(cmd) = (unsigned char *)NULL;
 706
 707	qla1280_print_scsi_cmd(5, cmd);
 708
 709#ifdef QLA_64BIT_PTR
 710	/*
 711	 * Using 64 bit commands if the PCI bridge doesn't support it is a
 712	 * bit wasteful, however this should really only happen if one's
 713	 * PCI controller is completely broken, like the BCM1250. For
 714	 * sane hardware this is not an issue.
 715	 */
 716	status = qla1280_64bit_start_scsi(ha, sp);
 717#else
 718	status = qla1280_32bit_start_scsi(ha, sp);
 719#endif
 720	return status;
 721}
 722
 723static DEF_SCSI_QCMD(qla1280_queuecommand)
 724
 725enum action {
 726	ABORT_COMMAND,
 727	DEVICE_RESET,
 728	BUS_RESET,
 729	ADAPTER_RESET,
 730};
 731
 732
 733static void qla1280_mailbox_timeout(struct timer_list *t)
 734{
 735	struct scsi_qla_host *ha = from_timer(ha, t, mailbox_timer);
 736	struct device_reg __iomem *reg;
 737	reg = ha->iobase;
 738
 739	ha->mailbox_out[0] = RD_REG_WORD(&reg->mailbox0);
 740	printk(KERN_ERR "scsi(%ld): mailbox timed out, mailbox0 %04x, "
 741	       "ictrl %04x, istatus %04x\n", ha->host_no, ha->mailbox_out[0],
 742	       RD_REG_WORD(&reg->ictrl), RD_REG_WORD(&reg->istatus));
 743	complete(ha->mailbox_wait);
 744}
 745
 746static int
 747_qla1280_wait_for_single_command(struct scsi_qla_host *ha, struct srb *sp,
 748				 struct completion *wait)
 749{
 750	int	status = FAILED;
 751	struct scsi_cmnd *cmd = sp->cmd;
 752
 753	spin_unlock_irq(ha->host->host_lock);
 754	wait_for_completion_timeout(wait, 4*HZ);
 755	spin_lock_irq(ha->host->host_lock);
 756	sp->wait = NULL;
 757	if(CMD_HANDLE(cmd) == COMPLETED_HANDLE) {
 758		status = SUCCESS;
 759		(*cmd->scsi_done)(cmd);
 760	}
 761	return status;
 762}
 763
 764static int
 765qla1280_wait_for_single_command(struct scsi_qla_host *ha, struct srb *sp)
 766{
 767	DECLARE_COMPLETION_ONSTACK(wait);
 768
 769	sp->wait = &wait;
 770	return _qla1280_wait_for_single_command(ha, sp, &wait);
 771}
 772
 773static int
 774qla1280_wait_for_pending_commands(struct scsi_qla_host *ha, int bus, int target)
 775{
 776	int		cnt;
 777	int		status;
 778	struct srb	*sp;
 779	struct scsi_cmnd *cmd;
 780
 781	status = SUCCESS;
 782
 783	/*
 784	 * Wait for all commands with the designated bus/target
 785	 * to be completed by the firmware
 786	 */
 787	for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
 788		sp = ha->outstanding_cmds[cnt];
 789		if (sp) {
 790			cmd = sp->cmd;
 791
 792			if (bus >= 0 && SCSI_BUS_32(cmd) != bus)
 793				continue;
 794			if (target >= 0 && SCSI_TCN_32(cmd) != target)
 795				continue;
 796
 797			status = qla1280_wait_for_single_command(ha, sp);
 798			if (status == FAILED)
 799				break;
 800		}
 801	}
 802	return status;
 803}
 804
 805/**************************************************************************
 806 * qla1280_error_action
 807 *    The function will attempt to perform a specified error action and
 808 *    wait for the results (or time out).
 809 *
 810 * Input:
 811 *      cmd = Linux SCSI command packet of the command that cause the
 812 *            bus reset.
 813 *      action = error action to take (see action_t)
 814 *
 815 * Returns:
 816 *      SUCCESS or FAILED
 817 *
 818 **************************************************************************/
 819static int
 820qla1280_error_action(struct scsi_cmnd *cmd, enum action action)
 821{
 822	struct scsi_qla_host *ha;
 823	int bus, target, lun;
 824	struct srb *sp;
 825	int i, found;
 826	int result=FAILED;
 827	int wait_for_bus=-1;
 828	int wait_for_target = -1;
 829	DECLARE_COMPLETION_ONSTACK(wait);
 830
 831	ENTER("qla1280_error_action");
 832
 833	ha = (struct scsi_qla_host *)(CMD_HOST(cmd)->hostdata);
 834	sp = (struct srb *)CMD_SP(cmd);
 835	bus = SCSI_BUS_32(cmd);
 836	target = SCSI_TCN_32(cmd);
 837	lun = SCSI_LUN_32(cmd);
 838
 839	dprintk(4, "error_action %i, istatus 0x%04x\n", action,
 840		RD_REG_WORD(&ha->iobase->istatus));
 841
 842	dprintk(4, "host_cmd 0x%04x, ictrl 0x%04x, jiffies %li\n",
 843		RD_REG_WORD(&ha->iobase->host_cmd),
 844		RD_REG_WORD(&ha->iobase->ictrl), jiffies);
 845
 846	if (qla1280_verbose)
 847		printk(KERN_INFO "scsi(%li): Resetting Cmnd=0x%p, "
 848		       "Handle=0x%p, action=0x%x\n",
 849		       ha->host_no, cmd, CMD_HANDLE(cmd), action);
 850
 851	/*
 852	 * Check to see if we have the command in the outstanding_cmds[]
 853	 * array.  If not then it must have completed before this error
 854	 * action was initiated.  If the error_action isn't ABORT_COMMAND
 855	 * then the driver must proceed with the requested action.
 856	 */
 857	found = -1;
 858	for (i = 0; i < MAX_OUTSTANDING_COMMANDS; i++) {
 859		if (sp == ha->outstanding_cmds[i]) {
 860			found = i;
 861			sp->wait = &wait; /* we'll wait for it to complete */
 862			break;
 863		}
 864	}
 865
 866	if (found < 0) {	/* driver doesn't have command */
 867		result = SUCCESS;
 868		if (qla1280_verbose) {
 869			printk(KERN_INFO
 870			       "scsi(%ld:%d:%d:%d): specified command has "
 871			       "already completed.\n", ha->host_no, bus,
 872				target, lun);
 873		}
 874	}
 875
 876	switch (action) {
 877
 878	case ABORT_COMMAND:
 879		dprintk(1, "qla1280: RISC aborting command\n");
 880		/*
 881		 * The abort might fail due to race when the host_lock
 882		 * is released to issue the abort.  As such, we
 883		 * don't bother to check the return status.
 884		 */
 885		if (found >= 0)
 886			qla1280_abort_command(ha, sp, found);
 887		break;
 888
 889	case DEVICE_RESET:
 890		if (qla1280_verbose)
 891			printk(KERN_INFO
 892			       "scsi(%ld:%d:%d:%d): Queueing device reset "
 893			       "command.\n", ha->host_no, bus, target, lun);
 894		if (qla1280_device_reset(ha, bus, target) == 0) {
 895			/* issued device reset, set wait conditions */
 896			wait_for_bus = bus;
 897			wait_for_target = target;
 898		}
 899		break;
 900
 901	case BUS_RESET:
 902		if (qla1280_verbose)
 903			printk(KERN_INFO "qla1280(%ld:%d): Issued bus "
 904			       "reset.\n", ha->host_no, bus);
 905		if (qla1280_bus_reset(ha, bus) == 0) {
 906			/* issued bus reset, set wait conditions */
 907			wait_for_bus = bus;
 908		}
 909		break;
 910
 911	case ADAPTER_RESET:
 912	default:
 913		if (qla1280_verbose) {
 914			printk(KERN_INFO
 915			       "scsi(%ld): Issued ADAPTER RESET\n",
 916			       ha->host_no);
 917			printk(KERN_INFO "scsi(%ld): I/O processing will "
 918			       "continue automatically\n", ha->host_no);
 919		}
 920		ha->flags.reset_active = 1;
 921
 922		if (qla1280_abort_isp(ha) != 0) {	/* it's dead */
 923			result = FAILED;
 924		}
 925
 926		ha->flags.reset_active = 0;
 927	}
 928
 929	/*
 930	 * At this point, the host_lock has been released and retaken
 931	 * by the issuance of the mailbox command.
 932	 * Wait for the command passed in by the mid-layer if it
 933	 * was found by the driver.  It might have been returned
 934	 * between eh recovery steps, hence the check of the "found"
 935	 * variable.
 936	 */
 937
 938	if (found >= 0)
 939		result = _qla1280_wait_for_single_command(ha, sp, &wait);
 940
 941	if (action == ABORT_COMMAND && result != SUCCESS) {
 942		printk(KERN_WARNING
 943		       "scsi(%li:%i:%i:%i): "
 944		       "Unable to abort command!\n",
 945		       ha->host_no, bus, target, lun);
 946	}
 947
 948	/*
 949	 * If the command passed in by the mid-layer has been
 950	 * returned by the board, then wait for any additional
 951	 * commands which are supposed to complete based upon
 952	 * the error action.
 953	 *
 954	 * All commands are unconditionally returned during a
 955	 * call to qla1280_abort_isp(), ADAPTER_RESET.  No need
 956	 * to wait for them.
 957	 */
 958	if (result == SUCCESS && wait_for_bus >= 0) {
 959		result = qla1280_wait_for_pending_commands(ha,
 960					wait_for_bus, wait_for_target);
 961	}
 962
 963	dprintk(1, "RESET returning %d\n", result);
 964
 965	LEAVE("qla1280_error_action");
 966	return result;
 967}
 968
 969/**************************************************************************
 970 *   qla1280_abort
 971 *     Abort the specified SCSI command(s).
 972 **************************************************************************/
 973static int
 974qla1280_eh_abort(struct scsi_cmnd * cmd)
 975{
 976	int rc;
 977
 978	spin_lock_irq(cmd->device->host->host_lock);
 979	rc = qla1280_error_action(cmd, ABORT_COMMAND);
 980	spin_unlock_irq(cmd->device->host->host_lock);
 981
 982	return rc;
 983}
 984
 985/**************************************************************************
 986 *   qla1280_device_reset
 987 *     Reset the specified SCSI device
 988 **************************************************************************/
 989static int
 990qla1280_eh_device_reset(struct scsi_cmnd *cmd)
 991{
 992	int rc;
 993
 994	spin_lock_irq(cmd->device->host->host_lock);
 995	rc = qla1280_error_action(cmd, DEVICE_RESET);
 996	spin_unlock_irq(cmd->device->host->host_lock);
 997
 998	return rc;
 999}
1000
1001/**************************************************************************
1002 *   qla1280_bus_reset
1003 *     Reset the specified bus.
1004 **************************************************************************/
1005static int
1006qla1280_eh_bus_reset(struct scsi_cmnd *cmd)
1007{
1008	int rc;
1009
1010	spin_lock_irq(cmd->device->host->host_lock);
1011	rc = qla1280_error_action(cmd, BUS_RESET);
1012	spin_unlock_irq(cmd->device->host->host_lock);
1013
1014	return rc;
1015}
1016
1017/**************************************************************************
1018 *   qla1280_adapter_reset
1019 *     Reset the specified adapter (both channels)
1020 **************************************************************************/
1021static int
1022qla1280_eh_adapter_reset(struct scsi_cmnd *cmd)
1023{
1024	int rc;
1025
1026	spin_lock_irq(cmd->device->host->host_lock);
1027	rc = qla1280_error_action(cmd, ADAPTER_RESET);
1028	spin_unlock_irq(cmd->device->host->host_lock);
1029
1030	return rc;
1031}
1032
1033static int
1034qla1280_biosparam(struct scsi_device *sdev, struct block_device *bdev,
1035		  sector_t capacity, int geom[])
1036{
1037	int heads, sectors, cylinders;
1038
1039	heads = 64;
1040	sectors = 32;
1041	cylinders = (unsigned long)capacity / (heads * sectors);
1042	if (cylinders > 1024) {
1043		heads = 255;
1044		sectors = 63;
1045		cylinders = (unsigned long)capacity / (heads * sectors);
1046		/* if (cylinders > 1023)
1047		   cylinders = 1023; */
1048	}
1049
1050	geom[0] = heads;
1051	geom[1] = sectors;
1052	geom[2] = cylinders;
1053
1054	return 0;
1055}
1056
1057 
1058/* disable risc and host interrupts */
1059static inline void
1060qla1280_disable_intrs(struct scsi_qla_host *ha)
1061{
1062	WRT_REG_WORD(&ha->iobase->ictrl, 0);
1063	RD_REG_WORD(&ha->iobase->ictrl);	/* PCI Posted Write flush */
1064}
1065
1066/* enable risc and host interrupts */
1067static inline void
1068qla1280_enable_intrs(struct scsi_qla_host *ha)
1069{
1070	WRT_REG_WORD(&ha->iobase->ictrl, (ISP_EN_INT | ISP_EN_RISC));
1071	RD_REG_WORD(&ha->iobase->ictrl);	/* PCI Posted Write flush */
1072}
1073
1074/**************************************************************************
1075 * qla1280_intr_handler
1076 *   Handles the H/W interrupt
1077 **************************************************************************/
1078static irqreturn_t
1079qla1280_intr_handler(int irq, void *dev_id)
1080{
1081	struct scsi_qla_host *ha;
1082	struct device_reg __iomem *reg;
1083	u16 data;
1084	int handled = 0;
1085
1086	ENTER_INTR ("qla1280_intr_handler");
1087	ha = (struct scsi_qla_host *)dev_id;
1088
1089	spin_lock(ha->host->host_lock);
1090
1091	ha->isr_count++;
1092	reg = ha->iobase;
1093
1094	qla1280_disable_intrs(ha);
1095
1096	data = qla1280_debounce_register(&reg->istatus);
1097	/* Check for pending interrupts. */
1098	if (data & RISC_INT) {	
1099		qla1280_isr(ha, &ha->done_q);
1100		handled = 1;
1101	}
1102	if (!list_empty(&ha->done_q))
1103		qla1280_done(ha);
1104
1105	spin_unlock(ha->host->host_lock);
1106
1107	qla1280_enable_intrs(ha);
1108
1109	LEAVE_INTR("qla1280_intr_handler");
1110	return IRQ_RETVAL(handled);
1111}
1112
1113
1114static int
1115qla1280_set_target_parameters(struct scsi_qla_host *ha, int bus, int target)
1116{
1117	uint8_t mr;
1118	uint16_t mb[MAILBOX_REGISTER_COUNT];
1119	struct nvram *nv;
1120	int status, lun;
1121
1122	nv = &ha->nvram;
1123
1124	mr = BIT_3 | BIT_2 | BIT_1 | BIT_0;
1125
1126	/* Set Target Parameters. */
1127	mb[0] = MBC_SET_TARGET_PARAMETERS;
1128	mb[1] = (uint16_t)((bus ? target | BIT_7 : target) << 8);
1129	mb[2] = nv->bus[bus].target[target].parameter.renegotiate_on_error << 8;
1130	mb[2] |= nv->bus[bus].target[target].parameter.stop_queue_on_check << 9;
1131	mb[2] |= nv->bus[bus].target[target].parameter.auto_request_sense << 10;
1132	mb[2] |= nv->bus[bus].target[target].parameter.tag_queuing << 11;
1133	mb[2] |= nv->bus[bus].target[target].parameter.enable_sync << 12;
1134	mb[2] |= nv->bus[bus].target[target].parameter.enable_wide << 13;
1135	mb[2] |= nv->bus[bus].target[target].parameter.parity_checking << 14;
1136	mb[2] |= nv->bus[bus].target[target].parameter.disconnect_allowed << 15;
1137
1138	if (IS_ISP1x160(ha)) {
1139		mb[2] |= nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr << 5;
1140		mb[3] =	(nv->bus[bus].target[target].flags.flags1x160.sync_offset << 8);
1141		mb[6] =	(nv->bus[bus].target[target].ppr_1x160.flags.ppr_options << 8) |
1142			 nv->bus[bus].target[target].ppr_1x160.flags.ppr_bus_width;
1143		mr |= BIT_6;
1144	} else {
1145		mb[3] =	(nv->bus[bus].target[target].flags.flags1x80.sync_offset << 8);
1146	}
1147	mb[3] |= nv->bus[bus].target[target].sync_period;
1148
1149	status = qla1280_mailbox_command(ha, mr, mb);
1150
1151	/* Set Device Queue Parameters. */
1152	for (lun = 0; lun < MAX_LUNS; lun++) {
1153		mb[0] = MBC_SET_DEVICE_QUEUE;
1154		mb[1] = (uint16_t)((bus ? target | BIT_7 : target) << 8);
1155		mb[1] |= lun;
1156		mb[2] = nv->bus[bus].max_queue_depth;
1157		mb[3] = nv->bus[bus].target[target].execution_throttle;
1158		status |= qla1280_mailbox_command(ha, 0x0f, mb);
1159	}
1160
1161	if (status)
1162		printk(KERN_WARNING "scsi(%ld:%i:%i): "
1163		       "qla1280_set_target_parameters() failed\n",
1164		       ha->host_no, bus, target);
1165	return status;
1166}
1167
1168
1169/**************************************************************************
1170 *   qla1280_slave_configure
1171 *
1172 * Description:
1173 *   Determines the queue depth for a given device.  There are two ways
1174 *   a queue depth can be obtained for a tagged queueing device.  One
1175 *   way is the default queue depth which is determined by whether
1176 *   If it is defined, then it is used
1177 *   as the default queue depth.  Otherwise, we use either 4 or 8 as the
1178 *   default queue depth (dependent on the number of hardware SCBs).
1179 **************************************************************************/
1180static int
1181qla1280_slave_configure(struct scsi_device *device)
1182{
1183	struct scsi_qla_host *ha;
1184	int default_depth = 3;
1185	int bus = device->channel;
1186	int target = device->id;
1187	int status = 0;
1188	struct nvram *nv;
1189	unsigned long flags;
1190
1191	ha = (struct scsi_qla_host *)device->host->hostdata;
1192	nv = &ha->nvram;
1193
1194	if (qla1280_check_for_dead_scsi_bus(ha, bus))
1195		return 1;
1196
1197	if (device->tagged_supported &&
1198	    (ha->bus_settings[bus].qtag_enables & (BIT_0 << target))) {
1199		scsi_change_queue_depth(device, ha->bus_settings[bus].hiwat);
1200	} else {
1201		scsi_change_queue_depth(device, default_depth);
1202	}
1203
1204	nv->bus[bus].target[target].parameter.enable_sync = device->sdtr;
1205	nv->bus[bus].target[target].parameter.enable_wide = device->wdtr;
1206	nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr = device->ppr;
1207
1208	if (driver_setup.no_sync ||
1209	    (driver_setup.sync_mask &&
1210	     (~driver_setup.sync_mask & (1 << target))))
1211		nv->bus[bus].target[target].parameter.enable_sync = 0;
1212	if (driver_setup.no_wide ||
1213	    (driver_setup.wide_mask &&
1214	     (~driver_setup.wide_mask & (1 << target))))
1215		nv->bus[bus].target[target].parameter.enable_wide = 0;
1216	if (IS_ISP1x160(ha)) {
1217		if (driver_setup.no_ppr ||
1218		    (driver_setup.ppr_mask &&
1219		     (~driver_setup.ppr_mask & (1 << target))))
1220			nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr = 0;
1221	}
1222
1223	spin_lock_irqsave(ha->host->host_lock, flags);
1224	if (nv->bus[bus].target[target].parameter.enable_sync)
1225		status = qla1280_set_target_parameters(ha, bus, target);
1226	qla1280_get_target_parameters(ha, device);
1227	spin_unlock_irqrestore(ha->host->host_lock, flags);
1228	return status;
1229}
1230
1231
1232/*
1233 * qla1280_done
1234 *      Process completed commands.
1235 *
1236 * Input:
1237 *      ha           = adapter block pointer.
1238 */
1239static void
1240qla1280_done(struct scsi_qla_host *ha)
1241{
1242	struct srb *sp;
1243	struct list_head *done_q;
1244	int bus, target, lun;
1245	struct scsi_cmnd *cmd;
1246
1247	ENTER("qla1280_done");
1248
1249	done_q = &ha->done_q;
1250
1251	while (!list_empty(done_q)) {
1252		sp = list_entry(done_q->next, struct srb, list);
1253
1254		list_del(&sp->list);
1255	
1256		cmd = sp->cmd;
1257		bus = SCSI_BUS_32(cmd);
1258		target = SCSI_TCN_32(cmd);
1259		lun = SCSI_LUN_32(cmd);
1260
1261		switch ((CMD_RESULT(cmd) >> 16)) {
1262		case DID_RESET:
1263			/* Issue marker command. */
1264			if (!ha->flags.abort_isp_active)
1265				qla1280_marker(ha, bus, target, 0, MK_SYNC_ID);
1266			break;
1267		case DID_ABORT:
1268			sp->flags &= ~SRB_ABORT_PENDING;
1269			sp->flags |= SRB_ABORTED;
1270			break;
1271		default:
1272			break;
1273		}
1274
1275		/* Release memory used for this I/O */
1276		scsi_dma_unmap(cmd);
1277
1278		/* Call the mid-level driver interrupt handler */
1279		ha->actthreads--;
1280
1281		if (sp->wait == NULL)
1282			(*(cmd)->scsi_done)(cmd);
1283		else
1284			complete(sp->wait);
1285	}
1286	LEAVE("qla1280_done");
1287}
1288
1289/*
1290 * Translates a ISP error to a Linux SCSI error
1291 */
1292static int
1293qla1280_return_status(struct response * sts, struct scsi_cmnd *cp)
1294{
1295	int host_status = DID_ERROR;
1296	uint16_t comp_status = le16_to_cpu(sts->comp_status);
1297	uint16_t state_flags = le16_to_cpu(sts->state_flags);
1298	uint32_t residual_length = le32_to_cpu(sts->residual_length);
1299	uint16_t scsi_status = le16_to_cpu(sts->scsi_status);
1300#if DEBUG_QLA1280_INTR
1301	static char *reason[] = {
1302		"DID_OK",
1303		"DID_NO_CONNECT",
1304		"DID_BUS_BUSY",
1305		"DID_TIME_OUT",
1306		"DID_BAD_TARGET",
1307		"DID_ABORT",
1308		"DID_PARITY",
1309		"DID_ERROR",
1310		"DID_RESET",
1311		"DID_BAD_INTR"
1312	};
1313#endif				/* DEBUG_QLA1280_INTR */
1314
1315	ENTER("qla1280_return_status");
1316
1317#if DEBUG_QLA1280_INTR
1318	/*
1319	  dprintk(1, "qla1280_return_status: compl status = 0x%04x\n",
1320	  comp_status);
1321	*/
1322#endif
1323
1324	switch (comp_status) {
1325	case CS_COMPLETE:
1326		host_status = DID_OK;
1327		break;
1328
1329	case CS_INCOMPLETE:
1330		if (!(state_flags & SF_GOT_BUS))
1331			host_status = DID_NO_CONNECT;
1332		else if (!(state_flags & SF_GOT_TARGET))
1333			host_status = DID_BAD_TARGET;
1334		else if (!(state_flags & SF_SENT_CDB))
1335			host_status = DID_ERROR;
1336		else if (!(state_flags & SF_TRANSFERRED_DATA))
1337			host_status = DID_ERROR;
1338		else if (!(state_flags & SF_GOT_STATUS))
1339			host_status = DID_ERROR;
1340		else if (!(state_flags & SF_GOT_SENSE))
1341			host_status = DID_ERROR;
1342		break;
1343
1344	case CS_RESET:
1345		host_status = DID_RESET;
1346		break;
1347
1348	case CS_ABORTED:
1349		host_status = DID_ABORT;
1350		break;
1351
1352	case CS_TIMEOUT:
1353		host_status = DID_TIME_OUT;
1354		break;
1355
1356	case CS_DATA_OVERRUN:
1357		dprintk(2, "Data overrun 0x%x\n", residual_length);
1358		dprintk(2, "qla1280_return_status: response packet data\n");
1359		qla1280_dump_buffer(2, (char *)sts, RESPONSE_ENTRY_SIZE);
1360		host_status = DID_ERROR;
1361		break;
1362
1363	case CS_DATA_UNDERRUN:
1364		if ((scsi_bufflen(cp) - residual_length) <
1365		    cp->underflow) {
1366			printk(KERN_WARNING
1367			       "scsi: Underflow detected - retrying "
1368			       "command.\n");
1369			host_status = DID_ERROR;
1370		} else {
1371			scsi_set_resid(cp, residual_length);
1372			host_status = DID_OK;
1373		}
1374		break;
1375
1376	default:
1377		host_status = DID_ERROR;
1378		break;
1379	}
1380
1381#if DEBUG_QLA1280_INTR
1382	dprintk(1, "qla1280 ISP status: host status (%s) scsi status %x\n",
1383		reason[host_status], scsi_status);
1384#endif
1385
1386	LEAVE("qla1280_return_status");
1387
1388	return (scsi_status & 0xff) | (host_status << 16);
1389}
1390
1391/****************************************************************************/
1392/*                QLogic ISP1280 Hardware Support Functions.                */
1393/****************************************************************************/
1394
1395/*
1396 * qla1280_initialize_adapter
1397 *      Initialize board.
1398 *
1399 * Input:
1400 *      ha = adapter block pointer.
1401 *
1402 * Returns:
1403 *      0 = success
1404 */
1405static int
1406qla1280_initialize_adapter(struct scsi_qla_host *ha)
1407{
1408	struct device_reg __iomem *reg;
1409	int status;
1410	int bus;
1411	unsigned long flags;
1412
1413	ENTER("qla1280_initialize_adapter");
1414
1415	/* Clear adapter flags. */
1416	ha->flags.online = 0;
1417	ha->flags.disable_host_adapter = 0;
1418	ha->flags.reset_active = 0;
1419	ha->flags.abort_isp_active = 0;
1420
1421	/* TODO: implement support for the 1040 nvram format */
1422	if (IS_ISP1040(ha))
1423		driver_setup.no_nvram = 1;
1424
1425	dprintk(1, "Configure PCI space for adapter...\n");
1426
1427	reg = ha->iobase;
1428
1429	/* Insure mailbox registers are free. */
1430	WRT_REG_WORD(&reg->semaphore, 0);
1431	WRT_REG_WORD(&reg->host_cmd, HC_CLR_RISC_INT);
1432	WRT_REG_WORD(&reg->host_cmd, HC_CLR_HOST_INT);
1433	RD_REG_WORD(&reg->host_cmd);
1434
1435	if (qla1280_read_nvram(ha)) {
1436		dprintk(2, "qla1280_initialize_adapter: failed to read "
1437			"NVRAM\n");
1438	}
1439
1440	/*
1441	 * It's necessary to grab the spin here as qla1280_mailbox_command
1442	 * needs to be able to drop the lock unconditionally to wait
1443	 * for completion.
1444	 */
1445	spin_lock_irqsave(ha->host->host_lock, flags);
1446
1447	status = qla1280_load_firmware(ha);
1448	if (status) {
1449		printk(KERN_ERR "scsi(%li): initialize: pci probe failed!\n",
1450		       ha->host_no);
1451		goto out;
1452	}
1453
1454	/* Setup adapter based on NVRAM parameters. */
1455	dprintk(1, "scsi(%ld): Configure NVRAM parameters\n", ha->host_no);
1456	qla1280_nvram_config(ha);
1457
1458	if (ha->flags.disable_host_adapter) {
1459		status = 1;
1460		goto out;
1461	}
1462
1463	status = qla1280_init_rings(ha);
1464	if (status)
1465		goto out;
1466
1467	/* Issue SCSI reset, if we can't reset twice then bus is dead */
1468	for (bus = 0; bus < ha->ports; bus++) {
1469		if (!ha->bus_settings[bus].disable_scsi_reset &&
1470		    qla1280_bus_reset(ha, bus) &&
1471		    qla1280_bus_reset(ha, bus))
1472			ha->bus_settings[bus].scsi_bus_dead = 1;
1473	}
1474
1475	ha->flags.online = 1;
1476 out:
1477	spin_unlock_irqrestore(ha->host->host_lock, flags);
1478
1479	if (status)
1480		dprintk(2, "qla1280_initialize_adapter: **** FAILED ****\n");
1481
1482	LEAVE("qla1280_initialize_adapter");
1483	return status;
1484}
1485
1486/*
1487 * qla1280_request_firmware
1488 *      Acquire firmware for chip.  Retain in memory
1489 *      for error recovery.
1490 *
1491 * Input:
1492 *      ha = adapter block pointer.
1493 *
1494 * Returns:
1495 *      Pointer to firmware image or an error code
1496 *      cast to pointer via ERR_PTR().
1497 */
1498static const struct firmware *
1499qla1280_request_firmware(struct scsi_qla_host *ha)
1500{
1501	const struct firmware *fw;
1502	int err;
1503	int index;
1504	char *fwname;
1505
1506	spin_unlock_irq(ha->host->host_lock);
1507	mutex_lock(&qla1280_firmware_mutex);
1508
1509	index = ql1280_board_tbl[ha->devnum].fw_index;
1510	fw = qla1280_fw_tbl[index].fw;
1511	if (fw)
1512		goto out;
1513
1514	fwname = qla1280_fw_tbl[index].fwname;
1515	err = request_firmware(&fw, fwname, &ha->pdev->dev);
1516
1517	if (err) {
1518		printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
1519		       fwname, err);
1520		fw = ERR_PTR(err);
1521		goto unlock;
1522	}
1523	if ((fw->size % 2) || (fw->size < 6)) {
1524		printk(KERN_ERR "Invalid firmware length %zu in image \"%s\"\n",
1525		       fw->size, fwname);
1526		release_firmware(fw);
1527		fw = ERR_PTR(

Large files files are truncated, but you can click here to view the full file