dc395x.c 135 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693
  1. /*
  2. * dc395x.c
  3. *
  4. * Device Driver for Tekram DC395(U/UW/F), DC315(U)
  5. * PCI SCSI Bus Master Host Adapter
  6. * (SCSI chip set used Tekram ASIC TRM-S1040)
  7. *
  8. * Authors:
  9. * C.L. Huang <[email protected]>
  10. * Erich Chen <[email protected]>
  11. * (C) Copyright 1995-1999 Tekram Technology Co., Ltd.
  12. *
  13. * Kurt Garloff <[email protected]>
  14. * (C) 1999-2000 Kurt Garloff
  15. *
  16. * Oliver Neukum <[email protected]>
  17. * Ali Akcaagac <[email protected]>
  18. * Jamie Lenehan <[email protected]>
  19. * (C) 2003
  20. *
  21. * License: GNU GPL
  22. *
  23. *************************************************************************
  24. *
  25. * Redistribution and use in source and binary forms, with or without
  26. * modification, are permitted provided that the following conditions
  27. * are met:
  28. * 1. Redistributions of source code must retain the above copyright
  29. * notice, this list of conditions and the following disclaimer.
  30. * 2. Redistributions in binary form must reproduce the above copyright
  31. * notice, this list of conditions and the following disclaimer in the
  32. * documentation and/or other materials provided with the distribution.
  33. * 3. The name of the author may not be used to endorse or promote products
  34. * derived from this software without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  37. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  39. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  42. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  43. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  45. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ************************************************************************
  48. */
  49. #include <linux/module.h>
  50. #include <linux/moduleparam.h>
  51. #include <linux/delay.h>
  52. #include <linux/ctype.h>
  53. #include <linux/blkdev.h>
  54. #include <linux/interrupt.h>
  55. #include <linux/init.h>
  56. #include <linux/spinlock.h>
  57. #include <linux/pci.h>
  58. #include <linux/list.h>
  59. #include <linux/vmalloc.h>
  60. #include <linux/slab.h>
  61. #include <asm/io.h>
  62. #include <scsi/scsi.h>
  63. #include <scsi/scsi_cmnd.h>
  64. #include <scsi/scsi_device.h>
  65. #include <scsi/scsi_host.h>
  66. #include <scsi/scsi_transport_spi.h>
  67. #include "dc395x.h"
  68. #define DC395X_NAME "dc395x"
  69. #define DC395X_BANNER "Tekram DC395(U/UW/F), DC315(U) - ASIC TRM-S1040"
  70. #define DC395X_VERSION "v2.05, 2004/03/08"
  71. /*---------------------------------------------------------------------------
  72. Features
  73. ---------------------------------------------------------------------------*/
  74. /*
  75. * Set to disable parts of the driver
  76. */
  77. /*#define DC395x_NO_DISCONNECT*/
  78. /*#define DC395x_NO_TAGQ*/
  79. /*#define DC395x_NO_SYNC*/
  80. /*#define DC395x_NO_WIDE*/
  81. /*---------------------------------------------------------------------------
  82. Debugging
  83. ---------------------------------------------------------------------------*/
  84. /*
  85. * Types of debugging that can be enabled and disabled
  86. */
  87. #define DBG_KG 0x0001
  88. #define DBG_0 0x0002
  89. #define DBG_1 0x0004
  90. #define DBG_SG 0x0020
  91. #define DBG_FIFO 0x0040
  92. #define DBG_PIO 0x0080
  93. /*
  94. * Set set of things to output debugging for.
  95. * Undefine to remove all debugging
  96. */
  97. /*#define DEBUG_MASK (DBG_0|DBG_1|DBG_SG|DBG_FIFO|DBG_PIO)*/
  98. /*#define DEBUG_MASK DBG_0*/
  99. /*
  100. * Output a kernel mesage at the specified level and append the
  101. * driver name and a ": " to the start of the message
  102. */
  103. #define dprintkl(level, format, arg...) \
  104. printk(level DC395X_NAME ": " format , ## arg)
  105. #ifdef DEBUG_MASK
  106. /*
  107. * print a debug message - this is formated with KERN_DEBUG, then the
  108. * driver name followed by a ": " and then the message is output.
  109. * This also checks that the specified debug level is enabled before
  110. * outputing the message
  111. */
  112. #define dprintkdbg(type, format, arg...) \
  113. do { \
  114. if ((type) & (DEBUG_MASK)) \
  115. dprintkl(KERN_DEBUG , format , ## arg); \
  116. } while (0)
  117. /*
  118. * Check if the specified type of debugging is enabled
  119. */
  120. #define debug_enabled(type) ((DEBUG_MASK) & (type))
  121. #else
  122. /*
  123. * No debugging. Do nothing
  124. */
  125. #define dprintkdbg(type, format, arg...) \
  126. do {} while (0)
  127. #define debug_enabled(type) (0)
  128. #endif
  129. #ifndef PCI_VENDOR_ID_TEKRAM
  130. #define PCI_VENDOR_ID_TEKRAM 0x1DE1 /* Vendor ID */
  131. #endif
  132. #ifndef PCI_DEVICE_ID_TEKRAM_TRMS1040
  133. #define PCI_DEVICE_ID_TEKRAM_TRMS1040 0x0391 /* Device ID */
  134. #endif
  135. #define DC395x_LOCK_IO(dev,flags) spin_lock_irqsave(((struct Scsi_Host *)dev)->host_lock, flags)
  136. #define DC395x_UNLOCK_IO(dev,flags) spin_unlock_irqrestore(((struct Scsi_Host *)dev)->host_lock, flags)
  137. #define DC395x_read8(acb,address) (u8)(inb(acb->io_port_base + (address)))
  138. #define DC395x_read16(acb,address) (u16)(inw(acb->io_port_base + (address)))
  139. #define DC395x_read32(acb,address) (u32)(inl(acb->io_port_base + (address)))
  140. #define DC395x_write8(acb,address,value) outb((value), acb->io_port_base + (address))
  141. #define DC395x_write16(acb,address,value) outw((value), acb->io_port_base + (address))
  142. #define DC395x_write32(acb,address,value) outl((value), acb->io_port_base + (address))
  143. #define TAG_NONE 255
  144. /*
  145. * srb->segement_x is the hw sg list. It is always allocated as a
  146. * DC395x_MAX_SG_LISTENTRY entries in a linear block which does not
  147. * cross a page boundy.
  148. */
  149. #define SEGMENTX_LEN (sizeof(struct SGentry)*DC395x_MAX_SG_LISTENTRY)
  150. struct SGentry {
  151. u32 address; /* bus! address */
  152. u32 length;
  153. };
  154. /* The SEEPROM structure for TRM_S1040 */
  155. struct NVRamTarget {
  156. u8 cfg0; /* Target configuration byte 0 */
  157. u8 period; /* Target period */
  158. u8 cfg2; /* Target configuration byte 2 */
  159. u8 cfg3; /* Target configuration byte 3 */
  160. };
  161. struct NvRamType {
  162. u8 sub_vendor_id[2]; /* 0,1 Sub Vendor ID */
  163. u8 sub_sys_id[2]; /* 2,3 Sub System ID */
  164. u8 sub_class; /* 4 Sub Class */
  165. u8 vendor_id[2]; /* 5,6 Vendor ID */
  166. u8 device_id[2]; /* 7,8 Device ID */
  167. u8 reserved; /* 9 Reserved */
  168. struct NVRamTarget target[DC395x_MAX_SCSI_ID];
  169. /** 10,11,12,13
  170. ** 14,15,16,17
  171. ** ....
  172. ** ....
  173. ** 70,71,72,73
  174. */
  175. u8 scsi_id; /* 74 Host Adapter SCSI ID */
  176. u8 channel_cfg; /* 75 Channel configuration */
  177. u8 delay_time; /* 76 Power on delay time */
  178. u8 max_tag; /* 77 Maximum tags */
  179. u8 reserved0; /* 78 */
  180. u8 boot_target; /* 79 */
  181. u8 boot_lun; /* 80 */
  182. u8 reserved1; /* 81 */
  183. u16 reserved2[22]; /* 82,..125 */
  184. u16 cksum; /* 126,127 */
  185. };
  186. struct ScsiReqBlk {
  187. struct list_head list; /* next/prev ptrs for srb lists */
  188. struct DeviceCtlBlk *dcb;
  189. struct scsi_cmnd *cmd;
  190. struct SGentry *segment_x; /* Linear array of hw sg entries (up to 64 entries) */
  191. dma_addr_t sg_bus_addr; /* Bus address of sg list (ie, of segment_x) */
  192. u8 sg_count; /* No of HW sg entries for this request */
  193. u8 sg_index; /* Index of HW sg entry for this request */
  194. size_t total_xfer_length; /* Total number of bytes remaining to be transferred */
  195. size_t request_length; /* Total number of bytes in this request */
  196. /*
  197. * The sense buffer handling function, request_sense, uses
  198. * the first hw sg entry (segment_x[0]) and the transfer
  199. * length (total_xfer_length). While doing this it stores the
  200. * original values into the last sg hw list
  201. * (srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1] and the
  202. * total_xfer_length in xferred. These values are restored in
  203. * pci_unmap_srb_sense. This is the only place xferred is used.
  204. */
  205. size_t xferred; /* Saved copy of total_xfer_length */
  206. u16 state;
  207. u8 msgin_buf[6];
  208. u8 msgout_buf[6];
  209. u8 adapter_status;
  210. u8 target_status;
  211. u8 msg_count;
  212. u8 end_message;
  213. u8 tag_number;
  214. u8 status;
  215. u8 retry_count;
  216. u8 flag;
  217. u8 scsi_phase;
  218. };
  219. struct DeviceCtlBlk {
  220. struct list_head list; /* next/prev ptrs for the dcb list */
  221. struct AdapterCtlBlk *acb;
  222. struct list_head srb_going_list; /* head of going srb list */
  223. struct list_head srb_waiting_list; /* head of waiting srb list */
  224. struct ScsiReqBlk *active_srb;
  225. u32 tag_mask;
  226. u16 max_command;
  227. u8 target_id; /* SCSI Target ID (SCSI Only) */
  228. u8 target_lun; /* SCSI Log. Unit (SCSI Only) */
  229. u8 identify_msg;
  230. u8 dev_mode;
  231. u8 inquiry7; /* To store Inquiry flags */
  232. u8 sync_mode; /* 0:async mode */
  233. u8 min_nego_period; /* for nego. */
  234. u8 sync_period; /* for reg. */
  235. u8 sync_offset; /* for reg. and nego.(low nibble) */
  236. u8 flag;
  237. u8 dev_type;
  238. u8 init_tcq_flag;
  239. };
  240. struct AdapterCtlBlk {
  241. struct Scsi_Host *scsi_host;
  242. unsigned long io_port_base;
  243. unsigned long io_port_len;
  244. struct list_head dcb_list; /* head of going dcb list */
  245. struct DeviceCtlBlk *dcb_run_robin;
  246. struct DeviceCtlBlk *active_dcb;
  247. struct list_head srb_free_list; /* head of free srb list */
  248. struct ScsiReqBlk *tmp_srb;
  249. struct timer_list waiting_timer;
  250. struct timer_list selto_timer;
  251. unsigned long last_reset;
  252. u16 srb_count;
  253. u8 sel_timeout;
  254. unsigned int irq_level;
  255. u8 tag_max_num;
  256. u8 acb_flag;
  257. u8 gmode2;
  258. u8 config;
  259. u8 lun_chk;
  260. u8 scan_devices;
  261. u8 hostid_bit;
  262. u8 dcb_map[DC395x_MAX_SCSI_ID];
  263. struct DeviceCtlBlk *children[DC395x_MAX_SCSI_ID][32];
  264. struct pci_dev *dev;
  265. u8 msg_len;
  266. struct ScsiReqBlk srb_array[DC395x_MAX_SRB_CNT];
  267. struct ScsiReqBlk srb;
  268. struct NvRamType eeprom; /* eeprom settings for this adapter */
  269. };
  270. /*---------------------------------------------------------------------------
  271. Forward declarations
  272. ---------------------------------------------------------------------------*/
  273. static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  274. u16 *pscsi_status);
  275. static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  276. u16 *pscsi_status);
  277. static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  278. u16 *pscsi_status);
  279. static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  280. u16 *pscsi_status);
  281. static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  282. u16 *pscsi_status);
  283. static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  284. u16 *pscsi_status);
  285. static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  286. u16 *pscsi_status);
  287. static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  288. u16 *pscsi_status);
  289. static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  290. u16 *pscsi_status);
  291. static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  292. u16 *pscsi_status);
  293. static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  294. u16 *pscsi_status);
  295. static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  296. u16 *pscsi_status);
  297. static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  298. u16 *pscsi_status);
  299. static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  300. u16 *pscsi_status);
  301. static void set_basic_config(struct AdapterCtlBlk *acb);
  302. static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
  303. struct ScsiReqBlk *srb);
  304. static void reset_scsi_bus(struct AdapterCtlBlk *acb);
  305. static void data_io_transfer(struct AdapterCtlBlk *acb,
  306. struct ScsiReqBlk *srb, u16 io_dir);
  307. static void disconnect(struct AdapterCtlBlk *acb);
  308. static void reselect(struct AdapterCtlBlk *acb);
  309. static u8 start_scsi(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
  310. struct ScsiReqBlk *srb);
  311. static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
  312. struct ScsiReqBlk *srb);
  313. static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
  314. struct ScsiReqBlk *srb);
  315. static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_code,
  316. struct scsi_cmnd *cmd, u8 force);
  317. static void scsi_reset_detect(struct AdapterCtlBlk *acb);
  318. static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb);
  319. static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
  320. struct ScsiReqBlk *srb);
  321. static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
  322. struct ScsiReqBlk *srb);
  323. static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
  324. struct ScsiReqBlk *srb);
  325. static void set_xfer_rate(struct AdapterCtlBlk *acb,
  326. struct DeviceCtlBlk *dcb);
  327. static void waiting_timeout(struct timer_list *t);
  328. /*---------------------------------------------------------------------------
  329. Static Data
  330. ---------------------------------------------------------------------------*/
  331. static u16 current_sync_offset = 0;
  332. static void *dc395x_scsi_phase0[] = {
  333. data_out_phase0,/* phase:0 */
  334. data_in_phase0, /* phase:1 */
  335. command_phase0, /* phase:2 */
  336. status_phase0, /* phase:3 */
  337. nop0, /* phase:4 PH_BUS_FREE .. initial phase */
  338. nop0, /* phase:5 PH_BUS_FREE .. initial phase */
  339. msgout_phase0, /* phase:6 */
  340. msgin_phase0, /* phase:7 */
  341. };
  342. static void *dc395x_scsi_phase1[] = {
  343. data_out_phase1,/* phase:0 */
  344. data_in_phase1, /* phase:1 */
  345. command_phase1, /* phase:2 */
  346. status_phase1, /* phase:3 */
  347. nop1, /* phase:4 PH_BUS_FREE .. initial phase */
  348. nop1, /* phase:5 PH_BUS_FREE .. initial phase */
  349. msgout_phase1, /* phase:6 */
  350. msgin_phase1, /* phase:7 */
  351. };
  352. /*
  353. *Fast20: 000 50ns, 20.0 MHz
  354. * 001 75ns, 13.3 MHz
  355. * 010 100ns, 10.0 MHz
  356. * 011 125ns, 8.0 MHz
  357. * 100 150ns, 6.6 MHz
  358. * 101 175ns, 5.7 MHz
  359. * 110 200ns, 5.0 MHz
  360. * 111 250ns, 4.0 MHz
  361. *
  362. *Fast40(LVDS): 000 25ns, 40.0 MHz
  363. * 001 50ns, 20.0 MHz
  364. * 010 75ns, 13.3 MHz
  365. * 011 100ns, 10.0 MHz
  366. * 100 125ns, 8.0 MHz
  367. * 101 150ns, 6.6 MHz
  368. * 110 175ns, 5.7 MHz
  369. * 111 200ns, 5.0 MHz
  370. */
  371. /*static u8 clock_period[] = {12,19,25,31,37,44,50,62};*/
  372. /* real period:48ns,76ns,100ns,124ns,148ns,176ns,200ns,248ns */
  373. static u8 clock_period[] = { 12, 18, 25, 31, 37, 43, 50, 62 };
  374. static u16 clock_speed[] = { 200, 133, 100, 80, 67, 58, 50, 40 };
  375. /*---------------------------------------------------------------------------
  376. Configuration
  377. ---------------------------------------------------------------------------*/
  378. /*
  379. * Module/boot parameters currently effect *all* instances of the
  380. * card in the system.
  381. */
  382. /*
  383. * Command line parameters are stored in a structure below.
  384. * These are the index's into the structure for the various
  385. * command line options.
  386. */
  387. #define CFG_ADAPTER_ID 0
  388. #define CFG_MAX_SPEED 1
  389. #define CFG_DEV_MODE 2
  390. #define CFG_ADAPTER_MODE 3
  391. #define CFG_TAGS 4
  392. #define CFG_RESET_DELAY 5
  393. #define CFG_NUM 6 /* number of configuration items */
  394. /*
  395. * Value used to indicate that a command line override
  396. * hasn't been used to modify the value.
  397. */
  398. #define CFG_PARAM_UNSET -1
  399. /*
  400. * Hold command line parameters.
  401. */
  402. struct ParameterData {
  403. int value; /* value of this setting */
  404. int min; /* minimum value */
  405. int max; /* maximum value */
  406. int def; /* default value */
  407. int safe; /* safe value */
  408. };
  409. static struct ParameterData cfg_data[] = {
  410. { /* adapter id */
  411. CFG_PARAM_UNSET,
  412. 0,
  413. 15,
  414. 7,
  415. 7
  416. },
  417. { /* max speed */
  418. CFG_PARAM_UNSET,
  419. 0,
  420. 7,
  421. 1, /* 13.3Mhz */
  422. 4, /* 6.7Hmz */
  423. },
  424. { /* dev mode */
  425. CFG_PARAM_UNSET,
  426. 0,
  427. 0x3f,
  428. NTC_DO_PARITY_CHK | NTC_DO_DISCONNECT | NTC_DO_SYNC_NEGO |
  429. NTC_DO_WIDE_NEGO | NTC_DO_TAG_QUEUEING |
  430. NTC_DO_SEND_START,
  431. NTC_DO_PARITY_CHK | NTC_DO_SEND_START
  432. },
  433. { /* adapter mode */
  434. CFG_PARAM_UNSET,
  435. 0,
  436. 0x2f,
  437. NAC_SCANLUN |
  438. NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET
  439. /*| NAC_ACTIVE_NEG*/,
  440. NAC_GT2DRIVES | NAC_GREATER_1G | NAC_POWERON_SCSI_RESET | 0x08
  441. },
  442. { /* tags */
  443. CFG_PARAM_UNSET,
  444. 0,
  445. 5,
  446. 3, /* 16 tags (??) */
  447. 2,
  448. },
  449. { /* reset delay */
  450. CFG_PARAM_UNSET,
  451. 0,
  452. 180,
  453. 1, /* 1 second */
  454. 10, /* 10 seconds */
  455. }
  456. };
  457. /*
  458. * Safe settings. If set to zero the BIOS/default values with
  459. * command line overrides will be used. If set to 1 then safe and
  460. * slow settings will be used.
  461. */
  462. static bool use_safe_settings = 0;
  463. module_param_named(safe, use_safe_settings, bool, 0);
  464. MODULE_PARM_DESC(safe, "Use safe and slow settings only. Default: false");
  465. module_param_named(adapter_id, cfg_data[CFG_ADAPTER_ID].value, int, 0);
  466. MODULE_PARM_DESC(adapter_id, "Adapter SCSI ID. Default 7 (0-15)");
  467. module_param_named(max_speed, cfg_data[CFG_MAX_SPEED].value, int, 0);
  468. MODULE_PARM_DESC(max_speed, "Maximum bus speed. Default 1 (0-7) Speeds: 0=20, 1=13.3, 2=10, 3=8, 4=6.7, 5=5.8, 6=5, 7=4 Mhz");
  469. module_param_named(dev_mode, cfg_data[CFG_DEV_MODE].value, int, 0);
  470. MODULE_PARM_DESC(dev_mode, "Device mode.");
  471. module_param_named(adapter_mode, cfg_data[CFG_ADAPTER_MODE].value, int, 0);
  472. MODULE_PARM_DESC(adapter_mode, "Adapter mode.");
  473. module_param_named(tags, cfg_data[CFG_TAGS].value, int, 0);
  474. MODULE_PARM_DESC(tags, "Number of tags (1<<x). Default 3 (0-5)");
  475. module_param_named(reset_delay, cfg_data[CFG_RESET_DELAY].value, int, 0);
  476. MODULE_PARM_DESC(reset_delay, "Reset delay in seconds. Default 1 (0-180)");
  477. /**
  478. * set_safe_settings - if the use_safe_settings option is set then
  479. * set all values to the safe and slow values.
  480. **/
  481. static void set_safe_settings(void)
  482. {
  483. if (use_safe_settings)
  484. {
  485. int i;
  486. dprintkl(KERN_INFO, "Using safe settings.\n");
  487. for (i = 0; i < CFG_NUM; i++)
  488. {
  489. cfg_data[i].value = cfg_data[i].safe;
  490. }
  491. }
  492. }
  493. /**
  494. * fix_settings - reset any boot parameters which are out of range
  495. * back to the default values.
  496. **/
  497. static void fix_settings(void)
  498. {
  499. int i;
  500. dprintkdbg(DBG_1,
  501. "setup: AdapterId=%08x MaxSpeed=%08x DevMode=%08x "
  502. "AdapterMode=%08x Tags=%08x ResetDelay=%08x\n",
  503. cfg_data[CFG_ADAPTER_ID].value,
  504. cfg_data[CFG_MAX_SPEED].value,
  505. cfg_data[CFG_DEV_MODE].value,
  506. cfg_data[CFG_ADAPTER_MODE].value,
  507. cfg_data[CFG_TAGS].value,
  508. cfg_data[CFG_RESET_DELAY].value);
  509. for (i = 0; i < CFG_NUM; i++)
  510. {
  511. if (cfg_data[i].value < cfg_data[i].min
  512. || cfg_data[i].value > cfg_data[i].max)
  513. cfg_data[i].value = cfg_data[i].def;
  514. }
  515. }
  516. /*
  517. * Mapping from the eeprom delay index value (index into this array)
  518. * to the number of actual seconds that the delay should be for.
  519. */
  520. static char eeprom_index_to_delay_map[] =
  521. { 1, 3, 5, 10, 16, 30, 60, 120 };
  522. /**
  523. * eeprom_index_to_delay - Take the eeprom delay setting and convert it
  524. * into a number of seconds.
  525. *
  526. * @eeprom: The eeprom structure in which we find the delay index to map.
  527. **/
  528. static void eeprom_index_to_delay(struct NvRamType *eeprom)
  529. {
  530. eeprom->delay_time = eeprom_index_to_delay_map[eeprom->delay_time];
  531. }
  532. /**
  533. * delay_to_eeprom_index - Take a delay in seconds and return the
  534. * closest eeprom index which will delay for at least that amount of
  535. * seconds.
  536. *
  537. * @delay: The delay, in seconds, to find the eeprom index for.
  538. **/
  539. static int delay_to_eeprom_index(int delay)
  540. {
  541. u8 idx = 0;
  542. while (idx < 7 && eeprom_index_to_delay_map[idx] < delay)
  543. idx++;
  544. return idx;
  545. }
  546. /**
  547. * eeprom_override - Override the eeprom settings, in the provided
  548. * eeprom structure, with values that have been set on the command
  549. * line.
  550. *
  551. * @eeprom: The eeprom data to override with command line options.
  552. **/
  553. static void eeprom_override(struct NvRamType *eeprom)
  554. {
  555. u8 id;
  556. /* Adapter Settings */
  557. if (cfg_data[CFG_ADAPTER_ID].value != CFG_PARAM_UNSET)
  558. eeprom->scsi_id = (u8)cfg_data[CFG_ADAPTER_ID].value;
  559. if (cfg_data[CFG_ADAPTER_MODE].value != CFG_PARAM_UNSET)
  560. eeprom->channel_cfg = (u8)cfg_data[CFG_ADAPTER_MODE].value;
  561. if (cfg_data[CFG_RESET_DELAY].value != CFG_PARAM_UNSET)
  562. eeprom->delay_time = delay_to_eeprom_index(
  563. cfg_data[CFG_RESET_DELAY].value);
  564. if (cfg_data[CFG_TAGS].value != CFG_PARAM_UNSET)
  565. eeprom->max_tag = (u8)cfg_data[CFG_TAGS].value;
  566. /* Device Settings */
  567. for (id = 0; id < DC395x_MAX_SCSI_ID; id++) {
  568. if (cfg_data[CFG_DEV_MODE].value != CFG_PARAM_UNSET)
  569. eeprom->target[id].cfg0 =
  570. (u8)cfg_data[CFG_DEV_MODE].value;
  571. if (cfg_data[CFG_MAX_SPEED].value != CFG_PARAM_UNSET)
  572. eeprom->target[id].period =
  573. (u8)cfg_data[CFG_MAX_SPEED].value;
  574. }
  575. }
  576. /*---------------------------------------------------------------------------
  577. ---------------------------------------------------------------------------*/
  578. static unsigned int list_size(struct list_head *head)
  579. {
  580. unsigned int count = 0;
  581. struct list_head *pos;
  582. list_for_each(pos, head)
  583. count++;
  584. return count;
  585. }
  586. static struct DeviceCtlBlk *dcb_get_next(struct list_head *head,
  587. struct DeviceCtlBlk *pos)
  588. {
  589. int use_next = 0;
  590. struct DeviceCtlBlk* next = NULL;
  591. struct DeviceCtlBlk* i;
  592. if (list_empty(head))
  593. return NULL;
  594. /* find supplied dcb and then select the next one */
  595. list_for_each_entry(i, head, list)
  596. if (use_next) {
  597. next = i;
  598. break;
  599. } else if (i == pos) {
  600. use_next = 1;
  601. }
  602. /* if no next one take the head one (ie, wraparound) */
  603. if (!next)
  604. list_for_each_entry(i, head, list) {
  605. next = i;
  606. break;
  607. }
  608. return next;
  609. }
  610. static void free_tag(struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
  611. {
  612. if (srb->tag_number < 255) {
  613. dcb->tag_mask &= ~(1 << srb->tag_number); /* free tag mask */
  614. srb->tag_number = 255;
  615. }
  616. }
  617. /* Find cmd in SRB list */
  618. static inline struct ScsiReqBlk *find_cmd(struct scsi_cmnd *cmd,
  619. struct list_head *head)
  620. {
  621. struct ScsiReqBlk *i;
  622. list_for_each_entry(i, head, list)
  623. if (i->cmd == cmd)
  624. return i;
  625. return NULL;
  626. }
  627. /* Sets the timer to wake us up */
  628. static void waiting_set_timer(struct AdapterCtlBlk *acb, unsigned long to)
  629. {
  630. if (timer_pending(&acb->waiting_timer))
  631. return;
  632. if (time_before(jiffies + to, acb->last_reset - HZ / 2))
  633. acb->waiting_timer.expires =
  634. acb->last_reset - HZ / 2 + 1;
  635. else
  636. acb->waiting_timer.expires = jiffies + to + 1;
  637. add_timer(&acb->waiting_timer);
  638. }
  639. /* Send the next command from the waiting list to the bus */
  640. static void waiting_process_next(struct AdapterCtlBlk *acb)
  641. {
  642. struct DeviceCtlBlk *start = NULL;
  643. struct DeviceCtlBlk *pos;
  644. struct DeviceCtlBlk *dcb;
  645. struct ScsiReqBlk *srb;
  646. struct list_head *dcb_list_head = &acb->dcb_list;
  647. if (acb->active_dcb
  648. || (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV)))
  649. return;
  650. if (timer_pending(&acb->waiting_timer))
  651. del_timer(&acb->waiting_timer);
  652. if (list_empty(dcb_list_head))
  653. return;
  654. /*
  655. * Find the starting dcb. Need to find it again in the list
  656. * since the list may have changed since we set the ptr to it
  657. */
  658. list_for_each_entry(dcb, dcb_list_head, list)
  659. if (dcb == acb->dcb_run_robin) {
  660. start = dcb;
  661. break;
  662. }
  663. if (!start) {
  664. /* This can happen! */
  665. start = list_entry(dcb_list_head->next, typeof(*start), list);
  666. acb->dcb_run_robin = start;
  667. }
  668. /*
  669. * Loop over the dcb, but we start somewhere (potentially) in
  670. * the middle of the loop so we need to manully do this.
  671. */
  672. pos = start;
  673. do {
  674. struct list_head *waiting_list_head = &pos->srb_waiting_list;
  675. /* Make sure, the next another device gets scheduled ... */
  676. acb->dcb_run_robin = dcb_get_next(dcb_list_head,
  677. acb->dcb_run_robin);
  678. if (list_empty(waiting_list_head) ||
  679. pos->max_command <= list_size(&pos->srb_going_list)) {
  680. /* move to next dcb */
  681. pos = dcb_get_next(dcb_list_head, pos);
  682. } else {
  683. srb = list_entry(waiting_list_head->next,
  684. struct ScsiReqBlk, list);
  685. /* Try to send to the bus */
  686. if (!start_scsi(acb, pos, srb))
  687. list_move(&srb->list, &pos->srb_going_list);
  688. else
  689. waiting_set_timer(acb, HZ/50);
  690. break;
  691. }
  692. } while (pos != start);
  693. }
  694. /* Wake up waiting queue */
  695. static void waiting_timeout(struct timer_list *t)
  696. {
  697. unsigned long flags;
  698. struct AdapterCtlBlk *acb = from_timer(acb, t, waiting_timer);
  699. dprintkdbg(DBG_1,
  700. "waiting_timeout: Queue woken up by timer. acb=%p\n", acb);
  701. DC395x_LOCK_IO(acb->scsi_host, flags);
  702. waiting_process_next(acb);
  703. DC395x_UNLOCK_IO(acb->scsi_host, flags);
  704. }
  705. /* Get the DCB for a given ID/LUN combination */
  706. static struct DeviceCtlBlk *find_dcb(struct AdapterCtlBlk *acb, u8 id, u8 lun)
  707. {
  708. return acb->children[id][lun];
  709. }
  710. /* Send SCSI Request Block (srb) to adapter (acb) */
  711. static void send_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
  712. {
  713. struct DeviceCtlBlk *dcb = srb->dcb;
  714. if (dcb->max_command <= list_size(&dcb->srb_going_list) ||
  715. acb->active_dcb ||
  716. (acb->acb_flag & (RESET_DETECT + RESET_DONE + RESET_DEV))) {
  717. list_add_tail(&srb->list, &dcb->srb_waiting_list);
  718. waiting_process_next(acb);
  719. return;
  720. }
  721. if (!start_scsi(acb, dcb, srb)) {
  722. list_add_tail(&srb->list, &dcb->srb_going_list);
  723. } else {
  724. list_add(&srb->list, &dcb->srb_waiting_list);
  725. waiting_set_timer(acb, HZ / 50);
  726. }
  727. }
  728. /* Prepare SRB for being sent to Device DCB w/ command *cmd */
  729. static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
  730. struct ScsiReqBlk *srb)
  731. {
  732. int nseg;
  733. enum dma_data_direction dir = cmd->sc_data_direction;
  734. dprintkdbg(DBG_0, "build_srb: (0x%p) <%02i-%i>\n",
  735. cmd, dcb->target_id, dcb->target_lun);
  736. srb->dcb = dcb;
  737. srb->cmd = cmd;
  738. srb->sg_count = 0;
  739. srb->total_xfer_length = 0;
  740. srb->sg_bus_addr = 0;
  741. srb->sg_index = 0;
  742. srb->adapter_status = 0;
  743. srb->target_status = 0;
  744. srb->msg_count = 0;
  745. srb->status = 0;
  746. srb->flag = 0;
  747. srb->state = 0;
  748. srb->retry_count = 0;
  749. srb->tag_number = TAG_NONE;
  750. srb->scsi_phase = PH_BUS_FREE; /* initial phase */
  751. srb->end_message = 0;
  752. nseg = scsi_dma_map(cmd);
  753. BUG_ON(nseg < 0);
  754. if (dir == DMA_NONE || !nseg) {
  755. dprintkdbg(DBG_0,
  756. "build_srb: [0] len=%d buf=%p use_sg=%d !MAP=%08x\n",
  757. cmd->bufflen, scsi_sglist(cmd), scsi_sg_count(cmd),
  758. srb->segment_x[0].address);
  759. } else {
  760. int i;
  761. u32 reqlen = scsi_bufflen(cmd);
  762. struct scatterlist *sg;
  763. struct SGentry *sgp = srb->segment_x;
  764. srb->sg_count = nseg;
  765. dprintkdbg(DBG_0,
  766. "build_srb: [n] len=%d buf=%p use_sg=%d segs=%d\n",
  767. reqlen, scsi_sglist(cmd), scsi_sg_count(cmd),
  768. srb->sg_count);
  769. scsi_for_each_sg(cmd, sg, srb->sg_count, i) {
  770. u32 busaddr = (u32)sg_dma_address(sg);
  771. u32 seglen = (u32)sg->length;
  772. sgp[i].address = busaddr;
  773. sgp[i].length = seglen;
  774. srb->total_xfer_length += seglen;
  775. }
  776. sgp += srb->sg_count - 1;
  777. /*
  778. * adjust last page if too big as it is allocated
  779. * on even page boundaries
  780. */
  781. if (srb->total_xfer_length > reqlen) {
  782. sgp->length -= (srb->total_xfer_length - reqlen);
  783. srb->total_xfer_length = reqlen;
  784. }
  785. /* Fixup for WIDE padding - make sure length is even */
  786. if (dcb->sync_period & WIDE_SYNC &&
  787. srb->total_xfer_length % 2) {
  788. srb->total_xfer_length++;
  789. sgp->length++;
  790. }
  791. srb->sg_bus_addr = dma_map_single(&dcb->acb->dev->dev,
  792. srb->segment_x, SEGMENTX_LEN, DMA_TO_DEVICE);
  793. dprintkdbg(DBG_SG, "build_srb: [n] map sg %p->%08x(%05x)\n",
  794. srb->segment_x, srb->sg_bus_addr, SEGMENTX_LEN);
  795. }
  796. srb->request_length = srb->total_xfer_length;
  797. }
  798. /**
  799. * dc395x_queue_command_lck - queue scsi command passed from the mid
  800. * layer, invoke 'done' on completion
  801. *
  802. * @cmd: pointer to scsi command object
  803. *
  804. * Returns 1 if the adapter (host) is busy, else returns 0. One
  805. * reason for an adapter to be busy is that the number
  806. * of outstanding queued commands is already equal to
  807. * struct Scsi_Host::can_queue .
  808. *
  809. * Required: if struct Scsi_Host::can_queue is ever non-zero
  810. * then this function is required.
  811. *
  812. * Locks: struct Scsi_Host::host_lock held on entry (with "irqsave")
  813. * and is expected to be held on return.
  814. *
  815. */
  816. static int dc395x_queue_command_lck(struct scsi_cmnd *cmd)
  817. {
  818. void (*done)(struct scsi_cmnd *) = scsi_done;
  819. struct DeviceCtlBlk *dcb;
  820. struct ScsiReqBlk *srb;
  821. struct AdapterCtlBlk *acb =
  822. (struct AdapterCtlBlk *)cmd->device->host->hostdata;
  823. dprintkdbg(DBG_0, "queue_command: (0x%p) <%02i-%i> cmnd=0x%02x\n",
  824. cmd, cmd->device->id, (u8)cmd->device->lun, cmd->cmnd[0]);
  825. /* Assume BAD_TARGET; will be cleared later */
  826. set_host_byte(cmd, DID_BAD_TARGET);
  827. /* ignore invalid targets */
  828. if (cmd->device->id >= acb->scsi_host->max_id ||
  829. cmd->device->lun >= acb->scsi_host->max_lun ||
  830. cmd->device->lun >31) {
  831. goto complete;
  832. }
  833. /* does the specified lun on the specified device exist */
  834. if (!(acb->dcb_map[cmd->device->id] & (1 << cmd->device->lun))) {
  835. dprintkl(KERN_INFO, "queue_command: Ignore target <%02i-%i>\n",
  836. cmd->device->id, (u8)cmd->device->lun);
  837. goto complete;
  838. }
  839. /* do we have a DCB for the device */
  840. dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
  841. if (!dcb) {
  842. /* should never happen */
  843. dprintkl(KERN_ERR, "queue_command: No such device <%02i-%i>",
  844. cmd->device->id, (u8)cmd->device->lun);
  845. goto complete;
  846. }
  847. set_host_byte(cmd, DID_OK);
  848. set_status_byte(cmd, SAM_STAT_GOOD);
  849. srb = list_first_entry_or_null(&acb->srb_free_list,
  850. struct ScsiReqBlk, list);
  851. if (!srb) {
  852. /*
  853. * Return 1 since we are unable to queue this command at this
  854. * point in time.
  855. */
  856. dprintkdbg(DBG_0, "queue_command: No free srb's\n");
  857. return 1;
  858. }
  859. list_del(&srb->list);
  860. build_srb(cmd, dcb, srb);
  861. if (!list_empty(&dcb->srb_waiting_list)) {
  862. /* append to waiting queue */
  863. list_add_tail(&srb->list, &dcb->srb_waiting_list);
  864. waiting_process_next(acb);
  865. } else {
  866. /* process immediately */
  867. send_srb(acb, srb);
  868. }
  869. dprintkdbg(DBG_1, "queue_command: (0x%p) done\n", cmd);
  870. return 0;
  871. complete:
  872. /*
  873. * Complete the command immediatey, and then return 0 to
  874. * indicate that we have handled the command. This is usually
  875. * done when the commad is for things like non existent
  876. * devices.
  877. */
  878. done(cmd);
  879. return 0;
  880. }
  881. static DEF_SCSI_QCMD(dc395x_queue_command)
  882. static void dump_register_info(struct AdapterCtlBlk *acb,
  883. struct DeviceCtlBlk *dcb, struct ScsiReqBlk *srb)
  884. {
  885. u16 pstat;
  886. struct pci_dev *dev = acb->dev;
  887. pci_read_config_word(dev, PCI_STATUS, &pstat);
  888. if (!dcb)
  889. dcb = acb->active_dcb;
  890. if (!srb && dcb)
  891. srb = dcb->active_srb;
  892. if (srb) {
  893. if (!srb->cmd)
  894. dprintkl(KERN_INFO, "dump: srb=%p cmd=%p OOOPS!\n",
  895. srb, srb->cmd);
  896. else
  897. dprintkl(KERN_INFO, "dump: srb=%p cmd=%p "
  898. "cmnd=0x%02x <%02i-%i>\n",
  899. srb, srb->cmd,
  900. srb->cmd->cmnd[0], srb->cmd->device->id,
  901. (u8)srb->cmd->device->lun);
  902. printk(" sglist=%p cnt=%i idx=%i len=%zu\n",
  903. srb->segment_x, srb->sg_count, srb->sg_index,
  904. srb->total_xfer_length);
  905. printk(" state=0x%04x status=0x%02x phase=0x%02x (%sconn.)\n",
  906. srb->state, srb->status, srb->scsi_phase,
  907. (acb->active_dcb) ? "" : "not");
  908. }
  909. dprintkl(KERN_INFO, "dump: SCSI{status=0x%04x fifocnt=0x%02x "
  910. "signals=0x%02x irqstat=0x%02x sync=0x%02x target=0x%02x "
  911. "rselid=0x%02x ctr=0x%08x irqen=0x%02x config=0x%04x "
  912. "config2=0x%02x cmd=0x%02x selto=0x%02x}\n",
  913. DC395x_read16(acb, TRM_S1040_SCSI_STATUS),
  914. DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
  915. DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL),
  916. DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS),
  917. DC395x_read8(acb, TRM_S1040_SCSI_SYNC),
  918. DC395x_read8(acb, TRM_S1040_SCSI_TARGETID),
  919. DC395x_read8(acb, TRM_S1040_SCSI_IDMSG),
  920. DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
  921. DC395x_read8(acb, TRM_S1040_SCSI_INTEN),
  922. DC395x_read16(acb, TRM_S1040_SCSI_CONFIG0),
  923. DC395x_read8(acb, TRM_S1040_SCSI_CONFIG2),
  924. DC395x_read8(acb, TRM_S1040_SCSI_COMMAND),
  925. DC395x_read8(acb, TRM_S1040_SCSI_TIMEOUT));
  926. dprintkl(KERN_INFO, "dump: DMA{cmd=0x%04x fifocnt=0x%02x fstat=0x%02x "
  927. "irqstat=0x%02x irqen=0x%02x cfg=0x%04x tctr=0x%08x "
  928. "ctctr=0x%08x addr=0x%08x:0x%08x}\n",
  929. DC395x_read16(acb, TRM_S1040_DMA_COMMAND),
  930. DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
  931. DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
  932. DC395x_read8(acb, TRM_S1040_DMA_STATUS),
  933. DC395x_read8(acb, TRM_S1040_DMA_INTEN),
  934. DC395x_read16(acb, TRM_S1040_DMA_CONFIG),
  935. DC395x_read32(acb, TRM_S1040_DMA_XCNT),
  936. DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
  937. DC395x_read32(acb, TRM_S1040_DMA_XHIGHADDR),
  938. DC395x_read32(acb, TRM_S1040_DMA_XLOWADDR));
  939. dprintkl(KERN_INFO, "dump: gen{gctrl=0x%02x gstat=0x%02x gtmr=0x%02x} "
  940. "pci{status=0x%04x}\n",
  941. DC395x_read8(acb, TRM_S1040_GEN_CONTROL),
  942. DC395x_read8(acb, TRM_S1040_GEN_STATUS),
  943. DC395x_read8(acb, TRM_S1040_GEN_TIMER),
  944. pstat);
  945. }
  946. static inline void clear_fifo(struct AdapterCtlBlk *acb, char *txt)
  947. {
  948. #if debug_enabled(DBG_FIFO)
  949. u8 lines = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
  950. u8 fifocnt = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
  951. if (!(fifocnt & 0x40))
  952. dprintkdbg(DBG_FIFO,
  953. "clear_fifo: (%i bytes) on phase %02x in %s\n",
  954. fifocnt & 0x3f, lines, txt);
  955. #endif
  956. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRFIFO);
  957. }
  958. static void reset_dev_param(struct AdapterCtlBlk *acb)
  959. {
  960. struct DeviceCtlBlk *dcb;
  961. struct NvRamType *eeprom = &acb->eeprom;
  962. dprintkdbg(DBG_0, "reset_dev_param: acb=%p\n", acb);
  963. list_for_each_entry(dcb, &acb->dcb_list, list) {
  964. u8 period_index;
  965. dcb->sync_mode &= ~(SYNC_NEGO_DONE + WIDE_NEGO_DONE);
  966. dcb->sync_period = 0;
  967. dcb->sync_offset = 0;
  968. dcb->dev_mode = eeprom->target[dcb->target_id].cfg0;
  969. period_index = eeprom->target[dcb->target_id].period & 0x07;
  970. dcb->min_nego_period = clock_period[period_index];
  971. if (!(dcb->dev_mode & NTC_DO_WIDE_NEGO)
  972. || !(acb->config & HCC_WIDE_CARD))
  973. dcb->sync_mode &= ~WIDE_NEGO_ENABLE;
  974. }
  975. }
  976. /*
  977. * perform a hard reset on the SCSI bus
  978. * @cmd - some command for this host (for fetching hooks)
  979. * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
  980. */
  981. static int __dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
  982. {
  983. struct AdapterCtlBlk *acb =
  984. (struct AdapterCtlBlk *)cmd->device->host->hostdata;
  985. dprintkl(KERN_INFO,
  986. "eh_bus_reset: (0%p) target=<%02i-%i> cmd=%p\n",
  987. cmd, cmd->device->id, (u8)cmd->device->lun, cmd);
  988. if (timer_pending(&acb->waiting_timer))
  989. del_timer(&acb->waiting_timer);
  990. /*
  991. * disable interrupt
  992. */
  993. DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
  994. DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
  995. DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
  996. DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
  997. reset_scsi_bus(acb);
  998. udelay(500);
  999. /* We may be in serious trouble. Wait some seconds */
  1000. acb->last_reset =
  1001. jiffies + 3 * HZ / 2 +
  1002. HZ * acb->eeprom.delay_time;
  1003. /*
  1004. * re-enable interrupt
  1005. */
  1006. /* Clear SCSI FIFO */
  1007. DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
  1008. clear_fifo(acb, "eh_bus_reset");
  1009. /* Delete pending IRQ */
  1010. DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
  1011. set_basic_config(acb);
  1012. reset_dev_param(acb);
  1013. doing_srb_done(acb, DID_RESET, cmd, 0);
  1014. acb->active_dcb = NULL;
  1015. acb->acb_flag = 0; /* RESET_DETECT, RESET_DONE ,RESET_DEV */
  1016. waiting_process_next(acb);
  1017. return SUCCESS;
  1018. }
  1019. static int dc395x_eh_bus_reset(struct scsi_cmnd *cmd)
  1020. {
  1021. int rc;
  1022. spin_lock_irq(cmd->device->host->host_lock);
  1023. rc = __dc395x_eh_bus_reset(cmd);
  1024. spin_unlock_irq(cmd->device->host->host_lock);
  1025. return rc;
  1026. }
  1027. /*
  1028. * abort an errant SCSI command
  1029. * @cmd - command to be aborted
  1030. * Returns: SUCCESS (0x2002) on success, else FAILED (0x2003).
  1031. */
  1032. static int dc395x_eh_abort(struct scsi_cmnd *cmd)
  1033. {
  1034. /*
  1035. * Look into our command queues: If it has not been sent already,
  1036. * we remove it and return success. Otherwise fail.
  1037. */
  1038. struct AdapterCtlBlk *acb =
  1039. (struct AdapterCtlBlk *)cmd->device->host->hostdata;
  1040. struct DeviceCtlBlk *dcb;
  1041. struct ScsiReqBlk *srb;
  1042. dprintkl(KERN_INFO, "eh_abort: (0x%p) target=<%02i-%i> cmd=%p\n",
  1043. cmd, cmd->device->id, (u8)cmd->device->lun, cmd);
  1044. dcb = find_dcb(acb, cmd->device->id, cmd->device->lun);
  1045. if (!dcb) {
  1046. dprintkl(KERN_DEBUG, "eh_abort: No such device\n");
  1047. return FAILED;
  1048. }
  1049. srb = find_cmd(cmd, &dcb->srb_waiting_list);
  1050. if (srb) {
  1051. list_del(&srb->list);
  1052. pci_unmap_srb_sense(acb, srb);
  1053. pci_unmap_srb(acb, srb);
  1054. free_tag(dcb, srb);
  1055. list_add_tail(&srb->list, &acb->srb_free_list);
  1056. dprintkl(KERN_DEBUG, "eh_abort: Command was waiting\n");
  1057. set_host_byte(cmd, DID_ABORT);
  1058. return SUCCESS;
  1059. }
  1060. srb = find_cmd(cmd, &dcb->srb_going_list);
  1061. if (srb) {
  1062. dprintkl(KERN_DEBUG, "eh_abort: Command in progress\n");
  1063. /* XXX: Should abort the command here */
  1064. } else {
  1065. dprintkl(KERN_DEBUG, "eh_abort: Command not found\n");
  1066. }
  1067. return FAILED;
  1068. }
  1069. /* SDTR */
  1070. static void build_sdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
  1071. struct ScsiReqBlk *srb)
  1072. {
  1073. u8 *ptr = srb->msgout_buf + srb->msg_count;
  1074. if (srb->msg_count > 1) {
  1075. dprintkl(KERN_INFO,
  1076. "build_sdtr: msgout_buf BUSY (%i: %02x %02x)\n",
  1077. srb->msg_count, srb->msgout_buf[0],
  1078. srb->msgout_buf[1]);
  1079. return;
  1080. }
  1081. if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO)) {
  1082. dcb->sync_offset = 0;
  1083. dcb->min_nego_period = 200 >> 2;
  1084. } else if (dcb->sync_offset == 0)
  1085. dcb->sync_offset = SYNC_NEGO_OFFSET;
  1086. srb->msg_count += spi_populate_sync_msg(ptr, dcb->min_nego_period,
  1087. dcb->sync_offset);
  1088. srb->state |= SRB_DO_SYNC_NEGO;
  1089. }
  1090. /* WDTR */
  1091. static void build_wdtr(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
  1092. struct ScsiReqBlk *srb)
  1093. {
  1094. u8 wide = ((dcb->dev_mode & NTC_DO_WIDE_NEGO) &
  1095. (acb->config & HCC_WIDE_CARD)) ? 1 : 0;
  1096. u8 *ptr = srb->msgout_buf + srb->msg_count;
  1097. if (srb->msg_count > 1) {
  1098. dprintkl(KERN_INFO,
  1099. "build_wdtr: msgout_buf BUSY (%i: %02x %02x)\n",
  1100. srb->msg_count, srb->msgout_buf[0],
  1101. srb->msgout_buf[1]);
  1102. return;
  1103. }
  1104. srb->msg_count += spi_populate_width_msg(ptr, wide);
  1105. srb->state |= SRB_DO_WIDE_NEGO;
  1106. }
  1107. #if 0
  1108. /* Timer to work around chip flaw: When selecting and the bus is
  1109. * busy, we sometimes miss a Selection timeout IRQ */
  1110. void selection_timeout_missed(unsigned long ptr);
  1111. /* Sets the timer to wake us up */
  1112. static void selto_timer(struct AdapterCtlBlk *acb)
  1113. {
  1114. if (timer_pending(&acb->selto_timer))
  1115. return;
  1116. acb->selto_timer.function = selection_timeout_missed;
  1117. acb->selto_timer.data = (unsigned long) acb;
  1118. if (time_before
  1119. (jiffies + HZ, acb->last_reset + HZ / 2))
  1120. acb->selto_timer.expires =
  1121. acb->last_reset + HZ / 2 + 1;
  1122. else
  1123. acb->selto_timer.expires = jiffies + HZ + 1;
  1124. add_timer(&acb->selto_timer);
  1125. }
  1126. void selection_timeout_missed(unsigned long ptr)
  1127. {
  1128. unsigned long flags;
  1129. struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)ptr;
  1130. struct ScsiReqBlk *srb;
  1131. dprintkl(KERN_DEBUG, "Chip forgot to produce SelTO IRQ!\n");
  1132. if (!acb->active_dcb || !acb->active_dcb->active_srb) {
  1133. dprintkl(KERN_DEBUG, "... but no cmd pending? Oops!\n");
  1134. return;
  1135. }
  1136. DC395x_LOCK_IO(acb->scsi_host, flags);
  1137. srb = acb->active_dcb->active_srb;
  1138. disconnect(acb);
  1139. DC395x_UNLOCK_IO(acb->scsi_host, flags);
  1140. }
  1141. #endif
  1142. static u8 start_scsi(struct AdapterCtlBlk* acb, struct DeviceCtlBlk* dcb,
  1143. struct ScsiReqBlk* srb)
  1144. {
  1145. u16 __maybe_unused s_stat2, return_code;
  1146. u8 s_stat, scsicommand, i, identify_message;
  1147. u8 *ptr;
  1148. dprintkdbg(DBG_0, "start_scsi: (0x%p) <%02i-%i> srb=%p\n",
  1149. dcb->target_id, dcb->target_lun, srb);
  1150. srb->tag_number = TAG_NONE; /* acb->tag_max_num: had error read in eeprom */
  1151. s_stat = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
  1152. s_stat2 = 0;
  1153. s_stat2 = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
  1154. #if 1
  1155. if (s_stat & 0x20 /* s_stat2 & 0x02000 */ ) {
  1156. dprintkdbg(DBG_KG, "start_scsi: (0x%p) BUSY %02x %04x\n",
  1157. s_stat, s_stat2);
  1158. /*
  1159. * Try anyway?
  1160. *
  1161. * We could, BUT: Sometimes the TRM_S1040 misses to produce a Selection
  1162. * Timeout, a Disconnect or a Reselection IRQ, so we would be screwed!
  1163. * (This is likely to be a bug in the hardware. Obviously, most people
  1164. * only have one initiator per SCSI bus.)
  1165. * Instead let this fail and have the timer make sure the command is
  1166. * tried again after a short time
  1167. */
  1168. /*selto_timer (acb); */
  1169. return 1;
  1170. }
  1171. #endif
  1172. if (acb->active_dcb) {
  1173. dprintkl(KERN_DEBUG, "start_scsi: (0x%p) Attempt to start a"
  1174. "command while another command (0x%p) is active.",
  1175. srb->cmd,
  1176. acb->active_dcb->active_srb ?
  1177. acb->active_dcb->active_srb->cmd : 0);
  1178. return 1;
  1179. }
  1180. if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
  1181. dprintkdbg(DBG_KG, "start_scsi: (0x%p) Failed (busy)\n", srb->cmd);
  1182. return 1;
  1183. }
  1184. /* Allow starting of SCSI commands half a second before we allow the mid-level
  1185. * to queue them again after a reset */
  1186. if (time_before(jiffies, acb->last_reset - HZ / 2)) {
  1187. dprintkdbg(DBG_KG, "start_scsi: Refuse cmds (reset wait)\n");
  1188. return 1;
  1189. }
  1190. /* Flush FIFO */
  1191. clear_fifo(acb, "start_scsi");
  1192. DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
  1193. DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
  1194. DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
  1195. DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
  1196. srb->scsi_phase = PH_BUS_FREE; /* initial phase */
  1197. identify_message = dcb->identify_msg;
  1198. /*DC395x_TRM_write8(TRM_S1040_SCSI_IDMSG, identify_message); */
  1199. /* Don't allow disconnection for AUTO_REQSENSE: Cont.All.Cond.! */
  1200. if (srb->flag & AUTO_REQSENSE)
  1201. identify_message &= 0xBF;
  1202. if (((srb->cmd->cmnd[0] == INQUIRY)
  1203. || (srb->cmd->cmnd[0] == REQUEST_SENSE)
  1204. || (srb->flag & AUTO_REQSENSE))
  1205. && (((dcb->sync_mode & WIDE_NEGO_ENABLE)
  1206. && !(dcb->sync_mode & WIDE_NEGO_DONE))
  1207. || ((dcb->sync_mode & SYNC_NEGO_ENABLE)
  1208. && !(dcb->sync_mode & SYNC_NEGO_DONE)))
  1209. && (dcb->target_lun == 0)) {
  1210. srb->msgout_buf[0] = identify_message;
  1211. srb->msg_count = 1;
  1212. scsicommand = SCMD_SEL_ATNSTOP;
  1213. srb->state = SRB_MSGOUT;
  1214. #ifndef SYNC_FIRST
  1215. if (dcb->sync_mode & WIDE_NEGO_ENABLE
  1216. && dcb->inquiry7 & SCSI_INQ_WBUS16) {
  1217. build_wdtr(acb, dcb, srb);
  1218. goto no_cmd;
  1219. }
  1220. #endif
  1221. if (dcb->sync_mode & SYNC_NEGO_ENABLE
  1222. && dcb->inquiry7 & SCSI_INQ_SYNC) {
  1223. build_sdtr(acb, dcb, srb);
  1224. goto no_cmd;
  1225. }
  1226. if (dcb->sync_mode & WIDE_NEGO_ENABLE
  1227. && dcb->inquiry7 & SCSI_INQ_WBUS16) {
  1228. build_wdtr(acb, dcb, srb);
  1229. goto no_cmd;
  1230. }
  1231. srb->msg_count = 0;
  1232. }
  1233. /* Send identify message */
  1234. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, identify_message);
  1235. scsicommand = SCMD_SEL_ATN;
  1236. srb->state = SRB_START_;
  1237. #ifndef DC395x_NO_TAGQ
  1238. if ((dcb->sync_mode & EN_TAG_QUEUEING)
  1239. && (identify_message & 0xC0)) {
  1240. /* Send Tag message */
  1241. u32 tag_mask = 1;
  1242. u8 tag_number = 0;
  1243. while (tag_mask & dcb->tag_mask
  1244. && tag_number < dcb->max_command) {
  1245. tag_mask = tag_mask << 1;
  1246. tag_number++;
  1247. }
  1248. if (tag_number >= dcb->max_command) {
  1249. dprintkl(KERN_WARNING, "start_scsi: (0x%p) "
  1250. "Out of tags target=<%02i-%i>)\n",
  1251. srb->cmd, srb->cmd->device->id,
  1252. (u8)srb->cmd->device->lun);
  1253. srb->state = SRB_READY;
  1254. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
  1255. DO_HWRESELECT);
  1256. return 1;
  1257. }
  1258. /* Send Tag id */
  1259. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, SIMPLE_QUEUE_TAG);
  1260. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, tag_number);
  1261. dcb->tag_mask |= tag_mask;
  1262. srb->tag_number = tag_number;
  1263. scsicommand = SCMD_SEL_ATN3;
  1264. srb->state = SRB_START_;
  1265. }
  1266. #endif
  1267. /*polling:*/
  1268. /* Send CDB ..command block ......... */
  1269. dprintkdbg(DBG_KG, "start_scsi: (0x%p) <%02i-%i> cmnd=0x%02x tag=%i\n",
  1270. srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun,
  1271. srb->cmd->cmnd[0], srb->tag_number);
  1272. if (srb->flag & AUTO_REQSENSE) {
  1273. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
  1274. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
  1275. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
  1276. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
  1277. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, SCSI_SENSE_BUFFERSIZE);
  1278. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
  1279. } else {
  1280. ptr = (u8 *)srb->cmd->cmnd;
  1281. for (i = 0; i < srb->cmd->cmd_len; i++)
  1282. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
  1283. }
  1284. no_cmd:
  1285. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
  1286. DO_HWRESELECT | DO_DATALATCH);
  1287. if (DC395x_read16(acb, TRM_S1040_SCSI_STATUS) & SCSIINTERRUPT) {
  1288. /*
  1289. * If start_scsi return 1:
  1290. * we caught an interrupt (must be reset or reselection ... )
  1291. * : Let's process it first!
  1292. */
  1293. dprintkdbg(DBG_0, "start_scsi: (0x%p) <%02i-%i> Failed - busy\n",
  1294. srb->cmd, dcb->target_id, dcb->target_lun);
  1295. srb->state = SRB_READY;
  1296. free_tag(dcb, srb);
  1297. srb->msg_count = 0;
  1298. return_code = 1;
  1299. /* This IRQ should NOT get lost, as we did not acknowledge it */
  1300. } else {
  1301. /*
  1302. * If start_scsi returns 0:
  1303. * we know that the SCSI processor is free
  1304. */
  1305. srb->scsi_phase = PH_BUS_FREE; /* initial phase */
  1306. dcb->active_srb = srb;
  1307. acb->active_dcb = dcb;
  1308. return_code = 0;
  1309. /* it's important for atn stop */
  1310. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
  1311. DO_DATALATCH | DO_HWRESELECT);
  1312. /* SCSI command */
  1313. DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, scsicommand);
  1314. }
  1315. return return_code;
  1316. }
  1317. #define DC395x_ENABLE_MSGOUT \
  1318. DC395x_write16 (acb, TRM_S1040_SCSI_CONTROL, DO_SETATN); \
  1319. srb->state |= SRB_MSGOUT
  1320. /* abort command */
  1321. static inline void enable_msgout_abort(struct AdapterCtlBlk *acb,
  1322. struct ScsiReqBlk *srb)
  1323. {
  1324. srb->msgout_buf[0] = ABORT;
  1325. srb->msg_count = 1;
  1326. DC395x_ENABLE_MSGOUT;
  1327. srb->state &= ~SRB_MSGIN;
  1328. srb->state |= SRB_MSGOUT;
  1329. }
  1330. /**
  1331. * dc395x_handle_interrupt - Handle an interrupt that has been confirmed to
  1332. * have been triggered for this card.
  1333. *
  1334. * @acb: a pointer to the adpter control block
  1335. * @scsi_status: the status return when we checked the card
  1336. **/
  1337. static void dc395x_handle_interrupt(struct AdapterCtlBlk *acb,
  1338. u16 scsi_status)
  1339. {
  1340. struct DeviceCtlBlk *dcb;
  1341. struct ScsiReqBlk *srb;
  1342. u16 phase;
  1343. u8 scsi_intstatus;
  1344. unsigned long flags;
  1345. void (*dc395x_statev)(struct AdapterCtlBlk *, struct ScsiReqBlk *,
  1346. u16 *);
  1347. DC395x_LOCK_IO(acb->scsi_host, flags);
  1348. /* This acknowledges the IRQ */
  1349. scsi_intstatus = DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
  1350. if ((scsi_status & 0x2007) == 0x2002)
  1351. dprintkl(KERN_DEBUG,
  1352. "COP after COP completed? %04x\n", scsi_status);
  1353. if (debug_enabled(DBG_KG)) {
  1354. if (scsi_intstatus & INT_SELTIMEOUT)
  1355. dprintkdbg(DBG_KG, "handle_interrupt: Selection timeout\n");
  1356. }
  1357. /*dprintkl(KERN_DEBUG, "handle_interrupt: intstatus = 0x%02x ", scsi_intstatus); */
  1358. if (timer_pending(&acb->selto_timer))
  1359. del_timer(&acb->selto_timer);
  1360. if (scsi_intstatus & (INT_SELTIMEOUT | INT_DISCONNECT)) {
  1361. disconnect(acb); /* bus free interrupt */
  1362. goto out_unlock;
  1363. }
  1364. if (scsi_intstatus & INT_RESELECTED) {
  1365. reselect(acb);
  1366. goto out_unlock;
  1367. }
  1368. if (scsi_intstatus & INT_SELECT) {
  1369. dprintkl(KERN_INFO, "Host does not support target mode!\n");
  1370. goto out_unlock;
  1371. }
  1372. if (scsi_intstatus & INT_SCSIRESET) {
  1373. scsi_reset_detect(acb);
  1374. goto out_unlock;
  1375. }
  1376. if (scsi_intstatus & (INT_BUSSERVICE | INT_CMDDONE)) {
  1377. dcb = acb->active_dcb;
  1378. if (!dcb) {
  1379. dprintkl(KERN_DEBUG,
  1380. "Oops: BusService (%04x %02x) w/o ActiveDCB!\n",
  1381. scsi_status, scsi_intstatus);
  1382. goto out_unlock;
  1383. }
  1384. srb = dcb->active_srb;
  1385. if (dcb->flag & ABORT_DEV_) {
  1386. dprintkdbg(DBG_0, "MsgOut Abort Device.....\n");
  1387. enable_msgout_abort(acb, srb);
  1388. }
  1389. /* software sequential machine */
  1390. phase = (u16)srb->scsi_phase;
  1391. /*
  1392. * 62037 or 62137
  1393. * call dc395x_scsi_phase0[]... "phase entry"
  1394. * handle every phase before start transfer
  1395. */
  1396. /* data_out_phase0, phase:0 */
  1397. /* data_in_phase0, phase:1 */
  1398. /* command_phase0, phase:2 */
  1399. /* status_phase0, phase:3 */
  1400. /* nop0, phase:4 PH_BUS_FREE .. initial phase */
  1401. /* nop0, phase:5 PH_BUS_FREE .. initial phase */
  1402. /* msgout_phase0, phase:6 */
  1403. /* msgin_phase0, phase:7 */
  1404. dc395x_statev = dc395x_scsi_phase0[phase];
  1405. dc395x_statev(acb, srb, &scsi_status);
  1406. /*
  1407. * if there were any exception occurred scsi_status
  1408. * will be modify to bus free phase new scsi_status
  1409. * transfer out from ... previous dc395x_statev
  1410. */
  1411. srb->scsi_phase = scsi_status & PHASEMASK;
  1412. phase = (u16)scsi_status & PHASEMASK;
  1413. /*
  1414. * call dc395x_scsi_phase1[]... "phase entry" handle
  1415. * every phase to do transfer
  1416. */
  1417. /* data_out_phase1, phase:0 */
  1418. /* data_in_phase1, phase:1 */
  1419. /* command_phase1, phase:2 */
  1420. /* status_phase1, phase:3 */
  1421. /* nop1, phase:4 PH_BUS_FREE .. initial phase */
  1422. /* nop1, phase:5 PH_BUS_FREE .. initial phase */
  1423. /* msgout_phase1, phase:6 */
  1424. /* msgin_phase1, phase:7 */
  1425. dc395x_statev = dc395x_scsi_phase1[phase];
  1426. dc395x_statev(acb, srb, &scsi_status);
  1427. }
  1428. out_unlock:
  1429. DC395x_UNLOCK_IO(acb->scsi_host, flags);
  1430. }
  1431. static irqreturn_t dc395x_interrupt(int irq, void *dev_id)
  1432. {
  1433. struct AdapterCtlBlk *acb = dev_id;
  1434. u16 scsi_status;
  1435. u8 dma_status;
  1436. irqreturn_t handled = IRQ_NONE;
  1437. /*
  1438. * Check for pending interrupt
  1439. */
  1440. scsi_status = DC395x_read16(acb, TRM_S1040_SCSI_STATUS);
  1441. dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
  1442. if (scsi_status & SCSIINTERRUPT) {
  1443. /* interrupt pending - let's process it! */
  1444. dc395x_handle_interrupt(acb, scsi_status);
  1445. handled = IRQ_HANDLED;
  1446. }
  1447. else if (dma_status & 0x20) {
  1448. /* Error from the DMA engine */
  1449. dprintkl(KERN_INFO, "Interrupt from DMA engine: 0x%02x!\n", dma_status);
  1450. #if 0
  1451. dprintkl(KERN_INFO, "This means DMA error! Try to handle ...\n");
  1452. if (acb->active_dcb) {
  1453. acb->active_dcb-> flag |= ABORT_DEV_;
  1454. if (acb->active_dcb->active_srb)
  1455. enable_msgout_abort(acb, acb->active_dcb->active_srb);
  1456. }
  1457. DC395x_write8(acb, TRM_S1040_DMA_CONTROL, ABORTXFER | CLRXFIFO);
  1458. #else
  1459. dprintkl(KERN_INFO, "Ignoring DMA error (probably a bad thing) ...\n");
  1460. acb = NULL;
  1461. #endif
  1462. handled = IRQ_HANDLED;
  1463. }
  1464. return handled;
  1465. }
  1466. static void msgout_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  1467. u16 *pscsi_status)
  1468. {
  1469. dprintkdbg(DBG_0, "msgout_phase0: (0x%p)\n", srb->cmd);
  1470. if (srb->state & (SRB_UNEXPECT_RESEL + SRB_ABORT_SENT))
  1471. *pscsi_status = PH_BUS_FREE; /*.. initial phase */
  1472. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
  1473. srb->state &= ~SRB_MSGOUT;
  1474. }
  1475. static void msgout_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  1476. u16 *pscsi_status)
  1477. {
  1478. u16 i;
  1479. u8 *ptr;
  1480. dprintkdbg(DBG_0, "msgout_phase1: (0x%p)\n", srb->cmd);
  1481. clear_fifo(acb, "msgout_phase1");
  1482. if (!(srb->state & SRB_MSGOUT)) {
  1483. srb->state |= SRB_MSGOUT;
  1484. dprintkl(KERN_DEBUG,
  1485. "msgout_phase1: (0x%p) Phase unexpected\n",
  1486. srb->cmd); /* So what ? */
  1487. }
  1488. if (!srb->msg_count) {
  1489. dprintkdbg(DBG_0, "msgout_phase1: (0x%p) NOP msg\n",
  1490. srb->cmd);
  1491. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, NOP);
  1492. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
  1493. /* it's important for atn stop */
  1494. DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
  1495. return;
  1496. }
  1497. ptr = (u8 *)srb->msgout_buf;
  1498. for (i = 0; i < srb->msg_count; i++)
  1499. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr++);
  1500. srb->msg_count = 0;
  1501. if (srb->msgout_buf[0] == ABORT_TASK_SET)
  1502. srb->state = SRB_ABORT_SENT;
  1503. DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
  1504. }
  1505. static void command_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  1506. u16 *pscsi_status)
  1507. {
  1508. dprintkdbg(DBG_0, "command_phase0: (0x%p)\n", srb->cmd);
  1509. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
  1510. }
  1511. static void command_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  1512. u16 *pscsi_status)
  1513. {
  1514. struct DeviceCtlBlk *dcb;
  1515. u8 *ptr;
  1516. u16 i;
  1517. dprintkdbg(DBG_0, "command_phase1: (0x%p)\n", srb->cmd);
  1518. clear_fifo(acb, "command_phase1");
  1519. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_CLRATN);
  1520. if (!(srb->flag & AUTO_REQSENSE)) {
  1521. ptr = (u8 *)srb->cmd->cmnd;
  1522. for (i = 0; i < srb->cmd->cmd_len; i++) {
  1523. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *ptr);
  1524. ptr++;
  1525. }
  1526. } else {
  1527. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, REQUEST_SENSE);
  1528. dcb = acb->active_dcb;
  1529. /* target id */
  1530. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, (dcb->target_lun << 5));
  1531. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
  1532. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
  1533. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, SCSI_SENSE_BUFFERSIZE);
  1534. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
  1535. }
  1536. srb->state |= SRB_COMMAND;
  1537. /* it's important for atn stop */
  1538. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
  1539. /* SCSI command */
  1540. DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_OUT);
  1541. }
  1542. /*
  1543. * Verify that the remaining space in the hw sg lists is the same as
  1544. * the count of remaining bytes in srb->total_xfer_length
  1545. */
  1546. static void sg_verify_length(struct ScsiReqBlk *srb)
  1547. {
  1548. if (debug_enabled(DBG_SG)) {
  1549. unsigned len = 0;
  1550. unsigned idx = srb->sg_index;
  1551. struct SGentry *psge = srb->segment_x + idx;
  1552. for (; idx < srb->sg_count; psge++, idx++)
  1553. len += psge->length;
  1554. if (len != srb->total_xfer_length)
  1555. dprintkdbg(DBG_SG,
  1556. "Inconsistent SRB S/G lengths (Tot=%i, Count=%i) !!\n",
  1557. srb->total_xfer_length, len);
  1558. }
  1559. }
  1560. /*
  1561. * Compute the next Scatter Gather list index and adjust its length
  1562. * and address if necessary
  1563. */
  1564. static void sg_update_list(struct ScsiReqBlk *srb, u32 left)
  1565. {
  1566. u8 idx;
  1567. u32 xferred = srb->total_xfer_length - left; /* bytes transferred */
  1568. struct SGentry *psge = srb->segment_x + srb->sg_index;
  1569. dprintkdbg(DBG_0,
  1570. "sg_update_list: Transferred %i of %i bytes, %i remain\n",
  1571. xferred, srb->total_xfer_length, left);
  1572. if (xferred == 0) {
  1573. /* nothing to update since we did not transfer any data */
  1574. return;
  1575. }
  1576. sg_verify_length(srb);
  1577. srb->total_xfer_length = left; /* update remaining count */
  1578. for (idx = srb->sg_index; idx < srb->sg_count; idx++) {
  1579. if (xferred >= psge->length) {
  1580. /* Complete SG entries done */
  1581. xferred -= psge->length;
  1582. } else {
  1583. /* Partial SG entry done */
  1584. dma_sync_single_for_cpu(&srb->dcb->acb->dev->dev,
  1585. srb->sg_bus_addr, SEGMENTX_LEN,
  1586. DMA_TO_DEVICE);
  1587. psge->length -= xferred;
  1588. psge->address += xferred;
  1589. srb->sg_index = idx;
  1590. dma_sync_single_for_device(&srb->dcb->acb->dev->dev,
  1591. srb->sg_bus_addr, SEGMENTX_LEN,
  1592. DMA_TO_DEVICE);
  1593. break;
  1594. }
  1595. psge++;
  1596. }
  1597. sg_verify_length(srb);
  1598. }
  1599. /*
  1600. * We have transferred a single byte (PIO mode?) and need to update
  1601. * the count of bytes remaining (total_xfer_length) and update the sg
  1602. * entry to either point to next byte in the current sg entry, or of
  1603. * already at the end to point to the start of the next sg entry
  1604. */
  1605. static void sg_subtract_one(struct ScsiReqBlk *srb)
  1606. {
  1607. sg_update_list(srb, srb->total_xfer_length - 1);
  1608. }
  1609. /*
  1610. * cleanup_after_transfer
  1611. *
  1612. * Makes sure, DMA and SCSI engine are empty, after the transfer has finished
  1613. * KG: Currently called from StatusPhase1 ()
  1614. * Should probably also be called from other places
  1615. * Best might be to call it in DataXXPhase0, if new phase will differ
  1616. */
  1617. static void cleanup_after_transfer(struct AdapterCtlBlk *acb,
  1618. struct ScsiReqBlk *srb)
  1619. {
  1620. /*DC395x_write8 (TRM_S1040_DMA_STATUS, FORCEDMACOMP); */
  1621. if (DC395x_read16(acb, TRM_S1040_DMA_COMMAND) & 0x0001) { /* read */
  1622. if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
  1623. clear_fifo(acb, "cleanup/in");
  1624. if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
  1625. DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
  1626. } else { /* write */
  1627. if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80))
  1628. DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
  1629. if (!(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) & 0x40))
  1630. clear_fifo(acb, "cleanup/out");
  1631. }
  1632. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH);
  1633. }
  1634. /*
  1635. * Those no of bytes will be transferred w/ PIO through the SCSI FIFO
  1636. * Seems to be needed for unknown reasons; could be a hardware bug :-(
  1637. */
  1638. #define DC395x_LASTPIO 4
  1639. static void data_out_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  1640. u16 *pscsi_status)
  1641. {
  1642. struct DeviceCtlBlk *dcb = srb->dcb;
  1643. u16 scsi_status = *pscsi_status;
  1644. u32 d_left_counter = 0;
  1645. dprintkdbg(DBG_0, "data_out_phase0: (0x%p) <%02i-%i>\n",
  1646. srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
  1647. /*
  1648. * KG: We need to drain the buffers before we draw any conclusions!
  1649. * This means telling the DMA to push the rest into SCSI, telling
  1650. * SCSI to push the rest to the bus.
  1651. * However, the device might have been the one to stop us (phase
  1652. * change), and the data in transit just needs to be accounted so
  1653. * it can be retransmitted.)
  1654. */
  1655. /*
  1656. * KG: Stop DMA engine pushing more data into the SCSI FIFO
  1657. * If we need more data, the DMA SG list will be freshly set up, anyway
  1658. */
  1659. dprintkdbg(DBG_PIO, "data_out_phase0: "
  1660. "DMA{fifocnt=0x%02x fifostat=0x%02x} "
  1661. "SCSI{fifocnt=0x%02x cnt=0x%06x status=0x%04x} total=0x%06x\n",
  1662. DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
  1663. DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
  1664. DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
  1665. DC395x_read32(acb, TRM_S1040_SCSI_COUNTER), scsi_status,
  1666. srb->total_xfer_length);
  1667. DC395x_write8(acb, TRM_S1040_DMA_CONTROL, STOPDMAXFER | CLRXFIFO);
  1668. if (!(srb->state & SRB_XFERPAD)) {
  1669. if (scsi_status & PARITYERROR)
  1670. srb->status |= PARITY_ERROR;
  1671. /*
  1672. * KG: Right, we can't just rely on the SCSI_COUNTER, because this
  1673. * is the no of bytes it got from the DMA engine not the no it
  1674. * transferred successfully to the device. (And the difference could
  1675. * be as much as the FIFO size, I guess ...)
  1676. */
  1677. if (!(scsi_status & SCSIXFERDONE)) {
  1678. /*
  1679. * when data transfer from DMA FIFO to SCSI FIFO
  1680. * if there was some data left in SCSI FIFO
  1681. */
  1682. d_left_counter =
  1683. (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
  1684. 0x1F);
  1685. if (dcb->sync_period & WIDE_SYNC)
  1686. d_left_counter <<= 1;
  1687. dprintkdbg(DBG_KG, "data_out_phase0: FIFO contains %i %s\n"
  1688. "SCSI{fifocnt=0x%02x cnt=0x%08x} "
  1689. "DMA{fifocnt=0x%04x cnt=0x%02x ctr=0x%08x}\n",
  1690. DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
  1691. (dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
  1692. DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT),
  1693. DC395x_read32(acb, TRM_S1040_SCSI_COUNTER),
  1694. DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
  1695. DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
  1696. DC395x_read32(acb, TRM_S1040_DMA_CXCNT));
  1697. }
  1698. /*
  1699. * calculate all the residue data that not yet tranfered
  1700. * SCSI transfer counter + left in SCSI FIFO data
  1701. *
  1702. * .....TRM_S1040_SCSI_COUNTER (24bits)
  1703. * The counter always decrement by one for every SCSI byte transfer.
  1704. * .....TRM_S1040_SCSI_FIFOCNT ( 5bits)
  1705. * The counter is SCSI FIFO offset counter (in units of bytes or! words)
  1706. */
  1707. if (srb->total_xfer_length > DC395x_LASTPIO)
  1708. d_left_counter +=
  1709. DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
  1710. /* Is this a good idea? */
  1711. /*clear_fifo(acb, "DOP1"); */
  1712. /* KG: What is this supposed to be useful for? WIDE padding stuff? */
  1713. if (d_left_counter == 1 && dcb->sync_period & WIDE_SYNC
  1714. && scsi_bufflen(srb->cmd) % 2) {
  1715. d_left_counter = 0;
  1716. dprintkl(KERN_INFO,
  1717. "data_out_phase0: Discard 1 byte (0x%02x)\n",
  1718. scsi_status);
  1719. }
  1720. /*
  1721. * KG: Oops again. Same thinko as above: The SCSI might have been
  1722. * faster than the DMA engine, so that it ran out of data.
  1723. * In that case, we have to do just nothing!
  1724. * But: Why the interrupt: No phase change. No XFERCNT_2_ZERO. Or?
  1725. */
  1726. /*
  1727. * KG: This is nonsense: We have been WRITING data to the bus
  1728. * If the SCSI engine has no bytes left, how should the DMA engine?
  1729. */
  1730. if (d_left_counter == 0) {
  1731. srb->total_xfer_length = 0;
  1732. } else {
  1733. /*
  1734. * if transfer not yet complete
  1735. * there were some data residue in SCSI FIFO or
  1736. * SCSI transfer counter not empty
  1737. */
  1738. long oldxferred =
  1739. srb->total_xfer_length - d_left_counter;
  1740. const int diff =
  1741. (dcb->sync_period & WIDE_SYNC) ? 2 : 1;
  1742. sg_update_list(srb, d_left_counter);
  1743. /* KG: Most ugly hack! Apparently, this works around a chip bug */
  1744. if ((srb->segment_x[srb->sg_index].length ==
  1745. diff && scsi_sg_count(srb->cmd))
  1746. || ((oldxferred & ~PAGE_MASK) ==
  1747. (PAGE_SIZE - diff))
  1748. ) {
  1749. dprintkl(KERN_INFO, "data_out_phase0: "
  1750. "Work around chip bug (%i)?\n", diff);
  1751. d_left_counter =
  1752. srb->total_xfer_length - diff;
  1753. sg_update_list(srb, d_left_counter);
  1754. /*srb->total_xfer_length -= diff; */
  1755. /*srb->virt_addr += diff; */
  1756. /*if (srb->cmd->use_sg) */
  1757. /* srb->sg_index++; */
  1758. }
  1759. }
  1760. }
  1761. if ((*pscsi_status & PHASEMASK) != PH_DATA_OUT) {
  1762. cleanup_after_transfer(acb, srb);
  1763. }
  1764. }
  1765. static void data_out_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  1766. u16 *pscsi_status)
  1767. {
  1768. dprintkdbg(DBG_0, "data_out_phase1: (0x%p) <%02i-%i>\n",
  1769. srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
  1770. clear_fifo(acb, "data_out_phase1");
  1771. /* do prepare before transfer when data out phase */
  1772. data_io_transfer(acb, srb, XFERDATAOUT);
  1773. }
  1774. static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  1775. u16 *pscsi_status)
  1776. {
  1777. u16 scsi_status = *pscsi_status;
  1778. dprintkdbg(DBG_0, "data_in_phase0: (0x%p) <%02i-%i>\n",
  1779. srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
  1780. /*
  1781. * KG: DataIn is much more tricky than DataOut. When the device is finished
  1782. * and switches to another phase, the SCSI engine should be finished too.
  1783. * But: There might still be bytes left in its FIFO to be fetched by the DMA
  1784. * engine and transferred to memory.
  1785. * We should wait for the FIFOs to be emptied by that (is there any way to
  1786. * enforce this?) and then stop the DMA engine, because it might think, that
  1787. * there are more bytes to follow. Yes, the device might disconnect prior to
  1788. * having all bytes transferred!
  1789. * Also we should make sure that all data from the DMA engine buffer's really
  1790. * made its way to the system memory! Some documentation on this would not
  1791. * seem to be a bad idea, actually.
  1792. */
  1793. if (!(srb->state & SRB_XFERPAD)) {
  1794. u32 d_left_counter;
  1795. unsigned int sc, fc;
  1796. if (scsi_status & PARITYERROR) {
  1797. dprintkl(KERN_INFO, "data_in_phase0: (0x%p) "
  1798. "Parity Error\n", srb->cmd);
  1799. srb->status |= PARITY_ERROR;
  1800. }
  1801. /*
  1802. * KG: We should wait for the DMA FIFO to be empty ...
  1803. * but: it would be better to wait first for the SCSI FIFO and then the
  1804. * the DMA FIFO to become empty? How do we know, that the device not already
  1805. * sent data to the FIFO in a MsgIn phase, eg.?
  1806. */
  1807. if (!(DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT) & 0x80)) {
  1808. #if 0
  1809. int ctr = 6000000;
  1810. dprintkl(KERN_DEBUG,
  1811. "DIP0: Wait for DMA FIFO to flush ...\n");
  1812. /*DC395x_write8 (TRM_S1040_DMA_CONTROL, STOPDMAXFER); */
  1813. /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 7); */
  1814. /*DC395x_write8 (TRM_S1040_SCSI_COMMAND, SCMD_DMA_IN); */
  1815. while (!
  1816. (DC395x_read16(acb, TRM_S1040_DMA_FIFOSTAT) &
  1817. 0x80) && --ctr);
  1818. if (ctr < 6000000 - 1)
  1819. dprintkl(KERN_DEBUG
  1820. "DIP0: Had to wait for DMA ...\n");
  1821. if (!ctr)
  1822. dprintkl(KERN_ERR,
  1823. "Deadlock in DIP0 waiting for DMA FIFO empty!!\n");
  1824. /*DC395x_write32 (TRM_S1040_SCSI_COUNTER, 0); */
  1825. #endif
  1826. dprintkdbg(DBG_KG, "data_in_phase0: "
  1827. "DMA{fifocnt=0x%02x fifostat=0x%02x}\n",
  1828. DC395x_read8(acb, TRM_S1040_DMA_FIFOCNT),
  1829. DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT));
  1830. }
  1831. /* Now: Check remainig data: The SCSI counters should tell us ... */
  1832. sc = DC395x_read32(acb, TRM_S1040_SCSI_COUNTER);
  1833. fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
  1834. d_left_counter = sc + ((fc & 0x1f)
  1835. << ((srb->dcb->sync_period & WIDE_SYNC) ? 1 :
  1836. 0));
  1837. dprintkdbg(DBG_KG, "data_in_phase0: "
  1838. "SCSI{fifocnt=0x%02x%s ctr=0x%08x} "
  1839. "DMA{fifocnt=0x%02x fifostat=0x%02x ctr=0x%08x} "
  1840. "Remain{totxfer=%i scsi_fifo+ctr=%i}\n",
  1841. fc,
  1842. (srb->dcb->sync_period & WIDE_SYNC) ? "words" : "bytes",
  1843. sc,
  1844. fc,
  1845. DC395x_read8(acb, TRM_S1040_DMA_FIFOSTAT),
  1846. DC395x_read32(acb, TRM_S1040_DMA_CXCNT),
  1847. srb->total_xfer_length, d_left_counter);
  1848. #if DC395x_LASTPIO
  1849. /* KG: Less than or equal to 4 bytes can not be transferred via DMA, it seems. */
  1850. if (d_left_counter
  1851. && srb->total_xfer_length <= DC395x_LASTPIO) {
  1852. size_t left_io = srb->total_xfer_length;
  1853. /*u32 addr = (srb->segment_x[srb->sg_index].address); */
  1854. /*sg_update_list (srb, d_left_counter); */
  1855. dprintkdbg(DBG_PIO, "data_in_phase0: PIO (%i %s) "
  1856. "for remaining %i bytes:",
  1857. fc & 0x1f,
  1858. (srb->dcb->sync_period & WIDE_SYNC) ?
  1859. "words" : "bytes",
  1860. srb->total_xfer_length);
  1861. if (srb->dcb->sync_period & WIDE_SYNC)
  1862. DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
  1863. CFG2_WIDEFIFO);
  1864. while (left_io) {
  1865. unsigned char *virt, *base = NULL;
  1866. unsigned long flags = 0;
  1867. size_t len = left_io;
  1868. size_t offset = srb->request_length - left_io;
  1869. local_irq_save(flags);
  1870. /* Assumption: it's inside one page as it's at most 4 bytes and
  1871. I just assume it's on a 4-byte boundary */
  1872. base = scsi_kmap_atomic_sg(scsi_sglist(srb->cmd),
  1873. srb->sg_count, &offset, &len);
  1874. virt = base + offset;
  1875. left_io -= len;
  1876. while (len) {
  1877. u8 byte;
  1878. byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
  1879. *virt++ = byte;
  1880. if (debug_enabled(DBG_PIO))
  1881. printk(" %02x", byte);
  1882. d_left_counter--;
  1883. sg_subtract_one(srb);
  1884. len--;
  1885. fc = DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT);
  1886. if (fc == 0x40) {
  1887. left_io = 0;
  1888. break;
  1889. }
  1890. }
  1891. WARN_ON((fc != 0x40) == !d_left_counter);
  1892. if (fc == 0x40 && (srb->dcb->sync_period & WIDE_SYNC)) {
  1893. /* Read the last byte ... */
  1894. if (srb->total_xfer_length > 0) {
  1895. u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
  1896. *virt++ = byte;
  1897. srb->total_xfer_length--;
  1898. if (debug_enabled(DBG_PIO))
  1899. printk(" %02x", byte);
  1900. }
  1901. DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
  1902. }
  1903. scsi_kunmap_atomic_sg(base);
  1904. local_irq_restore(flags);
  1905. }
  1906. /*printk(" %08x", *(u32*)(bus_to_virt (addr))); */
  1907. /*srb->total_xfer_length = 0; */
  1908. if (debug_enabled(DBG_PIO))
  1909. printk("\n");
  1910. }
  1911. #endif /* DC395x_LASTPIO */
  1912. #if 0
  1913. /*
  1914. * KG: This was in DATAOUT. Does it also belong here?
  1915. * Nobody seems to know what counter and fifo_cnt count exactly ...
  1916. */
  1917. if (!(scsi_status & SCSIXFERDONE)) {
  1918. /*
  1919. * when data transfer from DMA FIFO to SCSI FIFO
  1920. * if there was some data left in SCSI FIFO
  1921. */
  1922. d_left_counter =
  1923. (u32)(DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) &
  1924. 0x1F);
  1925. if (srb->dcb->sync_period & WIDE_SYNC)
  1926. d_left_counter <<= 1;
  1927. /*
  1928. * if WIDE scsi SCSI FIFOCNT unit is word !!!
  1929. * so need to *= 2
  1930. * KG: Seems to be correct ...
  1931. */
  1932. }
  1933. #endif
  1934. /* KG: This should not be needed any more! */
  1935. if (d_left_counter == 0
  1936. || (scsi_status & SCSIXFERCNT_2_ZERO)) {
  1937. #if 0
  1938. int ctr = 6000000;
  1939. u8 TempDMAstatus;
  1940. do {
  1941. TempDMAstatus =
  1942. DC395x_read8(acb, TRM_S1040_DMA_STATUS);
  1943. } while (!(TempDMAstatus & DMAXFERCOMP) && --ctr);
  1944. if (!ctr)
  1945. dprintkl(KERN_ERR,
  1946. "Deadlock in DataInPhase0 waiting for DMA!!\n");
  1947. srb->total_xfer_length = 0;
  1948. #endif
  1949. srb->total_xfer_length = d_left_counter;
  1950. } else { /* phase changed */
  1951. /*
  1952. * parsing the case:
  1953. * when a transfer not yet complete
  1954. * but be disconnected by target
  1955. * if transfer not yet complete
  1956. * there were some data residue in SCSI FIFO or
  1957. * SCSI transfer counter not empty
  1958. */
  1959. sg_update_list(srb, d_left_counter);
  1960. }
  1961. }
  1962. /* KG: The target may decide to disconnect: Empty FIFO before! */
  1963. if ((*pscsi_status & PHASEMASK) != PH_DATA_IN) {
  1964. cleanup_after_transfer(acb, srb);
  1965. }
  1966. }
  1967. static void data_in_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  1968. u16 *pscsi_status)
  1969. {
  1970. dprintkdbg(DBG_0, "data_in_phase1: (0x%p) <%02i-%i>\n",
  1971. srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
  1972. data_io_transfer(acb, srb, XFERDATAIN);
  1973. }
  1974. static void data_io_transfer(struct AdapterCtlBlk *acb,
  1975. struct ScsiReqBlk *srb, u16 io_dir)
  1976. {
  1977. struct DeviceCtlBlk *dcb = srb->dcb;
  1978. u8 bval;
  1979. dprintkdbg(DBG_0,
  1980. "data_io_transfer: (0x%p) <%02i-%i> %c len=%i, sg=(%i/%i)\n",
  1981. srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun,
  1982. ((io_dir & DMACMD_DIR) ? 'r' : 'w'),
  1983. srb->total_xfer_length, srb->sg_index, srb->sg_count);
  1984. if (srb == acb->tmp_srb)
  1985. dprintkl(KERN_ERR, "data_io_transfer: Using tmp_srb!\n");
  1986. if (srb->sg_index >= srb->sg_count) {
  1987. /* can't happen? out of bounds error */
  1988. return;
  1989. }
  1990. if (srb->total_xfer_length > DC395x_LASTPIO) {
  1991. u8 dma_status = DC395x_read8(acb, TRM_S1040_DMA_STATUS);
  1992. /*
  1993. * KG: What should we do: Use SCSI Cmd 0x90/0x92?
  1994. * Maybe, even ABORTXFER would be appropriate
  1995. */
  1996. if (dma_status & XFERPENDING) {
  1997. dprintkl(KERN_DEBUG, "data_io_transfer: Xfer pending! "
  1998. "Expect trouble!\n");
  1999. dump_register_info(acb, dcb, srb);
  2000. DC395x_write8(acb, TRM_S1040_DMA_CONTROL, CLRXFIFO);
  2001. }
  2002. /* clear_fifo(acb, "IO"); */
  2003. /*
  2004. * load what physical address of Scatter/Gather list table
  2005. * want to be transfer
  2006. */
  2007. srb->state |= SRB_DATA_XFER;
  2008. DC395x_write32(acb, TRM_S1040_DMA_XHIGHADDR, 0);
  2009. if (scsi_sg_count(srb->cmd)) { /* with S/G */
  2010. io_dir |= DMACMD_SG;
  2011. DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
  2012. srb->sg_bus_addr +
  2013. sizeof(struct SGentry) *
  2014. srb->sg_index);
  2015. /* load how many bytes in the sg list table */
  2016. DC395x_write32(acb, TRM_S1040_DMA_XCNT,
  2017. ((u32)(srb->sg_count -
  2018. srb->sg_index) << 3));
  2019. } else { /* without S/G */
  2020. io_dir &= ~DMACMD_SG;
  2021. DC395x_write32(acb, TRM_S1040_DMA_XLOWADDR,
  2022. srb->segment_x[0].address);
  2023. DC395x_write32(acb, TRM_S1040_DMA_XCNT,
  2024. srb->segment_x[0].length);
  2025. }
  2026. /* load total transfer length (24bits) max value 16Mbyte */
  2027. DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
  2028. srb->total_xfer_length);
  2029. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
  2030. if (io_dir & DMACMD_DIR) { /* read */
  2031. DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
  2032. SCMD_DMA_IN);
  2033. DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
  2034. } else {
  2035. DC395x_write16(acb, TRM_S1040_DMA_COMMAND, io_dir);
  2036. DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
  2037. SCMD_DMA_OUT);
  2038. }
  2039. }
  2040. #if DC395x_LASTPIO
  2041. else if (srb->total_xfer_length > 0) { /* The last four bytes: Do PIO */
  2042. /*
  2043. * load what physical address of Scatter/Gather list table
  2044. * want to be transfer
  2045. */
  2046. srb->state |= SRB_DATA_XFER;
  2047. /* load total transfer length (24bits) max value 16Mbyte */
  2048. DC395x_write32(acb, TRM_S1040_SCSI_COUNTER,
  2049. srb->total_xfer_length);
  2050. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
  2051. if (io_dir & DMACMD_DIR) { /* read */
  2052. DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
  2053. SCMD_FIFO_IN);
  2054. } else { /* write */
  2055. int ln = srb->total_xfer_length;
  2056. size_t left_io = srb->total_xfer_length;
  2057. if (srb->dcb->sync_period & WIDE_SYNC)
  2058. DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
  2059. CFG2_WIDEFIFO);
  2060. while (left_io) {
  2061. unsigned char *virt, *base = NULL;
  2062. unsigned long flags = 0;
  2063. size_t len = left_io;
  2064. size_t offset = srb->request_length - left_io;
  2065. local_irq_save(flags);
  2066. /* Again, max 4 bytes */
  2067. base = scsi_kmap_atomic_sg(scsi_sglist(srb->cmd),
  2068. srb->sg_count, &offset, &len);
  2069. virt = base + offset;
  2070. left_io -= len;
  2071. while (len--) {
  2072. if (debug_enabled(DBG_PIO))
  2073. printk(" %02x", *virt);
  2074. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, *virt++);
  2075. sg_subtract_one(srb);
  2076. }
  2077. scsi_kunmap_atomic_sg(base);
  2078. local_irq_restore(flags);
  2079. }
  2080. if (srb->dcb->sync_period & WIDE_SYNC) {
  2081. if (ln % 2) {
  2082. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 0);
  2083. if (debug_enabled(DBG_PIO))
  2084. printk(" |00");
  2085. }
  2086. DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
  2087. }
  2088. /*DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, ln); */
  2089. if (debug_enabled(DBG_PIO))
  2090. printk("\n");
  2091. DC395x_write8(acb, TRM_S1040_SCSI_COMMAND,
  2092. SCMD_FIFO_OUT);
  2093. }
  2094. }
  2095. #endif /* DC395x_LASTPIO */
  2096. else { /* xfer pad */
  2097. if (srb->sg_count) {
  2098. srb->adapter_status = H_OVER_UNDER_RUN;
  2099. srb->status |= OVER_RUN;
  2100. }
  2101. /*
  2102. * KG: despite the fact that we are using 16 bits I/O ops
  2103. * the SCSI FIFO is only 8 bits according to the docs
  2104. * (we can set bit 1 in 0x8f to serialize FIFO access ...)
  2105. */
  2106. if (dcb->sync_period & WIDE_SYNC) {
  2107. DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 2);
  2108. DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2,
  2109. CFG2_WIDEFIFO);
  2110. if (io_dir & DMACMD_DIR) {
  2111. DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
  2112. DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
  2113. } else {
  2114. /* Danger, Robinson: If you find KGs
  2115. * scattered over the wide disk, the driver
  2116. * or chip is to blame :-( */
  2117. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
  2118. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'G');
  2119. }
  2120. DC395x_write8(acb, TRM_S1040_SCSI_CONFIG2, 0);
  2121. } else {
  2122. DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
  2123. /* Danger, Robinson: If you find a collection of Ks on your disk
  2124. * something broke :-( */
  2125. if (io_dir & DMACMD_DIR)
  2126. DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
  2127. else
  2128. DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 'K');
  2129. }
  2130. srb->state |= SRB_XFERPAD;
  2131. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
  2132. /* SCSI command */
  2133. bval = (io_dir & DMACMD_DIR) ? SCMD_FIFO_IN : SCMD_FIFO_OUT;
  2134. DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, bval);
  2135. }
  2136. }
  2137. static void status_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  2138. u16 *pscsi_status)
  2139. {
  2140. dprintkdbg(DBG_0, "status_phase0: (0x%p) <%02i-%i>\n",
  2141. srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
  2142. srb->target_status = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
  2143. srb->end_message = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); /* get message */
  2144. srb->state = SRB_COMPLETED;
  2145. *pscsi_status = PH_BUS_FREE; /*.. initial phase */
  2146. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
  2147. DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
  2148. }
  2149. static void status_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  2150. u16 *pscsi_status)
  2151. {
  2152. dprintkdbg(DBG_0, "status_phase1: (0x%p) <%02i-%i>\n",
  2153. srb->cmd, srb->cmd->device->id, (u8)srb->cmd->device->lun);
  2154. srb->state = SRB_STATUS;
  2155. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
  2156. DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_COMP);
  2157. }
  2158. /* Check if the message is complete */
  2159. static inline u8 msgin_completed(u8 * msgbuf, u32 len)
  2160. {
  2161. if (*msgbuf == EXTENDED_MESSAGE) {
  2162. if (len < 2)
  2163. return 0;
  2164. if (len < msgbuf[1] + 2)
  2165. return 0;
  2166. } else if (*msgbuf >= 0x20 && *msgbuf <= 0x2f) /* two byte messages */
  2167. if (len < 2)
  2168. return 0;
  2169. return 1;
  2170. }
  2171. /* reject_msg */
  2172. static inline void msgin_reject(struct AdapterCtlBlk *acb,
  2173. struct ScsiReqBlk *srb)
  2174. {
  2175. srb->msgout_buf[0] = MESSAGE_REJECT;
  2176. srb->msg_count = 1;
  2177. DC395x_ENABLE_MSGOUT;
  2178. srb->state &= ~SRB_MSGIN;
  2179. srb->state |= SRB_MSGOUT;
  2180. dprintkl(KERN_INFO, "msgin_reject: 0x%02x <%02i-%i>\n",
  2181. srb->msgin_buf[0],
  2182. srb->dcb->target_id, srb->dcb->target_lun);
  2183. }
  2184. static struct ScsiReqBlk *msgin_qtag(struct AdapterCtlBlk *acb,
  2185. struct DeviceCtlBlk *dcb, u8 tag)
  2186. {
  2187. struct ScsiReqBlk *srb = NULL;
  2188. struct ScsiReqBlk *i;
  2189. dprintkdbg(DBG_0, "msgin_qtag: (0x%p) tag=%i srb=%p\n",
  2190. srb->cmd, tag, srb);
  2191. if (!(dcb->tag_mask & (1 << tag)))
  2192. dprintkl(KERN_DEBUG,
  2193. "msgin_qtag: tag_mask=0x%08x does not reserve tag %i!\n",
  2194. dcb->tag_mask, tag);
  2195. if (list_empty(&dcb->srb_going_list))
  2196. goto mingx0;
  2197. list_for_each_entry(i, &dcb->srb_going_list, list) {
  2198. if (i->tag_number == tag) {
  2199. srb = i;
  2200. break;
  2201. }
  2202. }
  2203. if (!srb)
  2204. goto mingx0;
  2205. dprintkdbg(DBG_0, "msgin_qtag: (0x%p) <%02i-%i>\n",
  2206. srb->cmd, srb->dcb->target_id, srb->dcb->target_lun);
  2207. if (dcb->flag & ABORT_DEV_) {
  2208. /*srb->state = SRB_ABORT_SENT; */
  2209. enable_msgout_abort(acb, srb);
  2210. }
  2211. if (!(srb->state & SRB_DISCONNECT))
  2212. goto mingx0;
  2213. memcpy(srb->msgin_buf, dcb->active_srb->msgin_buf, acb->msg_len);
  2214. srb->state |= dcb->active_srb->state;
  2215. srb->state |= SRB_DATA_XFER;
  2216. dcb->active_srb = srb;
  2217. /* How can we make the DORS happy? */
  2218. return srb;
  2219. mingx0:
  2220. srb = acb->tmp_srb;
  2221. srb->state = SRB_UNEXPECT_RESEL;
  2222. dcb->active_srb = srb;
  2223. srb->msgout_buf[0] = ABORT_TASK;
  2224. srb->msg_count = 1;
  2225. DC395x_ENABLE_MSGOUT;
  2226. dprintkl(KERN_DEBUG, "msgin_qtag: Unknown tag %i - abort\n", tag);
  2227. return srb;
  2228. }
  2229. static inline void reprogram_regs(struct AdapterCtlBlk *acb,
  2230. struct DeviceCtlBlk *dcb)
  2231. {
  2232. DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id);
  2233. DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period);
  2234. DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset);
  2235. set_xfer_rate(acb, dcb);
  2236. }
  2237. /* set async transfer mode */
  2238. static void msgin_set_async(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
  2239. {
  2240. struct DeviceCtlBlk *dcb = srb->dcb;
  2241. dprintkl(KERN_DEBUG, "msgin_set_async: No sync transfers <%02i-%i>\n",
  2242. dcb->target_id, dcb->target_lun);
  2243. dcb->sync_mode &= ~(SYNC_NEGO_ENABLE);
  2244. dcb->sync_mode |= SYNC_NEGO_DONE;
  2245. /*dcb->sync_period &= 0; */
  2246. dcb->sync_offset = 0;
  2247. dcb->min_nego_period = 200 >> 2; /* 200ns <=> 5 MHz */
  2248. srb->state &= ~SRB_DO_SYNC_NEGO;
  2249. reprogram_regs(acb, dcb);
  2250. if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
  2251. && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
  2252. build_wdtr(acb, dcb, srb);
  2253. DC395x_ENABLE_MSGOUT;
  2254. dprintkdbg(DBG_0, "msgin_set_async(rej): Try WDTR anyway\n");
  2255. }
  2256. }
  2257. /* set sync transfer mode */
  2258. static void msgin_set_sync(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
  2259. {
  2260. struct DeviceCtlBlk *dcb = srb->dcb;
  2261. u8 bval;
  2262. int fact;
  2263. dprintkdbg(DBG_1, "msgin_set_sync: <%02i> Sync: %ins "
  2264. "(%02i.%01i MHz) Offset %i\n",
  2265. dcb->target_id, srb->msgin_buf[3] << 2,
  2266. (250 / srb->msgin_buf[3]),
  2267. ((250 % srb->msgin_buf[3]) * 10) / srb->msgin_buf[3],
  2268. srb->msgin_buf[4]);
  2269. if (srb->msgin_buf[4] > 15)
  2270. srb->msgin_buf[4] = 15;
  2271. if (!(dcb->dev_mode & NTC_DO_SYNC_NEGO))
  2272. dcb->sync_offset = 0;
  2273. else if (dcb->sync_offset == 0)
  2274. dcb->sync_offset = srb->msgin_buf[4];
  2275. if (srb->msgin_buf[4] > dcb->sync_offset)
  2276. srb->msgin_buf[4] = dcb->sync_offset;
  2277. else
  2278. dcb->sync_offset = srb->msgin_buf[4];
  2279. bval = 0;
  2280. while (bval < 7 && (srb->msgin_buf[3] > clock_period[bval]
  2281. || dcb->min_nego_period >
  2282. clock_period[bval]))
  2283. bval++;
  2284. if (srb->msgin_buf[3] < clock_period[bval])
  2285. dprintkl(KERN_INFO,
  2286. "msgin_set_sync: Increase sync nego period to %ins\n",
  2287. clock_period[bval] << 2);
  2288. srb->msgin_buf[3] = clock_period[bval];
  2289. dcb->sync_period &= 0xf0;
  2290. dcb->sync_period |= ALT_SYNC | bval;
  2291. dcb->min_nego_period = srb->msgin_buf[3];
  2292. if (dcb->sync_period & WIDE_SYNC)
  2293. fact = 500;
  2294. else
  2295. fact = 250;
  2296. dprintkl(KERN_INFO,
  2297. "Target %02i: %s Sync: %ins Offset %i (%02i.%01i MB/s)\n",
  2298. dcb->target_id, (fact == 500) ? "Wide16" : "",
  2299. dcb->min_nego_period << 2, dcb->sync_offset,
  2300. (fact / dcb->min_nego_period),
  2301. ((fact % dcb->min_nego_period) * 10 +
  2302. dcb->min_nego_period / 2) / dcb->min_nego_period);
  2303. if (!(srb->state & SRB_DO_SYNC_NEGO)) {
  2304. /* Reply with corrected SDTR Message */
  2305. dprintkl(KERN_DEBUG, "msgin_set_sync: answer w/%ins %i\n",
  2306. srb->msgin_buf[3] << 2, srb->msgin_buf[4]);
  2307. memcpy(srb->msgout_buf, srb->msgin_buf, 5);
  2308. srb->msg_count = 5;
  2309. DC395x_ENABLE_MSGOUT;
  2310. dcb->sync_mode |= SYNC_NEGO_DONE;
  2311. } else {
  2312. if ((dcb->sync_mode & WIDE_NEGO_ENABLE)
  2313. && !(dcb->sync_mode & WIDE_NEGO_DONE)) {
  2314. build_wdtr(acb, dcb, srb);
  2315. DC395x_ENABLE_MSGOUT;
  2316. dprintkdbg(DBG_0, "msgin_set_sync: Also try WDTR\n");
  2317. }
  2318. }
  2319. srb->state &= ~SRB_DO_SYNC_NEGO;
  2320. dcb->sync_mode |= SYNC_NEGO_DONE | SYNC_NEGO_ENABLE;
  2321. reprogram_regs(acb, dcb);
  2322. }
  2323. static inline void msgin_set_nowide(struct AdapterCtlBlk *acb,
  2324. struct ScsiReqBlk *srb)
  2325. {
  2326. struct DeviceCtlBlk *dcb = srb->dcb;
  2327. dprintkdbg(DBG_1, "msgin_set_nowide: <%02i>\n", dcb->target_id);
  2328. dcb->sync_period &= ~WIDE_SYNC;
  2329. dcb->sync_mode &= ~(WIDE_NEGO_ENABLE);
  2330. dcb->sync_mode |= WIDE_NEGO_DONE;
  2331. srb->state &= ~SRB_DO_WIDE_NEGO;
  2332. reprogram_regs(acb, dcb);
  2333. if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
  2334. && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
  2335. build_sdtr(acb, dcb, srb);
  2336. DC395x_ENABLE_MSGOUT;
  2337. dprintkdbg(DBG_0, "msgin_set_nowide: Rejected. Try SDTR anyway\n");
  2338. }
  2339. }
  2340. static void msgin_set_wide(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
  2341. {
  2342. struct DeviceCtlBlk *dcb = srb->dcb;
  2343. u8 wide = (dcb->dev_mode & NTC_DO_WIDE_NEGO
  2344. && acb->config & HCC_WIDE_CARD) ? 1 : 0;
  2345. dprintkdbg(DBG_1, "msgin_set_wide: <%02i>\n", dcb->target_id);
  2346. if (srb->msgin_buf[3] > wide)
  2347. srb->msgin_buf[3] = wide;
  2348. /* Completed */
  2349. if (!(srb->state & SRB_DO_WIDE_NEGO)) {
  2350. dprintkl(KERN_DEBUG,
  2351. "msgin_set_wide: Wide nego initiated <%02i>\n",
  2352. dcb->target_id);
  2353. memcpy(srb->msgout_buf, srb->msgin_buf, 4);
  2354. srb->msg_count = 4;
  2355. srb->state |= SRB_DO_WIDE_NEGO;
  2356. DC395x_ENABLE_MSGOUT;
  2357. }
  2358. dcb->sync_mode |= (WIDE_NEGO_ENABLE | WIDE_NEGO_DONE);
  2359. if (srb->msgin_buf[3] > 0)
  2360. dcb->sync_period |= WIDE_SYNC;
  2361. else
  2362. dcb->sync_period &= ~WIDE_SYNC;
  2363. srb->state &= ~SRB_DO_WIDE_NEGO;
  2364. /*dcb->sync_mode &= ~(WIDE_NEGO_ENABLE+WIDE_NEGO_DONE); */
  2365. dprintkdbg(DBG_1,
  2366. "msgin_set_wide: Wide (%i bit) negotiated <%02i>\n",
  2367. (8 << srb->msgin_buf[3]), dcb->target_id);
  2368. reprogram_regs(acb, dcb);
  2369. if ((dcb->sync_mode & SYNC_NEGO_ENABLE)
  2370. && !(dcb->sync_mode & SYNC_NEGO_DONE)) {
  2371. build_sdtr(acb, dcb, srb);
  2372. DC395x_ENABLE_MSGOUT;
  2373. dprintkdbg(DBG_0, "msgin_set_wide: Also try SDTR.\n");
  2374. }
  2375. }
  2376. /*
  2377. * extended message codes:
  2378. *
  2379. * code description
  2380. *
  2381. * 02h Reserved
  2382. * 00h MODIFY DATA POINTER
  2383. * 01h SYNCHRONOUS DATA TRANSFER REQUEST
  2384. * 03h WIDE DATA TRANSFER REQUEST
  2385. * 04h - 7Fh Reserved
  2386. * 80h - FFh Vendor specific
  2387. */
  2388. static void msgin_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  2389. u16 *pscsi_status)
  2390. {
  2391. struct DeviceCtlBlk *dcb = acb->active_dcb;
  2392. dprintkdbg(DBG_0, "msgin_phase0: (0x%p)\n", srb->cmd);
  2393. srb->msgin_buf[acb->msg_len++] = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
  2394. if (msgin_completed(srb->msgin_buf, acb->msg_len)) {
  2395. /* Now eval the msg */
  2396. switch (srb->msgin_buf[0]) {
  2397. case DISCONNECT:
  2398. srb->state = SRB_DISCONNECT;
  2399. break;
  2400. case SIMPLE_QUEUE_TAG:
  2401. case HEAD_OF_QUEUE_TAG:
  2402. case ORDERED_QUEUE_TAG:
  2403. srb =
  2404. msgin_qtag(acb, dcb,
  2405. srb->msgin_buf[1]);
  2406. break;
  2407. case MESSAGE_REJECT:
  2408. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL,
  2409. DO_CLRATN | DO_DATALATCH);
  2410. /* A sync nego message was rejected ! */
  2411. if (srb->state & SRB_DO_SYNC_NEGO) {
  2412. msgin_set_async(acb, srb);
  2413. break;
  2414. }
  2415. /* A wide nego message was rejected ! */
  2416. if (srb->state & SRB_DO_WIDE_NEGO) {
  2417. msgin_set_nowide(acb, srb);
  2418. break;
  2419. }
  2420. enable_msgout_abort(acb, srb);
  2421. /*srb->state |= SRB_ABORT_SENT */
  2422. break;
  2423. case EXTENDED_MESSAGE:
  2424. /* SDTR */
  2425. if (srb->msgin_buf[1] == 3
  2426. && srb->msgin_buf[2] == EXTENDED_SDTR) {
  2427. msgin_set_sync(acb, srb);
  2428. break;
  2429. }
  2430. /* WDTR */
  2431. if (srb->msgin_buf[1] == 2
  2432. && srb->msgin_buf[2] == EXTENDED_WDTR
  2433. && srb->msgin_buf[3] <= 2) { /* sanity check ... */
  2434. msgin_set_wide(acb, srb);
  2435. break;
  2436. }
  2437. msgin_reject(acb, srb);
  2438. break;
  2439. case IGNORE_WIDE_RESIDUE:
  2440. /* Discard wide residual */
  2441. dprintkdbg(DBG_0, "msgin_phase0: Ignore Wide Residual!\n");
  2442. break;
  2443. case COMMAND_COMPLETE:
  2444. /* nothing has to be done */
  2445. break;
  2446. case SAVE_POINTERS:
  2447. /*
  2448. * SAVE POINTER may be ignored as we have the struct
  2449. * ScsiReqBlk* associated with the scsi command.
  2450. */
  2451. dprintkdbg(DBG_0, "msgin_phase0: (0x%p) "
  2452. "SAVE POINTER rem=%i Ignore\n",
  2453. srb->cmd, srb->total_xfer_length);
  2454. break;
  2455. case RESTORE_POINTERS:
  2456. dprintkdbg(DBG_0, "msgin_phase0: RESTORE POINTER. Ignore\n");
  2457. break;
  2458. case ABORT:
  2459. dprintkdbg(DBG_0, "msgin_phase0: (0x%p) "
  2460. "<%02i-%i> ABORT msg\n",
  2461. srb->cmd, dcb->target_id,
  2462. dcb->target_lun);
  2463. dcb->flag |= ABORT_DEV_;
  2464. enable_msgout_abort(acb, srb);
  2465. break;
  2466. default:
  2467. /* reject unknown messages */
  2468. if (srb->msgin_buf[0] & IDENTIFY_BASE) {
  2469. dprintkdbg(DBG_0, "msgin_phase0: Identify msg\n");
  2470. srb->msg_count = 1;
  2471. srb->msgout_buf[0] = dcb->identify_msg;
  2472. DC395x_ENABLE_MSGOUT;
  2473. srb->state |= SRB_MSGOUT;
  2474. /*break; */
  2475. }
  2476. msgin_reject(acb, srb);
  2477. }
  2478. /* Clear counter and MsgIn state */
  2479. srb->state &= ~SRB_MSGIN;
  2480. acb->msg_len = 0;
  2481. }
  2482. *pscsi_status = PH_BUS_FREE;
  2483. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important ... you know! */
  2484. DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
  2485. }
  2486. static void msgin_phase1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  2487. u16 *pscsi_status)
  2488. {
  2489. dprintkdbg(DBG_0, "msgin_phase1: (0x%p)\n", srb->cmd);
  2490. clear_fifo(acb, "msgin_phase1");
  2491. DC395x_write32(acb, TRM_S1040_SCSI_COUNTER, 1);
  2492. if (!(srb->state & SRB_MSGIN)) {
  2493. srb->state &= ~SRB_DISCONNECT;
  2494. srb->state |= SRB_MSGIN;
  2495. }
  2496. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
  2497. /* SCSI command */
  2498. DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_FIFO_IN);
  2499. }
  2500. static void nop0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  2501. u16 *pscsi_status)
  2502. {
  2503. }
  2504. static void nop1(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
  2505. u16 *pscsi_status)
  2506. {
  2507. }
  2508. static void set_xfer_rate(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb)
  2509. {
  2510. struct DeviceCtlBlk *i;
  2511. /* set all lun device's period, offset */
  2512. if (dcb->identify_msg & 0x07)
  2513. return;
  2514. if (acb->scan_devices) {
  2515. current_sync_offset = dcb->sync_offset;
  2516. return;
  2517. }
  2518. list_for_each_entry(i, &acb->dcb_list, list)
  2519. if (i->target_id == dcb->target_id) {
  2520. i->sync_period = dcb->sync_period;
  2521. i->sync_offset = dcb->sync_offset;
  2522. i->sync_mode = dcb->sync_mode;
  2523. i->min_nego_period = dcb->min_nego_period;
  2524. }
  2525. }
  2526. static void disconnect(struct AdapterCtlBlk *acb)
  2527. {
  2528. struct DeviceCtlBlk *dcb = acb->active_dcb;
  2529. struct ScsiReqBlk *srb;
  2530. if (!dcb) {
  2531. dprintkl(KERN_ERR, "disconnect: No such device\n");
  2532. udelay(500);
  2533. /* Suspend queue for a while */
  2534. acb->last_reset =
  2535. jiffies + HZ / 2 +
  2536. HZ * acb->eeprom.delay_time;
  2537. clear_fifo(acb, "disconnectEx");
  2538. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
  2539. return;
  2540. }
  2541. srb = dcb->active_srb;
  2542. acb->active_dcb = NULL;
  2543. dprintkdbg(DBG_0, "disconnect: (0x%p)\n", srb->cmd);
  2544. srb->scsi_phase = PH_BUS_FREE; /* initial phase */
  2545. clear_fifo(acb, "disconnect");
  2546. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT);
  2547. if (srb->state & SRB_UNEXPECT_RESEL) {
  2548. dprintkl(KERN_ERR,
  2549. "disconnect: Unexpected reselection <%02i-%i>\n",
  2550. dcb->target_id, dcb->target_lun);
  2551. srb->state = 0;
  2552. waiting_process_next(acb);
  2553. } else if (srb->state & SRB_ABORT_SENT) {
  2554. dcb->flag &= ~ABORT_DEV_;
  2555. acb->last_reset = jiffies + HZ / 2 + 1;
  2556. dprintkl(KERN_ERR, "disconnect: SRB_ABORT_SENT\n");
  2557. doing_srb_done(acb, DID_ABORT, srb->cmd, 1);
  2558. waiting_process_next(acb);
  2559. } else {
  2560. if ((srb->state & (SRB_START_ + SRB_MSGOUT))
  2561. || !(srb->
  2562. state & (SRB_DISCONNECT | SRB_COMPLETED))) {
  2563. /*
  2564. * Selection time out
  2565. * SRB_START_ || SRB_MSGOUT || (!SRB_DISCONNECT && !SRB_COMPLETED)
  2566. */
  2567. /* Unexp. Disc / Sel Timeout */
  2568. if (srb->state != SRB_START_
  2569. && srb->state != SRB_MSGOUT) {
  2570. srb->state = SRB_READY;
  2571. dprintkl(KERN_DEBUG,
  2572. "disconnect: (0x%p) Unexpected\n",
  2573. srb->cmd);
  2574. srb->target_status = SCSI_STAT_SEL_TIMEOUT;
  2575. goto disc1;
  2576. } else {
  2577. /* Normal selection timeout */
  2578. dprintkdbg(DBG_KG, "disconnect: (0x%p) "
  2579. "<%02i-%i> SelTO\n", srb->cmd,
  2580. dcb->target_id, dcb->target_lun);
  2581. if (srb->retry_count++ > DC395x_MAX_RETRIES
  2582. || acb->scan_devices) {
  2583. srb->target_status =
  2584. SCSI_STAT_SEL_TIMEOUT;
  2585. goto disc1;
  2586. }
  2587. free_tag(dcb, srb);
  2588. list_move(&srb->list, &dcb->srb_waiting_list);
  2589. dprintkdbg(DBG_KG,
  2590. "disconnect: (0x%p) Retry\n",
  2591. srb->cmd);
  2592. waiting_set_timer(acb, HZ / 20);
  2593. }
  2594. } else if (srb->state & SRB_DISCONNECT) {
  2595. u8 bval = DC395x_read8(acb, TRM_S1040_SCSI_SIGNAL);
  2596. /*
  2597. * SRB_DISCONNECT (This is what we expect!)
  2598. */
  2599. if (bval & 0x40) {
  2600. dprintkdbg(DBG_0, "disconnect: SCSI bus stat "
  2601. " 0x%02x: ACK set! Other controllers?\n",
  2602. bval);
  2603. /* It could come from another initiator, therefore don't do much ! */
  2604. } else
  2605. waiting_process_next(acb);
  2606. } else if (srb->state & SRB_COMPLETED) {
  2607. disc1:
  2608. /*
  2609. ** SRB_COMPLETED
  2610. */
  2611. free_tag(dcb, srb);
  2612. dcb->active_srb = NULL;
  2613. srb->state = SRB_FREE;
  2614. srb_done(acb, dcb, srb);
  2615. }
  2616. }
  2617. }
  2618. static void reselect(struct AdapterCtlBlk *acb)
  2619. {
  2620. struct DeviceCtlBlk *dcb = acb->active_dcb;
  2621. struct ScsiReqBlk *srb = NULL;
  2622. u16 rsel_tar_lun_id;
  2623. u8 id, lun;
  2624. dprintkdbg(DBG_0, "reselect: acb=%p\n", acb);
  2625. clear_fifo(acb, "reselect");
  2626. /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT | DO_DATALATCH); */
  2627. /* Read Reselected Target ID and LUN */
  2628. rsel_tar_lun_id = DC395x_read16(acb, TRM_S1040_SCSI_TARGETID);
  2629. if (dcb) { /* Arbitration lost but Reselection win */
  2630. srb = dcb->active_srb;
  2631. if (!srb) {
  2632. dprintkl(KERN_DEBUG, "reselect: Arb lost Resel won, "
  2633. "but active_srb == NULL\n");
  2634. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
  2635. return;
  2636. }
  2637. /* Why the if ? */
  2638. if (!acb->scan_devices) {
  2639. dprintkdbg(DBG_KG, "reselect: (0x%p) <%02i-%i> "
  2640. "Arb lost but Resel win rsel=%i stat=0x%04x\n",
  2641. srb->cmd, dcb->target_id,
  2642. dcb->target_lun, rsel_tar_lun_id,
  2643. DC395x_read16(acb, TRM_S1040_SCSI_STATUS));
  2644. /*srb->state |= SRB_DISCONNECT; */
  2645. srb->state = SRB_READY;
  2646. free_tag(dcb, srb);
  2647. list_move(&srb->list, &dcb->srb_waiting_list);
  2648. waiting_set_timer(acb, HZ / 20);
  2649. /* return; */
  2650. }
  2651. }
  2652. /* Read Reselected Target Id and LUN */
  2653. if (!(rsel_tar_lun_id & (IDENTIFY_BASE << 8)))
  2654. dprintkl(KERN_DEBUG, "reselect: Expects identify msg. "
  2655. "Got %i!\n", rsel_tar_lun_id);
  2656. id = rsel_tar_lun_id & 0xff;
  2657. lun = (rsel_tar_lun_id >> 8) & 7;
  2658. dcb = find_dcb(acb, id, lun);
  2659. if (!dcb) {
  2660. dprintkl(KERN_ERR, "reselect: From non existent device "
  2661. "<%02i-%i>\n", id, lun);
  2662. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
  2663. return;
  2664. }
  2665. acb->active_dcb = dcb;
  2666. if (!(dcb->dev_mode & NTC_DO_DISCONNECT))
  2667. dprintkl(KERN_DEBUG, "reselect: in spite of forbidden "
  2668. "disconnection? <%02i-%i>\n",
  2669. dcb->target_id, dcb->target_lun);
  2670. if (dcb->sync_mode & EN_TAG_QUEUEING) {
  2671. srb = acb->tmp_srb;
  2672. dcb->active_srb = srb;
  2673. } else {
  2674. /* There can be only one! */
  2675. srb = dcb->active_srb;
  2676. if (!srb || !(srb->state & SRB_DISCONNECT)) {
  2677. /*
  2678. * abort command
  2679. */
  2680. dprintkl(KERN_DEBUG,
  2681. "reselect: w/o disconnected cmds <%02i-%i>\n",
  2682. dcb->target_id, dcb->target_lun);
  2683. srb = acb->tmp_srb;
  2684. srb->state = SRB_UNEXPECT_RESEL;
  2685. dcb->active_srb = srb;
  2686. enable_msgout_abort(acb, srb);
  2687. } else {
  2688. if (dcb->flag & ABORT_DEV_) {
  2689. /*srb->state = SRB_ABORT_SENT; */
  2690. enable_msgout_abort(acb, srb);
  2691. } else
  2692. srb->state = SRB_DATA_XFER;
  2693. }
  2694. }
  2695. srb->scsi_phase = PH_BUS_FREE; /* initial phase */
  2696. /* Program HA ID, target ID, period and offset */
  2697. dprintkdbg(DBG_0, "reselect: select <%i>\n", dcb->target_id);
  2698. DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id); /* host ID */
  2699. DC395x_write8(acb, TRM_S1040_SCSI_TARGETID, dcb->target_id); /* target ID */
  2700. DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, dcb->sync_offset); /* offset */
  2701. DC395x_write8(acb, TRM_S1040_SCSI_SYNC, dcb->sync_period); /* sync period, wide */
  2702. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_DATALATCH); /* it's important for atn stop */
  2703. /* SCSI command */
  2704. DC395x_write8(acb, TRM_S1040_SCSI_COMMAND, SCMD_MSGACCEPT);
  2705. }
  2706. static inline u8 tagq_blacklist(char *name)
  2707. {
  2708. #ifndef DC395x_NO_TAGQ
  2709. #if 0
  2710. u8 i;
  2711. for (i = 0; i < BADDEVCNT; i++)
  2712. if (memcmp(name, DC395x_baddevname1[i], 28) == 0)
  2713. return 1;
  2714. #endif
  2715. return 0;
  2716. #else
  2717. return 1;
  2718. #endif
  2719. }
  2720. static void disc_tagq_set(struct DeviceCtlBlk *dcb, struct ScsiInqData *ptr)
  2721. {
  2722. /* Check for SCSI format (ANSI and Response data format) */
  2723. if ((ptr->Vers & 0x07) >= 2 || (ptr->RDF & 0x0F) == 2) {
  2724. if ((ptr->Flags & SCSI_INQ_CMDQUEUE)
  2725. && (dcb->dev_mode & NTC_DO_TAG_QUEUEING) &&
  2726. /*(dcb->dev_mode & NTC_DO_DISCONNECT) */
  2727. /* ((dcb->dev_type == TYPE_DISK)
  2728. || (dcb->dev_type == TYPE_MOD)) && */
  2729. !tagq_blacklist(((char *)ptr) + 8)) {
  2730. if (dcb->max_command == 1)
  2731. dcb->max_command =
  2732. dcb->acb->tag_max_num;
  2733. dcb->sync_mode |= EN_TAG_QUEUEING;
  2734. /*dcb->tag_mask = 0; */
  2735. } else
  2736. dcb->max_command = 1;
  2737. }
  2738. }
  2739. static void add_dev(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
  2740. struct ScsiInqData *ptr)
  2741. {
  2742. u8 bval1 = ptr->DevType & SCSI_DEVTYPE;
  2743. dcb->dev_type = bval1;
  2744. /* if (bval1 == TYPE_DISK || bval1 == TYPE_MOD) */
  2745. disc_tagq_set(dcb, ptr);
  2746. }
  2747. /* unmap mapped pci regions from SRB */
  2748. static void pci_unmap_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
  2749. {
  2750. struct scsi_cmnd *cmd = srb->cmd;
  2751. enum dma_data_direction dir = cmd->sc_data_direction;
  2752. if (scsi_sg_count(cmd) && dir != DMA_NONE) {
  2753. /* unmap DC395x SG list */
  2754. dprintkdbg(DBG_SG, "pci_unmap_srb: list=%08x(%05x)\n",
  2755. srb->sg_bus_addr, SEGMENTX_LEN);
  2756. dma_unmap_single(&acb->dev->dev, srb->sg_bus_addr, SEGMENTX_LEN,
  2757. DMA_TO_DEVICE);
  2758. dprintkdbg(DBG_SG, "pci_unmap_srb: segs=%i buffer=%p\n",
  2759. scsi_sg_count(cmd), scsi_bufflen(cmd));
  2760. /* unmap the sg segments */
  2761. scsi_dma_unmap(cmd);
  2762. }
  2763. }
  2764. /* unmap mapped pci sense buffer from SRB */
  2765. static void pci_unmap_srb_sense(struct AdapterCtlBlk *acb,
  2766. struct ScsiReqBlk *srb)
  2767. {
  2768. if (!(srb->flag & AUTO_REQSENSE))
  2769. return;
  2770. /* Unmap sense buffer */
  2771. dprintkdbg(DBG_SG, "pci_unmap_srb_sense: buffer=%08x\n",
  2772. srb->segment_x[0].address);
  2773. dma_unmap_single(&acb->dev->dev, srb->segment_x[0].address,
  2774. srb->segment_x[0].length, DMA_FROM_DEVICE);
  2775. /* Restore SG stuff */
  2776. srb->total_xfer_length = srb->xferred;
  2777. srb->segment_x[0].address =
  2778. srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address;
  2779. srb->segment_x[0].length =
  2780. srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length;
  2781. }
  2782. /*
  2783. * Complete execution of a SCSI command
  2784. * Signal completion to the generic SCSI driver
  2785. */
  2786. static void srb_done(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
  2787. struct ScsiReqBlk *srb)
  2788. {
  2789. u8 tempcnt, status;
  2790. struct scsi_cmnd *cmd = srb->cmd;
  2791. enum dma_data_direction dir = cmd->sc_data_direction;
  2792. int ckc_only = 1;
  2793. dprintkdbg(DBG_1, "srb_done: (0x%p) <%02i-%i>\n", srb->cmd,
  2794. srb->cmd->device->id, (u8)srb->cmd->device->lun);
  2795. dprintkdbg(DBG_SG, "srb_done: srb=%p sg=%i(%i/%i) buf=%p\n",
  2796. srb, scsi_sg_count(cmd), srb->sg_index, srb->sg_count,
  2797. scsi_sgtalbe(cmd));
  2798. status = srb->target_status;
  2799. set_host_byte(cmd, DID_OK);
  2800. set_status_byte(cmd, SAM_STAT_GOOD);
  2801. if (srb->flag & AUTO_REQSENSE) {
  2802. dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE1\n");
  2803. pci_unmap_srb_sense(acb, srb);
  2804. /*
  2805. ** target status..........................
  2806. */
  2807. srb->flag &= ~AUTO_REQSENSE;
  2808. srb->adapter_status = 0;
  2809. srb->target_status = SAM_STAT_CHECK_CONDITION;
  2810. if (debug_enabled(DBG_1)) {
  2811. switch (cmd->sense_buffer[2] & 0x0f) {
  2812. case NOT_READY:
  2813. dprintkl(KERN_DEBUG,
  2814. "ReqSense: NOT_READY cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
  2815. cmd->cmnd[0], dcb->target_id,
  2816. dcb->target_lun, status, acb->scan_devices);
  2817. break;
  2818. case UNIT_ATTENTION:
  2819. dprintkl(KERN_DEBUG,
  2820. "ReqSense: UNIT_ATTENTION cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
  2821. cmd->cmnd[0], dcb->target_id,
  2822. dcb->target_lun, status, acb->scan_devices);
  2823. break;
  2824. case ILLEGAL_REQUEST:
  2825. dprintkl(KERN_DEBUG,
  2826. "ReqSense: ILLEGAL_REQUEST cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
  2827. cmd->cmnd[0], dcb->target_id,
  2828. dcb->target_lun, status, acb->scan_devices);
  2829. break;
  2830. case MEDIUM_ERROR:
  2831. dprintkl(KERN_DEBUG,
  2832. "ReqSense: MEDIUM_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
  2833. cmd->cmnd[0], dcb->target_id,
  2834. dcb->target_lun, status, acb->scan_devices);
  2835. break;
  2836. case HARDWARE_ERROR:
  2837. dprintkl(KERN_DEBUG,
  2838. "ReqSense: HARDWARE_ERROR cmnd=0x%02x <%02i-%i> stat=%i scan=%i ",
  2839. cmd->cmnd[0], dcb->target_id,
  2840. dcb->target_lun, status, acb->scan_devices);
  2841. break;
  2842. }
  2843. if (cmd->sense_buffer[7] >= 6)
  2844. printk("sense=0x%02x ASC=0x%02x ASCQ=0x%02x "
  2845. "(0x%08x 0x%08x)\n",
  2846. cmd->sense_buffer[2], cmd->sense_buffer[12],
  2847. cmd->sense_buffer[13],
  2848. *((unsigned int *)(cmd->sense_buffer + 3)),
  2849. *((unsigned int *)(cmd->sense_buffer + 8)));
  2850. else
  2851. printk("sense=0x%02x No ASC/ASCQ (0x%08x)\n",
  2852. cmd->sense_buffer[2],
  2853. *((unsigned int *)(cmd->sense_buffer + 3)));
  2854. }
  2855. if (status == SAM_STAT_CHECK_CONDITION) {
  2856. set_host_byte(cmd, DID_BAD_TARGET);
  2857. goto ckc_e;
  2858. }
  2859. dprintkdbg(DBG_0, "srb_done: AUTO_REQSENSE2\n");
  2860. set_status_byte(cmd, SAM_STAT_CHECK_CONDITION);
  2861. goto ckc_e;
  2862. }
  2863. /*************************************************************/
  2864. if (status) {
  2865. /*
  2866. * target status..........................
  2867. */
  2868. if (status == SAM_STAT_CHECK_CONDITION) {
  2869. request_sense(acb, dcb, srb);
  2870. return;
  2871. } else if (status == SAM_STAT_TASK_SET_FULL) {
  2872. tempcnt = (u8)list_size(&dcb->srb_going_list);
  2873. dprintkl(KERN_INFO, "QUEUE_FULL for dev <%02i-%i> with %i cmnds\n",
  2874. dcb->target_id, dcb->target_lun, tempcnt);
  2875. if (tempcnt > 1)
  2876. tempcnt--;
  2877. dcb->max_command = tempcnt;
  2878. free_tag(dcb, srb);
  2879. list_move(&srb->list, &dcb->srb_waiting_list);
  2880. waiting_set_timer(acb, HZ / 20);
  2881. srb->adapter_status = 0;
  2882. srb->target_status = 0;
  2883. return;
  2884. } else if (status == SCSI_STAT_SEL_TIMEOUT) {
  2885. srb->adapter_status = H_SEL_TIMEOUT;
  2886. srb->target_status = 0;
  2887. set_host_byte(cmd, DID_NO_CONNECT);
  2888. } else {
  2889. srb->adapter_status = 0;
  2890. set_host_byte(cmd, DID_ERROR);
  2891. set_status_byte(cmd, status);
  2892. }
  2893. } else {
  2894. /*
  2895. ** process initiator status..........................
  2896. */
  2897. status = srb->adapter_status;
  2898. if (status & H_OVER_UNDER_RUN) {
  2899. srb->target_status = 0;
  2900. scsi_msg_to_host_byte(cmd, srb->end_message);
  2901. } else if (srb->status & PARITY_ERROR) {
  2902. set_host_byte(cmd, DID_PARITY);
  2903. } else { /* No error */
  2904. srb->adapter_status = 0;
  2905. srb->target_status = 0;
  2906. }
  2907. }
  2908. ckc_only = 0;
  2909. /* Check Error Conditions */
  2910. ckc_e:
  2911. pci_unmap_srb(acb, srb);
  2912. if (cmd->cmnd[0] == INQUIRY) {
  2913. unsigned char *base = NULL;
  2914. struct ScsiInqData *ptr;
  2915. unsigned long flags = 0;
  2916. struct scatterlist* sg = scsi_sglist(cmd);
  2917. size_t offset = 0, len = sizeof(struct ScsiInqData);
  2918. local_irq_save(flags);
  2919. base = scsi_kmap_atomic_sg(sg, scsi_sg_count(cmd), &offset, &len);
  2920. ptr = (struct ScsiInqData *)(base + offset);
  2921. if (!ckc_only && get_host_byte(cmd) == DID_OK
  2922. && cmd->cmnd[2] == 0 && scsi_bufflen(cmd) >= 8
  2923. && dir != DMA_NONE && ptr && (ptr->Vers & 0x07) >= 2)
  2924. dcb->inquiry7 = ptr->Flags;
  2925. /*if( srb->cmd->cmnd[0] == INQUIRY && */
  2926. /* (host_byte(cmd->result) == DID_OK || status_byte(cmd->result) & CHECK_CONDITION) ) */
  2927. if ((get_host_byte(cmd) == DID_OK) ||
  2928. (get_status_byte(cmd) == SAM_STAT_CHECK_CONDITION)) {
  2929. if (!dcb->init_tcq_flag) {
  2930. add_dev(acb, dcb, ptr);
  2931. dcb->init_tcq_flag = 1;
  2932. }
  2933. }
  2934. scsi_kunmap_atomic_sg(base);
  2935. local_irq_restore(flags);
  2936. }
  2937. /* Here is the info for Doug Gilbert's sg3 ... */
  2938. scsi_set_resid(cmd, srb->total_xfer_length);
  2939. if (debug_enabled(DBG_KG)) {
  2940. if (srb->total_xfer_length)
  2941. dprintkdbg(DBG_KG, "srb_done: (0x%p) <%02i-%i> "
  2942. "cmnd=0x%02x Missed %i bytes\n",
  2943. cmd, cmd->device->id, (u8)cmd->device->lun,
  2944. cmd->cmnd[0], srb->total_xfer_length);
  2945. }
  2946. if (srb != acb->tmp_srb) {
  2947. /* Add to free list */
  2948. dprintkdbg(DBG_0, "srb_done: (0x%p) done result=0x%08x\n",
  2949. cmd, cmd->result);
  2950. list_move_tail(&srb->list, &acb->srb_free_list);
  2951. } else {
  2952. dprintkl(KERN_ERR, "srb_done: ERROR! Completed cmd with tmp_srb\n");
  2953. }
  2954. scsi_done(cmd);
  2955. waiting_process_next(acb);
  2956. }
  2957. /* abort all cmds in our queues */
  2958. static void doing_srb_done(struct AdapterCtlBlk *acb, u8 did_flag,
  2959. struct scsi_cmnd *cmd, u8 force)
  2960. {
  2961. struct DeviceCtlBlk *dcb;
  2962. dprintkl(KERN_INFO, "doing_srb_done: pids ");
  2963. list_for_each_entry(dcb, &acb->dcb_list, list) {
  2964. struct ScsiReqBlk *srb;
  2965. struct ScsiReqBlk *tmp;
  2966. struct scsi_cmnd *p;
  2967. list_for_each_entry_safe(srb, tmp, &dcb->srb_going_list, list) {
  2968. p = srb->cmd;
  2969. printk("G:%p(%02i-%i) ", p,
  2970. p->device->id, (u8)p->device->lun);
  2971. list_del(&srb->list);
  2972. free_tag(dcb, srb);
  2973. list_add_tail(&srb->list, &acb->srb_free_list);
  2974. set_host_byte(p, did_flag);
  2975. set_status_byte(p, SAM_STAT_GOOD);
  2976. pci_unmap_srb_sense(acb, srb);
  2977. pci_unmap_srb(acb, srb);
  2978. if (force) {
  2979. /* For new EH, we normally don't need to give commands back,
  2980. * as they all complete or all time out */
  2981. scsi_done(p);
  2982. }
  2983. }
  2984. if (!list_empty(&dcb->srb_going_list))
  2985. dprintkl(KERN_DEBUG,
  2986. "How could the ML send cmnds to the Going queue? <%02i-%i>\n",
  2987. dcb->target_id, dcb->target_lun);
  2988. if (dcb->tag_mask)
  2989. dprintkl(KERN_DEBUG,
  2990. "tag_mask for <%02i-%i> should be empty, is %08x!\n",
  2991. dcb->target_id, dcb->target_lun,
  2992. dcb->tag_mask);
  2993. /* Waiting queue */
  2994. list_for_each_entry_safe(srb, tmp, &dcb->srb_waiting_list, list) {
  2995. p = srb->cmd;
  2996. printk("W:%p<%02i-%i>", p, p->device->id,
  2997. (u8)p->device->lun);
  2998. list_move_tail(&srb->list, &acb->srb_free_list);
  2999. set_host_byte(p, did_flag);
  3000. set_status_byte(p, SAM_STAT_GOOD);
  3001. pci_unmap_srb_sense(acb, srb);
  3002. pci_unmap_srb(acb, srb);
  3003. if (force) {
  3004. /* For new EH, we normally don't need to give commands back,
  3005. * as they all complete or all time out */
  3006. scsi_done(cmd);
  3007. }
  3008. }
  3009. if (!list_empty(&dcb->srb_waiting_list))
  3010. dprintkl(KERN_DEBUG, "ML queued %i cmnds again to <%02i-%i>\n",
  3011. list_size(&dcb->srb_waiting_list), dcb->target_id,
  3012. dcb->target_lun);
  3013. dcb->flag &= ~ABORT_DEV_;
  3014. }
  3015. printk("\n");
  3016. }
  3017. static void reset_scsi_bus(struct AdapterCtlBlk *acb)
  3018. {
  3019. dprintkdbg(DBG_0, "reset_scsi_bus: acb=%p\n", acb);
  3020. acb->acb_flag |= RESET_DEV; /* RESET_DETECT, RESET_DONE, RESET_DEV */
  3021. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
  3022. while (!(DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET))
  3023. /* nothing */;
  3024. }
  3025. static void set_basic_config(struct AdapterCtlBlk *acb)
  3026. {
  3027. u8 bval;
  3028. u16 wval;
  3029. DC395x_write8(acb, TRM_S1040_SCSI_TIMEOUT, acb->sel_timeout);
  3030. if (acb->config & HCC_PARITY)
  3031. bval = PHASELATCH | INITIATOR | BLOCKRST | PARITYCHECK;
  3032. else
  3033. bval = PHASELATCH | INITIATOR | BLOCKRST;
  3034. DC395x_write8(acb, TRM_S1040_SCSI_CONFIG0, bval);
  3035. /* program configuration 1: Act_Neg (+ Act_Neg_Enh? + Fast_Filter? + DataDis?) */
  3036. DC395x_write8(acb, TRM_S1040_SCSI_CONFIG1, 0x03); /* was 0x13: default */
  3037. /* program Host ID */
  3038. DC395x_write8(acb, TRM_S1040_SCSI_HOSTID, acb->scsi_host->this_id);
  3039. /* set ansynchronous transfer */
  3040. DC395x_write8(acb, TRM_S1040_SCSI_OFFSET, 0x00);
  3041. /* Turn LED control off */
  3042. wval = DC395x_read16(acb, TRM_S1040_GEN_CONTROL) & 0x7F;
  3043. DC395x_write16(acb, TRM_S1040_GEN_CONTROL, wval);
  3044. /* DMA config */
  3045. wval = DC395x_read16(acb, TRM_S1040_DMA_CONFIG) & ~DMA_FIFO_CTRL;
  3046. wval |=
  3047. DMA_FIFO_HALF_HALF | DMA_ENHANCE /*| DMA_MEM_MULTI_READ */ ;
  3048. DC395x_write16(acb, TRM_S1040_DMA_CONFIG, wval);
  3049. /* Clear pending interrupt status */
  3050. DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
  3051. /* Enable SCSI interrupt */
  3052. DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x7F);
  3053. DC395x_write8(acb, TRM_S1040_DMA_INTEN, EN_SCSIINTR | EN_DMAXFERERROR
  3054. /*| EN_DMAXFERABORT | EN_DMAXFERCOMP | EN_FORCEDMACOMP */
  3055. );
  3056. }
  3057. static void scsi_reset_detect(struct AdapterCtlBlk *acb)
  3058. {
  3059. dprintkl(KERN_INFO, "scsi_reset_detect: acb=%p\n", acb);
  3060. /* delay half a second */
  3061. if (timer_pending(&acb->waiting_timer))
  3062. del_timer(&acb->waiting_timer);
  3063. DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
  3064. DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
  3065. /*DC395x_write8(acb, TRM_S1040_DMA_CONTROL,STOPDMAXFER); */
  3066. udelay(500);
  3067. /* Maybe we locked up the bus? Then lets wait even longer ... */
  3068. acb->last_reset =
  3069. jiffies + 5 * HZ / 2 +
  3070. HZ * acb->eeprom.delay_time;
  3071. clear_fifo(acb, "scsi_reset_detect");
  3072. set_basic_config(acb);
  3073. /*1.25 */
  3074. /*DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_HWRESELECT); */
  3075. if (acb->acb_flag & RESET_DEV) { /* RESET_DETECT, RESET_DONE, RESET_DEV */
  3076. acb->acb_flag |= RESET_DONE;
  3077. } else {
  3078. acb->acb_flag |= RESET_DETECT;
  3079. reset_dev_param(acb);
  3080. doing_srb_done(acb, DID_RESET, NULL, 1);
  3081. /*DC395x_RecoverSRB( acb ); */
  3082. acb->active_dcb = NULL;
  3083. acb->acb_flag = 0;
  3084. waiting_process_next(acb);
  3085. }
  3086. }
  3087. static void request_sense(struct AdapterCtlBlk *acb, struct DeviceCtlBlk *dcb,
  3088. struct ScsiReqBlk *srb)
  3089. {
  3090. struct scsi_cmnd *cmd = srb->cmd;
  3091. dprintkdbg(DBG_1, "request_sense: (0x%p) <%02i-%i>\n",
  3092. cmd, cmd->device->id, (u8)cmd->device->lun);
  3093. srb->flag |= AUTO_REQSENSE;
  3094. srb->adapter_status = 0;
  3095. srb->target_status = 0;
  3096. /* KG: Can this prevent crap sense data ? */
  3097. memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  3098. /* Save some data */
  3099. srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].address =
  3100. srb->segment_x[0].address;
  3101. srb->segment_x[DC395x_MAX_SG_LISTENTRY - 1].length =
  3102. srb->segment_x[0].length;
  3103. srb->xferred = srb->total_xfer_length;
  3104. /* srb->segment_x : a one entry of S/G list table */
  3105. srb->total_xfer_length = SCSI_SENSE_BUFFERSIZE;
  3106. srb->segment_x[0].length = SCSI_SENSE_BUFFERSIZE;
  3107. /* Map sense buffer */
  3108. srb->segment_x[0].address = dma_map_single(&acb->dev->dev,
  3109. cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
  3110. DMA_FROM_DEVICE);
  3111. dprintkdbg(DBG_SG, "request_sense: map buffer %p->%08x(%05x)\n",
  3112. cmd->sense_buffer, srb->segment_x[0].address,
  3113. SCSI_SENSE_BUFFERSIZE);
  3114. srb->sg_count = 1;
  3115. srb->sg_index = 0;
  3116. if (start_scsi(acb, dcb, srb)) { /* Should only happen, if sb. else grabs the bus */
  3117. dprintkl(KERN_DEBUG,
  3118. "request_sense: (0x%p) failed <%02i-%i>\n",
  3119. srb->cmd, dcb->target_id, dcb->target_lun);
  3120. list_move(&srb->list, &dcb->srb_waiting_list);
  3121. waiting_set_timer(acb, HZ / 100);
  3122. }
  3123. }
  3124. /**
  3125. * device_alloc - Allocate a new device instance. This create the
  3126. * devices instance and sets up all the data items. The adapter
  3127. * instance is required to obtain confiuration information for this
  3128. * device. This does *not* add this device to the adapters device
  3129. * list.
  3130. *
  3131. * @acb: The adapter to obtain configuration information from.
  3132. * @target: The target for the new device.
  3133. * @lun: The lun for the new device.
  3134. *
  3135. * Return the new device if successful or NULL on failure.
  3136. **/
  3137. static struct DeviceCtlBlk *device_alloc(struct AdapterCtlBlk *acb,
  3138. u8 target, u8 lun)
  3139. {
  3140. struct NvRamType *eeprom = &acb->eeprom;
  3141. u8 period_index = eeprom->target[target].period & 0x07;
  3142. struct DeviceCtlBlk *dcb;
  3143. dcb = kmalloc(sizeof(struct DeviceCtlBlk), GFP_ATOMIC);
  3144. dprintkdbg(DBG_0, "device_alloc: <%02i-%i>\n", target, lun);
  3145. if (!dcb)
  3146. return NULL;
  3147. dcb->acb = NULL;
  3148. INIT_LIST_HEAD(&dcb->srb_going_list);
  3149. INIT_LIST_HEAD(&dcb->srb_waiting_list);
  3150. dcb->active_srb = NULL;
  3151. dcb->tag_mask = 0;
  3152. dcb->max_command = 1;
  3153. dcb->target_id = target;
  3154. dcb->target_lun = lun;
  3155. dcb->dev_mode = eeprom->target[target].cfg0;
  3156. #ifndef DC395x_NO_DISCONNECT
  3157. dcb->identify_msg =
  3158. IDENTIFY(dcb->dev_mode & NTC_DO_DISCONNECT, lun);
  3159. #else
  3160. dcb->identify_msg = IDENTIFY(0, lun);
  3161. #endif
  3162. dcb->inquiry7 = 0;
  3163. dcb->sync_mode = 0;
  3164. dcb->min_nego_period = clock_period[period_index];
  3165. dcb->sync_period = 0;
  3166. dcb->sync_offset = 0;
  3167. dcb->flag = 0;
  3168. #ifndef DC395x_NO_WIDE
  3169. if ((dcb->dev_mode & NTC_DO_WIDE_NEGO)
  3170. && (acb->config & HCC_WIDE_CARD))
  3171. dcb->sync_mode |= WIDE_NEGO_ENABLE;
  3172. #endif
  3173. #ifndef DC395x_NO_SYNC
  3174. if (dcb->dev_mode & NTC_DO_SYNC_NEGO)
  3175. if (!(lun) || current_sync_offset)
  3176. dcb->sync_mode |= SYNC_NEGO_ENABLE;
  3177. #endif
  3178. if (dcb->target_lun != 0) {
  3179. /* Copy settings */
  3180. struct DeviceCtlBlk *p = NULL, *iter;
  3181. list_for_each_entry(iter, &acb->dcb_list, list)
  3182. if (iter->target_id == dcb->target_id) {
  3183. p = iter;
  3184. break;
  3185. }
  3186. if (!p) {
  3187. kfree(dcb);
  3188. return NULL;
  3189. }
  3190. dprintkdbg(DBG_1,
  3191. "device_alloc: <%02i-%i> copy from <%02i-%i>\n",
  3192. dcb->target_id, dcb->target_lun,
  3193. p->target_id, p->target_lun);
  3194. dcb->sync_mode = p->sync_mode;
  3195. dcb->sync_period = p->sync_period;
  3196. dcb->min_nego_period = p->min_nego_period;
  3197. dcb->sync_offset = p->sync_offset;
  3198. dcb->inquiry7 = p->inquiry7;
  3199. }
  3200. return dcb;
  3201. }
  3202. /**
  3203. * adapter_add_device - Adds the device instance to the adaptor instance.
  3204. *
  3205. * @acb: The adapter device to be updated
  3206. * @dcb: A newly created and initialised device instance to add.
  3207. **/
  3208. static void adapter_add_device(struct AdapterCtlBlk *acb,
  3209. struct DeviceCtlBlk *dcb)
  3210. {
  3211. /* backpointer to adapter */
  3212. dcb->acb = acb;
  3213. /* set run_robin to this device if it is currently empty */
  3214. if (list_empty(&acb->dcb_list))
  3215. acb->dcb_run_robin = dcb;
  3216. /* add device to list */
  3217. list_add_tail(&dcb->list, &acb->dcb_list);
  3218. /* update device maps */
  3219. acb->dcb_map[dcb->target_id] |= (1 << dcb->target_lun);
  3220. acb->children[dcb->target_id][dcb->target_lun] = dcb;
  3221. }
  3222. /**
  3223. * adapter_remove_device - Removes the device instance from the adaptor
  3224. * instance. The device instance is not check in any way or freed by this.
  3225. * The caller is expected to take care of that. This will simply remove the
  3226. * device from the adapters data strcutures.
  3227. *
  3228. * @acb: The adapter device to be updated
  3229. * @dcb: A device that has previously been added to the adapter.
  3230. **/
  3231. static void adapter_remove_device(struct AdapterCtlBlk *acb,
  3232. struct DeviceCtlBlk *dcb)
  3233. {
  3234. struct DeviceCtlBlk *i;
  3235. struct DeviceCtlBlk *tmp;
  3236. dprintkdbg(DBG_0, "adapter_remove_device: <%02i-%i>\n",
  3237. dcb->target_id, dcb->target_lun);
  3238. /* fix up any pointers to this device that we have in the adapter */
  3239. if (acb->active_dcb == dcb)
  3240. acb->active_dcb = NULL;
  3241. if (acb->dcb_run_robin == dcb)
  3242. acb->dcb_run_robin = dcb_get_next(&acb->dcb_list, dcb);
  3243. /* unlink from list */
  3244. list_for_each_entry_safe(i, tmp, &acb->dcb_list, list)
  3245. if (dcb == i) {
  3246. list_del(&i->list);
  3247. break;
  3248. }
  3249. /* clear map and children */
  3250. acb->dcb_map[dcb->target_id] &= ~(1 << dcb->target_lun);
  3251. acb->children[dcb->target_id][dcb->target_lun] = NULL;
  3252. dcb->acb = NULL;
  3253. }
  3254. /**
  3255. * adapter_remove_and_free_device - Removes a single device from the adapter
  3256. * and then frees the device information.
  3257. *
  3258. * @acb: The adapter device to be updated
  3259. * @dcb: A device that has previously been added to the adapter.
  3260. */
  3261. static void adapter_remove_and_free_device(struct AdapterCtlBlk *acb,
  3262. struct DeviceCtlBlk *dcb)
  3263. {
  3264. if (list_size(&dcb->srb_going_list) > 1) {
  3265. dprintkdbg(DBG_1, "adapter_remove_and_free_device: <%02i-%i> "
  3266. "Won't remove because of %i active requests.\n",
  3267. dcb->target_id, dcb->target_lun,
  3268. list_size(&dcb->srb_going_list));
  3269. return;
  3270. }
  3271. adapter_remove_device(acb, dcb);
  3272. kfree(dcb);
  3273. }
  3274. /**
  3275. * adapter_remove_and_free_all_devices - Removes and frees all of the
  3276. * devices associated with the specified adapter.
  3277. *
  3278. * @acb: The adapter from which all devices should be removed.
  3279. **/
  3280. static void adapter_remove_and_free_all_devices(struct AdapterCtlBlk* acb)
  3281. {
  3282. struct DeviceCtlBlk *dcb;
  3283. struct DeviceCtlBlk *tmp;
  3284. dprintkdbg(DBG_1, "adapter_remove_and_free_all_devices: num=%i\n",
  3285. list_size(&acb->dcb_list));
  3286. list_for_each_entry_safe(dcb, tmp, &acb->dcb_list, list)
  3287. adapter_remove_and_free_device(acb, dcb);
  3288. }
  3289. /**
  3290. * dc395x_slave_alloc - Called by the scsi mid layer to tell us about a new
  3291. * scsi device that we need to deal with. We allocate a new device and then
  3292. * insert that device into the adapters device list.
  3293. *
  3294. * @scsi_device: The new scsi device that we need to handle.
  3295. **/
  3296. static int dc395x_slave_alloc(struct scsi_device *scsi_device)
  3297. {
  3298. struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
  3299. struct DeviceCtlBlk *dcb;
  3300. dcb = device_alloc(acb, scsi_device->id, scsi_device->lun);
  3301. if (!dcb)
  3302. return -ENOMEM;
  3303. adapter_add_device(acb, dcb);
  3304. return 0;
  3305. }
  3306. /**
  3307. * dc395x_slave_destroy - Called by the scsi mid layer to tell us about a
  3308. * device that is going away.
  3309. *
  3310. * @scsi_device: The new scsi device that we need to handle.
  3311. **/
  3312. static void dc395x_slave_destroy(struct scsi_device *scsi_device)
  3313. {
  3314. struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)scsi_device->host->hostdata;
  3315. struct DeviceCtlBlk *dcb = find_dcb(acb, scsi_device->id, scsi_device->lun);
  3316. if (dcb)
  3317. adapter_remove_and_free_device(acb, dcb);
  3318. }
  3319. /**
  3320. * trms1040_wait_30us: wait for 30 us
  3321. *
  3322. * Waits for 30us (using the chip by the looks of it..)
  3323. *
  3324. * @io_port: base I/O address
  3325. **/
  3326. static void trms1040_wait_30us(unsigned long io_port)
  3327. {
  3328. /* ScsiPortStallExecution(30); wait 30 us */
  3329. outb(5, io_port + TRM_S1040_GEN_TIMER);
  3330. while (!(inb(io_port + TRM_S1040_GEN_STATUS) & GTIMEOUT))
  3331. /* nothing */ ;
  3332. }
  3333. /**
  3334. * trms1040_write_cmd - write the secified command and address to
  3335. * chip
  3336. *
  3337. * @io_port: base I/O address
  3338. * @cmd: SB + op code (command) to send
  3339. * @addr: address to send
  3340. **/
  3341. static void trms1040_write_cmd(unsigned long io_port, u8 cmd, u8 addr)
  3342. {
  3343. int i;
  3344. u8 send_data;
  3345. /* program SB + OP code */
  3346. for (i = 0; i < 3; i++, cmd <<= 1) {
  3347. send_data = NVR_SELECT;
  3348. if (cmd & 0x04) /* Start from bit 2 */
  3349. send_data |= NVR_BITOUT;
  3350. outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
  3351. trms1040_wait_30us(io_port);
  3352. outb((send_data | NVR_CLOCK),
  3353. io_port + TRM_S1040_GEN_NVRAM);
  3354. trms1040_wait_30us(io_port);
  3355. }
  3356. /* send address */
  3357. for (i = 0; i < 7; i++, addr <<= 1) {
  3358. send_data = NVR_SELECT;
  3359. if (addr & 0x40) /* Start from bit 6 */
  3360. send_data |= NVR_BITOUT;
  3361. outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
  3362. trms1040_wait_30us(io_port);
  3363. outb((send_data | NVR_CLOCK),
  3364. io_port + TRM_S1040_GEN_NVRAM);
  3365. trms1040_wait_30us(io_port);
  3366. }
  3367. outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
  3368. trms1040_wait_30us(io_port);
  3369. }
  3370. /**
  3371. * trms1040_set_data - store a single byte in the eeprom
  3372. *
  3373. * Called from write all to write a single byte into the SSEEPROM
  3374. * Which is done one bit at a time.
  3375. *
  3376. * @io_port: base I/O address
  3377. * @addr: offset into EEPROM
  3378. * @byte: bytes to write
  3379. **/
  3380. static void trms1040_set_data(unsigned long io_port, u8 addr, u8 byte)
  3381. {
  3382. int i;
  3383. u8 send_data;
  3384. /* Send write command & address */
  3385. trms1040_write_cmd(io_port, 0x05, addr);
  3386. /* Write data */
  3387. for (i = 0; i < 8; i++, byte <<= 1) {
  3388. send_data = NVR_SELECT;
  3389. if (byte & 0x80) /* Start from bit 7 */
  3390. send_data |= NVR_BITOUT;
  3391. outb(send_data, io_port + TRM_S1040_GEN_NVRAM);
  3392. trms1040_wait_30us(io_port);
  3393. outb((send_data | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
  3394. trms1040_wait_30us(io_port);
  3395. }
  3396. outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
  3397. trms1040_wait_30us(io_port);
  3398. /* Disable chip select */
  3399. outb(0, io_port + TRM_S1040_GEN_NVRAM);
  3400. trms1040_wait_30us(io_port);
  3401. outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
  3402. trms1040_wait_30us(io_port);
  3403. /* Wait for write ready */
  3404. while (1) {
  3405. outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
  3406. trms1040_wait_30us(io_port);
  3407. outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
  3408. trms1040_wait_30us(io_port);
  3409. if (inb(io_port + TRM_S1040_GEN_NVRAM) & NVR_BITIN)
  3410. break;
  3411. }
  3412. /* Disable chip select */
  3413. outb(0, io_port + TRM_S1040_GEN_NVRAM);
  3414. }
  3415. /**
  3416. * trms1040_write_all - write 128 bytes to the eeprom
  3417. *
  3418. * Write the supplied 128 bytes to the chips SEEPROM
  3419. *
  3420. * @eeprom: the data to write
  3421. * @io_port: the base io port
  3422. **/
  3423. static void trms1040_write_all(struct NvRamType *eeprom, unsigned long io_port)
  3424. {
  3425. u8 *b_eeprom = (u8 *)eeprom;
  3426. u8 addr;
  3427. /* Enable SEEPROM */
  3428. outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
  3429. io_port + TRM_S1040_GEN_CONTROL);
  3430. /* write enable */
  3431. trms1040_write_cmd(io_port, 0x04, 0xFF);
  3432. outb(0, io_port + TRM_S1040_GEN_NVRAM);
  3433. trms1040_wait_30us(io_port);
  3434. /* write */
  3435. for (addr = 0; addr < 128; addr++, b_eeprom++)
  3436. trms1040_set_data(io_port, addr, *b_eeprom);
  3437. /* write disable */
  3438. trms1040_write_cmd(io_port, 0x04, 0x00);
  3439. outb(0, io_port + TRM_S1040_GEN_NVRAM);
  3440. trms1040_wait_30us(io_port);
  3441. /* Disable SEEPROM */
  3442. outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
  3443. io_port + TRM_S1040_GEN_CONTROL);
  3444. }
  3445. /**
  3446. * trms1040_get_data - get a single byte from the eeprom
  3447. *
  3448. * Called from read all to read a single byte into the SSEEPROM
  3449. * Which is done one bit at a time.
  3450. *
  3451. * @io_port: base I/O address
  3452. * @addr: offset into SEEPROM
  3453. *
  3454. * Returns the byte read.
  3455. **/
  3456. static u8 trms1040_get_data(unsigned long io_port, u8 addr)
  3457. {
  3458. int i;
  3459. u8 read_byte;
  3460. u8 result = 0;
  3461. /* Send read command & address */
  3462. trms1040_write_cmd(io_port, 0x06, addr);
  3463. /* read data */
  3464. for (i = 0; i < 8; i++) {
  3465. outb((NVR_SELECT | NVR_CLOCK), io_port + TRM_S1040_GEN_NVRAM);
  3466. trms1040_wait_30us(io_port);
  3467. outb(NVR_SELECT, io_port + TRM_S1040_GEN_NVRAM);
  3468. /* Get data bit while falling edge */
  3469. read_byte = inb(io_port + TRM_S1040_GEN_NVRAM);
  3470. result <<= 1;
  3471. if (read_byte & NVR_BITIN)
  3472. result |= 1;
  3473. trms1040_wait_30us(io_port);
  3474. }
  3475. /* Disable chip select */
  3476. outb(0, io_port + TRM_S1040_GEN_NVRAM);
  3477. return result;
  3478. }
  3479. /**
  3480. * trms1040_read_all - read all bytes from the eeprom
  3481. *
  3482. * Read the 128 bytes from the SEEPROM.
  3483. *
  3484. * @eeprom: where to store the data
  3485. * @io_port: the base io port
  3486. **/
  3487. static void trms1040_read_all(struct NvRamType *eeprom, unsigned long io_port)
  3488. {
  3489. u8 *b_eeprom = (u8 *)eeprom;
  3490. u8 addr;
  3491. /* Enable SEEPROM */
  3492. outb((inb(io_port + TRM_S1040_GEN_CONTROL) | EN_EEPROM),
  3493. io_port + TRM_S1040_GEN_CONTROL);
  3494. /* read details */
  3495. for (addr = 0; addr < 128; addr++, b_eeprom++)
  3496. *b_eeprom = trms1040_get_data(io_port, addr);
  3497. /* Disable SEEPROM */
  3498. outb((inb(io_port + TRM_S1040_GEN_CONTROL) & ~EN_EEPROM),
  3499. io_port + TRM_S1040_GEN_CONTROL);
  3500. }
  3501. /**
  3502. * check_eeprom - get and check contents of the eeprom
  3503. *
  3504. * Read seeprom 128 bytes into the memory provider in eeprom.
  3505. * Checks the checksum and if it's not correct it uses a set of default
  3506. * values.
  3507. *
  3508. * @eeprom: caller allocated strcuture to read the eeprom data into
  3509. * @io_port: io port to read from
  3510. **/
  3511. static void check_eeprom(struct NvRamType *eeprom, unsigned long io_port)
  3512. {
  3513. u16 *w_eeprom = (u16 *)eeprom;
  3514. u16 w_addr;
  3515. u16 cksum;
  3516. u32 d_addr;
  3517. u32 *d_eeprom;
  3518. trms1040_read_all(eeprom, io_port); /* read eeprom */
  3519. cksum = 0;
  3520. for (w_addr = 0, w_eeprom = (u16 *)eeprom; w_addr < 64;
  3521. w_addr++, w_eeprom++)
  3522. cksum += *w_eeprom;
  3523. if (cksum != 0x1234) {
  3524. /*
  3525. * Checksum is wrong.
  3526. * Load a set of defaults into the eeprom buffer
  3527. */
  3528. dprintkl(KERN_WARNING,
  3529. "EEProm checksum error: using default values and options.\n");
  3530. eeprom->sub_vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
  3531. eeprom->sub_vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
  3532. eeprom->sub_sys_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
  3533. eeprom->sub_sys_id[1] =
  3534. (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
  3535. eeprom->sub_class = 0x00;
  3536. eeprom->vendor_id[0] = (u8)PCI_VENDOR_ID_TEKRAM;
  3537. eeprom->vendor_id[1] = (u8)(PCI_VENDOR_ID_TEKRAM >> 8);
  3538. eeprom->device_id[0] = (u8)PCI_DEVICE_ID_TEKRAM_TRMS1040;
  3539. eeprom->device_id[1] =
  3540. (u8)(PCI_DEVICE_ID_TEKRAM_TRMS1040 >> 8);
  3541. eeprom->reserved = 0x00;
  3542. for (d_addr = 0, d_eeprom = (u32 *)eeprom->target;
  3543. d_addr < 16; d_addr++, d_eeprom++)
  3544. *d_eeprom = 0x00000077; /* cfg3,cfg2,period,cfg0 */
  3545. *d_eeprom++ = 0x04000F07; /* max_tag,delay_time,channel_cfg,scsi_id */
  3546. *d_eeprom++ = 0x00000015; /* reserved1,boot_lun,boot_target,reserved0 */
  3547. for (d_addr = 0; d_addr < 12; d_addr++, d_eeprom++)
  3548. *d_eeprom = 0x00;
  3549. /* Now load defaults (maybe set by boot/module params) */
  3550. set_safe_settings();
  3551. fix_settings();
  3552. eeprom_override(eeprom);
  3553. eeprom->cksum = 0x00;
  3554. for (w_addr = 0, cksum = 0, w_eeprom = (u16 *)eeprom;
  3555. w_addr < 63; w_addr++, w_eeprom++)
  3556. cksum += *w_eeprom;
  3557. *w_eeprom = 0x1234 - cksum;
  3558. trms1040_write_all(eeprom, io_port);
  3559. eeprom->delay_time = cfg_data[CFG_RESET_DELAY].value;
  3560. } else {
  3561. set_safe_settings();
  3562. eeprom_index_to_delay(eeprom);
  3563. eeprom_override(eeprom);
  3564. }
  3565. }
  3566. /**
  3567. * print_eeprom_settings - output the eeprom settings
  3568. * to the kernel log so people can see what they were.
  3569. *
  3570. * @eeprom: The eeprom data strucutre to show details for.
  3571. **/
  3572. static void print_eeprom_settings(struct NvRamType *eeprom)
  3573. {
  3574. dprintkl(KERN_INFO, "Used settings: AdapterID=%02i, Speed=%i(%02i.%01iMHz), dev_mode=0x%02x\n",
  3575. eeprom->scsi_id,
  3576. eeprom->target[0].period,
  3577. clock_speed[eeprom->target[0].period] / 10,
  3578. clock_speed[eeprom->target[0].period] % 10,
  3579. eeprom->target[0].cfg0);
  3580. dprintkl(KERN_INFO, " AdaptMode=0x%02x, Tags=%i(%02i), DelayReset=%is\n",
  3581. eeprom->channel_cfg, eeprom->max_tag,
  3582. 1 << eeprom->max_tag, eeprom->delay_time);
  3583. }
  3584. /* Free SG tables */
  3585. static void adapter_sg_tables_free(struct AdapterCtlBlk *acb)
  3586. {
  3587. int i;
  3588. const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
  3589. for (i = 0; i < DC395x_MAX_SRB_CNT; i += srbs_per_page)
  3590. kfree(acb->srb_array[i].segment_x);
  3591. }
  3592. /*
  3593. * Allocate SG tables; as we have to pci_map them, an SG list (struct SGentry*)
  3594. * should never cross a page boundary */
  3595. static int adapter_sg_tables_alloc(struct AdapterCtlBlk *acb)
  3596. {
  3597. const unsigned mem_needed = (DC395x_MAX_SRB_CNT+1)
  3598. *SEGMENTX_LEN;
  3599. int pages = (mem_needed+(PAGE_SIZE-1))/PAGE_SIZE;
  3600. const unsigned srbs_per_page = PAGE_SIZE/SEGMENTX_LEN;
  3601. int srb_idx = 0;
  3602. unsigned i = 0;
  3603. struct SGentry *ptr;
  3604. for (i = 0; i < DC395x_MAX_SRB_CNT; i++)
  3605. acb->srb_array[i].segment_x = NULL;
  3606. dprintkdbg(DBG_1, "Allocate %i pages for SG tables\n", pages);
  3607. while (pages--) {
  3608. ptr = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3609. if (!ptr) {
  3610. adapter_sg_tables_free(acb);
  3611. return 1;
  3612. }
  3613. dprintkdbg(DBG_1, "Allocate %li bytes at %p for SG segments %i\n",
  3614. PAGE_SIZE, ptr, srb_idx);
  3615. i = 0;
  3616. while (i < srbs_per_page && srb_idx < DC395x_MAX_SRB_CNT)
  3617. acb->srb_array[srb_idx++].segment_x =
  3618. ptr + (i++ * DC395x_MAX_SG_LISTENTRY);
  3619. }
  3620. if (i < srbs_per_page)
  3621. acb->srb.segment_x =
  3622. ptr + (i * DC395x_MAX_SG_LISTENTRY);
  3623. else
  3624. dprintkl(KERN_DEBUG, "No space for tmsrb SG table reserved?!\n");
  3625. return 0;
  3626. }
  3627. /**
  3628. * adapter_print_config - print adapter connection and termination
  3629. * config
  3630. *
  3631. * The io port in the adapter needs to have been set before calling
  3632. * this function.
  3633. *
  3634. * @acb: The adapter to print the information for.
  3635. **/
  3636. static void adapter_print_config(struct AdapterCtlBlk *acb)
  3637. {
  3638. u8 bval;
  3639. bval = DC395x_read8(acb, TRM_S1040_GEN_STATUS);
  3640. dprintkl(KERN_INFO, "%sConnectors: ",
  3641. ((bval & WIDESCSI) ? "(Wide) " : ""));
  3642. if (!(bval & CON5068))
  3643. printk("ext%s ", !(bval & EXT68HIGH) ? "68" : "50");
  3644. if (!(bval & CON68))
  3645. printk("int68%s ", !(bval & INT68HIGH) ? "" : "(50)");
  3646. if (!(bval & CON50))
  3647. printk("int50 ");
  3648. if ((bval & (CON5068 | CON50 | CON68)) ==
  3649. 0 /*(CON5068 | CON50 | CON68) */ )
  3650. printk(" Oops! (All 3?) ");
  3651. bval = DC395x_read8(acb, TRM_S1040_GEN_CONTROL);
  3652. printk(" Termination: ");
  3653. if (bval & DIS_TERM)
  3654. printk("Disabled\n");
  3655. else {
  3656. if (bval & AUTOTERM)
  3657. printk("Auto ");
  3658. if (bval & LOW8TERM)
  3659. printk("Low ");
  3660. if (bval & UP8TERM)
  3661. printk("High ");
  3662. printk("\n");
  3663. }
  3664. }
  3665. /**
  3666. * adapter_init_params - Initialize the various parameters in the
  3667. * adapter structure. Note that the pointer to the scsi_host is set
  3668. * early (when this instance is created) and the io_port and irq
  3669. * values are set later after they have been reserved. This just gets
  3670. * everything set to a good starting position.
  3671. *
  3672. * The eeprom structure in the adapter needs to have been set before
  3673. * calling this function.
  3674. *
  3675. * @acb: The adapter to initialize.
  3676. **/
  3677. static void adapter_init_params(struct AdapterCtlBlk *acb)
  3678. {
  3679. struct NvRamType *eeprom = &acb->eeprom;
  3680. int i;
  3681. /* NOTE: acb->scsi_host is set at scsi_host/acb creation time */
  3682. /* NOTE: acb->io_port_base is set at port registration time */
  3683. /* NOTE: acb->io_port_len is set at port registration time */
  3684. INIT_LIST_HEAD(&acb->dcb_list);
  3685. acb->dcb_run_robin = NULL;
  3686. acb->active_dcb = NULL;
  3687. INIT_LIST_HEAD(&acb->srb_free_list);
  3688. /* temp SRB for Q tag used or abort command used */
  3689. acb->tmp_srb = &acb->srb;
  3690. timer_setup(&acb->waiting_timer, waiting_timeout, 0);
  3691. timer_setup(&acb->selto_timer, NULL, 0);
  3692. acb->srb_count = DC395x_MAX_SRB_CNT;
  3693. acb->sel_timeout = DC395x_SEL_TIMEOUT; /* timeout=250ms */
  3694. /* NOTE: acb->irq_level is set at IRQ registration time */
  3695. acb->tag_max_num = 1 << eeprom->max_tag;
  3696. if (acb->tag_max_num > 30)
  3697. acb->tag_max_num = 30;
  3698. acb->acb_flag = 0; /* RESET_DETECT, RESET_DONE, RESET_DEV */
  3699. acb->gmode2 = eeprom->channel_cfg;
  3700. acb->config = 0; /* NOTE: actually set in adapter_init_chip */
  3701. if (eeprom->channel_cfg & NAC_SCANLUN)
  3702. acb->lun_chk = 1;
  3703. acb->scan_devices = 1;
  3704. acb->scsi_host->this_id = eeprom->scsi_id;
  3705. acb->hostid_bit = (1 << acb->scsi_host->this_id);
  3706. for (i = 0; i < DC395x_MAX_SCSI_ID; i++)
  3707. acb->dcb_map[i] = 0;
  3708. acb->msg_len = 0;
  3709. /* link static array of srbs into the srb free list */
  3710. for (i = 0; i < acb->srb_count - 1; i++)
  3711. list_add_tail(&acb->srb_array[i].list, &acb->srb_free_list);
  3712. }
  3713. /**
  3714. * adapter_init_scsi_host - Initialize the scsi host instance based on
  3715. * values that we have already stored in the adapter instance. There's
  3716. * some mention that a lot of these are deprecated, so we won't use
  3717. * them (we'll use the ones in the adapter instance) but we'll fill
  3718. * them in in case something else needs them.
  3719. *
  3720. * The eeprom structure, irq and io ports in the adapter need to have
  3721. * been set before calling this function.
  3722. *
  3723. * @host: The scsi host instance to fill in the values for.
  3724. **/
  3725. static void adapter_init_scsi_host(struct Scsi_Host *host)
  3726. {
  3727. struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
  3728. struct NvRamType *eeprom = &acb->eeprom;
  3729. host->max_cmd_len = 24;
  3730. host->can_queue = DC395x_MAX_CMD_QUEUE;
  3731. host->cmd_per_lun = DC395x_MAX_CMD_PER_LUN;
  3732. host->this_id = (int)eeprom->scsi_id;
  3733. host->io_port = acb->io_port_base;
  3734. host->n_io_port = acb->io_port_len;
  3735. host->dma_channel = -1;
  3736. host->unique_id = acb->io_port_base;
  3737. host->irq = acb->irq_level;
  3738. acb->last_reset = jiffies;
  3739. host->max_id = 16;
  3740. if (host->max_id - 1 == eeprom->scsi_id)
  3741. host->max_id--;
  3742. if (eeprom->channel_cfg & NAC_SCANLUN)
  3743. host->max_lun = 8;
  3744. else
  3745. host->max_lun = 1;
  3746. }
  3747. /**
  3748. * adapter_init_chip - Get the chip into a know state and figure out
  3749. * some of the settings that apply to this adapter.
  3750. *
  3751. * The io port in the adapter needs to have been set before calling
  3752. * this function. The config will be configured correctly on return.
  3753. *
  3754. * @acb: The adapter which we are to init.
  3755. **/
  3756. static void adapter_init_chip(struct AdapterCtlBlk *acb)
  3757. {
  3758. struct NvRamType *eeprom = &acb->eeprom;
  3759. /* Mask all the interrupt */
  3760. DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0x00);
  3761. DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0x00);
  3762. /* Reset SCSI module */
  3763. DC395x_write16(acb, TRM_S1040_SCSI_CONTROL, DO_RSTMODULE);
  3764. /* Reset PCI/DMA module */
  3765. DC395x_write8(acb, TRM_S1040_DMA_CONTROL, DMARESETMODULE);
  3766. udelay(20);
  3767. /* program configuration 0 */
  3768. acb->config = HCC_AUTOTERM | HCC_PARITY;
  3769. if (DC395x_read8(acb, TRM_S1040_GEN_STATUS) & WIDESCSI)
  3770. acb->config |= HCC_WIDE_CARD;
  3771. if (eeprom->channel_cfg & NAC_POWERON_SCSI_RESET)
  3772. acb->config |= HCC_SCSI_RESET;
  3773. if (acb->config & HCC_SCSI_RESET) {
  3774. dprintkl(KERN_INFO, "Performing initial SCSI bus reset\n");
  3775. DC395x_write8(acb, TRM_S1040_SCSI_CONTROL, DO_RSTSCSI);
  3776. /*while (!( DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS) & INT_SCSIRESET )); */
  3777. /*spin_unlock_irq (&io_request_lock); */
  3778. udelay(500);
  3779. acb->last_reset =
  3780. jiffies + HZ / 2 +
  3781. HZ * acb->eeprom.delay_time;
  3782. /*spin_lock_irq (&io_request_lock); */
  3783. }
  3784. }
  3785. /**
  3786. * adapter_init - Grab the resource for the card, setup the adapter
  3787. * information, set the card into a known state, create the various
  3788. * tables etc etc. This basically gets all adapter information all up
  3789. * to date, initialised and gets the chip in sync with it.
  3790. *
  3791. * @acb: The adapter which we are to init.
  3792. * @io_port: The base I/O port
  3793. * @io_port_len: The I/O port size
  3794. * @irq: IRQ
  3795. *
  3796. * Returns 0 if the initialization succeeds, any other value on
  3797. * failure.
  3798. **/
  3799. static int adapter_init(struct AdapterCtlBlk *acb, unsigned long io_port,
  3800. u32 io_port_len, unsigned int irq)
  3801. {
  3802. if (!request_region(io_port, io_port_len, DC395X_NAME)) {
  3803. dprintkl(KERN_ERR, "Failed to reserve IO region 0x%lx\n", io_port);
  3804. goto failed;
  3805. }
  3806. /* store port base to indicate we have registered it */
  3807. acb->io_port_base = io_port;
  3808. acb->io_port_len = io_port_len;
  3809. if (request_irq(irq, dc395x_interrupt, IRQF_SHARED, DC395X_NAME, acb)) {
  3810. /* release the region we just claimed */
  3811. dprintkl(KERN_INFO, "Failed to register IRQ\n");
  3812. goto failed;
  3813. }
  3814. /* store irq to indicate we have registered it */
  3815. acb->irq_level = irq;
  3816. /* get eeprom configuration information and command line settings etc */
  3817. check_eeprom(&acb->eeprom, io_port);
  3818. print_eeprom_settings(&acb->eeprom);
  3819. /* setup adapter control block */
  3820. adapter_init_params(acb);
  3821. /* display card connectors/termination settings */
  3822. adapter_print_config(acb);
  3823. if (adapter_sg_tables_alloc(acb)) {
  3824. dprintkl(KERN_DEBUG, "Memory allocation for SG tables failed\n");
  3825. goto failed;
  3826. }
  3827. adapter_init_scsi_host(acb->scsi_host);
  3828. adapter_init_chip(acb);
  3829. set_basic_config(acb);
  3830. dprintkdbg(DBG_0,
  3831. "adapter_init: acb=%p, pdcb_map=%p psrb_array=%p "
  3832. "size{acb=0x%04x dcb=0x%04x srb=0x%04x}\n",
  3833. acb, acb->dcb_map, acb->srb_array, sizeof(struct AdapterCtlBlk),
  3834. sizeof(struct DeviceCtlBlk), sizeof(struct ScsiReqBlk));
  3835. return 0;
  3836. failed:
  3837. if (acb->irq_level)
  3838. free_irq(acb->irq_level, acb);
  3839. if (acb->io_port_base)
  3840. release_region(acb->io_port_base, acb->io_port_len);
  3841. adapter_sg_tables_free(acb);
  3842. return 1;
  3843. }
  3844. /**
  3845. * adapter_uninit_chip - cleanly shut down the scsi controller chip,
  3846. * stopping all operations and disabling interrupt generation on the
  3847. * card.
  3848. *
  3849. * @acb: The adapter which we are to shutdown.
  3850. **/
  3851. static void adapter_uninit_chip(struct AdapterCtlBlk *acb)
  3852. {
  3853. /* disable interrupts */
  3854. DC395x_write8(acb, TRM_S1040_DMA_INTEN, 0);
  3855. DC395x_write8(acb, TRM_S1040_SCSI_INTEN, 0);
  3856. /* reset the scsi bus */
  3857. if (acb->config & HCC_SCSI_RESET)
  3858. reset_scsi_bus(acb);
  3859. /* clear any pending interrupt state */
  3860. DC395x_read8(acb, TRM_S1040_SCSI_INTSTATUS);
  3861. }
  3862. /**
  3863. * adapter_uninit - Shut down the chip and release any resources that
  3864. * we had allocated. Once this returns the adapter should not be used
  3865. * anymore.
  3866. *
  3867. * @acb: The adapter which we are to un-initialize.
  3868. **/
  3869. static void adapter_uninit(struct AdapterCtlBlk *acb)
  3870. {
  3871. unsigned long flags;
  3872. DC395x_LOCK_IO(acb->scsi_host, flags);
  3873. /* remove timers */
  3874. if (timer_pending(&acb->waiting_timer))
  3875. del_timer(&acb->waiting_timer);
  3876. if (timer_pending(&acb->selto_timer))
  3877. del_timer(&acb->selto_timer);
  3878. adapter_uninit_chip(acb);
  3879. adapter_remove_and_free_all_devices(acb);
  3880. DC395x_UNLOCK_IO(acb->scsi_host, flags);
  3881. if (acb->irq_level)
  3882. free_irq(acb->irq_level, acb);
  3883. if (acb->io_port_base)
  3884. release_region(acb->io_port_base, acb->io_port_len);
  3885. adapter_sg_tables_free(acb);
  3886. }
  3887. #undef YESNO
  3888. #define YESNO(YN) \
  3889. if (YN) seq_printf(m, " Yes ");\
  3890. else seq_printf(m, " No ")
  3891. static int dc395x_show_info(struct seq_file *m, struct Scsi_Host *host)
  3892. {
  3893. struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)host->hostdata;
  3894. int spd, spd1;
  3895. struct DeviceCtlBlk *dcb;
  3896. unsigned long flags;
  3897. int dev;
  3898. seq_puts(m, DC395X_BANNER " PCI SCSI Host Adapter\n"
  3899. " Driver Version " DC395X_VERSION "\n");
  3900. DC395x_LOCK_IO(acb->scsi_host, flags);
  3901. seq_printf(m, "SCSI Host Nr %i, ", host->host_no);
  3902. seq_printf(m, "DC395U/UW/F DC315/U %s\n",
  3903. (acb->config & HCC_WIDE_CARD) ? "Wide" : "");
  3904. seq_printf(m, "io_port_base 0x%04lx, ", acb->io_port_base);
  3905. seq_printf(m, "irq_level 0x%04x, ", acb->irq_level);
  3906. seq_printf(m, " SelTimeout %ims\n", (1638 * acb->sel_timeout) / 1000);
  3907. seq_printf(m, "MaxID %i, MaxLUN %llu, ", host->max_id, host->max_lun);
  3908. seq_printf(m, "AdapterID %i\n", host->this_id);
  3909. seq_printf(m, "tag_max_num %i", acb->tag_max_num);
  3910. /*seq_printf(m, ", DMA_Status %i\n", DC395x_read8(acb, TRM_S1040_DMA_STATUS)); */
  3911. seq_printf(m, ", FilterCfg 0x%02x",
  3912. DC395x_read8(acb, TRM_S1040_SCSI_CONFIG1));
  3913. seq_printf(m, ", DelayReset %is\n", acb->eeprom.delay_time);
  3914. /*seq_printf(m, "\n"); */
  3915. seq_printf(m, "Nr of DCBs: %i\n", list_size(&acb->dcb_list));
  3916. seq_printf(m, "Map of attached LUNs: %8ph\n", &acb->dcb_map[0]);
  3917. seq_printf(m, " %8ph\n", &acb->dcb_map[8]);
  3918. seq_puts(m,
  3919. "Un ID LUN Prty Sync Wide DsCn SndS TagQ nego_period SyncFreq SyncOffs MaxCmd\n");
  3920. dev = 0;
  3921. list_for_each_entry(dcb, &acb->dcb_list, list) {
  3922. int nego_period;
  3923. seq_printf(m, "%02i %02i %02i ", dev, dcb->target_id,
  3924. dcb->target_lun);
  3925. YESNO(dcb->dev_mode & NTC_DO_PARITY_CHK);
  3926. YESNO(dcb->sync_offset);
  3927. YESNO(dcb->sync_period & WIDE_SYNC);
  3928. YESNO(dcb->dev_mode & NTC_DO_DISCONNECT);
  3929. YESNO(dcb->dev_mode & NTC_DO_SEND_START);
  3930. YESNO(dcb->sync_mode & EN_TAG_QUEUEING);
  3931. nego_period = clock_period[dcb->sync_period & 0x07] << 2;
  3932. if (dcb->sync_offset)
  3933. seq_printf(m, " %03i ns ", nego_period);
  3934. else
  3935. seq_printf(m, " (%03i ns)", (dcb->min_nego_period << 2));
  3936. if (dcb->sync_offset & 0x0f) {
  3937. spd = 1000 / (nego_period);
  3938. spd1 = 1000 % (nego_period);
  3939. spd1 = (spd1 * 10 + nego_period / 2) / (nego_period);
  3940. seq_printf(m, " %2i.%1i M %02i ", spd, spd1,
  3941. (dcb->sync_offset & 0x0f));
  3942. } else
  3943. seq_puts(m, " ");
  3944. /* Add more info ... */
  3945. seq_printf(m, " %02i\n", dcb->max_command);
  3946. dev++;
  3947. }
  3948. if (timer_pending(&acb->waiting_timer))
  3949. seq_puts(m, "Waiting queue timer running\n");
  3950. else
  3951. seq_putc(m, '\n');
  3952. list_for_each_entry(dcb, &acb->dcb_list, list) {
  3953. struct ScsiReqBlk *srb;
  3954. if (!list_empty(&dcb->srb_waiting_list))
  3955. seq_printf(m, "DCB (%02i-%i): Waiting: %i:",
  3956. dcb->target_id, dcb->target_lun,
  3957. list_size(&dcb->srb_waiting_list));
  3958. list_for_each_entry(srb, &dcb->srb_waiting_list, list)
  3959. seq_printf(m, " %p", srb->cmd);
  3960. if (!list_empty(&dcb->srb_going_list))
  3961. seq_printf(m, "\nDCB (%02i-%i): Going : %i:",
  3962. dcb->target_id, dcb->target_lun,
  3963. list_size(&dcb->srb_going_list));
  3964. list_for_each_entry(srb, &dcb->srb_going_list, list)
  3965. seq_printf(m, " %p", srb->cmd);
  3966. if (!list_empty(&dcb->srb_waiting_list) || !list_empty(&dcb->srb_going_list))
  3967. seq_putc(m, '\n');
  3968. }
  3969. if (debug_enabled(DBG_1)) {
  3970. seq_printf(m, "DCB list for ACB %p:\n", acb);
  3971. list_for_each_entry(dcb, &acb->dcb_list, list) {
  3972. seq_printf(m, "%p -> ", dcb);
  3973. }
  3974. seq_puts(m, "END\n");
  3975. }
  3976. DC395x_UNLOCK_IO(acb->scsi_host, flags);
  3977. return 0;
  3978. }
  3979. static struct scsi_host_template dc395x_driver_template = {
  3980. .module = THIS_MODULE,
  3981. .proc_name = DC395X_NAME,
  3982. .show_info = dc395x_show_info,
  3983. .name = DC395X_BANNER " " DC395X_VERSION,
  3984. .queuecommand = dc395x_queue_command,
  3985. .slave_alloc = dc395x_slave_alloc,
  3986. .slave_destroy = dc395x_slave_destroy,
  3987. .can_queue = DC395x_MAX_CAN_QUEUE,
  3988. .this_id = 7,
  3989. .sg_tablesize = DC395x_MAX_SG_TABLESIZE,
  3990. .cmd_per_lun = DC395x_MAX_CMD_PER_LUN,
  3991. .eh_abort_handler = dc395x_eh_abort,
  3992. .eh_bus_reset_handler = dc395x_eh_bus_reset,
  3993. .dma_boundary = PAGE_SIZE - 1,
  3994. };
  3995. /**
  3996. * banner_display - Display banner on first instance of driver
  3997. * initialized.
  3998. **/
  3999. static void banner_display(void)
  4000. {
  4001. static int banner_done = 0;
  4002. if (!banner_done)
  4003. {
  4004. dprintkl(KERN_INFO, "%s %s\n", DC395X_BANNER, DC395X_VERSION);
  4005. banner_done = 1;
  4006. }
  4007. }
  4008. /**
  4009. * dc395x_init_one - Initialise a single instance of the adapter.
  4010. *
  4011. * The PCI layer will call this once for each instance of the adapter
  4012. * that it finds in the system. The pci_dev strcuture indicates which
  4013. * instance we are being called from.
  4014. *
  4015. * @dev: The PCI device to initialize.
  4016. * @id: Looks like a pointer to the entry in our pci device table
  4017. * that was actually matched by the PCI subsystem.
  4018. *
  4019. * Returns 0 on success, or an error code (-ve) on failure.
  4020. **/
  4021. static int dc395x_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  4022. {
  4023. struct Scsi_Host *scsi_host = NULL;
  4024. struct AdapterCtlBlk *acb = NULL;
  4025. unsigned long io_port_base;
  4026. unsigned int io_port_len;
  4027. unsigned int irq;
  4028. dprintkdbg(DBG_0, "Init one instance (%s)\n", pci_name(dev));
  4029. banner_display();
  4030. if (pci_enable_device(dev))
  4031. {
  4032. dprintkl(KERN_INFO, "PCI Enable device failed.\n");
  4033. return -ENODEV;
  4034. }
  4035. io_port_base = pci_resource_start(dev, 0) & PCI_BASE_ADDRESS_IO_MASK;
  4036. io_port_len = pci_resource_len(dev, 0);
  4037. irq = dev->irq;
  4038. dprintkdbg(DBG_0, "IO_PORT=0x%04lx, IRQ=0x%x\n", io_port_base, dev->irq);
  4039. /* allocate scsi host information (includes out adapter) */
  4040. scsi_host = scsi_host_alloc(&dc395x_driver_template,
  4041. sizeof(struct AdapterCtlBlk));
  4042. if (!scsi_host) {
  4043. dprintkl(KERN_INFO, "scsi_host_alloc failed\n");
  4044. goto fail;
  4045. }
  4046. acb = (struct AdapterCtlBlk*)scsi_host->hostdata;
  4047. acb->scsi_host = scsi_host;
  4048. acb->dev = dev;
  4049. /* initialise the adapter and everything we need */
  4050. if (adapter_init(acb, io_port_base, io_port_len, irq)) {
  4051. dprintkl(KERN_INFO, "adapter init failed\n");
  4052. acb = NULL;
  4053. goto fail;
  4054. }
  4055. pci_set_master(dev);
  4056. /* get the scsi mid level to scan for new devices on the bus */
  4057. if (scsi_add_host(scsi_host, &dev->dev)) {
  4058. dprintkl(KERN_ERR, "scsi_add_host failed\n");
  4059. goto fail;
  4060. }
  4061. pci_set_drvdata(dev, scsi_host);
  4062. scsi_scan_host(scsi_host);
  4063. return 0;
  4064. fail:
  4065. if (acb != NULL)
  4066. adapter_uninit(acb);
  4067. if (scsi_host != NULL)
  4068. scsi_host_put(scsi_host);
  4069. pci_disable_device(dev);
  4070. return -ENODEV;
  4071. }
  4072. /**
  4073. * dc395x_remove_one - Called to remove a single instance of the
  4074. * adapter.
  4075. *
  4076. * @dev: The PCI device to initialize.
  4077. **/
  4078. static void dc395x_remove_one(struct pci_dev *dev)
  4079. {
  4080. struct Scsi_Host *scsi_host = pci_get_drvdata(dev);
  4081. struct AdapterCtlBlk *acb = (struct AdapterCtlBlk *)(scsi_host->hostdata);
  4082. dprintkdbg(DBG_0, "dc395x_remove_one: acb=%p\n", acb);
  4083. scsi_remove_host(scsi_host);
  4084. adapter_uninit(acb);
  4085. pci_disable_device(dev);
  4086. scsi_host_put(scsi_host);
  4087. }
  4088. static struct pci_device_id dc395x_pci_table[] = {
  4089. {
  4090. .vendor = PCI_VENDOR_ID_TEKRAM,
  4091. .device = PCI_DEVICE_ID_TEKRAM_TRMS1040,
  4092. .subvendor = PCI_ANY_ID,
  4093. .subdevice = PCI_ANY_ID,
  4094. },
  4095. {} /* Terminating entry */
  4096. };
  4097. MODULE_DEVICE_TABLE(pci, dc395x_pci_table);
  4098. static struct pci_driver dc395x_driver = {
  4099. .name = DC395X_NAME,
  4100. .id_table = dc395x_pci_table,
  4101. .probe = dc395x_init_one,
  4102. .remove = dc395x_remove_one,
  4103. };
  4104. module_pci_driver(dc395x_driver);
  4105. MODULE_AUTHOR("C.L. Huang / Erich Chen / Kurt Garloff");
  4106. MODULE_DESCRIPTION("SCSI host adapter driver for Tekram TRM-S1040 based adapters: Tekram DC395 and DC315 series");
  4107. MODULE_LICENSE("GPL");