acornscsi.c 86 KB

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