wd33c93.c 61 KB

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