wd33c93.c 61 KB

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