tmscsim.c 79 KB

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