acornscsi.c 88 KB

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