tmscsim.c 80 KB

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