acornscsi.c 86 KB

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