/drivers/scsi/qla1280.c

https://bitbucket.org/abioy/linux · C · 4504 lines · 2804 code · 634 blank · 1066 comment · 416 complexity · 28cf0447a57ce1a5edb885a2ef69de56 MD5 · raw file

Large files are truncated click here to view the full file

  1. /******************************************************************************
  2. * QLOGIC LINUX SOFTWARE
  3. *
  4. * QLogic QLA1280 (Ultra2) and QLA12160 (Ultra3) SCSI driver
  5. * Copyright (C) 2000 Qlogic Corporation (www.qlogic.com)
  6. * Copyright (C) 2001-2004 Jes Sorensen, Wild Open Source Inc.
  7. * Copyright (C) 2003-2004 Christoph Hellwig
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2, or (at your option) any
  12. * later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. ******************************************************************************/
  20. #define QLA1280_VERSION "3.27.1"
  21. /*****************************************************************************
  22. Revision History:
  23. Rev 3.27.1, February 8, 2010, Michael Reed
  24. - Retain firmware image for error recovery.
  25. Rev 3.27, February 10, 2009, Michael Reed
  26. - General code cleanup.
  27. - Improve error recovery.
  28. Rev 3.26, January 16, 2006 Jes Sorensen
  29. - Ditch all < 2.6 support
  30. Rev 3.25.1, February 10, 2005 Christoph Hellwig
  31. - use pci_map_single to map non-S/G requests
  32. - remove qla1280_proc_info
  33. Rev 3.25, September 28, 2004, Christoph Hellwig
  34. - add support for ISP1020/1040
  35. - don't include "scsi.h" anymore for 2.6.x
  36. Rev 3.24.4 June 7, 2004 Christoph Hellwig
  37. - restructure firmware loading, cleanup initialization code
  38. - prepare support for ISP1020/1040 chips
  39. Rev 3.24.3 January 19, 2004, Jes Sorensen
  40. - Handle PCI DMA mask settings correctly
  41. - Correct order of error handling in probe_one, free_irq should not
  42. be called if request_irq failed
  43. Rev 3.24.2 January 19, 2004, James Bottomley & Andrew Vasquez
  44. - Big endian fixes (James)
  45. - Remove bogus IOCB content on zero data transfer commands (Andrew)
  46. Rev 3.24.1 January 5, 2004, Jes Sorensen
  47. - Initialize completion queue to avoid OOPS on probe
  48. - Handle interrupts during mailbox testing
  49. Rev 3.24 November 17, 2003, Christoph Hellwig
  50. - use struct list_head for completion queue
  51. - avoid old Scsi_FOO typedefs
  52. - cleanup 2.4 compat glue a bit
  53. - use <scsi/scsi_*.h> headers on 2.6 instead of "scsi.h"
  54. - make initialization for memory mapped vs port I/O more similar
  55. - remove broken pci config space manipulation
  56. - kill more cruft
  57. - this is an almost perfect 2.6 scsi driver now! ;)
  58. Rev 3.23.39 December 17, 2003, Jes Sorensen
  59. - Delete completion queue from srb if mailbox command failed to
  60. to avoid qla1280_done completeting qla1280_error_action's
  61. obsolete context
  62. - Reduce arguments for qla1280_done
  63. Rev 3.23.38 October 18, 2003, Christoph Hellwig
  64. - Convert to new-style hotplugable driver for 2.6
  65. - Fix missing scsi_unregister/scsi_host_put on HBA removal
  66. - Kill some more cruft
  67. Rev 3.23.37 October 1, 2003, Jes Sorensen
  68. - Make MMIO depend on CONFIG_X86_VISWS instead of yet another
  69. random CONFIG option
  70. - Clean up locking in probe path
  71. Rev 3.23.36 October 1, 2003, Christoph Hellwig
  72. - queuecommand only ever receives new commands - clear flags
  73. - Reintegrate lost fixes from Linux 2.5
  74. Rev 3.23.35 August 14, 2003, Jes Sorensen
  75. - Build against 2.6
  76. Rev 3.23.34 July 23, 2003, Jes Sorensen
  77. - Remove pointless TRUE/FALSE macros
  78. - Clean up vchan handling
  79. Rev 3.23.33 July 3, 2003, Jes Sorensen
  80. - Don't define register access macros before define determining MMIO.
  81. This just happend to work out on ia64 but not elsewhere.
  82. - Don't try and read from the card while it is in reset as
  83. it won't respond and causes an MCA
  84. Rev 3.23.32 June 23, 2003, Jes Sorensen
  85. - Basic support for boot time arguments
  86. Rev 3.23.31 June 8, 2003, Jes Sorensen
  87. - Reduce boot time messages
  88. Rev 3.23.30 June 6, 2003, Jes Sorensen
  89. - Do not enable sync/wide/ppr before it has been determined
  90. that the target device actually supports it
  91. - Enable DMA arbitration for multi channel controllers
  92. Rev 3.23.29 June 3, 2003, Jes Sorensen
  93. - Port to 2.5.69
  94. Rev 3.23.28 June 3, 2003, Jes Sorensen
  95. - Eliminate duplicate marker commands on bus resets
  96. - Handle outstanding commands appropriately on bus/device resets
  97. Rev 3.23.27 May 28, 2003, Jes Sorensen
  98. - Remove bogus input queue code, let the Linux SCSI layer do the work
  99. - Clean up NVRAM handling, only read it once from the card
  100. - Add a number of missing default nvram parameters
  101. Rev 3.23.26 Beta May 28, 2003, Jes Sorensen
  102. - Use completion queue for mailbox commands instead of busy wait
  103. Rev 3.23.25 Beta May 27, 2003, James Bottomley
  104. - Migrate to use new error handling code
  105. Rev 3.23.24 Beta May 21, 2003, James Bottomley
  106. - Big endian support
  107. - Cleanup data direction code
  108. Rev 3.23.23 Beta May 12, 2003, Jes Sorensen
  109. - Switch to using MMIO instead of PIO
  110. Rev 3.23.22 Beta April 15, 2003, Jes Sorensen
  111. - Fix PCI parity problem with 12160 during reset.
  112. Rev 3.23.21 Beta April 14, 2003, Jes Sorensen
  113. - Use pci_map_page()/pci_unmap_page() instead of map_single version.
  114. Rev 3.23.20 Beta April 9, 2003, Jes Sorensen
  115. - Remove < 2.4.x support
  116. - Introduce HOST_LOCK to make the spin lock changes portable.
  117. - Remove a bunch of idiotic and unnecessary typedef's
  118. - Kill all leftovers of target-mode support which never worked anyway
  119. Rev 3.23.19 Beta April 11, 2002, Linus Torvalds
  120. - Do qla1280_pci_config() before calling request_irq() and
  121. request_region()
  122. - Use pci_dma_hi32() to handle upper word of DMA addresses instead
  123. of large shifts
  124. - Hand correct arguments to free_irq() in case of failure
  125. Rev 3.23.18 Beta April 11, 2002, Jes Sorensen
  126. - Run source through Lindent and clean up the output
  127. Rev 3.23.17 Beta April 11, 2002, Jes Sorensen
  128. - Update SCSI firmware to qla1280 v8.15.00 and qla12160 v10.04.32
  129. Rev 3.23.16 Beta March 19, 2002, Jes Sorensen
  130. - Rely on mailbox commands generating interrupts - do not
  131. run qla1280_isr() from ql1280_mailbox_command()
  132. - Remove device_reg_t
  133. - Integrate ql12160_set_target_parameters() with 1280 version
  134. - Make qla1280_setup() non static
  135. - Do not call qla1280_check_for_dead_scsi_bus() on every I/O request
  136. sent to the card - this command pauses the firmware!!!
  137. Rev 3.23.15 Beta March 19, 2002, Jes Sorensen
  138. - Clean up qla1280.h - remove obsolete QL_DEBUG_LEVEL_x definitions
  139. - Remove a pile of pointless and confusing (srb_t **) and
  140. (scsi_lu_t *) typecasts
  141. - Explicit mark that we do not use the new error handling (for now)
  142. - Remove scsi_qla_host_t and use 'struct' instead
  143. - Remove in_abort, watchdog_enabled, dpc, dpc_sched, bios_enabled,
  144. pci_64bit_slot flags which weren't used for anything anyway
  145. - Grab host->host_lock while calling qla1280_isr() from abort()
  146. - Use spin_lock()/spin_unlock() in qla1280_intr_handler() - we
  147. do not need to save/restore flags in the interrupt handler
  148. - Enable interrupts early (before any mailbox access) in preparation
  149. for cleaning up the mailbox handling
  150. Rev 3.23.14 Beta March 14, 2002, Jes Sorensen
  151. - Further cleanups. Remove all trace of QL_DEBUG_LEVEL_x and replace
  152. it with proper use of dprintk().
  153. - Make qla1280_print_scsi_cmd() and qla1280_dump_buffer() both take
  154. a debug level argument to determine if data is to be printed
  155. - Add KERN_* info to printk()
  156. Rev 3.23.13 Beta March 14, 2002, Jes Sorensen
  157. - Significant cosmetic cleanups
  158. - Change debug code to use dprintk() and remove #if mess
  159. Rev 3.23.12 Beta March 13, 2002, Jes Sorensen
  160. - More cosmetic cleanups, fix places treating return as function
  161. - use cpu_relax() in qla1280_debounce_register()
  162. Rev 3.23.11 Beta March 13, 2002, Jes Sorensen
  163. - Make it compile under 2.5.5
  164. Rev 3.23.10 Beta October 1, 2001, Jes Sorensen
  165. - Do no typecast short * to long * in QL1280BoardTbl, this
  166. broke miserably on big endian boxes
  167. Rev 3.23.9 Beta September 30, 2001, Jes Sorensen
  168. - Remove pre 2.2 hack for checking for reentrance in interrupt handler
  169. - Make data types used to receive from SCSI_{BUS,TCN,LUN}_32
  170. unsigned int to match the types from struct scsi_cmnd
  171. Rev 3.23.8 Beta September 29, 2001, Jes Sorensen
  172. - Remove bogus timer_t typedef from qla1280.h
  173. - Remove obsolete pre 2.2 PCI setup code, use proper #define's
  174. for PCI_ values, call pci_set_master()
  175. - Fix memleak of qla1280_buffer on module unload
  176. - Only compile module parsing code #ifdef MODULE - should be
  177. changed to use individual MODULE_PARM's later
  178. - Remove dummy_buffer that was never modified nor printed
  179. - ENTER()/LEAVE() are noops unless QL_DEBUG_LEVEL_3, hence remove
  180. #ifdef QL_DEBUG_LEVEL_3/#endif around ENTER()/LEAVE() calls
  181. - Remove \r from print statements, this is Linux, not DOS
  182. - Remove obsolete QLA1280_{SCSILU,INTR,RING}_{LOCK,UNLOCK}
  183. dummy macros
  184. - Remove C++ compile hack in header file as Linux driver are not
  185. supposed to be compiled as C++
  186. - Kill MS_64BITS macro as it makes the code more readable
  187. - Remove unnecessary flags.in_interrupts bit
  188. Rev 3.23.7 Beta August 20, 2001, Jes Sorensen
  189. - Dont' check for set flags on q->q_flag one by one in qla1280_next()
  190. - Check whether the interrupt was generated by the QLA1280 before
  191. doing any processing
  192. - qla1280_status_entry(): Only zero out part of sense_buffer that
  193. is not being copied into
  194. - Remove more superflouous typecasts
  195. - qla1280_32bit_start_scsi() replace home-brew memcpy() with memcpy()
  196. Rev 3.23.6 Beta August 20, 2001, Tony Luck, Intel
  197. - Don't walk the entire list in qla1280_putq_t() just to directly
  198. grab the pointer to the last element afterwards
  199. Rev 3.23.5 Beta August 9, 2001, Jes Sorensen
  200. - Don't use IRQF_DISABLED, it's use is deprecated for this kinda driver
  201. Rev 3.23.4 Beta August 8, 2001, Jes Sorensen
  202. - Set dev->max_sectors to 1024
  203. Rev 3.23.3 Beta August 6, 2001, Jes Sorensen
  204. - Provide compat macros for pci_enable_device(), pci_find_subsys()
  205. and scsi_set_pci_device()
  206. - Call scsi_set_pci_device() for all devices
  207. - Reduce size of kernel version dependent device probe code
  208. - Move duplicate probe/init code to separate function
  209. - Handle error if qla1280_mem_alloc() fails
  210. - Kill OFFSET() macro and use Linux's PCI definitions instead
  211. - Kill private structure defining PCI config space (struct config_reg)
  212. - Only allocate I/O port region if not in MMIO mode
  213. - Remove duplicate (unused) sanity check of sife of srb_t
  214. Rev 3.23.2 Beta August 6, 2001, Jes Sorensen
  215. - Change home-brew memset() implementations to use memset()
  216. - Remove all references to COMTRACE() - accessing a PC's COM2 serial
  217. port directly is not legal under Linux.
  218. Rev 3.23.1 Beta April 24, 2001, Jes Sorensen
  219. - Remove pre 2.2 kernel support
  220. - clean up 64 bit DMA setting to use 2.4 API (provide backwards compat)
  221. - Fix MMIO access to use readl/writel instead of directly
  222. dereferencing pointers
  223. - Nuke MSDOS debugging code
  224. - Change true/false data types to int from uint8_t
  225. - Use int for counters instead of uint8_t etc.
  226. - Clean up size & byte order conversion macro usage
  227. Rev 3.23 Beta January 11, 2001 BN Qlogic
  228. - Added check of device_id when handling non
  229. QLA12160s during detect().
  230. Rev 3.22 Beta January 5, 2001 BN Qlogic
  231. - Changed queue_task() to schedule_task()
  232. for kernels 2.4.0 and higher.
  233. Note: 2.4.0-testxx kernels released prior to
  234. the actual 2.4.0 kernel release on January 2001
  235. will get compile/link errors with schedule_task().
  236. Please update your kernel to released 2.4.0 level,
  237. or comment lines in this file flagged with 3.22
  238. to resolve compile/link error of schedule_task().
  239. - Added -DCONFIG_SMP in addition to -D__SMP__
  240. in Makefile for 2.4.0 builds of driver as module.
  241. Rev 3.21 Beta January 4, 2001 BN Qlogic
  242. - Changed criteria of 64/32 Bit mode of HBA
  243. operation according to BITS_PER_LONG rather
  244. than HBA's NVRAM setting of >4Gig memory bit;
  245. so that the HBA auto-configures without the need
  246. to setup each system individually.
  247. Rev 3.20 Beta December 5, 2000 BN Qlogic
  248. - Added priority handling to IA-64 onboard SCSI
  249. ISP12160 chip for kernels greater than 2.3.18.
  250. - Added irqrestore for qla1280_intr_handler.
  251. - Enabled /proc/scsi/qla1280 interface.
  252. - Clear /proc/scsi/qla1280 counters in detect().
  253. Rev 3.19 Beta October 13, 2000 BN Qlogic
  254. - Declare driver_template for new kernel
  255. (2.4.0 and greater) scsi initialization scheme.
  256. - Update /proc/scsi entry for 2.3.18 kernels and
  257. above as qla1280
  258. Rev 3.18 Beta October 10, 2000 BN Qlogic
  259. - Changed scan order of adapters to map
  260. the QLA12160 followed by the QLA1280.
  261. Rev 3.17 Beta September 18, 2000 BN Qlogic
  262. - Removed warnings for 32 bit 2.4.x compiles
  263. - Corrected declared size for request and response
  264. DMA addresses that are kept in each ha
  265. Rev. 3.16 Beta August 25, 2000 BN Qlogic
  266. - Corrected 64 bit addressing issue on IA-64
  267. where the upper 32 bits were not properly
  268. passed to the RISC engine.
  269. Rev. 3.15 Beta August 22, 2000 BN Qlogic
  270. - Modified qla1280_setup_chip to properly load
  271. ISP firmware for greater that 4 Gig memory on IA-64
  272. Rev. 3.14 Beta August 16, 2000 BN Qlogic
  273. - Added setting of dma_mask to full 64 bit
  274. if flags.enable_64bit_addressing is set in NVRAM
  275. Rev. 3.13 Beta August 16, 2000 BN Qlogic
  276. - Use new PCI DMA mapping APIs for 2.4.x kernel
  277. Rev. 3.12 July 18, 2000 Redhat & BN Qlogic
  278. - Added check of pci_enable_device to detect() for 2.3.x
  279. - Use pci_resource_start() instead of
  280. pdev->resource[0].start in detect() for 2.3.x
  281. - Updated driver version
  282. Rev. 3.11 July 14, 2000 BN Qlogic
  283. - Updated SCSI Firmware to following versions:
  284. qla1x80: 8.13.08
  285. qla1x160: 10.04.08
  286. - Updated driver version to 3.11
  287. Rev. 3.10 June 23, 2000 BN Qlogic
  288. - Added filtering of AMI SubSys Vendor ID devices
  289. Rev. 3.9
  290. - DEBUG_QLA1280 undefined and new version BN Qlogic
  291. Rev. 3.08b May 9, 2000 MD Dell
  292. - Added logic to check against AMI subsystem vendor ID
  293. Rev. 3.08 May 4, 2000 DG Qlogic
  294. - Added logic to check for PCI subsystem ID.
  295. Rev. 3.07 Apr 24, 2000 DG & BN Qlogic
  296. - Updated SCSI Firmware to following versions:
  297. qla12160: 10.01.19
  298. qla1280: 8.09.00
  299. Rev. 3.06 Apr 12, 2000 DG & BN Qlogic
  300. - Internal revision; not released
  301. Rev. 3.05 Mar 28, 2000 DG & BN Qlogic
  302. - Edit correction for virt_to_bus and PROC.
  303. Rev. 3.04 Mar 28, 2000 DG & BN Qlogic
  304. - Merge changes from ia64 port.
  305. Rev. 3.03 Mar 28, 2000 BN Qlogic
  306. - Increase version to reflect new code drop with compile fix
  307. of issue with inclusion of linux/spinlock for 2.3 kernels
  308. Rev. 3.02 Mar 15, 2000 BN Qlogic
  309. - Merge qla1280_proc_info from 2.10 code base
  310. Rev. 3.01 Feb 10, 2000 BN Qlogic
  311. - Corrected code to compile on a 2.2.x kernel.
  312. Rev. 3.00 Jan 17, 2000 DG Qlogic
  313. - Added 64-bit support.
  314. Rev. 2.07 Nov 9, 1999 DG Qlogic
  315. - Added new routine to set target parameters for ISP12160.
  316. Rev. 2.06 Sept 10, 1999 DG Qlogic
  317. - Added support for ISP12160 Ultra 3 chip.
  318. Rev. 2.03 August 3, 1999 Fred Lewis, Intel DuPont
  319. - Modified code to remove errors generated when compiling with
  320. Cygnus IA64 Compiler.
  321. - Changed conversion of pointers to unsigned longs instead of integers.
  322. - Changed type of I/O port variables from uint32_t to unsigned long.
  323. - Modified OFFSET macro to work with 64-bit as well as 32-bit.
  324. - Changed sprintf and printk format specifiers for pointers to %p.
  325. - Changed some int to long type casts where needed in sprintf & printk.
  326. - Added l modifiers to sprintf and printk format specifiers for longs.
  327. - Removed unused local variables.
  328. Rev. 1.20 June 8, 1999 DG, Qlogic
  329. Changes to support RedHat release 6.0 (kernel 2.2.5).
  330. - Added SCSI exclusive access lock (io_request_lock) when accessing
  331. the adapter.
  332. - Added changes for the new LINUX interface template. Some new error
  333. handling routines have been added to the template, but for now we
  334. will use the old ones.
  335. - Initial Beta Release.
  336. *****************************************************************************/
  337. #include <linux/module.h>
  338. #include <linux/types.h>
  339. #include <linux/string.h>
  340. #include <linux/errno.h>
  341. #include <linux/kernel.h>
  342. #include <linux/ioport.h>
  343. #include <linux/delay.h>
  344. #include <linux/timer.h>
  345. #include <linux/pci.h>
  346. #include <linux/proc_fs.h>
  347. #include <linux/stat.h>
  348. #include <linux/pci_ids.h>
  349. #include <linux/interrupt.h>
  350. #include <linux/init.h>
  351. #include <linux/dma-mapping.h>
  352. #include <linux/firmware.h>
  353. #include <asm/io.h>
  354. #include <asm/irq.h>
  355. #include <asm/byteorder.h>
  356. #include <asm/processor.h>
  357. #include <asm/types.h>
  358. #include <asm/system.h>
  359. #include <scsi/scsi.h>
  360. #include <scsi/scsi_cmnd.h>
  361. #include <scsi/scsi_device.h>
  362. #include <scsi/scsi_host.h>
  363. #include <scsi/scsi_tcq.h>
  364. #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
  365. #include <asm/sn/io.h>
  366. #endif
  367. /*
  368. * Compile time Options:
  369. * 0 - Disable and 1 - Enable
  370. */
  371. #define DEBUG_QLA1280_INTR 0
  372. #define DEBUG_PRINT_NVRAM 0
  373. #define DEBUG_QLA1280 0
  374. /*
  375. * The SGI VISWS is broken and doesn't support MMIO ;-(
  376. */
  377. #ifdef CONFIG_X86_VISWS
  378. #define MEMORY_MAPPED_IO 0
  379. #else
  380. #define MEMORY_MAPPED_IO 1
  381. #endif
  382. #include "qla1280.h"
  383. #ifndef BITS_PER_LONG
  384. #error "BITS_PER_LONG not defined!"
  385. #endif
  386. #if (BITS_PER_LONG == 64) || defined CONFIG_HIGHMEM
  387. #define QLA_64BIT_PTR 1
  388. #endif
  389. #ifdef QLA_64BIT_PTR
  390. #define pci_dma_hi32(a) ((a >> 16) >> 16)
  391. #else
  392. #define pci_dma_hi32(a) 0
  393. #endif
  394. #define pci_dma_lo32(a) (a & 0xffffffff)
  395. #define NVRAM_DELAY() udelay(500) /* 2 microseconds */
  396. #if defined(__ia64__) && !defined(ia64_platform_is)
  397. #define ia64_platform_is(foo) (!strcmp(x, platform_name))
  398. #endif
  399. #define IS_ISP1040(ha) (ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP1020)
  400. #define IS_ISP1x40(ha) (ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP1020 || \
  401. ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP1240)
  402. #define IS_ISP1x160(ha) (ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP10160 || \
  403. ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP12160)
  404. static int qla1280_probe_one(struct pci_dev *, const struct pci_device_id *);
  405. static void qla1280_remove_one(struct pci_dev *);
  406. /*
  407. * QLogic Driver Support Function Prototypes.
  408. */
  409. static void qla1280_done(struct scsi_qla_host *);
  410. static int qla1280_get_token(char *);
  411. static int qla1280_setup(char *s) __init;
  412. /*
  413. * QLogic ISP1280 Hardware Support Function Prototypes.
  414. */
  415. static int qla1280_load_firmware(struct scsi_qla_host *);
  416. static int qla1280_init_rings(struct scsi_qla_host *);
  417. static int qla1280_nvram_config(struct scsi_qla_host *);
  418. static int qla1280_mailbox_command(struct scsi_qla_host *,
  419. uint8_t, uint16_t *);
  420. static int qla1280_bus_reset(struct scsi_qla_host *, int);
  421. static int qla1280_device_reset(struct scsi_qla_host *, int, int);
  422. static int qla1280_abort_command(struct scsi_qla_host *, struct srb *, int);
  423. static int qla1280_abort_isp(struct scsi_qla_host *);
  424. #ifdef QLA_64BIT_PTR
  425. static int qla1280_64bit_start_scsi(struct scsi_qla_host *, struct srb *);
  426. #else
  427. static int qla1280_32bit_start_scsi(struct scsi_qla_host *, struct srb *);
  428. #endif
  429. static void qla1280_nv_write(struct scsi_qla_host *, uint16_t);
  430. static void qla1280_poll(struct scsi_qla_host *);
  431. static void qla1280_reset_adapter(struct scsi_qla_host *);
  432. static void qla1280_marker(struct scsi_qla_host *, int, int, int, u8);
  433. static void qla1280_isp_cmd(struct scsi_qla_host *);
  434. static void qla1280_isr(struct scsi_qla_host *, struct list_head *);
  435. static void qla1280_rst_aen(struct scsi_qla_host *);
  436. static void qla1280_status_entry(struct scsi_qla_host *, struct response *,
  437. struct list_head *);
  438. static void qla1280_error_entry(struct scsi_qla_host *, struct response *,
  439. struct list_head *);
  440. static uint16_t qla1280_get_nvram_word(struct scsi_qla_host *, uint32_t);
  441. static uint16_t qla1280_nvram_request(struct scsi_qla_host *, uint32_t);
  442. static uint16_t qla1280_debounce_register(volatile uint16_t __iomem *);
  443. static request_t *qla1280_req_pkt(struct scsi_qla_host *);
  444. static int qla1280_check_for_dead_scsi_bus(struct scsi_qla_host *,
  445. unsigned int);
  446. static void qla1280_get_target_parameters(struct scsi_qla_host *,
  447. struct scsi_device *);
  448. static int qla1280_set_target_parameters(struct scsi_qla_host *, int, int);
  449. static struct qla_driver_setup driver_setup;
  450. /*
  451. * convert scsi data direction to request_t control flags
  452. */
  453. static inline uint16_t
  454. qla1280_data_direction(struct scsi_cmnd *cmnd)
  455. {
  456. switch(cmnd->sc_data_direction) {
  457. case DMA_FROM_DEVICE:
  458. return BIT_5;
  459. case DMA_TO_DEVICE:
  460. return BIT_6;
  461. case DMA_BIDIRECTIONAL:
  462. return BIT_5 | BIT_6;
  463. /*
  464. * We could BUG() on default here if one of the four cases aren't
  465. * met, but then again if we receive something like that from the
  466. * SCSI layer we have more serious problems. This shuts up GCC.
  467. */
  468. case DMA_NONE:
  469. default:
  470. return 0;
  471. }
  472. }
  473. #if DEBUG_QLA1280
  474. static void __qla1280_print_scsi_cmd(struct scsi_cmnd * cmd);
  475. static void __qla1280_dump_buffer(char *, int);
  476. #endif
  477. /*
  478. * insmod needs to find the variable and make it point to something
  479. */
  480. #ifdef MODULE
  481. static char *qla1280;
  482. /* insmod qla1280 options=verbose" */
  483. module_param(qla1280, charp, 0);
  484. #else
  485. __setup("qla1280=", qla1280_setup);
  486. #endif
  487. /*
  488. * We use the scsi_pointer structure that's included with each scsi_command
  489. * to overlay our struct srb over it. qla1280_init() checks that a srb is not
  490. * bigger than a scsi_pointer.
  491. */
  492. #define CMD_SP(Cmnd) &Cmnd->SCp
  493. #define CMD_CDBLEN(Cmnd) Cmnd->cmd_len
  494. #define CMD_CDBP(Cmnd) Cmnd->cmnd
  495. #define CMD_SNSP(Cmnd) Cmnd->sense_buffer
  496. #define CMD_SNSLEN(Cmnd) SCSI_SENSE_BUFFERSIZE
  497. #define CMD_RESULT(Cmnd) Cmnd->result
  498. #define CMD_HANDLE(Cmnd) Cmnd->host_scribble
  499. #define CMD_REQUEST(Cmnd) Cmnd->request->cmd
  500. #define CMD_HOST(Cmnd) Cmnd->device->host
  501. #define SCSI_BUS_32(Cmnd) Cmnd->device->channel
  502. #define SCSI_TCN_32(Cmnd) Cmnd->device->id
  503. #define SCSI_LUN_32(Cmnd) Cmnd->device->lun
  504. /*****************************************/
  505. /* ISP Boards supported by this driver */
  506. /*****************************************/
  507. struct qla_boards {
  508. char *name; /* Board ID String */
  509. int numPorts; /* Number of SCSI ports */
  510. int fw_index; /* index into qla1280_fw_tbl for firmware */
  511. };
  512. /* NOTE: the last argument in each entry is used to index ql1280_board_tbl */
  513. static struct pci_device_id qla1280_pci_tbl[] = {
  514. {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP12160,
  515. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  516. {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1020,
  517. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
  518. {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1080,
  519. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
  520. {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1240,
  521. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3},
  522. {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1280,
  523. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4},
  524. {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP10160,
  525. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5},
  526. {0,}
  527. };
  528. MODULE_DEVICE_TABLE(pci, qla1280_pci_tbl);
  529. DEFINE_MUTEX(qla1280_firmware_mutex);
  530. struct qla_fw {
  531. char *fwname;
  532. const struct firmware *fw;
  533. };
  534. #define QL_NUM_FW_IMAGES 3
  535. struct qla_fw qla1280_fw_tbl[QL_NUM_FW_IMAGES] = {
  536. {"qlogic/1040.bin", NULL}, /* image 0 */
  537. {"qlogic/1280.bin", NULL}, /* image 1 */
  538. {"qlogic/12160.bin", NULL}, /* image 2 */
  539. };
  540. /* NOTE: Order of boards in this table must match order in qla1280_pci_tbl */
  541. static struct qla_boards ql1280_board_tbl[] = {
  542. {.name = "QLA12160", .numPorts = 2, .fw_index = 2},
  543. {.name = "QLA1040" , .numPorts = 1, .fw_index = 0},
  544. {.name = "QLA1080" , .numPorts = 1, .fw_index = 1},
  545. {.name = "QLA1240" , .numPorts = 2, .fw_index = 1},
  546. {.name = "QLA1280" , .numPorts = 2, .fw_index = 1},
  547. {.name = "QLA10160", .numPorts = 1, .fw_index = 2},
  548. {.name = " ", .numPorts = 0, .fw_index = -1},
  549. };
  550. static int qla1280_verbose = 1;
  551. #if DEBUG_QLA1280
  552. static int ql_debug_level = 1;
  553. #define dprintk(level, format, a...) \
  554. do { if (ql_debug_level >= level) printk(KERN_ERR format, ##a); } while(0)
  555. #define qla1280_dump_buffer(level, buf, size) \
  556. if (ql_debug_level >= level) __qla1280_dump_buffer(buf, size)
  557. #define qla1280_print_scsi_cmd(level, cmd) \
  558. if (ql_debug_level >= level) __qla1280_print_scsi_cmd(cmd)
  559. #else
  560. #define ql_debug_level 0
  561. #define dprintk(level, format, a...) do{}while(0)
  562. #define qla1280_dump_buffer(a, b, c) do{}while(0)
  563. #define qla1280_print_scsi_cmd(a, b) do{}while(0)
  564. #endif
  565. #define ENTER(x) dprintk(3, "qla1280 : Entering %s()\n", x);
  566. #define LEAVE(x) dprintk(3, "qla1280 : Leaving %s()\n", x);
  567. #define ENTER_INTR(x) dprintk(4, "qla1280 : Entering %s()\n", x);
  568. #define LEAVE_INTR(x) dprintk(4, "qla1280 : Leaving %s()\n", x);
  569. static int qla1280_read_nvram(struct scsi_qla_host *ha)
  570. {
  571. uint16_t *wptr;
  572. uint8_t chksum;
  573. int cnt, i;
  574. struct nvram *nv;
  575. ENTER("qla1280_read_nvram");
  576. if (driver_setup.no_nvram)
  577. return 1;
  578. printk(KERN_INFO "scsi(%ld): Reading NVRAM\n", ha->host_no);
  579. wptr = (uint16_t *)&ha->nvram;
  580. nv = &ha->nvram;
  581. chksum = 0;
  582. for (cnt = 0; cnt < 3; cnt++) {
  583. *wptr = qla1280_get_nvram_word(ha, cnt);
  584. chksum += *wptr & 0xff;
  585. chksum += (*wptr >> 8) & 0xff;
  586. wptr++;
  587. }
  588. if (nv->id0 != 'I' || nv->id1 != 'S' ||
  589. nv->id2 != 'P' || nv->id3 != ' ' || nv->version < 1) {
  590. dprintk(2, "Invalid nvram ID or version!\n");
  591. chksum = 1;
  592. } else {
  593. for (; cnt < sizeof(struct nvram); cnt++) {
  594. *wptr = qla1280_get_nvram_word(ha, cnt);
  595. chksum += *wptr & 0xff;
  596. chksum += (*wptr >> 8) & 0xff;
  597. wptr++;
  598. }
  599. }
  600. dprintk(3, "qla1280_read_nvram: NVRAM Magic ID= %c %c %c %02x"
  601. " version %i\n", nv->id0, nv->id1, nv->id2, nv->id3,
  602. nv->version);
  603. if (chksum) {
  604. if (!driver_setup.no_nvram)
  605. printk(KERN_WARNING "scsi(%ld): Unable to identify or "
  606. "validate NVRAM checksum, using default "
  607. "settings\n", ha->host_no);
  608. ha->nvram_valid = 0;
  609. } else
  610. ha->nvram_valid = 1;
  611. /* The firmware interface is, um, interesting, in that the
  612. * actual firmware image on the chip is little endian, thus,
  613. * the process of taking that image to the CPU would end up
  614. * little endian. However, the firmware interface requires it
  615. * to be read a word (two bytes) at a time.
  616. *
  617. * The net result of this would be that the word (and
  618. * doubleword) quantites in the firmware would be correct, but
  619. * the bytes would be pairwise reversed. Since most of the
  620. * firmware quantites are, in fact, bytes, we do an extra
  621. * le16_to_cpu() in the firmware read routine.
  622. *
  623. * The upshot of all this is that the bytes in the firmware
  624. * are in the correct places, but the 16 and 32 bit quantites
  625. * are still in little endian format. We fix that up below by
  626. * doing extra reverses on them */
  627. nv->isp_parameter = cpu_to_le16(nv->isp_parameter);
  628. nv->firmware_feature.w = cpu_to_le16(nv->firmware_feature.w);
  629. for(i = 0; i < MAX_BUSES; i++) {
  630. nv->bus[i].selection_timeout = cpu_to_le16(nv->bus[i].selection_timeout);
  631. nv->bus[i].max_queue_depth = cpu_to_le16(nv->bus[i].max_queue_depth);
  632. }
  633. dprintk(1, "qla1280_read_nvram: Completed Reading NVRAM\n");
  634. LEAVE("qla1280_read_nvram");
  635. return chksum;
  636. }
  637. /**************************************************************************
  638. * qla1280_info
  639. * Return a string describing the driver.
  640. **************************************************************************/
  641. static const char *
  642. qla1280_info(struct Scsi_Host *host)
  643. {
  644. static char qla1280_scsi_name_buffer[125];
  645. char *bp;
  646. struct scsi_qla_host *ha;
  647. struct qla_boards *bdp;
  648. bp = &qla1280_scsi_name_buffer[0];
  649. ha = (struct scsi_qla_host *)host->hostdata;
  650. bdp = &ql1280_board_tbl[ha->devnum];
  651. memset(bp, 0, sizeof(qla1280_scsi_name_buffer));
  652. sprintf (bp,
  653. "QLogic %s PCI to SCSI Host Adapter\n"
  654. " Firmware version: %2d.%02d.%02d, Driver version %s",
  655. &bdp->name[0], ha->fwver1, ha->fwver2, ha->fwver3,
  656. QLA1280_VERSION);
  657. return bp;
  658. }
  659. /**************************************************************************
  660. * qla1280_queuecommand
  661. * Queue a command to the controller.
  662. *
  663. * Note:
  664. * The mid-level driver tries to ensures that queuecommand never gets invoked
  665. * concurrently with itself or the interrupt handler (although the
  666. * interrupt handler may call this routine as part of request-completion
  667. * handling). Unfortunely, it sometimes calls the scheduler in interrupt
  668. * context which is a big NO! NO!.
  669. **************************************************************************/
  670. static int
  671. qla1280_queuecommand(struct scsi_cmnd *cmd, void (*fn)(struct scsi_cmnd *))
  672. {
  673. struct Scsi_Host *host = cmd->device->host;
  674. struct scsi_qla_host *ha = (struct scsi_qla_host *)host->hostdata;
  675. struct srb *sp = (struct srb *)CMD_SP(cmd);
  676. int status;
  677. cmd->scsi_done = fn;
  678. sp->cmd = cmd;
  679. sp->flags = 0;
  680. sp->wait = NULL;
  681. CMD_HANDLE(cmd) = (unsigned char *)NULL;
  682. qla1280_print_scsi_cmd(5, cmd);
  683. #ifdef QLA_64BIT_PTR
  684. /*
  685. * Using 64 bit commands if the PCI bridge doesn't support it is a
  686. * bit wasteful, however this should really only happen if one's
  687. * PCI controller is completely broken, like the BCM1250. For
  688. * sane hardware this is not an issue.
  689. */
  690. status = qla1280_64bit_start_scsi(ha, sp);
  691. #else
  692. status = qla1280_32bit_start_scsi(ha, sp);
  693. #endif
  694. return status;
  695. }
  696. enum action {
  697. ABORT_COMMAND,
  698. DEVICE_RESET,
  699. BUS_RESET,
  700. ADAPTER_RESET,
  701. };
  702. static void qla1280_mailbox_timeout(unsigned long __data)
  703. {
  704. struct scsi_qla_host *ha = (struct scsi_qla_host *)__data;
  705. struct device_reg __iomem *reg;
  706. reg = ha->iobase;
  707. ha->mailbox_out[0] = RD_REG_WORD(&reg->mailbox0);
  708. printk(KERN_ERR "scsi(%ld): mailbox timed out, mailbox0 %04x, "
  709. "ictrl %04x, istatus %04x\n", ha->host_no, ha->mailbox_out[0],
  710. RD_REG_WORD(&reg->ictrl), RD_REG_WORD(&reg->istatus));
  711. complete(ha->mailbox_wait);
  712. }
  713. static int
  714. _qla1280_wait_for_single_command(struct scsi_qla_host *ha, struct srb *sp,
  715. struct completion *wait)
  716. {
  717. int status = FAILED;
  718. struct scsi_cmnd *cmd = sp->cmd;
  719. spin_unlock_irq(ha->host->host_lock);
  720. wait_for_completion_timeout(wait, 4*HZ);
  721. spin_lock_irq(ha->host->host_lock);
  722. sp->wait = NULL;
  723. if(CMD_HANDLE(cmd) == COMPLETED_HANDLE) {
  724. status = SUCCESS;
  725. (*cmd->scsi_done)(cmd);
  726. }
  727. return status;
  728. }
  729. static int
  730. qla1280_wait_for_single_command(struct scsi_qla_host *ha, struct srb *sp)
  731. {
  732. DECLARE_COMPLETION_ONSTACK(wait);
  733. sp->wait = &wait;
  734. return _qla1280_wait_for_single_command(ha, sp, &wait);
  735. }
  736. static int
  737. qla1280_wait_for_pending_commands(struct scsi_qla_host *ha, int bus, int target)
  738. {
  739. int cnt;
  740. int status;
  741. struct srb *sp;
  742. struct scsi_cmnd *cmd;
  743. status = SUCCESS;
  744. /*
  745. * Wait for all commands with the designated bus/target
  746. * to be completed by the firmware
  747. */
  748. for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  749. sp = ha->outstanding_cmds[cnt];
  750. if (sp) {
  751. cmd = sp->cmd;
  752. if (bus >= 0 && SCSI_BUS_32(cmd) != bus)
  753. continue;
  754. if (target >= 0 && SCSI_TCN_32(cmd) != target)
  755. continue;
  756. status = qla1280_wait_for_single_command(ha, sp);
  757. if (status == FAILED)
  758. break;
  759. }
  760. }
  761. return status;
  762. }
  763. /**************************************************************************
  764. * qla1280_error_action
  765. * The function will attempt to perform a specified error action and
  766. * wait for the results (or time out).
  767. *
  768. * Input:
  769. * cmd = Linux SCSI command packet of the command that cause the
  770. * bus reset.
  771. * action = error action to take (see action_t)
  772. *
  773. * Returns:
  774. * SUCCESS or FAILED
  775. *
  776. **************************************************************************/
  777. static int
  778. qla1280_error_action(struct scsi_cmnd *cmd, enum action action)
  779. {
  780. struct scsi_qla_host *ha;
  781. int bus, target, lun;
  782. struct srb *sp;
  783. int i, found;
  784. int result=FAILED;
  785. int wait_for_bus=-1;
  786. int wait_for_target = -1;
  787. DECLARE_COMPLETION_ONSTACK(wait);
  788. ENTER("qla1280_error_action");
  789. ha = (struct scsi_qla_host *)(CMD_HOST(cmd)->hostdata);
  790. sp = (struct srb *)CMD_SP(cmd);
  791. bus = SCSI_BUS_32(cmd);
  792. target = SCSI_TCN_32(cmd);
  793. lun = SCSI_LUN_32(cmd);
  794. dprintk(4, "error_action %i, istatus 0x%04x\n", action,
  795. RD_REG_WORD(&ha->iobase->istatus));
  796. dprintk(4, "host_cmd 0x%04x, ictrl 0x%04x, jiffies %li\n",
  797. RD_REG_WORD(&ha->iobase->host_cmd),
  798. RD_REG_WORD(&ha->iobase->ictrl), jiffies);
  799. if (qla1280_verbose)
  800. printk(KERN_INFO "scsi(%li): Resetting Cmnd=0x%p, "
  801. "Handle=0x%p, action=0x%x\n",
  802. ha->host_no, cmd, CMD_HANDLE(cmd), action);
  803. /*
  804. * Check to see if we have the command in the outstanding_cmds[]
  805. * array. If not then it must have completed before this error
  806. * action was initiated. If the error_action isn't ABORT_COMMAND
  807. * then the driver must proceed with the requested action.
  808. */
  809. found = -1;
  810. for (i = 0; i < MAX_OUTSTANDING_COMMANDS; i++) {
  811. if (sp == ha->outstanding_cmds[i]) {
  812. found = i;
  813. sp->wait = &wait; /* we'll wait for it to complete */
  814. break;
  815. }
  816. }
  817. if (found < 0) { /* driver doesn't have command */
  818. result = SUCCESS;
  819. if (qla1280_verbose) {
  820. printk(KERN_INFO
  821. "scsi(%ld:%d:%d:%d): specified command has "
  822. "already completed.\n", ha->host_no, bus,
  823. target, lun);
  824. }
  825. }
  826. switch (action) {
  827. case ABORT_COMMAND:
  828. dprintk(1, "qla1280: RISC aborting command\n");
  829. /*
  830. * The abort might fail due to race when the host_lock
  831. * is released to issue the abort. As such, we
  832. * don't bother to check the return status.
  833. */
  834. if (found >= 0)
  835. qla1280_abort_command(ha, sp, found);
  836. break;
  837. case DEVICE_RESET:
  838. if (qla1280_verbose)
  839. printk(KERN_INFO
  840. "scsi(%ld:%d:%d:%d): Queueing device reset "
  841. "command.\n", ha->host_no, bus, target, lun);
  842. if (qla1280_device_reset(ha, bus, target) == 0) {
  843. /* issued device reset, set wait conditions */
  844. wait_for_bus = bus;
  845. wait_for_target = target;
  846. }
  847. break;
  848. case BUS_RESET:
  849. if (qla1280_verbose)
  850. printk(KERN_INFO "qla1280(%ld:%d): Issued bus "
  851. "reset.\n", ha->host_no, bus);
  852. if (qla1280_bus_reset(ha, bus) == 0) {
  853. /* issued bus reset, set wait conditions */
  854. wait_for_bus = bus;
  855. }
  856. break;
  857. case ADAPTER_RESET:
  858. default:
  859. if (qla1280_verbose) {
  860. printk(KERN_INFO
  861. "scsi(%ld): Issued ADAPTER RESET\n",
  862. ha->host_no);
  863. printk(KERN_INFO "scsi(%ld): I/O processing will "
  864. "continue automatically\n", ha->host_no);
  865. }
  866. ha->flags.reset_active = 1;
  867. if (qla1280_abort_isp(ha) != 0) { /* it's dead */
  868. result = FAILED;
  869. }
  870. ha->flags.reset_active = 0;
  871. }
  872. /*
  873. * At this point, the host_lock has been released and retaken
  874. * by the issuance of the mailbox command.
  875. * Wait for the command passed in by the mid-layer if it
  876. * was found by the driver. It might have been returned
  877. * between eh recovery steps, hence the check of the "found"
  878. * variable.
  879. */
  880. if (found >= 0)
  881. result = _qla1280_wait_for_single_command(ha, sp, &wait);
  882. if (action == ABORT_COMMAND && result != SUCCESS) {
  883. printk(KERN_WARNING
  884. "scsi(%li:%i:%i:%i): "
  885. "Unable to abort command!\n",
  886. ha->host_no, bus, target, lun);
  887. }
  888. /*
  889. * If the command passed in by the mid-layer has been
  890. * returned by the board, then wait for any additional
  891. * commands which are supposed to complete based upon
  892. * the error action.
  893. *
  894. * All commands are unconditionally returned during a
  895. * call to qla1280_abort_isp(), ADAPTER_RESET. No need
  896. * to wait for them.
  897. */
  898. if (result == SUCCESS && wait_for_bus >= 0) {
  899. result = qla1280_wait_for_pending_commands(ha,
  900. wait_for_bus, wait_for_target);
  901. }
  902. dprintk(1, "RESET returning %d\n", result);
  903. LEAVE("qla1280_error_action");
  904. return result;
  905. }
  906. /**************************************************************************
  907. * qla1280_abort
  908. * Abort the specified SCSI command(s).
  909. **************************************************************************/
  910. static int
  911. qla1280_eh_abort(struct scsi_cmnd * cmd)
  912. {
  913. int rc;
  914. spin_lock_irq(cmd->device->host->host_lock);
  915. rc = qla1280_error_action(cmd, ABORT_COMMAND);
  916. spin_unlock_irq(cmd->device->host->host_lock);
  917. return rc;
  918. }
  919. /**************************************************************************
  920. * qla1280_device_reset
  921. * Reset the specified SCSI device
  922. **************************************************************************/
  923. static int
  924. qla1280_eh_device_reset(struct scsi_cmnd *cmd)
  925. {
  926. int rc;
  927. spin_lock_irq(cmd->device->host->host_lock);
  928. rc = qla1280_error_action(cmd, DEVICE_RESET);
  929. spin_unlock_irq(cmd->device->host->host_lock);
  930. return rc;
  931. }
  932. /**************************************************************************
  933. * qla1280_bus_reset
  934. * Reset the specified bus.
  935. **************************************************************************/
  936. static int
  937. qla1280_eh_bus_reset(struct scsi_cmnd *cmd)
  938. {
  939. int rc;
  940. spin_lock_irq(cmd->device->host->host_lock);
  941. rc = qla1280_error_action(cmd, BUS_RESET);
  942. spin_unlock_irq(cmd->device->host->host_lock);
  943. return rc;
  944. }
  945. /**************************************************************************
  946. * qla1280_adapter_reset
  947. * Reset the specified adapter (both channels)
  948. **************************************************************************/
  949. static int
  950. qla1280_eh_adapter_reset(struct scsi_cmnd *cmd)
  951. {
  952. int rc;
  953. spin_lock_irq(cmd->device->host->host_lock);
  954. rc = qla1280_error_action(cmd, ADAPTER_RESET);
  955. spin_unlock_irq(cmd->device->host->host_lock);
  956. return rc;
  957. }
  958. static int
  959. qla1280_biosparam(struct scsi_device *sdev, struct block_device *bdev,
  960. sector_t capacity, int geom[])
  961. {
  962. int heads, sectors, cylinders;
  963. heads = 64;
  964. sectors = 32;
  965. cylinders = (unsigned long)capacity / (heads * sectors);
  966. if (cylinders > 1024) {
  967. heads = 255;
  968. sectors = 63;
  969. cylinders = (unsigned long)capacity / (heads * sectors);
  970. /* if (cylinders > 1023)
  971. cylinders = 1023; */
  972. }
  973. geom[0] = heads;
  974. geom[1] = sectors;
  975. geom[2] = cylinders;
  976. return 0;
  977. }
  978. /* disable risc and host interrupts */
  979. static inline void
  980. qla1280_disable_intrs(struct scsi_qla_host *ha)
  981. {
  982. WRT_REG_WORD(&ha->iobase->ictrl, 0);
  983. RD_REG_WORD(&ha->iobase->ictrl); /* PCI Posted Write flush */
  984. }
  985. /* enable risc and host interrupts */
  986. static inline void
  987. qla1280_enable_intrs(struct scsi_qla_host *ha)
  988. {
  989. WRT_REG_WORD(&ha->iobase->ictrl, (ISP_EN_INT | ISP_EN_RISC));
  990. RD_REG_WORD(&ha->iobase->ictrl); /* PCI Posted Write flush */
  991. }
  992. /**************************************************************************
  993. * qla1280_intr_handler
  994. * Handles the H/W interrupt
  995. **************************************************************************/
  996. static irqreturn_t
  997. qla1280_intr_handler(int irq, void *dev_id)
  998. {
  999. struct scsi_qla_host *ha;
  1000. struct device_reg __iomem *reg;
  1001. u16 data;
  1002. int handled = 0;
  1003. ENTER_INTR ("qla1280_intr_handler");
  1004. ha = (struct scsi_qla_host *)dev_id;
  1005. spin_lock(ha->host->host_lock);
  1006. ha->isr_count++;
  1007. reg = ha->iobase;
  1008. qla1280_disable_intrs(ha);
  1009. data = qla1280_debounce_register(&reg->istatus);
  1010. /* Check for pending interrupts. */
  1011. if (data & RISC_INT) {
  1012. qla1280_isr(ha, &ha->done_q);
  1013. handled = 1;
  1014. }
  1015. if (!list_empty(&ha->done_q))
  1016. qla1280_done(ha);
  1017. spin_unlock(ha->host->host_lock);
  1018. qla1280_enable_intrs(ha);
  1019. LEAVE_INTR("qla1280_intr_handler");
  1020. return IRQ_RETVAL(handled);
  1021. }
  1022. static int
  1023. qla1280_set_target_parameters(struct scsi_qla_host *ha, int bus, int target)
  1024. {
  1025. uint8_t mr;
  1026. uint16_t mb[MAILBOX_REGISTER_COUNT];
  1027. struct nvram *nv;
  1028. int status, lun;
  1029. nv = &ha->nvram;
  1030. mr = BIT_3 | BIT_2 | BIT_1 | BIT_0;
  1031. /* Set Target Parameters. */
  1032. mb[0] = MBC_SET_TARGET_PARAMETERS;
  1033. mb[1] = (uint16_t)((bus ? target | BIT_7 : target) << 8);
  1034. mb[2] = nv->bus[bus].target[target].parameter.renegotiate_on_error << 8;
  1035. mb[2] |= nv->bus[bus].target[target].parameter.stop_queue_on_check << 9;
  1036. mb[2] |= nv->bus[bus].target[target].parameter.auto_request_sense << 10;
  1037. mb[2] |= nv->bus[bus].target[target].parameter.tag_queuing << 11;
  1038. mb[2] |= nv->bus[bus].target[target].parameter.enable_sync << 12;
  1039. mb[2] |= nv->bus[bus].target[target].parameter.enable_wide << 13;
  1040. mb[2] |= nv->bus[bus].target[target].parameter.parity_checking << 14;
  1041. mb[2] |= nv->bus[bus].target[target].parameter.disconnect_allowed << 15;
  1042. if (IS_ISP1x160(ha)) {
  1043. mb[2] |= nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr << 5;
  1044. mb[3] = (nv->bus[bus].target[target].flags.flags1x160.sync_offset << 8);
  1045. mb[6] = (nv->bus[bus].target[target].ppr_1x160.flags.ppr_options << 8) |
  1046. nv->bus[bus].target[target].ppr_1x160.flags.ppr_bus_width;
  1047. mr |= BIT_6;
  1048. } else {
  1049. mb[3] = (nv->bus[bus].target[target].flags.flags1x80.sync_offset << 8);
  1050. }
  1051. mb[3] |= nv->bus[bus].target[target].sync_period;
  1052. status = qla1280_mailbox_command(ha, mr, mb);
  1053. /* Set Device Queue Parameters. */
  1054. for (lun = 0; lun < MAX_LUNS; lun++) {
  1055. mb[0] = MBC_SET_DEVICE_QUEUE;
  1056. mb[1] = (uint16_t)((bus ? target | BIT_7 : target) << 8);
  1057. mb[1] |= lun;
  1058. mb[2] = nv->bus[bus].max_queue_depth;
  1059. mb[3] = nv->bus[bus].target[target].execution_throttle;
  1060. status |= qla1280_mailbox_command(ha, 0x0f, mb);
  1061. }
  1062. if (status)
  1063. printk(KERN_WARNING "scsi(%ld:%i:%i): "
  1064. "qla1280_set_target_parameters() failed\n",
  1065. ha->host_no, bus, target);
  1066. return status;
  1067. }
  1068. /**************************************************************************
  1069. * qla1280_slave_configure
  1070. *
  1071. * Description:
  1072. * Determines the queue depth for a given device. There are two ways
  1073. * a queue depth can be obtained for a tagged queueing device. One
  1074. * way is the default queue depth which is determined by whether
  1075. * If it is defined, then it is used
  1076. * as the default queue depth. Otherwise, we use either 4 or 8 as the
  1077. * default queue depth (dependent on the number of hardware SCBs).
  1078. **************************************************************************/
  1079. static int
  1080. qla1280_slave_configure(struct scsi_device *device)
  1081. {
  1082. struct scsi_qla_host *ha;
  1083. int default_depth = 3;
  1084. int bus = device->channel;
  1085. int target = device->id;
  1086. int status = 0;
  1087. struct nvram *nv;
  1088. unsigned long flags;
  1089. ha = (struct scsi_qla_host *)device->host->hostdata;
  1090. nv = &ha->nvram;
  1091. if (qla1280_check_for_dead_scsi_bus(ha, bus))
  1092. return 1;
  1093. if (device->tagged_supported &&
  1094. (ha->bus_settings[bus].qtag_enables & (BIT_0 << target))) {
  1095. scsi_adjust_queue_depth(device, MSG_ORDERED_TAG,
  1096. ha->bus_settings[bus].hiwat);
  1097. } else {
  1098. scsi_adjust_queue_depth(device, 0, default_depth);
  1099. }
  1100. nv->bus[bus].target[target].parameter.enable_sync = device->sdtr;
  1101. nv->bus[bus].target[target].parameter.enable_wide = device->wdtr;
  1102. nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr = device->ppr;
  1103. if (driver_setup.no_sync ||
  1104. (driver_setup.sync_mask &&
  1105. (~driver_setup.sync_mask & (1 << target))))
  1106. nv->bus[bus].target[target].parameter.enable_sync = 0;
  1107. if (driver_setup.no_wide ||
  1108. (driver_setup.wide_mask &&
  1109. (~driver_setup.wide_mask & (1 << target))))
  1110. nv->bus[bus].target[target].parameter.enable_wide = 0;
  1111. if (IS_ISP1x160(ha)) {
  1112. if (driver_setup.no_ppr ||
  1113. (driver_setup.ppr_mask &&
  1114. (~driver_setup.ppr_mask & (1 << target))))
  1115. nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr = 0;
  1116. }
  1117. spin_lock_irqsave(ha->host->host_lock, flags);
  1118. if (nv->bus[bus].target[target].parameter.enable_sync)
  1119. status = qla1280_set_target_parameters(ha, bus, target);
  1120. qla1280_get_target_parameters(ha, device);
  1121. spin_unlock_irqrestore(ha->host->host_lock, flags);
  1122. return status;
  1123. }
  1124. /*
  1125. * qla1280_done
  1126. * Process completed commands.
  1127. *
  1128. * Input:
  1129. * ha = adapter block pointer.
  1130. */
  1131. static void
  1132. qla1280_done(struct scsi_qla_host *ha)
  1133. {
  1134. struct srb *sp;
  1135. struct list_head *done_q;
  1136. int bus, target, lun;
  1137. struct scsi_cmnd *cmd;
  1138. ENTER("qla1280_done");
  1139. done_q = &ha->done_q;
  1140. while (!list_empty(done_q)) {
  1141. sp = list_entry(done_q->next, struct srb, list);
  1142. list_del(&sp->list);
  1143. cmd = sp->cmd;
  1144. bus = SCSI_BUS_32(cmd);
  1145. target = SCSI_TCN_32(cmd);
  1146. lun = SCSI_LUN_32(cmd);
  1147. switch ((CMD_RESULT(cmd) >> 16)) {
  1148. case DID_RESET:
  1149. /* Issue marker command. */
  1150. if (!ha->flags.abort_isp_active)
  1151. qla1280_marker(ha, bus, target, 0, MK_SYNC_ID);
  1152. break;
  1153. case DID_ABORT:
  1154. sp->flags &= ~SRB_ABORT_PENDING;
  1155. sp->flags |= SRB_ABORTED;
  1156. break;
  1157. default:
  1158. break;
  1159. }
  1160. /* Release memory used for this I/O */
  1161. scsi_dma_unmap(cmd);
  1162. /* Call the mid-level driver interrupt handler */
  1163. ha->actthreads--;
  1164. if (sp->wait == NULL)
  1165. (*(cmd)->scsi_done)(cmd);
  1166. else
  1167. complete(sp->wait);
  1168. }
  1169. LEAVE("qla1280_done");
  1170. }
  1171. /*
  1172. * Translates a ISP error to a Linux SCSI error
  1173. */
  1174. static int
  1175. qla1280_return_status(struct response * sts, struct scsi_cmnd *cp)
  1176. {
  1177. int host_status = DID_ERROR;
  1178. uint16_t comp_status = le16_to_cpu(sts->comp_status);
  1179. uint16_t state_flags = le16_to_cpu(sts->state_flags);
  1180. uint32_t residual_length = le32_to_cpu(sts->residual_length);
  1181. uint16_t scsi_status = le16_to_cpu(sts->scsi_status);
  1182. #if DEBUG_QLA1280_INTR
  1183. static char *reason[] = {
  1184. "DID_OK",
  1185. "DID_NO_CONNECT",
  1186. "DID_BUS_BUSY",
  1187. "DID_TIME_OUT",
  1188. "DID_BAD_TARGET",
  1189. "DID_ABORT",
  1190. "DID_PARITY",
  1191. "DID_ERROR",
  1192. "DID_RESET",
  1193. "DID_BAD_INTR"
  1194. };
  1195. #endif /* DEBUG_QLA1280_INTR */
  1196. ENTER("qla1280_return_status");
  1197. #if DEBUG_QLA1280_INTR
  1198. /*
  1199. dprintk(1, "qla1280_return_status: compl status = 0x%04x\n",
  1200. comp_status);
  1201. */
  1202. #endif
  1203. switch (comp_status) {
  1204. case CS_COMPLETE:
  1205. host_status = DID_OK;
  1206. break;
  1207. case CS_INCOMPLETE:
  1208. if (!(state_flags & SF_GOT_BUS))
  1209. host_status = DID_NO_CONNECT;
  1210. else if (!(state_flags & SF_GOT_TARGET))
  1211. host_status = DID_BAD_TARGET;
  1212. else if (!(state_flags & SF_SENT_CDB))
  1213. host_status = DID_ERROR;
  1214. else if (!(state_flags & SF_TRANSFERRED_DATA))
  1215. host_status = DID_ERROR;
  1216. else if (!(state_flags & SF_GOT_STATUS))
  1217. host_status = DID_ERROR;
  1218. else if (!(state_flags & SF_GOT_SENSE))
  1219. host_status = DID_ERROR;
  1220. break;
  1221. case CS_RESET:
  1222. host_status = DID_RESET;
  1223. break;
  1224. case CS_ABORTED:
  1225. host_status = DID_ABORT;
  1226. break;
  1227. case CS_TIMEOUT:
  1228. host_status = DID_TIME_OUT;
  1229. break;
  1230. case CS_DATA_OVERRUN:
  1231. dprintk(2, "Data overrun 0x%x\n", residual_length);
  1232. dprintk(2, "qla1280_return_status: response packet data\n");
  1233. qla1280_dump_buffer(2, (char *)sts, RESPONSE_ENTRY_SIZE);
  1234. host_status = DID_ERROR;
  1235. break;
  1236. case CS_DATA_UNDERRUN:
  1237. if ((scsi_bufflen(cp) - residual_length) <
  1238. cp->underflow) {
  1239. printk(KERN_WARNING
  1240. "scsi: Underflow detected - retrying "
  1241. "command.\n");
  1242. host_status = DID_ERROR;
  1243. } else {
  1244. scsi_set_resid(cp, residual_length);
  1245. host_status = DID_OK;
  1246. }
  1247. break;
  1248. default:
  1249. host_status = DID_ERROR;
  1250. break;
  1251. }
  1252. #if DEBUG_QLA1280_INTR
  1253. dprintk(1, "qla1280 ISP status: host status (%s) scsi status %x\n",
  1254. reason[host_status], scsi_status);
  1255. #endif
  1256. LEAVE("qla1280_return_status");
  1257. return (scsi_status & 0xff) | (host_status << 16);
  1258. }
  1259. /****************************************************************************/
  1260. /* QLogic ISP1280 Hardware Support Functions. */
  1261. /****************************************************************************/
  1262. /*
  1263. * qla1280_initialize_adapter
  1264. * Initialize board.
  1265. *
  1266. * Input:
  1267. * ha = adapter block pointer.
  1268. *
  1269. * Returns:
  1270. * 0 = success
  1271. */
  1272. static int __devinit
  1273. qla1280_initialize_adapter(struct scsi_qla_host *ha)
  1274. {
  1275. struct device_reg __iomem *reg;
  1276. int status;
  1277. int bus;
  1278. unsigned long flags;
  1279. ENTER("qla1280_initialize_adapter");
  1280. /* Clear adapter flags. */
  1281. ha->flags.online = 0;
  1282. ha->flags.disable_host_adapter = 0;
  1283. ha->flags.reset_active = 0;
  1284. ha->flags.abort_isp_active = 0;
  1285. #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
  1286. if (ia64_platform_is("sn2")) {
  1287. printk(KERN_INFO "scsi(%li): Enabling SN2 PCI DMA "
  1288. "dual channel lockup workaround\n", ha->host_no);
  1289. ha->flags.use_pci_vchannel = 1;
  1290. driver_setup.no_nvram = 1;
  1291. }
  1292. #endif
  1293. /* TODO: implement support for the 1040 nvram format */
  1294. if (IS_ISP1040(ha))
  1295. driver_setup.no_nvram = 1;
  1296. dprintk(1, "Configure PCI space for adapter...\n");
  1297. reg = ha->iobase;
  1298. /* Insure mailbox registers are free. */
  1299. WRT_REG_WORD(&reg->semaphore, 0);
  1300. WRT_REG_WORD(&reg->host_cmd, HC_CLR_RISC_INT);
  1301. WRT_REG_WORD(&reg->host_cmd, HC_CLR_HOST_INT);
  1302. RD_REG_WORD(&reg->host_cmd);
  1303. if (qla1280_read_nvram(ha)) {
  1304. dprintk(2, "qla1280_initialize_adapter: failed to read "
  1305. "NVRAM\n");
  1306. }
  1307. /*
  1308. * It's necessary to grab the spin here as qla1280_mailbox_command
  1309. * needs to be able to drop the lock unconditionally to wait
  1310. * for completion.
  1311. */
  1312. spin_lock_irqsave(ha->host->host_lock, flags);
  1313. status = qla1280_load_firmware(ha);
  1314. if (status) {
  1315. printk(KERN_ERR "scsi(%li): initialize: pci probe failed!\n",
  1316. ha->host_no);
  1317. goto out;
  1318. }
  1319. /* Setup adapter based on NVRAM parameters. */
  1320. dprintk(1, "scsi(%ld): Configure NVRAM parameters\n", ha->host_no);
  1321. qla1280_nvram_config(ha);
  1322. if (ha->flags.disable_host_adapter) {
  1323. status = 1;
  1324. goto out;
  1325. }
  1326. status = qla1280_init_rings(ha);
  1327. if (status)
  1328. goto out;
  1329. /* Issue SCSI reset, if we can't reset twice then bus is dead */
  1330. for (bus = 0; bus < ha->ports; bus++) {
  1331. if (!ha->bus_settings[bus].disable_scsi_reset &&
  1332. qla1280_bus_reset(ha, bus) &&