wd7000.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  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 defintion
  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/slab.h>
  174. #include <linux/spinlock.h>
  175. #include <linux/ioport.h>
  176. #include <linux/proc_fs.h>
  177. #include <linux/blkdev.h>
  178. #include <linux/init.h>
  179. #include <linux/stat.h>
  180. #include <linux/io.h>
  181. #include <asm/system.h>
  182. #include <asm/dma.h>
  183. #include <scsi/scsi.h>
  184. #include <scsi/scsi_cmnd.h>
  185. #include <scsi/scsi_device.h>
  186. #include <scsi/scsi_host.h>
  187. #include <scsi/scsicam.h>
  188. #undef WD7000_DEBUG /* general debug */
  189. #ifdef WD7000_DEBUG
  190. #define dprintk printk
  191. #else
  192. #define dprintk(format,args...)
  193. #endif
  194. /*
  195. * Mailbox structure sizes.
  196. * I prefer to keep the number of ICMBs much larger than the number of
  197. * OGMBs. OGMBs are used very quickly by the driver to start one or
  198. * more commands, while ICMBs are used by the host adapter per command.
  199. */
  200. #define OGMB_CNT 16
  201. #define ICMB_CNT 32
  202. /*
  203. * Scb's are shared by all active adapters. So, if they all become busy,
  204. * callers may be made to wait in alloc_scbs for them to free. That can
  205. * be avoided by setting MAX_SCBS to NUM_CONFIG * WD7000_Q. If you'd
  206. * rather conserve memory, use a smaller number (> 0, of course) - things
  207. * will should still work OK.
  208. */
  209. #define MAX_SCBS 32
  210. /*
  211. * In this version, sg_tablesize now defaults to WD7000_SG, and will
  212. * be set to SG_NONE for older boards. This is the reverse of the
  213. * previous default, and was changed so that the driver-level
  214. * scsi_host_template would reflect the driver's support for scatter/
  215. * gather.
  216. *
  217. * Also, it has been reported that boards at Revision 6 support scatter/
  218. * gather, so the new definition of an "older" board has been changed
  219. * accordingly.
  220. */
  221. #define WD7000_Q 16
  222. #define WD7000_SG 16
  223. /*
  224. * WD7000-specific mailbox structure
  225. *
  226. */
  227. typedef volatile struct mailbox {
  228. unchar status;
  229. unchar scbptr[3]; /* SCSI-style - MSB first (big endian) */
  230. } Mailbox;
  231. /*
  232. * This structure should contain all per-adapter global data. I.e., any
  233. * new global per-adapter data should put in here.
  234. */
  235. typedef struct adapter {
  236. struct Scsi_Host *sh; /* Pointer to Scsi_Host structure */
  237. int iobase; /* This adapter's I/O base address */
  238. int irq; /* This adapter's IRQ level */
  239. int dma; /* This adapter's DMA channel */
  240. int int_counter; /* This adapter's interrupt counter */
  241. int bus_on; /* This adapter's BUS_ON time */
  242. int bus_off; /* This adapter's BUS_OFF time */
  243. struct { /* This adapter's mailboxes */
  244. Mailbox ogmb[OGMB_CNT]; /* Outgoing mailboxes */
  245. Mailbox icmb[ICMB_CNT]; /* Incoming mailboxes */
  246. } mb;
  247. int next_ogmb; /* to reduce contention at mailboxes */
  248. unchar control; /* shadows CONTROL port value */
  249. unchar rev1, rev2; /* filled in by wd7000_revision */
  250. } Adapter;
  251. /*
  252. * (linear) base address for ROM BIOS
  253. */
  254. static const long wd7000_biosaddr[] = {
  255. 0xc0000, 0xc2000, 0xc4000, 0xc6000, 0xc8000, 0xca000, 0xcc000, 0xce000,
  256. 0xd0000, 0xd2000, 0xd4000, 0xd6000, 0xd8000, 0xda000, 0xdc000, 0xde000
  257. };
  258. #define NUM_ADDRS ARRAY_SIZE(wd7000_biosaddr)
  259. static const unsigned short wd7000_iobase[] = {
  260. 0x0300, 0x0308, 0x0310, 0x0318, 0x0320, 0x0328, 0x0330, 0x0338,
  261. 0x0340, 0x0348, 0x0350, 0x0358, 0x0360, 0x0368, 0x0370, 0x0378,
  262. 0x0380, 0x0388, 0x0390, 0x0398, 0x03a0, 0x03a8, 0x03b0, 0x03b8,
  263. 0x03c0, 0x03c8, 0x03d0, 0x03d8, 0x03e0, 0x03e8, 0x03f0, 0x03f8
  264. };
  265. #define NUM_IOPORTS ARRAY_SIZE(wd7000_iobase)
  266. static const short wd7000_irq[] = { 3, 4, 5, 7, 9, 10, 11, 12, 14, 15 };
  267. #define NUM_IRQS ARRAY_SIZE(wd7000_irq)
  268. static const short wd7000_dma[] = { 5, 6, 7 };
  269. #define NUM_DMAS ARRAY_SIZE(wd7000_dma)
  270. /*
  271. * The following is set up by wd7000_detect, and used thereafter for
  272. * proc and other global ookups
  273. */
  274. #define UNITS 8
  275. static struct Scsi_Host *wd7000_host[UNITS];
  276. #define BUS_ON 64 /* x 125ns = 8000ns (BIOS default) */
  277. #define BUS_OFF 15 /* x 125ns = 1875ns (BIOS default) */
  278. /*
  279. * Standard Adapter Configurations - used by wd7000_detect
  280. */
  281. typedef struct {
  282. short irq; /* IRQ level */
  283. short dma; /* DMA channel */
  284. unsigned iobase; /* I/O base address */
  285. short bus_on; /* Time that WD7000 spends on the AT-bus when */
  286. /* transferring data. BIOS default is 8000ns. */
  287. short bus_off; /* Time that WD7000 spends OFF THE BUS after */
  288. /* while it is transferring data. */
  289. /* BIOS default is 1875ns */
  290. } Config;
  291. /*
  292. * Add here your configuration...
  293. */
  294. static Config configs[] = {
  295. {15, 6, 0x350, BUS_ON, BUS_OFF}, /* defaults for single adapter */
  296. {11, 5, 0x320, BUS_ON, BUS_OFF}, /* defaults for second adapter */
  297. {7, 6, 0x350, BUS_ON, BUS_OFF}, /* My configuration (Zaga) */
  298. {-1, -1, 0x0, BUS_ON, BUS_OFF} /* Empty slot */
  299. };
  300. #define NUM_CONFIGS ARRAY_SIZE(configs)
  301. /*
  302. * The following list defines strings to look for in the BIOS that identify
  303. * it as the WD7000-FASST2 SST BIOS. I suspect that something should be
  304. * added for the Future Domain version.
  305. */
  306. typedef struct signature {
  307. const char *sig; /* String to look for */
  308. unsigned long ofs; /* offset from BIOS base address */
  309. unsigned len; /* length of string */
  310. } Signature;
  311. static const Signature signatures[] = {
  312. {"SSTBIOS", 0x0000d, 7} /* "SSTBIOS" @ offset 0x0000d */
  313. };
  314. #define NUM_SIGNATURES ARRAY_SIZE(signatures)
  315. /*
  316. * I/O Port Offsets and Bit Definitions
  317. * 4 addresses are used. Those not defined here are reserved.
  318. */
  319. #define ASC_STAT 0 /* Status, Read */
  320. #define ASC_COMMAND 0 /* Command, Write */
  321. #define ASC_INTR_STAT 1 /* Interrupt Status, Read */
  322. #define ASC_INTR_ACK 1 /* Acknowledge, Write */
  323. #define ASC_CONTROL 2 /* Control, Write */
  324. /*
  325. * ASC Status Port
  326. */
  327. #define INT_IM 0x80 /* Interrupt Image Flag */
  328. #define CMD_RDY 0x40 /* Command Port Ready */
  329. #define CMD_REJ 0x20 /* Command Port Byte Rejected */
  330. #define ASC_INIT 0x10 /* ASC Initialized Flag */
  331. #define ASC_STATMASK 0xf0 /* The lower 4 Bytes are reserved */
  332. /*
  333. * COMMAND opcodes
  334. *
  335. * Unfortunately, I have no idea how to properly use some of these commands,
  336. * as the OEM manual does not make it clear. I have not been able to use
  337. * enable/disable unsolicited interrupts or the reset commands with any
  338. * discernible effect whatsoever. I think they may be related to certain
  339. * ICB commands, but again, the OEM manual doesn't make that clear.
  340. */
  341. #define NO_OP 0 /* NO-OP toggles CMD_RDY bit in ASC_STAT */
  342. #define INITIALIZATION 1 /* initialization (10 bytes) */
  343. #define DISABLE_UNS_INTR 2 /* disable unsolicited interrupts */
  344. #define ENABLE_UNS_INTR 3 /* enable unsolicited interrupts */
  345. #define INTR_ON_FREE_OGMB 4 /* interrupt on free OGMB */
  346. #define SOFT_RESET 5 /* SCSI bus soft reset */
  347. #define HARD_RESET_ACK 6 /* SCSI bus hard reset acknowledge */
  348. #define START_OGMB 0x80 /* start command in OGMB (n) */
  349. #define SCAN_OGMBS 0xc0 /* start multiple commands, signature (n) */
  350. /* where (n) = lower 6 bits */
  351. /*
  352. * For INITIALIZATION:
  353. */
  354. typedef struct initCmd {
  355. unchar op; /* command opcode (= 1) */
  356. unchar ID; /* Adapter's SCSI ID */
  357. unchar bus_on; /* Bus on time, x 125ns (see below) */
  358. unchar bus_off; /* Bus off time, "" "" */
  359. unchar rsvd; /* Reserved */
  360. unchar mailboxes[3]; /* Address of Mailboxes, MSB first */
  361. unchar ogmbs; /* Number of outgoing MBs, max 64, 0,1 = 1 */
  362. unchar icmbs; /* Number of incoming MBs, "" "" */
  363. } InitCmd;
  364. /*
  365. * Interrupt Status Port - also returns diagnostic codes at ASC reset
  366. *
  367. * if msb is zero, the lower bits are diagnostic status
  368. * Diagnostics:
  369. * 01 No diagnostic error occurred
  370. * 02 RAM failure
  371. * 03 FIFO R/W failed
  372. * 04 SBIC register read/write failed
  373. * 05 Initialization D-FF failed
  374. * 06 Host IRQ D-FF failed
  375. * 07 ROM checksum error
  376. * Interrupt status (bitwise):
  377. * 10NNNNNN outgoing mailbox NNNNNN is free
  378. * 11NNNNNN incoming mailbox NNNNNN needs service
  379. */
  380. #define MB_INTR 0xC0 /* Mailbox Service possible/required */
  381. #define IMB_INTR 0x40 /* 1 Incoming / 0 Outgoing */
  382. #define MB_MASK 0x3f /* mask for mailbox number */
  383. /*
  384. * CONTROL port bits
  385. */
  386. #define INT_EN 0x08 /* Interrupt Enable */
  387. #define DMA_EN 0x04 /* DMA Enable */
  388. #define SCSI_RES 0x02 /* SCSI Reset */
  389. #define ASC_RES 0x01 /* ASC Reset */
  390. /*
  391. * Driver data structures:
  392. * - mb and scbs are required for interfacing with the host adapter.
  393. * An SCB has extra fields not visible to the adapter; mb's
  394. * _cannot_ do this, since the adapter assumes they are contiguous in
  395. * memory, 4 bytes each, with ICMBs following OGMBs, and uses this fact
  396. * to access them.
  397. * - An icb is for host-only (non-SCSI) commands. ICBs are 16 bytes each;
  398. * the additional bytes are used only by the driver.
  399. * - For now, a pool of SCBs are kept in global storage by this driver,
  400. * and are allocated and freed as needed.
  401. *
  402. * The 7000-FASST2 marks OGMBs empty as soon as it has _started_ a command,
  403. * not when it has finished. Since the SCB must be around for completion,
  404. * problems arise when SCBs correspond to OGMBs, which may be reallocated
  405. * earlier (or delayed unnecessarily until a command completes).
  406. * Mailboxes are used as transient data structures, simply for
  407. * carrying SCB addresses to/from the 7000-FASST2.
  408. *
  409. * Note also since SCBs are not "permanently" associated with mailboxes,
  410. * there is no need to keep a global list of scsi_cmnd pointers indexed
  411. * by OGMB. Again, SCBs reference their scsi_cmnds directly, so mailbox
  412. * indices need not be involved.
  413. */
  414. /*
  415. * WD7000-specific scatter/gather element structure
  416. */
  417. typedef struct sgb {
  418. unchar len[3];
  419. unchar ptr[3]; /* Also SCSI-style - MSB first */
  420. } Sgb;
  421. typedef struct scb { /* Command Control Block 5.4.1 */
  422. unchar op; /* Command Control Block Operation Code */
  423. unchar idlun; /* op=0,2:Target Id, op=1:Initiator Id */
  424. /* Outbound data transfer, length is checked */
  425. /* Inbound data transfer, length is checked */
  426. /* Logical Unit Number */
  427. unchar cdb[12]; /* SCSI Command Block */
  428. volatile unchar status; /* SCSI Return Status */
  429. volatile unchar vue; /* Vendor Unique Error Code */
  430. unchar maxlen[3]; /* Maximum Data Transfer Length */
  431. unchar dataptr[3]; /* SCSI Data Block Pointer */
  432. unchar linkptr[3]; /* Next Command Link Pointer */
  433. unchar direc; /* Transfer Direction */
  434. unchar reserved2[6]; /* SCSI Command Descriptor Block */
  435. /* end of hardware SCB */
  436. struct scsi_cmnd *SCpnt;/* scsi_cmnd using this SCB */
  437. Sgb sgb[WD7000_SG]; /* Scatter/gather list for this SCB */
  438. Adapter *host; /* host adapter */
  439. struct scb *next; /* for lists of scbs */
  440. } Scb;
  441. /*
  442. * This driver is written to allow host-only commands to be executed.
  443. * These use a 16-byte block called an ICB. The format is extended by the
  444. * driver to 18 bytes, to support the status returned in the ICMB and
  445. * an execution phase code.
  446. *
  447. * There are other formats besides these; these are the ones I've tried
  448. * to use. Formats for some of the defined ICB opcodes are not defined
  449. * (notably, get/set unsolicited interrupt status) in my copy of the OEM
  450. * manual, and others are ambiguous/hard to follow.
  451. */
  452. #define ICB_OP_MASK 0x80 /* distinguishes scbs from icbs */
  453. #define ICB_OP_OPEN_RBUF 0x80 /* open receive buffer */
  454. #define ICB_OP_RECV_CMD 0x81 /* receive command from initiator */
  455. #define ICB_OP_RECV_DATA 0x82 /* receive data from initiator */
  456. #define ICB_OP_RECV_SDATA 0x83 /* receive data with status from init. */
  457. #define ICB_OP_SEND_DATA 0x84 /* send data with status to initiator */
  458. #define ICB_OP_SEND_STAT 0x86 /* send command status to initiator */
  459. /* 0x87 is reserved */
  460. #define ICB_OP_READ_INIT 0x88 /* read initialization bytes */
  461. #define ICB_OP_READ_ID 0x89 /* read adapter's SCSI ID */
  462. #define ICB_OP_SET_UMASK 0x8A /* set unsolicited interrupt mask */
  463. #define ICB_OP_GET_UMASK 0x8B /* read unsolicited interrupt mask */
  464. #define ICB_OP_GET_REVISION 0x8C /* read firmware revision level */
  465. #define ICB_OP_DIAGNOSTICS 0x8D /* execute diagnostics */
  466. #define ICB_OP_SET_EPARMS 0x8E /* set execution parameters */
  467. #define ICB_OP_GET_EPARMS 0x8F /* read execution parameters */
  468. typedef struct icbRecvCmd {
  469. unchar op;
  470. unchar IDlun; /* Initiator SCSI ID/lun */
  471. unchar len[3]; /* command buffer length */
  472. unchar ptr[3]; /* command buffer address */
  473. unchar rsvd[7]; /* reserved */
  474. volatile unchar vue; /* vendor-unique error code */
  475. volatile unchar status; /* returned (icmb) status */
  476. volatile unchar phase; /* used by interrupt handler */
  477. } IcbRecvCmd;
  478. typedef struct icbSendStat {
  479. unchar op;
  480. unchar IDlun; /* Target SCSI ID/lun */
  481. unchar stat; /* (outgoing) completion status byte 1 */
  482. unchar rsvd[12]; /* reserved */
  483. volatile unchar vue; /* vendor-unique error code */
  484. volatile unchar status; /* returned (icmb) status */
  485. volatile unchar phase; /* used by interrupt handler */
  486. } IcbSendStat;
  487. typedef struct icbRevLvl {
  488. unchar op;
  489. volatile unchar primary; /* primary revision level (returned) */
  490. volatile unchar secondary; /* secondary revision level (returned) */
  491. unchar rsvd[12]; /* reserved */
  492. volatile unchar vue; /* vendor-unique error code */
  493. volatile unchar status; /* returned (icmb) status */
  494. volatile unchar phase; /* used by interrupt handler */
  495. } IcbRevLvl;
  496. typedef struct icbUnsMask { /* I'm totally guessing here */
  497. unchar op;
  498. volatile unchar mask[14]; /* mask bits */
  499. #if 0
  500. unchar rsvd[12]; /* reserved */
  501. #endif
  502. volatile unchar vue; /* vendor-unique error code */
  503. volatile unchar status; /* returned (icmb) status */
  504. volatile unchar phase; /* used by interrupt handler */
  505. } IcbUnsMask;
  506. typedef struct icbDiag {
  507. unchar op;
  508. unchar type; /* diagnostics type code (0-3) */
  509. unchar len[3]; /* buffer length */
  510. unchar ptr[3]; /* buffer address */
  511. unchar rsvd[7]; /* reserved */
  512. volatile unchar vue; /* vendor-unique error code */
  513. volatile unchar status; /* returned (icmb) status */
  514. volatile unchar phase; /* used by interrupt handler */
  515. } IcbDiag;
  516. #define ICB_DIAG_POWERUP 0 /* Power-up diags only */
  517. #define ICB_DIAG_WALKING 1 /* walking 1's pattern */
  518. #define ICB_DIAG_DMA 2 /* DMA - system memory diags */
  519. #define ICB_DIAG_FULL 3 /* do both 1 & 2 */
  520. typedef struct icbParms {
  521. unchar op;
  522. unchar rsvd1; /* reserved */
  523. unchar len[3]; /* parms buffer length */
  524. unchar ptr[3]; /* parms buffer address */
  525. unchar idx[2]; /* index (MSB-LSB) */
  526. unchar rsvd2[5]; /* reserved */
  527. volatile unchar vue; /* vendor-unique error code */
  528. volatile unchar status; /* returned (icmb) status */
  529. volatile unchar phase; /* used by interrupt handler */
  530. } IcbParms;
  531. typedef struct icbAny {
  532. unchar op;
  533. unchar data[14]; /* format-specific data */
  534. volatile unchar vue; /* vendor-unique error code */
  535. volatile unchar status; /* returned (icmb) status */
  536. volatile unchar phase; /* used by interrupt handler */
  537. } IcbAny;
  538. typedef union icb {
  539. unchar op; /* ICB opcode */
  540. IcbRecvCmd recv_cmd; /* format for receive command */
  541. IcbSendStat send_stat; /* format for send status */
  542. IcbRevLvl rev_lvl; /* format for get revision level */
  543. IcbDiag diag; /* format for execute diagnostics */
  544. IcbParms eparms; /* format for get/set exec parms */
  545. IcbAny icb; /* generic format */
  546. unchar data[18];
  547. } Icb;
  548. #ifdef MODULE
  549. static char *wd7000;
  550. module_param(wd7000, charp, 0);
  551. #endif
  552. /*
  553. * Driver SCB structure pool.
  554. *
  555. * The SCBs declared here are shared by all host adapters; hence, this
  556. * structure is not part of the Adapter structure.
  557. */
  558. static Scb scbs[MAX_SCBS];
  559. static Scb *scbfree; /* free list */
  560. static int freescbs = MAX_SCBS; /* free list counter */
  561. static spinlock_t scbpool_lock; /* guards the scb free list and count */
  562. /*
  563. * END of data/declarations - code follows.
  564. */
  565. static void __init setup_error(char *mesg, int *ints)
  566. {
  567. if (ints[0] == 3)
  568. printk(KERN_ERR "wd7000_setup: \"wd7000=%d,%d,0x%x\" -> %s\n", ints[1], ints[2], ints[3], mesg);
  569. else if (ints[0] == 4)
  570. printk(KERN_ERR "wd7000_setup: \"wd7000=%d,%d,0x%x,%d\" -> %s\n", ints[1], ints[2], ints[3], ints[4], mesg);
  571. else
  572. 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);
  573. }
  574. /*
  575. * Note: You can now set these options from the kernel's "command line".
  576. * The syntax is:
  577. *
  578. * wd7000=<IRQ>,<DMA>,<IO>[,<BUS_ON>[,<BUS_OFF>]]
  579. *
  580. * , where BUS_ON and BUS_OFF are in nanoseconds. BIOS default values
  581. * are 8000ns for BUS_ON and 1875ns for BUS_OFF.
  582. * eg:
  583. * wd7000=7,6,0x350
  584. *
  585. * will configure the driver for a WD-7000 controller
  586. * using IRQ 15 with a DMA channel 6, at IO base address 0x350.
  587. */
  588. static int __init wd7000_setup(char *str)
  589. {
  590. static short wd7000_card_num; /* .bss will zero this */
  591. short i;
  592. int ints[6];
  593. (void) get_options(str, ARRAY_SIZE(ints), ints);
  594. if (wd7000_card_num >= NUM_CONFIGS) {
  595. printk(KERN_ERR "%s: Too many \"wd7000=\" configurations in " "command line!\n", __func__);
  596. return 0;
  597. }
  598. if ((ints[0] < 3) || (ints[0] > 5)) {
  599. printk(KERN_ERR "%s: Error in command line! " "Usage: wd7000=<IRQ>,<DMA>,IO>[,<BUS_ON>" "[,<BUS_OFF>]]\n", __func__);
  600. } else {
  601. for (i = 0; i < NUM_IRQS; i++)
  602. if (ints[1] == wd7000_irq[i])
  603. break;
  604. if (i == NUM_IRQS) {
  605. setup_error("invalid IRQ.", ints);
  606. return 0;
  607. } else
  608. configs[wd7000_card_num].irq = ints[1];
  609. for (i = 0; i < NUM_DMAS; i++)
  610. if (ints[2] == wd7000_dma[i])
  611. break;
  612. if (i == NUM_DMAS) {
  613. setup_error("invalid DMA channel.", ints);
  614. return 0;
  615. } else
  616. configs[wd7000_card_num].dma = ints[2];
  617. for (i = 0; i < NUM_IOPORTS; i++)
  618. if (ints[3] == wd7000_iobase[i])
  619. break;
  620. if (i == NUM_IOPORTS) {
  621. setup_error("invalid I/O base address.", ints);
  622. return 0;
  623. } else
  624. configs[wd7000_card_num].iobase = ints[3];
  625. if (ints[0] > 3) {
  626. if ((ints[4] < 500) || (ints[4] > 31875)) {
  627. setup_error("BUS_ON value is out of range (500" " to 31875 nanoseconds)!", ints);
  628. configs[wd7000_card_num].bus_on = BUS_ON;
  629. } else
  630. configs[wd7000_card_num].bus_on = ints[4] / 125;
  631. } else
  632. configs[wd7000_card_num].bus_on = BUS_ON;
  633. if (ints[0] > 4) {
  634. if ((ints[5] < 500) || (ints[5] > 31875)) {
  635. setup_error("BUS_OFF value is out of range (500" " to 31875 nanoseconds)!", ints);
  636. configs[wd7000_card_num].bus_off = BUS_OFF;
  637. } else
  638. configs[wd7000_card_num].bus_off = ints[5] / 125;
  639. } else
  640. configs[wd7000_card_num].bus_off = BUS_OFF;
  641. if (wd7000_card_num) {
  642. for (i = 0; i < (wd7000_card_num - 1); i++) {
  643. int j = i + 1;
  644. for (; j < wd7000_card_num; j++)
  645. if (configs[i].irq == configs[j].irq) {
  646. setup_error("duplicated IRQ!", ints);
  647. return 0;
  648. }
  649. if (configs[i].dma == configs[j].dma) {
  650. setup_error("duplicated DMA " "channel!", ints);
  651. return 0;
  652. }
  653. if (configs[i].iobase == configs[j].iobase) {
  654. setup_error("duplicated I/O " "base address!", ints);
  655. return 0;
  656. }
  657. }
  658. }
  659. dprintk(KERN_DEBUG "wd7000_setup: IRQ=%d, DMA=%d, I/O=0x%x, "
  660. "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);
  661. wd7000_card_num++;
  662. }
  663. return 1;
  664. }
  665. __setup("wd7000=", wd7000_setup);
  666. static inline void any2scsi(unchar * scsi, int any)
  667. {
  668. *scsi++ = (unsigned)any >> 16;
  669. *scsi++ = (unsigned)any >> 8;
  670. *scsi++ = any;
  671. }
  672. static inline int scsi2int(unchar * scsi)
  673. {
  674. return (scsi[0] << 16) | (scsi[1] << 8) | scsi[2];
  675. }
  676. static inline void wd7000_enable_intr(Adapter * host)
  677. {
  678. host->control |= INT_EN;
  679. outb(host->control, host->iobase + ASC_CONTROL);
  680. }
  681. static inline void wd7000_enable_dma(Adapter * host)
  682. {
  683. unsigned long flags;
  684. host->control |= DMA_EN;
  685. outb(host->control, host->iobase + ASC_CONTROL);
  686. flags = claim_dma_lock();
  687. set_dma_mode(host->dma, DMA_MODE_CASCADE);
  688. enable_dma(host->dma);
  689. release_dma_lock(flags);
  690. }
  691. #define WAITnexttimeout 200 /* 2 seconds */
  692. static inline short WAIT(unsigned port, unsigned mask, unsigned allof, unsigned noneof)
  693. {
  694. unsigned WAITbits;
  695. unsigned long WAITtimeout = jiffies + WAITnexttimeout;
  696. while (time_before_eq(jiffies, WAITtimeout)) {
  697. WAITbits = inb(port) & mask;
  698. if (((WAITbits & allof) == allof) && ((WAITbits & noneof) == 0))
  699. return (0);
  700. }
  701. return (1);
  702. }
  703. static inline int command_out(Adapter * host, unchar * cmd, int len)
  704. {
  705. if (!WAIT(host->iobase + ASC_STAT, ASC_STATMASK, CMD_RDY, 0)) {
  706. while (len--) {
  707. do {
  708. outb(*cmd, host->iobase + ASC_COMMAND);
  709. WAIT(host->iobase + ASC_STAT, ASC_STATMASK, CMD_RDY, 0);
  710. } while (inb(host->iobase + ASC_STAT) & CMD_REJ);
  711. cmd++;
  712. }
  713. return (1);
  714. }
  715. printk(KERN_WARNING "wd7000 command_out: WAIT failed(%d)\n", len + 1);
  716. return (0);
  717. }
  718. /*
  719. * This version of alloc_scbs is in preparation for supporting multiple
  720. * commands per lun and command chaining, by queueing pending commands.
  721. * We will need to allocate Scbs in blocks since they will wait to be
  722. * executed so there is the possibility of deadlock otherwise.
  723. * Also, to keep larger requests from being starved by smaller requests,
  724. * we limit access to this routine with an internal busy flag, so that
  725. * the satisfiability of a request is not dependent on the size of the
  726. * request.
  727. */
  728. static inline Scb *alloc_scbs(struct Scsi_Host *host, int needed)
  729. {
  730. Scb *scb, *p = NULL;
  731. unsigned long flags;
  732. unsigned long timeout = jiffies + WAITnexttimeout;
  733. unsigned long now;
  734. int i;
  735. if (needed <= 0)
  736. return (NULL); /* sanity check */
  737. spin_unlock_irq(host->host_lock);
  738. retry:
  739. while (freescbs < needed) {
  740. timeout = jiffies + WAITnexttimeout;
  741. do {
  742. /* FIXME: can we actually just yield here ?? */
  743. for (now = jiffies; now == jiffies;)
  744. cpu_relax(); /* wait a jiffy */
  745. } while (freescbs < needed && time_before_eq(jiffies, timeout));
  746. /*
  747. * If we get here with enough free Scbs, we can take them.
  748. * Otherwise, we timed out and didn't get enough.
  749. */
  750. if (freescbs < needed) {
  751. printk(KERN_ERR "wd7000: can't get enough free SCBs.\n");
  752. return (NULL);
  753. }
  754. }
  755. /* Take the lock, then check we didnt get beaten, if so try again */
  756. spin_lock_irqsave(&scbpool_lock, flags);
  757. if (freescbs < needed) {
  758. spin_unlock_irqrestore(&scbpool_lock, flags);
  759. goto retry;
  760. }
  761. scb = scbfree;
  762. freescbs -= needed;
  763. for (i = 0; i < needed; i++) {
  764. p = scbfree;
  765. scbfree = p->next;
  766. }
  767. p->next = NULL;
  768. spin_unlock_irqrestore(&scbpool_lock, flags);
  769. spin_lock_irq(host->host_lock);
  770. return (scb);
  771. }
  772. static inline void free_scb(Scb * scb)
  773. {
  774. unsigned long flags;
  775. spin_lock_irqsave(&scbpool_lock, flags);
  776. memset(scb, 0, sizeof(Scb));
  777. scb->next = scbfree;
  778. scbfree = scb;
  779. freescbs++;
  780. spin_unlock_irqrestore(&scbpool_lock, flags);
  781. }
  782. static inline void init_scbs(void)
  783. {
  784. int i;
  785. spin_lock_init(&scbpool_lock);
  786. /* This is only ever called before the SCB pool is active */
  787. scbfree = &(scbs[0]);
  788. memset(scbs, 0, sizeof(scbs));
  789. for (i = 0; i < MAX_SCBS - 1; i++) {
  790. scbs[i].next = &(scbs[i + 1]);
  791. scbs[i].SCpnt = NULL;
  792. }
  793. scbs[MAX_SCBS - 1].next = NULL;
  794. scbs[MAX_SCBS - 1].SCpnt = NULL;
  795. }
  796. static int mail_out(Adapter * host, Scb * scbptr)
  797. /*
  798. * Note: this can also be used for ICBs; just cast to the parm type.
  799. */
  800. {
  801. int i, ogmb;
  802. unsigned long flags;
  803. unchar start_ogmb;
  804. Mailbox *ogmbs = host->mb.ogmb;
  805. int *next_ogmb = &(host->next_ogmb);
  806. dprintk("wd7000_mail_out: 0x%06lx", (long) scbptr);
  807. /* We first look for a free outgoing mailbox */
  808. spin_lock_irqsave(host->sh->host_lock, flags);
  809. ogmb = *next_ogmb;
  810. for (i = 0; i < OGMB_CNT; i++) {
  811. if (ogmbs[ogmb].status == 0) {
  812. dprintk(" using OGMB 0x%x", ogmb);
  813. ogmbs[ogmb].status = 1;
  814. any2scsi((unchar *) ogmbs[ogmb].scbptr, (int) scbptr);
  815. *next_ogmb = (ogmb + 1) % OGMB_CNT;
  816. break;
  817. } else
  818. ogmb = (ogmb + 1) % OGMB_CNT;
  819. }
  820. spin_unlock_irqrestore(host->sh->host_lock, flags);
  821. dprintk(", scb is 0x%06lx", (long) scbptr);
  822. if (i >= OGMB_CNT) {
  823. /*
  824. * Alternatively, we might issue the "interrupt on free OGMB",
  825. * and sleep, but it must be ensured that it isn't the init
  826. * task running. Instead, this version assumes that the caller
  827. * will be persistent, and try again. Since it's the adapter
  828. * that marks OGMB's free, waiting even with interrupts off
  829. * should work, since they are freed very quickly in most cases.
  830. */
  831. dprintk(", no free OGMBs.\n");
  832. return (0);
  833. }
  834. wd7000_enable_intr(host);
  835. start_ogmb = START_OGMB | ogmb;
  836. command_out(host, &start_ogmb, 1);
  837. dprintk(", awaiting interrupt.\n");
  838. return (1);
  839. }
  840. static int make_code(unsigned hosterr, unsigned scsierr)
  841. {
  842. #ifdef WD7000_DEBUG
  843. int in_error = hosterr;
  844. #endif
  845. switch ((hosterr >> 8) & 0xff) {
  846. case 0: /* Reserved */
  847. hosterr = DID_ERROR;
  848. break;
  849. case 1: /* Command Complete, no errors */
  850. hosterr = DID_OK;
  851. break;
  852. case 2: /* Command complete, error logged in scb status (scsierr) */
  853. hosterr = DID_OK;
  854. break;
  855. case 4: /* Command failed to complete - timeout */
  856. hosterr = DID_TIME_OUT;
  857. break;
  858. case 5: /* Command terminated; Bus reset by external device */
  859. hosterr = DID_RESET;
  860. break;
  861. case 6: /* Unexpected Command Received w/ host as target */
  862. hosterr = DID_BAD_TARGET;
  863. break;
  864. case 80: /* Unexpected Reselection */
  865. case 81: /* Unexpected Selection */
  866. hosterr = DID_BAD_INTR;
  867. break;
  868. case 82: /* Abort Command Message */
  869. hosterr = DID_ABORT;
  870. break;
  871. case 83: /* SCSI Bus Software Reset */
  872. case 84: /* SCSI Bus Hardware Reset */
  873. hosterr = DID_RESET;
  874. break;
  875. default: /* Reserved */
  876. hosterr = DID_ERROR;
  877. }
  878. #ifdef WD7000_DEBUG
  879. if (scsierr || hosterr)
  880. dprintk("\nSCSI command error: SCSI 0x%02x host 0x%04x return %d\n", scsierr, in_error, hosterr);
  881. #endif
  882. return (scsierr | (hosterr << 16));
  883. }
  884. #define wd7000_intr_ack(host) outb (0, host->iobase + ASC_INTR_ACK)
  885. static irqreturn_t wd7000_intr(int irq, void *dev_id)
  886. {
  887. Adapter *host = (Adapter *) dev_id;
  888. int flag, icmb, errstatus, icmb_status;
  889. int host_error, scsi_error;
  890. Scb *scb; /* for SCSI commands */
  891. IcbAny *icb; /* for host commands */
  892. struct scsi_cmnd *SCpnt;
  893. Mailbox *icmbs = host->mb.icmb;
  894. unsigned long flags;
  895. spin_lock_irqsave(host->sh->host_lock, flags);
  896. host->int_counter++;
  897. dprintk("wd7000_intr: irq = %d, host = 0x%06lx\n", irq, (long) host);
  898. flag = inb(host->iobase + ASC_INTR_STAT);
  899. dprintk("wd7000_intr: intr stat = 0x%02x\n", flag);
  900. if (!(inb(host->iobase + ASC_STAT) & INT_IM)) {
  901. /* NB: these are _very_ possible if IRQ 15 is being used, since
  902. * it's the "garbage collector" on the 2nd 8259 PIC. Specifically,
  903. * any interrupt signal into the 8259 which can't be identified
  904. * comes out as 7 from the 8259, which is 15 to the host. Thus, it
  905. * is a good thing the WD7000 has an interrupt status port, so we
  906. * can sort these out. Otherwise, electrical noise and other such
  907. * problems would be indistinguishable from valid interrupts...
  908. */
  909. dprintk("wd7000_intr: phantom interrupt...\n");
  910. goto ack;
  911. }
  912. if (!(flag & MB_INTR))
  913. goto ack;
  914. /* The interrupt is for a mailbox */
  915. if (!(flag & IMB_INTR)) {
  916. dprintk("wd7000_intr: free outgoing mailbox\n");
  917. /*
  918. * If sleep_on() and the "interrupt on free OGMB" command are
  919. * used in mail_out(), wake_up() should correspondingly be called
  920. * here. For now, we don't need to do anything special.
  921. */
  922. goto ack;
  923. }
  924. /* The interrupt is for an incoming mailbox */
  925. icmb = flag & MB_MASK;
  926. icmb_status = icmbs[icmb].status;
  927. if (icmb_status & 0x80) { /* unsolicited - result in ICMB */
  928. dprintk("wd7000_intr: unsolicited interrupt 0x%02x\n", icmb_status);
  929. goto ack;
  930. }
  931. /* Aaaargh! (Zaga) */
  932. scb = isa_bus_to_virt(scsi2int((unchar *) icmbs[icmb].scbptr));
  933. icmbs[icmb].status = 0;
  934. if (scb->op & ICB_OP_MASK) { /* an SCB is done */
  935. icb = (IcbAny *) scb;
  936. icb->status = icmb_status;
  937. icb->phase = 0;
  938. goto ack;
  939. }
  940. SCpnt = scb->SCpnt;
  941. if (--(SCpnt->SCp.phase) <= 0) { /* all scbs are done */
  942. host_error = scb->vue | (icmb_status << 8);
  943. scsi_error = scb->status;
  944. errstatus = make_code(host_error, scsi_error);
  945. SCpnt->result = errstatus;
  946. free_scb(scb);
  947. SCpnt->scsi_done(SCpnt);
  948. }
  949. ack:
  950. dprintk("wd7000_intr: return from interrupt handler\n");
  951. wd7000_intr_ack(host);
  952. spin_unlock_irqrestore(host->sh->host_lock, flags);
  953. return IRQ_HANDLED;
  954. }
  955. static int wd7000_queuecommand(struct scsi_cmnd *SCpnt,
  956. void (*done)(struct scsi_cmnd *))
  957. {
  958. Scb *scb;
  959. Sgb *sgb;
  960. unchar *cdb = (unchar *) SCpnt->cmnd;
  961. unchar idlun;
  962. short cdblen;
  963. int nseg;
  964. Adapter *host = (Adapter *) SCpnt->device->host->hostdata;
  965. cdblen = SCpnt->cmd_len;
  966. idlun = ((SCpnt->device->id << 5) & 0xe0) | (SCpnt->device->lun & 7);
  967. SCpnt->scsi_done = done;
  968. SCpnt->SCp.phase = 1;
  969. scb = alloc_scbs(SCpnt->device->host, 1);
  970. scb->idlun = idlun;
  971. memcpy(scb->cdb, cdb, cdblen);
  972. scb->direc = 0x40; /* Disable direction check */
  973. scb->SCpnt = SCpnt; /* so we can find stuff later */
  974. SCpnt->host_scribble = (unchar *) scb;
  975. scb->host = host;
  976. nseg = scsi_sg_count(SCpnt);
  977. if (nseg > 1) {
  978. struct scatterlist *sg;
  979. unsigned i;
  980. dprintk("Using scatter/gather with %d elements.\n", nseg);
  981. sgb = scb->sgb;
  982. scb->op = 1;
  983. any2scsi(scb->dataptr, (int) sgb);
  984. any2scsi(scb->maxlen, nseg * sizeof(Sgb));
  985. scsi_for_each_sg(SCpnt, sg, nseg, i) {
  986. any2scsi(sgb[i].ptr, isa_page_to_bus(sg_page(sg)) + sg->offset);
  987. any2scsi(sgb[i].len, sg->length);
  988. }
  989. } else {
  990. scb->op = 0;
  991. if (nseg) {
  992. struct scatterlist *sg = scsi_sglist(SCpnt);
  993. any2scsi(scb->dataptr, isa_page_to_bus(sg_page(sg)) + sg->offset);
  994. }
  995. any2scsi(scb->maxlen, scsi_bufflen(SCpnt));
  996. }
  997. /* FIXME: drop lock and yield here ? */
  998. while (!mail_out(host, scb))
  999. cpu_relax(); /* keep trying */
  1000. return 0;
  1001. }
  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_unlock_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"