PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/scsi/wd7000.c

http://github.com/mirrors/linux
C | 1657 lines | 979 code | 235 blank | 443 comment | 141 complexity | d26c6e249b378527146c3214fddded24 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /* $Id: $
  2. * linux/drivers/scsi/wd7000.c
  3. *
  4. * Copyright (C) 1992 Thomas Wuensche
  5. * closely related to the aha1542 driver from Tommy Thorn
  6. * ( as close as different hardware allows on a lowlevel-driver :-) )
  7. *
  8. * Revised (and renamed) by John Boyd <boyd@cis.ohio-state.edu> to
  9. * accommodate Eric Youngdale's modifications to scsi.c. Nov 1992.
  10. *
  11. * Additional changes to support scatter/gather. Dec. 1992. tw/jb
  12. *
  13. * No longer tries to reset SCSI bus at boot (it wasn't working anyway).
  14. * Rewritten to support multiple host adapters.
  15. * Miscellaneous cleanup.
  16. * So far, still doesn't do reset or abort correctly, since I have no idea
  17. * how to do them with this board (8^(. Jan 1994 jb
  18. *
  19. * This driver now supports both of the two standard configurations (per
  20. * the 3.36 Owner's Manual, my latest reference) by the same method as
  21. * before; namely, by looking for a BIOS signature. Thus, the location of
  22. * the BIOS signature determines the board configuration. Until I have
  23. * time to do something more flexible, users should stick to one of the
  24. * following:
  25. *
  26. * Standard configuration for single-adapter systems:
  27. * - BIOS at CE00h
  28. * - I/O base address 350h
  29. * - IRQ level 15
  30. * - DMA channel 6
  31. * Standard configuration for a second adapter in a system:
  32. * - BIOS at C800h
  33. * - I/O base address 330h
  34. * - IRQ level 11
  35. * - DMA channel 5
  36. *
  37. * Anyone who can recompile the kernel is welcome to add others as need
  38. * arises, but unpredictable results may occur if there are conflicts.
  39. * In any event, if there are multiple adapters in a system, they MUST
  40. * use different I/O bases, IRQ levels, and DMA channels, since they will be
  41. * indistinguishable (and in direct conflict) otherwise.
  42. *
  43. * As a point of information, the NO_OP command toggles the CMD_RDY bit
  44. * of the status port, and this fact could be used as a test for the I/O
  45. * base address (or more generally, board detection). There is an interrupt
  46. * status port, so IRQ probing could also be done. I suppose the full
  47. * DMA diagnostic could be used to detect the DMA channel being used. I
  48. * haven't done any of this, though, because I think there's too much of
  49. * a chance that such explorations could be destructive, if some other
  50. * board's resources are used inadvertently. So, call me a wimp, but I
  51. * don't want to try it. The only kind of exploration I trust is memory
  52. * exploration, since it's more certain that reading memory won't be
  53. * destructive.
  54. *
  55. * More to my liking would be a LILO boot command line specification, such
  56. * as is used by the aha152x driver (and possibly others). I'll look into
  57. * it, as I have time...
  58. *
  59. * I get mail occasionally from people who either are using or are
  60. * considering using a WD7000 with Linux. There is a variety of
  61. * nomenclature describing WD7000's. To the best of my knowledge, the
  62. * following is a brief summary (from an old WD doc - I don't work for
  63. * them or anything like that):
  64. *
  65. * WD7000-FASST2: This is a WD7000 board with the real-mode SST ROM BIOS
  66. * installed. Last I heard, the BIOS was actually done by Columbia
  67. * Data Products. The BIOS is only used by this driver (and thus
  68. * by Linux) to identify the board; none of it can be executed under
  69. * Linux.
  70. *
  71. * WD7000-ASC: This is the original adapter board, with or without BIOS.
  72. * The board uses a WD33C93 or WD33C93A SBIC, which in turn is
  73. * controlled by an onboard Z80 processor. The board interface
  74. * visible to the host CPU is defined effectively by the Z80's
  75. * firmware, and it is this firmware's revision level that is
  76. * determined and reported by this driver. (The version of the
  77. * on-board BIOS is of no interest whatsoever.) The host CPU has
  78. * no access to the SBIC; hence the fact that it is a WD33C93 is
  79. * also of no interest to this driver.
  80. *
  81. * WD7000-AX:
  82. * WD7000-MX:
  83. * WD7000-EX: These are newer versions of the WD7000-ASC. The -ASC is
  84. * largely built from discrete components; these boards use more
  85. * integration. The -AX is an ISA bus board (like the -ASC),
  86. * the -MX is an MCA (i.e., PS/2) bus board), and the -EX is an
  87. * EISA bus board.
  88. *
  89. * At the time of my documentation, the -?X boards were "future" products,
  90. * and were not yet available. However, I vaguely recall that Thomas
  91. * Wuensche had an -AX, so I believe at least it is supported by this
  92. * driver. I have no personal knowledge of either -MX or -EX boards.
  93. *
  94. * P.S. Just recently, I've discovered (directly from WD and Future
  95. * Domain) that all but the WD7000-EX have been out of production for
  96. * two years now. FD has production rights to the 7000-EX, and are
  97. * producing it under a new name, and with a new BIOS. If anyone has
  98. * one of the FD boards, it would be nice to come up with a signature
  99. * for it.
  100. * J.B. Jan 1994.
  101. *
  102. *
  103. * Revisions by Miroslav Zagorac <zaga@fly.cc.fer.hr>
  104. *
  105. * 08/24/1996.
  106. *
  107. * Enhancement for wd7000_detect function has been made, so you don't have
  108. * to enter BIOS ROM address in initialisation data (see struct Config).
  109. * We cannot detect IRQ, DMA and I/O base address for now, so we have to
  110. * enter them as arguments while wd_7000 is detected. If someone has IRQ,
  111. * DMA or I/O base address set to some other value, he can enter them in
  112. * configuration without any problem. Also I wrote a function wd7000_setup,
  113. * so now you can enter WD-7000 definition as kernel arguments,
  114. * as in lilo.conf:
  115. *
  116. * append="wd7000=IRQ,DMA,IO"
  117. *
  118. * PS: If card BIOS ROM is disabled, function wd7000_detect now will recognize
  119. * adapter, unlike the old one. Anyway, BIOS ROM from WD7000 adapter is
  120. * useless for Linux. B^)
  121. *
  122. *
  123. * 09/06/1996.
  124. *
  125. * Autodetecting of I/O base address from wd7000_detect function is removed,
  126. * some little bugs removed, etc...
  127. *
  128. * Thanks to Roger Scott for driver debugging.
  129. *
  130. * 06/07/1997
  131. *
  132. * Added support for /proc file system (/proc/scsi/wd7000/[0...] files).
  133. * Now, driver can handle hard disks with capacity >1GB.
  134. *
  135. * 01/15/1998
  136. *
  137. * Added support for BUS_ON and BUS_OFF parameters in config line.
  138. * Miscellaneous cleanup.
  139. *
  140. * 03/01/1998
  141. *
  142. * WD7000 driver now work on kernels >= 2.1.x
  143. *
  144. *
  145. * 12/31/2001 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  146. *
  147. * use host->host_lock, not io_request_lock, cleanups
  148. *
  149. * 2002/10/04 - Alan Cox <alan@lxorguk.ukuu.org.uk>
  150. *
  151. * Use dev_id for interrupts, kill __func__ pasting
  152. * Add a lock for the scb pool, clean up all other cli/sti usage stuff
  153. * Use the adapter lock for the other places we had the cli's
  154. *
  155. * 2002/10/06 - Alan Cox <alan@lxorguk.ukuu.org.uk>
  156. *
  157. * Switch to new style error handling
  158. * Clean up delay to udelay, and yielding sleeps
  159. * Make host reset actually reset the card
  160. * Make everything static
  161. *
  162. * 2003/02/12 - Christoph Hellwig <hch@infradead.org>
  163. *
  164. * Cleaned up host template definition
  165. * Removed now obsolete wd7000.h
  166. */
  167. #include <linux/delay.h>
  168. #include <linux/module.h>
  169. #include <linux/interrupt.h>
  170. #include <linux/kernel.h>
  171. #include <linux/types.h>
  172. #include <linux/string.h>
  173. #include <linux/spinlock.h>
  174. #include <linux/ioport.h>
  175. #include <linux/proc_fs.h>
  176. #include <linux/blkdev.h>
  177. #include <linux/init.h>
  178. #include <linux/stat.h>
  179. #include <linux/io.h>
  180. #include <asm/dma.h>
  181. #include <scsi/scsi.h>
  182. #include <scsi/scsi_cmnd.h>
  183. #include <scsi/scsi_device.h>
  184. #include <scsi/scsi_host.h>
  185. #include <scsi/scsicam.h>
  186. #undef WD7000_DEBUG /* general debug */
  187. #ifdef WD7000_DEBUG
  188. #define dprintk printk
  189. #else
  190. #define dprintk no_printk
  191. #endif
  192. /*
  193. * Mailbox structure sizes.
  194. * I prefer to keep the number of ICMBs much larger than the number of
  195. * OGMBs. OGMBs are used very quickly by the driver to start one or
  196. * more commands, while ICMBs are used by the host adapter per command.
  197. */
  198. #define OGMB_CNT 16
  199. #define ICMB_CNT 32
  200. /*
  201. * Scb's are shared by all active adapters. So, if they all become busy,
  202. * callers may be made to wait in alloc_scbs for them to free. That can
  203. * be avoided by setting MAX_SCBS to NUM_CONFIG * WD7000_Q. If you'd
  204. * rather conserve memory, use a smaller number (> 0, of course) - things
  205. * will should still work OK.
  206. */
  207. #define MAX_SCBS 32
  208. /*
  209. * In this version, sg_tablesize now defaults to WD7000_SG, and will
  210. * be set to SG_NONE for older boards. This is the reverse of the
  211. * previous default, and was changed so that the driver-level
  212. * scsi_host_template would reflect the driver's support for scatter/
  213. * gather.
  214. *
  215. * Also, it has been reported that boards at Revision 6 support scatter/
  216. * gather, so the new definition of an "older" board has been changed
  217. * accordingly.
  218. */
  219. #define WD7000_Q 16
  220. #define WD7000_SG 16
  221. /*
  222. * WD7000-specific mailbox structure
  223. *
  224. */
  225. typedef volatile struct mailbox {
  226. unchar status;
  227. unchar scbptr[3]; /* SCSI-style - MSB first (big endian) */
  228. } Mailbox;
  229. /*
  230. * This structure should contain all per-adapter global data. I.e., any
  231. * new global per-adapter data should put in here.
  232. */
  233. typedef struct adapter {
  234. struct Scsi_Host *sh; /* Pointer to Scsi_Host structure */
  235. int iobase; /* This adapter's I/O base address */
  236. int irq; /* This adapter's IRQ level */
  237. int dma; /* This adapter's DMA channel */
  238. int int_counter; /* This adapter's interrupt counter */
  239. int bus_on; /* This adapter's BUS_ON time */
  240. int bus_off; /* This adapter's BUS_OFF time */
  241. struct { /* This adapter's mailboxes */
  242. Mailbox ogmb[OGMB_CNT]; /* Outgoing mailboxes */
  243. Mailbox icmb[ICMB_CNT]; /* Incoming mailboxes */
  244. } mb;
  245. int next_ogmb; /* to reduce contention at mailboxes */
  246. unchar control; /* shadows CONTROL port value */
  247. unchar rev1, rev2; /* filled in by wd7000_revision */
  248. } Adapter;
  249. /*
  250. * (linear) base address for ROM BIOS
  251. */
  252. static const long wd7000_biosaddr[] = {
  253. 0xc0000, 0xc2000, 0xc4000, 0xc6000, 0xc8000, 0xca000, 0xcc000, 0xce000,
  254. 0xd0000, 0xd2000, 0xd4000, 0xd6000, 0xd8000, 0xda000, 0xdc000, 0xde000
  255. };
  256. #define NUM_ADDRS ARRAY_SIZE(wd7000_biosaddr)
  257. static const unsigned short wd7000_iobase[] = {
  258. 0x0300, 0x0308, 0x0310, 0x0318, 0x0320, 0x0328, 0x0330, 0x0338,
  259. 0x0340, 0x0348, 0x0350, 0x0358, 0x0360, 0x0368, 0x0370, 0x0378,
  260. 0x0380, 0x0388, 0x0390, 0x0398, 0x03a0, 0x03a8, 0x03b0, 0x03b8,
  261. 0x03c0, 0x03c8, 0x03d0, 0x03d8, 0x03e0, 0x03e8, 0x03f0, 0x03f8
  262. };
  263. #define NUM_IOPORTS ARRAY_SIZE(wd7000_iobase)
  264. static const short wd7000_irq[] = { 3, 4, 5, 7, 9, 10, 11, 12, 14, 15 };
  265. #define NUM_IRQS ARRAY_SIZE(wd7000_irq)
  266. static const short wd7000_dma[] = { 5, 6, 7 };
  267. #define NUM_DMAS ARRAY_SIZE(wd7000_dma)
  268. /*
  269. * The following is set up by wd7000_detect, and used thereafter for
  270. * proc and other global ookups
  271. */
  272. #define UNITS 8
  273. static struct Scsi_Host *wd7000_host[UNITS];
  274. #define BUS_ON 64 /* x 125ns = 8000ns (BIOS default) */
  275. #define BUS_OFF 15 /* x 125ns = 1875ns (BIOS default) */
  276. /*
  277. * Standard Adapter Configurations - used by wd7000_detect
  278. */
  279. typedef struct {
  280. short irq; /* IRQ level */
  281. short dma; /* DMA channel */
  282. unsigned iobase; /* I/O base address */
  283. short bus_on; /* Time that WD7000 spends on the AT-bus when */
  284. /* transferring data. BIOS default is 8000ns. */
  285. short bus_off; /* Time that WD7000 spends OFF THE BUS after */
  286. /* while it is transferring data. */
  287. /* BIOS default is 1875ns */
  288. } Config;
  289. /*
  290. * Add here your configuration...
  291. */
  292. static Config configs[] = {
  293. {15, 6, 0x350, BUS_ON, BUS_OFF}, /* defaults for single adapter */
  294. {11, 5, 0x320, BUS_ON, BUS_OFF}, /* defaults for second adapter */
  295. {7, 6, 0x350, BUS_ON, BUS_OFF}, /* My configuration (Zaga) */
  296. {-1, -1, 0x0, BUS_ON, BUS_OFF} /* Empty slot */
  297. };
  298. #define NUM_CONFIGS ARRAY_SIZE(configs)
  299. /*
  300. * The following list defines strings to look for in the BIOS that identify
  301. * it as the WD7000-FASST2 SST BIOS. I suspect that something should be
  302. * added for the Future Domain version.
  303. */
  304. typedef struct signature {
  305. const char *sig; /* String to look for */
  306. unsigned long ofs; /* offset from BIOS base address */
  307. unsigned len; /* length of string */
  308. } Signature;
  309. static const Signature signatures[] = {
  310. {"SSTBIOS", 0x0000d, 7} /* "SSTBIOS" @ offset 0x0000d */
  311. };
  312. #define NUM_SIGNATURES ARRAY_SIZE(signatures)
  313. /*
  314. * I/O Port Offsets and Bit Definitions
  315. * 4 addresses are used. Those not defined here are reserved.
  316. */
  317. #define ASC_STAT 0 /* Status, Read */
  318. #define ASC_COMMAND 0 /* Command, Write */
  319. #define ASC_INTR_STAT 1 /* Interrupt Status, Read */
  320. #define ASC_INTR_ACK 1 /* Acknowledge, Write */
  321. #define ASC_CONTROL 2 /* Control, Write */
  322. /*
  323. * ASC Status Port
  324. */
  325. #define INT_IM 0x80 /* Interrupt Image Flag */
  326. #define CMD_RDY 0x40 /* Command Port Ready */
  327. #define CMD_REJ 0x20 /* Command Port Byte Rejected */
  328. #define ASC_INIT 0x10 /* ASC Initialized Flag */
  329. #define ASC_STATMASK 0xf0 /* The lower 4 Bytes are reserved */
  330. /*
  331. * COMMAND opcodes
  332. *
  333. * Unfortunately, I have no idea how to properly use some of these commands,
  334. * as the OEM manual does not make it clear. I have not been able to use
  335. * enable/disable unsolicited interrupts or the reset commands with any
  336. * discernible effect whatsoever. I think they may be related to certain
  337. * ICB commands, but again, the OEM manual doesn't make that clear.
  338. */
  339. #define NO_OP 0 /* NO-OP toggles CMD_RDY bit in ASC_STAT */
  340. #define INITIALIZATION 1 /* initialization (10 bytes) */
  341. #define DISABLE_UNS_INTR 2 /* disable unsolicited interrupts */
  342. #define ENABLE_UNS_INTR 3 /* enable unsolicited interrupts */
  343. #define INTR_ON_FREE_OGMB 4 /* interrupt on free OGMB */
  344. #define SOFT_RESET 5 /* SCSI bus soft reset */
  345. #define HARD_RESET_ACK 6 /* SCSI bus hard reset acknowledge */
  346. #define START_OGMB 0x80 /* start command in OGMB (n) */
  347. #define SCAN_OGMBS 0xc0 /* start multiple commands, signature (n) */
  348. /* where (n) = lower 6 bits */
  349. /*
  350. * For INITIALIZATION:
  351. */
  352. typedef struct initCmd {
  353. unchar op; /* command opcode (= 1) */
  354. unchar ID; /* Adapter's SCSI ID */
  355. unchar bus_on; /* Bus on time, x 125ns (see below) */
  356. unchar bus_off; /* Bus off time, "" "" */
  357. unchar rsvd; /* Reserved */
  358. unchar mailboxes[3]; /* Address of Mailboxes, MSB first */
  359. unchar ogmbs; /* Number of outgoing MBs, max 64, 0,1 = 1 */
  360. unchar icmbs; /* Number of incoming MBs, "" "" */
  361. } InitCmd;
  362. /*
  363. * Interrupt Status Port - also returns diagnostic codes at ASC reset
  364. *
  365. * if msb is zero, the lower bits are diagnostic status
  366. * Diagnostics:
  367. * 01 No diagnostic error occurred
  368. * 02 RAM failure
  369. * 03 FIFO R/W failed
  370. * 04 SBIC register read/write failed
  371. * 05 Initialization D-FF failed
  372. * 06 Host IRQ D-FF failed
  373. * 07 ROM checksum error
  374. * Interrupt status (bitwise):
  375. * 10NNNNNN outgoing mailbox NNNNNN is free
  376. * 11NNNNNN incoming mailbox NNNNNN needs service
  377. */
  378. #define MB_INTR 0xC0 /* Mailbox Service possible/required */
  379. #define IMB_INTR 0x40 /* 1 Incoming / 0 Outgoing */
  380. #define MB_MASK 0x3f /* mask for mailbox number */
  381. /*
  382. * CONTROL port bits
  383. */
  384. #define INT_EN 0x08 /* Interrupt Enable */
  385. #define DMA_EN 0x04 /* DMA Enable */
  386. #define SCSI_RES 0x02 /* SCSI Reset */
  387. #define ASC_RES 0x01 /* ASC Reset */
  388. /*
  389. * Driver data structures:
  390. * - mb and scbs are required for interfacing with the host adapter.
  391. * An SCB has extra fields not visible to the adapter; mb's
  392. * _cannot_ do this, since the adapter assumes they are contiguous in
  393. * memory, 4 bytes each, with ICMBs following OGMBs, and uses this fact
  394. * to access them.
  395. * - An icb is for host-only (non-SCSI) commands. ICBs are 16 bytes each;
  396. * the additional bytes are used only by the driver.
  397. * - For now, a pool of SCBs are kept in global storage by this driver,
  398. * and are allocated and freed as needed.
  399. *
  400. * The 7000-FASST2 marks OGMBs empty as soon as it has _started_ a command,
  401. * not when it has finished. Since the SCB must be around for completion,
  402. * problems arise when SCBs correspond to OGMBs, which may be reallocated
  403. * earlier (or delayed unnecessarily until a command completes).
  404. * Mailboxes are used as transient data structures, simply for
  405. * carrying SCB addresses to/from the 7000-FASST2.
  406. *
  407. * Note also since SCBs are not "permanently" associated with mailboxes,
  408. * there is no need to keep a global list of scsi_cmnd pointers indexed
  409. * by OGMB. Again, SCBs reference their scsi_cmnds directly, so mailbox
  410. * indices need not be involved.
  411. */
  412. /*
  413. * WD7000-specific scatter/gather element structure
  414. */
  415. typedef struct sgb {
  416. unchar len[3];
  417. unchar ptr[3]; /* Also SCSI-style - MSB first */
  418. } Sgb;
  419. typedef struct scb { /* Command Control Block 5.4.1 */
  420. unchar op; /* Command Control Block Operation Code */
  421. unchar idlun; /* op=0,2:Target Id, op=1:Initiator Id */
  422. /* Outbound data transfer, length is checked */
  423. /* Inbound data transfer, length is checked */
  424. /* Logical Unit Number */
  425. unchar cdb[12]; /* SCSI Command Block */
  426. volatile unchar status; /* SCSI Return Status */
  427. volatile unchar vue; /* Vendor Unique Error Code */
  428. unchar maxlen[3]; /* Maximum Data Transfer Length */
  429. unchar dataptr[3]; /* SCSI Data Block Pointer */
  430. unchar linkptr[3]; /* Next Command Link Pointer */
  431. unchar direc; /* Transfer Direction */
  432. unchar reserved2[6]; /* SCSI Command Descriptor Block */
  433. /* end of hardware SCB */
  434. struct scsi_cmnd *SCpnt;/* scsi_cmnd using this SCB */
  435. Sgb sgb[WD7000_SG]; /* Scatter/gather list for this SCB */
  436. Adapter *host; /* host adapter */
  437. struct scb *next; /* for lists of scbs */
  438. } Scb;
  439. /*
  440. * This driver is written to allow host-only commands to be executed.
  441. * These use a 16-byte block called an ICB. The format is extended by the
  442. * driver to 18 bytes, to support the status returned in the ICMB and
  443. * an execution phase code.
  444. *
  445. * There are other formats besides these; these are the ones I've tried
  446. * to use. Formats for some of the defined ICB opcodes are not defined
  447. * (notably, get/set unsolicited interrupt status) in my copy of the OEM
  448. * manual, and others are ambiguous/hard to follow.
  449. */
  450. #define ICB_OP_MASK 0x80 /* distinguishes scbs from icbs */
  451. #define ICB_OP_OPEN_RBUF 0x80 /* open receive buffer */
  452. #define ICB_OP_RECV_CMD 0x81 /* receive command from initiator */
  453. #define ICB_OP_RECV_DATA 0x82 /* receive data from initiator */
  454. #define ICB_OP_RECV_SDATA 0x83 /* receive data with status from init. */
  455. #define ICB_OP_SEND_DATA 0x84 /* send data with status to initiator */
  456. #define ICB_OP_SEND_STAT 0x86 /* send command status to initiator */
  457. /* 0x87 is reserved */
  458. #define ICB_OP_READ_INIT 0x88 /* read initialization bytes */
  459. #define ICB_OP_READ_ID 0x89 /* read adapter's SCSI ID */
  460. #define ICB_OP_SET_UMASK 0x8A /* set unsolicited interrupt mask */
  461. #define ICB_OP_GET_UMASK 0x8B /* read unsolicited interrupt mask */
  462. #define ICB_OP_GET_REVISION 0x8C /* read firmware revision level */
  463. #define ICB_OP_DIAGNOSTICS 0x8D /* execute diagnostics */
  464. #define ICB_OP_SET_EPARMS 0x8E /* set execution parameters */
  465. #define ICB_OP_GET_EPARMS 0x8F /* read execution parameters */
  466. typedef struct icbRecvCmd {
  467. unchar op;
  468. unchar IDlun; /* Initiator SCSI ID/lun */
  469. unchar len[3]; /* command buffer length */
  470. unchar ptr[3]; /* command buffer address */
  471. unchar rsvd[7]; /* reserved */
  472. volatile unchar vue; /* vendor-unique error code */
  473. volatile unchar status; /* returned (icmb) status */
  474. volatile unchar phase; /* used by interrupt handler */
  475. } IcbRecvCmd;
  476. typedef struct icbSendStat {
  477. unchar op;
  478. unchar IDlun; /* Target SCSI ID/lun */
  479. unchar stat; /* (outgoing) completion status byte 1 */
  480. unchar rsvd[12]; /* reserved */
  481. volatile unchar vue; /* vendor-unique error code */
  482. volatile unchar status; /* returned (icmb) status */
  483. volatile unchar phase; /* used by interrupt handler */
  484. } IcbSendStat;
  485. typedef struct icbRevLvl {
  486. unchar op;
  487. volatile unchar primary; /* primary revision level (returned) */
  488. volatile unchar secondary; /* secondary revision level (returned) */
  489. unchar rsvd[12]; /* reserved */
  490. volatile unchar vue; /* vendor-unique error code */
  491. volatile unchar status; /* returned (icmb) status */
  492. volatile unchar phase; /* used by interrupt handler */
  493. } IcbRevLvl;
  494. typedef struct icbUnsMask { /* I'm totally guessing here */
  495. unchar op;
  496. volatile unchar mask[14]; /* mask bits */
  497. #if 0
  498. unchar rsvd[12]; /* reserved */
  499. #endif
  500. volatile unchar vue; /* vendor-unique error code */
  501. volatile unchar status; /* returned (icmb) status */
  502. volatile unchar phase; /* used by interrupt handler */
  503. } IcbUnsMask;
  504. typedef struct icbDiag {
  505. unchar op;
  506. unchar type; /* diagnostics type code (0-3) */
  507. unchar len[3]; /* buffer length */
  508. unchar ptr[3]; /* buffer address */
  509. unchar rsvd[7]; /* reserved */
  510. volatile unchar vue; /* vendor-unique error code */
  511. volatile unchar status; /* returned (icmb) status */
  512. volatile unchar phase; /* used by interrupt handler */
  513. } IcbDiag;
  514. #define ICB_DIAG_POWERUP 0 /* Power-up diags only */
  515. #define ICB_DIAG_WALKING 1 /* walking 1's pattern */
  516. #define ICB_DIAG_DMA 2 /* DMA - system memory diags */
  517. #define ICB_DIAG_FULL 3 /* do both 1 & 2 */
  518. typedef struct icbParms {
  519. unchar op;
  520. unchar rsvd1; /* reserved */
  521. unchar len[3]; /* parms buffer length */
  522. unchar ptr[3]; /* parms buffer address */
  523. unchar idx[2]; /* index (MSB-LSB) */
  524. unchar rsvd2[5]; /* reserved */
  525. volatile unchar vue; /* vendor-unique error code */
  526. volatile unchar status; /* returned (icmb) status */
  527. volatile unchar phase; /* used by interrupt handler */
  528. } IcbParms;
  529. typedef struct icbAny {
  530. unchar op;
  531. unchar data[14]; /* format-specific data */
  532. volatile unchar vue; /* vendor-unique error code */
  533. volatile unchar status; /* returned (icmb) status */
  534. volatile unchar phase; /* used by interrupt handler */
  535. } IcbAny;
  536. typedef union icb {
  537. unchar op; /* ICB opcode */
  538. IcbRecvCmd recv_cmd; /* format for receive command */
  539. IcbSendStat send_stat; /* format for send status */
  540. IcbRevLvl rev_lvl; /* format for get revision level */
  541. IcbDiag diag; /* format for execute diagnostics */
  542. IcbParms eparms; /* format for get/set exec parms */
  543. IcbAny icb; /* generic format */
  544. unchar data[18];
  545. } Icb;
  546. #ifdef MODULE
  547. static char *wd7000;
  548. module_param(wd7000, charp, 0);
  549. #endif
  550. /*
  551. * Driver SCB structure pool.
  552. *
  553. * The SCBs declared here are shared by all host adapters; hence, this
  554. * structure is not part of the Adapter structure.
  555. */
  556. static Scb scbs[MAX_SCBS];
  557. static Scb *scbfree; /* free list */
  558. static int freescbs = MAX_SCBS; /* free list counter */
  559. static spinlock_t scbpool_lock; /* guards the scb free list and count */
  560. /*
  561. * END of data/declarations - code follows.
  562. */
  563. static void __init setup_error(char *mesg, int *ints)
  564. {
  565. if (ints[0] == 3)
  566. printk(KERN_ERR "wd7000_setup: \"wd7000=%d,%d,0x%x\" -> %s\n", ints[1], ints[2], ints[3], mesg);
  567. else if (ints[0] == 4)
  568. printk(KERN_ERR "wd7000_setup: \"wd7000=%d,%d,0x%x,%d\" -> %s\n", ints[1], ints[2], ints[3], ints[4], mesg);
  569. else
  570. printk(KERN_ERR "wd7000_setup: \"wd7000=%d,%d,0x%x,%d,%d\" -> %s\n", ints[1], ints[2], ints[3], ints[4], ints[5], mesg);
  571. }
  572. /*
  573. * Note: You can now set these options from the kernel's "command line".
  574. * The syntax is:
  575. *
  576. * wd7000=<IRQ>,<DMA>,<IO>[,<BUS_ON>[,<BUS_OFF>]]
  577. *
  578. * , where BUS_ON and BUS_OFF are in nanoseconds. BIOS default values
  579. * are 8000ns for BUS_ON and 1875ns for BUS_OFF.
  580. * eg:
  581. * wd7000=7,6,0x350
  582. *
  583. * will configure the driver for a WD-7000 controller
  584. * using IRQ 15 with a DMA channel 6, at IO base address 0x350.
  585. */
  586. static int __init wd7000_setup(char *str)
  587. {
  588. static short wd7000_card_num; /* .bss will zero this */
  589. short i;
  590. int ints[6];
  591. (void) get_options(str, ARRAY_SIZE(ints), ints);
  592. if (wd7000_card_num >= NUM_CONFIGS) {
  593. printk(KERN_ERR "%s: Too many \"wd7000=\" configurations in " "command line!\n", __func__);
  594. return 0;
  595. }
  596. if ((ints[0] < 3) || (ints[0] > 5)) {
  597. printk(KERN_ERR "%s: Error in command line! " "Usage: wd7000=<IRQ>,<DMA>,IO>[,<BUS_ON>" "[,<BUS_OFF>]]\n", __func__);
  598. } else {
  599. for (i = 0; i < NUM_IRQS; i++)
  600. if (ints[1] == wd7000_irq[i])
  601. break;
  602. if (i == NUM_IRQS) {
  603. setup_error("invalid IRQ.", ints);
  604. return 0;
  605. } else
  606. configs[wd7000_card_num].irq = ints[1];
  607. for (i = 0; i < NUM_DMAS; i++)
  608. if (ints[2] == wd7000_dma[i])
  609. break;
  610. if (i == NUM_DMAS) {
  611. setup_error("invalid DMA channel.", ints);
  612. return 0;
  613. } else
  614. configs[wd7000_card_num].dma = ints[2];
  615. for (i = 0; i < NUM_IOPORTS; i++)
  616. if (ints[3] == wd7000_iobase[i])
  617. break;
  618. if (i == NUM_IOPORTS) {
  619. setup_error("invalid I/O base address.", ints);
  620. return 0;
  621. } else
  622. configs[wd7000_card_num].iobase = ints[3];
  623. if (ints[0] > 3) {
  624. if ((ints[4] < 500) || (ints[4] > 31875)) {
  625. setup_error("BUS_ON value is out of range (500" " to 31875 nanoseconds)!", ints);
  626. configs[wd7000_card_num].bus_on = BUS_ON;
  627. } else
  628. configs[wd7000_card_num].bus_on = ints[4] / 125;
  629. } else
  630. configs[wd7000_card_num].bus_on = BUS_ON;
  631. if (ints[0] > 4) {
  632. if ((ints[5] < 500) || (ints[5] > 31875)) {
  633. setup_error("BUS_OFF value is out of range (500" " to 31875 nanoseconds)!", ints);
  634. configs[wd7000_card_num].bus_off = BUS_OFF;
  635. } else
  636. configs[wd7000_card_num].bus_off = ints[5] / 125;
  637. } else
  638. configs[wd7000_card_num].bus_off = BUS_OFF;
  639. if (wd7000_card_num) {
  640. for (i = 0; i < (wd7000_card_num - 1); i++) {
  641. int j = i + 1;
  642. for (; j < wd7000_card_num; j++)
  643. if (configs[i].irq == configs[j].irq) {
  644. setup_error("duplicated IRQ!", ints);
  645. return 0;
  646. }
  647. if (configs[i].dma == configs[j].dma) {
  648. setup_error("duplicated DMA " "channel!", ints);
  649. return 0;
  650. }
  651. if (configs[i].iobase == configs[j].iobase) {
  652. setup_error("duplicated I/O " "base address!", ints);
  653. return 0;
  654. }
  655. }
  656. }
  657. dprintk(KERN_DEBUG "wd7000_setup: IRQ=%d, DMA=%d, I/O=0x%x, "
  658. "BUS_ON=%dns, BUS_OFF=%dns\n", configs[wd7000_card_num].irq, configs[wd7000_card_num].dma, configs[wd7000_card_num].iobase, configs[wd7000_card_num].bus_on * 125, configs[wd7000_card_num].bus_off * 125);
  659. wd7000_card_num++;
  660. }
  661. return 1;
  662. }
  663. __setup("wd7000=", wd7000_setup);
  664. static inline void any2scsi(unchar * scsi, int any)
  665. {
  666. *scsi++ = (unsigned)any >> 16;
  667. *scsi++ = (unsigned)any >> 8;
  668. *scsi++ = any;
  669. }
  670. static inline int scsi2int(unchar * scsi)
  671. {
  672. return (scsi[0] << 16) | (scsi[1] << 8) | scsi[2];
  673. }
  674. static inline void wd7000_enable_intr(Adapter * host)
  675. {
  676. host->control |= INT_EN;
  677. outb(host->control, host->iobase + ASC_CONTROL);
  678. }
  679. static inline void wd7000_enable_dma(Adapter * host)
  680. {
  681. unsigned long flags;
  682. host->control |= DMA_EN;
  683. outb(host->control, host->iobase + ASC_CONTROL);
  684. flags = claim_dma_lock();
  685. set_dma_mode(host->dma, DMA_MODE_CASCADE);
  686. enable_dma(host->dma);
  687. release_dma_lock(flags);
  688. }
  689. #define WAITnexttimeout 200 /* 2 seconds */
  690. static inline short WAIT(unsigned port, unsigned mask, unsigned allof, unsigned noneof)
  691. {
  692. unsigned WAITbits;
  693. unsigned long WAITtimeout = jiffies + WAITnexttimeout;
  694. while (time_before_eq(jiffies, WAITtimeout)) {
  695. WAITbits = inb(port) & mask;
  696. if (((WAITbits & allof) == allof) && ((WAITbits & noneof) == 0))
  697. return (0);
  698. }
  699. return (1);
  700. }
  701. static inline int command_out(Adapter * host, unchar * cmd, int len)
  702. {
  703. if (!WAIT(host->iobase + ASC_STAT, ASC_STATMASK, CMD_RDY, 0)) {
  704. while (len--) {
  705. do {
  706. outb(*cmd, host->iobase + ASC_COMMAND);
  707. WAIT(host->iobase + ASC_STAT, ASC_STATMASK, CMD_RDY, 0);
  708. } while (inb(host->iobase + ASC_STAT) & CMD_REJ);
  709. cmd++;
  710. }
  711. return (1);
  712. }
  713. printk(KERN_WARNING "wd7000 command_out: WAIT failed(%d)\n", len + 1);
  714. return (0);
  715. }
  716. /*
  717. * This version of alloc_scbs is in preparation for supporting multiple
  718. * commands per lun and command chaining, by queueing pending commands.
  719. * We will need to allocate Scbs in blocks since they will wait to be
  720. * executed so there is the possibility of deadlock otherwise.
  721. * Also, to keep larger requests from being starved by smaller requests,
  722. * we limit access to this routine with an internal busy flag, so that
  723. * the satisfiability of a request is not dependent on the size of the
  724. * request.
  725. */
  726. static inline Scb *alloc_scbs(struct Scsi_Host *host, int needed)
  727. {
  728. Scb *scb, *p = NULL;
  729. unsigned long flags;
  730. unsigned long timeout = jiffies + WAITnexttimeout;
  731. unsigned long now;
  732. int i;
  733. if (needed <= 0)
  734. return (NULL); /* sanity check */
  735. spin_unlock_irq(host->host_lock);
  736. retry:
  737. while (freescbs < needed) {
  738. timeout = jiffies + WAITnexttimeout;
  739. do {
  740. /* FIXME: can we actually just yield here ?? */
  741. for (now = jiffies; now == jiffies;)
  742. cpu_relax(); /* wait a jiffy */
  743. } while (freescbs < needed && time_before_eq(jiffies, timeout));
  744. /*
  745. * If we get here with enough free Scbs, we can take them.
  746. * Otherwise, we timed out and didn't get enough.
  747. */
  748. if (freescbs < needed) {
  749. printk(KERN_ERR "wd7000: can't get enough free SCBs.\n");
  750. return (NULL);
  751. }
  752. }
  753. /* Take the lock, then check we didn't get beaten, if so try again */
  754. spin_lock_irqsave(&scbpool_lock, flags);
  755. if (freescbs < needed) {
  756. spin_unlock_irqrestore(&scbpool_lock, flags);
  757. goto retry;
  758. }
  759. scb = scbfree;
  760. freescbs -= needed;
  761. for (i = 0; i < needed; i++) {
  762. p = scbfree;
  763. scbfree = p->next;
  764. }
  765. p->next = NULL;
  766. spin_unlock_irqrestore(&scbpool_lock, flags);
  767. spin_lock_irq(host->host_lock);
  768. return (scb);
  769. }
  770. static inline void free_scb(Scb * scb)
  771. {
  772. unsigned long flags;
  773. spin_lock_irqsave(&scbpool_lock, flags);
  774. memset(scb, 0, sizeof(Scb));
  775. scb->next = scbfree;
  776. scbfree = scb;
  777. freescbs++;
  778. spin_unlock_irqrestore(&scbpool_lock, flags);
  779. }
  780. static inline void init_scbs(void)
  781. {
  782. int i;
  783. spin_lock_init(&scbpool_lock);
  784. /* This is only ever called before the SCB pool is active */
  785. scbfree = &(scbs[0]);
  786. memset(scbs, 0, sizeof(scbs));
  787. for (i = 0; i < MAX_SCBS - 1; i++) {
  788. scbs[i].next = &(scbs[i + 1]);
  789. scbs[i].SCpnt = NULL;
  790. }
  791. scbs[MAX_SCBS - 1].next = NULL;
  792. scbs[MAX_SCBS - 1].SCpnt = NULL;
  793. }
  794. static int mail_out(Adapter * host, Scb * scbptr)
  795. /*
  796. * Note: this can also be used for ICBs; just cast to the parm type.
  797. */
  798. {
  799. int i, ogmb;
  800. unsigned long flags;
  801. unchar start_ogmb;
  802. Mailbox *ogmbs = host->mb.ogmb;
  803. int *next_ogmb = &(host->next_ogmb);
  804. dprintk("wd7000_mail_out: 0x%06lx", (long) scbptr);
  805. /* We first look for a free outgoing mailbox */
  806. spin_lock_irqsave(host->sh->host_lock, flags);
  807. ogmb = *next_ogmb;
  808. for (i = 0; i < OGMB_CNT; i++) {
  809. if (ogmbs[ogmb].status == 0) {
  810. dprintk(" using OGMB 0x%x", ogmb);
  811. ogmbs[ogmb].status = 1;
  812. any2scsi((unchar *) ogmbs[ogmb].scbptr, (int) scbptr);
  813. *next_ogmb = (ogmb + 1) % OGMB_CNT;
  814. break;
  815. } else
  816. ogmb = (ogmb + 1) % OGMB_CNT;
  817. }
  818. spin_unlock_irqrestore(host->sh->host_lock, flags);
  819. dprintk(", scb is 0x%06lx", (long) scbptr);
  820. if (i >= OGMB_CNT) {
  821. /*
  822. * Alternatively, we might issue the "interrupt on free OGMB",
  823. * and sleep, but it must be ensured that it isn't the init
  824. * task running. Instead, this version assumes that the caller
  825. * will be persistent, and try again. Since it's the adapter
  826. * that marks OGMB's free, waiting even with interrupts off
  827. * should work, since they are freed very quickly in most cases.
  828. */
  829. dprintk(", no free OGMBs.\n");
  830. return (0);
  831. }
  832. wd7000_enable_intr(host);
  833. start_ogmb = START_OGMB | ogmb;
  834. command_out(host, &start_ogmb, 1);
  835. dprintk(", awaiting interrupt.\n");
  836. return (1);
  837. }
  838. static int make_code(unsigned hosterr, unsigned scsierr)
  839. {
  840. #ifdef WD7000_DEBUG
  841. int in_error = hosterr;
  842. #endif
  843. switch ((hosterr >> 8) & 0xff) {
  844. case 0: /* Reserved */
  845. hosterr = DID_ERROR;
  846. break;
  847. case 1: /* Command Complete, no errors */
  848. hosterr = DID_OK;
  849. break;
  850. case 2: /* Command complete, error logged in scb status (scsierr) */
  851. hosterr = DID_OK;
  852. break;
  853. case 4: /* Command failed to complete - timeout */
  854. hosterr = DID_TIME_OUT;
  855. break;
  856. case 5: /* Command terminated; Bus reset by external device */
  857. hosterr = DID_RESET;
  858. break;
  859. case 6: /* Unexpected Command Received w/ host as target */
  860. hosterr = DID_BAD_TARGET;
  861. break;
  862. case 80: /* Unexpected Reselection */
  863. case 81: /* Unexpected Selection */
  864. hosterr = DID_BAD_INTR;
  865. break;
  866. case 82: /* Abort Command Message */
  867. hosterr = DID_ABORT;
  868. break;
  869. case 83: /* SCSI Bus Software Reset */
  870. case 84: /* SCSI Bus Hardware Reset */
  871. hosterr = DID_RESET;
  872. break;
  873. default: /* Reserved */
  874. hosterr = DID_ERROR;
  875. }
  876. #ifdef WD7000_DEBUG
  877. if (scsierr || hosterr)
  878. dprintk("\nSCSI command error: SCSI 0x%02x host 0x%04x return %d\n", scsierr, in_error, hosterr);
  879. #endif
  880. return (scsierr | (hosterr << 16));
  881. }
  882. #define wd7000_intr_ack(host) outb (0, host->iobase + ASC_INTR_ACK)
  883. static irqreturn_t wd7000_intr(int irq, void *dev_id)
  884. {
  885. Adapter *host = (Adapter *) dev_id;
  886. int flag, icmb, errstatus, icmb_status;
  887. int host_error, scsi_error;
  888. Scb *scb; /* for SCSI commands */
  889. IcbAny *icb; /* for host commands */
  890. struct scsi_cmnd *SCpnt;
  891. Mailbox *icmbs = host->mb.icmb;
  892. unsigned long flags;
  893. spin_lock_irqsave(host->sh->host_lock, flags);
  894. host->int_counter++;
  895. dprintk("wd7000_intr: irq = %d, host = 0x%06lx\n", irq, (long) host);
  896. flag = inb(host->iobase + ASC_INTR_STAT);
  897. dprintk("wd7000_intr: intr stat = 0x%02x\n", flag);
  898. if (!(inb(host->iobase + ASC_STAT) & INT_IM)) {
  899. /* NB: these are _very_ possible if IRQ 15 is being used, since
  900. * it's the "garbage collector" on the 2nd 8259 PIC. Specifically,
  901. * any interrupt signal into the 8259 which can't be identified
  902. * comes out as 7 from the 8259, which is 15 to the host. Thus, it
  903. * is a good thing the WD7000 has an interrupt status port, so we
  904. * can sort these out. Otherwise, electrical noise and other such
  905. * problems would be indistinguishable from valid interrupts...
  906. */
  907. dprintk("wd7000_intr: phantom interrupt...\n");
  908. goto ack;
  909. }
  910. if (!(flag & MB_INTR))
  911. goto ack;
  912. /* The interrupt is for a mailbox */
  913. if (!(flag & IMB_INTR)) {
  914. dprintk("wd7000_intr: free outgoing mailbox\n");
  915. /*
  916. * If sleep_on() and the "interrupt on free OGMB" command are
  917. * used in mail_out(), wake_up() should correspondingly be called
  918. * here. For now, we don't need to do anything special.
  919. */
  920. goto ack;
  921. }
  922. /* The interrupt is for an incoming mailbox */
  923. icmb = flag & MB_MASK;
  924. icmb_status = icmbs[icmb].status;
  925. if (icmb_status & 0x80) { /* unsolicited - result in ICMB */
  926. dprintk("wd7000_intr: unsolicited interrupt 0x%02x\n", icmb_status);
  927. goto ack;
  928. }
  929. /* Aaaargh! (Zaga) */
  930. scb = isa_bus_to_virt(scsi2int((unchar *) icmbs[icmb].scbptr));
  931. icmbs[icmb].status = 0;
  932. if (scb->op & ICB_OP_MASK) { /* an SCB is done */
  933. icb = (IcbAny *) scb;
  934. icb->status = icmb_status;
  935. icb->phase = 0;
  936. goto ack;
  937. }
  938. SCpnt = scb->SCpnt;
  939. if (--(SCpnt->SCp.phase) <= 0) { /* all scbs are done */
  940. host_error = scb->vue | (icmb_status << 8);
  941. scsi_error = scb->status;
  942. errstatus = make_code(host_error, scsi_error);
  943. SCpnt->result = errstatus;
  944. free_scb(scb);
  945. SCpnt->scsi_done(SCpnt);
  946. }
  947. ack:
  948. dprintk("wd7000_intr: return from interrupt handler\n");
  949. wd7000_intr_ack(host);
  950. spin_unlock_irqrestore(host->sh->host_lock, flags);
  951. return IRQ_HANDLED;
  952. }
  953. static int wd7000_queuecommand_lck(struct scsi_cmnd *SCpnt,
  954. void (*done)(struct scsi_cmnd *))
  955. {
  956. Scb *scb;
  957. Sgb *sgb;
  958. unchar *cdb = (unchar *) SCpnt->cmnd;
  959. unchar idlun;
  960. short cdblen;
  961. int nseg;
  962. Adapter *host = (Adapter *) SCpnt->device->host->hostdata;
  963. cdblen = SCpnt->cmd_len;
  964. idlun = ((SCpnt->device->id << 5) & 0xe0) | (SCpnt->device->lun & 7);
  965. SCpnt->scsi_done = done;
  966. SCpnt->SCp.phase = 1;
  967. scb = alloc_scbs(SCpnt->device->host, 1);
  968. scb->idlun = idlun;
  969. memcpy(scb->cdb, cdb, cdblen);
  970. scb->direc = 0x40; /* Disable direction check */
  971. scb->SCpnt = SCpnt; /* so we can find stuff later */
  972. SCpnt->host_scribble = (unchar *) scb;
  973. scb->host = host;
  974. nseg = scsi_sg_count(SCpnt);
  975. if (nseg > 1) {
  976. struct scatterlist *sg;
  977. unsigned i;
  978. dprintk("Using scatter/gather with %d elements.\n", nseg);
  979. sgb = scb->sgb;
  980. scb->op = 1;
  981. any2scsi(scb->dataptr, (int) sgb);
  982. any2scsi(scb->maxlen, nseg * sizeof(Sgb));
  983. scsi_for_each_sg(SCpnt, sg, nseg, i) {
  984. any2scsi(sgb[i].ptr, isa_page_to_bus(sg_page(sg)) + sg->offset);
  985. any2scsi(sgb[i].len, sg->length);
  986. }
  987. } else {
  988. scb->op = 0;
  989. if (nseg) {
  990. struct scatterlist *sg = scsi_sglist(SCpnt);
  991. any2scsi(scb->dataptr, isa_page_to_bus(sg_page(sg)) + sg->offset);
  992. }
  993. any2scsi(scb->maxlen, scsi_bufflen(SCpnt));
  994. }
  995. /* FIXME: drop lock and yield here ? */
  996. while (!mail_out(host, scb))
  997. cpu_relax(); /* keep trying */
  998. return 0;
  999. }
  1000. static DEF_SCSI_QCMD(wd7000_queuecommand)
  1001. static int wd7000_diagnostics(Adapter * host, int code)
  1002. {
  1003. static IcbDiag icb = { ICB_OP_DIAGNOSTICS };
  1004. static unchar buf[256];
  1005. unsigned long timeout;
  1006. icb.type = code;
  1007. any2scsi(icb.len, sizeof(buf));
  1008. any2scsi(icb.ptr, (int) &buf);
  1009. icb.phase = 1;
  1010. /*
  1011. * This routine is only called at init, so there should be OGMBs
  1012. * available. I'm assuming so here. If this is going to
  1013. * fail, I can just let the timeout catch the failure.
  1014. */
  1015. mail_out(host, (struct scb *) &icb);
  1016. timeout = jiffies + WAITnexttimeout; /* wait up to 2 seconds */
  1017. while (icb.phase && time_before(jiffies, timeout)) {
  1018. cpu_relax(); /* wait for completion */
  1019. barrier();
  1020. }
  1021. if (icb.phase) {
  1022. printk("wd7000_diagnostics: timed out.\n");
  1023. return (0);
  1024. }
  1025. if (make_code(icb.vue | (icb.status << 8), 0)) {
  1026. printk("wd7000_diagnostics: failed (0x%02x,0x%02x)\n", icb.vue, icb.status);
  1027. return (0);
  1028. }
  1029. return (1);
  1030. }
  1031. static int wd7000_adapter_reset(Adapter * host)
  1032. {
  1033. InitCmd init_cmd = {
  1034. INITIALIZATION,
  1035. 7,
  1036. host->bus_on,
  1037. host->bus_off,
  1038. 0,
  1039. {0, 0, 0},
  1040. OGMB_CNT,
  1041. ICMB_CNT
  1042. };
  1043. int diag;
  1044. /*
  1045. * Reset the adapter - only. The SCSI bus was initialized at power-up,
  1046. * and we need to do this just so we control the mailboxes, etc.
  1047. */
  1048. outb(ASC_RES, host->iobase + ASC_CONTROL);
  1049. udelay(40); /* reset pulse: this is 40us, only need 25us */
  1050. outb(0, host->iobase + ASC_CONTROL);
  1051. host->control = 0; /* this must always shadow ASC_CONTROL */
  1052. if (WAIT(host->iobase + ASC_STAT, ASC_STATMASK, CMD_RDY, 0)) {
  1053. printk(KERN_ERR "wd7000_init: WAIT timed out.\n");
  1054. return -1; /* -1 = not ok */
  1055. }
  1056. if ((diag = inb(host->iobase + ASC_INTR_STAT)) != 1) {
  1057. printk("wd7000_init: ");
  1058. switch (diag) {
  1059. case 2:
  1060. printk(KERN_ERR "RAM failure.\n");
  1061. break;
  1062. case 3:
  1063. printk(KERN_ERR "FIFO R/W failed\n");
  1064. break;
  1065. case 4:
  1066. printk(KERN_ERR "SBIC register R/W failed\n");
  1067. break;
  1068. case 5:
  1069. printk(KERN_ERR "Initialization D-FF failed.\n");
  1070. break;
  1071. case 6:
  1072. printk(KERN_ERR "Host IRQ D-FF failed.\n");
  1073. break;
  1074. case 7:
  1075. printk(KERN_ERR "ROM checksum error.\n");
  1076. break;
  1077. default:
  1078. printk(KERN_ERR "diagnostic code 0x%02Xh received.\n", diag);
  1079. }
  1080. return -1;
  1081. }
  1082. /* Clear mailboxes */
  1083. memset(&(host->mb), 0, sizeof(host->mb));
  1084. /* Execute init command */
  1085. any2scsi((unchar *) & (init_cmd.mailboxes), (int) &(host->mb));
  1086. if (!command_out(host, (unchar *) & init_cmd, sizeof(init_cmd))) {
  1087. printk(KERN_ERR "wd7000_adapter_reset: adapter initialization failed.\n");
  1088. return -1;
  1089. }
  1090. if (WAIT(host->iobase + ASC_STAT, ASC_STATMASK, ASC_INIT, 0)) {
  1091. printk("wd7000_adapter_reset: WAIT timed out.\n");
  1092. return -1;
  1093. }
  1094. return 0;
  1095. }
  1096. static int wd7000_init(Adapter * host)
  1097. {
  1098. if (wd7000_adapter_reset(host) == -1)
  1099. return 0;
  1100. if (request_irq(host->irq, wd7000_intr, 0, "wd7000", host)) {
  1101. printk("wd7000_init: can't get IRQ %d.\n", host->irq);
  1102. return (0);
  1103. }
  1104. if (request_dma(host->dma, "wd7000")) {
  1105. printk("wd7000_init: can't get DMA channel %d.\n", host->dma);
  1106. free_irq(host->irq, host);
  1107. return (0);
  1108. }
  1109. wd7000_enable_dma(host);
  1110. wd7000_enable_intr(host);
  1111. if (!wd7000_diagnostics(host, ICB_DIAG_FULL)) {
  1112. free_dma(host->dma);
  1113. free_irq(host->irq, NULL);
  1114. return (0);
  1115. }
  1116. return (1);
  1117. }
  1118. static void wd7000_revision(Adapter * host)
  1119. {
  1120. static IcbRevLvl icb = { ICB_OP_GET_REVISION };
  1121. icb.phase = 1;
  1122. /*
  1123. * Like diagnostics, this is only done at init time, in fact, from
  1124. * wd7000_detect, so there should be OGMBs available. If it fails,
  1125. * the only damage will be that the revision will show up as 0.0,
  1126. * which in turn means that scatter/gather will be disabled.
  1127. */
  1128. mail_out(host, (struct scb *) &icb);
  1129. while (icb.phase) {
  1130. cpu_relax(); /* wait for completion */
  1131. barrier();
  1132. }
  1133. host->rev1 = icb.primary;
  1134. host->rev2 = icb.secondary;
  1135. }
  1136. static int wd7000_set_info(struct Scsi_Host *host, char *buffer, int length)
  1137. {
  1138. dprintk("Buffer = <%.*s>, length = %d\n", length, buffer, length);
  1139. /*
  1140. * Currently this is a no-op
  1141. */
  1142. dprintk("Sorry, this function is currently out of order...\n");
  1143. return (length);
  1144. }
  1145. static int wd7000_show_info(struct seq_file *m, struct Scsi_Host *host)
  1146. {
  1147. Adapter *adapter = (Adapter *)host->hostdata;
  1148. unsigned long flags;
  1149. #ifdef WD7000_DEBUG
  1150. Mailbox *ogmbs, *icmbs;
  1151. short count;
  1152. #endif
  1153. spin_lock_irqsave(host->host_lock, flags);
  1154. seq_printf(m, "Host scsi%d: Western Digital WD-7000 (rev %d.%d)\n", host->host_no, adapter->rev1, adapter->rev2);
  1155. seq_printf(m, " IO base: 0x%x\n", adapter->iobase);
  1156. seq_printf(m, " IRQ: %d\n", adapter->irq);
  1157. seq_printf(m, " DMA channel: %d\n", adapter->dma);
  1158. seq_printf(m, " Interrupts: %d\n", adapter->int_counter);
  1159. seq_printf(m, " BUS_ON time: %d nanoseconds\n", adapter->bus_on * 125);
  1160. seq_printf(m, " BUS_OFF time: %d nanoseconds\n", adapter->bus_off * 125);
  1161. #ifdef WD7000_DEBUG
  1162. ogmbs = adapter->mb.ogmb;
  1163. icmbs = adapter->mb.icmb;
  1164. seq_printf(m, "\nControl port value: 0x%x\n", adapter->control);
  1165. seq_puts(m, "Incoming mailbox:\n");
  1166. seq_printf(m, " size: %d\n", ICMB_CNT);
  1167. seq_puts(m, " queued messages: ");
  1168. for (i = count = 0; i < ICMB_CNT; i++)
  1169. if (icmbs[i].status) {
  1170. count++;
  1171. seq_printf(m, "0x%x ", i);
  1172. }
  1173. seq_puts(m, count ? "\n" : "none\n");
  1174. seq_puts(m, "Outgoing mailbox:\n");
  1175. seq_printf(m, " size: %d\n", OGMB_CNT);
  1176. seq_printf(m, " next message: 0x%x\n", adapter->next_ogmb);
  1177. seq_puts(m, " queued messages: ");
  1178. for (i = count = 0; i < OGMB_CNT; i++)
  1179. if (ogmbs[i].status) {
  1180. count++;
  1181. seq_printf(m, "0x%x ", i);
  1182. }
  1183. seq_puts(m, count ? "\n" : "none\n");
  1184. #endif
  1185. spin_unlock_irqrestore(host->host_lock, flags);
  1186. return 0;
  1187. }
  1188. /*
  1189. * Returns the number of adapters this driver is supporting.
  1190. *
  1191. * The source for hosts.c says to wait to call scsi_register until 100%
  1192. * sure about an adapter. We need to do it a little sooner here; we
  1193. * need the storage set up by scsi_register before wd7000_init, and
  1194. * changing the location of an Adapter structure is more trouble than
  1195. * calling scsi_unregister.
  1196. *
  1197. */
  1198. static __init int wd7000_detect(struct scsi_host_template *tpnt)
  1199. {
  1200. short present = 0, biosaddr_ptr, sig_ptr, i, pass;
  1201. short biosptr[NUM_CONFIGS];
  1202. unsigned iobase;
  1203. Adapter *host = NULL;
  1204. struct Scsi_Host *sh;
  1205. int unit = 0;
  1206. dprintk("wd7000_detect: started\n");
  1207. #ifdef MODULE
  1208. if (wd7000)
  1209. wd7000_setup(wd7000);
  1210. #endif
  1211. for (i = 0; i < UNITS; wd7000_host[i++] = NULL);
  1212. for (i = 0; i < NUM_CONFIGS; biosptr[i++] = -1);
  1213. tpnt->proc_name = "wd7000";
  1214. tpnt->show_info = &wd7000_show_info;
  1215. tpnt->write_info = wd7000_set_info;
  1216. /*
  1217. * Set up SCB free list, which is shared by all adapters
  1218. */
  1219. init_scbs();
  1220. for (pass = 0; pass < NUM_CONFIGS; pass++) {
  1221. /*
  1222. * First, search for BIOS SIGNATURE...
  1223. */
  1224. for (biosaddr_ptr = 0; biosaddr_ptr < NUM_ADDRS; biosaddr_ptr++)
  1225. for (sig_ptr = 0; sig_ptr < NUM_SIGNATURES; sig_ptr++) {
  1226. for (i = 0; i < pass; i++)
  1227. if (biosptr[i] == biosaddr_ptr)
  1228. break;
  1229. if (i == pass) {
  1230. void __iomem *biosaddr = ioremap(wd7000_biosaddr[biosaddr_ptr] + signatures[sig_ptr].ofs,
  1231. signatures[sig_ptr].len);
  1232. short bios_match = 1;
  1233. if (biosaddr)
  1234. bios_match = check_signature(biosaddr, signatures[sig_ptr].sig, signatures[sig_ptr].len);
  1235. iounmap(biosaddr);
  1236. if (bios_match)
  1237. goto bios_matched;
  1238. }
  1239. }
  1240. bios_matched:
  1241. /*
  1242. * BIOS SIGNATURE has been found.
  1243. */
  1244. #ifdef WD7000_DEBUG
  1245. dprintk("wd7000_detect: pass %d\n", pass + 1);
  1246. if (biosaddr_ptr == NUM_ADDRS)
  1247. dprintk("WD-7000 SST BIOS not detected...\n");
  1248. else
  1249. dprintk("WD-7000 SST BIOS detected at 0x%lx: checking...\n", wd7000_biosaddr[biosaddr_ptr]);
  1250. #endif
  1251. if (configs[pass].irq < 0)
  1252. continue;
  1253. if (unit == UNITS)
  1254. continue;
  1255. iobase = configs[pass].iobase;
  1256. dprintk("wd7000_detect: check IO 0x%x region...\n", iobase);
  1257. if (request_region(iobase, 4, "wd7000")) {
  1258. dprintk("wd7000_detect: ASC reset (IO 0x%x) ...", iobase);
  1259. /*
  1260. * ASC reset...
  1261. */
  1262. outb(ASC_RES, iobase + ASC_CONTROL);
  1263. msleep(10);
  1264. outb(0, iobase + ASC_CONTROL);
  1265. if (WAIT(iobase + ASC_STAT, ASC_STATMASK, CMD_RDY, 0)) {
  1266. dprintk("failed!\n");
  1267. goto err_release;
  1268. } else
  1269. dprintk("ok!\n");
  1270. if (inb(iobase + ASC_INTR_STAT) == 1) {
  1271. /*
  1272. * We register here, to get a pointer to the extra space,
  1273. * which we'll use as the Adapter structure (host) for
  1274. * this adapter. It is located just after the registered
  1275. * Scsi_Host structure (sh), and is located by the empty
  1276. * array hostdata.
  1277. */
  1278. sh = scsi_register(tpnt, sizeof(Adapter));
  1279. if (sh == NULL)
  1280. goto err_release;
  1281. host = (Adapter *) sh->hostdata;
  1282. dprintk("wd7000_detect: adapter allocated at 0x%x\n", (int) host);
  1283. memset(host, 0, sizeof(Adapter));
  1284. host->irq = configs[pass].irq;
  1285. host->dma = configs[pass].dma;
  1286. host->iobase = iobase;
  1287. host->int_counter = 0;
  1288. host->bus_on = configs[pass].bus_on;
  1289. host->bus_off = configs[pass].bus_off;
  1290. host->sh = wd7000_host[unit] = sh;
  1291. unit++;
  1292. dprintk("wd7000_detect: Trying init WD-7000 card at IO " "0x%x, IRQ %d, DMA %d...\n", host->iobase, host->irq, host->dma);
  1293. if (!wd7000_init(host)) /* Initialization failed */
  1294. goto err_unregister;
  1295. /*
  1296. * OK from here - we'll use this adapter/configuration.
  1297. */
  1298. wd7000_revision(host); /* important for scatter/gather */
  1299. /*
  1300. * For boards before rev 6.0, scatter/gather isn't supported.
  1301. */
  1302. if (host->rev1 < 6)
  1303. sh->sg_tablesize = 1;
  1304. present++; /* count it */
  1305. if (biosaddr_ptr != NUM_ADDRS)
  1306. biosptr[pass] = biosaddr_ptr;
  1307. printk(KERN_INFO "Western Digital WD-7000 (rev %d.%d) ", host->rev1, host->rev2);
  1308. printk("using IO 0x%x, IRQ %d, DMA %d.\n", host->iobase, host->irq, host->dma);
  1309. printk(" BUS_ON time: %dns, BUS_OFF time: %dns\n", host->bus_on * 125, host->bus_off * 125);
  1310. }
  1311. } else
  1312. dprintk("wd7000_detect: IO 0x%x region already allocated!\n", iobase);
  1313. continue;
  1314. err_unregister:
  1315. scsi_unregister(sh);
  1316. err_release:
  1317. release_region(iobase, 4);
  1318. }
  1319. if (!present)
  1320. printk("Failed initialization of WD-7000 SCSI card!\n");
  1321. return (present);
  1322. }
  1323. static int wd7000_release(struct Scsi_Host *shost)
  1324. {
  1325. if (shost->irq)
  1326. free_irq(shost->irq, NULL);
  1327. if (shost->io_port && shost->n_io_port)
  1328. release_region(shost->io_port, shost->n_io_port);
  1329. scsi_unregister(shost);
  1330. return 0;
  1331. }
  1332. #if 0
  1333. /*
  1334. * I have absolutely NO idea how to do an abort with the WD7000...
  1335. */
  1336. static int wd7000_abort(Scsi_Cmnd * SCpnt)
  1337. {
  1338. Adapter *host = (Adapter *) SCpnt->device->host->hostdata;
  1339. if (inb(host->iobase + ASC_STAT) & INT_IM) {
  1340. printk("wd7000_abort: lost interrupt\n");
  1341. wd7000_intr_handle(host->irq, NULL, NULL);
  1342. return FAILED;
  1343. }
  1344. return FAILED;
  1345. }
  1346. #endif
  1347. /*
  1348. * Last resort. Reinitialize the board.
  1349. */
  1350. static int wd7000_host_reset(struct scsi_cmnd *SCpnt)
  1351. {
  1352. Adapter *host = (Adapter *) SCpnt->device->host->hostdata;
  1353. spin_lock_irq(SCpnt->device->host->host_lock);
  1354. if (wd7000_adapter_reset(host) < 0) {
  1355. spin_unlock_irq(SCpnt->device->host->host_lock);
  1356. return FAILED;
  1357. }
  1358. wd7000_enable_intr(host);
  1359. spin_unlock_irq(SCpnt->device->host->host_lock);
  1360. return SUCCESS;
  1361. }
  1362. /*
  1363. * This was borrowed directly from aha1542.c. (Zaga)
  1364. */
  1365. static int wd7000_biosparam(struct scsi_device *sdev,
  1366. struct block_device *bdev, sector_t capacity, int *ip)
  1367. {
  1368. char b[BDEVNAME_SIZE];
  1369. dprintk("wd7000_biosparam: dev=%s, size=%llu, ",
  1370. bdevname(bdev, b), (u64)capacity);
  1371. (void)b; /* unused var warning? */
  1372. /*
  1373. * try default translation
  1374. */
  1375. ip[0] = 64;
  1376. ip[1] = 32;
  1377. ip[2] = capacity >> 11;
  1378. /*
  1379. * for disks >1GB do some guessing
  1380. */
  1381. if (ip[2] >= 1024) {
  1382. int info[3];
  1383. /*
  1384. * try to figure out the geometry from the partition table
  1385. */
  1386. if ((scsicam_bios_param(bdev, capacity, info) < 0) || !(((info[0] == 64) && (info[1] == 32)) || ((info[0] == 255) && (info[1] == 63)))) {
  1387. printk("wd7000_biosparam: unable to verify geometry for disk with >1GB.\n" " using extended translation.\n");
  1388. ip[0] = 255;
  1389. ip[1] = 63;
  1390. ip[2] = (unsigned long) capacity / (255 * 63);
  1391. } else {
  1392. ip[0] = info[0];
  1393. ip[1] = info[1];
  1394. ip[2] = info[2];
  1395. if (info[0] == 255)
  1396. printk(KERN_INFO "%s: current partition table is " "using extended translation.\n", __func__);
  1397. }
  1398. }
  1399. dprintk("bios geometry: head=%d, sec=%d, cyl=%d\n", ip[0], ip[1], ip[2]);
  1400. dprintk("WARNING: check, if the bios geometry is correct.\n");
  1401. return (0);
  1402. }
  1403. MODULE_AUTHOR("Thomas Wuensche, John Boyd, Miroslav Zagorac");
  1404. MODULE_DESCRIPTION("Driver for the WD7000 series ISA controllers");
  1405. MODULE_LICENSE("GPL");
  1406. static struct scsi_host_template driver_template = {
  1407. .proc_name = "wd7000",
  1408. .show_info = wd7000_show_info,
  1409. .write_info = wd7000_set_info,
  1410. .name = "Western Digital WD-7000",
  1411. .detect = wd7000_detect,
  1412. .release = wd7000_release,
  1413. .queuecommand = wd7000_queuecommand,
  1414. .eh_host_reset_handler = wd7000_host_reset,
  1415. .bios_param = wd7000_biosparam,
  1416. .can_queue = WD7000_Q,
  1417. .this_id = 7,
  1418. .sg_tablesize = WD7000_SG,
  1419. .unchecked_isa_dma = 1,
  1420. .use_clustering = ENABLE_CLUSTERING,
  1421. };
  1422. #include "scsi_module.c"