dc395x.c 143 KB

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