acornscsi.c 88 KB

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