/drivers/scsi/qla1280.c

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

Large files are truncated 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. #include <linux/module.h>
  329. #include <linux/types.h>
  330. #include <linux/string.h>
  331. #include <linux/errno.h>
  332. #include <linux/kernel.h>
  333. #include <linux/ioport.h>
  334. #include <linux/delay.h>
  335. #include <linux/timer.h>
  336. #include <linux/pci.h>
  337. #include <linux/proc_fs.h>
  338. #include <linux/stat.h>
  339. #include <linux/pci_ids.h>
  340. #include <linux/interrupt.h>
  341. #include <linux/init.h>
  342. #include <linux/dma-mapping.h>
  343. #include <linux/firmware.h>
  344. #include <asm/io.h>
  345. #include <asm/irq.h>
  346. #include <asm/byteorder.h>
  347. #include <asm/processor.h>
  348. #include <asm/types.h>
  349. #include <scsi/scsi.h>
  350. #include <scsi/scsi_cmnd.h>
  351. #include <scsi/scsi_device.h>
  352. #include <scsi/scsi_host.h>
  353. #include <scsi/scsi_tcq.h>
  354. /*
  355. * Compile time Options:
  356. * 0 - Disable and 1 - Enable
  357. */
  358. #define DEBUG_QLA1280_INTR 0
  359. #define DEBUG_PRINT_NVRAM 0
  360. #define DEBUG_QLA1280 0
  361. #define MEMORY_MAPPED_IO 1
  362. #include "qla1280.h"
  363. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  364. #define QLA_64BIT_PTR 1
  365. #endif
  366. #define NVRAM_DELAY() udelay(500) /* 2 microseconds */
  367. #define IS_ISP1040(ha) (ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP1020)
  368. #define IS_ISP1x40(ha) (ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP1020 || \
  369. ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP1240)
  370. #define IS_ISP1x160(ha) (ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP10160 || \
  371. ha->pdev->device == PCI_DEVICE_ID_QLOGIC_ISP12160)
  372. static int qla1280_probe_one(struct pci_dev *, const struct pci_device_id *);
  373. static void qla1280_remove_one(struct pci_dev *);
  374. /*
  375. * QLogic Driver Support Function Prototypes.
  376. */
  377. static void qla1280_done(struct scsi_qla_host *);
  378. static int qla1280_get_token(char *);
  379. static int qla1280_setup(char *s) __init;
  380. /*
  381. * QLogic ISP1280 Hardware Support Function Prototypes.
  382. */
  383. static int qla1280_load_firmware(struct scsi_qla_host *);
  384. static int qla1280_init_rings(struct scsi_qla_host *);
  385. static int qla1280_nvram_config(struct scsi_qla_host *);
  386. static int qla1280_mailbox_command(struct scsi_qla_host *,
  387. uint8_t, uint16_t *);
  388. static int qla1280_bus_reset(struct scsi_qla_host *, int);
  389. static int qla1280_device_reset(struct scsi_qla_host *, int, int);
  390. static int qla1280_abort_command(struct scsi_qla_host *, struct srb *, int);
  391. static int qla1280_abort_isp(struct scsi_qla_host *);
  392. #ifdef QLA_64BIT_PTR
  393. static int qla1280_64bit_start_scsi(struct scsi_qla_host *, struct srb *);
  394. #else
  395. static int qla1280_32bit_start_scsi(struct scsi_qla_host *, struct srb *);
  396. #endif
  397. static void qla1280_nv_write(struct scsi_qla_host *, uint16_t);
  398. static void qla1280_poll(struct scsi_qla_host *);
  399. static void qla1280_reset_adapter(struct scsi_qla_host *);
  400. static void qla1280_marker(struct scsi_qla_host *, int, int, int, u8);
  401. static void qla1280_isp_cmd(struct scsi_qla_host *);
  402. static void qla1280_isr(struct scsi_qla_host *, struct list_head *);
  403. static void qla1280_rst_aen(struct scsi_qla_host *);
  404. static void qla1280_status_entry(struct scsi_qla_host *, struct response *,
  405. struct list_head *);
  406. static void qla1280_error_entry(struct scsi_qla_host *, struct response *,
  407. struct list_head *);
  408. static uint16_t qla1280_get_nvram_word(struct scsi_qla_host *, uint32_t);
  409. static uint16_t qla1280_nvram_request(struct scsi_qla_host *, uint32_t);
  410. static uint16_t qla1280_debounce_register(volatile uint16_t __iomem *);
  411. static request_t *qla1280_req_pkt(struct scsi_qla_host *);
  412. static int qla1280_check_for_dead_scsi_bus(struct scsi_qla_host *,
  413. unsigned int);
  414. static void qla1280_get_target_parameters(struct scsi_qla_host *,
  415. struct scsi_device *);
  416. static int qla1280_set_target_parameters(struct scsi_qla_host *, int, int);
  417. static struct qla_driver_setup driver_setup;
  418. /*
  419. * convert scsi data direction to request_t control flags
  420. */
  421. static inline uint16_t
  422. qla1280_data_direction(struct scsi_cmnd *cmnd)
  423. {
  424. switch(cmnd->sc_data_direction) {
  425. case DMA_FROM_DEVICE:
  426. return BIT_5;
  427. case DMA_TO_DEVICE:
  428. return BIT_6;
  429. case DMA_BIDIRECTIONAL:
  430. return BIT_5 | BIT_6;
  431. /*
  432. * We could BUG() on default here if one of the four cases aren't
  433. * met, but then again if we receive something like that from the
  434. * SCSI layer we have more serious problems. This shuts up GCC.
  435. */
  436. case DMA_NONE:
  437. default:
  438. return 0;
  439. }
  440. }
  441. #if DEBUG_QLA1280
  442. static void __qla1280_print_scsi_cmd(struct scsi_cmnd * cmd);
  443. static void __qla1280_dump_buffer(char *, int);
  444. #endif
  445. /*
  446. * insmod needs to find the variable and make it point to something
  447. */
  448. #ifdef MODULE
  449. static char *qla1280;
  450. /* insmod qla1280 options=verbose" */
  451. module_param(qla1280, charp, 0);
  452. #else
  453. __setup("qla1280=", qla1280_setup);
  454. #endif
  455. /*
  456. * We use the scsi_pointer structure that's included with each scsi_command
  457. * to overlay our struct srb over it. qla1280_init() checks that a srb is not
  458. * bigger than a scsi_pointer.
  459. */
  460. #define CMD_SP(Cmnd) &Cmnd->SCp
  461. #define CMD_CDBLEN(Cmnd) Cmnd->cmd_len
  462. #define CMD_CDBP(Cmnd) Cmnd->cmnd
  463. #define CMD_SNSP(Cmnd) Cmnd->sense_buffer
  464. #define CMD_SNSLEN(Cmnd) SCSI_SENSE_BUFFERSIZE
  465. #define CMD_RESULT(Cmnd) Cmnd->result
  466. #define CMD_HANDLE(Cmnd) Cmnd->host_scribble
  467. #define CMD_REQUEST(Cmnd) Cmnd->request->cmd
  468. #define CMD_HOST(Cmnd) Cmnd->device->host
  469. #define SCSI_BUS_32(Cmnd) Cmnd->device->channel
  470. #define SCSI_TCN_32(Cmnd) Cmnd->device->id
  471. #define SCSI_LUN_32(Cmnd) Cmnd->device->lun
  472. /*****************************************/
  473. /* ISP Boards supported by this driver */
  474. /*****************************************/
  475. struct qla_boards {
  476. char *name; /* Board ID String */
  477. int numPorts; /* Number of SCSI ports */
  478. int fw_index; /* index into qla1280_fw_tbl for firmware */
  479. };
  480. /* NOTE: the last argument in each entry is used to index ql1280_board_tbl */
  481. static struct pci_device_id qla1280_pci_tbl[] = {
  482. {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP12160,
  483. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  484. {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1020,
  485. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
  486. {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1080,
  487. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
  488. {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1240,
  489. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3},
  490. {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1280,
  491. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4},
  492. {PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP10160,
  493. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5},
  494. {0,}
  495. };
  496. MODULE_DEVICE_TABLE(pci, qla1280_pci_tbl);
  497. DEFINE_MUTEX(qla1280_firmware_mutex);
  498. struct qla_fw {
  499. char *fwname;
  500. const struct firmware *fw;
  501. };
  502. #define QL_NUM_FW_IMAGES 3
  503. struct qla_fw qla1280_fw_tbl[QL_NUM_FW_IMAGES] = {
  504. {"qlogic/1040.bin", NULL}, /* image 0 */
  505. {"qlogic/1280.bin", NULL}, /* image 1 */
  506. {"qlogic/12160.bin", NULL}, /* image 2 */
  507. };
  508. /* NOTE: Order of boards in this table must match order in qla1280_pci_tbl */
  509. static struct qla_boards ql1280_board_tbl[] = {
  510. {.name = "QLA12160", .numPorts = 2, .fw_index = 2},
  511. {.name = "QLA1040" , .numPorts = 1, .fw_index = 0},
  512. {.name = "QLA1080" , .numPorts = 1, .fw_index = 1},
  513. {.name = "QLA1240" , .numPorts = 2, .fw_index = 1},
  514. {.name = "QLA1280" , .numPorts = 2, .fw_index = 1},
  515. {.name = "QLA10160", .numPorts = 1, .fw_index = 2},
  516. {.name = " ", .numPorts = 0, .fw_index = -1},
  517. };
  518. static int qla1280_verbose = 1;
  519. #if DEBUG_QLA1280
  520. static int ql_debug_level = 1;
  521. #define dprintk(level, format, a...) \
  522. do { if (ql_debug_level >= level) printk(KERN_ERR format, ##a); } while(0)
  523. #define qla1280_dump_buffer(level, buf, size) \
  524. if (ql_debug_level >= level) __qla1280_dump_buffer(buf, size)
  525. #define qla1280_print_scsi_cmd(level, cmd) \
  526. if (ql_debug_level >= level) __qla1280_print_scsi_cmd(cmd)
  527. #else
  528. #define ql_debug_level 0
  529. #define dprintk(level, format, a...) do{}while(0)
  530. #define qla1280_dump_buffer(a, b, c) do{}while(0)
  531. #define qla1280_print_scsi_cmd(a, b) do{}while(0)
  532. #endif
  533. #define ENTER(x) dprintk(3, "qla1280 : Entering %s()\n", x);
  534. #define LEAVE(x) dprintk(3, "qla1280 : Leaving %s()\n", x);
  535. #define ENTER_INTR(x) dprintk(4, "qla1280 : Entering %s()\n", x);
  536. #define LEAVE_INTR(x) dprintk(4, "qla1280 : Leaving %s()\n", x);
  537. static int qla1280_read_nvram(struct scsi_qla_host *ha)
  538. {
  539. uint16_t *wptr;
  540. uint8_t chksum;
  541. int cnt, i;
  542. struct nvram *nv;
  543. ENTER("qla1280_read_nvram");
  544. if (driver_setup.no_nvram)
  545. return 1;
  546. printk(KERN_INFO "scsi(%ld): Reading NVRAM\n", ha->host_no);
  547. wptr = (uint16_t *)&ha->nvram;
  548. nv = &ha->nvram;
  549. chksum = 0;
  550. for (cnt = 0; cnt < 3; cnt++) {
  551. *wptr = qla1280_get_nvram_word(ha, cnt);
  552. chksum += *wptr & 0xff;
  553. chksum += (*wptr >> 8) & 0xff;
  554. wptr++;
  555. }
  556. if (nv->id0 != 'I' || nv->id1 != 'S' ||
  557. nv->id2 != 'P' || nv->id3 != ' ' || nv->version < 1) {
  558. dprintk(2, "Invalid nvram ID or version!\n");
  559. chksum = 1;
  560. } else {
  561. for (; cnt < sizeof(struct nvram); cnt++) {
  562. *wptr = qla1280_get_nvram_word(ha, cnt);
  563. chksum += *wptr & 0xff;
  564. chksum += (*wptr >> 8) & 0xff;
  565. wptr++;
  566. }
  567. }
  568. dprintk(3, "qla1280_read_nvram: NVRAM Magic ID= %c %c %c %02x"
  569. " version %i\n", nv->id0, nv->id1, nv->id2, nv->id3,
  570. nv->version);
  571. if (chksum) {
  572. if (!driver_setup.no_nvram)
  573. printk(KERN_WARNING "scsi(%ld): Unable to identify or "
  574. "validate NVRAM checksum, using default "
  575. "settings\n", ha->host_no);
  576. ha->nvram_valid = 0;
  577. } else
  578. ha->nvram_valid = 1;
  579. /* The firmware interface is, um, interesting, in that the
  580. * actual firmware image on the chip is little endian, thus,
  581. * the process of taking that image to the CPU would end up
  582. * little endian. However, the firmware interface requires it
  583. * to be read a word (two bytes) at a time.
  584. *
  585. * The net result of this would be that the word (and
  586. * doubleword) quantites in the firmware would be correct, but
  587. * the bytes would be pairwise reversed. Since most of the
  588. * firmware quantites are, in fact, bytes, we do an extra
  589. * le16_to_cpu() in the firmware read routine.
  590. *
  591. * The upshot of all this is that the bytes in the firmware
  592. * are in the correct places, but the 16 and 32 bit quantites
  593. * are still in little endian format. We fix that up below by
  594. * doing extra reverses on them */
  595. nv->isp_parameter = cpu_to_le16(nv->isp_parameter);
  596. nv->firmware_feature.w = cpu_to_le16(nv->firmware_feature.w);
  597. for(i = 0; i < MAX_BUSES; i++) {
  598. nv->bus[i].selection_timeout = cpu_to_le16(nv->bus[i].selection_timeout);
  599. nv->bus[i].max_queue_depth = cpu_to_le16(nv->bus[i].max_queue_depth);
  600. }
  601. dprintk(1, "qla1280_read_nvram: Completed Reading NVRAM\n");
  602. LEAVE("qla1280_read_nvram");
  603. return chksum;
  604. }
  605. /**************************************************************************
  606. * qla1280_info
  607. * Return a string describing the driver.
  608. **************************************************************************/
  609. static const char *
  610. qla1280_info(struct Scsi_Host *host)
  611. {
  612. static char qla1280_scsi_name_buffer[125];
  613. char *bp;
  614. struct scsi_qla_host *ha;
  615. struct qla_boards *bdp;
  616. bp = &qla1280_scsi_name_buffer[0];
  617. ha = (struct scsi_qla_host *)host->hostdata;
  618. bdp = &ql1280_board_tbl[ha->devnum];
  619. memset(bp, 0, sizeof(qla1280_scsi_name_buffer));
  620. sprintf (bp,
  621. "QLogic %s PCI to SCSI Host Adapter\n"
  622. " Firmware version: %2d.%02d.%02d, Driver version %s",
  623. &bdp->name[0], ha->fwver1, ha->fwver2, ha->fwver3,
  624. QLA1280_VERSION);
  625. return bp;
  626. }
  627. /**************************************************************************
  628. * qla1280_queuecommand
  629. * Queue a command to the controller.
  630. *
  631. * Note:
  632. * The mid-level driver tries to ensures that queuecommand never gets invoked
  633. * concurrently with itself or the interrupt handler (although the
  634. * interrupt handler may call this routine as part of request-completion
  635. * handling). Unfortunely, it sometimes calls the scheduler in interrupt
  636. * context which is a big NO! NO!.
  637. **************************************************************************/
  638. static int
  639. qla1280_queuecommand_lck(struct scsi_cmnd *cmd, void (*fn)(struct scsi_cmnd *))
  640. {
  641. struct Scsi_Host *host = cmd->device->host;
  642. struct scsi_qla_host *ha = (struct scsi_qla_host *)host->hostdata;
  643. struct srb *sp = (struct srb *)CMD_SP(cmd);
  644. int status;
  645. cmd->scsi_done = fn;
  646. sp->cmd = cmd;
  647. sp->flags = 0;
  648. sp->wait = NULL;
  649. CMD_HANDLE(cmd) = (unsigned char *)NULL;
  650. qla1280_print_scsi_cmd(5, cmd);
  651. #ifdef QLA_64BIT_PTR
  652. /*
  653. * Using 64 bit commands if the PCI bridge doesn't support it is a
  654. * bit wasteful, however this should really only happen if one's
  655. * PCI controller is completely broken, like the BCM1250. For
  656. * sane hardware this is not an issue.
  657. */
  658. status = qla1280_64bit_start_scsi(ha, sp);
  659. #else
  660. status = qla1280_32bit_start_scsi(ha, sp);
  661. #endif
  662. return status;
  663. }
  664. static DEF_SCSI_QCMD(qla1280_queuecommand)
  665. enum action {
  666. ABORT_COMMAND,
  667. DEVICE_RESET,
  668. BUS_RESET,
  669. ADAPTER_RESET,
  670. };
  671. static void qla1280_mailbox_timeout(struct timer_list *t)
  672. {
  673. struct scsi_qla_host *ha = from_timer(ha, t, mailbox_timer);
  674. struct device_reg __iomem *reg;
  675. reg = ha->iobase;
  676. ha->mailbox_out[0] = RD_REG_WORD(&reg->mailbox0);
  677. printk(KERN_ERR "scsi(%ld): mailbox timed out, mailbox0 %04x, "
  678. "ictrl %04x, istatus %04x\n", ha->host_no, ha->mailbox_out[0],
  679. RD_REG_WORD(&reg->ictrl), RD_REG_WORD(&reg->istatus));
  680. complete(ha->mailbox_wait);
  681. }
  682. static int
  683. _qla1280_wait_for_single_command(struct scsi_qla_host *ha, struct srb *sp,
  684. struct completion *wait)
  685. {
  686. int status = FAILED;
  687. struct scsi_cmnd *cmd = sp->cmd;
  688. spin_unlock_irq(ha->host->host_lock);
  689. wait_for_completion_timeout(wait, 4*HZ);
  690. spin_lock_irq(ha->host->host_lock);
  691. sp->wait = NULL;
  692. if(CMD_HANDLE(cmd) == COMPLETED_HANDLE) {
  693. status = SUCCESS;
  694. (*cmd->scsi_done)(cmd);
  695. }
  696. return status;
  697. }
  698. static int
  699. qla1280_wait_for_single_command(struct scsi_qla_host *ha, struct srb *sp)
  700. {
  701. DECLARE_COMPLETION_ONSTACK(wait);
  702. sp->wait = &wait;
  703. return _qla1280_wait_for_single_command(ha, sp, &wait);
  704. }
  705. static int
  706. qla1280_wait_for_pending_commands(struct scsi_qla_host *ha, int bus, int target)
  707. {
  708. int cnt;
  709. int status;
  710. struct srb *sp;
  711. struct scsi_cmnd *cmd;
  712. status = SUCCESS;
  713. /*
  714. * Wait for all commands with the designated bus/target
  715. * to be completed by the firmware
  716. */
  717. for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  718. sp = ha->outstanding_cmds[cnt];
  719. if (sp) {
  720. cmd = sp->cmd;
  721. if (bus >= 0 && SCSI_BUS_32(cmd) != bus)
  722. continue;
  723. if (target >= 0 && SCSI_TCN_32(cmd) != target)
  724. continue;
  725. status = qla1280_wait_for_single_command(ha, sp);
  726. if (status == FAILED)
  727. break;
  728. }
  729. }
  730. return status;
  731. }
  732. /**************************************************************************
  733. * qla1280_error_action
  734. * The function will attempt to perform a specified error action and
  735. * wait for the results (or time out).
  736. *
  737. * Input:
  738. * cmd = Linux SCSI command packet of the command that cause the
  739. * bus reset.
  740. * action = error action to take (see action_t)
  741. *
  742. * Returns:
  743. * SUCCESS or FAILED
  744. *
  745. **************************************************************************/
  746. static int
  747. qla1280_error_action(struct scsi_cmnd *cmd, enum action action)
  748. {
  749. struct scsi_qla_host *ha;
  750. int bus, target, lun;
  751. struct srb *sp;
  752. int i, found;
  753. int result=FAILED;
  754. int wait_for_bus=-1;
  755. int wait_for_target = -1;
  756. DECLARE_COMPLETION_ONSTACK(wait);
  757. ENTER("qla1280_error_action");
  758. ha = (struct scsi_qla_host *)(CMD_HOST(cmd)->hostdata);
  759. sp = (struct srb *)CMD_SP(cmd);
  760. bus = SCSI_BUS_32(cmd);
  761. target = SCSI_TCN_32(cmd);
  762. lun = SCSI_LUN_32(cmd);
  763. dprintk(4, "error_action %i, istatus 0x%04x\n", action,
  764. RD_REG_WORD(&ha->iobase->istatus));
  765. dprintk(4, "host_cmd 0x%04x, ictrl 0x%04x, jiffies %li\n",
  766. RD_REG_WORD(&ha->iobase->host_cmd),
  767. RD_REG_WORD(&ha->iobase->ictrl), jiffies);
  768. if (qla1280_verbose)
  769. printk(KERN_INFO "scsi(%li): Resetting Cmnd=0x%p, "
  770. "Handle=0x%p, action=0x%x\n",
  771. ha->host_no, cmd, CMD_HANDLE(cmd), action);
  772. /*
  773. * Check to see if we have the command in the outstanding_cmds[]
  774. * array. If not then it must have completed before this error
  775. * action was initiated. If the error_action isn't ABORT_COMMAND
  776. * then the driver must proceed with the requested action.
  777. */
  778. found = -1;
  779. for (i = 0; i < MAX_OUTSTANDING_COMMANDS; i++) {
  780. if (sp == ha->outstanding_cmds[i]) {
  781. found = i;
  782. sp->wait = &wait; /* we'll wait for it to complete */
  783. break;
  784. }
  785. }
  786. if (found < 0) { /* driver doesn't have command */
  787. result = SUCCESS;
  788. if (qla1280_verbose) {
  789. printk(KERN_INFO
  790. "scsi(%ld:%d:%d:%d): specified command has "
  791. "already completed.\n", ha->host_no, bus,
  792. target, lun);
  793. }
  794. }
  795. switch (action) {
  796. case ABORT_COMMAND:
  797. dprintk(1, "qla1280: RISC aborting command\n");
  798. /*
  799. * The abort might fail due to race when the host_lock
  800. * is released to issue the abort. As such, we
  801. * don't bother to check the return status.
  802. */
  803. if (found >= 0)
  804. qla1280_abort_command(ha, sp, found);
  805. break;
  806. case DEVICE_RESET:
  807. if (qla1280_verbose)
  808. printk(KERN_INFO
  809. "scsi(%ld:%d:%d:%d): Queueing device reset "
  810. "command.\n", ha->host_no, bus, target, lun);
  811. if (qla1280_device_reset(ha, bus, target) == 0) {
  812. /* issued device reset, set wait conditions */
  813. wait_for_bus = bus;
  814. wait_for_target = target;
  815. }
  816. break;
  817. case BUS_RESET:
  818. if (qla1280_verbose)
  819. printk(KERN_INFO "qla1280(%ld:%d): Issued bus "
  820. "reset.\n", ha->host_no, bus);
  821. if (qla1280_bus_reset(ha, bus) == 0) {
  822. /* issued bus reset, set wait conditions */
  823. wait_for_bus = bus;
  824. }
  825. break;
  826. case ADAPTER_RESET:
  827. default:
  828. if (qla1280_verbose) {
  829. printk(KERN_INFO
  830. "scsi(%ld): Issued ADAPTER RESET\n",
  831. ha->host_no);
  832. printk(KERN_INFO "scsi(%ld): I/O processing will "
  833. "continue automatically\n", ha->host_no);
  834. }
  835. ha->flags.reset_active = 1;
  836. if (qla1280_abort_isp(ha) != 0) { /* it's dead */
  837. result = FAILED;
  838. }
  839. ha->flags.reset_active = 0;
  840. }
  841. /*
  842. * At this point, the host_lock has been released and retaken
  843. * by the issuance of the mailbox command.
  844. * Wait for the command passed in by the mid-layer if it
  845. * was found by the driver. It might have been returned
  846. * between eh recovery steps, hence the check of the "found"
  847. * variable.
  848. */
  849. if (found >= 0)
  850. result = _qla1280_wait_for_single_command(ha, sp, &wait);
  851. if (action == ABORT_COMMAND && result != SUCCESS) {
  852. printk(KERN_WARNING
  853. "scsi(%li:%i:%i:%i): "
  854. "Unable to abort command!\n",
  855. ha->host_no, bus, target, lun);
  856. }
  857. /*
  858. * If the command passed in by the mid-layer has been
  859. * returned by the board, then wait for any additional
  860. * commands which are supposed to complete based upon
  861. * the error action.
  862. *
  863. * All commands are unconditionally returned during a
  864. * call to qla1280_abort_isp(), ADAPTER_RESET. No need
  865. * to wait for them.
  866. */
  867. if (result == SUCCESS && wait_for_bus >= 0) {
  868. result = qla1280_wait_for_pending_commands(ha,
  869. wait_for_bus, wait_for_target);
  870. }
  871. dprintk(1, "RESET returning %d\n", result);
  872. LEAVE("qla1280_error_action");
  873. return result;
  874. }
  875. /**************************************************************************
  876. * qla1280_abort
  877. * Abort the specified SCSI command(s).
  878. **************************************************************************/
  879. static int
  880. qla1280_eh_abort(struct scsi_cmnd * cmd)
  881. {
  882. int rc;
  883. spin_lock_irq(cmd->device->host->host_lock);
  884. rc = qla1280_error_action(cmd, ABORT_COMMAND);
  885. spin_unlock_irq(cmd->device->host->host_lock);
  886. return rc;
  887. }
  888. /**************************************************************************
  889. * qla1280_device_reset
  890. * Reset the specified SCSI device
  891. **************************************************************************/
  892. static int
  893. qla1280_eh_device_reset(struct scsi_cmnd *cmd)
  894. {
  895. int rc;
  896. spin_lock_irq(cmd->device->host->host_lock);
  897. rc = qla1280_error_action(cmd, DEVICE_RESET);
  898. spin_unlock_irq(cmd->device->host->host_lock);
  899. return rc;
  900. }
  901. /**************************************************************************
  902. * qla1280_bus_reset
  903. * Reset the specified bus.
  904. **************************************************************************/
  905. static int
  906. qla1280_eh_bus_reset(struct scsi_cmnd *cmd)
  907. {
  908. int rc;
  909. spin_lock_irq(cmd->device->host->host_lock);
  910. rc = qla1280_error_action(cmd, BUS_RESET);
  911. spin_unlock_irq(cmd->device->host->host_lock);
  912. return rc;
  913. }
  914. /**************************************************************************
  915. * qla1280_adapter_reset
  916. * Reset the specified adapter (both channels)
  917. **************************************************************************/
  918. static int
  919. qla1280_eh_adapter_reset(struct scsi_cmnd *cmd)
  920. {
  921. int rc;
  922. spin_lock_irq(cmd->device->host->host_lock);
  923. rc = qla1280_error_action(cmd, ADAPTER_RESET);
  924. spin_unlock_irq(cmd->device->host->host_lock);
  925. return rc;
  926. }
  927. static int
  928. qla1280_biosparam(struct scsi_device *sdev, struct block_device *bdev,
  929. sector_t capacity, int geom[])
  930. {
  931. int heads, sectors, cylinders;
  932. heads = 64;
  933. sectors = 32;
  934. cylinders = (unsigned long)capacity / (heads * sectors);
  935. if (cylinders > 1024) {
  936. heads = 255;
  937. sectors = 63;
  938. cylinders = (unsigned long)capacity / (heads * sectors);
  939. /* if (cylinders > 1023)
  940. cylinders = 1023; */
  941. }
  942. geom[0] = heads;
  943. geom[1] = sectors;
  944. geom[2] = cylinders;
  945. return 0;
  946. }
  947. /* disable risc and host interrupts */
  948. static inline void
  949. qla1280_disable_intrs(struct scsi_qla_host *ha)
  950. {
  951. WRT_REG_WORD(&ha->iobase->ictrl, 0);
  952. RD_REG_WORD(&ha->iobase->ictrl); /* PCI Posted Write flush */
  953. }
  954. /* enable risc and host interrupts */
  955. static inline void
  956. qla1280_enable_intrs(struct scsi_qla_host *ha)
  957. {
  958. WRT_REG_WORD(&ha->iobase->ictrl, (ISP_EN_INT | ISP_EN_RISC));
  959. RD_REG_WORD(&ha->iobase->ictrl); /* PCI Posted Write flush */
  960. }
  961. /**************************************************************************
  962. * qla1280_intr_handler
  963. * Handles the H/W interrupt
  964. **************************************************************************/
  965. static irqreturn_t
  966. qla1280_intr_handler(int irq, void *dev_id)
  967. {
  968. struct scsi_qla_host *ha;
  969. struct device_reg __iomem *reg;
  970. u16 data;
  971. int handled = 0;
  972. ENTER_INTR ("qla1280_intr_handler");
  973. ha = (struct scsi_qla_host *)dev_id;
  974. spin_lock(ha->host->host_lock);
  975. ha->isr_count++;
  976. reg = ha->iobase;
  977. qla1280_disable_intrs(ha);
  978. data = qla1280_debounce_register(&reg->istatus);
  979. /* Check for pending interrupts. */
  980. if (data & RISC_INT) {
  981. qla1280_isr(ha, &ha->done_q);
  982. handled = 1;
  983. }
  984. if (!list_empty(&ha->done_q))
  985. qla1280_done(ha);
  986. spin_unlock(ha->host->host_lock);
  987. qla1280_enable_intrs(ha);
  988. LEAVE_INTR("qla1280_intr_handler");
  989. return IRQ_RETVAL(handled);
  990. }
  991. static int
  992. qla1280_set_target_parameters(struct scsi_qla_host *ha, int bus, int target)
  993. {
  994. uint8_t mr;
  995. uint16_t mb[MAILBOX_REGISTER_COUNT];
  996. struct nvram *nv;
  997. int status, lun;
  998. nv = &ha->nvram;
  999. mr = BIT_3 | BIT_2 | BIT_1 | BIT_0;
  1000. /* Set Target Parameters. */
  1001. mb[0] = MBC_SET_TARGET_PARAMETERS;
  1002. mb[1] = (uint16_t)((bus ? target | BIT_7 : target) << 8);
  1003. mb[2] = nv->bus[bus].target[target].parameter.renegotiate_on_error << 8;
  1004. mb[2] |= nv->bus[bus].target[target].parameter.stop_queue_on_check << 9;
  1005. mb[2] |= nv->bus[bus].target[target].parameter.auto_request_sense << 10;
  1006. mb[2] |= nv->bus[bus].target[target].parameter.tag_queuing << 11;
  1007. mb[2] |= nv->bus[bus].target[target].parameter.enable_sync << 12;
  1008. mb[2] |= nv->bus[bus].target[target].parameter.enable_wide << 13;
  1009. mb[2] |= nv->bus[bus].target[target].parameter.parity_checking << 14;
  1010. mb[2] |= nv->bus[bus].target[target].parameter.disconnect_allowed << 15;
  1011. if (IS_ISP1x160(ha)) {
  1012. mb[2] |= nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr << 5;
  1013. mb[3] = (nv->bus[bus].target[target].flags.flags1x160.sync_offset << 8);
  1014. mb[6] = (nv->bus[bus].target[target].ppr_1x160.flags.ppr_options << 8) |
  1015. nv->bus[bus].target[target].ppr_1x160.flags.ppr_bus_width;
  1016. mr |= BIT_6;
  1017. } else {
  1018. mb[3] = (nv->bus[bus].target[target].flags.flags1x80.sync_offset << 8);
  1019. }
  1020. mb[3] |= nv->bus[bus].target[target].sync_period;
  1021. status = qla1280_mailbox_command(ha, mr, mb);
  1022. /* Set Device Queue Parameters. */
  1023. for (lun = 0; lun < MAX_LUNS; lun++) {
  1024. mb[0] = MBC_SET_DEVICE_QUEUE;
  1025. mb[1] = (uint16_t)((bus ? target | BIT_7 : target) << 8);
  1026. mb[1] |= lun;
  1027. mb[2] = nv->bus[bus].max_queue_depth;
  1028. mb[3] = nv->bus[bus].target[target].execution_throttle;
  1029. status |= qla1280_mailbox_command(ha, 0x0f, mb);
  1030. }
  1031. if (status)
  1032. printk(KERN_WARNING "scsi(%ld:%i:%i): "
  1033. "qla1280_set_target_parameters() failed\n",
  1034. ha->host_no, bus, target);
  1035. return status;
  1036. }
  1037. /**************************************************************************
  1038. * qla1280_slave_configure
  1039. *
  1040. * Description:
  1041. * Determines the queue depth for a given device. There are two ways
  1042. * a queue depth can be obtained for a tagged queueing device. One
  1043. * way is the default queue depth which is determined by whether
  1044. * If it is defined, then it is used
  1045. * as the default queue depth. Otherwise, we use either 4 or 8 as the
  1046. * default queue depth (dependent on the number of hardware SCBs).
  1047. **************************************************************************/
  1048. static int
  1049. qla1280_slave_configure(struct scsi_device *device)
  1050. {
  1051. struct scsi_qla_host *ha;
  1052. int default_depth = 3;
  1053. int bus = device->channel;
  1054. int target = device->id;
  1055. int status = 0;
  1056. struct nvram *nv;
  1057. unsigned long flags;
  1058. ha = (struct scsi_qla_host *)device->host->hostdata;
  1059. nv = &ha->nvram;
  1060. if (qla1280_check_for_dead_scsi_bus(ha, bus))
  1061. return 1;
  1062. if (device->tagged_supported &&
  1063. (ha->bus_settings[bus].qtag_enables & (BIT_0 << target))) {
  1064. scsi_change_queue_depth(device, ha->bus_settings[bus].hiwat);
  1065. } else {
  1066. scsi_change_queue_depth(device, default_depth);
  1067. }
  1068. nv->bus[bus].target[target].parameter.enable_sync = device->sdtr;
  1069. nv->bus[bus].target[target].parameter.enable_wide = device->wdtr;
  1070. nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr = device->ppr;
  1071. if (driver_setup.no_sync ||
  1072. (driver_setup.sync_mask &&
  1073. (~driver_setup.sync_mask & (1 << target))))
  1074. nv->bus[bus].target[target].parameter.enable_sync = 0;
  1075. if (driver_setup.no_wide ||
  1076. (driver_setup.wide_mask &&
  1077. (~driver_setup.wide_mask & (1 << target))))
  1078. nv->bus[bus].target[target].parameter.enable_wide = 0;
  1079. if (IS_ISP1x160(ha)) {
  1080. if (driver_setup.no_ppr ||
  1081. (driver_setup.ppr_mask &&
  1082. (~driver_setup.ppr_mask & (1 << target))))
  1083. nv->bus[bus].target[target].ppr_1x160.flags.enable_ppr = 0;
  1084. }
  1085. spin_lock_irqsave(ha->host->host_lock, flags);
  1086. if (nv->bus[bus].target[target].parameter.enable_sync)
  1087. status = qla1280_set_target_parameters(ha, bus, target);
  1088. qla1280_get_target_parameters(ha, device);
  1089. spin_unlock_irqrestore(ha->host->host_lock, flags);
  1090. return status;
  1091. }
  1092. /*
  1093. * qla1280_done
  1094. * Process completed commands.
  1095. *
  1096. * Input:
  1097. * ha = adapter block pointer.
  1098. */
  1099. static void
  1100. qla1280_done(struct scsi_qla_host *ha)
  1101. {
  1102. struct srb *sp;
  1103. struct list_head *done_q;
  1104. int bus, target, lun;
  1105. struct scsi_cmnd *cmd;
  1106. ENTER("qla1280_done");
  1107. done_q = &ha->done_q;
  1108. while (!list_empty(done_q)) {
  1109. sp = list_entry(done_q->next, struct srb, list);
  1110. list_del(&sp->list);
  1111. cmd = sp->cmd;
  1112. bus = SCSI_BUS_32(cmd);
  1113. target = SCSI_TCN_32(cmd);
  1114. lun = SCSI_LUN_32(cmd);
  1115. switch ((CMD_RESULT(cmd) >> 16)) {
  1116. case DID_RESET:
  1117. /* Issue marker command. */
  1118. if (!ha->flags.abort_isp_active)
  1119. qla1280_marker(ha, bus, target, 0, MK_SYNC_ID);
  1120. break;
  1121. case DID_ABORT:
  1122. sp->flags &= ~SRB_ABORT_PENDING;
  1123. sp->flags |= SRB_ABORTED;
  1124. break;
  1125. default:
  1126. break;
  1127. }
  1128. /* Release memory used for this I/O */
  1129. scsi_dma_unmap(cmd);
  1130. /* Call the mid-level driver interrupt handler */
  1131. ha->actthreads--;
  1132. if (sp->wait == NULL)
  1133. (*(cmd)->scsi_done)(cmd);
  1134. else
  1135. complete(sp->wait);
  1136. }
  1137. LEAVE("qla1280_done");
  1138. }
  1139. /*
  1140. * Translates a ISP error to a Linux SCSI error
  1141. */
  1142. static int
  1143. qla1280_return_status(struct response * sts, struct scsi_cmnd *cp)
  1144. {
  1145. int host_status = DID_ERROR;
  1146. uint16_t comp_status = le16_to_cpu(sts->comp_status);
  1147. uint16_t state_flags = le16_to_cpu(sts->state_flags);
  1148. uint32_t residual_length = le32_to_cpu(sts->residual_length);
  1149. uint16_t scsi_status = le16_to_cpu(sts->scsi_status);
  1150. #if DEBUG_QLA1280_INTR
  1151. static char *reason[] = {
  1152. "DID_OK",
  1153. "DID_NO_CONNECT",
  1154. "DID_BUS_BUSY",
  1155. "DID_TIME_OUT",
  1156. "DID_BAD_TARGET",
  1157. "DID_ABORT",
  1158. "DID_PARITY",
  1159. "DID_ERROR",
  1160. "DID_RESET",
  1161. "DID_BAD_INTR"
  1162. };
  1163. #endif /* DEBUG_QLA1280_INTR */
  1164. ENTER("qla1280_return_status");
  1165. #if DEBUG_QLA1280_INTR
  1166. /*
  1167. dprintk(1, "qla1280_return_status: compl status = 0x%04x\n",
  1168. comp_status);
  1169. */
  1170. #endif
  1171. switch (comp_status) {
  1172. case CS_COMPLETE:
  1173. host_status = DID_OK;
  1174. break;
  1175. case CS_INCOMPLETE:
  1176. if (!(state_flags & SF_GOT_BUS))
  1177. host_status = DID_NO_CONNECT;
  1178. else if (!(state_flags & SF_GOT_TARGET))
  1179. host_status = DID_BAD_TARGET;
  1180. else if (!(state_flags & SF_SENT_CDB))
  1181. host_status = DID_ERROR;
  1182. else if (!(state_flags & SF_TRANSFERRED_DATA))
  1183. host_status = DID_ERROR;
  1184. else if (!(state_flags & SF_GOT_STATUS))
  1185. host_status = DID_ERROR;
  1186. else if (!(state_flags & SF_GOT_SENSE))
  1187. host_status = DID_ERROR;
  1188. break;
  1189. case CS_RESET:
  1190. host_status = DID_RESET;
  1191. break;
  1192. case CS_ABORTED:
  1193. host_status = DID_ABORT;
  1194. break;
  1195. case CS_TIMEOUT:
  1196. host_status = DID_TIME_OUT;
  1197. break;
  1198. case CS_DATA_OVERRUN:
  1199. dprintk(2, "Data overrun 0x%x\n", residual_length);
  1200. dprintk(2, "qla1280_return_status: response packet data\n");
  1201. qla1280_dump_buffer(2, (char *)sts, RESPONSE_ENTRY_SIZE);
  1202. host_status = DID_ERROR;
  1203. break;
  1204. case CS_DATA_UNDERRUN:
  1205. if ((scsi_bufflen(cp) - residual_length) <
  1206. cp->underflow) {
  1207. printk(KERN_WARNING
  1208. "scsi: Underflow detected - retrying "
  1209. "command.\n");
  1210. host_status = DID_ERROR;
  1211. } else {
  1212. scsi_set_resid(cp, residual_length);
  1213. host_status = DID_OK;
  1214. }
  1215. break;
  1216. default:
  1217. host_status = DID_ERROR;
  1218. break;
  1219. }
  1220. #if DEBUG_QLA1280_INTR
  1221. dprintk(1, "qla1280 ISP status: host status (%s) scsi status %x\n",
  1222. reason[host_status], scsi_status);
  1223. #endif
  1224. LEAVE("qla1280_return_status");
  1225. return (scsi_status & 0xff) | (host_status << 16);
  1226. }
  1227. /****************************************************************************/
  1228. /* QLogic ISP1280 Hardware Support Functions. */
  1229. /****************************************************************************/
  1230. /*
  1231. * qla1280_initialize_adapter
  1232. * Initialize board.
  1233. *
  1234. * Input:
  1235. * ha = adapter block pointer.
  1236. *
  1237. * Returns:
  1238. * 0 = success
  1239. */
  1240. static int
  1241. qla1280_initialize_adapter(struct scsi_qla_host *ha)
  1242. {
  1243. struct device_reg __iomem *reg;
  1244. int status;
  1245. int bus;
  1246. unsigned long flags;
  1247. ENTER("qla1280_initialize_adapter");
  1248. /* Clear adapter flags. */
  1249. ha->flags.online = 0;
  1250. ha->flags.disable_host_adapter = 0;
  1251. ha->flags.reset_active = 0;
  1252. ha->flags.abort_isp_active = 0;
  1253. /* TODO: implement support for the 1040 nvram format */
  1254. if (IS_ISP1040(ha))
  1255. driver_setup.no_nvram = 1;
  1256. dprintk(1, "Configure PCI space for adapter...\n");
  1257. reg = ha->iobase;
  1258. /* Insure mailbox registers are free. */
  1259. WRT_REG_WORD(&reg->semaphore, 0);
  1260. WRT_REG_WORD(&reg->host_cmd, HC_CLR_RISC_INT);
  1261. WRT_REG_WORD(&reg->host_cmd, HC_CLR_HOST_INT);
  1262. RD_REG_WORD(&reg->host_cmd);
  1263. if (qla1280_read_nvram(ha)) {
  1264. dprintk(2, "qla1280_initialize_adapter: failed to read "
  1265. "NVRAM\n");
  1266. }
  1267. /*
  1268. * It's necessary to grab the spin here as qla1280_mailbox_command
  1269. * needs to be able to drop the lock unconditionally to wait
  1270. * for completion.
  1271. */
  1272. spin_lock_irqsave(ha->host->host_lock, flags);
  1273. status = qla1280_load_firmware(ha);
  1274. if (status) {
  1275. printk(KERN_ERR "scsi(%li): initialize: pci probe failed!\n",
  1276. ha->host_no);
  1277. goto out;
  1278. }
  1279. /* Setup adapter based on NVRAM parameters. */
  1280. dprintk(1, "scsi(%ld): Configure NVRAM parameters\n", ha->host_no);
  1281. qla1280_nvram_config(ha);
  1282. if (ha->flags.disable_host_adapter) {
  1283. status = 1;
  1284. goto out;
  1285. }
  1286. status = qla1280_init_rings(ha);
  1287. if (status)
  1288. goto out;
  1289. /* Issue SCSI reset, if we can't reset twice then bus is dead */
  1290. for (bus = 0; bus < ha->ports; bus++) {
  1291. if (!ha->bus_settings[bus].disable_scsi_reset &&
  1292. qla1280_bus_reset(ha, bus) &&
  1293. qla1280_bus_reset(ha, bus))
  1294. ha->bus_settings[bus].scsi_bus_dead = 1;
  1295. }
  1296. ha->flags.online = 1;
  1297. out:
  1298. spin_unlock_irqrestore(ha->host->host_lock, flags);
  1299. if (status)
  1300. dprintk(2, "qla1280_initialize_adapter: **** FAILED ****\n");
  1301. LEAVE("qla1280_initialize_adapter");
  1302. return status;
  1303. }
  1304. /*
  1305. * qla1280_request_firmware
  1306. * Acquire firmware for chip. Retain in memory
  1307. * for error recovery.
  1308. *
  1309. * Input:
  1310. * ha = adapter block pointer.
  1311. *
  1312. * Returns:
  1313. * Pointer to firmware image or an error code
  1314. * cast to pointer via ERR_PTR().
  1315. */
  1316. static const struct firmware *
  1317. qla1280_request_firmware(struct scsi_qla_host *ha)
  1318. {
  1319. const struct firmware *fw;
  1320. int err;
  1321. int index;
  1322. char *fwname;
  1323. spin_unlock_irq(ha->host->host_lock);
  1324. mutex_lock(&qla1280_firmware_mutex);
  1325. index = ql1280_board_tbl[ha->devnum].fw_index;
  1326. fw = qla1280_fw_tbl[index].fw;
  1327. if (fw)
  1328. goto out;
  1329. fwname = qla1280_fw_tbl[index].fwname;
  1330. err = request_firmware(&fw, fwname, &ha->pdev->dev);
  1331. if (err) {
  1332. printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
  1333. fwname, err);
  1334. fw = ERR_PTR(err);
  1335. goto unlock;
  1336. }
  1337. if ((fw->size % 2) || (fw->size < 6)) {
  1338. printk(KERN_ERR "Invalid firmware length %zu in image \"%s\"\n",
  1339. fw->size, fwname);
  1340. release_firmware(fw);
  1341. fw = ERR_PTR(