wd33c93.c 61 KB

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