acornscsi.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129
  1. /*
  2. * linux/drivers/acorn/scsi/acornscsi.c
  3. *
  4. * Acorn SCSI 3 driver
  5. * By R.M.King.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Abandoned using the Select and Transfer command since there were
  12. * some nasty races between our software and the target devices that
  13. * were not easy to solve, and the device errata had a lot of entries
  14. * for this command, some of them quite nasty...
  15. *
  16. * Changelog:
  17. * 26-Sep-1997 RMK Re-jigged to use the queue module.
  18. * Re-coded state machine to be based on driver
  19. * state not scsi state. Should be easier to debug.
  20. * Added acornscsi_release to clean up properly.
  21. * Updated proc/scsi reporting.
  22. * 05-Oct-1997 RMK Implemented writing to SCSI devices.
  23. * 06-Oct-1997 RMK Corrected small (non-serious) bug with the connect/
  24. * reconnect race condition causing a warning message.
  25. * 12-Oct-1997 RMK Added catch for re-entering interrupt routine.
  26. * 15-Oct-1997 RMK Improved handling of commands.
  27. * 27-Jun-1998 RMK Changed asm/delay.h to linux/delay.h.
  28. * 13-Dec-1998 RMK Better abort code and command handling. Extra state
  29. * transitions added to allow dodgy devices to work.
  30. */
  31. #define DEBUG_NO_WRITE 1
  32. #define DEBUG_QUEUES 2
  33. #define DEBUG_DMA 4
  34. #define DEBUG_ABORT 8
  35. #define DEBUG_DISCON 16
  36. #define DEBUG_CONNECT 32
  37. #define DEBUG_PHASES 64
  38. #define DEBUG_WRITE 128
  39. #define DEBUG_LINK 256
  40. #define DEBUG_MESSAGES 512
  41. #define DEBUG_RESET 1024
  42. #define DEBUG_ALL (DEBUG_RESET|DEBUG_MESSAGES|DEBUG_LINK|DEBUG_WRITE|\
  43. DEBUG_PHASES|DEBUG_CONNECT|DEBUG_DISCON|DEBUG_ABORT|\
  44. DEBUG_DMA|DEBUG_QUEUES)
  45. /* DRIVER CONFIGURATION
  46. *
  47. * SCSI-II Tagged queue support.
  48. *
  49. * I don't have any SCSI devices that support it, so it is totally untested
  50. * (except to make sure that it doesn't interfere with any non-tagging
  51. * devices). It is not fully implemented either - what happens when a
  52. * tagging device reconnects???
  53. *
  54. * You can tell if you have a device that supports tagged queueing my
  55. * cating (eg) /proc/scsi/acornscsi/0 and see if the SCSI revision is reported
  56. * as '2 TAG'.
  57. *
  58. * Also note that CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE is normally set in the config
  59. * scripts, but disabled here. Once debugged, remove the #undef, otherwise to debug,
  60. * comment out the undef.
  61. */
  62. #undef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
  63. /*
  64. * SCSI-II Linked command support.
  65. *
  66. * The higher level code doesn't support linked commands yet, and so the option
  67. * is undef'd here.
  68. */
  69. #undef CONFIG_SCSI_ACORNSCSI_LINK
  70. /*
  71. * SCSI-II Synchronous transfer support.
  72. *
  73. * Tried and tested...
  74. *
  75. * SDTR_SIZE - maximum number of un-acknowledged bytes (0 = off, 12 = max)
  76. * SDTR_PERIOD - period of REQ signal (min=125, max=1020)
  77. * DEFAULT_PERIOD - default REQ period.
  78. */
  79. #define SDTR_SIZE 12
  80. #define SDTR_PERIOD 125
  81. #define DEFAULT_PERIOD 500
  82. /*
  83. * Debugging information
  84. *
  85. * DEBUG - bit mask from list above
  86. * DEBUG_TARGET - is defined to the target number if you want to debug
  87. * a specific target. [only recon/write/dma].
  88. */
  89. #define DEBUG (DEBUG_RESET|DEBUG_WRITE|DEBUG_NO_WRITE)
  90. /* only allow writing to SCSI device 0 */
  91. #define NO_WRITE 0xFE
  92. /*#define DEBUG_TARGET 2*/
  93. /*
  94. * Select timeout time (in 10ms units)
  95. *
  96. * This is the timeout used between the start of selection and the WD33C93
  97. * chip deciding that the device isn't responding.
  98. */
  99. #define TIMEOUT_TIME 10
  100. /*
  101. * Define this if you want to have verbose explaination of SCSI
  102. * status/messages.
  103. */
  104. #undef CONFIG_ACORNSCSI_CONSTANTS
  105. /*
  106. * Define this if you want to use the on board DMAC [don't remove this option]
  107. * If not set, then use PIO mode (not currently supported).
  108. */
  109. #define USE_DMAC
  110. /*
  111. * ====================================================================================
  112. */
  113. #ifdef DEBUG_TARGET
  114. #define DBG(cmd,xxx...) \
  115. if (cmd->device->id == DEBUG_TARGET) { \
  116. xxx; \
  117. }
  118. #else
  119. #define DBG(cmd,xxx...) xxx
  120. #endif
  121. #ifndef STRINGIFY
  122. #define STRINGIFY(x) #x
  123. #endif
  124. #define STRx(x) STRINGIFY(x)
  125. #define NO_WRITE_STR STRx(NO_WRITE)
  126. #include <linux/module.h>
  127. #include <linux/kernel.h>
  128. #include <linux/string.h>
  129. #include <linux/signal.h>
  130. #include <linux/errno.h>
  131. #include <linux/proc_fs.h>
  132. #include <linux/ioport.h>
  133. #include <linux/blkdev.h>
  134. #include <linux/delay.h>
  135. #include <linux/interrupt.h>
  136. #include <linux/init.h>
  137. #include <linux/bitops.h>
  138. #include <asm/system.h>
  139. #include <asm/io.h>
  140. #include <asm/ecard.h>
  141. #include "../scsi.h"
  142. #include <scsi/scsi_dbg.h>
  143. #include <scsi/scsi_host.h>
  144. #include <scsi/scsi_transport_spi.h>
  145. #include "acornscsi.h"
  146. #include "msgqueue.h"
  147. #include "scsi.h"
  148. #include <scsi/scsicam.h>
  149. #define VER_MAJOR 2
  150. #define VER_MINOR 0
  151. #define VER_PATCH 6
  152. #ifndef ABORT_TAG
  153. #define ABORT_TAG 0xd
  154. #else
  155. #error "Yippee! ABORT TAG is now defined! Remove this error!"
  156. #endif
  157. #ifdef CONFIG_SCSI_ACORNSCSI_LINK
  158. #error SCSI2 LINKed commands not supported (yet)!
  159. #endif
  160. #ifdef USE_DMAC
  161. /*
  162. * DMAC setup parameters
  163. */
  164. #define INIT_DEVCON0 (DEVCON0_RQL|DEVCON0_EXW|DEVCON0_CMP)
  165. #define INIT_DEVCON1 (DEVCON1_BHLD)
  166. #define DMAC_READ (MODECON_READ)
  167. #define DMAC_WRITE (MODECON_WRITE)
  168. #define INIT_SBICDMA (CTRL_DMABURST)
  169. #define scsi_xferred have_data_in
  170. /*
  171. * Size of on-board DMA buffer
  172. */
  173. #define DMAC_BUFFER_SIZE 65536
  174. #endif
  175. #define STATUS_BUFFER_TO_PRINT 24
  176. unsigned int sdtr_period = SDTR_PERIOD;
  177. unsigned int sdtr_size = SDTR_SIZE;
  178. static void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp,
  179. unsigned int result);
  180. static int acornscsi_reconnect_finish(AS_Host *host);
  181. static void acornscsi_dma_cleanup(AS_Host *host);
  182. static void acornscsi_abortcmd(AS_Host *host, unsigned char tag);
  183. /* ====================================================================================
  184. * Miscellaneous
  185. */
  186. static inline void
  187. sbic_arm_write(unsigned int io_port, int reg, int value)
  188. {
  189. __raw_writeb(reg, io_port);
  190. __raw_writeb(value, io_port + 4);
  191. }
  192. #define sbic_arm_writenext(io,val) \
  193. __raw_writeb((val), (io) + 4)
  194. static inline
  195. int sbic_arm_read(unsigned int io_port, int reg)
  196. {
  197. if(reg == SBIC_ASR)
  198. return __raw_readl(io_port) & 255;
  199. __raw_writeb(reg, io_port);
  200. return __raw_readl(io_port + 4) & 255;
  201. }
  202. #define sbic_arm_readnext(io) \
  203. __raw_readb((io) + 4)
  204. #ifdef USE_DMAC
  205. #define dmac_read(io_port,reg) \
  206. inb((io_port) + (reg))
  207. #define dmac_write(io_port,reg,value) \
  208. ({ outb((value), (io_port) + (reg)); })
  209. #define dmac_clearintr(io_port) \
  210. ({ outb(0, (io_port)); })
  211. static inline
  212. unsigned int dmac_address(unsigned int io_port)
  213. {
  214. return dmac_read(io_port, DMAC_TXADRHI) << 16 |
  215. dmac_read(io_port, DMAC_TXADRMD) << 8 |
  216. dmac_read(io_port, DMAC_TXADRLO);
  217. }
  218. static
  219. void acornscsi_dumpdma(AS_Host *host, char *where)
  220. {
  221. unsigned int mode, addr, len;
  222. mode = dmac_read(host->dma.io_port, DMAC_MODECON);
  223. addr = dmac_address(host->dma.io_port);
  224. len = dmac_read(host->dma.io_port, DMAC_TXCNTHI) << 8 |
  225. dmac_read(host->dma.io_port, DMAC_TXCNTLO);
  226. printk("scsi%d: %s: DMAC %02x @%06x+%04x msk %02x, ",
  227. host->host->host_no, where,
  228. mode, addr, (len + 1) & 0xffff,
  229. dmac_read(host->dma.io_port, DMAC_MASKREG));
  230. printk("DMA @%06x, ", host->dma.start_addr);
  231. printk("BH @%p +%04x, ", host->scsi.SCp.ptr,
  232. host->scsi.SCp.this_residual);
  233. printk("DT @+%04x ST @+%04x", host->dma.transferred,
  234. host->scsi.SCp.scsi_xferred);
  235. printk("\n");
  236. }
  237. #endif
  238. static
  239. unsigned long acornscsi_sbic_xfcount(AS_Host *host)
  240. {
  241. unsigned long length;
  242. length = sbic_arm_read(host->scsi.io_port, SBIC_TRANSCNTH) << 16;
  243. length |= sbic_arm_readnext(host->scsi.io_port) << 8;
  244. length |= sbic_arm_readnext(host->scsi.io_port);
  245. return length;
  246. }
  247. static int
  248. acornscsi_sbic_wait(AS_Host *host, int stat_mask, int stat, int timeout, char *msg)
  249. {
  250. int asr;
  251. do {
  252. asr = sbic_arm_read(host->scsi.io_port, SBIC_ASR);
  253. if ((asr & stat_mask) == stat)
  254. return 0;
  255. udelay(1);
  256. } while (--timeout);
  257. printk("scsi%d: timeout while %s\n", host->host->host_no, msg);
  258. return -1;
  259. }
  260. static
  261. int acornscsi_sbic_issuecmd(AS_Host *host, int command)
  262. {
  263. if (acornscsi_sbic_wait(host, ASR_CIP, 0, 1000, "issuing command"))
  264. return -1;
  265. sbic_arm_write(host->scsi.io_port, SBIC_CMND, command);
  266. return 0;
  267. }
  268. static void
  269. acornscsi_csdelay(unsigned int cs)
  270. {
  271. unsigned long target_jiffies, flags;
  272. target_jiffies = jiffies + 1 + cs * HZ / 100;
  273. local_save_flags(flags);
  274. local_irq_enable();
  275. while (time_before(jiffies, target_jiffies)) barrier();
  276. local_irq_restore(flags);
  277. }
  278. static
  279. void acornscsi_resetcard(AS_Host *host)
  280. {
  281. unsigned int i, timeout;
  282. /* assert reset line */
  283. host->card.page_reg = 0x80;
  284. outb(host->card.page_reg, host->card.io_page);
  285. /* wait 3 cs. SCSI standard says 25ms. */
  286. acornscsi_csdelay(3);
  287. host->card.page_reg = 0;
  288. outb(host->card.page_reg, host->card.io_page);
  289. /*
  290. * Should get a reset from the card
  291. */
  292. timeout = 1000;
  293. do {
  294. if (inb(host->card.io_intr) & 8)
  295. break;
  296. udelay(1);
  297. } while (--timeout);
  298. if (timeout == 0)
  299. printk("scsi%d: timeout while resetting card\n",
  300. host->host->host_no);
  301. sbic_arm_read(host->scsi.io_port, SBIC_ASR);
  302. sbic_arm_read(host->scsi.io_port, SBIC_SSR);
  303. /* setup sbic - WD33C93A */
  304. sbic_arm_write(host->scsi.io_port, SBIC_OWNID, OWNID_EAF | host->host->this_id);
  305. sbic_arm_write(host->scsi.io_port, SBIC_CMND, CMND_RESET);
  306. /*
  307. * Command should cause a reset interrupt
  308. */
  309. timeout = 1000;
  310. do {
  311. if (inb(host->card.io_intr) & 8)
  312. break;
  313. udelay(1);
  314. } while (--timeout);
  315. if (timeout == 0)
  316. printk("scsi%d: timeout while resetting card\n",
  317. host->host->host_no);
  318. sbic_arm_read(host->scsi.io_port, SBIC_ASR);
  319. if (sbic_arm_read(host->scsi.io_port, SBIC_SSR) != 0x01)
  320. printk(KERN_CRIT "scsi%d: WD33C93A didn't give enhanced reset interrupt\n",
  321. host->host->host_no);
  322. sbic_arm_write(host->scsi.io_port, SBIC_CTRL, INIT_SBICDMA | CTRL_IDI);
  323. sbic_arm_write(host->scsi.io_port, SBIC_TIMEOUT, TIMEOUT_TIME);
  324. sbic_arm_write(host->scsi.io_port, SBIC_SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
  325. sbic_arm_write(host->scsi.io_port, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
  326. host->card.page_reg = 0x40;
  327. outb(host->card.page_reg, host->card.io_page);
  328. /* setup dmac - uPC71071 */
  329. dmac_write(host->dma.io_port, DMAC_INIT, 0);
  330. #ifdef USE_DMAC
  331. dmac_write(host->dma.io_port, DMAC_INIT, INIT_8BIT);
  332. dmac_write(host->dma.io_port, DMAC_CHANNEL, CHANNEL_0);
  333. dmac_write(host->dma.io_port, DMAC_DEVCON0, INIT_DEVCON0);
  334. dmac_write(host->dma.io_port, DMAC_DEVCON1, INIT_DEVCON1);
  335. #endif
  336. host->SCpnt = NULL;
  337. host->scsi.phase = PHASE_IDLE;
  338. host->scsi.disconnectable = 0;
  339. memset(host->busyluns, 0, sizeof(host->busyluns));
  340. for (i = 0; i < 8; i++) {
  341. host->device[i].sync_state = SYNC_NEGOCIATE;
  342. host->device[i].disconnect_ok = 1;
  343. }
  344. /* wait 25 cs. SCSI standard says 250ms. */
  345. acornscsi_csdelay(25);
  346. }
  347. /*=============================================================================================
  348. * Utility routines (eg. debug)
  349. */
  350. #ifdef CONFIG_ACORNSCSI_CONSTANTS
  351. static char *acornscsi_interrupttype[] = {
  352. "rst", "suc", "p/a", "3",
  353. "term", "5", "6", "7",
  354. "serv", "9", "a", "b",
  355. "c", "d", "e", "f"
  356. };
  357. static signed char acornscsi_map[] = {
  358. 0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  359. -1, 2, -1, -1, -1, -1, 3, -1, 4, 5, 6, 7, 8, 9, 10, 11,
  360. 12, 13, 14, -1, -1, -1, -1, -1, 4, 5, 6, 7, 8, 9, 10, 11,
  361. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  362. 15, 16, 17, 18, 19, -1, -1, 20, 4, 5, 6, 7, 8, 9, 10, 11,
  363. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  364. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  365. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  366. 21, 22, -1, -1, -1, 23, -1, -1, 4, 5, 6, 7, 8, 9, 10, 11,
  367. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  368. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  369. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  370. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  371. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  372. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  373. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  374. };
  375. static char *acornscsi_interruptcode[] = {
  376. /* 0 */
  377. "reset - normal mode", /* 00 */
  378. "reset - advanced mode", /* 01 */
  379. /* 2 */
  380. "sel", /* 11 */
  381. "sel+xfer", /* 16 */
  382. "data-out", /* 18 */
  383. "data-in", /* 19 */
  384. "cmd", /* 1A */
  385. "stat", /* 1B */
  386. "??-out", /* 1C */
  387. "??-in", /* 1D */
  388. "msg-out", /* 1E */
  389. "msg-in", /* 1F */
  390. /* 12 */
  391. "/ACK asserted", /* 20 */
  392. "save-data-ptr", /* 21 */
  393. "{re}sel", /* 22 */
  394. /* 15 */
  395. "inv cmd", /* 40 */
  396. "unexpected disconnect", /* 41 */
  397. "sel timeout", /* 42 */
  398. "P err", /* 43 */
  399. "P err+ATN", /* 44 */
  400. "bad status byte", /* 47 */
  401. /* 21 */
  402. "resel, no id", /* 80 */
  403. "resel", /* 81 */
  404. "discon", /* 85 */
  405. };
  406. static
  407. void print_scsi_status(unsigned int ssr)
  408. {
  409. if (acornscsi_map[ssr] != -1)
  410. printk("%s:%s",
  411. acornscsi_interrupttype[(ssr >> 4)],
  412. acornscsi_interruptcode[acornscsi_map[ssr]]);
  413. else
  414. printk("%X:%X", ssr >> 4, ssr & 0x0f);
  415. }
  416. #endif
  417. static
  418. void print_sbic_status(int asr, int ssr, int cmdphase)
  419. {
  420. #ifdef CONFIG_ACORNSCSI_CONSTANTS
  421. printk("sbic: %c%c%c%c%c%c ",
  422. asr & ASR_INT ? 'I' : 'i',
  423. asr & ASR_LCI ? 'L' : 'l',
  424. asr & ASR_BSY ? 'B' : 'b',
  425. asr & ASR_CIP ? 'C' : 'c',
  426. asr & ASR_PE ? 'P' : 'p',
  427. asr & ASR_DBR ? 'D' : 'd');
  428. printk("scsi: ");
  429. print_scsi_status(ssr);
  430. printk(" ph %02X\n", cmdphase);
  431. #else
  432. printk("sbic: %02X scsi: %X:%X ph: %02X\n",
  433. asr, (ssr & 0xf0)>>4, ssr & 0x0f, cmdphase);
  434. #endif
  435. }
  436. static void
  437. acornscsi_dumplogline(AS_Host *host, int target, int line)
  438. {
  439. unsigned long prev;
  440. signed int ptr;
  441. ptr = host->status_ptr[target] - STATUS_BUFFER_TO_PRINT;
  442. if (ptr < 0)
  443. ptr += STATUS_BUFFER_SIZE;
  444. printk("%c: %3s:", target == 8 ? 'H' : '0' + target,
  445. line == 0 ? "ph" : line == 1 ? "ssr" : "int");
  446. prev = host->status[target][ptr].when;
  447. for (; ptr != host->status_ptr[target]; ptr = (ptr + 1) & (STATUS_BUFFER_SIZE - 1)) {
  448. unsigned long time_diff;
  449. if (!host->status[target][ptr].when)
  450. continue;
  451. switch (line) {
  452. case 0:
  453. printk("%c%02X", host->status[target][ptr].irq ? '-' : ' ',
  454. host->status[target][ptr].ph);
  455. break;
  456. case 1:
  457. printk(" %02X", host->status[target][ptr].ssr);
  458. break;
  459. case 2:
  460. time_diff = host->status[target][ptr].when - prev;
  461. prev = host->status[target][ptr].when;
  462. if (time_diff == 0)
  463. printk("==^");
  464. else if (time_diff >= 100)
  465. printk(" ");
  466. else
  467. printk(" %02ld", time_diff);
  468. break;
  469. }
  470. }
  471. printk("\n");
  472. }
  473. static
  474. void acornscsi_dumplog(AS_Host *host, int target)
  475. {
  476. do {
  477. acornscsi_dumplogline(host, target, 0);
  478. acornscsi_dumplogline(host, target, 1);
  479. acornscsi_dumplogline(host, target, 2);
  480. if (target == 8)
  481. break;
  482. target = 8;
  483. } while (1);
  484. }
  485. static
  486. char acornscsi_target(AS_Host *host)
  487. {
  488. if (host->SCpnt)
  489. return '0' + host->SCpnt->device->id;
  490. return 'H';
  491. }
  492. /*
  493. * Prototype: cmdtype_t acornscsi_cmdtype(int command)
  494. * Purpose : differentiate READ from WRITE from other commands
  495. * Params : command - command to interpret
  496. * Returns : CMD_READ - command reads data,
  497. * CMD_WRITE - command writes data,
  498. * CMD_MISC - everything else
  499. */
  500. static inline
  501. cmdtype_t acornscsi_cmdtype(int command)
  502. {
  503. switch (command) {
  504. case WRITE_6: case WRITE_10: case WRITE_12:
  505. return CMD_WRITE;
  506. case READ_6: case READ_10: case READ_12:
  507. return CMD_READ;
  508. default:
  509. return CMD_MISC;
  510. }
  511. }
  512. /*
  513. * Prototype: int acornscsi_datadirection(int command)
  514. * Purpose : differentiate between commands that have a DATA IN phase
  515. * and a DATA OUT phase
  516. * Params : command - command to interpret
  517. * Returns : DATADIR_OUT - data out phase expected
  518. * DATADIR_IN - data in phase expected
  519. */
  520. static
  521. datadir_t acornscsi_datadirection(int command)
  522. {
  523. switch (command) {
  524. case CHANGE_DEFINITION: case COMPARE: case COPY:
  525. case COPY_VERIFY: case LOG_SELECT: case MODE_SELECT:
  526. case MODE_SELECT_10: case SEND_DIAGNOSTIC: case WRITE_BUFFER:
  527. case FORMAT_UNIT: case REASSIGN_BLOCKS: case RESERVE:
  528. case SEARCH_EQUAL: case SEARCH_HIGH: case SEARCH_LOW:
  529. case WRITE_6: case WRITE_10: case WRITE_VERIFY:
  530. case UPDATE_BLOCK: case WRITE_LONG: case WRITE_SAME:
  531. case SEARCH_HIGH_12: case SEARCH_EQUAL_12: case SEARCH_LOW_12:
  532. case WRITE_12: case WRITE_VERIFY_12: case SET_WINDOW:
  533. case MEDIUM_SCAN: case SEND_VOLUME_TAG: case 0xea:
  534. return DATADIR_OUT;
  535. default:
  536. return DATADIR_IN;
  537. }
  538. }
  539. /*
  540. * Purpose : provide values for synchronous transfers with 33C93.
  541. * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
  542. * Modified by Russell King for 8MHz WD33C93A
  543. */
  544. static struct sync_xfer_tbl {
  545. unsigned int period_ns;
  546. unsigned char reg_value;
  547. } sync_xfer_table[] = {
  548. { 1, 0x20 }, { 249, 0x20 }, { 374, 0x30 },
  549. { 499, 0x40 }, { 624, 0x50 }, { 749, 0x60 },
  550. { 874, 0x70 }, { 999, 0x00 }, { 0, 0 }
  551. };
  552. /*
  553. * Prototype: int acornscsi_getperiod(unsigned char syncxfer)
  554. * Purpose : period for the synchronous transfer setting
  555. * Params : syncxfer SYNCXFER register value
  556. * Returns : period in ns.
  557. */
  558. static
  559. int acornscsi_getperiod(unsigned char syncxfer)
  560. {
  561. int i;
  562. syncxfer &= 0xf0;
  563. if (syncxfer == 0x10)
  564. syncxfer = 0;
  565. for (i = 1; sync_xfer_table[i].period_ns; i++)
  566. if (syncxfer == sync_xfer_table[i].reg_value)
  567. return sync_xfer_table[i].period_ns;
  568. return 0;
  569. }
  570. /*
  571. * Prototype: int round_period(unsigned int period)
  572. * Purpose : return index into above table for a required REQ period
  573. * Params : period - time (ns) for REQ
  574. * Returns : table index
  575. * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
  576. */
  577. static inline
  578. int round_period(unsigned int period)
  579. {
  580. int i;
  581. for (i = 1; sync_xfer_table[i].period_ns; i++) {
  582. if ((period <= sync_xfer_table[i].period_ns) &&
  583. (period > sync_xfer_table[i - 1].period_ns))
  584. return i;
  585. }
  586. return 7;
  587. }
  588. /*
  589. * Prototype: unsigned char calc_sync_xfer(unsigned int period, unsigned int offset)
  590. * Purpose : calculate value for 33c93s SYNC register
  591. * Params : period - time (ns) for REQ
  592. * offset - offset in bytes between REQ/ACK
  593. * Returns : value for SYNC register
  594. * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
  595. */
  596. static
  597. unsigned char calc_sync_xfer(unsigned int period, unsigned int offset)
  598. {
  599. return sync_xfer_table[round_period(period)].reg_value |
  600. ((offset < SDTR_SIZE) ? offset : SDTR_SIZE);
  601. }
  602. /* ====================================================================================
  603. * Command functions
  604. */
  605. /*
  606. * Function: acornscsi_kick(AS_Host *host)
  607. * Purpose : kick next command to interface
  608. * Params : host - host to send command to
  609. * Returns : INTR_IDLE if idle, otherwise INTR_PROCESSING
  610. * Notes : interrupts are always disabled!
  611. */
  612. static
  613. intr_ret_t acornscsi_kick(AS_Host *host)
  614. {
  615. int from_queue = 0;
  616. struct scsi_cmnd *SCpnt;
  617. /* first check to see if a command is waiting to be executed */
  618. SCpnt = host->origSCpnt;
  619. host->origSCpnt = NULL;
  620. /* retrieve next command */
  621. if (!SCpnt) {
  622. SCpnt = queue_remove_exclude(&host->queues.issue, host->busyluns);
  623. if (!SCpnt)
  624. return INTR_IDLE;
  625. from_queue = 1;
  626. }
  627. if (host->scsi.disconnectable && host->SCpnt) {
  628. queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
  629. host->scsi.disconnectable = 0;
  630. #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
  631. DBG(host->SCpnt, printk("scsi%d.%c: moved command to disconnected queue\n",
  632. host->host->host_no, acornscsi_target(host)));
  633. #endif
  634. host->SCpnt = NULL;
  635. }
  636. /*
  637. * If we have an interrupt pending, then we may have been reselected.
  638. * In this case, we don't want to write to the registers
  639. */
  640. if (!(sbic_arm_read(host->scsi.io_port, SBIC_ASR) & (ASR_INT|ASR_BSY|ASR_CIP))) {
  641. sbic_arm_write(host->scsi.io_port, SBIC_DESTID, SCpnt->device->id);
  642. sbic_arm_write(host->scsi.io_port, SBIC_CMND, CMND_SELWITHATN);
  643. }
  644. /*
  645. * claim host busy - all of these must happen atomically wrt
  646. * our interrupt routine. Failure means command loss.
  647. */
  648. host->scsi.phase = PHASE_CONNECTING;
  649. host->SCpnt = SCpnt;
  650. host->scsi.SCp = SCpnt->SCp;
  651. host->dma.xfer_setup = 0;
  652. host->dma.xfer_required = 0;
  653. host->dma.xfer_done = 0;
  654. #if (DEBUG & (DEBUG_ABORT|DEBUG_CONNECT))
  655. DBG(SCpnt,printk("scsi%d.%c: starting cmd %02X\n",
  656. host->host->host_no, '0' + SCpnt->device->id,
  657. SCpnt->cmnd[0]));
  658. #endif
  659. if (from_queue) {
  660. #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
  661. /*
  662. * tagged queueing - allocate a new tag to this command
  663. */
  664. if (SCpnt->device->simple_tags) {
  665. SCpnt->device->current_tag += 1;
  666. if (SCpnt->device->current_tag == 0)
  667. SCpnt->device->current_tag = 1;
  668. SCpnt->tag = SCpnt->device->current_tag;
  669. } else
  670. #endif
  671. set_bit(SCpnt->device->id * 8 + SCpnt->device->lun, host->busyluns);
  672. host->stats.removes += 1;
  673. switch (acornscsi_cmdtype(SCpnt->cmnd[0])) {
  674. case CMD_WRITE:
  675. host->stats.writes += 1;
  676. break;
  677. case CMD_READ:
  678. host->stats.reads += 1;
  679. break;
  680. case CMD_MISC:
  681. host->stats.miscs += 1;
  682. break;
  683. }
  684. }
  685. return INTR_PROCESSING;
  686. }
  687. /*
  688. * Function: void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp, unsigned int result)
  689. * Purpose : complete processing for command
  690. * Params : host - interface that completed
  691. * result - driver byte of result
  692. */
  693. static void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp,
  694. unsigned int result)
  695. {
  696. struct scsi_cmnd *SCpnt = *SCpntp;
  697. /* clean up */
  698. sbic_arm_write(host->scsi.io_port, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
  699. host->stats.fins += 1;
  700. if (SCpnt) {
  701. *SCpntp = NULL;
  702. acornscsi_dma_cleanup(host);
  703. SCpnt->result = result << 16 | host->scsi.SCp.Message << 8 | host->scsi.SCp.Status;
  704. /*
  705. * In theory, this should not happen. In practice, it seems to.
  706. * Only trigger an error if the device attempts to report all happy
  707. * but with untransferred buffers... If we don't do something, then
  708. * data loss will occur. Should we check SCpnt->underflow here?
  709. * It doesn't appear to be set to something meaningful by the higher
  710. * levels all the time.
  711. */
  712. if (result == DID_OK) {
  713. int xfer_warn = 0;
  714. if (SCpnt->underflow == 0) {
  715. if (host->scsi.SCp.ptr &&
  716. acornscsi_cmdtype(SCpnt->cmnd[0]) != CMD_MISC)
  717. xfer_warn = 1;
  718. } else {
  719. if (host->scsi.SCp.scsi_xferred < SCpnt->underflow ||
  720. host->scsi.SCp.scsi_xferred != host->dma.transferred)
  721. xfer_warn = 1;
  722. }
  723. /* ANSI standard says: (SCSI-2 Rev 10c Sect 5.6.6)
  724. * Targets which break data transfers into multiple
  725. * connections shall end each successful connection
  726. * (except possibly the last) with a SAVE DATA
  727. * POINTER - DISCONNECT message sequence.
  728. *
  729. * This makes it difficult to ensure that a transfer has
  730. * completed. If we reach the end of a transfer during
  731. * the command, then we can only have finished the transfer.
  732. * therefore, if we seem to have some data remaining, this
  733. * is not a problem.
  734. */
  735. if (host->dma.xfer_done)
  736. xfer_warn = 0;
  737. if (xfer_warn) {
  738. switch (status_byte(SCpnt->result)) {
  739. case CHECK_CONDITION:
  740. case COMMAND_TERMINATED:
  741. case BUSY:
  742. case QUEUE_FULL:
  743. case RESERVATION_CONFLICT:
  744. break;
  745. default:
  746. printk(KERN_ERR "scsi%d.H: incomplete data transfer detected: result=%08X command=",
  747. host->host->host_no, SCpnt->result);
  748. __scsi_print_command(SCpnt->cmnd);
  749. acornscsi_dumpdma(host, "done");
  750. acornscsi_dumplog(host, SCpnt->device->id);
  751. SCpnt->result &= 0xffff;
  752. SCpnt->result |= DID_ERROR << 16;
  753. }
  754. }
  755. }
  756. if (!SCpnt->scsi_done)
  757. panic("scsi%d.H: null scsi_done function in acornscsi_done", host->host->host_no);
  758. clear_bit(SCpnt->device->id * 8 + SCpnt->device->lun, host->busyluns);
  759. SCpnt->scsi_done(SCpnt);
  760. } else
  761. printk("scsi%d: null command in acornscsi_done", host->host->host_no);
  762. host->scsi.phase = PHASE_IDLE;
  763. }
  764. /* ====================================================================================
  765. * DMA routines
  766. */
  767. /*
  768. * Purpose : update SCSI Data Pointer
  769. * Notes : this will only be one SG entry or less
  770. */
  771. static
  772. void acornscsi_data_updateptr(AS_Host *host, struct scsi_pointer *SCp, unsigned int length)
  773. {
  774. SCp->ptr += length;
  775. SCp->this_residual -= length;
  776. if (SCp->this_residual == 0 && next_SCp(SCp) == 0)
  777. host->dma.xfer_done = 1;
  778. }
  779. /*
  780. * Prototype: void acornscsi_data_read(AS_Host *host, char *ptr,
  781. * unsigned int start_addr, unsigned int length)
  782. * Purpose : read data from DMA RAM
  783. * Params : host - host to transfer from
  784. * ptr - DRAM address
  785. * start_addr - host mem address
  786. * length - number of bytes to transfer
  787. * Notes : this will only be one SG entry or less
  788. */
  789. static
  790. void acornscsi_data_read(AS_Host *host, char *ptr,
  791. unsigned int start_addr, unsigned int length)
  792. {
  793. extern void __acornscsi_in(int port, char *buf, int len);
  794. unsigned int page, offset, len = length;
  795. page = (start_addr >> 12);
  796. offset = start_addr & ((1 << 12) - 1);
  797. outb((page & 0x3f) | host->card.page_reg, host->card.io_page);
  798. while (len > 0) {
  799. unsigned int this_len;
  800. if (len + offset > (1 << 12))
  801. this_len = (1 << 12) - offset;
  802. else
  803. this_len = len;
  804. __acornscsi_in(host->card.io_ram + (offset << 1), ptr, this_len);
  805. offset += this_len;
  806. ptr += this_len;
  807. len -= this_len;
  808. if (offset == (1 << 12)) {
  809. offset = 0;
  810. page ++;
  811. outb((page & 0x3f) | host->card.page_reg, host->card.io_page);
  812. }
  813. }
  814. outb(host->card.page_reg, host->card.io_page);
  815. }
  816. /*
  817. * Prototype: void acornscsi_data_write(AS_Host *host, char *ptr,
  818. * unsigned int start_addr, unsigned int length)
  819. * Purpose : write data to DMA RAM
  820. * Params : host - host to transfer from
  821. * ptr - DRAM address
  822. * start_addr - host mem address
  823. * length - number of bytes to transfer
  824. * Notes : this will only be one SG entry or less
  825. */
  826. static
  827. void acornscsi_data_write(AS_Host *host, char *ptr,
  828. unsigned int start_addr, unsigned int length)
  829. {
  830. extern void __acornscsi_out(int port, char *buf, int len);
  831. unsigned int page, offset, len = length;
  832. page = (start_addr >> 12);
  833. offset = start_addr & ((1 << 12) - 1);
  834. outb((page & 0x3f) | host->card.page_reg, host->card.io_page);
  835. while (len > 0) {
  836. unsigned int this_len;
  837. if (len + offset > (1 << 12))
  838. this_len = (1 << 12) - offset;
  839. else
  840. this_len = len;
  841. __acornscsi_out(host->card.io_ram + (offset << 1), ptr, this_len);
  842. offset += this_len;
  843. ptr += this_len;
  844. len -= this_len;
  845. if (offset == (1 << 12)) {
  846. offset = 0;
  847. page ++;
  848. outb((page & 0x3f) | host->card.page_reg, host->card.io_page);
  849. }
  850. }
  851. outb(host->card.page_reg, host->card.io_page);
  852. }
  853. /* =========================================================================================
  854. * On-board DMA routines
  855. */
  856. #ifdef USE_DMAC
  857. /*
  858. * Prototype: void acornscsi_dmastop(AS_Host *host)
  859. * Purpose : stop all DMA
  860. * Params : host - host on which to stop DMA
  861. * Notes : This is called when leaving DATA IN/OUT phase,
  862. * or when interface is RESET
  863. */
  864. static inline
  865. void acornscsi_dma_stop(AS_Host *host)
  866. {
  867. dmac_write(host->dma.io_port, DMAC_MASKREG, MASK_ON);
  868. dmac_clearintr(host->dma.io_intr_clear);
  869. #if (DEBUG & DEBUG_DMA)
  870. DBG(host->SCpnt, acornscsi_dumpdma(host, "stop"));
  871. #endif
  872. }
  873. /*
  874. * Function: void acornscsi_dma_setup(AS_Host *host, dmadir_t direction)
  875. * Purpose : setup DMA controller for data transfer
  876. * Params : host - host to setup
  877. * direction - data transfer direction
  878. * Notes : This is called when entering DATA I/O phase, not
  879. * while we're in a DATA I/O phase
  880. */
  881. static
  882. void acornscsi_dma_setup(AS_Host *host, dmadir_t direction)
  883. {
  884. unsigned int address, length, mode;
  885. host->dma.direction = direction;
  886. dmac_write(host->dma.io_port, DMAC_MASKREG, MASK_ON);
  887. if (direction == DMA_OUT) {
  888. #if (DEBUG & DEBUG_NO_WRITE)
  889. if (NO_WRITE & (1 << host->SCpnt->device->id)) {
  890. printk(KERN_CRIT "scsi%d.%c: I can't handle DMA_OUT!\n",
  891. host->host->host_no, acornscsi_target(host));
  892. return;
  893. }
  894. #endif
  895. mode = DMAC_WRITE;
  896. } else
  897. mode = DMAC_READ;
  898. /*
  899. * Allocate some buffer space, limited to half the buffer size
  900. */
  901. length = min_t(unsigned int, host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
  902. if (length) {
  903. host->dma.start_addr = address = host->dma.free_addr;
  904. host->dma.free_addr = (host->dma.free_addr + length) &
  905. (DMAC_BUFFER_SIZE - 1);
  906. /*
  907. * Transfer data to DMA memory
  908. */
  909. if (direction == DMA_OUT)
  910. acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
  911. length);
  912. length -= 1;
  913. dmac_write(host->dma.io_port, DMAC_TXCNTLO, length);
  914. dmac_write(host->dma.io_port, DMAC_TXCNTHI, length >> 8);
  915. dmac_write(host->dma.io_port, DMAC_TXADRLO, address);
  916. dmac_write(host->dma.io_port, DMAC_TXADRMD, address >> 8);
  917. dmac_write(host->dma.io_port, DMAC_TXADRHI, 0);
  918. dmac_write(host->dma.io_port, DMAC_MODECON, mode);
  919. dmac_write(host->dma.io_port, DMAC_MASKREG, MASK_OFF);
  920. #if (DEBUG & DEBUG_DMA)
  921. DBG(host->SCpnt, acornscsi_dumpdma(host, "strt"));
  922. #endif
  923. host->dma.xfer_setup = 1;
  924. }
  925. }
  926. /*
  927. * Function: void acornscsi_dma_cleanup(AS_Host *host)
  928. * Purpose : ensure that all DMA transfers are up-to-date & host->scsi.SCp is correct
  929. * Params : host - host to finish
  930. * Notes : This is called when a command is:
  931. * terminating, RESTORE_POINTERS, SAVE_POINTERS, DISCONECT
  932. * : This must not return until all transfers are completed.
  933. */
  934. static
  935. void acornscsi_dma_cleanup(AS_Host *host)
  936. {
  937. dmac_write(host->dma.io_port, DMAC_MASKREG, MASK_ON);
  938. dmac_clearintr(host->dma.io_intr_clear);
  939. /*
  940. * Check for a pending transfer
  941. */
  942. if (host->dma.xfer_required) {
  943. host->dma.xfer_required = 0;
  944. if (host->dma.direction == DMA_IN)
  945. acornscsi_data_read(host, host->dma.xfer_ptr,
  946. host->dma.xfer_start, host->dma.xfer_length);
  947. }
  948. /*
  949. * Has a transfer been setup?
  950. */
  951. if (host->dma.xfer_setup) {
  952. unsigned int transferred;
  953. host->dma.xfer_setup = 0;
  954. #if (DEBUG & DEBUG_DMA)
  955. DBG(host->SCpnt, acornscsi_dumpdma(host, "cupi"));
  956. #endif
  957. /*
  958. * Calculate number of bytes transferred from DMA.
  959. */
  960. transferred = dmac_address(host->dma.io_port) - host->dma.start_addr;
  961. host->dma.transferred += transferred;
  962. if (host->dma.direction == DMA_IN)
  963. acornscsi_data_read(host, host->scsi.SCp.ptr,
  964. host->dma.start_addr, transferred);
  965. /*
  966. * Update SCSI pointers
  967. */
  968. acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
  969. #if (DEBUG & DEBUG_DMA)
  970. DBG(host->SCpnt, acornscsi_dumpdma(host, "cupo"));
  971. #endif
  972. }
  973. }
  974. /*
  975. * Function: void acornscsi_dmacintr(AS_Host *host)
  976. * Purpose : handle interrupts from DMAC device
  977. * Params : host - host to process
  978. * Notes : If reading, we schedule the read to main memory &
  979. * allow the transfer to continue.
  980. * : If writing, we fill the onboard DMA memory from main
  981. * memory.
  982. * : Called whenever DMAC finished it's current transfer.
  983. */
  984. static
  985. void acornscsi_dma_intr(AS_Host *host)
  986. {
  987. unsigned int address, length, transferred;
  988. #if (DEBUG & DEBUG_DMA)
  989. DBG(host->SCpnt, acornscsi_dumpdma(host, "inti"));
  990. #endif
  991. dmac_write(host->dma.io_port, DMAC_MASKREG, MASK_ON);
  992. dmac_clearintr(host->dma.io_intr_clear);
  993. /*
  994. * Calculate amount transferred via DMA
  995. */
  996. transferred = dmac_address(host->dma.io_port) - host->dma.start_addr;
  997. host->dma.transferred += transferred;
  998. /*
  999. * Schedule DMA transfer off board
  1000. */
  1001. if (host->dma.direction == DMA_IN) {
  1002. host->dma.xfer_start = host->dma.start_addr;
  1003. host->dma.xfer_length = transferred;
  1004. host->dma.xfer_ptr = host->scsi.SCp.ptr;
  1005. host->dma.xfer_required = 1;
  1006. }
  1007. acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
  1008. /*
  1009. * Allocate some buffer space, limited to half the on-board RAM size
  1010. */
  1011. length = min_t(unsigned int, host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
  1012. if (length) {
  1013. host->dma.start_addr = address = host->dma.free_addr;
  1014. host->dma.free_addr = (host->dma.free_addr + length) &
  1015. (DMAC_BUFFER_SIZE - 1);
  1016. /*
  1017. * Transfer data to DMA memory
  1018. */
  1019. if (host->dma.direction == DMA_OUT)
  1020. acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
  1021. length);
  1022. length -= 1;
  1023. dmac_write(host->dma.io_port, DMAC_TXCNTLO, length);
  1024. dmac_write(host->dma.io_port, DMAC_TXCNTHI, length >> 8);
  1025. dmac_write(host->dma.io_port, DMAC_TXADRLO, address);
  1026. dmac_write(host->dma.io_port, DMAC_TXADRMD, address >> 8);
  1027. dmac_write(host->dma.io_port, DMAC_TXADRHI, 0);
  1028. dmac_write(host->dma.io_port, DMAC_MASKREG, MASK_OFF);
  1029. #if (DEBUG & DEBUG_DMA)
  1030. DBG(host->SCpnt, acornscsi_dumpdma(host, "into"));
  1031. #endif
  1032. } else {
  1033. host->dma.xfer_setup = 0;
  1034. #if 0
  1035. /*
  1036. * If the interface still wants more, then this is an error.
  1037. * We give it another byte, but we also attempt to raise an
  1038. * attention condition. We continue giving one byte until
  1039. * the device recognises the attention.
  1040. */
  1041. if (dmac_read(host->dma.io_port, DMAC_STATUS) & STATUS_RQ0) {
  1042. acornscsi_abortcmd(host, host->SCpnt->tag);
  1043. dmac_write(host->dma.io_port, DMAC_TXCNTLO, 0);
  1044. dmac_write(host->dma.io_port, DMAC_TXCNTHI, 0);
  1045. dmac_write(host->dma.io_port, DMAC_TXADRLO, 0);
  1046. dmac_write(host->dma.io_port, DMAC_TXADRMD, 0);
  1047. dmac_write(host->dma.io_port, DMAC_TXADRHI, 0);
  1048. dmac_write(host->dma.io_port, DMAC_MASKREG, MASK_OFF);
  1049. }
  1050. #endif
  1051. }
  1052. }
  1053. /*
  1054. * Function: void acornscsi_dma_xfer(AS_Host *host)
  1055. * Purpose : transfer data between AcornSCSI and memory
  1056. * Params : host - host to process
  1057. */
  1058. static
  1059. void acornscsi_dma_xfer(AS_Host *host)
  1060. {
  1061. host->dma.xfer_required = 0;
  1062. if (host->dma.direction == DMA_IN)
  1063. acornscsi_data_read(host, host->dma.xfer_ptr,
  1064. host->dma.xfer_start, host->dma.xfer_length);
  1065. }
  1066. /*
  1067. * Function: void acornscsi_dma_adjust(AS_Host *host)
  1068. * Purpose : adjust DMA pointers & count for bytes transferred to
  1069. * SBIC but not SCSI bus.
  1070. * Params : host - host to adjust DMA count for
  1071. */
  1072. static
  1073. void acornscsi_dma_adjust(AS_Host *host)
  1074. {
  1075. if (host->dma.xfer_setup) {
  1076. signed long transferred;
  1077. #if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
  1078. DBG(host->SCpnt, acornscsi_dumpdma(host, "adji"));
  1079. #endif
  1080. /*
  1081. * Calculate correct DMA address - DMA is ahead of SCSI bus while
  1082. * writing.
  1083. * host->scsi.SCp.scsi_xferred is the number of bytes
  1084. * actually transferred to/from the SCSI bus.
  1085. * host->dma.transferred is the number of bytes transferred
  1086. * over DMA since host->dma.start_addr was last set.
  1087. *
  1088. * real_dma_addr = host->dma.start_addr + host->scsi.SCp.scsi_xferred
  1089. * - host->dma.transferred
  1090. */
  1091. transferred = host->scsi.SCp.scsi_xferred - host->dma.transferred;
  1092. if (transferred < 0)
  1093. printk("scsi%d.%c: Ack! DMA write correction %ld < 0!\n",
  1094. host->host->host_no, acornscsi_target(host), transferred);
  1095. else if (transferred == 0)
  1096. host->dma.xfer_setup = 0;
  1097. else {
  1098. transferred += host->dma.start_addr;
  1099. dmac_write(host->dma.io_port, DMAC_TXADRLO, transferred);
  1100. dmac_write(host->dma.io_port, DMAC_TXADRMD, transferred >> 8);
  1101. dmac_write(host->dma.io_port, DMAC_TXADRHI, transferred >> 16);
  1102. #if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
  1103. DBG(host->SCpnt, acornscsi_dumpdma(host, "adjo"));
  1104. #endif
  1105. }
  1106. }
  1107. }
  1108. #endif
  1109. /* =========================================================================================
  1110. * Data I/O
  1111. */
  1112. static int
  1113. acornscsi_write_pio(AS_Host *host, char *bytes, int *ptr, int len, unsigned int max_timeout)
  1114. {
  1115. unsigned int asr, timeout = max_timeout;
  1116. int my_ptr = *ptr;
  1117. while (my_ptr < len) {
  1118. asr = sbic_arm_read(host->scsi.io_port, SBIC_ASR);
  1119. if (asr & ASR_DBR) {
  1120. timeout = max_timeout;
  1121. sbic_arm_write(host->scsi.io_port, SBIC_DATA, bytes[my_ptr++]);
  1122. } else if (asr & ASR_INT)
  1123. break;
  1124. else if (--timeout == 0)
  1125. break;
  1126. udelay(1);
  1127. }
  1128. *ptr = my_ptr;
  1129. return (timeout == 0) ? -1 : 0;
  1130. }
  1131. /*
  1132. * Function: void acornscsi_sendcommand(AS_Host *host)
  1133. * Purpose : send a command to a target
  1134. * Params : host - host which is connected to target
  1135. */
  1136. static void
  1137. acornscsi_sendcommand(AS_Host *host)
  1138. {
  1139. struct scsi_cmnd *SCpnt = host->SCpnt;
  1140. sbic_arm_write(host->scsi.io_port, SBIC_TRANSCNTH, 0);
  1141. sbic_arm_writenext(host->scsi.io_port, 0);
  1142. sbic_arm_writenext(host->scsi.io_port, SCpnt->cmd_len - host->scsi.SCp.sent_command);
  1143. acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
  1144. if (acornscsi_write_pio(host, SCpnt->cmnd,
  1145. (int *)&host->scsi.SCp.sent_command, SCpnt->cmd_len, 1000000))
  1146. printk("scsi%d: timeout while sending command\n", host->host->host_no);
  1147. host->scsi.phase = PHASE_COMMAND;
  1148. }
  1149. static
  1150. void acornscsi_sendmessage(AS_Host *host)
  1151. {
  1152. unsigned int message_length = msgqueue_msglength(&host->scsi.msgs);
  1153. unsigned int msgnr;
  1154. struct message *msg;
  1155. #if (DEBUG & DEBUG_MESSAGES)
  1156. printk("scsi%d.%c: sending message ",
  1157. host->host->host_no, acornscsi_target(host));
  1158. #endif
  1159. switch (message_length) {
  1160. case 0:
  1161. acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
  1162. acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 1");
  1163. sbic_arm_write(host->scsi.io_port, SBIC_DATA, NOP);
  1164. host->scsi.last_message = NOP;
  1165. #if (DEBUG & DEBUG_MESSAGES)
  1166. printk("NOP");
  1167. #endif
  1168. break;
  1169. case 1:
  1170. acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
  1171. msg = msgqueue_getmsg(&host->scsi.msgs, 0);
  1172. acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 2");
  1173. sbic_arm_write(host->scsi.io_port, SBIC_DATA, msg->msg[0]);
  1174. host->scsi.last_message = msg->msg[0];
  1175. #if (DEBUG & DEBUG_MESSAGES)
  1176. spi_print_msg(msg->msg);
  1177. #endif
  1178. break;
  1179. default:
  1180. /*
  1181. * ANSI standard says: (SCSI-2 Rev 10c Sect 5.6.14)
  1182. * 'When a target sends this (MESSAGE_REJECT) message, it
  1183. * shall change to MESSAGE IN phase and send this message
  1184. * prior to requesting additional message bytes from the
  1185. * initiator. This provides an interlock so that the
  1186. * initiator can determine which message byte is rejected.
  1187. */
  1188. sbic_arm_write(host->scsi.io_port, SBIC_TRANSCNTH, 0);
  1189. sbic_arm_writenext(host->scsi.io_port, 0);
  1190. sbic_arm_writenext(host->scsi.io_port, message_length);
  1191. acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
  1192. msgnr = 0;
  1193. while ((msg = msgqueue_getmsg(&host->scsi.msgs, msgnr++)) != NULL) {
  1194. unsigned int i;
  1195. #if (DEBUG & DEBUG_MESSAGES)
  1196. spi_print_msg(msg);
  1197. #endif
  1198. i = 0;
  1199. if (acornscsi_write_pio(host, msg->msg, &i, msg->length, 1000000))
  1200. printk("scsi%d: timeout while sending message\n", host->host->host_no);
  1201. host->scsi.last_message = msg->msg[0];
  1202. if (msg->msg[0] == EXTENDED_MESSAGE)
  1203. host->scsi.last_message |= msg->msg[2] << 8;
  1204. if (i != msg->length)
  1205. break;
  1206. }
  1207. break;
  1208. }
  1209. #if (DEBUG & DEBUG_MESSAGES)
  1210. printk("\n");
  1211. #endif
  1212. }
  1213. /*
  1214. * Function: void acornscsi_readstatusbyte(AS_Host *host)
  1215. * Purpose : Read status byte from connected target
  1216. * Params : host - host connected to target
  1217. */
  1218. static
  1219. void acornscsi_readstatusbyte(AS_Host *host)
  1220. {
  1221. acornscsi_sbic_issuecmd(host, CMND_XFERINFO|CMND_SBT);
  1222. acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "reading status byte");
  1223. host->scsi.SCp.Status = sbic_arm_read(host->scsi.io_port, SBIC_DATA);
  1224. }
  1225. /*
  1226. * Function: unsigned char acornscsi_readmessagebyte(AS_Host *host)
  1227. * Purpose : Read one message byte from connected target
  1228. * Params : host - host connected to target
  1229. */
  1230. static
  1231. unsigned char acornscsi_readmessagebyte(AS_Host *host)
  1232. {
  1233. unsigned char message;
  1234. acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
  1235. acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "for message byte");
  1236. message = sbic_arm_read(host->scsi.io_port, SBIC_DATA);
  1237. /* wait for MSGIN-XFER-PAUSED */
  1238. acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after message byte");
  1239. sbic_arm_read(host->scsi.io_port, SBIC_SSR);
  1240. return message;
  1241. }
  1242. /*
  1243. * Function: void acornscsi_message(AS_Host *host)
  1244. * Purpose : Read complete message from connected target & action message
  1245. * Params : host - host connected to target
  1246. */
  1247. static
  1248. void acornscsi_message(AS_Host *host)
  1249. {
  1250. unsigned char message[16];
  1251. unsigned int msgidx = 0, msglen = 1;
  1252. do {
  1253. message[msgidx] = acornscsi_readmessagebyte(host);
  1254. switch (msgidx) {
  1255. case 0:
  1256. if (message[0] == EXTENDED_MESSAGE ||
  1257. (message[0] >= 0x20 && message[0] <= 0x2f))
  1258. msglen = 2;
  1259. break;
  1260. case 1:
  1261. if (message[0] == EXTENDED_MESSAGE)
  1262. msglen += message[msgidx];
  1263. break;
  1264. }
  1265. msgidx += 1;
  1266. if (msgidx < msglen) {
  1267. acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
  1268. /* wait for next msg-in */
  1269. acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after negate ack");
  1270. sbic_arm_read(host->scsi.io_port, SBIC_SSR);
  1271. }
  1272. } while (msgidx < msglen);
  1273. #if (DEBUG & DEBUG_MESSAGES)
  1274. printk("scsi%d.%c: message in: ",
  1275. host->host->host_no, acornscsi_target(host));
  1276. spi_print_msg(message);
  1277. printk("\n");
  1278. #endif
  1279. if (host->scsi.phase == PHASE_RECONNECTED) {
  1280. /*
  1281. * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.17)
  1282. * 'Whenever a target reconnects to an initiator to continue
  1283. * a tagged I/O process, the SIMPLE QUEUE TAG message shall
  1284. * be sent immediately following the IDENTIFY message...'
  1285. */
  1286. if (message[0] == SIMPLE_QUEUE_TAG)
  1287. host->scsi.reconnected.tag = message[1];
  1288. if (acornscsi_reconnect_finish(host))
  1289. host->scsi.phase = PHASE_MSGIN;
  1290. }
  1291. switch (message[0]) {
  1292. case ABORT:
  1293. case ABORT_TAG:
  1294. case COMMAND_COMPLETE:
  1295. if (host->scsi.phase != PHASE_STATUSIN) {
  1296. printk(KERN_ERR "scsi%d.%c: command complete following non-status in phase?\n",
  1297. host->host->host_no, acornscsi_target(host));
  1298. acornscsi_dumplog(host, host->SCpnt->device->id);
  1299. }
  1300. host->scsi.phase = PHASE_DONE;
  1301. host->scsi.SCp.Message = message[0];
  1302. break;
  1303. case SAVE_POINTERS:
  1304. /*
  1305. * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.20)
  1306. * 'The SAVE DATA POINTER message is sent from a target to
  1307. * direct the initiator to copy the active data pointer to
  1308. * the saved data pointer for the current I/O process.
  1309. */
  1310. acornscsi_dma_cleanup(host);
  1311. host->SCpnt->SCp = host->scsi.SCp;
  1312. host->SCpnt->SCp.sent_command = 0;
  1313. host->scsi.phase = PHASE_MSGIN;
  1314. break;
  1315. case RESTORE_POINTERS:
  1316. /*
  1317. * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.19)
  1318. * 'The RESTORE POINTERS message is sent from a target to
  1319. * direct the initiator to copy the most recently saved
  1320. * command, data, and status pointers for the I/O process
  1321. * to the corresponding active pointers. The command and
  1322. * status pointers shall be restored to the beginning of
  1323. * the present command and status areas.'
  1324. */
  1325. acornscsi_dma_cleanup(host);
  1326. host->scsi.SCp = host->SCpnt->SCp;
  1327. host->scsi.phase = PHASE_MSGIN;
  1328. break;
  1329. case DISCONNECT:
  1330. /*
  1331. * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 6.4.2)
  1332. * 'On those occasions when an error or exception condition occurs
  1333. * and the target elects to repeat the information transfer, the
  1334. * target may repeat the transfer either issuing a RESTORE POINTERS
  1335. * message or by disconnecting without issuing a SAVE POINTERS
  1336. * message. When reconnection is completed, the most recent
  1337. * saved pointer values are restored.'
  1338. */
  1339. acornscsi_dma_cleanup(host);
  1340. host->scsi.phase = PHASE_DISCONNECT;
  1341. break;
  1342. case MESSAGE_REJECT:
  1343. #if 0 /* this isn't needed any more */
  1344. /*
  1345. * If we were negociating sync transfer, we don't yet know if
  1346. * this REJECT is for the sync transfer or for the tagged queue/wide
  1347. * transfer. Re-initiate sync transfer negociation now, and if
  1348. * we got a REJECT in response to SDTR, then it'll be set to DONE.
  1349. */
  1350. if (host->device[host->SCpnt->device->id].sync_state == SYNC_SENT_REQUEST)
  1351. host->device[host->SCpnt->device->id].sync_state = SYNC_NEGOCIATE;
  1352. #endif
  1353. /*
  1354. * If we have any messages waiting to go out, then assert ATN now
  1355. */
  1356. if (msgqueue_msglength(&host->scsi.msgs))
  1357. acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
  1358. switch (host->scsi.last_message) {
  1359. #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
  1360. case HEAD_OF_QUEUE_TAG:
  1361. case ORDERED_QUEUE_TAG:
  1362. case SIMPLE_QUEUE_TAG:
  1363. /*
  1364. * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.17)
  1365. * If a target does not implement tagged queuing and a queue tag
  1366. * message is received, it shall respond with a MESSAGE REJECT
  1367. * message and accept the I/O process as if it were untagged.
  1368. */
  1369. printk(KERN_NOTICE "scsi%d.%c: disabling tagged queueing\n",
  1370. host->host->host_no, acornscsi_target(host));
  1371. host->SCpnt->device->simple_tags = 0;
  1372. set_bit(host->SCpnt->device->id * 8 + host->SCpnt->device->lun, host->busyluns);
  1373. break;
  1374. #endif
  1375. case EXTENDED_MESSAGE | (EXTENDED_SDTR << 8):
  1376. /*
  1377. * Target can't handle synchronous transfers
  1378. */
  1379. printk(KERN_NOTICE "scsi%d.%c: Using asynchronous transfer\n",
  1380. host->host->host_no, acornscsi_target(host));
  1381. host->device[host->SCpnt->device->id].sync_xfer = SYNCHTRANSFER_2DBA;
  1382. host->device[host->SCpnt->device->id].sync_state = SYNC_ASYNCHRONOUS;
  1383. sbic_arm_write(host->scsi.io_port, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
  1384. break;
  1385. default:
  1386. break;
  1387. }
  1388. break;
  1389. case QUEUE_FULL:
  1390. /* TODO: target queue is full */
  1391. break;
  1392. case SIMPLE_QUEUE_TAG:
  1393. /* tag queue reconnect... message[1] = queue tag. Print something to indicate something happened! */
  1394. printk("scsi%d.%c: reconnect queue tag %02X\n",
  1395. host->host->host_no, acornscsi_target(host),
  1396. message[1]);
  1397. break;
  1398. case EXTENDED_MESSAGE:
  1399. switch (message[2]) {
  1400. #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
  1401. case EXTENDED_SDTR:
  1402. if (host->device[host->SCpnt->device->id].sync_state == SYNC_SENT_REQUEST) {
  1403. /*
  1404. * We requested synchronous transfers. This isn't quite right...
  1405. * We can only say if this succeeded if we proceed on to execute the
  1406. * command from this message. If we get a MESSAGE PARITY ERROR,
  1407. * and the target retries fail, then we fallback to asynchronous mode
  1408. */
  1409. host->device[host->SCpnt->device->id].sync_state = SYNC_COMPLETED;
  1410. printk(KERN_NOTICE "scsi%d.%c: Using synchronous transfer, offset %d, %d ns\n",
  1411. host->host->host_no, acornscsi_target(host),
  1412. message[4], message[3] * 4);
  1413. host->device[host->SCpnt->device->id].sync_xfer =
  1414. calc_sync_xfer(message[3] * 4, message[4]);
  1415. } else {
  1416. unsigned char period, length;
  1417. /*
  1418. * Target requested synchronous transfers. The agreement is only
  1419. * to be in operation AFTER the target leaves message out phase.
  1420. */
  1421. acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
  1422. period = max_t(unsigned int, message[3], sdtr_period / 4);
  1423. length = min_t(unsigned int, message[4], sdtr_size);
  1424. msgqueue_addmsg(&host->scsi.msgs, 5, EXTENDED_MESSAGE, 3,
  1425. EXTENDED_SDTR, period, length);
  1426. host->device[host->SCpnt->device->id].sync_xfer =
  1427. calc_sync_xfer(period * 4, length);
  1428. }
  1429. sbic_arm_write(host->scsi.io_port, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
  1430. break;
  1431. #else
  1432. /* We do not accept synchronous transfers. Respond with a
  1433. * MESSAGE_REJECT.
  1434. */
  1435. #endif
  1436. case EXTENDED_WDTR:
  1437. /* The WD33C93A is only 8-bit. We respond with a MESSAGE_REJECT
  1438. * to a wide data transfer request.
  1439. */
  1440. default:
  1441. acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
  1442. msgqueue_flush(&host->scsi.msgs);
  1443. msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
  1444. break;
  1445. }
  1446. break;
  1447. #ifdef CONFIG_SCSI_ACORNSCSI_LINK
  1448. case LINKED_CMD_COMPLETE:
  1449. case LINKED_FLG_CMD_COMPLETE:
  1450. /*
  1451. * We don't support linked commands yet
  1452. */
  1453. if (0) {
  1454. #if (DEBUG & DEBUG_LINK)
  1455. printk("scsi%d.%c: lun %d tag %d linked command complete\n",
  1456. host->host->host_no, acornscsi_target(host), host->SCpnt->tag);
  1457. #endif
  1458. /*
  1459. * A linked command should only terminate with one of these messages
  1460. * if there are more linked commands available.
  1461. */
  1462. if (!host->SCpnt->next_link) {
  1463. printk(KERN_WARNING "scsi%d.%c: lun %d tag %d linked command complete, but no next_link\n",
  1464. instance->host_no, acornscsi_target(host), host->SCpnt->tag);
  1465. acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
  1466. msgqueue_addmsg(&host->scsi.msgs, 1, ABORT);
  1467. } else {
  1468. struct scsi_cmnd *SCpnt = host->SCpnt;
  1469. acornscsi_dma_cleanup(host);
  1470. host->SCpnt = host->SCpnt->next_link;
  1471. host->SCpnt->tag = SCpnt->tag;
  1472. SCpnt->result = DID_OK | host->scsi.SCp.Message << 8 | host->Scsi.SCp.Status;
  1473. SCpnt->done(SCpnt);
  1474. /* initialise host->SCpnt->SCp */
  1475. }
  1476. break;
  1477. }
  1478. #endif
  1479. default: /* reject message */
  1480. printk(KERN_ERR "scsi%d.%c: unrecognised message %02X, rejecting\n",
  1481. host->host->host_no, acornscsi_target(host),
  1482. message[0]);
  1483. acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
  1484. msgqueue_flush(&host->scsi.msgs);
  1485. msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
  1486. host->scsi.phase = PHASE_MSGIN;
  1487. break;
  1488. }
  1489. acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
  1490. }
  1491. /*
  1492. * Function: int acornscsi_buildmessages(AS_Host *host)
  1493. * Purpose : build the connection messages for a host
  1494. * Params : host - host to add messages to
  1495. */
  1496. static
  1497. void acornscsi_buildmessages(AS_Host *host)
  1498. {
  1499. #if 0
  1500. /* does the device need resetting? */
  1501. if (cmd_reset) {
  1502. msgqueue_addmsg(&host->scsi.msgs, 1, BUS_DEVICE_RESET);
  1503. return;
  1504. }
  1505. #endif
  1506. msgqueue_addmsg(&host->scsi.msgs, 1,
  1507. IDENTIFY(host->device[host->SCpnt->device->id].disconnect_ok,
  1508. host->SCpnt->device->lun));
  1509. #if 0
  1510. /* does the device need the current command aborted */
  1511. if (cmd_aborted) {
  1512. acornscsi_abortcmd(host->SCpnt->tag);
  1513. return;
  1514. }
  1515. #endif
  1516. #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
  1517. if (host->SCpnt->tag) {
  1518. unsigned int tag_type;
  1519. if (host->SCpnt->cmnd[0] == REQUEST_SENSE ||
  1520. host->SCpnt->cmnd[0] == TEST_UNIT_READY ||
  1521. host->SCpnt->cmnd[0] == INQUIRY)
  1522. tag_type = HEAD_OF_QUEUE_TAG;
  1523. else
  1524. tag_type = SIMPLE_QUEUE_TAG;
  1525. msgqueue_addmsg(&host->scsi.msgs, 2, tag_type, host->SCpnt->tag);
  1526. }
  1527. #endif
  1528. #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
  1529. if (host->device[host->SCpnt->device->id].sync_state == SYNC_NEGOCIATE) {
  1530. host->device[host->SCpnt->device->id].sync_state = SYNC_SENT_REQUEST;
  1531. msgqueue_addmsg(&host->scsi.msgs, 5,
  1532. EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
  1533. sdtr_period / 4, sdtr_size);
  1534. }
  1535. #endif
  1536. }
  1537. /*
  1538. * Function: int acornscsi_starttransfer(AS_Host *host)
  1539. * Purpose : transfer data to/from connected target
  1540. * Params : host - host to which target is connected
  1541. * Returns : 0 if failure
  1542. */
  1543. static
  1544. int acornscsi_starttransfer(AS_Host *host)
  1545. {
  1546. int residual;
  1547. if (!host->scsi.SCp.ptr /*&& host->scsi.SCp.this_residual*/) {
  1548. printk(KERN_ERR "scsi%d.%c: null buffer passed to acornscsi_starttransfer\n",
  1549. host->host->host_no, acornscsi_target(host));
  1550. return 0;
  1551. }
  1552. residual = scsi_bufflen(host->SCpnt) - host->scsi.SCp.scsi_xferred;
  1553. sbic_arm_write(host->scsi.io_port, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
  1554. sbic_arm_writenext(host->scsi.io_port, residual >> 16);
  1555. sbic_arm_writenext(host->scsi.io_port, residual >> 8);
  1556. sbic_arm_writenext(host->scsi.io_port, residual);
  1557. acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
  1558. return 1;
  1559. }
  1560. /* =========================================================================================
  1561. * Connection & Disconnection
  1562. */
  1563. /*
  1564. * Function : acornscsi_reconnect(AS_Host *host)
  1565. * Purpose : reconnect a previously disconnected command
  1566. * Params : host - host specific data
  1567. * Remarks : SCSI spec says:
  1568. * 'The set of active pointers is restored from the set
  1569. * of saved pointers upon reconnection of the I/O process'
  1570. */
  1571. static
  1572. int acornscsi_reconnect(AS_Host *host)
  1573. {
  1574. unsigned int target, lun, ok = 0;
  1575. target = sbic_arm_read(host->scsi.io_port, SBIC_SOURCEID);
  1576. if (!(target & 8))
  1577. printk(KERN_ERR "scsi%d: invalid source id after reselection "
  1578. "- device fault?\n",
  1579. host->host->host_no);
  1580. target &= 7;
  1581. if (host->SCpnt && !host->scsi.disconnectable) {
  1582. printk(KERN_ERR "scsi%d.%d: reconnected while command in "
  1583. "progress to target %d?\n",
  1584. host->host->host_no, target, host->SCpnt->device->id);
  1585. host->SCpnt = NULL;
  1586. }
  1587. lun = sbic_arm_read(host->scsi.io_port, SBIC_DATA) & 7;
  1588. host->scsi.reconnected.target = target;
  1589. host->scsi.reconnected.lun = lun;
  1590. host->scsi.reconnected.tag = 0;
  1591. if (host->scsi.disconnectable && host->SCpnt &&
  1592. host->SCpnt->device->id == target && host->SCpnt->device->lun == lun)
  1593. ok = 1;
  1594. if (!ok && queue_probetgtlun(&host->queues.disconnected, target, lun))
  1595. ok = 1;
  1596. ADD_STATUS(target, 0x81, host->scsi.phase, 0);
  1597. if (ok) {
  1598. host->scsi.phase = PHASE_RECONNECTED;
  1599. } else {
  1600. /* this doesn't seem to work */
  1601. printk(KERN_ERR "scsi%d.%c: reselected with no command "
  1602. "to reconnect with\n",
  1603. host->host->host_no, '0' + target);
  1604. acornscsi_dumplog(host, target);
  1605. acornscsi_abortcmd(host, 0);
  1606. if (host->SCpnt) {
  1607. queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
  1608. host->SCpnt = NULL;
  1609. }
  1610. }
  1611. acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
  1612. return !ok;
  1613. }
  1614. /*
  1615. * Function: int acornscsi_reconect_finish(AS_Host *host)
  1616. * Purpose : finish reconnecting a command
  1617. * Params : host - host to complete
  1618. * Returns : 0 if failed
  1619. */
  1620. static
  1621. int acornscsi_reconnect_finish(AS_Host *host)
  1622. {
  1623. if (host->scsi.disconnectable && host->SCpnt) {
  1624. host->scsi.disconnectable = 0;
  1625. if (host->SCpnt->device->id == host->scsi.reconnected.target &&
  1626. host->SCpnt->device->lun == host->scsi.reconnected.lun &&
  1627. host->SCpnt->tag == host->scsi.reconnected.tag) {
  1628. #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
  1629. DBG(host->SCpnt, printk("scsi%d.%c: reconnected",
  1630. host->host->host_no, acornscsi_target(host)));
  1631. #endif
  1632. } else {
  1633. queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
  1634. #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
  1635. DBG(host->SCpnt, printk("scsi%d.%c: had to move command "
  1636. "to disconnected queue\n",
  1637. host->host->host_no, acornscsi_target(host)));
  1638. #endif
  1639. host->SCpnt = NULL;
  1640. }
  1641. }
  1642. if (!host->SCpnt) {
  1643. host->SCpnt = queue_remove_tgtluntag(&host->queues.disconnected,
  1644. host->scsi.reconnected.target,
  1645. host->scsi.reconnected.lun,
  1646. host->scsi.reconnected.tag);
  1647. #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
  1648. DBG(host->SCpnt, printk("scsi%d.%c: had to get command",
  1649. host->host->host_no, acornscsi_target(host)));
  1650. #endif
  1651. }
  1652. if (!host->SCpnt)
  1653. acornscsi_abortcmd(host, host->scsi.reconnected.tag);
  1654. else {
  1655. /*
  1656. * Restore data pointer from SAVED pointers.
  1657. */
  1658. host->scsi.SCp = host->SCpnt->SCp;
  1659. #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
  1660. printk(", data pointers: [%p, %X]",
  1661. host->scsi.SCp.ptr, host->scsi.SCp.this_residual);
  1662. #endif
  1663. }
  1664. #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
  1665. printk("\n");
  1666. #endif
  1667. host->dma.transferred = host->scsi.SCp.scsi_xferred;
  1668. return host->SCpnt != NULL;
  1669. }
  1670. /*
  1671. * Function: void acornscsi_disconnect_unexpected(AS_Host *host)
  1672. * Purpose : handle an unexpected disconnect
  1673. * Params : host - host on which disconnect occurred
  1674. */
  1675. static
  1676. void acornscsi_disconnect_unexpected(AS_Host *host)
  1677. {
  1678. printk(KERN_ERR "scsi%d.%c: unexpected disconnect\n",
  1679. host->host->host_no, acornscsi_target(host));
  1680. #if (DEBUG & DEBUG_ABORT)
  1681. acornscsi_dumplog(host, 8);
  1682. #endif
  1683. acornscsi_done(host, &host->SCpnt, DID_ERROR);
  1684. }
  1685. /*
  1686. * Function: void acornscsi_abortcmd(AS_host *host, unsigned char tag)
  1687. * Purpose : abort a currently executing command
  1688. * Params : host - host with connected command to abort
  1689. * tag - tag to abort
  1690. */
  1691. static
  1692. void acornscsi_abortcmd(AS_Host *host, unsigned char tag)
  1693. {
  1694. host->scsi.phase = PHASE_ABORTED;
  1695. sbic_arm_write(host->scsi.io_port, SBIC_CMND, CMND_ASSERTATN);
  1696. msgqueue_flush(&host->scsi.msgs);
  1697. #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
  1698. if (tag)
  1699. msgqueue_addmsg(&host->scsi.msgs, 2, ABORT_TAG, tag);
  1700. else
  1701. #endif
  1702. msgqueue_addmsg(&host->scsi.msgs, 1, ABORT);
  1703. }
  1704. /* ==========================================================================================
  1705. * Interrupt routines.
  1706. */
  1707. /*
  1708. * Function: int acornscsi_sbicintr(AS_Host *host)
  1709. * Purpose : handle interrupts from SCSI device
  1710. * Params : host - host to process
  1711. * Returns : INTR_PROCESS if expecting another SBIC interrupt
  1712. * INTR_IDLE if no interrupt
  1713. * INTR_NEXT_COMMAND if we have finished processing the command
  1714. */
  1715. static
  1716. intr_ret_t acornscsi_sbicintr(AS_Host *host, int in_irq)
  1717. {
  1718. unsigned int asr, ssr;
  1719. asr = sbic_arm_read(host->scsi.io_port, SBIC_ASR);
  1720. if (!(asr & ASR_INT))
  1721. return INTR_IDLE;
  1722. ssr = sbic_arm_read(host->scsi.io_port, SBIC_SSR);
  1723. #if (DEBUG & DEBUG_PHASES)
  1724. print_sbic_status(asr, ssr, host->scsi.phase);
  1725. #endif
  1726. ADD_STATUS(8, ssr, host->scsi.phase, in_irq);
  1727. if (host->SCpnt && !host->scsi.disconnectable)
  1728. ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, in_irq);
  1729. switch (ssr) {
  1730. case 0x00: /* reset state - not advanced */
  1731. printk(KERN_ERR "scsi%d: reset in standard mode but wanted advanced mode.\n",
  1732. host->host->host_no);
  1733. /* setup sbic - WD33C93A */
  1734. sbic_arm_write(host->scsi.io_port, SBIC_OWNID, OWNID_EAF | host->host->this_id);
  1735. sbic_arm_write(host->scsi.io_port, SBIC_CMND, CMND_RESET);
  1736. return INTR_IDLE;
  1737. case 0x01: /* reset state - advanced */
  1738. sbic_arm_write(host->scsi.io_port, SBIC_CTRL, INIT_SBICDMA | CTRL_IDI);
  1739. sbic_arm_write(host->scsi.io_port, SBIC_TIMEOUT, TIMEOUT_TIME);
  1740. sbic_arm_write(host->scsi.io_port, SBIC_SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
  1741. sbic_arm_write(host->scsi.io_port, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
  1742. msgqueue_flush(&host->scsi.msgs);
  1743. return INTR_IDLE;
  1744. case 0x41: /* unexpected disconnect aborted command */
  1745. acornscsi_disconnect_unexpected(host);
  1746. return INTR_NEXT_COMMAND;
  1747. }
  1748. switch (host->scsi.phase) {
  1749. case PHASE_CONNECTING: /* STATE: command removed from issue queue */
  1750. switch (ssr) {
  1751. case 0x11: /* -> PHASE_CONNECTED */
  1752. /* BUS FREE -> SELECTION */
  1753. host->scsi.phase = PHASE_CONNECTED;
  1754. msgqueue_flush(&host->scsi.msgs);
  1755. host->dma.transferred = host->scsi.SCp.scsi_xferred;
  1756. /* 33C93 gives next interrupt indicating bus phase */
  1757. asr = sbic_arm_read(host->scsi.io_port, SBIC_ASR);
  1758. if (!(asr & ASR_INT))
  1759. break;
  1760. ssr = sbic_arm_read(host->scsi.io_port, SBIC_SSR);
  1761. ADD_STATUS(8, ssr, host->scsi.phase, 1);
  1762. ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, 1);
  1763. goto connected;
  1764. case 0x42: /* select timed out */
  1765. /* -> PHASE_IDLE */
  1766. acornscsi_done(host, &host->SCpnt, DID_NO_CONNECT);
  1767. return INTR_NEXT_COMMAND;
  1768. case 0x81: /* -> PHASE_RECONNECTED or PHASE_ABORTED */
  1769. /* BUS FREE -> RESELECTION */
  1770. host->origSCpnt = host->SCpnt;
  1771. host->SCpnt = NULL;
  1772. msgqueue_flush(&host->scsi.msgs);
  1773. acornscsi_reconnect(host);
  1774. break;
  1775. default:
  1776. printk(KERN_ERR "scsi%d.%c: PHASE_CONNECTING, SSR %02X?\n",
  1777. host->host->host_no, acornscsi_target(host), ssr);
  1778. acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
  1779. acornscsi_abortcmd(host, host->SCpnt->tag);
  1780. }
  1781. return INTR_PROCESSING;
  1782. connected:
  1783. case PHASE_CONNECTED: /* STATE: device selected ok */
  1784. switch (ssr) {
  1785. #ifdef NONSTANDARD
  1786. case 0x8a: /* -> PHASE_COMMAND, PHASE_COMMANDPAUSED */
  1787. /* SELECTION -> COMMAND */
  1788. acornscsi_sendcommand(host);
  1789. break;
  1790. case 0x8b: /* -> PHASE_STATUS */
  1791. /* SELECTION -> STATUS */
  1792. acornscsi_readstatusbyte(host);
  1793. host->scsi.phase = PHASE_STATUSIN;
  1794. break;
  1795. #endif
  1796. case 0x8e: /* -> PHASE_MSGOUT */
  1797. /* SELECTION ->MESSAGE OUT */
  1798. host->scsi.phase = PHASE_MSGOUT;
  1799. acornscsi_buildmessages(host);
  1800. acornscsi_sendmessage(host);
  1801. break;
  1802. /* these should not happen */
  1803. case 0x85: /* target disconnected */
  1804. acornscsi_done(host, &host->SCpnt, DID_ERROR);
  1805. break;
  1806. default:
  1807. printk(KERN_ERR "scsi%d.%c: PHASE_CONNECTED, SSR %02X?\n",
  1808. host->host->host_no, acornscsi_target(host), ssr);
  1809. acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
  1810. acornscsi_abortcmd(host, host->SCpnt->tag);
  1811. }
  1812. return INTR_PROCESSING;
  1813. case PHASE_MSGOUT: /* STATE: connected & sent IDENTIFY message */
  1814. /*
  1815. * SCSI standard says that MESSAGE OUT phases can be followed by a
  1816. * DATA phase, STATUS phase, MESSAGE IN phase or COMMAND phase
  1817. */
  1818. switch (ssr) {
  1819. case 0x8a: /* -> PHASE_COMMAND, PHASE_COMMANDPAUSED */
  1820. case 0x1a: /* -> PHASE_COMMAND, PHASE_COMMANDPAUSED */
  1821. /* MESSAGE OUT -> COMMAND */
  1822. acornscsi_sendcommand(host);
  1823. break;
  1824. case 0x8b: /* -> PHASE_STATUS */
  1825. case 0x1b: /* -> PHASE_STATUS */
  1826. /* MESSAGE OUT -> STATUS */
  1827. acornscsi_readstatusbyte(host);
  1828. host->scsi.phase = PHASE_STATUSIN;
  1829. break;
  1830. case 0x8e: /* -> PHASE_MSGOUT */
  1831. /* MESSAGE_OUT(MESSAGE_IN) ->MESSAGE OUT */
  1832. acornscsi_sendmessage(host);
  1833. break;
  1834. case 0x4f: /* -> PHASE_MSGIN, PHASE_DISCONNECT */
  1835. case 0x1f: /* -> PHASE_MSGIN, PHASE_DISCONNECT */
  1836. /* MESSAGE OUT -> MESSAGE IN */
  1837. acornscsi_message(host);
  1838. break;
  1839. default:
  1840. printk(KERN_ERR "scsi%d.%c: PHASE_MSGOUT, SSR %02X?\n",
  1841. host->host->host_no, acornscsi_target(host), ssr);
  1842. acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
  1843. }
  1844. return INTR_PROCESSING;
  1845. case PHASE_COMMAND: /* STATE: connected & command sent */
  1846. switch (ssr) {
  1847. case 0x18: /* -> PHASE_DATAOUT */
  1848. /* COMMAND -> DATA OUT */
  1849. if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
  1850. acornscsi_abortcmd(host, host->SCpnt->tag);
  1851. acornscsi_dma_setup(host, DMA_OUT);
  1852. if (!acornscsi_starttransfer(host))
  1853. acornscsi_abortcmd(host, host->SCpnt->tag);
  1854. host->scsi.phase = PHASE_DATAOUT;
  1855. return INTR_IDLE;
  1856. case 0x19: /* -> PHASE_DATAIN */
  1857. /* COMMAND -> DATA IN */
  1858. if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
  1859. acornscsi_abortcmd(host, host->SCpnt->tag);
  1860. acornscsi_dma_setup(host, DMA_IN);
  1861. if (!acornscsi_starttransfer(host))
  1862. acornscsi_abortcmd(host, host->SCpnt->tag);
  1863. host->scsi.phase = PHASE_DATAIN;
  1864. return INTR_IDLE;
  1865. case 0x1b: /* -> PHASE_STATUS */
  1866. /* COMMAND -> STATUS */
  1867. acornscsi_readstatusbyte(host);
  1868. host->scsi.phase = PHASE_STATUSIN;
  1869. break;
  1870. case 0x1e: /* -> PHASE_MSGOUT */
  1871. /* COMMAND -> MESSAGE OUT */
  1872. acornscsi_sendmessage(host);
  1873. break;
  1874. case 0x1f: /* -> PHASE_MSGIN, PHASE_DISCONNECT */
  1875. /* COMMAND -> MESSAGE IN */
  1876. acornscsi_message(host);
  1877. break;
  1878. default:
  1879. printk(KERN_ERR "scsi%d.%c: PHASE_COMMAND, SSR %02X?\n",
  1880. host->host->host_no, acornscsi_target(host), ssr);
  1881. acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
  1882. }
  1883. return INTR_PROCESSING;
  1884. case PHASE_DISCONNECT: /* STATE: connected, received DISCONNECT msg */
  1885. if (ssr == 0x85) { /* -> PHASE_IDLE */
  1886. host->scsi.disconnectable = 1;
  1887. host->scsi.reconnected.tag = 0;
  1888. host->scsi.phase = PHASE_IDLE;
  1889. host->stats.disconnects += 1;
  1890. } else {
  1891. printk(KERN_ERR "scsi%d.%c: PHASE_DISCONNECT, SSR %02X instead of disconnect?\n",
  1892. host->host->host_no, acornscsi_target(host), ssr);
  1893. acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
  1894. }
  1895. return INTR_NEXT_COMMAND;
  1896. case PHASE_IDLE: /* STATE: disconnected */
  1897. if (ssr == 0x81) /* -> PHASE_RECONNECTED or PHASE_ABORTED */
  1898. acornscsi_reconnect(host);
  1899. else {
  1900. printk(KERN_ERR "scsi%d.%c: PHASE_IDLE, SSR %02X while idle?\n",
  1901. host->host->host_no, acornscsi_target(host), ssr);
  1902. acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
  1903. }
  1904. return INTR_PROCESSING;
  1905. case PHASE_RECONNECTED: /* STATE: device reconnected to initiator */
  1906. /*
  1907. * Command reconnected - if MESGIN, get message - it may be
  1908. * the tag. If not, get command out of disconnected queue
  1909. */
  1910. /*
  1911. * If we reconnected and we're not in MESSAGE IN phase after IDENTIFY,
  1912. * reconnect I_T_L command
  1913. */
  1914. if (ssr != 0x8f && !acornscsi_reconnect_finish(host))
  1915. return INTR_IDLE;
  1916. ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, in_irq);
  1917. switch (ssr) {
  1918. case 0x88: /* data out phase */
  1919. /* -> PHASE_DATAOUT */
  1920. /* MESSAGE IN -> DATA OUT */
  1921. acornscsi_dma_setup(host, DMA_OUT);
  1922. if (!acornscsi_starttransfer(host))
  1923. acornscsi_abortcmd(host, host->SCpnt->tag);
  1924. host->scsi.phase = PHASE_DATAOUT;
  1925. return INTR_IDLE;
  1926. case 0x89: /* data in phase */
  1927. /* -> PHASE_DATAIN */
  1928. /* MESSAGE IN -> DATA IN */
  1929. acornscsi_dma_setup(host, DMA_IN);
  1930. if (!acornscsi_starttransfer(host))
  1931. acornscsi_abortcmd(host, host->SCpnt->tag);
  1932. host->scsi.phase = PHASE_DATAIN;
  1933. return INTR_IDLE;
  1934. case 0x8a: /* command out */
  1935. /* MESSAGE IN -> COMMAND */
  1936. acornscsi_sendcommand(host);/* -> PHASE_COMMAND, PHASE_COMMANDPAUSED */
  1937. break;
  1938. case 0x8b: /* status in */
  1939. /* -> PHASE_STATUSIN */
  1940. /* MESSAGE IN -> STATUS */
  1941. acornscsi_readstatusbyte(host);
  1942. host->scsi.phase = PHASE_STATUSIN;
  1943. break;
  1944. case 0x8e: /* message out */
  1945. /* -> PHASE_MSGOUT */
  1946. /* MESSAGE IN -> MESSAGE OUT */
  1947. acornscsi_sendmessage(host);
  1948. break;
  1949. case 0x8f: /* message in */
  1950. acornscsi_message(host); /* -> PHASE_MSGIN, PHASE_DISCONNECT */
  1951. break;
  1952. default:
  1953. printk(KERN_ERR "scsi%d.%c: PHASE_RECONNECTED, SSR %02X after reconnect?\n",
  1954. host->host->host_no, acornscsi_target(host), ssr);
  1955. acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
  1956. }
  1957. return INTR_PROCESSING;
  1958. case PHASE_DATAIN: /* STATE: transferred data in */
  1959. /*
  1960. * This is simple - if we disconnect then the DMA address & count is
  1961. * correct.
  1962. */
  1963. switch (ssr) {
  1964. case 0x19: /* -> PHASE_DATAIN */
  1965. case 0x89: /* -> PHASE_DATAIN */
  1966. acornscsi_abortcmd(host, host->SCpnt->tag);
  1967. return INTR_IDLE;
  1968. case 0x1b: /* -> PHASE_STATUSIN */
  1969. case 0x4b: /* -> PHASE_STATUSIN */
  1970. case 0x8b: /* -> PHASE_STATUSIN */
  1971. /* DATA IN -> STATUS */
  1972. host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
  1973. acornscsi_sbic_xfcount(host);
  1974. acornscsi_dma_stop(host);
  1975. acornscsi_readstatusbyte(host);
  1976. host->scsi.phase = PHASE_STATUSIN;
  1977. break;
  1978. case 0x1e: /* -> PHASE_MSGOUT */
  1979. case 0x4e: /* -> PHASE_MSGOUT */
  1980. case 0x8e: /* -> PHASE_MSGOUT */
  1981. /* DATA IN -> MESSAGE OUT */
  1982. host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
  1983. acornscsi_sbic_xfcount(host);
  1984. acornscsi_dma_stop(host);
  1985. acornscsi_sendmessage(host);
  1986. break;
  1987. case 0x1f: /* message in */
  1988. case 0x4f: /* message in */
  1989. case 0x8f: /* message in */
  1990. /* DATA IN -> MESSAGE IN */
  1991. host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
  1992. acornscsi_sbic_xfcount(host);
  1993. acornscsi_dma_stop(host);
  1994. acornscsi_message(host); /* -> PHASE_MSGIN, PHASE_DISCONNECT */
  1995. break;
  1996. default:
  1997. printk(KERN_ERR "scsi%d.%c: PHASE_DATAIN, SSR %02X?\n",
  1998. host->host->host_no, acornscsi_target(host), ssr);
  1999. acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
  2000. }
  2001. return INTR_PROCESSING;
  2002. case PHASE_DATAOUT: /* STATE: transferred data out */
  2003. /*
  2004. * This is more complicated - if we disconnect, the DMA could be 12
  2005. * bytes ahead of us. We need to correct this.
  2006. */
  2007. switch (ssr) {
  2008. case 0x18: /* -> PHASE_DATAOUT */
  2009. case 0x88: /* -> PHASE_DATAOUT */
  2010. acornscsi_abortcmd(host, host->SCpnt->tag);
  2011. return INTR_IDLE;
  2012. case 0x1b: /* -> PHASE_STATUSIN */
  2013. case 0x4b: /* -> PHASE_STATUSIN */
  2014. case 0x8b: /* -> PHASE_STATUSIN */
  2015. /* DATA OUT -> STATUS */
  2016. host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
  2017. acornscsi_sbic_xfcount(host);
  2018. acornscsi_dma_stop(host);
  2019. acornscsi_dma_adjust(host);
  2020. acornscsi_readstatusbyte(host);
  2021. host->scsi.phase = PHASE_STATUSIN;
  2022. break;
  2023. case 0x1e: /* -> PHASE_MSGOUT */
  2024. case 0x4e: /* -> PHASE_MSGOUT */
  2025. case 0x8e: /* -> PHASE_MSGOUT */
  2026. /* DATA OUT -> MESSAGE OUT */
  2027. host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
  2028. acornscsi_sbic_xfcount(host);
  2029. acornscsi_dma_stop(host);
  2030. acornscsi_dma_adjust(host);
  2031. acornscsi_sendmessage(host);
  2032. break;
  2033. case 0x1f: /* message in */
  2034. case 0x4f: /* message in */
  2035. case 0x8f: /* message in */
  2036. /* DATA OUT -> MESSAGE IN */
  2037. host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
  2038. acornscsi_sbic_xfcount(host);
  2039. acornscsi_dma_stop(host);
  2040. acornscsi_dma_adjust(host);
  2041. acornscsi_message(host); /* -> PHASE_MSGIN, PHASE_DISCONNECT */
  2042. break;
  2043. default:
  2044. printk(KERN_ERR "scsi%d.%c: PHASE_DATAOUT, SSR %02X?\n",
  2045. host->host->host_no, acornscsi_target(host), ssr);
  2046. acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
  2047. }
  2048. return INTR_PROCESSING;
  2049. case PHASE_STATUSIN: /* STATE: status in complete */
  2050. switch (ssr) {
  2051. case 0x1f: /* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
  2052. case 0x8f: /* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
  2053. /* STATUS -> MESSAGE IN */
  2054. acornscsi_message(host);
  2055. break;
  2056. case 0x1e: /* -> PHASE_MSGOUT */
  2057. case 0x8e: /* -> PHASE_MSGOUT */
  2058. /* STATUS -> MESSAGE OUT */
  2059. acornscsi_sendmessage(host);
  2060. break;
  2061. default:
  2062. printk(KERN_ERR "scsi%d.%c: PHASE_STATUSIN, SSR %02X instead of MESSAGE_IN?\n",
  2063. host->host->host_no, acornscsi_target(host), ssr);
  2064. acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
  2065. }
  2066. return INTR_PROCESSING;
  2067. case PHASE_MSGIN: /* STATE: message in */
  2068. switch (ssr) {
  2069. case 0x1e: /* -> PHASE_MSGOUT */
  2070. case 0x4e: /* -> PHASE_MSGOUT */
  2071. case 0x8e: /* -> PHASE_MSGOUT */
  2072. /* MESSAGE IN -> MESSAGE OUT */
  2073. acornscsi_sendmessage(host);
  2074. break;
  2075. case 0x1f: /* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
  2076. case 0x2f:
  2077. case 0x4f:
  2078. case 0x8f:
  2079. acornscsi_message(host);
  2080. break;
  2081. case 0x85:
  2082. printk("scsi%d.%c: strange message in disconnection\n",
  2083. host->host->host_no, acornscsi_target(host));
  2084. acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
  2085. acornscsi_done(host, &host->SCpnt, DID_ERROR);
  2086. break;
  2087. default:
  2088. printk(KERN_ERR "scsi%d.%c: PHASE_MSGIN, SSR %02X after message in?\n",
  2089. host->host->host_no, acornscsi_target(host), ssr);
  2090. acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
  2091. }
  2092. return INTR_PROCESSING;
  2093. case PHASE_DONE: /* STATE: received status & message */
  2094. switch (ssr) {
  2095. case 0x85: /* -> PHASE_IDLE */
  2096. acornscsi_done(host, &host->SCpnt, DID_OK);
  2097. return INTR_NEXT_COMMAND;
  2098. case 0x1e:
  2099. case 0x8e:
  2100. acornscsi_sendmessage(host);
  2101. break;
  2102. default:
  2103. printk(KERN_ERR "scsi%d.%c: PHASE_DONE, SSR %02X instead of disconnect?\n",
  2104. host->host->host_no, acornscsi_target(host), ssr);
  2105. acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
  2106. }
  2107. return INTR_PROCESSING;
  2108. case PHASE_ABORTED:
  2109. switch (ssr) {
  2110. case 0x85:
  2111. if (host->SCpnt)
  2112. acornscsi_done(host, &host->SCpnt, DID_ABORT);
  2113. else {
  2114. clear_bit(host->scsi.reconnected.target * 8 + host->scsi.reconnected.lun,
  2115. host->busyluns);
  2116. host->scsi.phase = PHASE_IDLE;
  2117. }
  2118. return INTR_NEXT_COMMAND;
  2119. case 0x1e:
  2120. case 0x2e:
  2121. case 0x4e:
  2122. case 0x8e:
  2123. acornscsi_sendmessage(host);
  2124. break;
  2125. default:
  2126. printk(KERN_ERR "scsi%d.%c: PHASE_ABORTED, SSR %02X?\n",
  2127. host->host->host_no, acornscsi_target(host), ssr);
  2128. acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
  2129. }
  2130. return INTR_PROCESSING;
  2131. default:
  2132. printk(KERN_ERR "scsi%d.%c: unknown driver phase %d\n",
  2133. host->host->host_no, acornscsi_target(host), ssr);
  2134. acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
  2135. }
  2136. return INTR_PROCESSING;
  2137. }
  2138. /*
  2139. * Prototype: void acornscsi_intr(int irq, void *dev_id)
  2140. * Purpose : handle interrupts from Acorn SCSI card
  2141. * Params : irq - interrupt number
  2142. * dev_id - device specific data (AS_Host structure)
  2143. */
  2144. static irqreturn_t
  2145. acornscsi_intr(int irq, void *dev_id)
  2146. {
  2147. AS_Host *host = (AS_Host *)dev_id;
  2148. intr_ret_t ret;
  2149. int iostatus;
  2150. int in_irq = 0;
  2151. do {
  2152. ret = INTR_IDLE;
  2153. iostatus = inb(host->card.io_intr);
  2154. if (iostatus & 2) {
  2155. acornscsi_dma_intr(host);
  2156. iostatus = inb(host->card.io_intr);
  2157. }
  2158. if (iostatus & 8)
  2159. ret = acornscsi_sbicintr(host, in_irq);
  2160. /*
  2161. * If we have a transfer pending, start it.
  2162. * Only start it if the interface has already started transferring
  2163. * it's data
  2164. */
  2165. if (host->dma.xfer_required)
  2166. acornscsi_dma_xfer(host);
  2167. if (ret == INTR_NEXT_COMMAND)
  2168. ret = acornscsi_kick(host);
  2169. in_irq = 1;
  2170. } while (ret != INTR_IDLE);
  2171. return IRQ_HANDLED;
  2172. }
  2173. /*=============================================================================================
  2174. * Interfaces between interrupt handler and rest of scsi code
  2175. */
  2176. /*
  2177. * Function : acornscsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  2178. * Purpose : queues a SCSI command
  2179. * Params : cmd - SCSI command
  2180. * done - function called on completion, with pointer to command descriptor
  2181. * Returns : 0, or < 0 on error.
  2182. */
  2183. int acornscsi_queuecmd(struct scsi_cmnd *SCpnt,
  2184. void (*done)(struct scsi_cmnd *))
  2185. {
  2186. AS_Host *host = (AS_Host *)SCpnt->device->host->hostdata;
  2187. if (!done) {
  2188. /* there should be some way of rejecting errors like this without panicing... */
  2189. panic("scsi%d: queuecommand called with NULL done function [cmd=%p]",
  2190. host->host->host_no, SCpnt);
  2191. return -EINVAL;
  2192. }
  2193. #if (DEBUG & DEBUG_NO_WRITE)
  2194. if (acornscsi_cmdtype(SCpnt->cmnd[0]) == CMD_WRITE && (NO_WRITE & (1 << SCpnt->device->id))) {
  2195. printk(KERN_CRIT "scsi%d.%c: WRITE attempted with NO_WRITE flag set\n",
  2196. host->host->host_no, '0' + SCpnt->device->id);
  2197. SCpnt->result = DID_NO_CONNECT << 16;
  2198. done(SCpnt);
  2199. return 0;
  2200. }
  2201. #endif
  2202. SCpnt->scsi_done = done;
  2203. SCpnt->host_scribble = NULL;
  2204. SCpnt->result = 0;
  2205. SCpnt->tag = 0;
  2206. SCpnt->SCp.phase = (int)acornscsi_datadirection(SCpnt->cmnd[0]);
  2207. SCpnt->SCp.sent_command = 0;
  2208. SCpnt->SCp.scsi_xferred = 0;
  2209. init_SCp(SCpnt);
  2210. host->stats.queues += 1;
  2211. {
  2212. unsigned long flags;
  2213. if (!queue_add_cmd_ordered(&host->queues.issue, SCpnt)) {
  2214. SCpnt->result = DID_ERROR << 16;
  2215. done(SCpnt);
  2216. return 0;
  2217. }
  2218. local_irq_save(flags);
  2219. if (host->scsi.phase == PHASE_IDLE)
  2220. acornscsi_kick(host);
  2221. local_irq_restore(flags);
  2222. }
  2223. return 0;
  2224. }
  2225. /*
  2226. * Prototype: void acornscsi_reportstatus(struct scsi_cmnd **SCpntp1, struct scsi_cmnd **SCpntp2, int result)
  2227. * Purpose : pass a result to *SCpntp1, and check if *SCpntp1 = *SCpntp2
  2228. * Params : SCpntp1 - pointer to command to return
  2229. * SCpntp2 - pointer to command to check
  2230. * result - result to pass back to mid-level done function
  2231. * Returns : *SCpntp2 = NULL if *SCpntp1 is the same command structure as *SCpntp2.
  2232. */
  2233. static inline void acornscsi_reportstatus(struct scsi_cmnd **SCpntp1,
  2234. struct scsi_cmnd **SCpntp2,
  2235. int result)
  2236. {
  2237. struct scsi_cmnd *SCpnt = *SCpntp1;
  2238. if (SCpnt) {
  2239. *SCpntp1 = NULL;
  2240. SCpnt->result = result;
  2241. SCpnt->scsi_done(SCpnt);
  2242. }
  2243. if (SCpnt == *SCpntp2)
  2244. *SCpntp2 = NULL;
  2245. }
  2246. enum res_abort { res_not_running, res_success, res_success_clear, res_snooze };
  2247. /*
  2248. * Prototype: enum res acornscsi_do_abort(struct scsi_cmnd *SCpnt)
  2249. * Purpose : abort a command on this host
  2250. * Params : SCpnt - command to abort
  2251. * Returns : our abort status
  2252. */
  2253. static enum res_abort acornscsi_do_abort(AS_Host *host, struct scsi_cmnd *SCpnt)
  2254. {
  2255. enum res_abort res = res_not_running;
  2256. if (queue_remove_cmd(&host->queues.issue, SCpnt)) {
  2257. /*
  2258. * The command was on the issue queue, and has not been
  2259. * issued yet. We can remove the command from the queue,
  2260. * and acknowledge the abort. Neither the devices nor the
  2261. * interface know about the command.
  2262. */
  2263. //#if (DEBUG & DEBUG_ABORT)
  2264. printk("on issue queue ");
  2265. //#endif
  2266. res = res_success;
  2267. } else if (queue_remove_cmd(&host->queues.disconnected, SCpnt)) {
  2268. /*
  2269. * The command was on the disconnected queue. Simply
  2270. * acknowledge the abort condition, and when the target
  2271. * reconnects, we will give it an ABORT message. The
  2272. * target should then disconnect, and we will clear
  2273. * the busylun bit.
  2274. */
  2275. //#if (DEBUG & DEBUG_ABORT)
  2276. printk("on disconnected queue ");
  2277. //#endif
  2278. res = res_success;
  2279. } else if (host->SCpnt == SCpnt) {
  2280. unsigned long flags;
  2281. //#if (DEBUG & DEBUG_ABORT)
  2282. printk("executing ");
  2283. //#endif
  2284. local_irq_save(flags);
  2285. switch (host->scsi.phase) {
  2286. /*
  2287. * If the interface is idle, and the command is 'disconnectable',
  2288. * then it is the same as on the disconnected queue. We simply
  2289. * remove all traces of the command. When the target reconnects,
  2290. * we will give it an ABORT message since the command could not
  2291. * be found. When the target finally disconnects, we will clear
  2292. * the busylun bit.
  2293. */
  2294. case PHASE_IDLE:
  2295. if (host->scsi.disconnectable) {
  2296. host->scsi.disconnectable = 0;
  2297. host->SCpnt = NULL;
  2298. res = res_success;
  2299. }
  2300. break;
  2301. /*
  2302. * If the command has connected and done nothing further,
  2303. * simply force a disconnect. We also need to clear the
  2304. * busylun bit.
  2305. */
  2306. case PHASE_CONNECTED:
  2307. sbic_arm_write(host->scsi.io_port, SBIC_CMND, CMND_DISCONNECT);
  2308. host->SCpnt = NULL;
  2309. res = res_success_clear;
  2310. break;
  2311. default:
  2312. acornscsi_abortcmd(host, host->SCpnt->tag);
  2313. res = res_snooze;
  2314. }
  2315. local_irq_restore(flags);
  2316. } else if (host->origSCpnt == SCpnt) {
  2317. /*
  2318. * The command will be executed next, but a command
  2319. * is currently using the interface. This is similar to
  2320. * being on the issue queue, except the busylun bit has
  2321. * been set.
  2322. */
  2323. host->origSCpnt = NULL;
  2324. //#if (DEBUG & DEBUG_ABORT)
  2325. printk("waiting for execution ");
  2326. //#endif
  2327. res = res_success_clear;
  2328. } else
  2329. printk("unknown ");
  2330. return res;
  2331. }
  2332. /*
  2333. * Prototype: int acornscsi_abort(struct scsi_cmnd *SCpnt)
  2334. * Purpose : abort a command on this host
  2335. * Params : SCpnt - command to abort
  2336. * Returns : one of SCSI_ABORT_ macros
  2337. */
  2338. int acornscsi_abort(struct scsi_cmnd *SCpnt)
  2339. {
  2340. AS_Host *host = (AS_Host *) SCpnt->device->host->hostdata;
  2341. int result;
  2342. host->stats.aborts += 1;
  2343. #if (DEBUG & DEBUG_ABORT)
  2344. {
  2345. int asr, ssr;
  2346. asr = sbic_arm_read(host->scsi.io_port, SBIC_ASR);
  2347. ssr = sbic_arm_read(host->scsi.io_port, SBIC_SSR);
  2348. printk(KERN_WARNING "acornscsi_abort: ");
  2349. print_sbic_status(asr, ssr, host->scsi.phase);
  2350. acornscsi_dumplog(host, SCpnt->device->id);
  2351. }
  2352. #endif
  2353. printk("scsi%d: ", host->host->host_no);
  2354. switch (acornscsi_do_abort(host, SCpnt)) {
  2355. /*
  2356. * We managed to find the command and cleared it out.
  2357. * We do not expect the command to be executing on the
  2358. * target, but we have set the busylun bit.
  2359. */
  2360. case res_success_clear:
  2361. //#if (DEBUG & DEBUG_ABORT)
  2362. printk("clear ");
  2363. //#endif
  2364. clear_bit(SCpnt->device->id * 8 + SCpnt->device->lun, host->busyluns);
  2365. /*
  2366. * We found the command, and cleared it out. Either
  2367. * the command is still known to be executing on the
  2368. * target, or the busylun bit is not set.
  2369. */
  2370. case res_success:
  2371. //#if (DEBUG & DEBUG_ABORT)
  2372. printk("success\n");
  2373. //#endif
  2374. SCpnt->result = DID_ABORT << 16;
  2375. SCpnt->scsi_done(SCpnt);
  2376. result = SCSI_ABORT_SUCCESS;
  2377. break;
  2378. /*
  2379. * We did find the command, but unfortunately we couldn't
  2380. * unhook it from ourselves. Wait some more, and if it
  2381. * still doesn't complete, reset the interface.
  2382. */
  2383. case res_snooze:
  2384. //#if (DEBUG & DEBUG_ABORT)
  2385. printk("snooze\n");
  2386. //#endif
  2387. result = SCSI_ABORT_SNOOZE;
  2388. break;
  2389. /*
  2390. * The command could not be found (either because it completed,
  2391. * or it got dropped.
  2392. */
  2393. default:
  2394. case res_not_running:
  2395. acornscsi_dumplog(host, SCpnt->device->id);
  2396. #if (DEBUG & DEBUG_ABORT)
  2397. result = SCSI_ABORT_SNOOZE;
  2398. #else
  2399. result = SCSI_ABORT_NOT_RUNNING;
  2400. #endif
  2401. //#if (DEBUG & DEBUG_ABORT)
  2402. printk("not running\n");
  2403. //#endif
  2404. break;
  2405. }
  2406. return result;
  2407. }
  2408. /*
  2409. * Prototype: int acornscsi_reset(struct scsi_cmnd *SCpnt, unsigned int reset_flags)
  2410. * Purpose : reset a command on this host/reset this host
  2411. * Params : SCpnt - command causing reset
  2412. * result - what type of reset to perform
  2413. * Returns : one of SCSI_RESET_ macros
  2414. */
  2415. int acornscsi_reset(struct scsi_cmnd *SCpnt, unsigned int reset_flags)
  2416. {
  2417. AS_Host *host = (AS_Host *)SCpnt->device->host->hostdata;
  2418. struct scsi_cmnd *SCptr;
  2419. host->stats.resets += 1;
  2420. #if (DEBUG & DEBUG_RESET)
  2421. {
  2422. int asr, ssr;
  2423. asr = sbic_arm_read(host->scsi.io_port, SBIC_ASR);
  2424. ssr = sbic_arm_read(host->scsi.io_port, SBIC_SSR);
  2425. printk(KERN_WARNING "acornscsi_reset: ");
  2426. print_sbic_status(asr, ssr, host->scsi.phase);
  2427. acornscsi_dumplog(host, SCpnt->device->id);
  2428. }
  2429. #endif
  2430. acornscsi_dma_stop(host);
  2431. SCptr = host->SCpnt;
  2432. /*
  2433. * do hard reset. This resets all devices on this host, and so we
  2434. * must set the reset status on all commands.
  2435. */
  2436. acornscsi_resetcard(host);
  2437. /*
  2438. * report reset on commands current connected/disconnected
  2439. */
  2440. acornscsi_reportstatus(&host->SCpnt, &SCptr, DID_RESET);
  2441. while ((SCptr = queue_remove(&host->queues.disconnected)) != NULL)
  2442. acornscsi_reportstatus(&SCptr, &SCpnt, DID_RESET);
  2443. if (SCpnt) {
  2444. SCpnt->result = DID_RESET << 16;
  2445. SCpnt->scsi_done(SCpnt);
  2446. }
  2447. return SCSI_RESET_BUS_RESET | SCSI_RESET_HOST_RESET | SCSI_RESET_SUCCESS;
  2448. }
  2449. /*==============================================================================================
  2450. * initialisation & miscellaneous support
  2451. */
  2452. /*
  2453. * Function: char *acornscsi_info(struct Scsi_Host *host)
  2454. * Purpose : return a string describing this interface
  2455. * Params : host - host to give information on
  2456. * Returns : a constant string
  2457. */
  2458. const
  2459. char *acornscsi_info(struct Scsi_Host *host)
  2460. {
  2461. static char string[100], *p;
  2462. p = string;
  2463. p += sprintf(string, "%s at port %08lX irq %d v%d.%d.%d"
  2464. #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
  2465. " SYNC"
  2466. #endif
  2467. #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
  2468. " TAG"
  2469. #endif
  2470. #ifdef CONFIG_SCSI_ACORNSCSI_LINK
  2471. " LINK"
  2472. #endif
  2473. #if (DEBUG & DEBUG_NO_WRITE)
  2474. " NOWRITE ("NO_WRITE_STR")"
  2475. #endif
  2476. , host->hostt->name, host->io_port, host->irq,
  2477. VER_MAJOR, VER_MINOR, VER_PATCH);
  2478. return string;
  2479. }
  2480. int acornscsi_proc_info(struct Scsi_Host *instance, char *buffer, char **start, off_t offset,
  2481. int length, int inout)
  2482. {
  2483. int pos, begin = 0, devidx;
  2484. struct scsi_device *scd;
  2485. AS_Host *host;
  2486. char *p = buffer;
  2487. if (inout == 1)
  2488. return -EINVAL;
  2489. host = (AS_Host *)instance->hostdata;
  2490. p += sprintf(p, "AcornSCSI driver v%d.%d.%d"
  2491. #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
  2492. " SYNC"
  2493. #endif
  2494. #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
  2495. " TAG"
  2496. #endif
  2497. #ifdef CONFIG_SCSI_ACORNSCSI_LINK
  2498. " LINK"
  2499. #endif
  2500. #if (DEBUG & DEBUG_NO_WRITE)
  2501. " NOWRITE ("NO_WRITE_STR")"
  2502. #endif
  2503. "\n\n", VER_MAJOR, VER_MINOR, VER_PATCH);
  2504. p += sprintf(p, "SBIC: WD33C93A Address: %08X IRQ : %d\n",
  2505. host->scsi.io_port, host->scsi.irq);
  2506. #ifdef USE_DMAC
  2507. p += sprintf(p, "DMAC: uPC71071 Address: %08X IRQ : %d\n\n",
  2508. host->dma.io_port, host->scsi.irq);
  2509. #endif
  2510. p += sprintf(p, "Statistics:\n"
  2511. "Queued commands: %-10u Issued commands: %-10u\n"
  2512. "Done commands : %-10u Reads : %-10u\n"
  2513. "Writes : %-10u Others : %-10u\n"
  2514. "Disconnects : %-10u Aborts : %-10u\n"
  2515. "Resets : %-10u\n\nLast phases:",
  2516. host->stats.queues, host->stats.removes,
  2517. host->stats.fins, host->stats.reads,
  2518. host->stats.writes, host->stats.miscs,
  2519. host->stats.disconnects, host->stats.aborts,
  2520. host->stats.resets);
  2521. for (devidx = 0; devidx < 9; devidx ++) {
  2522. unsigned int statptr, prev;
  2523. p += sprintf(p, "\n%c:", devidx == 8 ? 'H' : ('0' + devidx));
  2524. statptr = host->status_ptr[devidx] - 10;
  2525. if ((signed int)statptr < 0)
  2526. statptr += STATUS_BUFFER_SIZE;
  2527. prev = host->status[devidx][statptr].when;
  2528. for (; statptr != host->status_ptr[devidx]; statptr = (statptr + 1) & (STATUS_BUFFER_SIZE - 1)) {
  2529. if (host->status[devidx][statptr].when) {
  2530. p += sprintf(p, "%c%02X:%02X+%2ld",
  2531. host->status[devidx][statptr].irq ? '-' : ' ',
  2532. host->status[devidx][statptr].ph,
  2533. host->status[devidx][statptr].ssr,
  2534. (host->status[devidx][statptr].when - prev) < 100 ?
  2535. (host->status[devidx][statptr].when - prev) : 99);
  2536. prev = host->status[devidx][statptr].when;
  2537. }
  2538. }
  2539. }
  2540. p += sprintf(p, "\nAttached devices:\n");
  2541. shost_for_each_device(scd, instance) {
  2542. p += sprintf(p, "Device/Lun TaggedQ Sync\n");
  2543. p += sprintf(p, " %d/%d ", scd->id, scd->lun);
  2544. if (scd->tagged_supported)
  2545. p += sprintf(p, "%3sabled(%3d) ",
  2546. scd->simple_tags ? "en" : "dis",
  2547. scd->current_tag);
  2548. else
  2549. p += sprintf(p, "unsupported ");
  2550. if (host->device[scd->id].sync_xfer & 15)
  2551. p += sprintf(p, "offset %d, %d ns\n",
  2552. host->device[scd->id].sync_xfer & 15,
  2553. acornscsi_getperiod(host->device[scd->id].sync_xfer));
  2554. else
  2555. p += sprintf(p, "async\n");
  2556. pos = p - buffer;
  2557. if (pos + begin < offset) {
  2558. begin += pos;
  2559. p = buffer;
  2560. }
  2561. pos = p - buffer;
  2562. if (pos + begin > offset + length) {
  2563. scsi_device_put(scd);
  2564. break;
  2565. }
  2566. }
  2567. pos = p - buffer;
  2568. *start = buffer + (offset - begin);
  2569. pos -= offset - begin;
  2570. if (pos > length)
  2571. pos = length;
  2572. return pos;
  2573. }
  2574. static struct scsi_host_template acornscsi_template = {
  2575. .module = THIS_MODULE,
  2576. .proc_info = acornscsi_proc_info,
  2577. .name = "AcornSCSI",
  2578. .info = acornscsi_info,
  2579. .queuecommand = acornscsi_queuecmd,
  2580. #warning fixme
  2581. .abort = acornscsi_abort,
  2582. .reset = acornscsi_reset,
  2583. .can_queue = 16,
  2584. .this_id = 7,
  2585. .sg_tablesize = SG_ALL,
  2586. .cmd_per_lun = 2,
  2587. .use_clustering = DISABLE_CLUSTERING,
  2588. .proc_name = "acornscsi",
  2589. };
  2590. static int __devinit
  2591. acornscsi_probe(struct expansion_card *ec, const struct ecard_id *id)
  2592. {
  2593. struct Scsi_Host *host;
  2594. AS_Host *ashost;
  2595. int ret = -ENOMEM;
  2596. host = scsi_host_alloc(&acornscsi_template, sizeof(AS_Host));
  2597. if (!host)
  2598. goto out;
  2599. ashost = (AS_Host *)host->hostdata;
  2600. host->io_port = ecard_address(ec, ECARD_MEMC, 0);
  2601. host->irq = ec->irq;
  2602. ashost->host = host;
  2603. ashost->scsi.io_port = ioaddr(host->io_port + 0x800);
  2604. ashost->scsi.irq = host->irq;
  2605. ashost->card.io_intr = POD_SPACE(host->io_port) + 0x800;
  2606. ashost->card.io_page = POD_SPACE(host->io_port) + 0xc00;
  2607. ashost->card.io_ram = ioaddr(host->io_port);
  2608. ashost->dma.io_port = host->io_port + 0xc00;
  2609. ashost->dma.io_intr_clear = POD_SPACE(host->io_port) + 0x800;
  2610. ec->irqaddr = (char *)ioaddr(ashost->card.io_intr);
  2611. ec->irqmask = 0x0a;
  2612. ret = -EBUSY;
  2613. if (!request_region(host->io_port + 0x800, 2, "acornscsi(sbic)"))
  2614. goto err_1;
  2615. if (!request_region(ashost->card.io_intr, 1, "acornscsi(intr)"))
  2616. goto err_2;
  2617. if (!request_region(ashost->card.io_page, 1, "acornscsi(page)"))
  2618. goto err_3;
  2619. #ifdef USE_DMAC
  2620. if (!request_region(ashost->dma.io_port, 256, "acornscsi(dmac)"))
  2621. goto err_4;
  2622. #endif
  2623. if (!request_region(host->io_port, 2048, "acornscsi(ram)"))
  2624. goto err_5;
  2625. ret = request_irq(host->irq, acornscsi_intr, IRQF_DISABLED, "acornscsi", ashost);
  2626. if (ret) {
  2627. printk(KERN_CRIT "scsi%d: IRQ%d not free: %d\n",
  2628. host->host_no, ashost->scsi.irq, ret);
  2629. goto err_6;
  2630. }
  2631. memset(&ashost->stats, 0, sizeof (ashost->stats));
  2632. queue_initialise(&ashost->queues.issue);
  2633. queue_initialise(&ashost->queues.disconnected);
  2634. msgqueue_initialise(&ashost->scsi.msgs);
  2635. acornscsi_resetcard(ashost);
  2636. ret = scsi_add_host(host, &ec->dev);
  2637. if (ret)
  2638. goto err_7;
  2639. scsi_scan_host(host);
  2640. goto out;
  2641. err_7:
  2642. free_irq(host->irq, ashost);
  2643. err_6:
  2644. release_region(host->io_port, 2048);
  2645. err_5:
  2646. #ifdef USE_DMAC
  2647. release_region(ashost->dma.io_port, 256);
  2648. #endif
  2649. err_4:
  2650. release_region(ashost->card.io_page, 1);
  2651. err_3:
  2652. release_region(ashost->card.io_intr, 1);
  2653. err_2:
  2654. release_region(host->io_port + 0x800, 2);
  2655. err_1:
  2656. scsi_host_put(host);
  2657. out:
  2658. return ret;
  2659. }
  2660. static void __devexit acornscsi_remove(struct expansion_card *ec)
  2661. {
  2662. struct Scsi_Host *host = ecard_get_drvdata(ec);
  2663. AS_Host *ashost = (AS_Host *)host->hostdata;
  2664. ecard_set_drvdata(ec, NULL);
  2665. scsi_remove_host(host);
  2666. /*
  2667. * Put card into RESET state
  2668. */
  2669. outb(0x80, ashost->card.io_page);
  2670. free_irq(host->irq, ashost);
  2671. release_region(host->io_port + 0x800, 2);
  2672. release_region(ashost->card.io_intr, 1);
  2673. release_region(ashost->card.io_page, 1);
  2674. release_region(ashost->dma.io_port, 256);
  2675. release_region(host->io_port, 2048);
  2676. msgqueue_free(&ashost->scsi.msgs);
  2677. queue_free(&ashost->queues.disconnected);
  2678. queue_free(&ashost->queues.issue);
  2679. scsi_host_put(host);
  2680. }
  2681. static const struct ecard_id acornscsi_cids[] = {
  2682. { MANU_ACORN, PROD_ACORN_SCSI },
  2683. { 0xffff, 0xffff },
  2684. };
  2685. static struct ecard_driver acornscsi_driver = {
  2686. .probe = acornscsi_probe,
  2687. .remove = __devexit_p(acornscsi_remove),
  2688. .id_table = acornscsi_cids,
  2689. .drv = {
  2690. .name = "acornscsi",
  2691. },
  2692. };
  2693. static int __init acornscsi_init(void)
  2694. {
  2695. return ecard_register_driver(&acornscsi_driver);
  2696. }
  2697. static void __exit acornscsi_exit(void)
  2698. {
  2699. ecard_remove_driver(&acornscsi_driver);
  2700. }
  2701. module_init(acornscsi_init);
  2702. module_exit(acornscsi_exit);
  2703. MODULE_AUTHOR("Russell King");
  2704. MODULE_DESCRIPTION("AcornSCSI driver");
  2705. MODULE_LICENSE("GPL");