tmscsim.c 77 KB

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