tmscsim.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697
  1. /************************************************************************
  2. * FILE NAME : TMSCSIM.C *
  3. * BY : C.L. Huang, ching@tekram.com.tw *
  4. * Description: Device Driver for Tekram DC-390(T) PCI SCSI *
  5. * Bus Master Host Adapter *
  6. * (C)Copyright 1995-1996 Tekram Technology Co., Ltd. *
  7. ************************************************************************
  8. * (C) Copyright: put under GNU GPL in 10/96 *
  9. * (see Documentation/scsi/tmscsim.txt) *
  10. ************************************************************************
  11. * $Id: tmscsim.c,v 2.60.2.30 2000/12/20 01:07:12 garloff Exp $ *
  12. * Enhancements and bugfixes by *
  13. * Kurt Garloff <kurt@garloff.de> <garloff@suse.de> *
  14. ************************************************************************
  15. * HISTORY: *
  16. * *
  17. * REV# DATE NAME DESCRIPTION *
  18. * 1.00 96/04/24 CLH First release *
  19. * 1.01 96/06/12 CLH Fixed bug of Media Change for Removable *
  20. * Device, scan all LUN. Support Pre2.0.10 *
  21. * 1.02 96/06/18 CLH Fixed bug of Command timeout ... *
  22. * 1.03 96/09/25 KG Added tmscsim_proc_info() *
  23. * 1.04 96/10/11 CLH Updating for support KV 2.0.x *
  24. * 1.05 96/10/18 KG Fixed bug in DC390_abort(null ptr deref)*
  25. * 1.06 96/10/25 KG Fixed module support *
  26. * 1.07 96/11/09 KG Fixed tmscsim_proc_info() *
  27. * 1.08 96/11/18 KG Fixed null ptr in DC390_Disconnect() *
  28. * 1.09 96/11/30 KG Added register the allocated IO space *
  29. * 1.10 96/12/05 CLH Modified tmscsim_proc_info(), and reset *
  30. * pending interrupt in DC390_detect() *
  31. * 1.11 97/02/05 KG/CLH Fixeds problem with partitions greater *
  32. * than 1GB *
  33. * 1.12 98/02/15 MJ Rewritten PCI probing *
  34. * 1.13 98/04/08 KG Support for non DC390, __initfunc decls,*
  35. * changed max devs from 10 to 16 *
  36. * 1.14a 98/05/05 KG Dynamic DCB allocation, add-single-dev *
  37. * for LUNs if LUN_SCAN (BIOS) not set *
  38. * runtime config using /proc interface *
  39. * 1.14b 98/05/06 KG eliminated cli (); sti (); spinlocks *
  40. * 1.14c 98/05/07 KG 2.0.x compatibility *
  41. * 1.20a 98/05/07 KG changed names of funcs to be consistent *
  42. * DC390_ (entry points), dc390_ (internal)*
  43. * reworked locking *
  44. * 1.20b 98/05/12 KG bugs: version, kfree, _ctmp *
  45. * debug output *
  46. * 1.20c 98/05/12 KG bugs: kfree, parsing, EEpromDefaults *
  47. * 1.20d 98/05/14 KG bugs: list linkage, clear flag after *
  48. * reset on startup, code cleanup *
  49. * 1.20e 98/05/15 KG spinlock comments, name space cleanup *
  50. * pLastDCB now part of ACB structure *
  51. * added stats, timeout for 2.1, TagQ bug *
  52. * RESET and INQUIRY interface commands *
  53. * 1.20f 98/05/18 KG spinlocks fixes, max_lun fix, free DCBs *
  54. * for missing LUNs, pending int *
  55. * 1.20g 98/05/19 KG Clean up: Avoid short *
  56. * 1.20h 98/05/21 KG Remove AdaptSCSIID, max_lun ... *
  57. * 1.20i 98/05/21 KG Aiiie: Bug with TagQMask *
  58. * 1.20j 98/05/24 KG Handle STAT_BUSY, handle pACB->pLinkDCB *
  59. * == 0 in remove_dev and DoingSRB_Done *
  60. * 1.20k 98/05/25 KG DMA_INT (experimental) *
  61. * 1.20l 98/05/27 KG remove DMA_INT; DMA_IDLE cmds added; *
  62. * 1.20m 98/06/10 KG glitch configurable; made some global *
  63. * vars part of ACB; use DC390_readX *
  64. * 1.20n 98/06/11 KG startup params *
  65. * 1.20o 98/06/15 KG added TagMaxNum to boot/module params *
  66. * Device Nr -> Idx, TagMaxNum power of 2 *
  67. * 1.20p 98/06/17 KG Docu updates. Reset depends on settings *
  68. * pci_set_master added; 2.0.xx: pcibios_* *
  69. * used instead of MechNum things ... *
  70. * 1.20q 98/06/23 KG Changed defaults. Added debug code for *
  71. * removable media and fixed it. TagMaxNum *
  72. * fixed for DC390. Locking: ACB, DRV for *
  73. * better IRQ sharing. Spelling: Queueing *
  74. * Parsing and glitch_cfg changes. Display *
  75. * real SyncSpeed value. Made DisConn *
  76. * functional (!) *
  77. * 1.20r 98/06/30 KG Debug macros, allow disabling DsCn, set *
  78. * BIT4 in CtrlR4, EN_PAGE_INT, 2.0 module *
  79. * param -1 fixed. *
  80. * 1.20s 98/08/20 KG Debug info on abort(), try to check PCI,*
  81. * phys_to_bus instead of phys_to_virt, *
  82. * fixed sel. process, fixed locking, *
  83. * added MODULE_XXX infos, changed IRQ *
  84. * request flags, disable DMA_INT *
  85. * 1.20t 98/09/07 KG TagQ report fixed; Write Erase DMA Stat;*
  86. * initfunc -> __init; better abort; *
  87. * Timeout for XFER_DONE & BLAST_COMPLETE; *
  88. * Allow up to 33 commands being processed *
  89. * 2.0a 98/10/14 KG Max Cmnds back to 17. DMA_Stat clearing *
  90. * all flags. Clear within while() loops *
  91. * in DataIn_0/Out_0. Null ptr in dumpinfo *
  92. * for pSRB==0. Better locking during init.*
  93. * bios_param() now respects part. table. *
  94. * 2.0b 98/10/24 KG Docu fixes. Timeout Msg in DMA Blast. *
  95. * Disallow illegal idx in INQUIRY/REMOVE *
  96. * 2.0c 98/11/19 KG Cleaned up detect/init for SMP boxes, *
  97. * Write Erase DMA (1.20t) caused problems *
  98. * 2.0d 98/12/25 KG Christmas release ;-) Message handling *
  99. * completely reworked. Handle target ini- *
  100. * tiated SDTR correctly. *
  101. * 2.0d1 99/01/25 KG Try to handle RESTORE_PTR *
  102. * 2.0d2 99/02/08 KG Check for failure of kmalloc, correct *
  103. * inclusion of scsicam.h, DelayReset *
  104. * 2.0d3 99/05/31 KG DRIVER_OK -> DID_OK, DID_NO_CONNECT, *
  105. * detect Target mode and warn. *
  106. * pcmd->result handling cleaned up. *
  107. * 2.0d4 99/06/01 KG Cleaned selection process. Found bug *
  108. * which prevented more than 16 tags. Now: *
  109. * 24. SDTR cleanup. Cleaner multi-LUN *
  110. * handling. Don't modify ControlRegs/FIFO *
  111. * when connected. *
  112. * 2.0d5 99/06/01 KG Clear DevID, Fix INQUIRY after cfg chg. *
  113. * 2.0d6 99/06/02 KG Added ADD special command to allow cfg. *
  114. * before detection. Reset SYNC_NEGO_DONE *
  115. * after a bus reset. *
  116. * 2.0d7 99/06/03 KG Fixed bugs wrt add,remove commands *
  117. * 2.0d8 99/06/04 KG Removed copying of cmnd into CmdBlock. *
  118. * Fixed Oops in _release(). *
  119. * 2.0d9 99/06/06 KG Also tag queue INQUIRY, T_U_R, ... *
  120. * Allow arb. no. of Tagged Cmnds. Max 32 *
  121. * 2.0d1099/06/20 KG TagMaxNo changes now honoured! Queueing *
  122. * clearified (renamed ..) TagMask handling*
  123. * cleaned. *
  124. * 2.0d1199/06/28 KG cmd->result now identical to 2.0d2 *
  125. * 2.0d1299/07/04 KG Changed order of processing in IRQ *
  126. * 2.0d1399/07/05 KG Don't update DCB fields if removed *
  127. * 2.0d1499/07/05 KG remove_dev: Move kfree() to the end *
  128. * 2.0d1599/07/12 KG use_new_eh_code: 0, ULONG -> UINT where *
  129. * appropriate *
  130. * 2.0d1699/07/13 KG Reenable StartSCSI interrupt, Retry msg *
  131. * 2.0d1799/07/15 KG Remove debug msg. Disable recfg. when *
  132. * there are queued cmnds *
  133. * 2.0d1899/07/18 KG Selection timeout: Don't requeue *
  134. * 2.0d1999/07/18 KG Abort: Only call scsi_done if dequeued *
  135. * 2.0d2099/07/19 KG Rst_Detect: DoingSRB_Done *
  136. * 2.0d2199/08/15 KG dev_id for request/free_irq, cmnd[0] for*
  137. * RETRY, SRBdone does DID_ABORT for the *
  138. * cmd passed by DC390_reset() *
  139. * 2.0d2299/08/25 KG dev_id fixed. can_queue: 42 *
  140. * 2.0d2399/08/25 KG Removed some debugging code. dev_id *
  141. * now is set to pACB. Use u8,u16,u32. *
  142. * 2.0d2499/11/14 KG Unreg. I/O if failed IRQ alloc. Call *
  143. * done () w/ DID_BAD_TARGET in case of *
  144. * missing DCB. We are old EH!! *
  145. * 2.0d2500/01/15 KG 2.3.3x compat from Andreas Schultz *
  146. * set unique_id. Disable RETRY message. *
  147. * 2.0d2600/01/29 KG Go to new EH. *
  148. * 2.0d2700/01/31 KG ... but maintain 2.0 compat. *
  149. * and fix DCB freeing *
  150. * 2.0d2800/02/14 KG Queue statistics fixed, dump special cmd*
  151. * Waiting_Timer for failed StartSCSI *
  152. * New EH: Don't return cmnds to ML on RST *
  153. * Use old EH (don't have new EH fns yet) *
  154. * Reset: Unlock, but refuse to queue *
  155. * 2.3 __setup function *
  156. * 2.0e 00/05/22 KG Return residual for 2.3 *
  157. * 2.0e1 00/05/25 KG Compile fixes for 2.3.99 *
  158. * 2.0e2 00/05/27 KG Jeff Garzik's pci_enable_device() *
  159. * 2.0e3 00/09/29 KG Some 2.4 changes. Don't try Sync Nego *
  160. * before INQUIRY has reported ability. *
  161. * Recognise INQUIRY as scanning command. *
  162. * 2.0e4 00/10/13 KG Allow compilation into 2.4 kernel *
  163. * 2.0e5 00/11/17 KG Store Inq.flags in DCB *
  164. * 2.0e6 00/11/22 KG 2.4 init function (Thx to O.Schumann) *
  165. * 2.4 PCI device table (Thx to A.Richter) *
  166. * 2.0e7 00/11/28 KG Allow overriding of BIOS settings *
  167. * 2.0f 00/12/20 KG Handle failed INQUIRYs during scan *
  168. * 2.1a 03/11/29 GL, KG Initial fixing for 2.6. Convert to *
  169. * use the current PCI-mapping API, update *
  170. * command-queuing. *
  171. * 2.1b 04/04/13 GL Fix for 64-bit platforms *
  172. * 2.1b1 04/01/31 GL (applied 05.04) Remove internal *
  173. * command-queuing. *
  174. * 2.1b2 04/02/01 CH (applied 05.04) Fix error-handling *
  175. * 2.1c 04/05/23 GL Update to use the new pci_driver API, *
  176. * some scsi EH updates, more cleanup. *
  177. * 2.1d 04/05/27 GL Moved setting of scan_devices to *
  178. * slave_alloc/_configure/_destroy, as *
  179. * suggested by CH. *
  180. ***********************************************************************/
  181. /* DEBUG options */
  182. //#define DC390_DEBUG0
  183. //#define DC390_DEBUG1
  184. //#define DC390_DCBDEBUG
  185. //#define DC390_PARSEDEBUG
  186. //#define DC390_REMOVABLEDEBUG
  187. //#define DC390_LOCKDEBUG
  188. //#define NOP do{}while(0)
  189. #define C_NOP
  190. /* Debug definitions */
  191. #ifdef DC390_DEBUG0
  192. # define DEBUG0(x) x
  193. #else
  194. # define DEBUG0(x) C_NOP
  195. #endif
  196. #ifdef DC390_DEBUG1
  197. # define DEBUG1(x) x
  198. #else
  199. # define DEBUG1(x) C_NOP
  200. #endif
  201. #ifdef DC390_DCBDEBUG
  202. # define DCBDEBUG(x) x
  203. #else
  204. # define DCBDEBUG(x) C_NOP
  205. #endif
  206. #ifdef DC390_PARSEDEBUG
  207. # define PARSEDEBUG(x) x
  208. #else
  209. # define PARSEDEBUG(x) C_NOP
  210. #endif
  211. #ifdef DC390_REMOVABLEDEBUG
  212. # define REMOVABLEDEBUG(x) x
  213. #else
  214. # define REMOVABLEDEBUG(x) C_NOP
  215. #endif
  216. #define DCBDEBUG1(x) C_NOP
  217. #include <linux/config.h>
  218. #include <linux/module.h>
  219. #include <linux/delay.h>
  220. #include <linux/signal.h>
  221. #include <linux/sched.h>
  222. #include <linux/errno.h>
  223. #include <linux/kernel.h>
  224. #include <linux/ioport.h>
  225. #include <linux/pci.h>
  226. #include <linux/proc_fs.h>
  227. #include <linux/string.h>
  228. #include <linux/mm.h>
  229. #include <linux/blkdev.h>
  230. #include <linux/timer.h>
  231. #include <linux/interrupt.h>
  232. #include <linux/init.h>
  233. #include <linux/spinlock.h>
  234. #include <asm/io.h>
  235. #include <scsi/scsi.h>
  236. #include <scsi/scsi_cmnd.h>
  237. #include <scsi/scsi_device.h>
  238. #include <scsi/scsi_host.h>
  239. #include <scsi/scsicam.h>
  240. #include <scsi/scsi_tcq.h>
  241. #define DC390_BANNER "Tekram DC390/AM53C974"
  242. #define DC390_VERSION "2.1d 2004-05-27"
  243. #define PCI_DEVICE_ID_AMD53C974 PCI_DEVICE_ID_AMD_SCSI
  244. #include "tmscsim.h"
  245. static void dc390_DataOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
  246. static void dc390_DataIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
  247. static void dc390_Command_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
  248. static void dc390_Status_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
  249. static void dc390_MsgOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
  250. static void dc390_MsgIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
  251. static void dc390_DataOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
  252. static void dc390_DataInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
  253. static void dc390_CommandPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
  254. static void dc390_StatusPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
  255. static void dc390_MsgOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
  256. static void dc390_MsgInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
  257. static void dc390_Nop_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
  258. static void dc390_Nop_1( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus);
  259. static void dc390_SetXferRate( struct dc390_acb* pACB, struct dc390_dcb* pDCB );
  260. static void dc390_Disconnect( struct dc390_acb* pACB );
  261. static void dc390_Reselect( struct dc390_acb* pACB );
  262. static void dc390_SRBdone( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB );
  263. static void dc390_ScsiRstDetect( struct dc390_acb* pACB );
  264. static void dc390_EnableMsgOut_Abort(struct dc390_acb*, struct dc390_srb*);
  265. static void dc390_dumpinfo(struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB);
  266. static void dc390_ResetDevParam(struct dc390_acb* pACB);
  267. static u32 dc390_laststatus = 0;
  268. static u8 dc390_adapterCnt = 0;
  269. /* Startup values, to be overriden on the commandline */
  270. static int tmscsim[] = {-2, -2, -2, -2, -2, -2};
  271. module_param_array(tmscsim, int, NULL, 0);
  272. MODULE_PARM_DESC(tmscsim, "Host SCSI ID, Speed (0=10MHz), Device Flags, Adapter Flags, Max Tags (log2(tags)-1), DelayReset (s)");
  273. MODULE_AUTHOR("C.L. Huang / Kurt Garloff");
  274. MODULE_DESCRIPTION("SCSI host adapter driver for Tekram DC390 and other AMD53C974A based PCI SCSI adapters");
  275. MODULE_LICENSE("GPL");
  276. MODULE_SUPPORTED_DEVICE("sd,sr,sg,st");
  277. static void *dc390_phase0[]={
  278. dc390_DataOut_0,
  279. dc390_DataIn_0,
  280. dc390_Command_0,
  281. dc390_Status_0,
  282. dc390_Nop_0,
  283. dc390_Nop_0,
  284. dc390_MsgOut_0,
  285. dc390_MsgIn_0,
  286. dc390_Nop_1
  287. };
  288. static void *dc390_phase1[]={
  289. dc390_DataOutPhase,
  290. dc390_DataInPhase,
  291. dc390_CommandPhase,
  292. dc390_StatusPhase,
  293. dc390_Nop_0,
  294. dc390_Nop_0,
  295. dc390_MsgOutPhase,
  296. dc390_MsgInPhase,
  297. dc390_Nop_1
  298. };
  299. #ifdef DC390_DEBUG1
  300. static char* dc390_p0_str[] = {
  301. "dc390_DataOut_0",
  302. "dc390_DataIn_0",
  303. "dc390_Command_0",
  304. "dc390_Status_0",
  305. "dc390_Nop_0",
  306. "dc390_Nop_0",
  307. "dc390_MsgOut_0",
  308. "dc390_MsgIn_0",
  309. "dc390_Nop_1"
  310. };
  311. static char* dc390_p1_str[] = {
  312. "dc390_DataOutPhase",
  313. "dc390_DataInPhase",
  314. "dc390_CommandPhase",
  315. "dc390_StatusPhase",
  316. "dc390_Nop_0",
  317. "dc390_Nop_0",
  318. "dc390_MsgOutPhase",
  319. "dc390_MsgInPhase",
  320. "dc390_Nop_1"
  321. };
  322. #endif
  323. static u8 dc390_eepromBuf[MAX_ADAPTER_NUM][EE_LEN];
  324. static u8 dc390_clock_period1[] = {4, 5, 6, 7, 8, 10, 13, 20};
  325. static u8 dc390_clock_speed[] = {100,80,67,57,50, 40, 31, 20};
  326. /***********************************************************************
  327. * Functions for the management of the internal structures
  328. * (DCBs, SRBs, Queueing)
  329. *
  330. **********************************************************************/
  331. static struct dc390_dcb __inline__ *dc390_findDCB ( struct dc390_acb* pACB, u8 id, u8 lun)
  332. {
  333. struct dc390_dcb* pDCB = pACB->pLinkDCB; if (!pDCB) return NULL;
  334. while (pDCB->TargetID != id || pDCB->TargetLUN != lun)
  335. {
  336. pDCB = pDCB->pNextDCB;
  337. if (pDCB == pACB->pLinkDCB)
  338. return NULL;
  339. }
  340. DCBDEBUG1( printk (KERN_DEBUG "DCB %p (%02x,%02x) found.\n", \
  341. pDCB, pDCB->TargetID, pDCB->TargetLUN));
  342. return pDCB;
  343. }
  344. /* Insert SRB oin top of free list */
  345. static __inline__ void dc390_Free_insert (struct dc390_acb* pACB, struct dc390_srb* pSRB)
  346. {
  347. DEBUG0(printk ("DC390: Free SRB %p\n", pSRB));
  348. pSRB->pNextSRB = pACB->pFreeSRB;
  349. pACB->pFreeSRB = pSRB;
  350. }
  351. static __inline__ void dc390_Going_append (struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
  352. {
  353. pDCB->GoingSRBCnt++;
  354. DEBUG0(printk("DC390: Append SRB %p to Going\n", pSRB));
  355. /* Append to the list of Going commands */
  356. if( pDCB->pGoingSRB )
  357. pDCB->pGoingLast->pNextSRB = pSRB;
  358. else
  359. pDCB->pGoingSRB = pSRB;
  360. pDCB->pGoingLast = pSRB;
  361. /* No next one in sent list */
  362. pSRB->pNextSRB = NULL;
  363. }
  364. static __inline__ void dc390_Going_remove (struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
  365. {
  366. DEBUG0(printk("DC390: Remove SRB %p from Going\n", pSRB));
  367. if (pSRB == pDCB->pGoingSRB)
  368. pDCB->pGoingSRB = pSRB->pNextSRB;
  369. else
  370. {
  371. struct dc390_srb* psrb = pDCB->pGoingSRB;
  372. while (psrb && psrb->pNextSRB != pSRB)
  373. psrb = psrb->pNextSRB;
  374. if (!psrb)
  375. { printk (KERN_ERR "DC390: Remove non-ex. SRB %p from Going!\n", pSRB); return; }
  376. psrb->pNextSRB = pSRB->pNextSRB;
  377. if (pSRB == pDCB->pGoingLast)
  378. pDCB->pGoingLast = psrb;
  379. }
  380. pDCB->GoingSRBCnt--;
  381. }
  382. static struct scatterlist* dc390_sg_build_single(struct scatterlist *sg, void *addr, unsigned int length)
  383. {
  384. memset(sg, 0, sizeof(struct scatterlist));
  385. sg->page = virt_to_page(addr);
  386. sg->length = length;
  387. sg->offset = (unsigned long)addr & ~PAGE_MASK;
  388. return sg;
  389. }
  390. /* Create pci mapping */
  391. static int dc390_pci_map (struct dc390_srb* pSRB)
  392. {
  393. int error = 0;
  394. struct scsi_cmnd *pcmd = pSRB->pcmd;
  395. struct pci_dev *pdev = pSRB->pSRBDCB->pDCBACB->pdev;
  396. dc390_cmd_scp_t* cmdp = ((dc390_cmd_scp_t*)(&pcmd->SCp));
  397. /* Map sense buffer */
  398. if (pSRB->SRBFlag & AUTO_REQSENSE) {
  399. pSRB->pSegmentList = dc390_sg_build_single(&pSRB->Segmentx, pcmd->sense_buffer, sizeof(pcmd->sense_buffer));
  400. pSRB->SGcount = pci_map_sg(pdev, pSRB->pSegmentList, 1,
  401. DMA_FROM_DEVICE);
  402. cmdp->saved_dma_handle = sg_dma_address(pSRB->pSegmentList);
  403. /* TODO: error handling */
  404. if (pSRB->SGcount != 1)
  405. error = 1;
  406. DEBUG1(printk("%s(): Mapped sense buffer %p at %x\n", __FUNCTION__, pcmd->sense_buffer, cmdp->saved_dma_handle));
  407. /* Map SG list */
  408. } else if (pcmd->use_sg) {
  409. pSRB->pSegmentList = (struct scatterlist *) pcmd->request_buffer;
  410. pSRB->SGcount = pci_map_sg(pdev, pSRB->pSegmentList, pcmd->use_sg,
  411. pcmd->sc_data_direction);
  412. /* TODO: error handling */
  413. if (!pSRB->SGcount)
  414. error = 1;
  415. DEBUG1(printk("%s(): Mapped SG %p with %d (%d) elements\n",\
  416. __FUNCTION__, pcmd->request_buffer, pSRB->SGcount, pcmd->use_sg));
  417. /* Map single segment */
  418. } else if (pcmd->request_buffer && pcmd->request_bufflen) {
  419. pSRB->pSegmentList = dc390_sg_build_single(&pSRB->Segmentx, pcmd->request_buffer, pcmd->request_bufflen);
  420. pSRB->SGcount = pci_map_sg(pdev, pSRB->pSegmentList, 1,
  421. pcmd->sc_data_direction);
  422. cmdp->saved_dma_handle = sg_dma_address(pSRB->pSegmentList);
  423. /* TODO: error handling */
  424. if (pSRB->SGcount != 1)
  425. error = 1;
  426. DEBUG1(printk("%s(): Mapped request buffer %p at %x\n", __FUNCTION__, pcmd->request_buffer, cmdp->saved_dma_handle));
  427. /* No mapping !? */
  428. } else
  429. pSRB->SGcount = 0;
  430. return error;
  431. }
  432. /* Remove pci mapping */
  433. static void dc390_pci_unmap (struct dc390_srb* pSRB)
  434. {
  435. struct scsi_cmnd *pcmd = pSRB->pcmd;
  436. struct pci_dev *pdev = pSRB->pSRBDCB->pDCBACB->pdev;
  437. DEBUG1(dc390_cmd_scp_t* cmdp = ((dc390_cmd_scp_t*)(&pcmd->SCp)));
  438. if (pSRB->SRBFlag) {
  439. pci_unmap_sg(pdev, &pSRB->Segmentx, 1, DMA_FROM_DEVICE);
  440. DEBUG1(printk("%s(): Unmapped sense buffer at %x\n", __FUNCTION__, cmdp->saved_dma_handle));
  441. } else if (pcmd->use_sg) {
  442. pci_unmap_sg(pdev, pcmd->request_buffer, pcmd->use_sg, pcmd->sc_data_direction);
  443. DEBUG1(printk("%s(): Unmapped SG at %p with %d elements\n", __FUNCTION__, pcmd->request_buffer, pcmd->use_sg));
  444. } else if (pcmd->request_buffer && pcmd->request_bufflen) {
  445. pci_unmap_sg(pdev, &pSRB->Segmentx, 1, pcmd->sc_data_direction);
  446. DEBUG1(printk("%s(): Unmapped request buffer at %x\n", __FUNCTION__, cmdp->saved_dma_handle));
  447. }
  448. }
  449. static void __inline__
  450. dc390_freetag (struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
  451. {
  452. if (pSRB->TagNumber != SCSI_NO_TAG) {
  453. pDCB->TagMask &= ~(1 << pSRB->TagNumber); /* free tag mask */
  454. pSRB->TagNumber = SCSI_NO_TAG;
  455. }
  456. }
  457. static int
  458. dc390_StartSCSI( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB )
  459. {
  460. struct scsi_cmnd *scmd = pSRB->pcmd;
  461. struct scsi_device *sdev = scmd->device;
  462. u8 cmd, disc_allowed, try_sync_nego;
  463. char tag[2];
  464. pSRB->ScsiPhase = SCSI_NOP0;
  465. if (pACB->Connected)
  466. {
  467. // Should not happen normally
  468. printk (KERN_WARNING "DC390: Can't select when connected! (%08x,%02x)\n",
  469. pSRB->SRBState, pSRB->SRBFlag);
  470. pSRB->SRBState = SRB_READY;
  471. pACB->SelConn++;
  472. return 1;
  473. }
  474. if (time_before (jiffies, pACB->pScsiHost->last_reset))
  475. {
  476. DEBUG0(printk ("DC390: We were just reset and don't accept commands yet!\n"));
  477. return 1;
  478. }
  479. /* KG: Moved pci mapping here */
  480. dc390_pci_map(pSRB);
  481. /* TODO: error handling */
  482. DC390_write8 (Scsi_Dest_ID, pDCB->TargetID);
  483. DC390_write8 (Sync_Period, pDCB->SyncPeriod);
  484. DC390_write8 (Sync_Offset, pDCB->SyncOffset);
  485. DC390_write8 (CtrlReg1, pDCB->CtrlR1);
  486. DC390_write8 (CtrlReg3, pDCB->CtrlR3);
  487. DC390_write8 (CtrlReg4, pDCB->CtrlR4);
  488. DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD); /* Flush FIFO */
  489. DEBUG1(printk (KERN_INFO "DC390: Start SCSI command: %02x (Sync:%02x)\n",\
  490. scmd->cmnd[0], pDCB->SyncMode));
  491. /* Don't disconnect on AUTO_REQSENSE, cause it might be an
  492. * Contingent Allegiance Condition (6.6), where no tags should be used.
  493. * All other have to be allowed to disconnect to prevent Incorrect
  494. * Initiator Connection (6.8.2/6.5.2) */
  495. /* Changed KG, 99/06/06 */
  496. if (! (pSRB->SRBFlag & AUTO_REQSENSE))
  497. disc_allowed = pDCB->DevMode & EN_DISCONNECT_;
  498. else
  499. disc_allowed = 0;
  500. if ((pDCB->SyncMode & SYNC_ENABLE) && pDCB->TargetLUN == 0 && sdev->sdtr &&
  501. (((scmd->cmnd[0] == REQUEST_SENSE || (pSRB->SRBFlag & AUTO_REQSENSE)) &&
  502. !(pDCB->SyncMode & SYNC_NEGO_DONE)) || scmd->cmnd[0] == INQUIRY))
  503. try_sync_nego = 1;
  504. else
  505. try_sync_nego = 0;
  506. pSRB->MsgCnt = 0;
  507. cmd = SEL_W_ATN;
  508. DC390_write8 (ScsiFifo, IDENTIFY(disc_allowed, pDCB->TargetLUN));
  509. /* Change 99/05/31: Don't use tags when not disconnecting (BUSY) */
  510. if ((pDCB->SyncMode & EN_TAG_QUEUEING) && disc_allowed && scsi_populate_tag_msg(scmd, tag)) {
  511. DC390_write8(ScsiFifo, tag[0]);
  512. pDCB->TagMask |= 1 << tag[1];
  513. pSRB->TagNumber = tag[1];
  514. DC390_write8(ScsiFifo, tag[1]);
  515. DEBUG1(printk(KERN_INFO "DC390: Select w/DisCn for Cmd %li (SRB %p), block tag %02x\n", scmd->pid, pSRB, tag[1]));
  516. cmd = SEL_W_ATN3;
  517. } else {
  518. /* No TagQ */
  519. //no_tag:
  520. DEBUG1(printk(KERN_INFO "DC390: Select w%s/DisCn for Cmd %li (SRB %p), No TagQ\n", disc_allowed ? "" : "o", scmd->pid, pSRB));
  521. }
  522. pSRB->SRBState = SRB_START_;
  523. if (try_sync_nego)
  524. {
  525. u8 Sync_Off = pDCB->SyncOffset;
  526. DEBUG0(printk (KERN_INFO "DC390: NEW Sync Nego code triggered (%i %i)\n", pDCB->TargetID, pDCB->TargetLUN));
  527. pSRB->MsgOutBuf[0] = EXTENDED_MESSAGE;
  528. pSRB->MsgOutBuf[1] = 3;
  529. pSRB->MsgOutBuf[2] = EXTENDED_SDTR;
  530. pSRB->MsgOutBuf[3] = pDCB->NegoPeriod;
  531. if (!(Sync_Off & 0x0f)) Sync_Off = SYNC_NEGO_OFFSET;
  532. pSRB->MsgOutBuf[4] = Sync_Off;
  533. pSRB->MsgCnt = 5;
  534. //pSRB->SRBState = SRB_MSGOUT_;
  535. pSRB->SRBState |= DO_SYNC_NEGO;
  536. cmd = SEL_W_ATN_STOP;
  537. }
  538. /* Command is written in CommandPhase, if SEL_W_ATN_STOP ... */
  539. if (cmd != SEL_W_ATN_STOP)
  540. {
  541. if( pSRB->SRBFlag & AUTO_REQSENSE )
  542. {
  543. DC390_write8 (ScsiFifo, REQUEST_SENSE);
  544. DC390_write8 (ScsiFifo, pDCB->TargetLUN << 5);
  545. DC390_write8 (ScsiFifo, 0);
  546. DC390_write8 (ScsiFifo, 0);
  547. DC390_write8 (ScsiFifo, sizeof(scmd->sense_buffer));
  548. DC390_write8 (ScsiFifo, 0);
  549. DEBUG1(printk (KERN_DEBUG "DC390: AutoReqSense !\n"));
  550. }
  551. else /* write cmnd to bus */
  552. {
  553. u8 *ptr; u8 i;
  554. ptr = (u8 *)scmd->cmnd;
  555. for (i = 0; i < scmd->cmd_len; i++)
  556. DC390_write8 (ScsiFifo, *(ptr++));
  557. }
  558. }
  559. DEBUG0(if (pACB->pActiveDCB) \
  560. printk (KERN_WARNING "DC390: ActiveDCB != 0\n"));
  561. DEBUG0(if (pDCB->pActiveSRB) \
  562. printk (KERN_WARNING "DC390: ActiveSRB != 0\n"));
  563. //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
  564. if (DC390_read8 (Scsi_Status) & INTERRUPT)
  565. {
  566. dc390_freetag (pDCB, pSRB);
  567. DEBUG0(printk ("DC390: Interrupt during Start SCSI (pid %li, target %02i-%02i)\n",
  568. scmd->pid, scmd->device->id, scmd->device->lun));
  569. pSRB->SRBState = SRB_READY;
  570. //DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
  571. pACB->SelLost++;
  572. return 1;
  573. }
  574. DC390_write8 (ScsiCmd, cmd);
  575. pACB->pActiveDCB = pDCB;
  576. pDCB->pActiveSRB = pSRB;
  577. pACB->Connected = 1;
  578. pSRB->ScsiPhase = SCSI_NOP1;
  579. return 0;
  580. }
  581. //#define DMA_INT EN_DMA_INT /*| EN_PAGE_INT*/
  582. #define DMA_INT 0
  583. #if DMA_INT
  584. /* This is similar to AM53C974.c ... */
  585. static u8
  586. dc390_dma_intr (struct dc390_acb* pACB)
  587. {
  588. struct dc390_srb* pSRB;
  589. u8 dstate;
  590. DEBUG0(u16 pstate; struct pci_dev *pdev = pACB->pdev);
  591. DEBUG0(pci_read_config_word(pdev, PCI_STATUS, &pstate));
  592. DEBUG0(if (pstate & (PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY))\
  593. { printk(KERN_WARNING "DC390: PCI state = %04x!\n", pstate); \
  594. pci_write_config_word(pdev, PCI_STATUS, (PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY));});
  595. dstate = DC390_read8 (DMA_Status);
  596. if (! pACB->pActiveDCB || ! pACB->pActiveDCB->pActiveSRB) return dstate;
  597. else pSRB = pACB->pActiveDCB->pActiveSRB;
  598. if (dstate & (DMA_XFER_ABORT | DMA_XFER_ERROR | POWER_DOWN | PCI_MS_ABORT))
  599. {
  600. printk (KERN_ERR "DC390: DMA error (%02x)!\n", dstate);
  601. return dstate;
  602. }
  603. if (dstate & DMA_XFER_DONE)
  604. {
  605. u32 residual, xferCnt; int ctr = 6000000;
  606. if (! (DC390_read8 (DMA_Cmd) & READ_DIRECTION))
  607. {
  608. do
  609. {
  610. DEBUG1(printk (KERN_DEBUG "DC390: read residual bytes ... \n"));
  611. dstate = DC390_read8 (DMA_Status);
  612. residual = DC390_read8 (CtcReg_Low) | DC390_read8 (CtcReg_Mid) << 8 |
  613. DC390_read8 (CtcReg_High) << 16;
  614. residual += DC390_read8 (Current_Fifo) & 0x1f;
  615. } while (residual && ! (dstate & SCSI_INTERRUPT) && --ctr);
  616. if (!ctr) printk (KERN_CRIT "DC390: dma_intr: DMA aborted unfinished: %06x bytes remain!!\n", DC390_read32 (DMA_Wk_ByteCntr));
  617. /* residual = ... */
  618. }
  619. else
  620. residual = 0;
  621. /* ??? */
  622. xferCnt = pSRB->SGToBeXferLen - residual;
  623. pSRB->SGBusAddr += xferCnt;
  624. pSRB->TotalXferredLen += xferCnt;
  625. pSRB->SGToBeXferLen = residual;
  626. # ifdef DC390_DEBUG0
  627. printk (KERN_INFO "DC390: DMA: residual = %i, xfer = %i\n",
  628. (unsigned int)residual, (unsigned int)xferCnt);
  629. # endif
  630. DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
  631. }
  632. dc390_laststatus &= ~0xff000000; dc390_laststatus |= dstate << 24;
  633. return dstate;
  634. }
  635. #endif
  636. static void __inline__
  637. dc390_InvalidCmd(struct dc390_acb* pACB)
  638. {
  639. if (pACB->pActiveDCB->pActiveSRB->SRBState & (SRB_START_ | SRB_MSGOUT))
  640. DC390_write8(ScsiCmd, CLEAR_FIFO_CMD);
  641. }
  642. static irqreturn_t __inline__
  643. DC390_Interrupt(int irq, void *dev_id, struct pt_regs *regs)
  644. {
  645. struct dc390_acb *pACB = (struct dc390_acb*)dev_id;
  646. struct dc390_dcb *pDCB;
  647. struct dc390_srb *pSRB;
  648. u8 sstatus=0;
  649. u8 phase;
  650. void (*stateV)( struct dc390_acb*, struct dc390_srb*, u8 *);
  651. u8 istate, istatus;
  652. #if DMA_INT
  653. u8 dstatus;
  654. #endif
  655. sstatus = DC390_read8 (Scsi_Status);
  656. if( !(sstatus & INTERRUPT) )
  657. return IRQ_NONE;
  658. DEBUG1(printk (KERN_DEBUG "sstatus=%02x,", sstatus));
  659. #if DMA_INT
  660. spin_lock_irq(pACB->pScsiHost->host_lock);
  661. dstatus = dc390_dma_intr (pACB);
  662. spin_unlock_irq(pACB->pScsiHost->host_lock);
  663. DEBUG1(printk (KERN_DEBUG "dstatus=%02x,", dstatus));
  664. if (! (dstatus & SCSI_INTERRUPT))
  665. {
  666. DEBUG0(printk (KERN_WARNING "DC390 Int w/o SCSI actions (only DMA?)\n"));
  667. return IRQ_NONE;
  668. }
  669. #else
  670. //DC390_write32 (DMA_ScsiBusCtrl, WRT_ERASE_DMA_STAT | EN_INT_ON_PCI_ABORT);
  671. //dstatus = DC390_read8 (DMA_Status);
  672. //DC390_write32 (DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT);
  673. #endif
  674. spin_lock_irq(pACB->pScsiHost->host_lock);
  675. istate = DC390_read8 (Intern_State);
  676. istatus = DC390_read8 (INT_Status); /* This clears Scsi_Status, Intern_State and INT_Status ! */
  677. DEBUG1(printk (KERN_INFO "Istatus(Res,Inv,Dis,Serv,Succ,ReS,SelA,Sel)=%02x,",istatus));
  678. dc390_laststatus &= ~0x00ffffff;
  679. dc390_laststatus |= /* dstatus<<24 | */ sstatus<<16 | istate<<8 | istatus;
  680. if (sstatus & ILLEGAL_OP_ERR)
  681. {
  682. printk ("DC390: Illegal Operation detected (%08x)!\n", dc390_laststatus);
  683. dc390_dumpinfo (pACB, pACB->pActiveDCB, pACB->pActiveDCB->pActiveSRB);
  684. }
  685. else if (istatus & INVALID_CMD)
  686. {
  687. printk ("DC390: Invalid Command detected (%08x)!\n", dc390_laststatus);
  688. dc390_InvalidCmd( pACB );
  689. goto unlock;
  690. }
  691. if (istatus & SCSI_RESET)
  692. {
  693. dc390_ScsiRstDetect( pACB );
  694. goto unlock;
  695. }
  696. if (istatus & DISCONNECTED)
  697. {
  698. dc390_Disconnect( pACB );
  699. goto unlock;
  700. }
  701. if (istatus & RESELECTED)
  702. {
  703. dc390_Reselect( pACB );
  704. goto unlock;
  705. }
  706. else if (istatus & (SELECTED | SEL_ATTENTION))
  707. {
  708. printk (KERN_ERR "DC390: Target mode not supported!\n");
  709. goto unlock;
  710. }
  711. if (istatus & (SUCCESSFUL_OP|SERVICE_REQUEST) )
  712. {
  713. pDCB = pACB->pActiveDCB;
  714. if (!pDCB)
  715. {
  716. printk (KERN_ERR "DC390: Suc. op/ Serv. req: pActiveDCB = 0!\n");
  717. goto unlock;
  718. }
  719. pSRB = pDCB->pActiveSRB;
  720. if( pDCB->DCBFlag & ABORT_DEV_ )
  721. dc390_EnableMsgOut_Abort (pACB, pSRB);
  722. phase = pSRB->ScsiPhase;
  723. DEBUG1(printk (KERN_INFO "DC390: [%i]%s(0) (%02x)\n", phase, dc390_p0_str[phase], sstatus));
  724. stateV = (void *) dc390_phase0[phase];
  725. ( *stateV )( pACB, pSRB, &sstatus );
  726. pSRB->ScsiPhase = sstatus & 7;
  727. phase = (u8) sstatus & 7;
  728. DEBUG1(printk (KERN_INFO "DC390: [%i]%s(1) (%02x)\n", phase, dc390_p1_str[phase], sstatus));
  729. stateV = (void *) dc390_phase1[phase];
  730. ( *stateV )( pACB, pSRB, &sstatus );
  731. }
  732. unlock:
  733. spin_unlock_irq(pACB->pScsiHost->host_lock);
  734. return IRQ_HANDLED;
  735. }
  736. static irqreturn_t do_DC390_Interrupt( int irq, void *dev_id, struct pt_regs *regs)
  737. {
  738. irqreturn_t ret;
  739. DEBUG1(printk (KERN_INFO "DC390: Irq (%i) caught: ", irq));
  740. /* Locking is done in DC390_Interrupt */
  741. ret = DC390_Interrupt(irq, dev_id, regs);
  742. DEBUG1(printk (".. IRQ returned\n"));
  743. return ret;
  744. }
  745. static void
  746. dc390_DataOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
  747. {
  748. u8 sstatus;
  749. struct scatterlist *psgl;
  750. u32 ResidCnt, xferCnt;
  751. u8 dstate = 0;
  752. sstatus = *psstatus;
  753. if( !(pSRB->SRBState & SRB_XFERPAD) )
  754. {
  755. if( sstatus & (PARITY_ERR | ILLEGAL_OP_ERR) )
  756. pSRB->SRBStatus |= PARITY_ERROR;
  757. if( sstatus & COUNT_2_ZERO )
  758. {
  759. unsigned long timeout = jiffies + HZ;
  760. /* Function called from the ISR with the host_lock held and interrupts disabled */
  761. if (pSRB->SGToBeXferLen)
  762. while (time_before(jiffies, timeout) && !((dstate = DC390_read8 (DMA_Status)) & DMA_XFER_DONE)) {
  763. spin_unlock_irq(pACB->pScsiHost->host_lock);
  764. udelay(50);
  765. spin_lock_irq(pACB->pScsiHost->host_lock);
  766. }
  767. if (!time_before(jiffies, timeout))
  768. printk (KERN_CRIT "DC390: Deadlock in DataOut_0: DMA aborted unfinished: %06x bytes remain!!\n",
  769. DC390_read32 (DMA_Wk_ByteCntr));
  770. dc390_laststatus &= ~0xff000000;
  771. dc390_laststatus |= dstate << 24;
  772. pSRB->TotalXferredLen += pSRB->SGToBeXferLen;
  773. pSRB->SGIndex++;
  774. if( pSRB->SGIndex < pSRB->SGcount )
  775. {
  776. pSRB->pSegmentList++;
  777. psgl = pSRB->pSegmentList;
  778. pSRB->SGBusAddr = cpu_to_le32(pci_dma_lo32(sg_dma_address(psgl)));
  779. pSRB->SGToBeXferLen = cpu_to_le32(sg_dma_len(psgl));
  780. }
  781. else
  782. pSRB->SGToBeXferLen = 0;
  783. }
  784. else
  785. {
  786. ResidCnt = (u32) DC390_read8 (Current_Fifo) & 0x1f;
  787. ResidCnt |= (u32) DC390_read8 (CtcReg_High) << 16;
  788. ResidCnt |= (u32) DC390_read8 (CtcReg_Mid) << 8;
  789. ResidCnt += (u32) DC390_read8 (CtcReg_Low);
  790. xferCnt = pSRB->SGToBeXferLen - ResidCnt;
  791. pSRB->SGBusAddr += xferCnt;
  792. pSRB->TotalXferredLen += xferCnt;
  793. pSRB->SGToBeXferLen = ResidCnt;
  794. }
  795. }
  796. if ((*psstatus & 7) != SCSI_DATA_OUT)
  797. {
  798. DC390_write8 (DMA_Cmd, WRITE_DIRECTION+DMA_IDLE_CMD); /* | DMA_INT */
  799. DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
  800. }
  801. }
  802. static void
  803. dc390_DataIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
  804. {
  805. u8 sstatus, residual, bval;
  806. struct scatterlist *psgl;
  807. u32 ResidCnt, i;
  808. unsigned long xferCnt;
  809. u8 *ptr;
  810. sstatus = *psstatus;
  811. if( !(pSRB->SRBState & SRB_XFERPAD) )
  812. {
  813. if( sstatus & (PARITY_ERR | ILLEGAL_OP_ERR))
  814. pSRB->SRBStatus |= PARITY_ERROR;
  815. if( sstatus & COUNT_2_ZERO )
  816. {
  817. int dstate = 0;
  818. unsigned long timeout = jiffies + HZ;
  819. /* Function called from the ISR with the host_lock held and interrupts disabled */
  820. if (pSRB->SGToBeXferLen)
  821. while (time_before(jiffies, timeout) && !((dstate = DC390_read8 (DMA_Status)) & DMA_XFER_DONE)) {
  822. spin_unlock_irq(pACB->pScsiHost->host_lock);
  823. udelay(50);
  824. spin_lock_irq(pACB->pScsiHost->host_lock);
  825. }
  826. if (!time_before(jiffies, timeout)) {
  827. printk (KERN_CRIT "DC390: Deadlock in DataIn_0: DMA aborted unfinished: %06x bytes remain!!\n",
  828. DC390_read32 (DMA_Wk_ByteCntr));
  829. printk (KERN_CRIT "DC390: DataIn_0: DMA State: %i\n", dstate);
  830. }
  831. dc390_laststatus &= ~0xff000000;
  832. dc390_laststatus |= dstate << 24;
  833. DEBUG1(ResidCnt = ((unsigned long) DC390_read8 (CtcReg_High) << 16) \
  834. + ((unsigned long) DC390_read8 (CtcReg_Mid) << 8) \
  835. + ((unsigned long) DC390_read8 (CtcReg_Low)));
  836. DEBUG1(printk (KERN_DEBUG "Count_2_Zero (ResidCnt=%i,ToBeXfer=%li),", ResidCnt, pSRB->SGToBeXferLen));
  837. DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_IDLE_CMD); /* | DMA_INT */
  838. pSRB->TotalXferredLen += pSRB->SGToBeXferLen;
  839. pSRB->SGIndex++;
  840. if( pSRB->SGIndex < pSRB->SGcount )
  841. {
  842. pSRB->pSegmentList++;
  843. psgl = pSRB->pSegmentList;
  844. pSRB->SGBusAddr = cpu_to_le32(pci_dma_lo32(sg_dma_address(psgl)));
  845. pSRB->SGToBeXferLen = cpu_to_le32(sg_dma_len(psgl));
  846. }
  847. else
  848. pSRB->SGToBeXferLen = 0;
  849. }
  850. else /* phase changed */
  851. {
  852. residual = 0;
  853. bval = DC390_read8 (Current_Fifo);
  854. while( bval & 0x1f )
  855. {
  856. DEBUG1(printk (KERN_DEBUG "Check for residuals,"));
  857. if( (bval & 0x1f) == 1 )
  858. {
  859. for(i=0; i < 0x100; i++)
  860. {
  861. bval = DC390_read8 (Current_Fifo);
  862. if( !(bval & 0x1f) )
  863. goto din_1;
  864. else if( i == 0x0ff )
  865. {
  866. residual = 1; /* ;1 residual byte */
  867. goto din_1;
  868. }
  869. }
  870. }
  871. else
  872. bval = DC390_read8 (Current_Fifo);
  873. }
  874. din_1:
  875. DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_BLAST_CMD);
  876. for (i = 0xa000; i; i--)
  877. {
  878. bval = DC390_read8 (DMA_Status);
  879. if (bval & BLAST_COMPLETE)
  880. break;
  881. }
  882. /* It seems a DMA Blast abort isn't that bad ... */
  883. if (!i) printk (KERN_ERR "DC390: DMA Blast aborted unfinished!\n");
  884. //DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_IDLE_CMD); /* | DMA_INT */
  885. dc390_laststatus &= ~0xff000000; dc390_laststatus |= bval << 24;
  886. DEBUG1(printk (KERN_DEBUG "Blast: Read %i times DMA_Status %02x", 0xa000-i, bval));
  887. ResidCnt = (u32) DC390_read8 (CtcReg_High);
  888. ResidCnt <<= 8;
  889. ResidCnt |= (u32) DC390_read8 (CtcReg_Mid);
  890. ResidCnt <<= 8;
  891. ResidCnt |= (u32) DC390_read8 (CtcReg_Low);
  892. xferCnt = pSRB->SGToBeXferLen - ResidCnt;
  893. pSRB->SGBusAddr += xferCnt;
  894. pSRB->TotalXferredLen += xferCnt;
  895. pSRB->SGToBeXferLen = ResidCnt;
  896. if( residual )
  897. {
  898. bval = DC390_read8 (ScsiFifo); /* get one residual byte */
  899. ptr = (u8 *) bus_to_virt( pSRB->SGBusAddr );
  900. *ptr = bval;
  901. pSRB->SGBusAddr++; xferCnt++;
  902. pSRB->TotalXferredLen++;
  903. pSRB->SGToBeXferLen--;
  904. }
  905. DEBUG1(printk (KERN_DEBUG "Xfered: %li, Total: %li, Remaining: %li\n", xferCnt,\
  906. pSRB->TotalXferredLen, pSRB->SGToBeXferLen));
  907. }
  908. }
  909. if ((*psstatus & 7) != SCSI_DATA_IN)
  910. {
  911. DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
  912. DC390_write8 (DMA_Cmd, READ_DIRECTION+DMA_IDLE_CMD); /* | DMA_INT */
  913. }
  914. }
  915. static void
  916. dc390_Command_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
  917. {
  918. }
  919. static void
  920. dc390_Status_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
  921. {
  922. pSRB->TargetStatus = DC390_read8 (ScsiFifo);
  923. //udelay (1);
  924. pSRB->EndMessage = DC390_read8 (ScsiFifo); /* get message */
  925. *psstatus = SCSI_NOP0;
  926. pSRB->SRBState = SRB_COMPLETED;
  927. DC390_write8 (ScsiCmd, MSG_ACCEPTED_CMD);
  928. }
  929. static void
  930. dc390_MsgOut_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
  931. {
  932. if( pSRB->SRBState & (SRB_UNEXPECT_RESEL+SRB_ABORT_SENT) )
  933. *psstatus = SCSI_NOP0;
  934. //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
  935. }
  936. static void __inline__
  937. dc390_reprog (struct dc390_acb* pACB, struct dc390_dcb* pDCB)
  938. {
  939. DC390_write8 (Sync_Period, pDCB->SyncPeriod);
  940. DC390_write8 (Sync_Offset, pDCB->SyncOffset);
  941. DC390_write8 (CtrlReg3, pDCB->CtrlR3);
  942. DC390_write8 (CtrlReg4, pDCB->CtrlR4);
  943. dc390_SetXferRate (pACB, pDCB);
  944. }
  945. #ifdef DC390_DEBUG0
  946. static void
  947. dc390_printMsg (u8 *MsgBuf, u8 len)
  948. {
  949. int i;
  950. printk (" %02x", MsgBuf[0]);
  951. for (i = 1; i < len; i++)
  952. printk (" %02x", MsgBuf[i]);
  953. printk ("\n");
  954. }
  955. #endif
  956. #define DC390_ENABLE_MSGOUT DC390_write8 (ScsiCmd, SET_ATN_CMD)
  957. /* reject_msg */
  958. static void __inline__
  959. dc390_MsgIn_reject (struct dc390_acb* pACB, struct dc390_srb* pSRB)
  960. {
  961. pSRB->MsgOutBuf[0] = MESSAGE_REJECT;
  962. pSRB->MsgCnt = 1;
  963. DC390_ENABLE_MSGOUT;
  964. DEBUG0 (printk (KERN_INFO "DC390: Reject message\n"));
  965. }
  966. /* abort command */
  967. static void
  968. dc390_EnableMsgOut_Abort ( struct dc390_acb* pACB, struct dc390_srb* pSRB )
  969. {
  970. pSRB->MsgOutBuf[0] = ABORT;
  971. pSRB->MsgCnt = 1; DC390_ENABLE_MSGOUT;
  972. pSRB->pSRBDCB->DCBFlag &= ~ABORT_DEV_;
  973. }
  974. static struct dc390_srb*
  975. dc390_MsgIn_QTag (struct dc390_acb* pACB, struct dc390_dcb* pDCB, s8 tag)
  976. {
  977. struct dc390_srb* pSRB = pDCB->pGoingSRB;
  978. if (pSRB)
  979. {
  980. struct scsi_cmnd *scmd = scsi_find_tag(pSRB->pcmd->device, tag);
  981. pSRB = (struct dc390_srb *)scmd->host_scribble;
  982. if (pDCB->DCBFlag & ABORT_DEV_)
  983. {
  984. pSRB->SRBState = SRB_ABORT_SENT;
  985. dc390_EnableMsgOut_Abort( pACB, pSRB );
  986. }
  987. if (!(pSRB->SRBState & SRB_DISCONNECT))
  988. goto mingx0;
  989. pDCB->pActiveSRB = pSRB;
  990. pSRB->SRBState = SRB_DATA_XFER;
  991. }
  992. else
  993. {
  994. mingx0:
  995. pSRB = pACB->pTmpSRB;
  996. pSRB->SRBState = SRB_UNEXPECT_RESEL;
  997. pDCB->pActiveSRB = pSRB;
  998. pSRB->MsgOutBuf[0] = ABORT_TAG;
  999. pSRB->MsgCnt = 1; DC390_ENABLE_MSGOUT;
  1000. }
  1001. return pSRB;
  1002. }
  1003. /* set async transfer mode */
  1004. static void
  1005. dc390_MsgIn_set_async (struct dc390_acb* pACB, struct dc390_srb* pSRB)
  1006. {
  1007. struct dc390_dcb* pDCB = pSRB->pSRBDCB;
  1008. if (!(pSRB->SRBState & DO_SYNC_NEGO))
  1009. printk (KERN_INFO "DC390: Target %i initiates Non-Sync?\n", pDCB->TargetID);
  1010. pSRB->SRBState &= ~DO_SYNC_NEGO;
  1011. pDCB->SyncMode &= ~(SYNC_ENABLE+SYNC_NEGO_DONE);
  1012. pDCB->SyncPeriod = 0;
  1013. pDCB->SyncOffset = 0;
  1014. //pDCB->NegoPeriod = 50; /* 200ns <=> 5 MHz */
  1015. pDCB->CtrlR3 = FAST_CLK; /* fast clock / normal scsi */
  1016. pDCB->CtrlR4 &= 0x3f;
  1017. pDCB->CtrlR4 |= pACB->glitch_cfg; /* glitch eater */
  1018. dc390_reprog (pACB, pDCB);
  1019. }
  1020. /* set sync transfer mode */
  1021. static void
  1022. dc390_MsgIn_set_sync (struct dc390_acb* pACB, struct dc390_srb* pSRB)
  1023. {
  1024. u8 bval;
  1025. u16 wval, wval1;
  1026. struct dc390_dcb* pDCB = pSRB->pSRBDCB;
  1027. u8 oldsyncperiod = pDCB->SyncPeriod;
  1028. u8 oldsyncoffset = pDCB->SyncOffset;
  1029. if (!(pSRB->SRBState & DO_SYNC_NEGO))
  1030. {
  1031. printk (KERN_INFO "DC390: Target %i initiates Sync: %ins %i ... answer ...\n",
  1032. pDCB->TargetID, pSRB->MsgInBuf[3]<<2, pSRB->MsgInBuf[4]);
  1033. /* reject */
  1034. //dc390_MsgIn_reject (pACB, pSRB);
  1035. //return dc390_MsgIn_set_async (pACB, pSRB);
  1036. /* Reply with corrected SDTR Message */
  1037. if (pSRB->MsgInBuf[4] > 15)
  1038. {
  1039. printk (KERN_INFO "DC390: Lower Sync Offset to 15\n");
  1040. pSRB->MsgInBuf[4] = 15;
  1041. }
  1042. if (pSRB->MsgInBuf[3] < pDCB->NegoPeriod)
  1043. {
  1044. printk (KERN_INFO "DC390: Set sync nego period to %ins\n", pDCB->NegoPeriod << 2);
  1045. pSRB->MsgInBuf[3] = pDCB->NegoPeriod;
  1046. }
  1047. memcpy (pSRB->MsgOutBuf, pSRB->MsgInBuf, 5);
  1048. pSRB->MsgCnt = 5;
  1049. DC390_ENABLE_MSGOUT;
  1050. }
  1051. pSRB->SRBState &= ~DO_SYNC_NEGO;
  1052. pDCB->SyncMode |= SYNC_ENABLE+SYNC_NEGO_DONE;
  1053. pDCB->SyncOffset &= 0x0f0;
  1054. pDCB->SyncOffset |= pSRB->MsgInBuf[4];
  1055. pDCB->NegoPeriod = pSRB->MsgInBuf[3];
  1056. wval = (u16) pSRB->MsgInBuf[3];
  1057. wval = wval << 2; wval -= 3; wval1 = wval / 25; /* compute speed */
  1058. if( (wval1 * 25) != wval) wval1++;
  1059. bval = FAST_CLK+FAST_SCSI; /* fast clock / fast scsi */
  1060. pDCB->CtrlR4 &= 0x3f; /* Glitch eater: 12ns less than normal */
  1061. if (pACB->glitch_cfg != NS_TO_GLITCH(0))
  1062. pDCB->CtrlR4 |= NS_TO_GLITCH(((GLITCH_TO_NS(pACB->glitch_cfg)) - 1));
  1063. else
  1064. pDCB->CtrlR4 |= NS_TO_GLITCH(0);
  1065. if (wval1 < 4) pDCB->CtrlR4 |= NS_TO_GLITCH(0); /* Ultra */
  1066. if (wval1 >= 8)
  1067. {
  1068. wval1--; /* Timing computation differs by 1 from FAST_SCSI */
  1069. bval = FAST_CLK; /* fast clock / normal scsi */
  1070. pDCB->CtrlR4 |= pACB->glitch_cfg; /* glitch eater */
  1071. }
  1072. pDCB->CtrlR3 = bval;
  1073. pDCB->SyncPeriod = (u8)wval1;
  1074. if ((oldsyncperiod != wval1 || oldsyncoffset != pDCB->SyncOffset) && pDCB->TargetLUN == 0)
  1075. {
  1076. if (! (bval & FAST_SCSI)) wval1++;
  1077. printk (KERN_INFO "DC390: Target %i: Sync transfer %i.%1i MHz, Offset %i\n", pDCB->TargetID,
  1078. 40/wval1, ((40%wval1)*10+wval1/2)/wval1, pDCB->SyncOffset & 0x0f);
  1079. }
  1080. dc390_reprog (pACB, pDCB);
  1081. }
  1082. /* handle RESTORE_PTR */
  1083. /* I presume, this command is already mapped, so, have to remap. */
  1084. static void
  1085. dc390_restore_ptr (struct dc390_acb* pACB, struct dc390_srb* pSRB)
  1086. {
  1087. struct scsi_cmnd *pcmd = pSRB->pcmd;
  1088. struct scatterlist *psgl;
  1089. pSRB->TotalXferredLen = 0;
  1090. pSRB->SGIndex = 0;
  1091. if (pcmd->use_sg) {
  1092. pSRB->pSegmentList = (struct scatterlist *)pcmd->request_buffer;
  1093. psgl = pSRB->pSegmentList;
  1094. //dc390_pci_sync(pSRB);
  1095. while (pSRB->TotalXferredLen + (unsigned long) sg_dma_len(psgl) < pSRB->Saved_Ptr)
  1096. {
  1097. pSRB->TotalXferredLen += (unsigned long) sg_dma_len(psgl);
  1098. pSRB->SGIndex++;
  1099. if( pSRB->SGIndex < pSRB->SGcount )
  1100. {
  1101. pSRB->pSegmentList++;
  1102. psgl = pSRB->pSegmentList;
  1103. pSRB->SGBusAddr = cpu_to_le32(pci_dma_lo32(sg_dma_address(psgl)));
  1104. pSRB->SGToBeXferLen = cpu_to_le32(sg_dma_len(psgl));
  1105. }
  1106. else
  1107. pSRB->SGToBeXferLen = 0;
  1108. }
  1109. pSRB->SGToBeXferLen -= (pSRB->Saved_Ptr - pSRB->TotalXferredLen);
  1110. pSRB->SGBusAddr += (pSRB->Saved_Ptr - pSRB->TotalXferredLen);
  1111. printk (KERN_INFO "DC390: Pointer restored. Segment %i, Total %li, Bus %08lx\n",
  1112. pSRB->SGIndex, pSRB->Saved_Ptr, pSRB->SGBusAddr);
  1113. } else if(pcmd->request_buffer) {
  1114. //dc390_pci_sync(pSRB);
  1115. sg_dma_len(&pSRB->Segmentx) = pcmd->request_bufflen - pSRB->Saved_Ptr;
  1116. pSRB->SGcount = 1;
  1117. pSRB->pSegmentList = (struct scatterlist *) &pSRB->Segmentx;
  1118. } else {
  1119. pSRB->SGcount = 0;
  1120. printk (KERN_INFO "DC390: RESTORE_PTR message for Transfer without Scatter-Gather ??\n");
  1121. }
  1122. pSRB->TotalXferredLen = pSRB->Saved_Ptr;
  1123. }
  1124. /* According to the docs, the AM53C974 reads the message and
  1125. * generates a Successful Operation IRQ before asserting ACK for
  1126. * the last byte (how does it know whether it's the last ?) */
  1127. /* The old code handled it in another way, indicating, that on
  1128. * every message byte an IRQ is generated and every byte has to
  1129. * be manually ACKed. Hmmm ? (KG, 98/11/28) */
  1130. /* The old implementation was correct. Sigh! */
  1131. /* Check if the message is complete */
  1132. static u8 __inline__
  1133. dc390_MsgIn_complete (u8 *msgbuf, u32 len)
  1134. {
  1135. if (*msgbuf == EXTENDED_MESSAGE)
  1136. {
  1137. if (len < 2) return 0;
  1138. if (len < msgbuf[1] + 2) return 0;
  1139. }
  1140. else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f) // two byte messages
  1141. if (len < 2) return 0;
  1142. return 1;
  1143. }
  1144. /* read and eval received messages */
  1145. static void
  1146. dc390_MsgIn_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
  1147. {
  1148. struct dc390_dcb* pDCB = pACB->pActiveDCB;
  1149. /* Read the msg */
  1150. pSRB->MsgInBuf[pACB->MsgLen++] = DC390_read8 (ScsiFifo);
  1151. //pSRB->SRBState = 0;
  1152. /* Msg complete ? */
  1153. if (dc390_MsgIn_complete (pSRB->MsgInBuf, pACB->MsgLen))
  1154. {
  1155. DEBUG0 (printk (KERN_INFO "DC390: MsgIn:"); dc390_printMsg (pSRB->MsgInBuf, pACB->MsgLen));
  1156. /* Now eval the msg */
  1157. switch (pSRB->MsgInBuf[0])
  1158. {
  1159. case DISCONNECT:
  1160. pSRB->SRBState = SRB_DISCONNECT; break;
  1161. case SIMPLE_QUEUE_TAG:
  1162. case HEAD_OF_QUEUE_TAG:
  1163. case ORDERED_QUEUE_TAG:
  1164. pSRB = dc390_MsgIn_QTag (pACB, pDCB, pSRB->MsgInBuf[1]);
  1165. break;
  1166. case MESSAGE_REJECT:
  1167. DC390_write8 (ScsiCmd, RESET_ATN_CMD);
  1168. pDCB->NegoPeriod = 50; /* 200ns <=> 5 MHz */
  1169. if( pSRB->SRBState & DO_SYNC_NEGO)
  1170. dc390_MsgIn_set_async (pACB, pSRB);
  1171. break;
  1172. case EXTENDED_MESSAGE:
  1173. /* reject every extended msg but SDTR */
  1174. if (pSRB->MsgInBuf[1] != 3 || pSRB->MsgInBuf[2] != EXTENDED_SDTR)
  1175. dc390_MsgIn_reject (pACB, pSRB);
  1176. else
  1177. {
  1178. if (pSRB->MsgInBuf[3] == 0 || pSRB->MsgInBuf[4] == 0)
  1179. dc390_MsgIn_set_async (pACB, pSRB);
  1180. else
  1181. dc390_MsgIn_set_sync (pACB, pSRB);
  1182. }
  1183. // nothing has to be done
  1184. case COMMAND_COMPLETE: break;
  1185. // SAVE POINTER may be ignored as we have the struct dc390_srb* associated with the
  1186. // scsi command. Thanks, Gerard, for pointing it out.
  1187. case SAVE_POINTERS:
  1188. pSRB->Saved_Ptr = pSRB->TotalXferredLen;
  1189. break;
  1190. // The device might want to restart transfer with a RESTORE
  1191. case RESTORE_POINTERS:
  1192. DEBUG0(printk ("DC390: RESTORE POINTER message received ... try to handle\n"));
  1193. dc390_restore_ptr (pACB, pSRB);
  1194. break;
  1195. // reject unknown messages
  1196. default: dc390_MsgIn_reject (pACB, pSRB);
  1197. }
  1198. /* Clear counter and MsgIn state */
  1199. pSRB->SRBState &= ~SRB_MSGIN;
  1200. pACB->MsgLen = 0;
  1201. }
  1202. *psstatus = SCSI_NOP0;
  1203. DC390_write8 (ScsiCmd, MSG_ACCEPTED_CMD);
  1204. //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
  1205. }
  1206. static void
  1207. dc390_DataIO_Comm( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 ioDir)
  1208. {
  1209. struct scatterlist *psgl;
  1210. unsigned long lval;
  1211. struct dc390_dcb* pDCB = pACB->pActiveDCB;
  1212. if (pSRB == pACB->pTmpSRB)
  1213. {
  1214. if (pDCB)
  1215. printk(KERN_ERR "DC390: pSRB == pTmpSRB! (TagQ Error?) (%02i-%i)\n", pDCB->TargetID, pDCB->TargetLUN);
  1216. else
  1217. printk(KERN_ERR "DC390: pSRB == pTmpSRB! (TagQ Error?) (DCB 0!)\n");
  1218. /* Try to recover - some broken disks react badly to tagged INQUIRY */
  1219. if (pDCB && pACB->scan_devices && pDCB->GoingSRBCnt == 1) {
  1220. pSRB = pDCB->pGoingSRB;
  1221. pDCB->pActiveSRB = pSRB;
  1222. } else {
  1223. pSRB->pSRBDCB = pDCB;
  1224. dc390_EnableMsgOut_Abort(pACB, pSRB);
  1225. if (pDCB)
  1226. pDCB->DCBFlag |= ABORT_DEV;
  1227. return;
  1228. }
  1229. }
  1230. if( pSRB->SGIndex < pSRB->SGcount )
  1231. {
  1232. DC390_write8 (DMA_Cmd, DMA_IDLE_CMD | ioDir /* | DMA_INT */);
  1233. if( !pSRB->SGToBeXferLen )
  1234. {
  1235. psgl = pSRB->pSegmentList;
  1236. pSRB->SGBusAddr = cpu_to_le32(pci_dma_lo32(sg_dma_address(psgl)));
  1237. pSRB->SGToBeXferLen = cpu_to_le32(sg_dma_len(psgl));
  1238. DEBUG1(printk (KERN_DEBUG " DC390: Next SG segment."));
  1239. }
  1240. lval = pSRB->SGToBeXferLen;
  1241. DEBUG1(printk (KERN_DEBUG " DC390: Start transfer: %li bytes (address %08lx)\n", lval, pSRB->SGBusAddr));
  1242. DC390_write8 (CtcReg_Low, (u8) lval);
  1243. lval >>= 8;
  1244. DC390_write8 (CtcReg_Mid, (u8) lval);
  1245. lval >>= 8;
  1246. DC390_write8 (CtcReg_High, (u8) lval);
  1247. DC390_write32 (DMA_XferCnt, pSRB->SGToBeXferLen);
  1248. DC390_write32 (DMA_XferAddr, pSRB->SGBusAddr);
  1249. //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD | ioDir); /* | DMA_INT; */
  1250. pSRB->SRBState = SRB_DATA_XFER;
  1251. DC390_write8 (ScsiCmd, DMA_COMMAND+INFO_XFER_CMD);
  1252. DC390_write8 (DMA_Cmd, DMA_START_CMD | ioDir | DMA_INT);
  1253. //DEBUG1(DC390_write32 (DMA_ScsiBusCtrl, WRT_ERASE_DMA_STAT | EN_INT_ON_PCI_ABORT));
  1254. //DEBUG1(printk (KERN_DEBUG "DC390: DMA_Status: %02x\n", DC390_read8 (DMA_Status)));
  1255. //DEBUG1(DC390_write32 (DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT));
  1256. }
  1257. else /* xfer pad */
  1258. {
  1259. if( pSRB->SGcount )
  1260. {
  1261. pSRB->AdaptStatus = H_OVER_UNDER_RUN;
  1262. pSRB->SRBStatus |= OVER_RUN;
  1263. DEBUG0(printk (KERN_WARNING " DC390: Overrun -"));
  1264. }
  1265. DEBUG0(printk (KERN_WARNING " Clear transfer pad \n"));
  1266. DC390_write8 (CtcReg_Low, 0);
  1267. DC390_write8 (CtcReg_Mid, 0);
  1268. DC390_write8 (CtcReg_High, 0);
  1269. pSRB->SRBState |= SRB_XFERPAD;
  1270. DC390_write8 (ScsiCmd, DMA_COMMAND+XFER_PAD_BYTE);
  1271. /*
  1272. DC390_write8 (DMA_Cmd, DMA_IDLE_CMD | ioDir); // | DMA_INT;
  1273. DC390_write8 (DMA_Cmd, DMA_START_CMD | ioDir | DMA_INT);
  1274. */
  1275. }
  1276. }
  1277. static void
  1278. dc390_DataOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
  1279. {
  1280. dc390_DataIO_Comm (pACB, pSRB, WRITE_DIRECTION);
  1281. }
  1282. static void
  1283. dc390_DataInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
  1284. {
  1285. dc390_DataIO_Comm (pACB, pSRB, READ_DIRECTION);
  1286. }
  1287. static void
  1288. dc390_CommandPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
  1289. {
  1290. struct dc390_dcb* pDCB;
  1291. u8 i, cnt;
  1292. u8 *ptr;
  1293. DC390_write8 (ScsiCmd, RESET_ATN_CMD);
  1294. DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
  1295. if( !(pSRB->SRBFlag & AUTO_REQSENSE) )
  1296. {
  1297. cnt = (u8) pSRB->pcmd->cmd_len;
  1298. ptr = (u8 *) pSRB->pcmd->cmnd;
  1299. for(i=0; i < cnt; i++)
  1300. DC390_write8 (ScsiFifo, *(ptr++));
  1301. }
  1302. else
  1303. {
  1304. DC390_write8 (ScsiFifo, REQUEST_SENSE);
  1305. pDCB = pACB->pActiveDCB;
  1306. DC390_write8 (ScsiFifo, pDCB->TargetLUN << 5);
  1307. DC390_write8 (ScsiFifo, 0);
  1308. DC390_write8 (ScsiFifo, 0);
  1309. DC390_write8 (ScsiFifo, sizeof(pSRB->pcmd->sense_buffer));
  1310. DC390_write8 (ScsiFifo, 0);
  1311. DEBUG0(printk(KERN_DEBUG "DC390: AutoReqSense (CmndPhase)!\n"));
  1312. }
  1313. pSRB->SRBState = SRB_COMMAND;
  1314. DC390_write8 (ScsiCmd, INFO_XFER_CMD);
  1315. }
  1316. static void
  1317. dc390_StatusPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
  1318. {
  1319. DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
  1320. pSRB->SRBState = SRB_STATUS;
  1321. DC390_write8 (ScsiCmd, INITIATOR_CMD_CMPLTE);
  1322. //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
  1323. }
  1324. static void
  1325. dc390_MsgOutPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
  1326. {
  1327. u8 bval, i, cnt;
  1328. u8 *ptr;
  1329. struct dc390_dcb* pDCB;
  1330. DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
  1331. pDCB = pACB->pActiveDCB;
  1332. if( !(pSRB->SRBState & SRB_MSGOUT) )
  1333. {
  1334. cnt = pSRB->MsgCnt;
  1335. if( cnt )
  1336. {
  1337. ptr = (u8 *) pSRB->MsgOutBuf;
  1338. for(i=0; i < cnt; i++)
  1339. DC390_write8 (ScsiFifo, *(ptr++));
  1340. pSRB->MsgCnt = 0;
  1341. if( (pDCB->DCBFlag & ABORT_DEV_) &&
  1342. (pSRB->MsgOutBuf[0] == ABORT) )
  1343. pSRB->SRBState = SRB_ABORT_SENT;
  1344. }
  1345. else
  1346. {
  1347. bval = ABORT; /* ??? MSG_NOP */
  1348. if( (pSRB->pcmd->cmnd[0] == INQUIRY ) ||
  1349. (pSRB->pcmd->cmnd[0] == REQUEST_SENSE) ||
  1350. (pSRB->SRBFlag & AUTO_REQSENSE) )
  1351. {
  1352. if( pDCB->SyncMode & SYNC_ENABLE )
  1353. goto mop1;
  1354. }
  1355. DC390_write8 (ScsiFifo, bval);
  1356. }
  1357. DC390_write8 (ScsiCmd, INFO_XFER_CMD);
  1358. }
  1359. else
  1360. {
  1361. mop1:
  1362. printk (KERN_ERR "DC390: OLD Sync Nego code triggered! (%i %i)\n", pDCB->TargetID, pDCB->TargetLUN);
  1363. DC390_write8 (ScsiFifo, EXTENDED_MESSAGE);
  1364. DC390_write8 (ScsiFifo, 3); /* ;length of extended msg */
  1365. DC390_write8 (ScsiFifo, EXTENDED_SDTR); /* ; sync nego */
  1366. DC390_write8 (ScsiFifo, pDCB->NegoPeriod);
  1367. if (pDCB->SyncOffset & 0x0f)
  1368. DC390_write8 (ScsiFifo, pDCB->SyncOffset);
  1369. else
  1370. DC390_write8 (ScsiFifo, SYNC_NEGO_OFFSET);
  1371. pSRB->SRBState |= DO_SYNC_NEGO;
  1372. DC390_write8 (ScsiCmd, INFO_XFER_CMD);
  1373. }
  1374. }
  1375. static void
  1376. dc390_MsgInPhase( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
  1377. {
  1378. DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
  1379. if( !(pSRB->SRBState & SRB_MSGIN) )
  1380. {
  1381. pSRB->SRBState &= ~SRB_DISCONNECT;
  1382. pSRB->SRBState |= SRB_MSGIN;
  1383. }
  1384. DC390_write8 (ScsiCmd, INFO_XFER_CMD);
  1385. //DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
  1386. }
  1387. static void
  1388. dc390_Nop_0( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
  1389. {
  1390. }
  1391. static void
  1392. dc390_Nop_1( struct dc390_acb* pACB, struct dc390_srb* pSRB, u8 *psstatus)
  1393. {
  1394. }
  1395. static void
  1396. dc390_SetXferRate( struct dc390_acb* pACB, struct dc390_dcb* pDCB )
  1397. {
  1398. u8 bval, i, cnt;
  1399. struct dc390_dcb* ptr;
  1400. if( !(pDCB->TargetLUN) )
  1401. {
  1402. if( !pACB->scan_devices )
  1403. {
  1404. ptr = pACB->pLinkDCB;
  1405. cnt = pACB->DCBCnt;
  1406. bval = pDCB->TargetID;
  1407. for(i=0; i<cnt; i++)
  1408. {
  1409. if( ptr->TargetID == bval )
  1410. {
  1411. ptr->SyncPeriod = pDCB->SyncPeriod;
  1412. ptr->SyncOffset = pDCB->SyncOffset;
  1413. ptr->CtrlR3 = pDCB->CtrlR3;
  1414. ptr->CtrlR4 = pDCB->CtrlR4;
  1415. ptr->SyncMode = pDCB->SyncMode;
  1416. }
  1417. ptr = ptr->pNextDCB;
  1418. }
  1419. }
  1420. }
  1421. return;
  1422. }
  1423. static void
  1424. dc390_Disconnect( struct dc390_acb* pACB )
  1425. {
  1426. struct dc390_dcb *pDCB;
  1427. struct dc390_srb *pSRB, *psrb;
  1428. u8 i, cnt;
  1429. DEBUG0(printk(KERN_INFO "DISC,"));
  1430. if (!pACB->Connected) printk(KERN_ERR "DC390: Disconnect not-connected bus?\n");
  1431. pACB->Connected = 0;
  1432. pDCB = pACB->pActiveDCB;
  1433. if (!pDCB)
  1434. {
  1435. DEBUG0(printk(KERN_ERR "ACB:%p->ActiveDCB:%p IOPort:%04x IRQ:%02x !\n",\
  1436. pACB, pDCB, pACB->IOPortBase, pACB->IRQLevel));
  1437. mdelay(400);
  1438. DC390_read8 (INT_Status); /* Reset Pending INT */
  1439. DC390_write8 (ScsiCmd, EN_SEL_RESEL);
  1440. return;
  1441. }
  1442. DC390_write8 (ScsiCmd, EN_SEL_RESEL);
  1443. pSRB = pDCB->pActiveSRB;
  1444. pACB->pActiveDCB = NULL;
  1445. pSRB->ScsiPhase = SCSI_NOP0;
  1446. if( pSRB->SRBState & SRB_UNEXPECT_RESEL )
  1447. pSRB->SRBState = 0;
  1448. else if( pSRB->SRBState & SRB_ABORT_SENT )
  1449. {
  1450. pDCB->TagMask = 0;
  1451. pDCB->DCBFlag = 0;
  1452. cnt = pDCB->GoingSRBCnt;
  1453. pDCB->GoingSRBCnt = 0;
  1454. pSRB = pDCB->pGoingSRB;
  1455. for( i=0; i < cnt; i++)
  1456. {
  1457. psrb = pSRB->pNextSRB;
  1458. dc390_Free_insert (pACB, pSRB);
  1459. pSRB = psrb;
  1460. }
  1461. pDCB->pGoingSRB = NULL;
  1462. }
  1463. else
  1464. {
  1465. if( (pSRB->SRBState & (SRB_START_+SRB_MSGOUT)) ||
  1466. !(pSRB->SRBState & (SRB_DISCONNECT+SRB_COMPLETED)) )
  1467. { /* Selection time out */
  1468. pSRB->TargetStatus = SCSI_STAT_SEL_TIMEOUT;
  1469. goto disc1;
  1470. }
  1471. else if (!(pSRB->SRBState & SRB_DISCONNECT) && (pSRB->SRBState & SRB_COMPLETED))
  1472. {
  1473. disc1:
  1474. dc390_freetag (pDCB, pSRB);
  1475. pDCB->pActiveSRB = NULL;
  1476. pSRB->SRBState = SRB_FREE;
  1477. dc390_SRBdone( pACB, pDCB, pSRB);
  1478. }
  1479. }
  1480. pACB->MsgLen = 0;
  1481. }
  1482. static void
  1483. dc390_Reselect( struct dc390_acb* pACB )
  1484. {
  1485. struct dc390_dcb* pDCB;
  1486. struct dc390_srb* pSRB;
  1487. u8 id, lun;
  1488. DEBUG0(printk(KERN_INFO "RSEL,"));
  1489. pACB->Connected = 1;
  1490. pDCB = pACB->pActiveDCB;
  1491. if( pDCB )
  1492. { /* Arbitration lost but Reselection won */
  1493. DEBUG0(printk ("DC390: (ActiveDCB != 0: Arb. lost but resel. won)!\n"));
  1494. pSRB = pDCB->pActiveSRB;
  1495. if( !( pACB->scan_devices ) )
  1496. {
  1497. struct scsi_cmnd *pcmd = pSRB->pcmd;
  1498. pcmd->resid = pcmd->request_bufflen;
  1499. SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
  1500. dc390_Going_remove(pDCB, pSRB);
  1501. dc390_Free_insert(pACB, pSRB);
  1502. pcmd->scsi_done (pcmd);
  1503. DEBUG0(printk(KERN_DEBUG"DC390: Return SRB %p to free\n", pSRB));
  1504. }
  1505. }
  1506. /* Get ID */
  1507. lun = DC390_read8 (ScsiFifo);
  1508. DEBUG0(printk ("Dev %02x,", lun));
  1509. if (!(lun & (1 << pACB->pScsiHost->this_id)))
  1510. printk (KERN_ERR "DC390: Reselection must select host adapter: %02x!\n", lun);
  1511. else
  1512. lun ^= 1 << pACB->pScsiHost->this_id; /* Mask AdapterID */
  1513. id = 0; while (lun >>= 1) id++;
  1514. /* Get LUN */
  1515. lun = DC390_read8 (ScsiFifo);
  1516. if (!(lun & IDENTIFY_BASE)) printk (KERN_ERR "DC390: Resel: Expect identify message!\n");
  1517. lun &= 7;
  1518. DEBUG0(printk ("(%02i-%i),", id, lun));
  1519. pDCB = dc390_findDCB (pACB, id, lun);
  1520. if (!pDCB)
  1521. {
  1522. printk (KERN_ERR "DC390: Reselect from non existing device (%02i-%i)\n",
  1523. id, lun);
  1524. return;
  1525. }
  1526. pACB->pActiveDCB = pDCB;
  1527. /* TagQ: We expect a message soon, so never mind the exact SRB */
  1528. if( pDCB->SyncMode & EN_TAG_QUEUEING )
  1529. {
  1530. pSRB = pACB->pTmpSRB;
  1531. pDCB->pActiveSRB = pSRB;
  1532. }
  1533. else
  1534. {
  1535. pSRB = pDCB->pActiveSRB;
  1536. if( !pSRB || !(pSRB->SRBState & SRB_DISCONNECT) )
  1537. {
  1538. pSRB= pACB->pTmpSRB;
  1539. pSRB->SRBState = SRB_UNEXPECT_RESEL;
  1540. printk (KERN_ERR "DC390: Reselect without outstanding cmnd (%02i-%i)\n",
  1541. id, lun);
  1542. pDCB->pActiveSRB = pSRB;
  1543. dc390_EnableMsgOut_Abort ( pACB, pSRB );
  1544. }
  1545. else
  1546. {
  1547. if( pDCB->DCBFlag & ABORT_DEV_ )
  1548. {
  1549. pSRB->SRBState = SRB_ABORT_SENT;
  1550. printk (KERN_INFO "DC390: Reselect: Abort (%02i-%i)\n",
  1551. id, lun);
  1552. dc390_EnableMsgOut_Abort( pACB, pSRB );
  1553. }
  1554. else
  1555. pSRB->SRBState = SRB_DATA_XFER;
  1556. }
  1557. }
  1558. DEBUG1(printk (KERN_DEBUG "Resel SRB(%p): TagNum (%02x)\n", pSRB, pSRB->TagNumber));
  1559. pSRB->ScsiPhase = SCSI_NOP0;
  1560. DC390_write8 (Scsi_Dest_ID, pDCB->TargetID);
  1561. DC390_write8 (Sync_Period, pDCB->SyncPeriod);
  1562. DC390_write8 (Sync_Offset, pDCB->SyncOffset);
  1563. DC390_write8 (CtrlReg1, pDCB->CtrlR1);
  1564. DC390_write8 (CtrlReg3, pDCB->CtrlR3);
  1565. DC390_write8 (CtrlReg4, pDCB->CtrlR4); /* ; Glitch eater */
  1566. DC390_write8 (ScsiCmd, MSG_ACCEPTED_CMD); /* ;to release the /ACK signal */
  1567. }
  1568. static int __inline__
  1569. dc390_RequestSense(struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
  1570. {
  1571. struct scsi_cmnd *pcmd;
  1572. pcmd = pSRB->pcmd;
  1573. REMOVABLEDEBUG(printk(KERN_INFO "DC390: RequestSense(Cmd %02x, Id %02x, LUN %02x)\n",\
  1574. pcmd->cmnd[0], pDCB->TargetID, pDCB->TargetLUN));
  1575. pSRB->SRBFlag |= AUTO_REQSENSE;
  1576. pSRB->SavedSGCount = pcmd->use_sg;
  1577. pSRB->SavedTotXLen = pSRB->TotalXferredLen;
  1578. pSRB->AdaptStatus = 0;
  1579. pSRB->TargetStatus = 0; /* CHECK_CONDITION<<1; */
  1580. /* We are called from SRBdone, original PCI mapping has been removed
  1581. * already, new one is set up from StartSCSI */
  1582. pSRB->SGIndex = 0;
  1583. pSRB->TotalXferredLen = 0;
  1584. pSRB->SGToBeXferLen = 0;
  1585. return dc390_StartSCSI(pACB, pDCB, pSRB);
  1586. }
  1587. static void
  1588. dc390_SRBdone( struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB )
  1589. {
  1590. u8 status;
  1591. struct scsi_cmnd *pcmd;
  1592. pcmd = pSRB->pcmd;
  1593. /* KG: Moved pci_unmap here */
  1594. dc390_pci_unmap(pSRB);
  1595. status = pSRB->TargetStatus;
  1596. DEBUG0(printk (" SRBdone (%02x,%08x), SRB %p, pid %li\n", status, pcmd->result,\
  1597. pSRB, pcmd->pid));
  1598. if(pSRB->SRBFlag & AUTO_REQSENSE)
  1599. { /* Last command was a Request Sense */
  1600. pSRB->SRBFlag &= ~AUTO_REQSENSE;
  1601. pSRB->AdaptStatus = 0;
  1602. pSRB->TargetStatus = CHECK_CONDITION << 1;
  1603. //pcmd->result = MK_RES(DRIVER_SENSE,DID_OK,0,status);
  1604. if (status == (CHECK_CONDITION << 1))
  1605. pcmd->result = MK_RES_LNX(0, DID_BAD_TARGET, 0, /*CHECK_CONDITION*/0);
  1606. else /* Retry */
  1607. {
  1608. if( pSRB->pcmd->cmnd[0] == TEST_UNIT_READY /* || pSRB->pcmd->cmnd[0] == START_STOP */)
  1609. {
  1610. /* Don't retry on TEST_UNIT_READY */
  1611. pcmd->result = MK_RES_LNX(DRIVER_SENSE,DID_OK,0,CHECK_CONDITION);
  1612. REMOVABLEDEBUG(printk(KERN_INFO "Cmd=%02x, Result=%08x, XferL=%08x\n",pSRB->pcmd->cmnd[0],\
  1613. (u32) pcmd->result, (u32) pSRB->TotalXferredLen));
  1614. } else {
  1615. SET_RES_DRV(pcmd->result, DRIVER_SENSE);
  1616. pcmd->use_sg = pSRB->SavedSGCount;
  1617. //pSRB->ScsiCmdLen = (u8) (pSRB->Segment1[0] >> 8);
  1618. DEBUG0 (printk ("DC390: RETRY pid %li (%02x), target %02i-%02i\n", pcmd->pid, pcmd->cmnd[0], pcmd->device->id, pcmd->device->lun));
  1619. pSRB->TotalXferredLen = 0;
  1620. SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
  1621. }
  1622. }
  1623. goto cmd_done;
  1624. }
  1625. if( status )
  1626. {
  1627. if( status_byte(status) == CHECK_CONDITION )
  1628. {
  1629. if (dc390_RequestSense(pACB, pDCB, pSRB)) {
  1630. SET_RES_DID(pcmd->result, DID_ERROR);
  1631. goto cmd_done;
  1632. }
  1633. return;
  1634. }
  1635. else if( status_byte(status) == QUEUE_FULL )
  1636. {
  1637. scsi_track_queue_full(pcmd->device, pDCB->GoingSRBCnt - 1);
  1638. pcmd->use_sg = pSRB->SavedSGCount;
  1639. DEBUG0 (printk ("DC390: RETRY pid %li (%02x), target %02i-%02i\n", pcmd->pid, pcmd->cmnd[0], pcmd->device->id, pcmd->device->lun));
  1640. pSRB->TotalXferredLen = 0;
  1641. SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
  1642. }
  1643. else if(status == SCSI_STAT_SEL_TIMEOUT)
  1644. {
  1645. pSRB->AdaptStatus = H_SEL_TIMEOUT;
  1646. pSRB->TargetStatus = 0;
  1647. pcmd->result = MK_RES(0,DID_NO_CONNECT,0,0);
  1648. /* Devices are removed below ... */
  1649. }
  1650. else if (status_byte(status) == BUSY &&
  1651. (pcmd->cmnd[0] == TEST_UNIT_READY || pcmd->cmnd[0] == INQUIRY) &&
  1652. pACB->scan_devices)
  1653. {
  1654. pSRB->AdaptStatus = 0;
  1655. pSRB->TargetStatus = status;
  1656. pcmd->result = MK_RES(0,0,pSRB->EndMessage,/*status*/0);
  1657. }
  1658. else
  1659. { /* Another error */
  1660. pSRB->TotalXferredLen = 0;
  1661. SET_RES_DID(pcmd->result, DID_SOFT_ERROR);
  1662. goto cmd_done;
  1663. }
  1664. }
  1665. else
  1666. { /* Target status == 0 */
  1667. status = pSRB->AdaptStatus;
  1668. if(status & H_OVER_UNDER_RUN)
  1669. {
  1670. pSRB->TargetStatus = 0;
  1671. SET_RES_DID(pcmd->result,DID_OK);
  1672. SET_RES_MSG(pcmd->result,pSRB->EndMessage);
  1673. }
  1674. else if( pSRB->SRBStatus & PARITY_ERROR)
  1675. {
  1676. //pcmd->result = MK_RES(0,DID_PARITY,pSRB->EndMessage,0);
  1677. SET_RES_DID(pcmd->result,DID_PARITY);
  1678. SET_RES_MSG(pcmd->result,pSRB->EndMessage);
  1679. }
  1680. else /* No error */
  1681. {
  1682. pSRB->AdaptStatus = 0;
  1683. pSRB->TargetStatus = 0;
  1684. SET_RES_DID(pcmd->result,DID_OK);
  1685. }
  1686. }
  1687. cmd_done:
  1688. pcmd->resid = pcmd->request_bufflen - pSRB->TotalXferredLen;
  1689. dc390_Going_remove (pDCB, pSRB);
  1690. /* Add to free list */
  1691. dc390_Free_insert (pACB, pSRB);
  1692. DEBUG0(printk (KERN_DEBUG "DC390: SRBdone: done pid %li\n", pcmd->pid));
  1693. pcmd->scsi_done (pcmd);
  1694. return;
  1695. }
  1696. /* Remove all SRBs from Going list and inform midlevel */
  1697. static void
  1698. dc390_DoingSRB_Done(struct dc390_acb* pACB, struct scsi_cmnd *cmd)
  1699. {
  1700. struct dc390_dcb *pDCB, *pdcb;
  1701. struct dc390_srb *psrb, *psrb2;
  1702. int i;
  1703. struct scsi_cmnd *pcmd;
  1704. pDCB = pACB->pLinkDCB;
  1705. pdcb = pDCB;
  1706. if (! pdcb) return;
  1707. do
  1708. {
  1709. psrb = pdcb->pGoingSRB;
  1710. for (i = 0; i < pdcb->GoingSRBCnt; i++)
  1711. {
  1712. psrb2 = psrb->pNextSRB;
  1713. pcmd = psrb->pcmd;
  1714. dc390_Free_insert (pACB, psrb);
  1715. psrb = psrb2;
  1716. }
  1717. pdcb->GoingSRBCnt = 0;
  1718. pdcb->pGoingSRB = NULL;
  1719. pdcb->TagMask = 0;
  1720. pdcb = pdcb->pNextDCB;
  1721. } while( pdcb != pDCB );
  1722. }
  1723. static void
  1724. dc390_ResetSCSIBus( struct dc390_acb* pACB )
  1725. {
  1726. //DC390_write8 (ScsiCmd, RST_DEVICE_CMD);
  1727. //udelay (250);
  1728. //DC390_write8 (ScsiCmd, NOP_CMD);
  1729. DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
  1730. DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
  1731. DC390_write8 (ScsiCmd, RST_SCSI_BUS_CMD);
  1732. pACB->Connected = 0;
  1733. return;
  1734. }
  1735. static void
  1736. dc390_ScsiRstDetect( struct dc390_acb* pACB )
  1737. {
  1738. printk ("DC390: Rst_Detect: laststat = %08x\n", dc390_laststatus);
  1739. //DEBUG0(printk(KERN_INFO "RST_DETECT,"));
  1740. DC390_write8 (DMA_Cmd, DMA_IDLE_CMD);
  1741. /* Unlock before ? */
  1742. /* delay half a second */
  1743. udelay (1000);
  1744. DC390_write8 (ScsiCmd, CLEAR_FIFO_CMD);
  1745. pACB->pScsiHost->last_reset = jiffies + 5*HZ/2
  1746. + HZ * dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY];
  1747. pACB->Connected = 0;
  1748. if( pACB->ACBFlag & RESET_DEV )
  1749. pACB->ACBFlag |= RESET_DONE;
  1750. else
  1751. { /* Reset was issued by sb else */
  1752. pACB->ACBFlag |= RESET_DETECT;
  1753. dc390_ResetDevParam( pACB );
  1754. dc390_DoingSRB_Done( pACB, NULL);
  1755. //dc390_RecoverSRB( pACB );
  1756. pACB->pActiveDCB = NULL;
  1757. pACB->ACBFlag = 0;
  1758. }
  1759. return;
  1760. }
  1761. static int DC390_queuecommand(struct scsi_cmnd *cmd,
  1762. void (*done)(struct scsi_cmnd *))
  1763. {
  1764. struct scsi_device *sdev = cmd->device;
  1765. struct dc390_acb *acb = (struct dc390_acb *)sdev->host->hostdata;
  1766. struct dc390_dcb *dcb = sdev->hostdata;
  1767. struct dc390_srb *srb;
  1768. if (sdev->queue_depth <= dcb->GoingSRBCnt)
  1769. goto device_busy;
  1770. if (acb->pActiveDCB)
  1771. goto host_busy;
  1772. if (acb->ACBFlag & (RESET_DETECT|RESET_DONE|RESET_DEV))
  1773. goto host_busy;
  1774. srb = acb->pFreeSRB;
  1775. if (unlikely(srb == NULL))
  1776. goto host_busy;
  1777. cmd->scsi_done = done;
  1778. cmd->result = 0;
  1779. acb->Cmds++;
  1780. acb->pFreeSRB = srb->pNextSRB;
  1781. srb->pNextSRB = NULL;
  1782. srb->pSRBDCB = dcb;
  1783. srb->pcmd = cmd;
  1784. cmd->host_scribble = (char *)srb;
  1785. srb->SGIndex = 0;
  1786. srb->AdaptStatus = 0;
  1787. srb->TargetStatus = 0;
  1788. srb->MsgCnt = 0;
  1789. srb->SRBStatus = 0;
  1790. srb->SRBFlag = 0;
  1791. srb->SRBState = 0;
  1792. srb->TotalXferredLen = 0;
  1793. srb->SGBusAddr = 0;
  1794. srb->SGToBeXferLen = 0;
  1795. srb->ScsiPhase = 0;
  1796. srb->EndMessage = 0;
  1797. srb->TagNumber = SCSI_NO_TAG;
  1798. if (dc390_StartSCSI(acb, dcb, srb)) {
  1799. dc390_Free_insert(acb, srb);
  1800. goto host_busy;
  1801. }
  1802. dc390_Going_append(dcb, srb);
  1803. return 0;
  1804. host_busy:
  1805. return SCSI_MLQUEUE_HOST_BUSY;
  1806. device_busy:
  1807. return SCSI_MLQUEUE_DEVICE_BUSY;
  1808. }
  1809. static void dc390_dumpinfo (struct dc390_acb* pACB, struct dc390_dcb* pDCB, struct dc390_srb* pSRB)
  1810. {
  1811. struct pci_dev *pdev;
  1812. u16 pstat;
  1813. if (!pDCB) pDCB = pACB->pActiveDCB;
  1814. if (!pSRB && pDCB) pSRB = pDCB->pActiveSRB;
  1815. if (pSRB)
  1816. {
  1817. printk ("DC390: SRB: Xferred %08lx, Remain %08lx, State %08x, Phase %02x\n",
  1818. pSRB->TotalXferredLen, pSRB->SGToBeXferLen, pSRB->SRBState,
  1819. pSRB->ScsiPhase);
  1820. printk ("DC390: AdpaterStatus: %02x, SRB Status %02x\n", pSRB->AdaptStatus, pSRB->SRBStatus);
  1821. }
  1822. printk ("DC390: Status of last IRQ (DMA/SC/Int/IRQ): %08x\n", dc390_laststatus);
  1823. printk ("DC390: Register dump: SCSI block:\n");
  1824. printk ("DC390: XferCnt Cmd Stat IntS IRQS FFIS Ctl1 Ctl2 Ctl3 Ctl4\n");
  1825. printk ("DC390: %06x %02x %02x %02x",
  1826. DC390_read8(CtcReg_Low) + (DC390_read8(CtcReg_Mid) << 8) + (DC390_read8(CtcReg_High) << 16),
  1827. DC390_read8(ScsiCmd), DC390_read8(Scsi_Status), DC390_read8(Intern_State));
  1828. printk (" %02x %02x %02x %02x %02x %02x\n",
  1829. DC390_read8(INT_Status), DC390_read8(Current_Fifo), DC390_read8(CtrlReg1),
  1830. DC390_read8(CtrlReg2), DC390_read8(CtrlReg3), DC390_read8(CtrlReg4));
  1831. DC390_write32 (DMA_ScsiBusCtrl, WRT_ERASE_DMA_STAT | EN_INT_ON_PCI_ABORT);
  1832. if (DC390_read8(Current_Fifo) & 0x1f)
  1833. {
  1834. printk ("DC390: FIFO:");
  1835. while (DC390_read8(Current_Fifo) & 0x1f) printk (" %02x", DC390_read8(ScsiFifo));
  1836. printk ("\n");
  1837. }
  1838. printk ("DC390: Register dump: DMA engine:\n");
  1839. printk ("DC390: Cmd STrCnt SBusA WrkBC WrkAC Stat SBusCtrl\n");
  1840. printk ("DC390: %02x %08x %08x %08x %08x %02x %08x\n",
  1841. DC390_read8(DMA_Cmd), DC390_read32(DMA_XferCnt), DC390_read32(DMA_XferAddr),
  1842. DC390_read32(DMA_Wk_ByteCntr), DC390_read32(DMA_Wk_AddrCntr),
  1843. DC390_read8(DMA_Status), DC390_read32(DMA_ScsiBusCtrl));
  1844. DC390_write32 (DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT);
  1845. pdev = pACB->pdev;
  1846. pci_read_config_word(pdev, PCI_STATUS, &pstat);
  1847. printk ("DC390: Register dump: PCI Status: %04x\n", pstat);
  1848. printk ("DC390: In case of driver trouble read Documentation/scsi/tmscsim.txt\n");
  1849. }
  1850. static int DC390_abort(struct scsi_cmnd *cmd)
  1851. {
  1852. struct dc390_acb *pACB = (struct dc390_acb*) cmd->device->host->hostdata;
  1853. struct dc390_dcb *pDCB = (struct dc390_dcb*) cmd->device->hostdata;
  1854. printk("DC390: Abort command (pid %li, Device %02i-%02i)\n",
  1855. cmd->pid, cmd->device->id, cmd->device->lun);
  1856. /* abort() is too stupid for already sent commands at the moment.
  1857. * If it's called we are in trouble anyway, so let's dump some info
  1858. * into the syslog at least. (KG, 98/08/20,99/06/20) */
  1859. dc390_dumpinfo(pACB, pDCB, NULL);
  1860. pDCB->DCBFlag |= ABORT_DEV_;
  1861. printk(KERN_INFO "DC390: Aborted pid %li\n", cmd->pid);
  1862. return FAILED;
  1863. }
  1864. static void dc390_ResetDevParam( struct dc390_acb* pACB )
  1865. {
  1866. struct dc390_dcb *pDCB, *pdcb;
  1867. pDCB = pACB->pLinkDCB;
  1868. if (! pDCB) return;
  1869. pdcb = pDCB;
  1870. do
  1871. {
  1872. pDCB->SyncMode &= ~SYNC_NEGO_DONE;
  1873. pDCB->SyncPeriod = 0;
  1874. pDCB->SyncOffset = 0;
  1875. pDCB->TagMask = 0;
  1876. pDCB->CtrlR3 = FAST_CLK;
  1877. pDCB->CtrlR4 &= NEGATE_REQACKDATA | CTRL4_RESERVED | NEGATE_REQACK;
  1878. pDCB->CtrlR4 |= pACB->glitch_cfg;
  1879. pDCB = pDCB->pNextDCB;
  1880. }
  1881. while( pdcb != pDCB );
  1882. pACB->ACBFlag &= ~(RESET_DEV | RESET_DONE | RESET_DETECT);
  1883. }
  1884. static int DC390_bus_reset (struct scsi_cmnd *cmd)
  1885. {
  1886. struct dc390_acb* pACB = (struct dc390_acb*) cmd->device->host->hostdata;
  1887. u8 bval;
  1888. spin_lock_irq(cmd->device->host->host_lock);
  1889. bval = DC390_read8(CtrlReg1) | DIS_INT_ON_SCSI_RST;
  1890. DC390_write8(CtrlReg1, bval); /* disable IRQ on bus reset */
  1891. pACB->ACBFlag |= RESET_DEV;
  1892. dc390_ResetSCSIBus(pACB);
  1893. dc390_ResetDevParam(pACB);
  1894. mdelay(1);
  1895. pACB->pScsiHost->last_reset = jiffies + 3*HZ/2
  1896. + HZ * dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY];
  1897. DC390_write8(ScsiCmd, CLEAR_FIFO_CMD);
  1898. DC390_read8(INT_Status); /* Reset Pending INT */
  1899. dc390_DoingSRB_Done(pACB, cmd);
  1900. pACB->pActiveDCB = NULL;
  1901. pACB->ACBFlag = 0;
  1902. bval = DC390_read8(CtrlReg1) & ~DIS_INT_ON_SCSI_RST;
  1903. DC390_write8(CtrlReg1, bval); /* re-enable interrupt */
  1904. spin_unlock_irq(cmd->device->host->host_lock);
  1905. return SUCCESS;
  1906. }
  1907. /**
  1908. * dc390_slave_alloc - Called by the scsi mid layer to tell us about a new
  1909. * scsi device that we need to deal with.
  1910. *
  1911. * @scsi_device: The new scsi device that we need to handle.
  1912. */
  1913. static int dc390_slave_alloc(struct scsi_device *scsi_device)
  1914. {
  1915. struct dc390_acb *pACB = (struct dc390_acb*) scsi_device->host->hostdata;
  1916. struct dc390_dcb *pDCB, *pDCB2 = NULL;
  1917. uint id = scsi_device->id;
  1918. uint lun = scsi_device->lun;
  1919. pDCB = kmalloc(sizeof(struct dc390_dcb), GFP_KERNEL);
  1920. if (!pDCB)
  1921. return -ENOMEM;
  1922. memset(pDCB, 0, sizeof(struct dc390_dcb));
  1923. if (!pACB->DCBCnt++) {
  1924. pACB->pLinkDCB = pDCB;
  1925. pACB->pDCBRunRobin = pDCB;
  1926. } else {
  1927. pACB->pLastDCB->pNextDCB = pDCB;
  1928. }
  1929. pDCB->pNextDCB = pACB->pLinkDCB;
  1930. pACB->pLastDCB = pDCB;
  1931. pDCB->pDCBACB = pACB;
  1932. pDCB->TargetID = id;
  1933. pDCB->TargetLUN = lun;
  1934. /*
  1935. * Some values are for all LUNs: Copy them
  1936. * In a clean way: We would have an own structure for a SCSI-ID
  1937. */
  1938. if (lun && (pDCB2 = dc390_findDCB(pACB, id, 0))) {
  1939. pDCB->DevMode = pDCB2->DevMode;
  1940. pDCB->SyncMode = pDCB2->SyncMode & SYNC_NEGO_DONE;
  1941. pDCB->SyncPeriod = pDCB2->SyncPeriod;
  1942. pDCB->SyncOffset = pDCB2->SyncOffset;
  1943. pDCB->NegoPeriod = pDCB2->NegoPeriod;
  1944. pDCB->CtrlR3 = pDCB2->CtrlR3;
  1945. pDCB->CtrlR4 = pDCB2->CtrlR4;
  1946. } else {
  1947. u8 index = pACB->AdapterIndex;
  1948. PEEprom prom = (PEEprom) &dc390_eepromBuf[index][id << 2];
  1949. pDCB->DevMode = prom->EE_MODE1;
  1950. pDCB->NegoPeriod =
  1951. (dc390_clock_period1[prom->EE_SPEED] * 25) >> 2;
  1952. pDCB->CtrlR3 = FAST_CLK;
  1953. pDCB->CtrlR4 = pACB->glitch_cfg | CTRL4_RESERVED;
  1954. if (dc390_eepromBuf[index][EE_MODE2] & ACTIVE_NEGATION)
  1955. pDCB->CtrlR4 |= NEGATE_REQACKDATA | NEGATE_REQACK;
  1956. }
  1957. if (pDCB->DevMode & SYNC_NEGO_)
  1958. pDCB->SyncMode |= SYNC_ENABLE;
  1959. else {
  1960. pDCB->SyncMode = 0;
  1961. pDCB->SyncOffset &= ~0x0f;
  1962. }
  1963. pDCB->CtrlR1 = pACB->pScsiHost->this_id;
  1964. if (pDCB->DevMode & PARITY_CHK_)
  1965. pDCB->CtrlR1 |= PARITY_ERR_REPO;
  1966. pACB->scan_devices = 1;
  1967. scsi_device->hostdata = pDCB;
  1968. return 0;
  1969. }
  1970. /**
  1971. * dc390_slave_destroy - Called by the scsi mid layer to tell us about a
  1972. * device that is going away.
  1973. *
  1974. * @scsi_device: The scsi device that we need to remove.
  1975. */
  1976. static void dc390_slave_destroy(struct scsi_device *scsi_device)
  1977. {
  1978. struct dc390_acb* pACB = (struct dc390_acb*) scsi_device->host->hostdata;
  1979. struct dc390_dcb* pDCB = (struct dc390_dcb*) scsi_device->hostdata;
  1980. struct dc390_dcb* pPrevDCB = pACB->pLinkDCB;
  1981. pACB->scan_devices = 0;
  1982. BUG_ON(pDCB->GoingSRBCnt > 1);
  1983. if (pDCB == pACB->pLinkDCB) {
  1984. if (pACB->pLastDCB == pDCB) {
  1985. pDCB->pNextDCB = NULL;
  1986. pACB->pLastDCB = NULL;
  1987. }
  1988. pACB->pLinkDCB = pDCB->pNextDCB;
  1989. } else {
  1990. while (pPrevDCB->pNextDCB != pDCB)
  1991. pPrevDCB = pPrevDCB->pNextDCB;
  1992. pPrevDCB->pNextDCB = pDCB->pNextDCB;
  1993. if (pDCB == pACB->pLastDCB)
  1994. pACB->pLastDCB = pPrevDCB;
  1995. }
  1996. if (pDCB == pACB->pActiveDCB)
  1997. pACB->pActiveDCB = NULL;
  1998. if (pDCB == pACB->pLinkDCB)
  1999. pACB->pLinkDCB = pDCB->pNextDCB;
  2000. if (pDCB == pACB->pDCBRunRobin)
  2001. pACB->pDCBRunRobin = pDCB->pNextDCB;
  2002. kfree(pDCB);
  2003. pACB->DCBCnt--;
  2004. }
  2005. static int dc390_slave_configure(struct scsi_device *sdev)
  2006. {
  2007. struct dc390_acb *acb = (struct dc390_acb *)sdev->host->hostdata;
  2008. struct dc390_dcb *dcb = (struct dc390_dcb *)sdev->hostdata;
  2009. acb->scan_devices = 0;
  2010. if (sdev->tagged_supported && (dcb->DevMode & TAG_QUEUEING_)) {
  2011. dcb->SyncMode |= EN_TAG_QUEUEING;
  2012. scsi_activate_tcq(sdev, acb->TagMaxNum);
  2013. }
  2014. return 0;
  2015. }
  2016. static struct scsi_host_template driver_template = {
  2017. .module = THIS_MODULE,
  2018. .proc_name = "tmscsim",
  2019. .name = DC390_BANNER " V" DC390_VERSION,
  2020. .slave_alloc = dc390_slave_alloc,
  2021. .slave_configure = dc390_slave_configure,
  2022. .slave_destroy = dc390_slave_destroy,
  2023. .queuecommand = DC390_queuecommand,
  2024. .eh_abort_handler = DC390_abort,
  2025. .eh_bus_reset_handler = DC390_bus_reset,
  2026. .can_queue = 1,
  2027. .this_id = 7,
  2028. .sg_tablesize = SG_ALL,
  2029. .cmd_per_lun = 1,
  2030. .use_clustering = DISABLE_CLUSTERING,
  2031. };
  2032. /***********************************************************************
  2033. * Functions for access to DC390 EEPROM
  2034. * and some to emulate it
  2035. *
  2036. **********************************************************************/
  2037. static void __devinit dc390_eeprom_prepare_read(struct pci_dev *pdev, u8 cmd)
  2038. {
  2039. u8 carryFlag = 1, j = 0x80, bval;
  2040. int i;
  2041. for (i = 0; i < 9; i++) {
  2042. if (carryFlag) {
  2043. pci_write_config_byte(pdev, 0x80, 0x40);
  2044. bval = 0xc0;
  2045. } else
  2046. bval = 0x80;
  2047. udelay(160);
  2048. pci_write_config_byte(pdev, 0x80, bval);
  2049. udelay(160);
  2050. pci_write_config_byte(pdev, 0x80, 0);
  2051. udelay(160);
  2052. carryFlag = (cmd & j) ? 1 : 0;
  2053. j >>= 1;
  2054. }
  2055. }
  2056. static u16 __devinit dc390_eeprom_get_data(struct pci_dev *pdev)
  2057. {
  2058. int i;
  2059. u16 wval = 0;
  2060. u8 bval;
  2061. for (i = 0; i < 16; i++) {
  2062. wval <<= 1;
  2063. pci_write_config_byte(pdev, 0x80, 0x80);
  2064. udelay(160);
  2065. pci_write_config_byte(pdev, 0x80, 0x40);
  2066. udelay(160);
  2067. pci_read_config_byte(pdev, 0x00, &bval);
  2068. if (bval == 0x22)
  2069. wval |= 1;
  2070. }
  2071. return wval;
  2072. }
  2073. static void __devinit dc390_read_eeprom(struct pci_dev *pdev, u16 *ptr)
  2074. {
  2075. u8 cmd = EEPROM_READ, i;
  2076. for (i = 0; i < 0x40; i++) {
  2077. pci_write_config_byte(pdev, 0xc0, 0);
  2078. udelay(160);
  2079. dc390_eeprom_prepare_read(pdev, cmd++);
  2080. *ptr++ = dc390_eeprom_get_data(pdev);
  2081. pci_write_config_byte(pdev, 0x80, 0);
  2082. pci_write_config_byte(pdev, 0x80, 0);
  2083. udelay(160);
  2084. }
  2085. }
  2086. /* Override EEprom values with explicitly set values */
  2087. static void __devinit dc390_eeprom_override(u8 index)
  2088. {
  2089. u8 *ptr = (u8 *) dc390_eepromBuf[index], id;
  2090. /* Adapter Settings */
  2091. if (tmscsim[0] != -2)
  2092. ptr[EE_ADAPT_SCSI_ID] = (u8)tmscsim[0]; /* Adapter ID */
  2093. if (tmscsim[3] != -2)
  2094. ptr[EE_MODE2] = (u8)tmscsim[3];
  2095. if (tmscsim[5] != -2)
  2096. ptr[EE_DELAY] = tmscsim[5]; /* Reset delay */
  2097. if (tmscsim[4] != -2)
  2098. ptr[EE_TAG_CMD_NUM] = (u8)tmscsim[4]; /* Tagged Cmds */
  2099. /* Device Settings */
  2100. for (id = 0; id < MAX_SCSI_ID; id++) {
  2101. if (tmscsim[2] != -2)
  2102. ptr[id << 2] = (u8)tmscsim[2]; /* EE_MODE1 */
  2103. if (tmscsim[1] != -2)
  2104. ptr[(id << 2) + 1] = (u8)tmscsim[1]; /* EE_Speed */
  2105. }
  2106. }
  2107. static int __devinitdata tmscsim_def[] = {
  2108. 7,
  2109. 0 /* 10MHz */,
  2110. PARITY_CHK_ | SEND_START_ | EN_DISCONNECT_ | SYNC_NEGO_ | TAG_QUEUEING_,
  2111. MORE2_DRV | GREATER_1G | RST_SCSI_BUS | ACTIVE_NEGATION | LUN_CHECK,
  2112. 3 /* 16 Tags per LUN */,
  2113. 1 /* s delay after Reset */,
  2114. };
  2115. /* Copy defaults over set values where missing */
  2116. static void __devinit dc390_fill_with_defaults (void)
  2117. {
  2118. int i;
  2119. for (i = 0; i < 6; i++) {
  2120. if (tmscsim[i] < 0 || tmscsim[i] > 255)
  2121. tmscsim[i] = tmscsim_def[i];
  2122. }
  2123. /* Sanity checks */
  2124. if (tmscsim[0] > 7)
  2125. tmscsim[0] = 7;
  2126. if (tmscsim[1] > 7)
  2127. tmscsim[1] = 4;
  2128. if (tmscsim[4] > 5)
  2129. tmscsim[4] = 4;
  2130. if (tmscsim[5] > 180)
  2131. tmscsim[5] = 180;
  2132. }
  2133. static void __devinit dc390_check_eeprom(struct pci_dev *pdev, u8 index)
  2134. {
  2135. u8 interpd[] = {1, 3, 5, 10, 16, 30, 60, 120};
  2136. u8 EEbuf[128];
  2137. u16 *ptr = (u16 *)EEbuf, wval = 0;
  2138. int i;
  2139. dc390_read_eeprom(pdev, ptr);
  2140. memcpy(dc390_eepromBuf[index], EEbuf, EE_ADAPT_SCSI_ID);
  2141. memcpy(&dc390_eepromBuf[index][EE_ADAPT_SCSI_ID],
  2142. &EEbuf[REAL_EE_ADAPT_SCSI_ID], EE_LEN - EE_ADAPT_SCSI_ID);
  2143. dc390_eepromBuf[index][EE_DELAY] = interpd[dc390_eepromBuf[index][EE_DELAY]];
  2144. for (i = 0; i < 0x40; i++, ptr++)
  2145. wval += *ptr;
  2146. /* no Tekram EEprom found */
  2147. if (wval != 0x1234) {
  2148. int speed;
  2149. printk(KERN_INFO "DC390_init: No EEPROM found! Trying default settings ...\n");
  2150. /*
  2151. * XXX(hch): bogus, because we might have tekram and
  2152. * non-tekram hbas in a single machine.
  2153. */
  2154. dc390_fill_with_defaults();
  2155. speed = dc390_clock_speed[tmscsim[1]];
  2156. printk(KERN_INFO "DC390: Used defaults: AdaptID=%i, SpeedIdx=%i (%i.%i MHz), "
  2157. "DevMode=0x%02x, AdaptMode=0x%02x, TaggedCmnds=%i (%i), DelayReset=%is\n",
  2158. tmscsim[0], tmscsim[1], speed / 10, speed % 10,
  2159. (u8)tmscsim[2], (u8)tmscsim[3], tmscsim[4], 2 << (tmscsim[4]), tmscsim[5]);
  2160. }
  2161. }
  2162. static void __devinit dc390_init_hw(struct dc390_acb *pACB, u8 index)
  2163. {
  2164. struct Scsi_Host *shost = pACB->pScsiHost;
  2165. u8 dstate;
  2166. /* Disable SCSI bus reset interrupt */
  2167. DC390_write8(CtrlReg1, DIS_INT_ON_SCSI_RST | shost->this_id);
  2168. if (pACB->Gmode2 & RST_SCSI_BUS) {
  2169. dc390_ResetSCSIBus(pACB);
  2170. udelay(1000);
  2171. shost->last_reset = jiffies + HZ/2 +
  2172. HZ * dc390_eepromBuf[pACB->AdapterIndex][EE_DELAY];
  2173. }
  2174. pACB->ACBFlag = 0;
  2175. /* Reset Pending INT */
  2176. DC390_read8(INT_Status);
  2177. /* 250ms selection timeout */
  2178. DC390_write8(Scsi_TimeOut, SEL_TIMEOUT);
  2179. /* Conversion factor = 0 , 40MHz clock */
  2180. DC390_write8(Clk_Factor, CLK_FREQ_40MHZ);
  2181. /* NOP cmd - clear command register */
  2182. DC390_write8(ScsiCmd, NOP_CMD);
  2183. /* Enable Feature and SCSI-2 */
  2184. DC390_write8(CtrlReg2, EN_FEATURE+EN_SCSI2_CMD);
  2185. /* Fast clock */
  2186. DC390_write8(CtrlReg3, FAST_CLK);
  2187. /* Negation */
  2188. DC390_write8(CtrlReg4, pACB->glitch_cfg | /* glitch eater */
  2189. (dc390_eepromBuf[index][EE_MODE2] & ACTIVE_NEGATION) ?
  2190. NEGATE_REQACKDATA : 0);
  2191. /* Clear Transfer Count High: ID */
  2192. DC390_write8(CtcReg_High, 0);
  2193. DC390_write8(DMA_Cmd, DMA_IDLE_CMD);
  2194. DC390_write8(ScsiCmd, CLEAR_FIFO_CMD);
  2195. DC390_write32(DMA_ScsiBusCtrl, EN_INT_ON_PCI_ABORT);
  2196. dstate = DC390_read8(DMA_Status);
  2197. DC390_write8(DMA_Status, dstate);
  2198. }
  2199. static int __devinit dc390_probe_one(struct pci_dev *pdev,
  2200. const struct pci_device_id *id)
  2201. {
  2202. struct dc390_acb *pACB;
  2203. struct Scsi_Host *shost;
  2204. unsigned long io_port;
  2205. int error = -ENODEV, i;
  2206. if (pci_enable_device(pdev))
  2207. goto out;
  2208. pci_set_master(pdev);
  2209. error = -ENOMEM;
  2210. shost = scsi_host_alloc(&driver_template, sizeof(struct dc390_acb));
  2211. if (!shost)
  2212. goto out_disable_device;
  2213. pACB = (struct dc390_acb *)shost->hostdata;
  2214. memset(pACB, 0, sizeof(struct dc390_acb));
  2215. dc390_check_eeprom(pdev, dc390_adapterCnt);
  2216. dc390_eeprom_override(dc390_adapterCnt);
  2217. io_port = pci_resource_start(pdev, 0);
  2218. shost->this_id = dc390_eepromBuf[dc390_adapterCnt][EE_ADAPT_SCSI_ID];
  2219. shost->io_port = io_port;
  2220. shost->n_io_port = 0x80;
  2221. shost->irq = pdev->irq;
  2222. shost->base = io_port;
  2223. shost->unique_id = io_port;
  2224. shost->last_reset = jiffies;
  2225. pACB->pScsiHost = shost;
  2226. pACB->IOPortBase = (u16) io_port;
  2227. pACB->IRQLevel = pdev->irq;
  2228. shost->max_id = 8;
  2229. if (shost->max_id - 1 ==
  2230. dc390_eepromBuf[dc390_adapterCnt][EE_ADAPT_SCSI_ID])
  2231. shost->max_id--;
  2232. if (dc390_eepromBuf[dc390_adapterCnt][EE_MODE2] & LUN_CHECK)
  2233. shost->max_lun = 8;
  2234. else
  2235. shost->max_lun = 1;
  2236. pACB->pFreeSRB = pACB->SRB_array;
  2237. pACB->SRBCount = MAX_SRB_CNT;
  2238. pACB->AdapterIndex = dc390_adapterCnt;
  2239. pACB->TagMaxNum =
  2240. 2 << dc390_eepromBuf[dc390_adapterCnt][EE_TAG_CMD_NUM];
  2241. pACB->Gmode2 = dc390_eepromBuf[dc390_adapterCnt][EE_MODE2];
  2242. for (i = 0; i < pACB->SRBCount-1; i++)
  2243. pACB->SRB_array[i].pNextSRB = &pACB->SRB_array[i+1];
  2244. pACB->SRB_array[pACB->SRBCount-1].pNextSRB = NULL;
  2245. pACB->pTmpSRB = &pACB->TmpSRB;
  2246. pACB->sel_timeout = SEL_TIMEOUT;
  2247. pACB->glitch_cfg = EATER_25NS;
  2248. pACB->pdev = pdev;
  2249. if (!request_region(io_port, shost->n_io_port, "tmscsim")) {
  2250. printk(KERN_ERR "DC390: register IO ports error!\n");
  2251. goto out_host_put;
  2252. }
  2253. /* Reset Pending INT */
  2254. DC390_read8_(INT_Status, io_port);
  2255. if (request_irq(pdev->irq, do_DC390_Interrupt, SA_SHIRQ,
  2256. "tmscsim", pACB)) {
  2257. printk(KERN_ERR "DC390: register IRQ error!\n");
  2258. goto out_release_region;
  2259. }
  2260. dc390_init_hw(pACB, dc390_adapterCnt);
  2261. dc390_adapterCnt++;
  2262. pci_set_drvdata(pdev, shost);
  2263. error = scsi_add_host(shost, &pdev->dev);
  2264. if (error)
  2265. goto out_free_irq;
  2266. scsi_scan_host(shost);
  2267. return 0;
  2268. out_free_irq:
  2269. free_irq(pdev->irq, pACB);
  2270. out_release_region:
  2271. release_region(io_port, shost->n_io_port);
  2272. out_host_put:
  2273. scsi_host_put(shost);
  2274. out_disable_device:
  2275. pci_disable_device(pdev);
  2276. out:
  2277. return error;
  2278. }
  2279. /**
  2280. * dc390_remove_one - Called to remove a single instance of the adapter.
  2281. *
  2282. * @dev: The PCI device to remove.
  2283. */
  2284. static void __devexit dc390_remove_one(struct pci_dev *dev)
  2285. {
  2286. struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
  2287. unsigned long iflags;
  2288. struct dc390_acb* pACB = (struct dc390_acb*) scsi_host->hostdata;
  2289. u8 bval;
  2290. scsi_remove_host(scsi_host);
  2291. spin_lock_irqsave(scsi_host->host_lock, iflags);
  2292. pACB->ACBFlag = RESET_DEV;
  2293. bval = DC390_read8(CtrlReg1) | DIS_INT_ON_SCSI_RST;
  2294. DC390_write8 (CtrlReg1, bval); /* disable interrupt */
  2295. if (pACB->Gmode2 & RST_SCSI_BUS)
  2296. dc390_ResetSCSIBus(pACB);
  2297. spin_unlock_irqrestore(scsi_host->host_lock, iflags);
  2298. free_irq(scsi_host->irq, pACB);
  2299. release_region(scsi_host->io_port, scsi_host->n_io_port);
  2300. pci_disable_device(dev);
  2301. scsi_host_put(scsi_host);
  2302. pci_set_drvdata(dev, NULL);
  2303. }
  2304. static struct pci_device_id tmscsim_pci_tbl[] = {
  2305. { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD53C974,
  2306. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  2307. { }
  2308. };
  2309. MODULE_DEVICE_TABLE(pci, tmscsim_pci_tbl);
  2310. static struct pci_driver dc390_driver = {
  2311. .name = "tmscsim",
  2312. .id_table = tmscsim_pci_tbl,
  2313. .probe = dc390_probe_one,
  2314. .remove = __devexit_p(dc390_remove_one),
  2315. };
  2316. static int __init dc390_module_init(void)
  2317. {
  2318. if (tmscsim[0] == -1 || tmscsim[0] > 15) {
  2319. tmscsim[0] = 7;
  2320. tmscsim[1] = 4;
  2321. tmscsim[2] = PARITY_CHK_ | TAG_QUEUEING_;
  2322. tmscsim[3] = MORE2_DRV | GREATER_1G | RST_SCSI_BUS | ACTIVE_NEGATION;
  2323. tmscsim[4] = 2;
  2324. tmscsim[5] = 10;
  2325. printk (KERN_INFO "DC390: Using safe settings.\n");
  2326. }
  2327. return pci_module_init(&dc390_driver);
  2328. }
  2329. static void __exit dc390_module_exit(void)
  2330. {
  2331. pci_unregister_driver(&dc390_driver);
  2332. }
  2333. module_init(dc390_module_init);
  2334. module_exit(dc390_module_exit);
  2335. #ifndef MODULE
  2336. static int __init dc390_setup (char *str)
  2337. {
  2338. int ints[8],i, im;
  2339. get_options(str, ARRAY_SIZE(ints), ints);
  2340. im = ints[0];
  2341. if (im > 6) {
  2342. printk (KERN_NOTICE "DC390: ignore extra params!\n");
  2343. im = 6;
  2344. }
  2345. for (i = 0; i < im; i++)
  2346. tmscsim[i] = ints[i+1];
  2347. /* dc390_checkparams (); */
  2348. return 1;
  2349. }
  2350. __setup("tmscsim=", dc390_setup);
  2351. #endif