wd33c93.c 64 KB

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