acornscsi.c 88 KB

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