wd33c93.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 1996 John Shifflett, GeoLog Consulting
  4. * [email protected]
  5. * [email protected]
  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 [email protected] or [email protected]! 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 <[email protected]> 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 <[email protected]> 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 (scsi_pointer->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. strncpy(p1, str, SETUP_BUFFER_SIZE - strlen(setup_buffer));
  1500. setup_buffer[SETUP_BUFFER_SIZE - 1] = '\0';
  1501. p1 = setup_buffer;
  1502. i = 0;
  1503. while (*p1 && (i < MAX_SETUP_ARGS)) {
  1504. p2 = strchr(p1, ',');
  1505. if (p2) {
  1506. *p2 = '\0';
  1507. if (p1 != p2)
  1508. setup_args[i] = p1;
  1509. p1 = p2 + 1;
  1510. i++;
  1511. } else {
  1512. setup_args[i] = p1;
  1513. break;
  1514. }
  1515. }
  1516. for (i = 0; i < MAX_SETUP_ARGS; i++)
  1517. setup_used[i] = 0;
  1518. done_setup = 1;
  1519. return 1;
  1520. }
  1521. __setup("wd33c93=", wd33c93_setup);
  1522. /* check_setup_args() returns index if key found, 0 if not
  1523. */
  1524. static int
  1525. check_setup_args(char *key, int *flags, int *val, char *buf)
  1526. {
  1527. int x;
  1528. char *cp;
  1529. for (x = 0; x < MAX_SETUP_ARGS; x++) {
  1530. if (setup_used[x])
  1531. continue;
  1532. if (!strncmp(setup_args[x], key, strlen(key)))
  1533. break;
  1534. if (!strncmp(setup_args[x], "next", strlen("next")))
  1535. return 0;
  1536. }
  1537. if (x == MAX_SETUP_ARGS)
  1538. return 0;
  1539. setup_used[x] = 1;
  1540. cp = setup_args[x] + strlen(key);
  1541. *val = -1;
  1542. if (*cp != ':')
  1543. return ++x;
  1544. cp++;
  1545. if ((*cp >= '0') && (*cp <= '9')) {
  1546. *val = simple_strtoul(cp, NULL, 0);
  1547. }
  1548. return ++x;
  1549. }
  1550. /*
  1551. * Calculate internal data-transfer-clock cycle from input-clock
  1552. * frequency (/MHz) and fill 'sx_table'.
  1553. *
  1554. * The original driver used to rely on a fixed sx_table, containing periods
  1555. * for (only) the lower limits of the respective input-clock-frequency ranges
  1556. * (8-10/12-15/16-20 MHz). Although it seems, that no problems occurred with
  1557. * this setting so far, it might be desirable to adjust the transfer periods
  1558. * closer to the really attached, possibly 25% higher, input-clock, since
  1559. * - the wd33c93 may really use a significant shorter period, than it has
  1560. * negotiated (eg. thrashing the target, which expects 4/8MHz, with 5/10MHz
  1561. * instead).
  1562. * - the wd33c93 may ask the target for a lower transfer rate, than the target
  1563. * is capable of (eg. negotiating for an assumed minimum of 252ns instead of
  1564. * possible 200ns, which indeed shows up in tests as an approx. 10% lower
  1565. * transfer rate).
  1566. */
  1567. static inline unsigned int
  1568. round_4(unsigned int x)
  1569. {
  1570. switch (x & 3) {
  1571. case 1: --x;
  1572. break;
  1573. case 2: ++x;
  1574. fallthrough;
  1575. case 3: ++x;
  1576. }
  1577. return x;
  1578. }
  1579. static void
  1580. calc_sx_table(unsigned int mhz, struct sx_period sx_table[9])
  1581. {
  1582. unsigned int d, i;
  1583. if (mhz < 11)
  1584. d = 2; /* divisor for 8-10 MHz input-clock */
  1585. else if (mhz < 16)
  1586. d = 3; /* divisor for 12-15 MHz input-clock */
  1587. else
  1588. d = 4; /* divisor for 16-20 MHz input-clock */
  1589. d = (100000 * d) / 2 / mhz; /* 100 x DTCC / nanosec */
  1590. sx_table[0].period_ns = 1;
  1591. sx_table[0].reg_value = 0x20;
  1592. for (i = 1; i < 8; i++) {
  1593. sx_table[i].period_ns = round_4((i+1)*d / 100);
  1594. sx_table[i].reg_value = (i+1)*0x10;
  1595. }
  1596. sx_table[7].reg_value = 0;
  1597. sx_table[8].period_ns = 0;
  1598. sx_table[8].reg_value = 0;
  1599. }
  1600. /*
  1601. * check and, maybe, map an init- or "clock:"- argument.
  1602. */
  1603. static uchar
  1604. set_clk_freq(int freq, int *mhz)
  1605. {
  1606. int x = freq;
  1607. if (WD33C93_FS_8_10 == freq)
  1608. freq = 8;
  1609. else if (WD33C93_FS_12_15 == freq)
  1610. freq = 12;
  1611. else if (WD33C93_FS_16_20 == freq)
  1612. freq = 16;
  1613. else if (freq > 7 && freq < 11)
  1614. x = WD33C93_FS_8_10;
  1615. else if (freq > 11 && freq < 16)
  1616. x = WD33C93_FS_12_15;
  1617. else if (freq > 15 && freq < 21)
  1618. x = WD33C93_FS_16_20;
  1619. else {
  1620. /* Hmm, wouldn't it be safer to assume highest freq here? */
  1621. x = WD33C93_FS_8_10;
  1622. freq = 8;
  1623. }
  1624. *mhz = freq;
  1625. return x;
  1626. }
  1627. /*
  1628. * to be used with the resync: fast: ... options
  1629. */
  1630. static inline void set_resync ( struct WD33C93_hostdata *hd, int mask )
  1631. {
  1632. int i;
  1633. for (i = 0; i < 8; i++)
  1634. if (mask & (1 << i))
  1635. hd->sync_stat[i] = SS_UNSET;
  1636. }
  1637. void
  1638. wd33c93_init(struct Scsi_Host *instance, const wd33c93_regs regs,
  1639. dma_setup_t setup, dma_stop_t stop, int clock_freq)
  1640. {
  1641. struct WD33C93_hostdata *hostdata;
  1642. int i;
  1643. int flags;
  1644. int val;
  1645. char buf[32];
  1646. if (!done_setup && setup_strings)
  1647. wd33c93_setup(setup_strings);
  1648. hostdata = (struct WD33C93_hostdata *) instance->hostdata;
  1649. hostdata->regs = regs;
  1650. hostdata->clock_freq = set_clk_freq(clock_freq, &i);
  1651. calc_sx_table(i, hostdata->sx_table);
  1652. hostdata->dma_setup = setup;
  1653. hostdata->dma_stop = stop;
  1654. hostdata->dma_bounce_buffer = NULL;
  1655. hostdata->dma_bounce_len = 0;
  1656. for (i = 0; i < 8; i++) {
  1657. hostdata->busy[i] = 0;
  1658. hostdata->sync_xfer[i] =
  1659. calc_sync_xfer(DEFAULT_SX_PER / 4, DEFAULT_SX_OFF,
  1660. 0, hostdata->sx_table);
  1661. hostdata->sync_stat[i] = SS_UNSET; /* using default sync values */
  1662. #ifdef PROC_STATISTICS
  1663. hostdata->cmd_cnt[i] = 0;
  1664. hostdata->disc_allowed_cnt[i] = 0;
  1665. hostdata->disc_done_cnt[i] = 0;
  1666. #endif
  1667. }
  1668. hostdata->input_Q = NULL;
  1669. hostdata->selecting = NULL;
  1670. hostdata->connected = NULL;
  1671. hostdata->disconnected_Q = NULL;
  1672. hostdata->state = S_UNCONNECTED;
  1673. hostdata->dma = D_DMA_OFF;
  1674. hostdata->level2 = L2_BASIC;
  1675. hostdata->disconnect = DIS_ADAPTIVE;
  1676. hostdata->args = DEBUG_DEFAULTS;
  1677. hostdata->incoming_ptr = 0;
  1678. hostdata->outgoing_len = 0;
  1679. hostdata->default_sx_per = DEFAULT_SX_PER;
  1680. hostdata->no_dma = 0; /* default is DMA enabled */
  1681. #ifdef PROC_INTERFACE
  1682. hostdata->proc = PR_VERSION | PR_INFO | PR_STATISTICS |
  1683. PR_CONNECTED | PR_INPUTQ | PR_DISCQ | PR_STOP;
  1684. #ifdef PROC_STATISTICS
  1685. hostdata->dma_cnt = 0;
  1686. hostdata->pio_cnt = 0;
  1687. hostdata->int_cnt = 0;
  1688. #endif
  1689. #endif
  1690. if (check_setup_args("clock", &flags, &val, buf)) {
  1691. hostdata->clock_freq = set_clk_freq(val, &val);
  1692. calc_sx_table(val, hostdata->sx_table);
  1693. }
  1694. if (check_setup_args("nosync", &flags, &val, buf))
  1695. hostdata->no_sync = val;
  1696. if (check_setup_args("nodma", &flags, &val, buf))
  1697. hostdata->no_dma = (val == -1) ? 1 : val;
  1698. if (check_setup_args("period", &flags, &val, buf))
  1699. hostdata->default_sx_per =
  1700. hostdata->sx_table[round_period((unsigned int) val,
  1701. hostdata->sx_table)].period_ns;
  1702. if (check_setup_args("disconnect", &flags, &val, buf)) {
  1703. if ((val >= DIS_NEVER) && (val <= DIS_ALWAYS))
  1704. hostdata->disconnect = val;
  1705. else
  1706. hostdata->disconnect = DIS_ADAPTIVE;
  1707. }
  1708. if (check_setup_args("level2", &flags, &val, buf))
  1709. hostdata->level2 = val;
  1710. if (check_setup_args("debug", &flags, &val, buf))
  1711. hostdata->args = val & DB_MASK;
  1712. if (check_setup_args("burst", &flags, &val, buf))
  1713. hostdata->dma_mode = val ? CTRL_BURST:CTRL_DMA;
  1714. if (WD33C93_FS_16_20 == hostdata->clock_freq /* divisor 4 */
  1715. && check_setup_args("fast", &flags, &val, buf))
  1716. hostdata->fast = !!val;
  1717. if ((i = check_setup_args("next", &flags, &val, buf))) {
  1718. while (i)
  1719. setup_used[--i] = 1;
  1720. }
  1721. #ifdef PROC_INTERFACE
  1722. if (check_setup_args("proc", &flags, &val, buf))
  1723. hostdata->proc = val;
  1724. #endif
  1725. spin_lock_irq(&hostdata->lock);
  1726. reset_wd33c93(instance);
  1727. spin_unlock_irq(&hostdata->lock);
  1728. printk("wd33c93-%d: chip=%s/%d no_sync=0x%x no_dma=%d",
  1729. instance->host_no,
  1730. (hostdata->chip == C_WD33C93) ? "WD33c93" : (hostdata->chip ==
  1731. C_WD33C93A) ?
  1732. "WD33c93A" : (hostdata->chip ==
  1733. C_WD33C93B) ? "WD33c93B" : "unknown",
  1734. hostdata->microcode, hostdata->no_sync, hostdata->no_dma);
  1735. #ifdef DEBUGGING_ON
  1736. printk(" debug_flags=0x%02x\n", hostdata->args);
  1737. #else
  1738. printk(" debugging=OFF\n");
  1739. #endif
  1740. printk(" setup_args=");
  1741. for (i = 0; i < MAX_SETUP_ARGS; i++)
  1742. printk("%s,", setup_args[i]);
  1743. printk("\n");
  1744. printk(" Version %s - %s\n", WD33C93_VERSION, WD33C93_DATE);
  1745. }
  1746. int wd33c93_write_info(struct Scsi_Host *instance, char *buf, int len)
  1747. {
  1748. #ifdef PROC_INTERFACE
  1749. char *bp;
  1750. struct WD33C93_hostdata *hd;
  1751. int x;
  1752. hd = (struct WD33C93_hostdata *) instance->hostdata;
  1753. /* We accept the following
  1754. * keywords (same format as command-line, but arguments are not optional):
  1755. * debug
  1756. * disconnect
  1757. * period
  1758. * resync
  1759. * proc
  1760. * nodma
  1761. * level2
  1762. * burst
  1763. * fast
  1764. * nosync
  1765. */
  1766. buf[len] = '\0';
  1767. for (bp = buf; *bp; ) {
  1768. while (',' == *bp || ' ' == *bp)
  1769. ++bp;
  1770. if (!strncmp(bp, "debug:", 6)) {
  1771. hd->args = simple_strtoul(bp+6, &bp, 0) & DB_MASK;
  1772. } else if (!strncmp(bp, "disconnect:", 11)) {
  1773. x = simple_strtoul(bp+11, &bp, 0);
  1774. if (x < DIS_NEVER || x > DIS_ALWAYS)
  1775. x = DIS_ADAPTIVE;
  1776. hd->disconnect = x;
  1777. } else if (!strncmp(bp, "period:", 7)) {
  1778. x = simple_strtoul(bp+7, &bp, 0);
  1779. hd->default_sx_per =
  1780. hd->sx_table[round_period((unsigned int) x,
  1781. hd->sx_table)].period_ns;
  1782. } else if (!strncmp(bp, "resync:", 7)) {
  1783. set_resync(hd, (int)simple_strtoul(bp+7, &bp, 0));
  1784. } else if (!strncmp(bp, "proc:", 5)) {
  1785. hd->proc = simple_strtoul(bp+5, &bp, 0);
  1786. } else if (!strncmp(bp, "nodma:", 6)) {
  1787. hd->no_dma = simple_strtoul(bp+6, &bp, 0);
  1788. } else if (!strncmp(bp, "level2:", 7)) {
  1789. hd->level2 = simple_strtoul(bp+7, &bp, 0);
  1790. } else if (!strncmp(bp, "burst:", 6)) {
  1791. hd->dma_mode =
  1792. simple_strtol(bp+6, &bp, 0) ? CTRL_BURST:CTRL_DMA;
  1793. } else if (!strncmp(bp, "fast:", 5)) {
  1794. x = !!simple_strtol(bp+5, &bp, 0);
  1795. if (x != hd->fast)
  1796. set_resync(hd, 0xff);
  1797. hd->fast = x;
  1798. } else if (!strncmp(bp, "nosync:", 7)) {
  1799. x = simple_strtoul(bp+7, &bp, 0);
  1800. set_resync(hd, x ^ hd->no_sync);
  1801. hd->no_sync = x;
  1802. } else {
  1803. break; /* unknown keyword,syntax-error,... */
  1804. }
  1805. }
  1806. return len;
  1807. #else
  1808. return 0;
  1809. #endif
  1810. }
  1811. int
  1812. wd33c93_show_info(struct seq_file *m, struct Scsi_Host *instance)
  1813. {
  1814. #ifdef PROC_INTERFACE
  1815. struct WD33C93_hostdata *hd;
  1816. struct scsi_cmnd *cmd;
  1817. int x;
  1818. hd = (struct WD33C93_hostdata *) instance->hostdata;
  1819. spin_lock_irq(&hd->lock);
  1820. if (hd->proc & PR_VERSION)
  1821. seq_printf(m, "\nVersion %s - %s.",
  1822. WD33C93_VERSION, WD33C93_DATE);
  1823. if (hd->proc & PR_INFO) {
  1824. seq_printf(m, "\nclock_freq=%02x no_sync=%02x no_dma=%d"
  1825. " dma_mode=%02x fast=%d",
  1826. hd->clock_freq, hd->no_sync, hd->no_dma, hd->dma_mode, hd->fast);
  1827. seq_puts(m, "\nsync_xfer[] = ");
  1828. for (x = 0; x < 7; x++)
  1829. seq_printf(m, "\t%02x", hd->sync_xfer[x]);
  1830. seq_puts(m, "\nsync_stat[] = ");
  1831. for (x = 0; x < 7; x++)
  1832. seq_printf(m, "\t%02x", hd->sync_stat[x]);
  1833. }
  1834. #ifdef PROC_STATISTICS
  1835. if (hd->proc & PR_STATISTICS) {
  1836. seq_puts(m, "\ncommands issued: ");
  1837. for (x = 0; x < 7; x++)
  1838. seq_printf(m, "\t%ld", hd->cmd_cnt[x]);
  1839. seq_puts(m, "\ndisconnects allowed:");
  1840. for (x = 0; x < 7; x++)
  1841. seq_printf(m, "\t%ld", hd->disc_allowed_cnt[x]);
  1842. seq_puts(m, "\ndisconnects done: ");
  1843. for (x = 0; x < 7; x++)
  1844. seq_printf(m, "\t%ld", hd->disc_done_cnt[x]);
  1845. seq_printf(m,
  1846. "\ninterrupts: %ld, DATA_PHASE ints: %ld DMA, %ld PIO",
  1847. hd->int_cnt, hd->dma_cnt, hd->pio_cnt);
  1848. }
  1849. #endif
  1850. if (hd->proc & PR_CONNECTED) {
  1851. seq_puts(m, "\nconnected: ");
  1852. if (hd->connected) {
  1853. cmd = (struct scsi_cmnd *) hd->connected;
  1854. seq_printf(m, " %d:%llu(%02x)",
  1855. cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
  1856. }
  1857. }
  1858. if (hd->proc & PR_INPUTQ) {
  1859. seq_puts(m, "\ninput_Q: ");
  1860. cmd = (struct scsi_cmnd *) hd->input_Q;
  1861. while (cmd) {
  1862. seq_printf(m, " %d:%llu(%02x)",
  1863. cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
  1864. cmd = (struct scsi_cmnd *) cmd->host_scribble;
  1865. }
  1866. }
  1867. if (hd->proc & PR_DISCQ) {
  1868. seq_puts(m, "\ndisconnected_Q:");
  1869. cmd = (struct scsi_cmnd *) hd->disconnected_Q;
  1870. while (cmd) {
  1871. seq_printf(m, " %d:%llu(%02x)",
  1872. cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
  1873. cmd = (struct scsi_cmnd *) cmd->host_scribble;
  1874. }
  1875. }
  1876. seq_putc(m, '\n');
  1877. spin_unlock_irq(&hd->lock);
  1878. #endif /* PROC_INTERFACE */
  1879. return 0;
  1880. }
  1881. EXPORT_SYMBOL(wd33c93_host_reset);
  1882. EXPORT_SYMBOL(wd33c93_init);
  1883. EXPORT_SYMBOL(wd33c93_abort);
  1884. EXPORT_SYMBOL(wd33c93_queuecommand);
  1885. EXPORT_SYMBOL(wd33c93_intr);
  1886. EXPORT_SYMBOL(wd33c93_show_info);
  1887. EXPORT_SYMBOL(wd33c93_write_info);