tmscsim.c 76 KB

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