wd33c93.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076
  1. /*
  2. * Copyright (c) 1996 John Shifflett, GeoLog Consulting
  3. * john@geolog.com
  4. * jshiffle@netcom.com
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. /*
  17. * Drew Eckhardt's excellent 'Generic NCR5380' sources from Linux-PC
  18. * provided much of the inspiration and some of the code for this
  19. * driver. Everything I know about Amiga DMA was gleaned from careful
  20. * reading of Hamish Mcdonald's original wd33c93 driver; in fact, I
  21. * borrowed shamelessly from all over that source. Thanks Hamish!
  22. *
  23. * _This_ driver is (I feel) an improvement over the old one in
  24. * several respects:
  25. *
  26. * - Target Disconnection/Reconnection is now supported. Any
  27. * system with more than one device active on the SCSI bus
  28. * will benefit from this. The driver defaults to what I
  29. * call 'adaptive disconnect' - meaning that each command
  30. * is evaluated individually as to whether or not it should
  31. * be run with the option to disconnect/reselect (if the
  32. * device chooses), or as a "SCSI-bus-hog".
  33. *
  34. * - Synchronous data transfers are now supported. Because of
  35. * a few devices that choke after telling the driver that
  36. * they can do sync transfers, we don't automatically use
  37. * this faster protocol - it can be enabled via the command-
  38. * line on a device-by-device basis.
  39. *
  40. * - Runtime operating parameters can now be specified through
  41. * the 'amiboot' or the 'insmod' command line. For amiboot do:
  42. * "amiboot [usual stuff] wd33c93=blah,blah,blah"
  43. * The defaults should be good for most people. See the comment
  44. * for 'setup_strings' below for more details.
  45. *
  46. * - The old driver relied exclusively on what the Western Digital
  47. * docs call "Combination Level 2 Commands", which are a great
  48. * idea in that the CPU is relieved of a lot of interrupt
  49. * overhead. However, by accepting a certain (user-settable)
  50. * amount of additional interrupts, this driver achieves
  51. * better control over the SCSI bus, and data transfers are
  52. * almost as fast while being much easier to define, track,
  53. * and debug.
  54. *
  55. *
  56. * TODO:
  57. * more speed. linked commands.
  58. *
  59. *
  60. * People with bug reports, wish-lists, complaints, comments,
  61. * or improvements are asked to pah-leeez email me (John Shifflett)
  62. * at john@geolog.com or jshiffle@netcom.com! I'm anxious to get
  63. * this thing into as good a shape as possible, and I'm positive
  64. * there are lots of lurking bugs and "Stupid Places".
  65. *
  66. * Updates:
  67. *
  68. * Added support for pre -A chips, which don't have advanced features
  69. * and will generate CSR_RESEL rather than CSR_RESEL_AM.
  70. * Richard Hirst <richard@sleepie.demon.co.uk> August 2000
  71. */
  72. #include <linux/config.h>
  73. #include <linux/module.h>
  74. #include <linux/sched.h>
  75. #include <linux/string.h>
  76. #include <linux/delay.h>
  77. #include <linux/init.h>
  78. #include <linux/blkdev.h>
  79. #include <asm/irq.h>
  80. #include <scsi/scsi.h>
  81. #include <scsi/scsi_cmnd.h>
  82. #include <scsi/scsi_device.h>
  83. #include <scsi/scsi_host.h>
  84. #include "wd33c93.h"
  85. #define WD33C93_VERSION "1.26"
  86. #define WD33C93_DATE "22/Feb/2003"
  87. MODULE_AUTHOR("John Shifflett");
  88. MODULE_DESCRIPTION("Generic WD33C93 SCSI driver");
  89. MODULE_LICENSE("GPL");
  90. /*
  91. * 'setup_strings' is a single string used to pass operating parameters and
  92. * settings from the kernel/module command-line to the driver. 'setup_args[]'
  93. * is an array of strings that define the compile-time default values for
  94. * these settings. If Linux boots with an amiboot or insmod command-line,
  95. * those settings are combined with 'setup_args[]'. Note that amiboot
  96. * command-lines are prefixed with "wd33c93=" while insmod uses a
  97. * "setup_strings=" prefix. The driver recognizes the following keywords
  98. * (lower case required) and arguments:
  99. *
  100. * - nosync:bitmask -bitmask is a byte where the 1st 7 bits correspond with
  101. * the 7 possible SCSI devices. Set a bit to negotiate for
  102. * asynchronous transfers on that device. To maintain
  103. * backwards compatibility, a command-line such as
  104. * "wd33c93=255" will be automatically translated to
  105. * "wd33c93=nosync:0xff".
  106. * - nodma:x -x = 1 to disable DMA, x = 0 to enable it. Argument is
  107. * optional - if not present, same as "nodma:1".
  108. * - period:ns -ns is the minimum # of nanoseconds in a SCSI data transfer
  109. * period. Default is 500; acceptable values are 250 - 1000.
  110. * - disconnect:x -x = 0 to never allow disconnects, 2 to always allow them.
  111. * x = 1 does 'adaptive' disconnects, which is the default
  112. * and generally the best choice.
  113. * - debug:x -If 'DEBUGGING_ON' is defined, x is a bit mask that causes
  114. * various types of debug output to printed - see the DB_xxx
  115. * defines in wd33c93.h
  116. * - clock:x -x = clock input in MHz for WD33c93 chip. Normal values
  117. * would be from 8 through 20. Default is 8.
  118. * - next -No argument. Used to separate blocks of keywords when
  119. * there's more than one host adapter in the system.
  120. *
  121. * Syntax Notes:
  122. * - Numeric arguments can be decimal or the '0x' form of hex notation. There
  123. * _must_ be a colon between a keyword and its numeric argument, with no
  124. * spaces.
  125. * - Keywords are separated by commas, no spaces, in the standard kernel
  126. * command-line manner.
  127. * - A keyword in the 'nth' comma-separated command-line member will overwrite
  128. * the 'nth' element of setup_args[]. A blank command-line member (in
  129. * other words, a comma with no preceding keyword) will _not_ overwrite
  130. * the corresponding setup_args[] element.
  131. * - If a keyword is used more than once, the first one applies to the first
  132. * SCSI host found, the second to the second card, etc, unless the 'next'
  133. * keyword is used to change the order.
  134. *
  135. * Some amiboot examples (for insmod, use 'setup_strings' instead of 'wd33c93'):
  136. * - wd33c93=nosync:255
  137. * - wd33c93=nodma
  138. * - wd33c93=nodma:1
  139. * - wd33c93=disconnect:2,nosync:0x08,period:250
  140. * - wd33c93=debug:0x1c
  141. */
  142. /* Normally, no defaults are specified */
  143. static char *setup_args[] = { "", "", "", "", "", "", "", "", "" };
  144. static char *setup_strings;
  145. module_param(setup_strings, charp, 0);
  146. static void wd33c93_execute(struct Scsi_Host *instance);
  147. #ifdef CONFIG_WD33C93_PIO
  148. static inline uchar
  149. read_wd33c93(const wd33c93_regs regs, uchar reg_num)
  150. {
  151. uchar data;
  152. outb(reg_num, regs.SASR);
  153. data = inb(regs.SCMD);
  154. return data;
  155. }
  156. static inline unsigned long
  157. read_wd33c93_count(const wd33c93_regs regs)
  158. {
  159. unsigned long value;
  160. outb(WD_TRANSFER_COUNT_MSB, regs.SASR);
  161. value = inb(regs.SCMD) << 16;
  162. value |= inb(regs.SCMD) << 8;
  163. value |= inb(regs.SCMD);
  164. return value;
  165. }
  166. static inline uchar
  167. read_aux_stat(const wd33c93_regs regs)
  168. {
  169. return inb(regs.SASR);
  170. }
  171. static inline void
  172. write_wd33c93(const wd33c93_regs regs, uchar reg_num, uchar value)
  173. {
  174. outb(reg_num, regs.SASR);
  175. outb(value, regs.SCMD);
  176. }
  177. static inline void
  178. write_wd33c93_count(const wd33c93_regs regs, unsigned long value)
  179. {
  180. outb(WD_TRANSFER_COUNT_MSB, regs.SASR);
  181. outb((value >> 16) & 0xff, regs.SCMD);
  182. outb((value >> 8) & 0xff, regs.SCMD);
  183. outb( value & 0xff, regs.SCMD);
  184. }
  185. #define write_wd33c93_cmd(regs, cmd) \
  186. write_wd33c93((regs), WD_COMMAND, (cmd))
  187. static inline void
  188. write_wd33c93_cdb(const wd33c93_regs regs, uint len, uchar cmnd[])
  189. {
  190. int i;
  191. outb(WD_CDB_1, regs.SASR);
  192. for (i=0; i<len; i++)
  193. outb(cmnd[i], regs.SCMD);
  194. }
  195. #else /* CONFIG_WD33C93_PIO */
  196. static inline uchar
  197. read_wd33c93(const wd33c93_regs regs, uchar reg_num)
  198. {
  199. *regs.SASR = reg_num;
  200. mb();
  201. return (*regs.SCMD);
  202. }
  203. static unsigned long
  204. read_wd33c93_count(const wd33c93_regs regs)
  205. {
  206. unsigned long value;
  207. *regs.SASR = WD_TRANSFER_COUNT_MSB;
  208. mb();
  209. value = *regs.SCMD << 16;
  210. value |= *regs.SCMD << 8;
  211. value |= *regs.SCMD;
  212. mb();
  213. return value;
  214. }
  215. static inline uchar
  216. read_aux_stat(const wd33c93_regs regs)
  217. {
  218. return *regs.SASR;
  219. }
  220. static inline void
  221. write_wd33c93(const wd33c93_regs regs, uchar reg_num, uchar value)
  222. {
  223. *regs.SASR = reg_num;
  224. mb();
  225. *regs.SCMD = value;
  226. mb();
  227. }
  228. static void
  229. write_wd33c93_count(const wd33c93_regs regs, unsigned long value)
  230. {
  231. *regs.SASR = WD_TRANSFER_COUNT_MSB;
  232. mb();
  233. *regs.SCMD = value >> 16;
  234. *regs.SCMD = value >> 8;
  235. *regs.SCMD = value;
  236. mb();
  237. }
  238. static inline void
  239. write_wd33c93_cmd(const wd33c93_regs regs, uchar cmd)
  240. {
  241. *regs.SASR = WD_COMMAND;
  242. mb();
  243. *regs.SCMD = cmd;
  244. mb();
  245. }
  246. static inline void
  247. write_wd33c93_cdb(const wd33c93_regs regs, uint len, uchar cmnd[])
  248. {
  249. int i;
  250. *regs.SASR = WD_CDB_1;
  251. for (i = 0; i < len; i++)
  252. *regs.SCMD = cmnd[i];
  253. }
  254. #endif /* CONFIG_WD33C93_PIO */
  255. static inline uchar
  256. read_1_byte(const wd33c93_regs regs)
  257. {
  258. uchar asr;
  259. uchar x = 0;
  260. write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
  261. write_wd33c93_cmd(regs, WD_CMD_TRANS_INFO | 0x80);
  262. do {
  263. asr = read_aux_stat(regs);
  264. if (asr & ASR_DBR)
  265. x = read_wd33c93(regs, WD_DATA);
  266. } while (!(asr & ASR_INT));
  267. return x;
  268. }
  269. static struct sx_period sx_table[] = {
  270. {1, 0x20},
  271. {252, 0x20},
  272. {376, 0x30},
  273. {500, 0x40},
  274. {624, 0x50},
  275. {752, 0x60},
  276. {876, 0x70},
  277. {1000, 0x00},
  278. {0, 0}
  279. };
  280. static int
  281. round_period(unsigned int period)
  282. {
  283. int x;
  284. for (x = 1; sx_table[x].period_ns; x++) {
  285. if ((period <= sx_table[x - 0].period_ns) &&
  286. (period > sx_table[x - 1].period_ns)) {
  287. return x;
  288. }
  289. }
  290. return 7;
  291. }
  292. static uchar
  293. calc_sync_xfer(unsigned int period, unsigned int offset)
  294. {
  295. uchar result;
  296. period *= 4; /* convert SDTR code to ns */
  297. result = sx_table[round_period(period)].reg_value;
  298. result |= (offset < OPTIMUM_SX_OFF) ? offset : OPTIMUM_SX_OFF;
  299. return result;
  300. }
  301. int
  302. wd33c93_queuecommand(struct scsi_cmnd *cmd,
  303. void (*done)(struct scsi_cmnd *))
  304. {
  305. struct WD33C93_hostdata *hostdata;
  306. struct scsi_cmnd *tmp;
  307. hostdata = (struct WD33C93_hostdata *) cmd->device->host->hostdata;
  308. DB(DB_QUEUE_COMMAND,
  309. printk("Q-%d-%02x-%ld( ", cmd->device->id, cmd->cmnd[0], cmd->pid))
  310. /* Set up a few fields in the scsi_cmnd structure for our own use:
  311. * - host_scribble is the pointer to the next cmd in the input queue
  312. * - scsi_done points to the routine we call when a cmd is finished
  313. * - result is what you'd expect
  314. */
  315. cmd->host_scribble = NULL;
  316. cmd->scsi_done = done;
  317. cmd->result = 0;
  318. /* We use the Scsi_Pointer structure that's included with each command
  319. * as a scratchpad (as it's intended to be used!). The handy thing about
  320. * the SCp.xxx fields is that they're always associated with a given
  321. * cmd, and are preserved across disconnect-reselect. This means we
  322. * can pretty much ignore SAVE_POINTERS and RESTORE_POINTERS messages
  323. * if we keep all the critical pointers and counters in SCp:
  324. * - SCp.ptr is the pointer into the RAM buffer
  325. * - SCp.this_residual is the size of that buffer
  326. * - SCp.buffer points to the current scatter-gather buffer
  327. * - SCp.buffers_residual tells us how many S.G. buffers there are
  328. * - SCp.have_data_in is not used
  329. * - SCp.sent_command is not used
  330. * - SCp.phase records this command's SRCID_ER bit setting
  331. */
  332. if (cmd->use_sg) {
  333. cmd->SCp.buffer = (struct scatterlist *) cmd->buffer;
  334. cmd->SCp.buffers_residual = cmd->use_sg - 1;
  335. cmd->SCp.ptr = page_address(cmd->SCp.buffer->page) +
  336. cmd->SCp.buffer->offset;
  337. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  338. } else {
  339. cmd->SCp.buffer = NULL;
  340. cmd->SCp.buffers_residual = 0;
  341. cmd->SCp.ptr = (char *) cmd->request_buffer;
  342. cmd->SCp.this_residual = cmd->request_bufflen;
  343. }
  344. /* WD docs state that at the conclusion of a "LEVEL2" command, the
  345. * status byte can be retrieved from the LUN register. Apparently,
  346. * this is the case only for *uninterrupted* LEVEL2 commands! If
  347. * there are any unexpected phases entered, even if they are 100%
  348. * legal (different devices may choose to do things differently),
  349. * the LEVEL2 command sequence is exited. This often occurs prior
  350. * to receiving the status byte, in which case the driver does a
  351. * status phase interrupt and gets the status byte on its own.
  352. * While such a command can then be "resumed" (ie restarted to
  353. * finish up as a LEVEL2 command), the LUN register will NOT be
  354. * a valid status byte at the command's conclusion, and we must
  355. * use the byte obtained during the earlier interrupt. Here, we
  356. * preset SCp.Status to an illegal value (0xff) so that when
  357. * this command finally completes, we can tell where the actual
  358. * status byte is stored.
  359. */
  360. cmd->SCp.Status = ILLEGAL_STATUS_BYTE;
  361. /*
  362. * Add the cmd to the end of 'input_Q'. Note that REQUEST SENSE
  363. * commands are added to the head of the queue so that the desired
  364. * sense data is not lost before REQUEST_SENSE executes.
  365. */
  366. spin_lock_irq(&hostdata->lock);
  367. if (!(hostdata->input_Q) || (cmd->cmnd[0] == REQUEST_SENSE)) {
  368. cmd->host_scribble = (uchar *) hostdata->input_Q;
  369. hostdata->input_Q = cmd;
  370. } else { /* find the end of the queue */
  371. for (tmp = (struct scsi_cmnd *) hostdata->input_Q;
  372. tmp->host_scribble;
  373. tmp = (struct scsi_cmnd *) tmp->host_scribble) ;
  374. tmp->host_scribble = (uchar *) cmd;
  375. }
  376. /* We know that there's at least one command in 'input_Q' now.
  377. * Go see if any of them are runnable!
  378. */
  379. wd33c93_execute(cmd->device->host);
  380. DB(DB_QUEUE_COMMAND, printk(")Q-%ld ", cmd->pid))
  381. spin_unlock_irq(&hostdata->lock);
  382. return 0;
  383. }
  384. /*
  385. * This routine attempts to start a scsi command. If the host_card is
  386. * already connected, we give up immediately. Otherwise, look through
  387. * the input_Q, using the first command we find that's intended
  388. * for a currently non-busy target/lun.
  389. *
  390. * wd33c93_execute() is always called with interrupts disabled or from
  391. * the wd33c93_intr itself, which means that a wd33c93 interrupt
  392. * cannot occur while we are in here.
  393. */
  394. static void
  395. wd33c93_execute(struct Scsi_Host *instance)
  396. {
  397. struct WD33C93_hostdata *hostdata =
  398. (struct WD33C93_hostdata *) instance->hostdata;
  399. const wd33c93_regs regs = hostdata->regs;
  400. struct scsi_cmnd *cmd, *prev;
  401. DB(DB_EXECUTE, printk("EX("))
  402. if (hostdata->selecting || hostdata->connected) {
  403. DB(DB_EXECUTE, printk(")EX-0 "))
  404. return;
  405. }
  406. /*
  407. * Search through the input_Q for a command destined
  408. * for an idle target/lun.
  409. */
  410. cmd = (struct scsi_cmnd *) hostdata->input_Q;
  411. prev = NULL;
  412. while (cmd) {
  413. if (!(hostdata->busy[cmd->device->id] & (1 << cmd->device->lun)))
  414. break;
  415. prev = cmd;
  416. cmd = (struct scsi_cmnd *) cmd->host_scribble;
  417. }
  418. /* quit if queue empty or all possible targets are busy */
  419. if (!cmd) {
  420. DB(DB_EXECUTE, printk(")EX-1 "))
  421. return;
  422. }
  423. /* remove command from queue */
  424. if (prev)
  425. prev->host_scribble = cmd->host_scribble;
  426. else
  427. hostdata->input_Q = (struct scsi_cmnd *) cmd->host_scribble;
  428. #ifdef PROC_STATISTICS
  429. hostdata->cmd_cnt[cmd->device->id]++;
  430. #endif
  431. /*
  432. * Start the selection process
  433. */
  434. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  435. write_wd33c93(regs, WD_DESTINATION_ID, cmd->device->id);
  436. else
  437. write_wd33c93(regs, WD_DESTINATION_ID, cmd->device->id | DSTID_DPD);
  438. /* Now we need to figure out whether or not this command is a good
  439. * candidate for disconnect/reselect. We guess to the best of our
  440. * ability, based on a set of hierarchical rules. When several
  441. * devices are operating simultaneously, disconnects are usually
  442. * an advantage. In a single device system, or if only 1 device
  443. * is being accessed, transfers usually go faster if disconnects
  444. * are not allowed:
  445. *
  446. * + Commands should NEVER disconnect if hostdata->disconnect =
  447. * DIS_NEVER (this holds for tape drives also), and ALWAYS
  448. * disconnect if hostdata->disconnect = DIS_ALWAYS.
  449. * + Tape drive commands should always be allowed to disconnect.
  450. * + Disconnect should be allowed if disconnected_Q isn't empty.
  451. * + Commands should NOT disconnect if input_Q is empty.
  452. * + Disconnect should be allowed if there are commands in input_Q
  453. * for a different target/lun. In this case, the other commands
  454. * should be made disconnect-able, if not already.
  455. *
  456. * I know, I know - this code would flunk me out of any
  457. * "C Programming 101" class ever offered. But it's easy
  458. * to change around and experiment with for now.
  459. */
  460. cmd->SCp.phase = 0; /* assume no disconnect */
  461. if (hostdata->disconnect == DIS_NEVER)
  462. goto no;
  463. if (hostdata->disconnect == DIS_ALWAYS)
  464. goto yes;
  465. if (cmd->device->type == 1) /* tape drive? */
  466. goto yes;
  467. if (hostdata->disconnected_Q) /* other commands disconnected? */
  468. goto yes;
  469. if (!(hostdata->input_Q)) /* input_Q empty? */
  470. goto no;
  471. for (prev = (struct scsi_cmnd *) hostdata->input_Q; prev;
  472. prev = (struct scsi_cmnd *) prev->host_scribble) {
  473. if ((prev->device->id != cmd->device->id) ||
  474. (prev->device->lun != cmd->device->lun)) {
  475. for (prev = (struct scsi_cmnd *) hostdata->input_Q; prev;
  476. prev = (struct scsi_cmnd *) prev->host_scribble)
  477. prev->SCp.phase = 1;
  478. goto yes;
  479. }
  480. }
  481. goto no;
  482. yes:
  483. cmd->SCp.phase = 1;
  484. #ifdef PROC_STATISTICS
  485. hostdata->disc_allowed_cnt[cmd->device->id]++;
  486. #endif
  487. no:
  488. write_wd33c93(regs, WD_SOURCE_ID, ((cmd->SCp.phase) ? SRCID_ER : 0));
  489. write_wd33c93(regs, WD_TARGET_LUN, cmd->device->lun);
  490. write_wd33c93(regs, WD_SYNCHRONOUS_TRANSFER,
  491. hostdata->sync_xfer[cmd->device->id]);
  492. hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
  493. if ((hostdata->level2 == L2_NONE) ||
  494. (hostdata->sync_stat[cmd->device->id] == SS_UNSET)) {
  495. /*
  496. * Do a 'Select-With-ATN' command. This will end with
  497. * one of the following interrupts:
  498. * CSR_RESEL_AM: failure - can try again later.
  499. * CSR_TIMEOUT: failure - give up.
  500. * CSR_SELECT: success - proceed.
  501. */
  502. hostdata->selecting = cmd;
  503. /* Every target has its own synchronous transfer setting, kept in the
  504. * sync_xfer array, and a corresponding status byte in sync_stat[].
  505. * Each target's sync_stat[] entry is initialized to SX_UNSET, and its
  506. * sync_xfer[] entry is initialized to the default/safe value. SS_UNSET
  507. * means that the parameters are undetermined as yet, and that we
  508. * need to send an SDTR message to this device after selection is
  509. * complete: We set SS_FIRST to tell the interrupt routine to do so.
  510. * If we've been asked not to try synchronous transfers on this
  511. * target (and _all_ luns within it), we'll still send the SDTR message
  512. * later, but at that time we'll negotiate for async by specifying a
  513. * sync fifo depth of 0.
  514. */
  515. if (hostdata->sync_stat[cmd->device->id] == SS_UNSET)
  516. hostdata->sync_stat[cmd->device->id] = SS_FIRST;
  517. hostdata->state = S_SELECTING;
  518. write_wd33c93_count(regs, 0); /* guarantee a DATA_PHASE interrupt */
  519. write_wd33c93_cmd(regs, WD_CMD_SEL_ATN);
  520. } else {
  521. /*
  522. * Do a 'Select-With-ATN-Xfer' command. This will end with
  523. * one of the following interrupts:
  524. * CSR_RESEL_AM: failure - can try again later.
  525. * CSR_TIMEOUT: failure - give up.
  526. * anything else: success - proceed.
  527. */
  528. hostdata->connected = cmd;
  529. write_wd33c93(regs, WD_COMMAND_PHASE, 0);
  530. /* copy command_descriptor_block into WD chip
  531. * (take advantage of auto-incrementing)
  532. */
  533. write_wd33c93_cdb(regs, cmd->cmd_len, cmd->cmnd);
  534. /* The wd33c93 only knows about Group 0, 1, and 5 commands when
  535. * it's doing a 'select-and-transfer'. To be safe, we write the
  536. * size of the CDB into the OWN_ID register for every case. This
  537. * way there won't be problems with vendor-unique, audio, etc.
  538. */
  539. write_wd33c93(regs, WD_OWN_ID, cmd->cmd_len);
  540. /* When doing a non-disconnect command with DMA, we can save
  541. * ourselves a DATA phase interrupt later by setting everything
  542. * up ahead of time.
  543. */
  544. if ((cmd->SCp.phase == 0) && (hostdata->no_dma == 0)) {
  545. if (hostdata->dma_setup(cmd,
  546. (cmd->sc_data_direction == DMA_TO_DEVICE) ?
  547. DATA_OUT_DIR : DATA_IN_DIR))
  548. write_wd33c93_count(regs, 0); /* guarantee a DATA_PHASE interrupt */
  549. else {
  550. write_wd33c93_count(regs,
  551. cmd->SCp.this_residual);
  552. write_wd33c93(regs, WD_CONTROL,
  553. CTRL_IDI | CTRL_EDI | CTRL_DMA);
  554. hostdata->dma = D_DMA_RUNNING;
  555. }
  556. } else
  557. write_wd33c93_count(regs, 0); /* guarantee a DATA_PHASE interrupt */
  558. hostdata->state = S_RUNNING_LEVEL2;
  559. write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
  560. }
  561. /*
  562. * Since the SCSI bus can handle only 1 connection at a time,
  563. * we get out of here now. If the selection fails, or when
  564. * the command disconnects, we'll come back to this routine
  565. * to search the input_Q again...
  566. */
  567. DB(DB_EXECUTE,
  568. printk("%s%ld)EX-2 ", (cmd->SCp.phase) ? "d:" : "", cmd->pid))
  569. }
  570. static void
  571. transfer_pio(const wd33c93_regs regs, uchar * buf, int cnt,
  572. int data_in_dir, struct WD33C93_hostdata *hostdata)
  573. {
  574. uchar asr;
  575. DB(DB_TRANSFER,
  576. printk("(%p,%d,%s:", buf, cnt, data_in_dir ? "in" : "out"))
  577. write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
  578. write_wd33c93_count(regs, cnt);
  579. write_wd33c93_cmd(regs, WD_CMD_TRANS_INFO);
  580. if (data_in_dir) {
  581. do {
  582. asr = read_aux_stat(regs);
  583. if (asr & ASR_DBR)
  584. *buf++ = read_wd33c93(regs, WD_DATA);
  585. } while (!(asr & ASR_INT));
  586. } else {
  587. do {
  588. asr = read_aux_stat(regs);
  589. if (asr & ASR_DBR)
  590. write_wd33c93(regs, WD_DATA, *buf++);
  591. } while (!(asr & ASR_INT));
  592. }
  593. /* Note: we are returning with the interrupt UN-cleared.
  594. * Since (presumably) an entire I/O operation has
  595. * completed, the bus phase is probably different, and
  596. * the interrupt routine will discover this when it
  597. * responds to the uncleared int.
  598. */
  599. }
  600. static void
  601. transfer_bytes(const wd33c93_regs regs, struct scsi_cmnd *cmd,
  602. int data_in_dir)
  603. {
  604. struct WD33C93_hostdata *hostdata;
  605. unsigned long length;
  606. hostdata = (struct WD33C93_hostdata *) cmd->device->host->hostdata;
  607. /* Normally, you'd expect 'this_residual' to be non-zero here.
  608. * In a series of scatter-gather transfers, however, this
  609. * routine will usually be called with 'this_residual' equal
  610. * to 0 and 'buffers_residual' non-zero. This means that a
  611. * previous transfer completed, clearing 'this_residual', and
  612. * now we need to setup the next scatter-gather buffer as the
  613. * source or destination for THIS transfer.
  614. */
  615. if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
  616. ++cmd->SCp.buffer;
  617. --cmd->SCp.buffers_residual;
  618. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  619. cmd->SCp.ptr = page_address(cmd->SCp.buffer->page) +
  620. cmd->SCp.buffer->offset;
  621. }
  622. write_wd33c93(regs, WD_SYNCHRONOUS_TRANSFER,
  623. hostdata->sync_xfer[cmd->device->id]);
  624. /* 'hostdata->no_dma' is TRUE if we don't even want to try DMA.
  625. * Update 'this_residual' and 'ptr' after 'transfer_pio()' returns.
  626. */
  627. if (hostdata->no_dma || hostdata->dma_setup(cmd, data_in_dir)) {
  628. #ifdef PROC_STATISTICS
  629. hostdata->pio_cnt++;
  630. #endif
  631. transfer_pio(regs, (uchar *) cmd->SCp.ptr,
  632. cmd->SCp.this_residual, data_in_dir, hostdata);
  633. length = cmd->SCp.this_residual;
  634. cmd->SCp.this_residual = read_wd33c93_count(regs);
  635. cmd->SCp.ptr += (length - cmd->SCp.this_residual);
  636. }
  637. /* We are able to do DMA (in fact, the Amiga hardware is
  638. * already going!), so start up the wd33c93 in DMA mode.
  639. * We set 'hostdata->dma' = D_DMA_RUNNING so that when the
  640. * transfer completes and causes an interrupt, we're
  641. * reminded to tell the Amiga to shut down its end. We'll
  642. * postpone the updating of 'this_residual' and 'ptr'
  643. * until then.
  644. */
  645. else {
  646. #ifdef PROC_STATISTICS
  647. hostdata->dma_cnt++;
  648. #endif
  649. write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_DMA);
  650. write_wd33c93_count(regs, cmd->SCp.this_residual);
  651. if ((hostdata->level2 >= L2_DATA) ||
  652. (hostdata->level2 == L2_BASIC && cmd->SCp.phase == 0)) {
  653. write_wd33c93(regs, WD_COMMAND_PHASE, 0x45);
  654. write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
  655. hostdata->state = S_RUNNING_LEVEL2;
  656. } else
  657. write_wd33c93_cmd(regs, WD_CMD_TRANS_INFO);
  658. hostdata->dma = D_DMA_RUNNING;
  659. }
  660. }
  661. void
  662. wd33c93_intr(struct Scsi_Host *instance)
  663. {
  664. struct WD33C93_hostdata *hostdata =
  665. (struct WD33C93_hostdata *) instance->hostdata;
  666. const wd33c93_regs regs = hostdata->regs;
  667. struct scsi_cmnd *patch, *cmd;
  668. uchar asr, sr, phs, id, lun, *ucp, msg;
  669. unsigned long length, flags;
  670. asr = read_aux_stat(regs);
  671. if (!(asr & ASR_INT) || (asr & ASR_BSY))
  672. return;
  673. spin_lock_irqsave(&hostdata->lock, flags);
  674. #ifdef PROC_STATISTICS
  675. hostdata->int_cnt++;
  676. #endif
  677. cmd = (struct scsi_cmnd *) hostdata->connected; /* assume we're connected */
  678. sr = read_wd33c93(regs, WD_SCSI_STATUS); /* clear the interrupt */
  679. phs = read_wd33c93(regs, WD_COMMAND_PHASE);
  680. DB(DB_INTR, printk("{%02x:%02x-", asr, sr))
  681. /* After starting a DMA transfer, the next interrupt
  682. * is guaranteed to be in response to completion of
  683. * the transfer. Since the Amiga DMA hardware runs in
  684. * in an open-ended fashion, it needs to be told when
  685. * to stop; do that here if D_DMA_RUNNING is true.
  686. * Also, we have to update 'this_residual' and 'ptr'
  687. * based on the contents of the TRANSFER_COUNT register,
  688. * in case the device decided to do an intermediate
  689. * disconnect (a device may do this if it has to do a
  690. * seek, or just to be nice and let other devices have
  691. * some bus time during long transfers). After doing
  692. * whatever is needed, we go on and service the WD3393
  693. * interrupt normally.
  694. */
  695. if (hostdata->dma == D_DMA_RUNNING) {
  696. DB(DB_TRANSFER,
  697. printk("[%p/%d:", cmd->SCp.ptr, cmd->SCp.this_residual))
  698. hostdata->dma_stop(cmd->device->host, cmd, 1);
  699. hostdata->dma = D_DMA_OFF;
  700. length = cmd->SCp.this_residual;
  701. cmd->SCp.this_residual = read_wd33c93_count(regs);
  702. cmd->SCp.ptr += (length - cmd->SCp.this_residual);
  703. DB(DB_TRANSFER,
  704. printk("%p/%d]", cmd->SCp.ptr, cmd->SCp.this_residual))
  705. }
  706. /* Respond to the specific WD3393 interrupt - there are quite a few! */
  707. switch (sr) {
  708. case CSR_TIMEOUT:
  709. DB(DB_INTR, printk("TIMEOUT"))
  710. if (hostdata->state == S_RUNNING_LEVEL2)
  711. hostdata->connected = NULL;
  712. else {
  713. cmd = (struct scsi_cmnd *) hostdata->selecting; /* get a valid cmd */
  714. hostdata->selecting = NULL;
  715. }
  716. cmd->result = DID_NO_CONNECT << 16;
  717. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  718. hostdata->state = S_UNCONNECTED;
  719. cmd->scsi_done(cmd);
  720. /* From esp.c:
  721. * There is a window of time within the scsi_done() path
  722. * of execution where interrupts are turned back on full
  723. * blast and left that way. During that time we could
  724. * reconnect to a disconnected command, then we'd bomb
  725. * out below. We could also end up executing two commands
  726. * at _once_. ...just so you know why the restore_flags()
  727. * is here...
  728. */
  729. spin_unlock_irqrestore(&hostdata->lock, flags);
  730. /* We are not connected to a target - check to see if there
  731. * are commands waiting to be executed.
  732. */
  733. wd33c93_execute(instance);
  734. break;
  735. /* Note: this interrupt should not occur in a LEVEL2 command */
  736. case CSR_SELECT:
  737. DB(DB_INTR, printk("SELECT"))
  738. hostdata->connected = cmd =
  739. (struct scsi_cmnd *) hostdata->selecting;
  740. hostdata->selecting = NULL;
  741. /* construct an IDENTIFY message with correct disconnect bit */
  742. hostdata->outgoing_msg[0] = (0x80 | 0x00 | cmd->device->lun);
  743. if (cmd->SCp.phase)
  744. hostdata->outgoing_msg[0] |= 0x40;
  745. if (hostdata->sync_stat[cmd->device->id] == SS_FIRST) {
  746. #ifdef SYNC_DEBUG
  747. printk(" sending SDTR ");
  748. #endif
  749. hostdata->sync_stat[cmd->device->id] = SS_WAITING;
  750. /* Tack on a 2nd message to ask about synchronous transfers. If we've
  751. * been asked to do only asynchronous transfers on this device, we
  752. * request a fifo depth of 0, which is equivalent to async - should
  753. * solve the problems some people have had with GVP's Guru ROM.
  754. */
  755. hostdata->outgoing_msg[1] = EXTENDED_MESSAGE;
  756. hostdata->outgoing_msg[2] = 3;
  757. hostdata->outgoing_msg[3] = EXTENDED_SDTR;
  758. if (hostdata->no_sync & (1 << cmd->device->id)) {
  759. hostdata->outgoing_msg[4] =
  760. hostdata->default_sx_per / 4;
  761. hostdata->outgoing_msg[5] = 0;
  762. } else {
  763. hostdata->outgoing_msg[4] = OPTIMUM_SX_PER / 4;
  764. hostdata->outgoing_msg[5] = OPTIMUM_SX_OFF;
  765. }
  766. hostdata->outgoing_len = 6;
  767. } else
  768. hostdata->outgoing_len = 1;
  769. hostdata->state = S_CONNECTED;
  770. spin_unlock_irqrestore(&hostdata->lock, flags);
  771. break;
  772. case CSR_XFER_DONE | PHS_DATA_IN:
  773. case CSR_UNEXP | PHS_DATA_IN:
  774. case CSR_SRV_REQ | PHS_DATA_IN:
  775. DB(DB_INTR,
  776. printk("IN-%d.%d", cmd->SCp.this_residual,
  777. cmd->SCp.buffers_residual))
  778. transfer_bytes(regs, cmd, DATA_IN_DIR);
  779. if (hostdata->state != S_RUNNING_LEVEL2)
  780. hostdata->state = S_CONNECTED;
  781. spin_unlock_irqrestore(&hostdata->lock, flags);
  782. break;
  783. case CSR_XFER_DONE | PHS_DATA_OUT:
  784. case CSR_UNEXP | PHS_DATA_OUT:
  785. case CSR_SRV_REQ | PHS_DATA_OUT:
  786. DB(DB_INTR,
  787. printk("OUT-%d.%d", cmd->SCp.this_residual,
  788. cmd->SCp.buffers_residual))
  789. transfer_bytes(regs, cmd, DATA_OUT_DIR);
  790. if (hostdata->state != S_RUNNING_LEVEL2)
  791. hostdata->state = S_CONNECTED;
  792. spin_unlock_irqrestore(&hostdata->lock, flags);
  793. break;
  794. /* Note: this interrupt should not occur in a LEVEL2 command */
  795. case CSR_XFER_DONE | PHS_COMMAND:
  796. case CSR_UNEXP | PHS_COMMAND:
  797. case CSR_SRV_REQ | PHS_COMMAND:
  798. DB(DB_INTR, printk("CMND-%02x,%ld", cmd->cmnd[0], cmd->pid))
  799. transfer_pio(regs, cmd->cmnd, cmd->cmd_len, DATA_OUT_DIR,
  800. hostdata);
  801. hostdata->state = S_CONNECTED;
  802. spin_unlock_irqrestore(&hostdata->lock, flags);
  803. break;
  804. case CSR_XFER_DONE | PHS_STATUS:
  805. case CSR_UNEXP | PHS_STATUS:
  806. case CSR_SRV_REQ | PHS_STATUS:
  807. DB(DB_INTR, printk("STATUS="))
  808. cmd->SCp.Status = read_1_byte(regs);
  809. DB(DB_INTR, printk("%02x", cmd->SCp.Status))
  810. if (hostdata->level2 >= L2_BASIC) {
  811. sr = read_wd33c93(regs, WD_SCSI_STATUS); /* clear interrupt */
  812. hostdata->state = S_RUNNING_LEVEL2;
  813. write_wd33c93(regs, WD_COMMAND_PHASE, 0x50);
  814. write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
  815. } else {
  816. hostdata->state = S_CONNECTED;
  817. }
  818. spin_unlock_irqrestore(&hostdata->lock, flags);
  819. break;
  820. case CSR_XFER_DONE | PHS_MESS_IN:
  821. case CSR_UNEXP | PHS_MESS_IN:
  822. case CSR_SRV_REQ | PHS_MESS_IN:
  823. DB(DB_INTR, printk("MSG_IN="))
  824. msg = read_1_byte(regs);
  825. sr = read_wd33c93(regs, WD_SCSI_STATUS); /* clear interrupt */
  826. hostdata->incoming_msg[hostdata->incoming_ptr] = msg;
  827. if (hostdata->incoming_msg[0] == EXTENDED_MESSAGE)
  828. msg = EXTENDED_MESSAGE;
  829. else
  830. hostdata->incoming_ptr = 0;
  831. cmd->SCp.Message = msg;
  832. switch (msg) {
  833. case COMMAND_COMPLETE:
  834. DB(DB_INTR, printk("CCMP-%ld", cmd->pid))
  835. write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
  836. hostdata->state = S_PRE_CMP_DISC;
  837. break;
  838. case SAVE_POINTERS:
  839. DB(DB_INTR, printk("SDP"))
  840. write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
  841. hostdata->state = S_CONNECTED;
  842. break;
  843. case RESTORE_POINTERS:
  844. DB(DB_INTR, printk("RDP"))
  845. if (hostdata->level2 >= L2_BASIC) {
  846. write_wd33c93(regs, WD_COMMAND_PHASE, 0x45);
  847. write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
  848. hostdata->state = S_RUNNING_LEVEL2;
  849. } else {
  850. write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
  851. hostdata->state = S_CONNECTED;
  852. }
  853. break;
  854. case DISCONNECT:
  855. DB(DB_INTR, printk("DIS"))
  856. cmd->device->disconnect = 1;
  857. write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
  858. hostdata->state = S_PRE_TMP_DISC;
  859. break;
  860. case MESSAGE_REJECT:
  861. DB(DB_INTR, printk("REJ"))
  862. #ifdef SYNC_DEBUG
  863. printk("-REJ-");
  864. #endif
  865. if (hostdata->sync_stat[cmd->device->id] == SS_WAITING)
  866. hostdata->sync_stat[cmd->device->id] = SS_SET;
  867. write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
  868. hostdata->state = S_CONNECTED;
  869. break;
  870. case EXTENDED_MESSAGE:
  871. DB(DB_INTR, printk("EXT"))
  872. ucp = hostdata->incoming_msg;
  873. #ifdef SYNC_DEBUG
  874. printk("%02x", ucp[hostdata->incoming_ptr]);
  875. #endif
  876. /* Is this the last byte of the extended message? */
  877. if ((hostdata->incoming_ptr >= 2) &&
  878. (hostdata->incoming_ptr == (ucp[1] + 1))) {
  879. switch (ucp[2]) { /* what's the EXTENDED code? */
  880. case EXTENDED_SDTR:
  881. id = calc_sync_xfer(ucp[3], ucp[4]);
  882. if (hostdata->sync_stat[cmd->device->id] !=
  883. SS_WAITING) {
  884. /* A device has sent an unsolicited SDTR message; rather than go
  885. * through the effort of decoding it and then figuring out what
  886. * our reply should be, we're just gonna say that we have a
  887. * synchronous fifo depth of 0. This will result in asynchronous
  888. * transfers - not ideal but so much easier.
  889. * Actually, this is OK because it assures us that if we don't
  890. * specifically ask for sync transfers, we won't do any.
  891. */
  892. write_wd33c93_cmd(regs, WD_CMD_ASSERT_ATN); /* want MESS_OUT */
  893. hostdata->outgoing_msg[0] =
  894. EXTENDED_MESSAGE;
  895. hostdata->outgoing_msg[1] = 3;
  896. hostdata->outgoing_msg[2] =
  897. EXTENDED_SDTR;
  898. hostdata->outgoing_msg[3] =
  899. hostdata->default_sx_per /
  900. 4;
  901. hostdata->outgoing_msg[4] = 0;
  902. hostdata->outgoing_len = 5;
  903. hostdata->sync_xfer[cmd->device->id] =
  904. calc_sync_xfer(hostdata->
  905. default_sx_per
  906. / 4, 0);
  907. } else {
  908. hostdata->sync_xfer[cmd->device->id] = id;
  909. }
  910. #ifdef SYNC_DEBUG
  911. printk("sync_xfer=%02x",
  912. hostdata->sync_xfer[cmd->device->id]);
  913. #endif
  914. hostdata->sync_stat[cmd->device->id] =
  915. SS_SET;
  916. write_wd33c93_cmd(regs,
  917. WD_CMD_NEGATE_ACK);
  918. hostdata->state = S_CONNECTED;
  919. break;
  920. case EXTENDED_WDTR:
  921. write_wd33c93_cmd(regs, WD_CMD_ASSERT_ATN); /* want MESS_OUT */
  922. printk("sending WDTR ");
  923. hostdata->outgoing_msg[0] =
  924. EXTENDED_MESSAGE;
  925. hostdata->outgoing_msg[1] = 2;
  926. hostdata->outgoing_msg[2] =
  927. EXTENDED_WDTR;
  928. hostdata->outgoing_msg[3] = 0; /* 8 bit transfer width */
  929. hostdata->outgoing_len = 4;
  930. write_wd33c93_cmd(regs,
  931. WD_CMD_NEGATE_ACK);
  932. hostdata->state = S_CONNECTED;
  933. break;
  934. default:
  935. write_wd33c93_cmd(regs, WD_CMD_ASSERT_ATN); /* want MESS_OUT */
  936. printk
  937. ("Rejecting Unknown Extended Message(%02x). ",
  938. ucp[2]);
  939. hostdata->outgoing_msg[0] =
  940. MESSAGE_REJECT;
  941. hostdata->outgoing_len = 1;
  942. write_wd33c93_cmd(regs,
  943. WD_CMD_NEGATE_ACK);
  944. hostdata->state = S_CONNECTED;
  945. break;
  946. }
  947. hostdata->incoming_ptr = 0;
  948. }
  949. /* We need to read more MESS_IN bytes for the extended message */
  950. else {
  951. hostdata->incoming_ptr++;
  952. write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
  953. hostdata->state = S_CONNECTED;
  954. }
  955. break;
  956. default:
  957. printk("Rejecting Unknown Message(%02x) ", msg);
  958. write_wd33c93_cmd(regs, WD_CMD_ASSERT_ATN); /* want MESS_OUT */
  959. hostdata->outgoing_msg[0] = MESSAGE_REJECT;
  960. hostdata->outgoing_len = 1;
  961. write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
  962. hostdata->state = S_CONNECTED;
  963. }
  964. spin_unlock_irqrestore(&hostdata->lock, flags);
  965. break;
  966. /* Note: this interrupt will occur only after a LEVEL2 command */
  967. case CSR_SEL_XFER_DONE:
  968. /* Make sure that reselection is enabled at this point - it may
  969. * have been turned off for the command that just completed.
  970. */
  971. write_wd33c93(regs, WD_SOURCE_ID, SRCID_ER);
  972. if (phs == 0x60) {
  973. DB(DB_INTR, printk("SX-DONE-%ld", cmd->pid))
  974. cmd->SCp.Message = COMMAND_COMPLETE;
  975. lun = read_wd33c93(regs, WD_TARGET_LUN);
  976. DB(DB_INTR, printk(":%d.%d", cmd->SCp.Status, lun))
  977. hostdata->connected = NULL;
  978. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  979. hostdata->state = S_UNCONNECTED;
  980. if (cmd->SCp.Status == ILLEGAL_STATUS_BYTE)
  981. cmd->SCp.Status = lun;
  982. if (cmd->cmnd[0] == REQUEST_SENSE
  983. && cmd->SCp.Status != GOOD)
  984. cmd->result =
  985. (cmd->
  986. result & 0x00ffff) | (DID_ERROR << 16);
  987. else
  988. cmd->result =
  989. cmd->SCp.Status | (cmd->SCp.Message << 8);
  990. cmd->scsi_done(cmd);
  991. /* We are no longer connected to a target - check to see if
  992. * there are commands waiting to be executed.
  993. */
  994. spin_unlock_irqrestore(&hostdata->lock, flags);
  995. wd33c93_execute(instance);
  996. } else {
  997. printk
  998. ("%02x:%02x:%02x-%ld: Unknown SEL_XFER_DONE phase!!---",
  999. asr, sr, phs, cmd->pid);
  1000. spin_unlock_irqrestore(&hostdata->lock, flags);
  1001. }
  1002. break;
  1003. /* Note: this interrupt will occur only after a LEVEL2 command */
  1004. case CSR_SDP:
  1005. DB(DB_INTR, printk("SDP"))
  1006. hostdata->state = S_RUNNING_LEVEL2;
  1007. write_wd33c93(regs, WD_COMMAND_PHASE, 0x41);
  1008. write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
  1009. spin_unlock_irqrestore(&hostdata->lock, flags);
  1010. break;
  1011. case CSR_XFER_DONE | PHS_MESS_OUT:
  1012. case CSR_UNEXP | PHS_MESS_OUT:
  1013. case CSR_SRV_REQ | PHS_MESS_OUT:
  1014. DB(DB_INTR, printk("MSG_OUT="))
  1015. /* To get here, we've probably requested MESSAGE_OUT and have
  1016. * already put the correct bytes in outgoing_msg[] and filled
  1017. * in outgoing_len. We simply send them out to the SCSI bus.
  1018. * Sometimes we get MESSAGE_OUT phase when we're not expecting
  1019. * it - like when our SDTR message is rejected by a target. Some
  1020. * targets send the REJECT before receiving all of the extended
  1021. * message, and then seem to go back to MESSAGE_OUT for a byte
  1022. * or two. Not sure why, or if I'm doing something wrong to
  1023. * cause this to happen. Regardless, it seems that sending
  1024. * NOP messages in these situations results in no harm and
  1025. * makes everyone happy.
  1026. */
  1027. if (hostdata->outgoing_len == 0) {
  1028. hostdata->outgoing_len = 1;
  1029. hostdata->outgoing_msg[0] = NOP;
  1030. }
  1031. transfer_pio(regs, hostdata->outgoing_msg,
  1032. hostdata->outgoing_len, DATA_OUT_DIR, hostdata);
  1033. DB(DB_INTR, printk("%02x", hostdata->outgoing_msg[0]))
  1034. hostdata->outgoing_len = 0;
  1035. hostdata->state = S_CONNECTED;
  1036. spin_unlock_irqrestore(&hostdata->lock, flags);
  1037. break;
  1038. case CSR_UNEXP_DISC:
  1039. /* I think I've seen this after a request-sense that was in response
  1040. * to an error condition, but not sure. We certainly need to do
  1041. * something when we get this interrupt - the question is 'what?'.
  1042. * Let's think positively, and assume some command has finished
  1043. * in a legal manner (like a command that provokes a request-sense),
  1044. * so we treat it as a normal command-complete-disconnect.
  1045. */
  1046. /* Make sure that reselection is enabled at this point - it may
  1047. * have been turned off for the command that just completed.
  1048. */
  1049. write_wd33c93(regs, WD_SOURCE_ID, SRCID_ER);
  1050. if (cmd == NULL) {
  1051. printk(" - Already disconnected! ");
  1052. hostdata->state = S_UNCONNECTED;
  1053. spin_unlock_irqrestore(&hostdata->lock, flags);
  1054. return;
  1055. }
  1056. DB(DB_INTR, printk("UNEXP_DISC-%ld", cmd->pid))
  1057. hostdata->connected = NULL;
  1058. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  1059. hostdata->state = S_UNCONNECTED;
  1060. if (cmd->cmnd[0] == REQUEST_SENSE && cmd->SCp.Status != GOOD)
  1061. cmd->result =
  1062. (cmd->result & 0x00ffff) | (DID_ERROR << 16);
  1063. else
  1064. cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
  1065. cmd->scsi_done(cmd);
  1066. /* We are no longer connected to a target - check to see if
  1067. * there are commands waiting to be executed.
  1068. */
  1069. /* look above for comments on scsi_done() */
  1070. spin_unlock_irqrestore(&hostdata->lock, flags);
  1071. wd33c93_execute(instance);
  1072. break;
  1073. case CSR_DISC:
  1074. /* Make sure that reselection is enabled at this point - it may
  1075. * have been turned off for the command that just completed.
  1076. */
  1077. write_wd33c93(regs, WD_SOURCE_ID, SRCID_ER);
  1078. DB(DB_INTR, printk("DISC-%ld", cmd->pid))
  1079. if (cmd == NULL) {
  1080. printk(" - Already disconnected! ");
  1081. hostdata->state = S_UNCONNECTED;
  1082. }
  1083. switch (hostdata->state) {
  1084. case S_PRE_CMP_DISC:
  1085. hostdata->connected = NULL;
  1086. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  1087. hostdata->state = S_UNCONNECTED;
  1088. DB(DB_INTR, printk(":%d", cmd->SCp.Status))
  1089. if (cmd->cmnd[0] == REQUEST_SENSE
  1090. && cmd->SCp.Status != GOOD)
  1091. cmd->result =
  1092. (cmd->
  1093. result & 0x00ffff) | (DID_ERROR << 16);
  1094. else
  1095. cmd->result =
  1096. cmd->SCp.Status | (cmd->SCp.Message << 8);
  1097. cmd->scsi_done(cmd);
  1098. break;
  1099. case S_PRE_TMP_DISC:
  1100. case S_RUNNING_LEVEL2:
  1101. cmd->host_scribble = (uchar *) hostdata->disconnected_Q;
  1102. hostdata->disconnected_Q = cmd;
  1103. hostdata->connected = NULL;
  1104. hostdata->state = S_UNCONNECTED;
  1105. #ifdef PROC_STATISTICS
  1106. hostdata->disc_done_cnt[cmd->device->id]++;
  1107. #endif
  1108. break;
  1109. default:
  1110. printk("*** Unexpected DISCONNECT interrupt! ***");
  1111. hostdata->state = S_UNCONNECTED;
  1112. }
  1113. /* We are no longer connected to a target - check to see if
  1114. * there are commands waiting to be executed.
  1115. */
  1116. spin_unlock_irqrestore(&hostdata->lock, flags);
  1117. wd33c93_execute(instance);
  1118. break;
  1119. case CSR_RESEL_AM:
  1120. case CSR_RESEL:
  1121. DB(DB_INTR, printk("RESEL%s", sr == CSR_RESEL_AM ? "_AM" : ""))
  1122. /* Old chips (pre -A ???) don't have advanced features and will
  1123. * generate CSR_RESEL. In that case we have to extract the LUN the
  1124. * hard way (see below).
  1125. * First we have to make sure this reselection didn't
  1126. * happen during Arbitration/Selection of some other device.
  1127. * If yes, put losing command back on top of input_Q.
  1128. */
  1129. if (hostdata->level2 <= L2_NONE) {
  1130. if (hostdata->selecting) {
  1131. cmd = (struct scsi_cmnd *) hostdata->selecting;
  1132. hostdata->selecting = NULL;
  1133. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  1134. cmd->host_scribble =
  1135. (uchar *) hostdata->input_Q;
  1136. hostdata->input_Q = cmd;
  1137. }
  1138. }
  1139. else {
  1140. if (cmd) {
  1141. if (phs == 0x00) {
  1142. hostdata->busy[cmd->device->id] &=
  1143. ~(1 << cmd->device->lun);
  1144. cmd->host_scribble =
  1145. (uchar *) hostdata->input_Q;
  1146. hostdata->input_Q = cmd;
  1147. } else {
  1148. printk
  1149. ("---%02x:%02x:%02x-TROUBLE: Intrusive ReSelect!---",
  1150. asr, sr, phs);
  1151. while (1)
  1152. printk("\r");
  1153. }
  1154. }
  1155. }
  1156. /* OK - find out which device reselected us. */
  1157. id = read_wd33c93(regs, WD_SOURCE_ID);
  1158. id &= SRCID_MASK;
  1159. /* and extract the lun from the ID message. (Note that we don't
  1160. * bother to check for a valid message here - I guess this is
  1161. * not the right way to go, but...)
  1162. */
  1163. if (sr == CSR_RESEL_AM) {
  1164. lun = read_wd33c93(regs, WD_DATA);
  1165. if (hostdata->level2 < L2_RESELECT)
  1166. write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
  1167. lun &= 7;
  1168. } else {
  1169. /* Old chip; wait for msgin phase to pick up the LUN. */
  1170. for (lun = 255; lun; lun--) {
  1171. if ((asr = read_aux_stat(regs)) & ASR_INT)
  1172. break;
  1173. udelay(10);
  1174. }
  1175. if (!(asr & ASR_INT)) {
  1176. printk
  1177. ("wd33c93: Reselected without IDENTIFY\n");
  1178. lun = 0;
  1179. } else {
  1180. /* Verify this is a change to MSG_IN and read the message */
  1181. sr = read_wd33c93(regs, WD_SCSI_STATUS);
  1182. if (sr == (CSR_ABORT | PHS_MESS_IN) ||
  1183. sr == (CSR_UNEXP | PHS_MESS_IN) ||
  1184. sr == (CSR_SRV_REQ | PHS_MESS_IN)) {
  1185. /* Got MSG_IN, grab target LUN */
  1186. lun = read_1_byte(regs);
  1187. /* Now we expect a 'paused with ACK asserted' int.. */
  1188. asr = read_aux_stat(regs);
  1189. if (!(asr & ASR_INT)) {
  1190. udelay(10);
  1191. asr = read_aux_stat(regs);
  1192. if (!(asr & ASR_INT))
  1193. printk
  1194. ("wd33c93: No int after LUN on RESEL (%02x)\n",
  1195. asr);
  1196. }
  1197. sr = read_wd33c93(regs, WD_SCSI_STATUS);
  1198. if (sr != CSR_MSGIN)
  1199. printk
  1200. ("wd33c93: Not paused with ACK on RESEL (%02x)\n",
  1201. sr);
  1202. lun &= 7;
  1203. write_wd33c93_cmd(regs,
  1204. WD_CMD_NEGATE_ACK);
  1205. } else {
  1206. printk
  1207. ("wd33c93: Not MSG_IN on reselect (%02x)\n",
  1208. sr);
  1209. lun = 0;
  1210. }
  1211. }
  1212. }
  1213. /* Now we look for the command that's reconnecting. */
  1214. cmd = (struct scsi_cmnd *) hostdata->disconnected_Q;
  1215. patch = NULL;
  1216. while (cmd) {
  1217. if (id == cmd->device->id && lun == cmd->device->lun)
  1218. break;
  1219. patch = cmd;
  1220. cmd = (struct scsi_cmnd *) cmd->host_scribble;
  1221. }
  1222. /* Hmm. Couldn't find a valid command.... What to do? */
  1223. if (!cmd) {
  1224. printk
  1225. ("---TROUBLE: target %d.%d not in disconnect queue---",
  1226. id, lun);
  1227. spin_unlock_irqrestore(&hostdata->lock, flags);
  1228. return;
  1229. }
  1230. /* Ok, found the command - now start it up again. */
  1231. if (patch)
  1232. patch->host_scribble = cmd->host_scribble;
  1233. else
  1234. hostdata->disconnected_Q =
  1235. (struct scsi_cmnd *) cmd->host_scribble;
  1236. hostdata->connected = cmd;
  1237. /* We don't need to worry about 'initialize_SCp()' or 'hostdata->busy[]'
  1238. * because these things are preserved over a disconnect.
  1239. * But we DO need to fix the DPD bit so it's correct for this command.
  1240. */
  1241. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  1242. write_wd33c93(regs, WD_DESTINATION_ID, cmd->device->id);
  1243. else
  1244. write_wd33c93(regs, WD_DESTINATION_ID,
  1245. cmd->device->id | DSTID_DPD);
  1246. if (hostdata->level2 >= L2_RESELECT) {
  1247. write_wd33c93_count(regs, 0); /* we want a DATA_PHASE interrupt */
  1248. write_wd33c93(regs, WD_COMMAND_PHASE, 0x45);
  1249. write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
  1250. hostdata->state = S_RUNNING_LEVEL2;
  1251. } else
  1252. hostdata->state = S_CONNECTED;
  1253. DB(DB_INTR, printk("-%ld", cmd->pid))
  1254. spin_unlock_irqrestore(&hostdata->lock, flags);
  1255. break;
  1256. default:
  1257. printk("--UNKNOWN INTERRUPT:%02x:%02x:%02x--", asr, sr, phs);
  1258. spin_unlock_irqrestore(&hostdata->lock, flags);
  1259. }
  1260. DB(DB_INTR, printk("} "))
  1261. }
  1262. static void
  1263. reset_wd33c93(struct Scsi_Host *instance)
  1264. {
  1265. struct WD33C93_hostdata *hostdata =
  1266. (struct WD33C93_hostdata *) instance->hostdata;
  1267. const wd33c93_regs regs = hostdata->regs;
  1268. uchar sr;
  1269. #ifdef CONFIG_SGI_IP22
  1270. {
  1271. int busycount = 0;
  1272. extern void sgiwd93_reset(unsigned long);
  1273. /* wait 'til the chip gets some time for us */
  1274. while ((read_aux_stat(regs) & ASR_BSY) && busycount++ < 100)
  1275. udelay (10);
  1276. /*
  1277. * there are scsi devices out there, which manage to lock up
  1278. * the wd33c93 in a busy condition. In this state it won't
  1279. * accept the reset command. The only way to solve this is to
  1280. * give the chip a hardware reset (if possible). The code below
  1281. * does this for the SGI Indy, where this is possible
  1282. */
  1283. /* still busy ? */
  1284. if (read_aux_stat(regs) & ASR_BSY)
  1285. sgiwd93_reset(instance->base); /* yeah, give it the hard one */
  1286. }
  1287. #endif
  1288. write_wd33c93(regs, WD_OWN_ID, OWNID_EAF | OWNID_RAF |
  1289. instance->this_id | hostdata->clock_freq);
  1290. write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
  1291. write_wd33c93(regs, WD_SYNCHRONOUS_TRANSFER,
  1292. calc_sync_xfer(hostdata->default_sx_per / 4,
  1293. DEFAULT_SX_OFF));
  1294. write_wd33c93(regs, WD_COMMAND, WD_CMD_RESET);
  1295. #ifdef CONFIG_MVME147_SCSI
  1296. udelay(25); /* The old wd33c93 on MVME147 needs this, at least */
  1297. #endif
  1298. while (!(read_aux_stat(regs) & ASR_INT))
  1299. ;
  1300. sr = read_wd33c93(regs, WD_SCSI_STATUS);
  1301. hostdata->microcode = read_wd33c93(regs, WD_CDB_1);
  1302. if (sr == 0x00)
  1303. hostdata->chip = C_WD33C93;
  1304. else if (sr == 0x01) {
  1305. write_wd33c93(regs, WD_QUEUE_TAG, 0xa5); /* any random number */
  1306. sr = read_wd33c93(regs, WD_QUEUE_TAG);
  1307. if (sr == 0xa5) {
  1308. hostdata->chip = C_WD33C93B;
  1309. write_wd33c93(regs, WD_QUEUE_TAG, 0);
  1310. } else
  1311. hostdata->chip = C_WD33C93A;
  1312. } else
  1313. hostdata->chip = C_UNKNOWN_CHIP;
  1314. write_wd33c93(regs, WD_TIMEOUT_PERIOD, TIMEOUT_PERIOD_VALUE);
  1315. write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
  1316. }
  1317. int
  1318. wd33c93_host_reset(struct scsi_cmnd * SCpnt)
  1319. {
  1320. struct Scsi_Host *instance;
  1321. struct WD33C93_hostdata *hostdata;
  1322. int i;
  1323. instance = SCpnt->device->host;
  1324. hostdata = (struct WD33C93_hostdata *) instance->hostdata;
  1325. printk("scsi%d: reset. ", instance->host_no);
  1326. disable_irq(instance->irq);
  1327. hostdata->dma_stop(instance, NULL, 0);
  1328. for (i = 0; i < 8; i++) {
  1329. hostdata->busy[i] = 0;
  1330. hostdata->sync_xfer[i] =
  1331. calc_sync_xfer(DEFAULT_SX_PER / 4, DEFAULT_SX_OFF);
  1332. hostdata->sync_stat[i] = SS_UNSET; /* using default sync values */
  1333. }
  1334. hostdata->input_Q = NULL;
  1335. hostdata->selecting = NULL;
  1336. hostdata->connected = NULL;
  1337. hostdata->disconnected_Q = NULL;
  1338. hostdata->state = S_UNCONNECTED;
  1339. hostdata->dma = D_DMA_OFF;
  1340. hostdata->incoming_ptr = 0;
  1341. hostdata->outgoing_len = 0;
  1342. reset_wd33c93(instance);
  1343. SCpnt->result = DID_RESET << 16;
  1344. enable_irq(instance->irq);
  1345. return SUCCESS;
  1346. }
  1347. int
  1348. wd33c93_abort(struct scsi_cmnd * cmd)
  1349. {
  1350. struct Scsi_Host *instance;
  1351. struct WD33C93_hostdata *hostdata;
  1352. wd33c93_regs regs;
  1353. struct scsi_cmnd *tmp, *prev;
  1354. disable_irq(cmd->device->host->irq);
  1355. instance = cmd->device->host;
  1356. hostdata = (struct WD33C93_hostdata *) instance->hostdata;
  1357. regs = hostdata->regs;
  1358. /*
  1359. * Case 1 : If the command hasn't been issued yet, we simply remove it
  1360. * from the input_Q.
  1361. */
  1362. tmp = (struct scsi_cmnd *) hostdata->input_Q;
  1363. prev = NULL;
  1364. while (tmp) {
  1365. if (tmp == cmd) {
  1366. if (prev)
  1367. prev->host_scribble = cmd->host_scribble;
  1368. else
  1369. hostdata->input_Q =
  1370. (struct scsi_cmnd *) cmd->host_scribble;
  1371. cmd->host_scribble = NULL;
  1372. cmd->result = DID_ABORT << 16;
  1373. printk
  1374. ("scsi%d: Abort - removing command %ld from input_Q. ",
  1375. instance->host_no, cmd->pid);
  1376. enable_irq(cmd->device->host->irq);
  1377. cmd->scsi_done(cmd);
  1378. return SUCCESS;
  1379. }
  1380. prev = tmp;
  1381. tmp = (struct scsi_cmnd *) tmp->host_scribble;
  1382. }
  1383. /*
  1384. * Case 2 : If the command is connected, we're going to fail the abort
  1385. * and let the high level SCSI driver retry at a later time or
  1386. * issue a reset.
  1387. *
  1388. * Timeouts, and therefore aborted commands, will be highly unlikely
  1389. * and handling them cleanly in this situation would make the common
  1390. * case of noresets less efficient, and would pollute our code. So,
  1391. * we fail.
  1392. */
  1393. if (hostdata->connected == cmd) {
  1394. uchar sr, asr;
  1395. unsigned long timeout;
  1396. printk("scsi%d: Aborting connected command %ld - ",
  1397. instance->host_no, cmd->pid);
  1398. printk("stopping DMA - ");
  1399. if (hostdata->dma == D_DMA_RUNNING) {
  1400. hostdata->dma_stop(instance, cmd, 0);
  1401. hostdata->dma = D_DMA_OFF;
  1402. }
  1403. printk("sending wd33c93 ABORT command - ");
  1404. write_wd33c93(regs, WD_CONTROL,
  1405. CTRL_IDI | CTRL_EDI | CTRL_POLLED);
  1406. write_wd33c93_cmd(regs, WD_CMD_ABORT);
  1407. /* Now we have to attempt to flush out the FIFO... */
  1408. printk("flushing fifo - ");
  1409. timeout = 1000000;
  1410. do {
  1411. asr = read_aux_stat(regs);
  1412. if (asr & ASR_DBR)
  1413. read_wd33c93(regs, WD_DATA);
  1414. } while (!(asr & ASR_INT) && timeout-- > 0);
  1415. sr = read_wd33c93(regs, WD_SCSI_STATUS);
  1416. printk
  1417. ("asr=%02x, sr=%02x, %ld bytes un-transferred (timeout=%ld) - ",
  1418. asr, sr, read_wd33c93_count(regs), timeout);
  1419. /*
  1420. * Abort command processed.
  1421. * Still connected.
  1422. * We must disconnect.
  1423. */
  1424. printk("sending wd33c93 DISCONNECT command - ");
  1425. write_wd33c93_cmd(regs, WD_CMD_DISCONNECT);
  1426. timeout = 1000000;
  1427. asr = read_aux_stat(regs);
  1428. while ((asr & ASR_CIP) && timeout-- > 0)
  1429. asr = read_aux_stat(regs);
  1430. sr = read_wd33c93(regs, WD_SCSI_STATUS);
  1431. printk("asr=%02x, sr=%02x.", asr, sr);
  1432. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  1433. hostdata->connected = NULL;
  1434. hostdata->state = S_UNCONNECTED;
  1435. cmd->result = DID_ABORT << 16;
  1436. /* sti();*/
  1437. wd33c93_execute(instance);
  1438. enable_irq(cmd->device->host->irq);
  1439. cmd->scsi_done(cmd);
  1440. return SUCCESS;
  1441. }
  1442. /*
  1443. * Case 3: If the command is currently disconnected from the bus,
  1444. * we're not going to expend much effort here: Let's just return
  1445. * an ABORT_SNOOZE and hope for the best...
  1446. */
  1447. tmp = (struct scsi_cmnd *) hostdata->disconnected_Q;
  1448. while (tmp) {
  1449. if (tmp == cmd) {
  1450. printk
  1451. ("scsi%d: Abort - command %ld found on disconnected_Q - ",
  1452. instance->host_no, cmd->pid);
  1453. printk("Abort SNOOZE. ");
  1454. enable_irq(cmd->device->host->irq);
  1455. return FAILED;
  1456. }
  1457. tmp = (struct scsi_cmnd *) tmp->host_scribble;
  1458. }
  1459. /*
  1460. * Case 4 : If we reached this point, the command was not found in any of
  1461. * the queues.
  1462. *
  1463. * We probably reached this point because of an unlikely race condition
  1464. * between the command completing successfully and the abortion code,
  1465. * so we won't panic, but we will notify the user in case something really
  1466. * broke.
  1467. */
  1468. /* sti();*/
  1469. wd33c93_execute(instance);
  1470. enable_irq(cmd->device->host->irq);
  1471. printk("scsi%d: warning : SCSI command probably completed successfully"
  1472. " before abortion. ", instance->host_no);
  1473. return FAILED;
  1474. }
  1475. #define MAX_WD33C93_HOSTS 4
  1476. #define MAX_SETUP_ARGS ((int)(sizeof(setup_args) / sizeof(char *)))
  1477. #define SETUP_BUFFER_SIZE 200
  1478. static char setup_buffer[SETUP_BUFFER_SIZE];
  1479. static char setup_used[MAX_SETUP_ARGS];
  1480. static int done_setup = 0;
  1481. int
  1482. wd33c93_setup(char *str)
  1483. {
  1484. int i;
  1485. char *p1, *p2;
  1486. /* The kernel does some processing of the command-line before calling
  1487. * this function: If it begins with any decimal or hex number arguments,
  1488. * ints[0] = how many numbers found and ints[1] through [n] are the values
  1489. * themselves. str points to where the non-numeric arguments (if any)
  1490. * start: We do our own parsing of those. We construct synthetic 'nosync'
  1491. * keywords out of numeric args (to maintain compatibility with older
  1492. * versions) and then add the rest of the arguments.
  1493. */
  1494. p1 = setup_buffer;
  1495. *p1 = '\0';
  1496. if (str)
  1497. strncpy(p1, str, SETUP_BUFFER_SIZE - strlen(setup_buffer));
  1498. setup_buffer[SETUP_BUFFER_SIZE - 1] = '\0';
  1499. p1 = setup_buffer;
  1500. i = 0;
  1501. while (*p1 && (i < MAX_SETUP_ARGS)) {
  1502. p2 = strchr(p1, ',');
  1503. if (p2) {
  1504. *p2 = '\0';
  1505. if (p1 != p2)
  1506. setup_args[i] = p1;
  1507. p1 = p2 + 1;
  1508. i++;
  1509. } else {
  1510. setup_args[i] = p1;
  1511. break;
  1512. }
  1513. }
  1514. for (i = 0; i < MAX_SETUP_ARGS; i++)
  1515. setup_used[i] = 0;
  1516. done_setup = 1;
  1517. return 1;
  1518. }
  1519. __setup("wd33c93=", wd33c93_setup);
  1520. /* check_setup_args() returns index if key found, 0 if not
  1521. */
  1522. static int
  1523. check_setup_args(char *key, int *flags, int *val, char *buf)
  1524. {
  1525. int x;
  1526. char *cp;
  1527. for (x = 0; x < MAX_SETUP_ARGS; x++) {
  1528. if (setup_used[x])
  1529. continue;
  1530. if (!strncmp(setup_args[x], key, strlen(key)))
  1531. break;
  1532. if (!strncmp(setup_args[x], "next", strlen("next")))
  1533. return 0;
  1534. }
  1535. if (x == MAX_SETUP_ARGS)
  1536. return 0;
  1537. setup_used[x] = 1;
  1538. cp = setup_args[x] + strlen(key);
  1539. *val = -1;
  1540. if (*cp != ':')
  1541. return ++x;
  1542. cp++;
  1543. if ((*cp >= '0') && (*cp <= '9')) {
  1544. *val = simple_strtoul(cp, NULL, 0);
  1545. }
  1546. return ++x;
  1547. }
  1548. void
  1549. wd33c93_init(struct Scsi_Host *instance, const wd33c93_regs regs,
  1550. dma_setup_t setup, dma_stop_t stop, int clock_freq)
  1551. {
  1552. struct WD33C93_hostdata *hostdata;
  1553. int i;
  1554. int flags;
  1555. int val;
  1556. char buf[32];
  1557. if (!done_setup && setup_strings)
  1558. wd33c93_setup(setup_strings);
  1559. hostdata = (struct WD33C93_hostdata *) instance->hostdata;
  1560. hostdata->regs = regs;
  1561. hostdata->clock_freq = clock_freq;
  1562. hostdata->dma_setup = setup;
  1563. hostdata->dma_stop = stop;
  1564. hostdata->dma_bounce_buffer = NULL;
  1565. hostdata->dma_bounce_len = 0;
  1566. for (i = 0; i < 8; i++) {
  1567. hostdata->busy[i] = 0;
  1568. hostdata->sync_xfer[i] =
  1569. calc_sync_xfer(DEFAULT_SX_PER / 4, DEFAULT_SX_OFF);
  1570. hostdata->sync_stat[i] = SS_UNSET; /* using default sync values */
  1571. #ifdef PROC_STATISTICS
  1572. hostdata->cmd_cnt[i] = 0;
  1573. hostdata->disc_allowed_cnt[i] = 0;
  1574. hostdata->disc_done_cnt[i] = 0;
  1575. #endif
  1576. }
  1577. hostdata->input_Q = NULL;
  1578. hostdata->selecting = NULL;
  1579. hostdata->connected = NULL;
  1580. hostdata->disconnected_Q = NULL;
  1581. hostdata->state = S_UNCONNECTED;
  1582. hostdata->dma = D_DMA_OFF;
  1583. hostdata->level2 = L2_BASIC;
  1584. hostdata->disconnect = DIS_ADAPTIVE;
  1585. hostdata->args = DEBUG_DEFAULTS;
  1586. hostdata->incoming_ptr = 0;
  1587. hostdata->outgoing_len = 0;
  1588. hostdata->default_sx_per = DEFAULT_SX_PER;
  1589. hostdata->no_sync = 0xff; /* sync defaults to off */
  1590. hostdata->no_dma = 0; /* default is DMA enabled */
  1591. #ifdef PROC_INTERFACE
  1592. hostdata->proc = PR_VERSION | PR_INFO | PR_STATISTICS |
  1593. PR_CONNECTED | PR_INPUTQ | PR_DISCQ | PR_STOP;
  1594. #ifdef PROC_STATISTICS
  1595. hostdata->dma_cnt = 0;
  1596. hostdata->pio_cnt = 0;
  1597. hostdata->int_cnt = 0;
  1598. #endif
  1599. #endif
  1600. if (check_setup_args("nosync", &flags, &val, buf))
  1601. hostdata->no_sync = val;
  1602. if (check_setup_args("nodma", &flags, &val, buf))
  1603. hostdata->no_dma = (val == -1) ? 1 : val;
  1604. if (check_setup_args("period", &flags, &val, buf))
  1605. hostdata->default_sx_per =
  1606. sx_table[round_period((unsigned int) val)].period_ns;
  1607. if (check_setup_args("disconnect", &flags, &val, buf)) {
  1608. if ((val >= DIS_NEVER) && (val <= DIS_ALWAYS))
  1609. hostdata->disconnect = val;
  1610. else
  1611. hostdata->disconnect = DIS_ADAPTIVE;
  1612. }
  1613. if (check_setup_args("level2", &flags, &val, buf))
  1614. hostdata->level2 = val;
  1615. if (check_setup_args("debug", &flags, &val, buf))
  1616. hostdata->args = val & DB_MASK;
  1617. if (check_setup_args("clock", &flags, &val, buf)) {
  1618. if (val > 7 && val < 11)
  1619. val = WD33C93_FS_8_10;
  1620. else if (val > 11 && val < 16)
  1621. val = WD33C93_FS_12_15;
  1622. else if (val > 15 && val < 21)
  1623. val = WD33C93_FS_16_20;
  1624. else
  1625. val = WD33C93_FS_8_10;
  1626. hostdata->clock_freq = val;
  1627. }
  1628. if ((i = check_setup_args("next", &flags, &val, buf))) {
  1629. while (i)
  1630. setup_used[--i] = 1;
  1631. }
  1632. #ifdef PROC_INTERFACE
  1633. if (check_setup_args("proc", &flags, &val, buf))
  1634. hostdata->proc = val;
  1635. #endif
  1636. spin_lock_irq(&hostdata->lock);
  1637. reset_wd33c93(instance);
  1638. spin_unlock_irq(&hostdata->lock);
  1639. printk("wd33c93-%d: chip=%s/%d no_sync=0x%x no_dma=%d",
  1640. instance->host_no,
  1641. (hostdata->chip == C_WD33C93) ? "WD33c93" : (hostdata->chip ==
  1642. C_WD33C93A) ?
  1643. "WD33c93A" : (hostdata->chip ==
  1644. C_WD33C93B) ? "WD33c93B" : "unknown",
  1645. hostdata->microcode, hostdata->no_sync, hostdata->no_dma);
  1646. #ifdef DEBUGGING_ON
  1647. printk(" debug_flags=0x%02x\n", hostdata->args);
  1648. #else
  1649. printk(" debugging=OFF\n");
  1650. #endif
  1651. printk(" setup_args=");
  1652. for (i = 0; i < MAX_SETUP_ARGS; i++)
  1653. printk("%s,", setup_args[i]);
  1654. printk("\n");
  1655. printk(" Version %s - %s, Compiled %s at %s\n",
  1656. WD33C93_VERSION, WD33C93_DATE, __DATE__, __TIME__);
  1657. }
  1658. int
  1659. wd33c93_proc_info(struct Scsi_Host *instance, char *buf, char **start, off_t off, int len, int in)
  1660. {
  1661. #ifdef PROC_INTERFACE
  1662. char *bp;
  1663. char tbuf[128];
  1664. struct WD33C93_hostdata *hd;
  1665. struct scsi_cmnd *cmd;
  1666. int x, i;
  1667. static int stop = 0;
  1668. hd = (struct WD33C93_hostdata *) instance->hostdata;
  1669. /* If 'in' is TRUE we need to _read_ the proc file. We accept the following
  1670. * keywords (same format as command-line, but only ONE per read):
  1671. * debug
  1672. * disconnect
  1673. * period
  1674. * resync
  1675. * proc
  1676. * nodma
  1677. */
  1678. if (in) {
  1679. buf[len] = '\0';
  1680. bp = buf;
  1681. if (!strncmp(bp, "debug:", 6)) {
  1682. bp += 6;
  1683. hd->args = simple_strtoul(bp, NULL, 0) & DB_MASK;
  1684. } else if (!strncmp(bp, "disconnect:", 11)) {
  1685. bp += 11;
  1686. x = simple_strtoul(bp, NULL, 0);
  1687. if (x < DIS_NEVER || x > DIS_ALWAYS)
  1688. x = DIS_ADAPTIVE;
  1689. hd->disconnect = x;
  1690. } else if (!strncmp(bp, "period:", 7)) {
  1691. bp += 7;
  1692. x = simple_strtoul(bp, NULL, 0);
  1693. hd->default_sx_per =
  1694. sx_table[round_period((unsigned int) x)].period_ns;
  1695. } else if (!strncmp(bp, "resync:", 7)) {
  1696. bp += 7;
  1697. x = simple_strtoul(bp, NULL, 0);
  1698. for (i = 0; i < 7; i++)
  1699. if (x & (1 << i))
  1700. hd->sync_stat[i] = SS_UNSET;
  1701. } else if (!strncmp(bp, "proc:", 5)) {
  1702. bp += 5;
  1703. hd->proc = simple_strtoul(bp, NULL, 0);
  1704. } else if (!strncmp(bp, "nodma:", 6)) {
  1705. bp += 6;
  1706. hd->no_dma = simple_strtoul(bp, NULL, 0);
  1707. } else if (!strncmp(bp, "level2:", 7)) {
  1708. bp += 7;
  1709. hd->level2 = simple_strtoul(bp, NULL, 0);
  1710. }
  1711. return len;
  1712. }
  1713. spin_lock_irq(&hd->lock);
  1714. bp = buf;
  1715. *bp = '\0';
  1716. if (hd->proc & PR_VERSION) {
  1717. sprintf(tbuf, "\nVersion %s - %s. Compiled %s %s",
  1718. WD33C93_VERSION, WD33C93_DATE, __DATE__, __TIME__);
  1719. strcat(bp, tbuf);
  1720. }
  1721. if (hd->proc & PR_INFO) {
  1722. sprintf(tbuf, "\nclock_freq=%02x no_sync=%02x no_dma=%d",
  1723. hd->clock_freq, hd->no_sync, hd->no_dma);
  1724. strcat(bp, tbuf);
  1725. strcat(bp, "\nsync_xfer[] = ");
  1726. for (x = 0; x < 7; x++) {
  1727. sprintf(tbuf, "\t%02x", hd->sync_xfer[x]);
  1728. strcat(bp, tbuf);
  1729. }
  1730. strcat(bp, "\nsync_stat[] = ");
  1731. for (x = 0; x < 7; x++) {
  1732. sprintf(tbuf, "\t%02x", hd->sync_stat[x]);
  1733. strcat(bp, tbuf);
  1734. }
  1735. }
  1736. #ifdef PROC_STATISTICS
  1737. if (hd->proc & PR_STATISTICS) {
  1738. strcat(bp, "\ncommands issued: ");
  1739. for (x = 0; x < 7; x++) {
  1740. sprintf(tbuf, "\t%ld", hd->cmd_cnt[x]);
  1741. strcat(bp, tbuf);
  1742. }
  1743. strcat(bp, "\ndisconnects allowed:");
  1744. for (x = 0; x < 7; x++) {
  1745. sprintf(tbuf, "\t%ld", hd->disc_allowed_cnt[x]);
  1746. strcat(bp, tbuf);
  1747. }
  1748. strcat(bp, "\ndisconnects done: ");
  1749. for (x = 0; x < 7; x++) {
  1750. sprintf(tbuf, "\t%ld", hd->disc_done_cnt[x]);
  1751. strcat(bp, tbuf);
  1752. }
  1753. sprintf(tbuf,
  1754. "\ninterrupts: %ld, DATA_PHASE ints: %ld DMA, %ld PIO",
  1755. hd->int_cnt, hd->dma_cnt, hd->pio_cnt);
  1756. strcat(bp, tbuf);
  1757. }
  1758. #endif
  1759. if (hd->proc & PR_CONNECTED) {
  1760. strcat(bp, "\nconnected: ");
  1761. if (hd->connected) {
  1762. cmd = (struct scsi_cmnd *) hd->connected;
  1763. sprintf(tbuf, " %ld-%d:%d(%02x)",
  1764. cmd->pid, cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
  1765. strcat(bp, tbuf);
  1766. }
  1767. }
  1768. if (hd->proc & PR_INPUTQ) {
  1769. strcat(bp, "\ninput_Q: ");
  1770. cmd = (struct scsi_cmnd *) hd->input_Q;
  1771. while (cmd) {
  1772. sprintf(tbuf, " %ld-%d:%d(%02x)",
  1773. cmd->pid, cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
  1774. strcat(bp, tbuf);
  1775. cmd = (struct scsi_cmnd *) cmd->host_scribble;
  1776. }
  1777. }
  1778. if (hd->proc & PR_DISCQ) {
  1779. strcat(bp, "\ndisconnected_Q:");
  1780. cmd = (struct scsi_cmnd *) hd->disconnected_Q;
  1781. while (cmd) {
  1782. sprintf(tbuf, " %ld-%d:%d(%02x)",
  1783. cmd->pid, cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
  1784. strcat(bp, tbuf);
  1785. cmd = (struct scsi_cmnd *) cmd->host_scribble;
  1786. }
  1787. }
  1788. strcat(bp, "\n");
  1789. spin_unlock_irq(&hd->lock);
  1790. *start = buf;
  1791. if (stop) {
  1792. stop = 0;
  1793. return 0;
  1794. }
  1795. if (off > 0x40000) /* ALWAYS stop after 256k bytes have been read */
  1796. stop = 1;
  1797. if (hd->proc & PR_STOP) /* stop every other time */
  1798. stop = 1;
  1799. return strlen(bp);
  1800. #else /* PROC_INTERFACE */
  1801. return 0;
  1802. #endif /* PROC_INTERFACE */
  1803. }
  1804. void
  1805. wd33c93_release(void)
  1806. {
  1807. }
  1808. EXPORT_SYMBOL(wd33c93_host_reset);
  1809. EXPORT_SYMBOL(wd33c93_init);
  1810. EXPORT_SYMBOL(wd33c93_release);
  1811. EXPORT_SYMBOL(wd33c93_abort);
  1812. EXPORT_SYMBOL(wd33c93_queuecommand);
  1813. EXPORT_SYMBOL(wd33c93_intr);
  1814. EXPORT_SYMBOL(wd33c93_proc_info);