acornscsi.c 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131
  1. /*
  2. * linux/drivers/acorn/scsi/acornscsi.c
  3. *
  4. * Acorn SCSI 3 driver
  5. * By R.M.King.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Abandoned using the Select and Transfer command since there were
  12. * some nasty races between our software and the target devices that
  13. * were not easy to solve, and the device errata had a lot of entries
  14. * for this command, some of them quite nasty...
  15. *
  16. * Changelog:
  17. * 26-Sep-1997 RMK Re-jigged to use the queue module.
  18. * Re-coded state machine to be based on driver
  19. * state not scsi state. Should be easier to debug.
  20. * Added acornscsi_release to clean up properly.
  21. * Updated proc/scsi reporting.
  22. * 05-Oct-1997 RMK Implemented writing to SCSI devices.
  23. * 06-Oct-1997 RMK Corrected small (non-serious) bug with the connect/
  24. * reconnect race condition causing a warning message.
  25. * 12-Oct-1997 RMK Added catch for re-entering interrupt routine.
  26. * 15-Oct-1997 RMK Improved handling of commands.
  27. * 27-Jun-1998 RMK Changed asm/delay.h to linux/delay.h.
  28. * 13-Dec-1998 RMK Better abort code and command handling. Extra state
  29. * transitions added to allow dodgy devices to work.
  30. */
  31. #define DEBUG_NO_WRITE 1
  32. #define DEBUG_QUEUES 2
  33. #define DEBUG_DMA 4
  34. #define DEBUG_ABORT 8
  35. #define DEBUG_DISCON 16
  36. #define DEBUG_CONNECT 32
  37. #define DEBUG_PHASES 64
  38. #define DEBUG_WRITE 128
  39. #define DEBUG_LINK 256
  40. #define DEBUG_MESSAGES 512
  41. #define DEBUG_RESET 1024
  42. #define DEBUG_ALL (DEBUG_RESET|DEBUG_MESSAGES|DEBUG_LINK|DEBUG_WRITE|\
  43. DEBUG_PHASES|DEBUG_CONNECT|DEBUG_DISCON|DEBUG_ABORT|\
  44. DEBUG_DMA|DEBUG_QUEUES)
  45. /* DRIVER CONFIGURATION
  46. *
  47. * SCSI-II Tagged queue support.
  48. *
  49. * I don't have any SCSI devices that support it, so it is totally untested
  50. * (except to make sure that it doesn't interfere with any non-tagging
  51. * devices). It is not fully implemented either - what happens when a
  52. * tagging device reconnects???
  53. *
  54. * You can tell if you have a device that supports tagged queueing my
  55. * cating (eg) /proc/scsi/acornscsi/0 and see if the SCSI revision is reported
  56. * as '2 TAG'.
  57. *
  58. * Also note that CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE is normally set in the config
  59. * scripts, but disabled here. Once debugged, remove the #undef, otherwise to debug,
  60. * comment out the undef.
  61. */
  62. #undef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
  63. /*
  64. * SCSI-II Linked command support.
  65. *
  66. * The higher level code doesn't support linked commands yet, and so the option
  67. * is undef'd here.
  68. */
  69. #undef CONFIG_SCSI_ACORNSCSI_LINK
  70. /*
  71. * SCSI-II Synchronous transfer support.
  72. *
  73. * Tried and tested...
  74. *
  75. * SDTR_SIZE - maximum number of un-acknowledged bytes (0 = off, 12 = max)
  76. * SDTR_PERIOD - period of REQ signal (min=125, max=1020)
  77. * DEFAULT_PERIOD - default REQ period.
  78. */
  79. #define SDTR_SIZE 12
  80. #define SDTR_PERIOD 125
  81. #define DEFAULT_PERIOD 500
  82. /*
  83. * Debugging information
  84. *
  85. * DEBUG - bit mask from list above
  86. * DEBUG_TARGET - is defined to the target number if you want to debug
  87. * a specific target. [only recon/write/dma].
  88. */
  89. #define DEBUG (DEBUG_RESET|DEBUG_WRITE|DEBUG_NO_WRITE)
  90. /* only allow writing to SCSI device 0 */
  91. #define NO_WRITE 0xFE
  92. /*#define DEBUG_TARGET 2*/
  93. /*
  94. * Select timeout time (in 10ms units)
  95. *
  96. * This is the timeout used between the start of selection and the WD33C93
  97. * chip deciding that the device isn't responding.
  98. */
  99. #define TIMEOUT_TIME 10
  100. /*
  101. * Define this if you want to have verbose explaination of SCSI
  102. * status/messages.
  103. */
  104. #undef CONFIG_ACORNSCSI_CONSTANTS
  105. /*
  106. * Define this if you want to use the on board DMAC [don't remove this option]
  107. * If not set, then use PIO mode (not currently supported).
  108. */
  109. #define USE_DMAC
  110. /*
  111. * ====================================================================================
  112. */
  113. #ifdef DEBUG_TARGET
  114. #define DBG(cmd,xxx...) \
  115. if (cmd->device->id == DEBUG_TARGET) { \
  116. xxx; \
  117. }
  118. #else
  119. #define DBG(cmd,xxx...) xxx
  120. #endif
  121. #ifndef STRINGIFY
  122. #define STRINGIFY(x) #x
  123. #endif
  124. #define STRx(x) STRINGIFY(x)
  125. #define NO_WRITE_STR STRx(NO_WRITE)
  126. #include <linux/config.h>
  127. #include <linux/module.h>
  128. #include <linux/kernel.h>
  129. #include <linux/sched.h>
  130. #include <linux/string.h>
  131. #include <linux/signal.h>
  132. #include <linux/errno.h>
  133. #include <linux/proc_fs.h>
  134. #include <linux/ioport.h>
  135. #include <linux/blkdev.h>
  136. #include <linux/delay.h>
  137. #include <linux/interrupt.h>
  138. #include <linux/init.h>
  139. #include <linux/bitops.h>
  140. #include <asm/system.h>
  141. #include <asm/io.h>
  142. #include <asm/ecard.h>
  143. #include "../scsi.h"
  144. #include <scsi/scsi_dbg.h>
  145. #include <scsi/scsi_host.h>
  146. #include <scsi/scsi_transport_spi.h>
  147. #include "acornscsi.h"
  148. #include "msgqueue.h"
  149. #include "scsi.h"
  150. #include <scsi/scsicam.h>
  151. #define VER_MAJOR 2
  152. #define VER_MINOR 0
  153. #define VER_PATCH 6
  154. #ifndef ABORT_TAG
  155. #define ABORT_TAG 0xd
  156. #else
  157. #error "Yippee! ABORT TAG is now defined! Remove this error!"
  158. #endif
  159. #ifdef CONFIG_SCSI_ACORNSCSI_LINK
  160. #error SCSI2 LINKed commands not supported (yet)!
  161. #endif
  162. #ifdef USE_DMAC
  163. /*
  164. * DMAC setup parameters
  165. */
  166. #define INIT_DEVCON0 (DEVCON0_RQL|DEVCON0_EXW|DEVCON0_CMP)
  167. #define INIT_DEVCON1 (DEVCON1_BHLD)
  168. #define DMAC_READ (MODECON_READ)
  169. #define DMAC_WRITE (MODECON_WRITE)
  170. #define INIT_SBICDMA (CTRL_DMABURST)
  171. #define scsi_xferred have_data_in
  172. /*
  173. * Size of on-board DMA buffer
  174. */
  175. #define DMAC_BUFFER_SIZE 65536
  176. #endif
  177. #define STATUS_BUFFER_TO_PRINT 24
  178. unsigned int sdtr_period = SDTR_PERIOD;
  179. unsigned int sdtr_size = SDTR_SIZE;
  180. static void acornscsi_done(AS_Host *host, Scsi_Cmnd **SCpntp, 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. 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, 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
  695. void acornscsi_done(AS_Host *host, Scsi_Cmnd **SCpntp, unsigned int result)
  696. {
  697. 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. 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. 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(Scsi_Cmnd *cmd, void (*done)(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(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
  2186. {
  2187. AS_Host *host = (AS_Host *)SCpnt->device->host->hostdata;
  2188. if (!done) {
  2189. /* there should be some way of rejecting errors like this without panicing... */
  2190. panic("scsi%d: queuecommand called with NULL done function [cmd=%p]",
  2191. host->host->host_no, SCpnt);
  2192. return -EINVAL;
  2193. }
  2194. #if (DEBUG & DEBUG_NO_WRITE)
  2195. if (acornscsi_cmdtype(SCpnt->cmnd[0]) == CMD_WRITE && (NO_WRITE & (1 << SCpnt->device->id))) {
  2196. printk(KERN_CRIT "scsi%d.%c: WRITE attempted with NO_WRITE flag set\n",
  2197. host->host->host_no, '0' + SCpnt->device->id);
  2198. SCpnt->result = DID_NO_CONNECT << 16;
  2199. done(SCpnt);
  2200. return 0;
  2201. }
  2202. #endif
  2203. SCpnt->scsi_done = done;
  2204. SCpnt->host_scribble = NULL;
  2205. SCpnt->result = 0;
  2206. SCpnt->tag = 0;
  2207. SCpnt->SCp.phase = (int)acornscsi_datadirection(SCpnt->cmnd[0]);
  2208. SCpnt->SCp.sent_command = 0;
  2209. SCpnt->SCp.scsi_xferred = 0;
  2210. init_SCp(SCpnt);
  2211. host->stats.queues += 1;
  2212. {
  2213. unsigned long flags;
  2214. if (!queue_add_cmd_ordered(&host->queues.issue, SCpnt)) {
  2215. SCpnt->result = DID_ERROR << 16;
  2216. done(SCpnt);
  2217. return 0;
  2218. }
  2219. local_irq_save(flags);
  2220. if (host->scsi.phase == PHASE_IDLE)
  2221. acornscsi_kick(host);
  2222. local_irq_restore(flags);
  2223. }
  2224. return 0;
  2225. }
  2226. /*
  2227. * Prototype: void acornscsi_reportstatus(Scsi_Cmnd **SCpntp1, Scsi_Cmnd **SCpntp2, int result)
  2228. * Purpose : pass a result to *SCpntp1, and check if *SCpntp1 = *SCpntp2
  2229. * Params : SCpntp1 - pointer to command to return
  2230. * SCpntp2 - pointer to command to check
  2231. * result - result to pass back to mid-level done function
  2232. * Returns : *SCpntp2 = NULL if *SCpntp1 is the same command structure as *SCpntp2.
  2233. */
  2234. static inline
  2235. void acornscsi_reportstatus(Scsi_Cmnd **SCpntp1, Scsi_Cmnd **SCpntp2, int result)
  2236. {
  2237. Scsi_Cmnd *SCpnt = *SCpntp1;
  2238. if (SCpnt) {
  2239. *SCpntp1 = NULL;
  2240. SCpnt->result = result;
  2241. SCpnt->scsi_done(SCpnt);
  2242. }
  2243. if (SCpnt == *SCpntp2)
  2244. *SCpntp2 = NULL;
  2245. }
  2246. enum res_abort { res_not_running, res_success, res_success_clear, res_snooze };
  2247. /*
  2248. * Prototype: enum res acornscsi_do_abort(Scsi_Cmnd *SCpnt)
  2249. * Purpose : abort a command on this host
  2250. * Params : SCpnt - command to abort
  2251. * Returns : our abort status
  2252. */
  2253. static enum res_abort
  2254. acornscsi_do_abort(AS_Host *host, Scsi_Cmnd *SCpnt)
  2255. {
  2256. enum res_abort res = res_not_running;
  2257. if (queue_remove_cmd(&host->queues.issue, SCpnt)) {
  2258. /*
  2259. * The command was on the issue queue, and has not been
  2260. * issued yet. We can remove the command from the queue,
  2261. * and acknowledge the abort. Neither the devices nor the
  2262. * interface know about the command.
  2263. */
  2264. //#if (DEBUG & DEBUG_ABORT)
  2265. printk("on issue queue ");
  2266. //#endif
  2267. res = res_success;
  2268. } else if (queue_remove_cmd(&host->queues.disconnected, SCpnt)) {
  2269. /*
  2270. * The command was on the disconnected queue. Simply
  2271. * acknowledge the abort condition, and when the target
  2272. * reconnects, we will give it an ABORT message. The
  2273. * target should then disconnect, and we will clear
  2274. * the busylun bit.
  2275. */
  2276. //#if (DEBUG & DEBUG_ABORT)
  2277. printk("on disconnected queue ");
  2278. //#endif
  2279. res = res_success;
  2280. } else if (host->SCpnt == SCpnt) {
  2281. unsigned long flags;
  2282. //#if (DEBUG & DEBUG_ABORT)
  2283. printk("executing ");
  2284. //#endif
  2285. local_irq_save(flags);
  2286. switch (host->scsi.phase) {
  2287. /*
  2288. * If the interface is idle, and the command is 'disconnectable',
  2289. * then it is the same as on the disconnected queue. We simply
  2290. * remove all traces of the command. When the target reconnects,
  2291. * we will give it an ABORT message since the command could not
  2292. * be found. When the target finally disconnects, we will clear
  2293. * the busylun bit.
  2294. */
  2295. case PHASE_IDLE:
  2296. if (host->scsi.disconnectable) {
  2297. host->scsi.disconnectable = 0;
  2298. host->SCpnt = NULL;
  2299. res = res_success;
  2300. }
  2301. break;
  2302. /*
  2303. * If the command has connected and done nothing further,
  2304. * simply force a disconnect. We also need to clear the
  2305. * busylun bit.
  2306. */
  2307. case PHASE_CONNECTED:
  2308. sbic_arm_write(host->scsi.io_port, SBIC_CMND, CMND_DISCONNECT);
  2309. host->SCpnt = NULL;
  2310. res = res_success_clear;
  2311. break;
  2312. default:
  2313. acornscsi_abortcmd(host, host->SCpnt->tag);
  2314. res = res_snooze;
  2315. }
  2316. local_irq_restore(flags);
  2317. } else if (host->origSCpnt == SCpnt) {
  2318. /*
  2319. * The command will be executed next, but a command
  2320. * is currently using the interface. This is similar to
  2321. * being on the issue queue, except the busylun bit has
  2322. * been set.
  2323. */
  2324. host->origSCpnt = NULL;
  2325. //#if (DEBUG & DEBUG_ABORT)
  2326. printk("waiting for execution ");
  2327. //#endif
  2328. res = res_success_clear;
  2329. } else
  2330. printk("unknown ");
  2331. return res;
  2332. }
  2333. /*
  2334. * Prototype: int acornscsi_abort(Scsi_Cmnd *SCpnt)
  2335. * Purpose : abort a command on this host
  2336. * Params : SCpnt - command to abort
  2337. * Returns : one of SCSI_ABORT_ macros
  2338. */
  2339. int acornscsi_abort(Scsi_Cmnd *SCpnt)
  2340. {
  2341. AS_Host *host = (AS_Host *) SCpnt->device->host->hostdata;
  2342. int result;
  2343. host->stats.aborts += 1;
  2344. #if (DEBUG & DEBUG_ABORT)
  2345. {
  2346. int asr, ssr;
  2347. asr = sbic_arm_read(host->scsi.io_port, SBIC_ASR);
  2348. ssr = sbic_arm_read(host->scsi.io_port, SBIC_SSR);
  2349. printk(KERN_WARNING "acornscsi_abort: ");
  2350. print_sbic_status(asr, ssr, host->scsi.phase);
  2351. acornscsi_dumplog(host, SCpnt->device->id);
  2352. }
  2353. #endif
  2354. printk("scsi%d: ", host->host->host_no);
  2355. switch (acornscsi_do_abort(host, SCpnt)) {
  2356. /*
  2357. * We managed to find the command and cleared it out.
  2358. * We do not expect the command to be executing on the
  2359. * target, but we have set the busylun bit.
  2360. */
  2361. case res_success_clear:
  2362. //#if (DEBUG & DEBUG_ABORT)
  2363. printk("clear ");
  2364. //#endif
  2365. clear_bit(SCpnt->device->id * 8 + SCpnt->device->lun, host->busyluns);
  2366. /*
  2367. * We found the command, and cleared it out. Either
  2368. * the command is still known to be executing on the
  2369. * target, or the busylun bit is not set.
  2370. */
  2371. case res_success:
  2372. //#if (DEBUG & DEBUG_ABORT)
  2373. printk("success\n");
  2374. //#endif
  2375. SCpnt->result = DID_ABORT << 16;
  2376. SCpnt->scsi_done(SCpnt);
  2377. result = SCSI_ABORT_SUCCESS;
  2378. break;
  2379. /*
  2380. * We did find the command, but unfortunately we couldn't
  2381. * unhook it from ourselves. Wait some more, and if it
  2382. * still doesn't complete, reset the interface.
  2383. */
  2384. case res_snooze:
  2385. //#if (DEBUG & DEBUG_ABORT)
  2386. printk("snooze\n");
  2387. //#endif
  2388. result = SCSI_ABORT_SNOOZE;
  2389. break;
  2390. /*
  2391. * The command could not be found (either because it completed,
  2392. * or it got dropped.
  2393. */
  2394. default:
  2395. case res_not_running:
  2396. acornscsi_dumplog(host, SCpnt->device->id);
  2397. #if (DEBUG & DEBUG_ABORT)
  2398. result = SCSI_ABORT_SNOOZE;
  2399. #else
  2400. result = SCSI_ABORT_NOT_RUNNING;
  2401. #endif
  2402. //#if (DEBUG & DEBUG_ABORT)
  2403. printk("not running\n");
  2404. //#endif
  2405. break;
  2406. }
  2407. return result;
  2408. }
  2409. /*
  2410. * Prototype: int acornscsi_reset(Scsi_Cmnd *SCpnt, unsigned int reset_flags)
  2411. * Purpose : reset a command on this host/reset this host
  2412. * Params : SCpnt - command causing reset
  2413. * result - what type of reset to perform
  2414. * Returns : one of SCSI_RESET_ macros
  2415. */
  2416. int acornscsi_reset(Scsi_Cmnd *SCpnt, unsigned int reset_flags)
  2417. {
  2418. AS_Host *host = (AS_Host *)SCpnt->device->host->hostdata;
  2419. Scsi_Cmnd *SCptr;
  2420. host->stats.resets += 1;
  2421. #if (DEBUG & DEBUG_RESET)
  2422. {
  2423. int asr, ssr;
  2424. asr = sbic_arm_read(host->scsi.io_port, SBIC_ASR);
  2425. ssr = sbic_arm_read(host->scsi.io_port, SBIC_SSR);
  2426. printk(KERN_WARNING "acornscsi_reset: ");
  2427. print_sbic_status(asr, ssr, host->scsi.phase);
  2428. acornscsi_dumplog(host, SCpnt->device->id);
  2429. }
  2430. #endif
  2431. acornscsi_dma_stop(host);
  2432. SCptr = host->SCpnt;
  2433. /*
  2434. * do hard reset. This resets all devices on this host, and so we
  2435. * must set the reset status on all commands.
  2436. */
  2437. acornscsi_resetcard(host);
  2438. /*
  2439. * report reset on commands current connected/disconnected
  2440. */
  2441. acornscsi_reportstatus(&host->SCpnt, &SCptr, DID_RESET);
  2442. while ((SCptr = queue_remove(&host->queues.disconnected)) != NULL)
  2443. acornscsi_reportstatus(&SCptr, &SCpnt, DID_RESET);
  2444. if (SCpnt) {
  2445. SCpnt->result = DID_RESET << 16;
  2446. SCpnt->scsi_done(SCpnt);
  2447. }
  2448. return SCSI_RESET_BUS_RESET | SCSI_RESET_HOST_RESET | SCSI_RESET_SUCCESS;
  2449. }
  2450. /*==============================================================================================
  2451. * initialisation & miscellaneous support
  2452. */
  2453. /*
  2454. * Function: char *acornscsi_info(struct Scsi_Host *host)
  2455. * Purpose : return a string describing this interface
  2456. * Params : host - host to give information on
  2457. * Returns : a constant string
  2458. */
  2459. const
  2460. char *acornscsi_info(struct Scsi_Host *host)
  2461. {
  2462. static char string[100], *p;
  2463. p = string;
  2464. p += sprintf(string, "%s at port %08lX irq %d v%d.%d.%d"
  2465. #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
  2466. " SYNC"
  2467. #endif
  2468. #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
  2469. " TAG"
  2470. #endif
  2471. #ifdef CONFIG_SCSI_ACORNSCSI_LINK
  2472. " LINK"
  2473. #endif
  2474. #if (DEBUG & DEBUG_NO_WRITE)
  2475. " NOWRITE ("NO_WRITE_STR")"
  2476. #endif
  2477. , host->hostt->name, host->io_port, host->irq,
  2478. VER_MAJOR, VER_MINOR, VER_PATCH);
  2479. return string;
  2480. }
  2481. int acornscsi_proc_info(struct Scsi_Host *instance, char *buffer, char **start, off_t offset,
  2482. int length, int inout)
  2483. {
  2484. int pos, begin = 0, devidx;
  2485. struct scsi_device *scd;
  2486. AS_Host *host;
  2487. char *p = buffer;
  2488. if (inout == 1)
  2489. return -EINVAL;
  2490. host = (AS_Host *)instance->hostdata;
  2491. p += sprintf(p, "AcornSCSI driver v%d.%d.%d"
  2492. #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
  2493. " SYNC"
  2494. #endif
  2495. #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
  2496. " TAG"
  2497. #endif
  2498. #ifdef CONFIG_SCSI_ACORNSCSI_LINK
  2499. " LINK"
  2500. #endif
  2501. #if (DEBUG & DEBUG_NO_WRITE)
  2502. " NOWRITE ("NO_WRITE_STR")"
  2503. #endif
  2504. "\n\n", VER_MAJOR, VER_MINOR, VER_PATCH);
  2505. p += sprintf(p, "SBIC: WD33C93A Address: %08X IRQ : %d\n",
  2506. host->scsi.io_port, host->scsi.irq);
  2507. #ifdef USE_DMAC
  2508. p += sprintf(p, "DMAC: uPC71071 Address: %08X IRQ : %d\n\n",
  2509. host->dma.io_port, host->scsi.irq);
  2510. #endif
  2511. p += sprintf(p, "Statistics:\n"
  2512. "Queued commands: %-10u Issued commands: %-10u\n"
  2513. "Done commands : %-10u Reads : %-10u\n"
  2514. "Writes : %-10u Others : %-10u\n"
  2515. "Disconnects : %-10u Aborts : %-10u\n"
  2516. "Resets : %-10u\n\nLast phases:",
  2517. host->stats.queues, host->stats.removes,
  2518. host->stats.fins, host->stats.reads,
  2519. host->stats.writes, host->stats.miscs,
  2520. host->stats.disconnects, host->stats.aborts,
  2521. host->stats.resets);
  2522. for (devidx = 0; devidx < 9; devidx ++) {
  2523. unsigned int statptr, prev;
  2524. p += sprintf(p, "\n%c:", devidx == 8 ? 'H' : ('0' + devidx));
  2525. statptr = host->status_ptr[devidx] - 10;
  2526. if ((signed int)statptr < 0)
  2527. statptr += STATUS_BUFFER_SIZE;
  2528. prev = host->status[devidx][statptr].when;
  2529. for (; statptr != host->status_ptr[devidx]; statptr = (statptr + 1) & (STATUS_BUFFER_SIZE - 1)) {
  2530. if (host->status[devidx][statptr].when) {
  2531. p += sprintf(p, "%c%02X:%02X+%2ld",
  2532. host->status[devidx][statptr].irq ? '-' : ' ',
  2533. host->status[devidx][statptr].ph,
  2534. host->status[devidx][statptr].ssr,
  2535. (host->status[devidx][statptr].when - prev) < 100 ?
  2536. (host->status[devidx][statptr].when - prev) : 99);
  2537. prev = host->status[devidx][statptr].when;
  2538. }
  2539. }
  2540. }
  2541. p += sprintf(p, "\nAttached devices:\n");
  2542. shost_for_each_device(scd, instance) {
  2543. p += sprintf(p, "Device/Lun TaggedQ Sync\n");
  2544. p += sprintf(p, " %d/%d ", scd->id, scd->lun);
  2545. if (scd->tagged_supported)
  2546. p += sprintf(p, "%3sabled(%3d) ",
  2547. scd->simple_tags ? "en" : "dis",
  2548. scd->current_tag);
  2549. else
  2550. p += sprintf(p, "unsupported ");
  2551. if (host->device[scd->id].sync_xfer & 15)
  2552. p += sprintf(p, "offset %d, %d ns\n",
  2553. host->device[scd->id].sync_xfer & 15,
  2554. acornscsi_getperiod(host->device[scd->id].sync_xfer));
  2555. else
  2556. p += sprintf(p, "async\n");
  2557. pos = p - buffer;
  2558. if (pos + begin < offset) {
  2559. begin += pos;
  2560. p = buffer;
  2561. }
  2562. pos = p - buffer;
  2563. if (pos + begin > offset + length) {
  2564. scsi_device_put(scd);
  2565. break;
  2566. }
  2567. }
  2568. pos = p - buffer;
  2569. *start = buffer + (offset - begin);
  2570. pos -= offset - begin;
  2571. if (pos > length)
  2572. pos = length;
  2573. return pos;
  2574. }
  2575. static struct scsi_host_template acornscsi_template = {
  2576. .module = THIS_MODULE,
  2577. .proc_info = acornscsi_proc_info,
  2578. .name = "AcornSCSI",
  2579. .info = acornscsi_info,
  2580. .queuecommand = acornscsi_queuecmd,
  2581. #warning fixme
  2582. .abort = acornscsi_abort,
  2583. .reset = acornscsi_reset,
  2584. .can_queue = 16,
  2585. .this_id = 7,
  2586. .sg_tablesize = SG_ALL,
  2587. .cmd_per_lun = 2,
  2588. .unchecked_isa_dma = 0,
  2589. .use_clustering = DISABLE_CLUSTERING,
  2590. .proc_name = "acornscsi",
  2591. };
  2592. static int __devinit
  2593. acornscsi_probe(struct expansion_card *ec, const struct ecard_id *id)
  2594. {
  2595. struct Scsi_Host *host;
  2596. AS_Host *ashost;
  2597. int ret = -ENOMEM;
  2598. host = scsi_host_alloc(&acornscsi_template, sizeof(AS_Host));
  2599. if (!host)
  2600. goto out;
  2601. ashost = (AS_Host *)host->hostdata;
  2602. host->io_port = ecard_address(ec, ECARD_MEMC, 0);
  2603. host->irq = ec->irq;
  2604. ashost->host = host;
  2605. ashost->scsi.io_port = ioaddr(host->io_port + 0x800);
  2606. ashost->scsi.irq = host->irq;
  2607. ashost->card.io_intr = POD_SPACE(host->io_port) + 0x800;
  2608. ashost->card.io_page = POD_SPACE(host->io_port) + 0xc00;
  2609. ashost->card.io_ram = ioaddr(host->io_port);
  2610. ashost->dma.io_port = host->io_port + 0xc00;
  2611. ashost->dma.io_intr_clear = POD_SPACE(host->io_port) + 0x800;
  2612. ec->irqaddr = (char *)ioaddr(ashost->card.io_intr);
  2613. ec->irqmask = 0x0a;
  2614. ret = -EBUSY;
  2615. if (!request_region(host->io_port + 0x800, 2, "acornscsi(sbic)"))
  2616. goto err_1;
  2617. if (!request_region(ashost->card.io_intr, 1, "acornscsi(intr)"))
  2618. goto err_2;
  2619. if (!request_region(ashost->card.io_page, 1, "acornscsi(page)"))
  2620. goto err_3;
  2621. #ifdef USE_DMAC
  2622. if (!request_region(ashost->dma.io_port, 256, "acornscsi(dmac)"))
  2623. goto err_4;
  2624. #endif
  2625. if (!request_region(host->io_port, 2048, "acornscsi(ram)"))
  2626. goto err_5;
  2627. ret = request_irq(host->irq, acornscsi_intr, SA_INTERRUPT, "acornscsi", ashost);
  2628. if (ret) {
  2629. printk(KERN_CRIT "scsi%d: IRQ%d not free: %d\n",
  2630. host->host_no, ashost->scsi.irq, ret);
  2631. goto err_6;
  2632. }
  2633. memset(&ashost->stats, 0, sizeof (ashost->stats));
  2634. queue_initialise(&ashost->queues.issue);
  2635. queue_initialise(&ashost->queues.disconnected);
  2636. msgqueue_initialise(&ashost->scsi.msgs);
  2637. acornscsi_resetcard(ashost);
  2638. ret = scsi_add_host(host, &ec->dev);
  2639. if (ret)
  2640. goto err_7;
  2641. scsi_scan_host(host);
  2642. goto out;
  2643. err_7:
  2644. free_irq(host->irq, ashost);
  2645. err_6:
  2646. release_region(host->io_port, 2048);
  2647. err_5:
  2648. #ifdef USE_DMAC
  2649. release_region(ashost->dma.io_port, 256);
  2650. #endif
  2651. err_4:
  2652. release_region(ashost->card.io_page, 1);
  2653. err_3:
  2654. release_region(ashost->card.io_intr, 1);
  2655. err_2:
  2656. release_region(host->io_port + 0x800, 2);
  2657. err_1:
  2658. scsi_host_put(host);
  2659. out:
  2660. return ret;
  2661. }
  2662. static void __devexit acornscsi_remove(struct expansion_card *ec)
  2663. {
  2664. struct Scsi_Host *host = ecard_get_drvdata(ec);
  2665. AS_Host *ashost = (AS_Host *)host->hostdata;
  2666. ecard_set_drvdata(ec, NULL);
  2667. scsi_remove_host(host);
  2668. /*
  2669. * Put card into RESET state
  2670. */
  2671. outb(0x80, ashost->card.io_page);
  2672. free_irq(host->irq, ashost);
  2673. release_region(host->io_port + 0x800, 2);
  2674. release_region(ashost->card.io_intr, 1);
  2675. release_region(ashost->card.io_page, 1);
  2676. release_region(ashost->dma.io_port, 256);
  2677. release_region(host->io_port, 2048);
  2678. msgqueue_free(&ashost->scsi.msgs);
  2679. queue_free(&ashost->queues.disconnected);
  2680. queue_free(&ashost->queues.issue);
  2681. scsi_host_put(host);
  2682. }
  2683. static const struct ecard_id acornscsi_cids[] = {
  2684. { MANU_ACORN, PROD_ACORN_SCSI },
  2685. { 0xffff, 0xffff },
  2686. };
  2687. static struct ecard_driver acornscsi_driver = {
  2688. .probe = acornscsi_probe,
  2689. .remove = __devexit_p(acornscsi_remove),
  2690. .id_table = acornscsi_cids,
  2691. .drv = {
  2692. .name = "acornscsi",
  2693. },
  2694. };
  2695. static int __init acornscsi_init(void)
  2696. {
  2697. return ecard_register_driver(&acornscsi_driver);
  2698. }
  2699. static void __exit acornscsi_exit(void)
  2700. {
  2701. ecard_remove_driver(&acornscsi_driver);
  2702. }
  2703. module_init(acornscsi_init);
  2704. module_exit(acornscsi_exit);
  2705. MODULE_AUTHOR("Russell King");
  2706. MODULE_DESCRIPTION("AcornSCSI driver");
  2707. MODULE_LICENSE("GPL");