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