PageRenderTime 71ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/scsi/wd7000.c

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