acornscsi.c 86 KB

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