wd7000.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674
  1. /* $Id: wd7000.c,v 1.1.1.1 2007/06/12 07:27:09 eyryu Exp $
  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@redhat.com>
  150. *
  151. * Use dev_id for interrupts, kill __FUNCTION__ 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@redhat.com>
  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", __FUNCTION__);
  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", __FUNCTION__);
  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. 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. if (SCpnt->use_sg) {
  976. struct scatterlist *sg = (struct scatterlist *) SCpnt->request_buffer;
  977. unsigned i;
  978. if (SCpnt->device->host->sg_tablesize == SG_NONE) {
  979. panic("wd7000_queuecommand: scatter/gather not supported.\n");
  980. }
  981. dprintk("Using scatter/gather with %d elements.\n", SCpnt->use_sg);
  982. sgb = scb->sgb;
  983. scb->op = 1;
  984. any2scsi(scb->dataptr, (int) sgb);
  985. any2scsi(scb->maxlen, SCpnt->use_sg * sizeof(Sgb));
  986. for (i = 0; i < SCpnt->use_sg; i++) {
  987. any2scsi(sgb[i].ptr, isa_page_to_bus(sg[i].page) + sg[i].offset);
  988. any2scsi(sgb[i].len, sg[i].length);
  989. }
  990. } else {
  991. scb->op = 0;
  992. any2scsi(scb->dataptr, isa_virt_to_bus(SCpnt->request_buffer));
  993. any2scsi(scb->maxlen, SCpnt->request_bufflen);
  994. }
  995. /* FIXME: drop lock and yield here ? */
  996. while (!mail_out(host, scb))
  997. cpu_relax(); /* keep trying */
  998. return 0;
  999. }
  1000. static int wd7000_diagnostics(Adapter * host, int code)
  1001. {
  1002. static IcbDiag icb = { ICB_OP_DIAGNOSTICS };
  1003. static unchar buf[256];
  1004. unsigned long timeout;
  1005. icb.type = code;
  1006. any2scsi(icb.len, sizeof(buf));
  1007. any2scsi(icb.ptr, (int) &buf);
  1008. icb.phase = 1;
  1009. /*
  1010. * This routine is only called at init, so there should be OGMBs
  1011. * available. I'm assuming so here. If this is going to
  1012. * fail, I can just let the timeout catch the failure.
  1013. */
  1014. mail_out(host, (struct scb *) &icb);
  1015. timeout = jiffies + WAITnexttimeout; /* wait up to 2 seconds */
  1016. while (icb.phase && time_before(jiffies, timeout)) {
  1017. cpu_relax(); /* wait for completion */
  1018. barrier();
  1019. }
  1020. if (icb.phase) {
  1021. printk("wd7000_diagnostics: timed out.\n");
  1022. return (0);
  1023. }
  1024. if (make_code(icb.vue | (icb.status << 8), 0)) {
  1025. printk("wd7000_diagnostics: failed (0x%02x,0x%02x)\n", icb.vue, icb.status);
  1026. return (0);
  1027. }
  1028. return (1);
  1029. }
  1030. static int wd7000_adapter_reset(Adapter * host)
  1031. {
  1032. InitCmd init_cmd = {
  1033. INITIALIZATION,
  1034. 7,
  1035. host->bus_on,
  1036. host->bus_off,
  1037. 0,
  1038. {0, 0, 0},
  1039. OGMB_CNT,
  1040. ICMB_CNT
  1041. };
  1042. int diag;
  1043. /*
  1044. * Reset the adapter - only. The SCSI bus was initialized at power-up,
  1045. * and we need to do this just so we control the mailboxes, etc.
  1046. */
  1047. outb(ASC_RES, host->iobase + ASC_CONTROL);
  1048. udelay(40); /* reset pulse: this is 40us, only need 25us */
  1049. outb(0, host->iobase + ASC_CONTROL);
  1050. host->control = 0; /* this must always shadow ASC_CONTROL */
  1051. if (WAIT(host->iobase + ASC_STAT, ASC_STATMASK, CMD_RDY, 0)) {
  1052. printk(KERN_ERR "wd7000_init: WAIT timed out.\n");
  1053. return -1; /* -1 = not ok */
  1054. }
  1055. if ((diag = inb(host->iobase + ASC_INTR_STAT)) != 1) {
  1056. printk("wd7000_init: ");
  1057. switch (diag) {
  1058. case 2:
  1059. printk(KERN_ERR "RAM failure.\n");
  1060. break;
  1061. case 3:
  1062. printk(KERN_ERR "FIFO R/W failed\n");
  1063. break;
  1064. case 4:
  1065. printk(KERN_ERR "SBIC register R/W failed\n");
  1066. break;
  1067. case 5:
  1068. printk(KERN_ERR "Initialization D-FF failed.\n");
  1069. break;
  1070. case 6:
  1071. printk(KERN_ERR "Host IRQ D-FF failed.\n");
  1072. break;
  1073. case 7:
  1074. printk(KERN_ERR "ROM checksum error.\n");
  1075. break;
  1076. default:
  1077. printk(KERN_ERR "diagnostic code 0x%02Xh received.\n", diag);
  1078. }
  1079. return -1;
  1080. }
  1081. /* Clear mailboxes */
  1082. memset(&(host->mb), 0, sizeof(host->mb));
  1083. /* Execute init command */
  1084. any2scsi((unchar *) & (init_cmd.mailboxes), (int) &(host->mb));
  1085. if (!command_out(host, (unchar *) & init_cmd, sizeof(init_cmd))) {
  1086. printk(KERN_ERR "wd7000_adapter_reset: adapter initialization failed.\n");
  1087. return -1;
  1088. }
  1089. if (WAIT(host->iobase + ASC_STAT, ASC_STATMASK, ASC_INIT, 0)) {
  1090. printk("wd7000_adapter_reset: WAIT timed out.\n");
  1091. return -1;
  1092. }
  1093. return 0;
  1094. }
  1095. static int wd7000_init(Adapter * host)
  1096. {
  1097. if (wd7000_adapter_reset(host) == -1)
  1098. return 0;
  1099. if (request_irq(host->irq, wd7000_intr, IRQF_DISABLED, "wd7000", host)) {
  1100. printk("wd7000_init: can't get IRQ %d.\n", host->irq);
  1101. return (0);
  1102. }
  1103. if (request_dma(host->dma, "wd7000")) {
  1104. printk("wd7000_init: can't get DMA channel %d.\n", host->dma);
  1105. free_irq(host->irq, host);
  1106. return (0);
  1107. }
  1108. wd7000_enable_dma(host);
  1109. wd7000_enable_intr(host);
  1110. if (!wd7000_diagnostics(host, ICB_DIAG_FULL)) {
  1111. free_dma(host->dma);
  1112. free_irq(host->irq, NULL);
  1113. return (0);
  1114. }
  1115. return (1);
  1116. }
  1117. static void wd7000_revision(Adapter * host)
  1118. {
  1119. static IcbRevLvl icb = { ICB_OP_GET_REVISION };
  1120. icb.phase = 1;
  1121. /*
  1122. * Like diagnostics, this is only done at init time, in fact, from
  1123. * wd7000_detect, so there should be OGMBs available. If it fails,
  1124. * the only damage will be that the revision will show up as 0.0,
  1125. * which in turn means that scatter/gather will be disabled.
  1126. */
  1127. mail_out(host, (struct scb *) &icb);
  1128. while (icb.phase) {
  1129. cpu_relax(); /* wait for completion */
  1130. barrier();
  1131. }
  1132. host->rev1 = icb.primary;
  1133. host->rev2 = icb.secondary;
  1134. }
  1135. #undef SPRINTF
  1136. #define SPRINTF(args...) { if (pos < (buffer + length)) pos += sprintf (pos, ## args); }
  1137. static int wd7000_set_info(char *buffer, int length, struct Scsi_Host *host)
  1138. {
  1139. dprintk("Buffer = <%.*s>, length = %d\n", length, buffer, length);
  1140. /*
  1141. * Currently this is a no-op
  1142. */
  1143. dprintk("Sorry, this function is currently out of order...\n");
  1144. return (length);
  1145. }
  1146. static int wd7000_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset, int length, int inout)
  1147. {
  1148. Adapter *adapter = (Adapter *)host->hostdata;
  1149. unsigned long flags;
  1150. char *pos = buffer;
  1151. #ifdef WD7000_DEBUG
  1152. Mailbox *ogmbs, *icmbs;
  1153. short count;
  1154. #endif
  1155. /*
  1156. * Has data been written to the file ?
  1157. */
  1158. if (inout)
  1159. return (wd7000_set_info(buffer, length, host));
  1160. spin_lock_irqsave(host->host_lock, flags);
  1161. SPRINTF("Host scsi%d: Western Digital WD-7000 (rev %d.%d)\n", host->host_no, adapter->rev1, adapter->rev2);
  1162. SPRINTF(" IO base: 0x%x\n", adapter->iobase);
  1163. SPRINTF(" IRQ: %d\n", adapter->irq);
  1164. SPRINTF(" DMA channel: %d\n", adapter->dma);
  1165. SPRINTF(" Interrupts: %d\n", adapter->int_counter);
  1166. SPRINTF(" BUS_ON time: %d nanoseconds\n", adapter->bus_on * 125);
  1167. SPRINTF(" BUS_OFF time: %d nanoseconds\n", adapter->bus_off * 125);
  1168. #ifdef WD7000_DEBUG
  1169. ogmbs = adapter->mb.ogmb;
  1170. icmbs = adapter->mb.icmb;
  1171. SPRINTF("\nControl port value: 0x%x\n", adapter->control);
  1172. SPRINTF("Incoming mailbox:\n");
  1173. SPRINTF(" size: %d\n", ICMB_CNT);
  1174. SPRINTF(" queued messages: ");
  1175. for (i = count = 0; i < ICMB_CNT; i++)
  1176. if (icmbs[i].status) {
  1177. count++;
  1178. SPRINTF("0x%x ", i);
  1179. }
  1180. SPRINTF(count ? "\n" : "none\n");
  1181. SPRINTF("Outgoing mailbox:\n");
  1182. SPRINTF(" size: %d\n", OGMB_CNT);
  1183. SPRINTF(" next message: 0x%x\n", adapter->next_ogmb);
  1184. SPRINTF(" queued messages: ");
  1185. for (i = count = 0; i < OGMB_CNT; i++)
  1186. if (ogmbs[i].status) {
  1187. count++;
  1188. SPRINTF("0x%x ", i);
  1189. }
  1190. SPRINTF(count ? "\n" : "none\n");
  1191. #endif
  1192. spin_unlock_irqrestore(host->host_lock, flags);
  1193. /*
  1194. * Calculate start of next buffer, and return value.
  1195. */
  1196. *start = buffer + offset;
  1197. if ((pos - buffer) < offset)
  1198. return (0);
  1199. else if ((pos - buffer - offset) < length)
  1200. return (pos - buffer - offset);
  1201. else
  1202. return (length);
  1203. }
  1204. /*
  1205. * Returns the number of adapters this driver is supporting.
  1206. *
  1207. * The source for hosts.c says to wait to call scsi_register until 100%
  1208. * sure about an adapter. We need to do it a little sooner here; we
  1209. * need the storage set up by scsi_register before wd7000_init, and
  1210. * changing the location of an Adapter structure is more trouble than
  1211. * calling scsi_unregister.
  1212. *
  1213. */
  1214. static __init int wd7000_detect(struct scsi_host_template *tpnt)
  1215. {
  1216. short present = 0, biosaddr_ptr, sig_ptr, i, pass;
  1217. short biosptr[NUM_CONFIGS];
  1218. unsigned iobase;
  1219. Adapter *host = NULL;
  1220. struct Scsi_Host *sh;
  1221. int unit = 0;
  1222. dprintk("wd7000_detect: started\n");
  1223. #ifdef MODULE
  1224. if (wd7000)
  1225. wd7000_setup(wd7000);
  1226. #endif
  1227. for (i = 0; i < UNITS; wd7000_host[i++] = NULL);
  1228. for (i = 0; i < NUM_CONFIGS; biosptr[i++] = -1);
  1229. tpnt->proc_name = "wd7000";
  1230. tpnt->proc_info = &wd7000_proc_info;
  1231. /*
  1232. * Set up SCB free list, which is shared by all adapters
  1233. */
  1234. init_scbs();
  1235. for (pass = 0; pass < NUM_CONFIGS; pass++) {
  1236. /*
  1237. * First, search for BIOS SIGNATURE...
  1238. */
  1239. for (biosaddr_ptr = 0; biosaddr_ptr < NUM_ADDRS; biosaddr_ptr++)
  1240. for (sig_ptr = 0; sig_ptr < NUM_SIGNATURES; sig_ptr++) {
  1241. for (i = 0; i < pass; i++)
  1242. if (biosptr[i] == biosaddr_ptr)
  1243. break;
  1244. if (i == pass) {
  1245. void __iomem *biosaddr = ioremap(wd7000_biosaddr[biosaddr_ptr] + signatures[sig_ptr].ofs,
  1246. signatures[sig_ptr].len);
  1247. short bios_match = 1;
  1248. if (biosaddr)
  1249. bios_match = check_signature(biosaddr, signatures[sig_ptr].sig, signatures[sig_ptr].len);
  1250. iounmap(biosaddr);
  1251. if (bios_match)
  1252. goto bios_matched;
  1253. }
  1254. }
  1255. bios_matched:
  1256. /*
  1257. * BIOS SIGNATURE has been found.
  1258. */
  1259. #ifdef WD7000_DEBUG
  1260. dprintk("wd7000_detect: pass %d\n", pass + 1);
  1261. if (biosaddr_ptr == NUM_ADDRS)
  1262. dprintk("WD-7000 SST BIOS not detected...\n");
  1263. else
  1264. dprintk("WD-7000 SST BIOS detected at 0x%lx: checking...\n", wd7000_biosaddr[biosaddr_ptr]);
  1265. #endif
  1266. if (configs[pass].irq < 0)
  1267. continue;
  1268. if (unit == UNITS)
  1269. continue;
  1270. iobase = configs[pass].iobase;
  1271. dprintk("wd7000_detect: check IO 0x%x region...\n", iobase);
  1272. if (request_region(iobase, 4, "wd7000")) {
  1273. dprintk("wd7000_detect: ASC reset (IO 0x%x) ...", iobase);
  1274. /*
  1275. * ASC reset...
  1276. */
  1277. outb(ASC_RES, iobase + ASC_CONTROL);
  1278. msleep(10);
  1279. outb(0, iobase + ASC_CONTROL);
  1280. if (WAIT(iobase + ASC_STAT, ASC_STATMASK, CMD_RDY, 0)) {
  1281. dprintk("failed!\n");
  1282. goto err_release;
  1283. } else
  1284. dprintk("ok!\n");
  1285. if (inb(iobase + ASC_INTR_STAT) == 1) {
  1286. /*
  1287. * We register here, to get a pointer to the extra space,
  1288. * which we'll use as the Adapter structure (host) for
  1289. * this adapter. It is located just after the registered
  1290. * Scsi_Host structure (sh), and is located by the empty
  1291. * array hostdata.
  1292. */
  1293. sh = scsi_register(tpnt, sizeof(Adapter));
  1294. if (sh == NULL)
  1295. goto err_release;
  1296. host = (Adapter *) sh->hostdata;
  1297. dprintk("wd7000_detect: adapter allocated at 0x%x\n", (int) host);
  1298. memset(host, 0, sizeof(Adapter));
  1299. host->irq = configs[pass].irq;
  1300. host->dma = configs[pass].dma;
  1301. host->iobase = iobase;
  1302. host->int_counter = 0;
  1303. host->bus_on = configs[pass].bus_on;
  1304. host->bus_off = configs[pass].bus_off;
  1305. host->sh = wd7000_host[unit] = sh;
  1306. unit++;
  1307. dprintk("wd7000_detect: Trying init WD-7000 card at IO " "0x%x, IRQ %d, DMA %d...\n", host->iobase, host->irq, host->dma);
  1308. if (!wd7000_init(host)) /* Initialization failed */
  1309. goto err_unregister;
  1310. /*
  1311. * OK from here - we'll use this adapter/configuration.
  1312. */
  1313. wd7000_revision(host); /* important for scatter/gather */
  1314. /*
  1315. * For boards before rev 6.0, scatter/gather isn't supported.
  1316. */
  1317. if (host->rev1 < 6)
  1318. sh->sg_tablesize = SG_NONE;
  1319. present++; /* count it */
  1320. if (biosaddr_ptr != NUM_ADDRS)
  1321. biosptr[pass] = biosaddr_ptr;
  1322. printk(KERN_INFO "Western Digital WD-7000 (rev %d.%d) ", host->rev1, host->rev2);
  1323. printk("using IO 0x%x, IRQ %d, DMA %d.\n", host->iobase, host->irq, host->dma);
  1324. printk(" BUS_ON time: %dns, BUS_OFF time: %dns\n", host->bus_on * 125, host->bus_off * 125);
  1325. }
  1326. } else
  1327. dprintk("wd7000_detect: IO 0x%x region already allocated!\n", iobase);
  1328. continue;
  1329. err_unregister:
  1330. scsi_unregister(sh);
  1331. err_release:
  1332. release_region(iobase, 4);
  1333. }
  1334. if (!present)
  1335. printk("Failed initialization of WD-7000 SCSI card!\n");
  1336. return (present);
  1337. }
  1338. static int wd7000_release(struct Scsi_Host *shost)
  1339. {
  1340. if (shost->irq)
  1341. free_irq(shost->irq, NULL);
  1342. if (shost->io_port && shost->n_io_port)
  1343. release_region(shost->io_port, shost->n_io_port);
  1344. scsi_unregister(shost);
  1345. return 0;
  1346. }
  1347. #if 0
  1348. /*
  1349. * I have absolutely NO idea how to do an abort with the WD7000...
  1350. */
  1351. static int wd7000_abort(Scsi_Cmnd * SCpnt)
  1352. {
  1353. Adapter *host = (Adapter *) SCpnt->device->host->hostdata;
  1354. if (inb(host->iobase + ASC_STAT) & INT_IM) {
  1355. printk("wd7000_abort: lost interrupt\n");
  1356. wd7000_intr_handle(host->irq, NULL, NULL);
  1357. return FAILED;
  1358. }
  1359. return FAILED;
  1360. }
  1361. #endif
  1362. /*
  1363. * Last resort. Reinitialize the board.
  1364. */
  1365. static int wd7000_host_reset(struct scsi_cmnd *SCpnt)
  1366. {
  1367. Adapter *host = (Adapter *) SCpnt->device->host->hostdata;
  1368. spin_unlock_irq(SCpnt->device->host->host_lock);
  1369. if (wd7000_adapter_reset(host) < 0) {
  1370. spin_unlock_irq(SCpnt->device->host->host_lock);
  1371. return FAILED;
  1372. }
  1373. wd7000_enable_intr(host);
  1374. spin_unlock_irq(SCpnt->device->host->host_lock);
  1375. return SUCCESS;
  1376. }
  1377. /*
  1378. * This was borrowed directly from aha1542.c. (Zaga)
  1379. */
  1380. static int wd7000_biosparam(struct scsi_device *sdev,
  1381. struct block_device *bdev, sector_t capacity, int *ip)
  1382. {
  1383. char b[BDEVNAME_SIZE];
  1384. dprintk("wd7000_biosparam: dev=%s, size=%d, ",
  1385. bdevname(bdev, b), capacity);
  1386. (void)b; /* unused var warning? */
  1387. /*
  1388. * try default translation
  1389. */
  1390. ip[0] = 64;
  1391. ip[1] = 32;
  1392. ip[2] = capacity >> 11;
  1393. /*
  1394. * for disks >1GB do some guessing
  1395. */
  1396. if (ip[2] >= 1024) {
  1397. int info[3];
  1398. /*
  1399. * try to figure out the geometry from the partition table
  1400. */
  1401. if ((scsicam_bios_param(bdev, capacity, info) < 0) || !(((info[0] == 64) && (info[1] == 32)) || ((info[0] == 255) && (info[1] == 63)))) {
  1402. printk("wd7000_biosparam: unable to verify geometry for disk with >1GB.\n" " using extended translation.\n");
  1403. ip[0] = 255;
  1404. ip[1] = 63;
  1405. ip[2] = (unsigned long) capacity / (255 * 63);
  1406. } else {
  1407. ip[0] = info[0];
  1408. ip[1] = info[1];
  1409. ip[2] = info[2];
  1410. if (info[0] == 255)
  1411. printk(KERN_INFO "%s: current partition table is " "using extended translation.\n", __FUNCTION__);
  1412. }
  1413. }
  1414. dprintk("bios geometry: head=%d, sec=%d, cyl=%d\n", ip[0], ip[1], ip[2]);
  1415. dprintk("WARNING: check, if the bios geometry is correct.\n");
  1416. return (0);
  1417. }
  1418. MODULE_AUTHOR("Thomas Wuensche, John Boyd, Miroslav Zagorac");
  1419. MODULE_DESCRIPTION("Driver for the WD7000 series ISA controllers");
  1420. MODULE_LICENSE("GPL");
  1421. static struct scsi_host_template driver_template = {
  1422. .proc_name = "wd7000",
  1423. .proc_info = wd7000_proc_info,
  1424. .name = "Western Digital WD-7000",
  1425. .detect = wd7000_detect,
  1426. .release = wd7000_release,
  1427. .queuecommand = wd7000_queuecommand,
  1428. .eh_host_reset_handler = wd7000_host_reset,
  1429. .bios_param = wd7000_biosparam,
  1430. .can_queue = WD7000_Q,
  1431. .this_id = 7,
  1432. .sg_tablesize = WD7000_SG,
  1433. .cmd_per_lun = 1,
  1434. .unchecked_isa_dma = 1,
  1435. .use_clustering = ENABLE_CLUSTERING,
  1436. };
  1437. #include "scsi_module.c"