53c7xx.c 191 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104
  1. /*
  2. * 53c710 driver. Modified from Drew Eckhardts driver
  3. * for 53c810 by Richard Hirst [richard@sleepie.demon.co.uk]
  4. * Check out PERM_OPTIONS and EXPECTED_CLOCK, which may be defined in the
  5. * relevant machine specific file (eg. mvme16x.[ch], amiga7xx.[ch]).
  6. * There are also currently some defines at the top of 53c7xx.scr.
  7. * The chip type is #defined in script_asm.pl, as well as the Makefile.
  8. * Host scsi ID expected to be 7 - see NCR53c7x0_init().
  9. *
  10. * I have removed the PCI code and some of the 53c8xx specific code -
  11. * simply to make this file smaller and easier to manage.
  12. *
  13. * MVME16x issues:
  14. * Problems trying to read any chip registers in NCR53c7x0_init(), as they
  15. * may never have been set by 16xBug (eg. If kernel has come in over tftp).
  16. */
  17. /*
  18. * Adapted for Linux/m68k Amiga platforms for the A4000T/A4091 and
  19. * WarpEngine SCSI controllers.
  20. * By Alan Hourihane <alanh@fairlite.demon.co.uk>
  21. * Thanks to Richard Hirst for making it possible with the MVME additions
  22. */
  23. /*
  24. * 53c710 rev 0 doesn't support add with carry. Rev 1 and 2 does. To
  25. * overcome this problem you can define FORCE_DSA_ALIGNMENT, which ensures
  26. * that the DSA address is always xxxxxx00. If disconnection is not allowed,
  27. * then the script only ever tries to add small (< 256) positive offsets to
  28. * DSA, so lack of carry isn't a problem. FORCE_DSA_ALIGNMENT can, of course,
  29. * be defined for all chip revisions at a small cost in memory usage.
  30. */
  31. #define FORCE_DSA_ALIGNMENT
  32. /*
  33. * Selection timer does not always work on the 53c710, depending on the
  34. * timing at the last disconnect, if this is a problem for you, try
  35. * using validids as detailed below.
  36. *
  37. * Options for the NCR7xx driver
  38. *
  39. * noasync:0 - disables sync and asynchronous negotiation
  40. * nosync:0 - disables synchronous negotiation (does async)
  41. * nodisconnect:0 - disables disconnection
  42. * validids:0x?? - Bitmask field that disallows certain ID's.
  43. * - e.g. 0x03 allows ID 0,1
  44. * - 0x1F allows ID 0,1,2,3,4
  45. * opthi:n - replace top word of options with 'n'
  46. * optlo:n - replace bottom word of options with 'n'
  47. * - ALWAYS SPECIFY opthi THEN optlo <<<<<<<<<<
  48. */
  49. /*
  50. * PERM_OPTIONS are driver options which will be enabled for all NCR boards
  51. * in the system at driver initialization time.
  52. *
  53. * Don't THINK about touching these in PERM_OPTIONS :
  54. * OPTION_MEMORY_MAPPED
  55. * 680x0 doesn't have an IO map!
  56. *
  57. * OPTION_DEBUG_TEST1
  58. * Test 1 does bus mastering and interrupt tests, which will help weed
  59. * out brain damaged main boards.
  60. *
  61. * Other PERM_OPTIONS settings are listed below. Note the actual options
  62. * required are set in the relevant file (mvme16x.c, amiga7xx.c, etc):
  63. *
  64. * OPTION_NO_ASYNC
  65. * Don't negotiate for asynchronous transfers on the first command
  66. * when OPTION_ALWAYS_SYNCHRONOUS is set. Useful for dain bramaged
  67. * devices which do something bad rather than sending a MESSAGE
  68. * REJECT back to us like they should if they can't cope.
  69. *
  70. * OPTION_SYNCHRONOUS
  71. * Enable support for synchronous transfers. Target negotiated
  72. * synchronous transfers will be responded to. To initiate
  73. * a synchronous transfer request, call
  74. *
  75. * request_synchronous (hostno, target)
  76. *
  77. * from within KGDB.
  78. *
  79. * OPTION_ALWAYS_SYNCHRONOUS
  80. * Negotiate for synchronous transfers with every target after
  81. * driver initialization or a SCSI bus reset. This is a bit dangerous,
  82. * since there are some dain bramaged SCSI devices which will accept
  83. * SDTR messages but keep talking asynchronously.
  84. *
  85. * OPTION_DISCONNECT
  86. * Enable support for disconnect/reconnect. To change the
  87. * default setting on a given host adapter, call
  88. *
  89. * request_disconnect (hostno, allow)
  90. *
  91. * where allow is non-zero to allow, 0 to disallow.
  92. *
  93. * If you really want to run 10MHz FAST SCSI-II transfers, you should
  94. * know that the NCR driver currently ignores parity information. Most
  95. * systems do 5MHz SCSI fine. I've seen a lot that have problems faster
  96. * than 8MHz. To play it safe, we only request 5MHz transfers.
  97. *
  98. * If you'd rather get 10MHz transfers, edit sdtr_message and change
  99. * the fourth byte from 50 to 25.
  100. */
  101. /*
  102. * Sponsored by
  103. * iX Multiuser Multitasking Magazine
  104. * Hannover, Germany
  105. * hm@ix.de
  106. *
  107. * Copyright 1993, 1994, 1995 Drew Eckhardt
  108. * Visionary Computing
  109. * (Unix and Linux consulting and custom programming)
  110. * drew@PoohSticks.ORG
  111. * +1 (303) 786-7975
  112. *
  113. * TolerANT and SCSI SCRIPTS are registered trademarks of NCR Corporation.
  114. *
  115. * For more information, please consult
  116. *
  117. * NCR53C810
  118. * SCSI I/O Processor
  119. * Programmer's Guide
  120. *
  121. * NCR 53C810
  122. * PCI-SCSI I/O Processor
  123. * Data Manual
  124. *
  125. * NCR 53C810/53C820
  126. * PCI-SCSI I/O Processor Design In Guide
  127. *
  128. * For literature on Symbios Logic Inc. formerly NCR, SCSI,
  129. * and Communication products please call (800) 334-5454 or
  130. * (719) 536-3300.
  131. *
  132. * PCI BIOS Specification Revision
  133. * PCI Local Bus Specification
  134. * PCI System Design Guide
  135. *
  136. * PCI Special Interest Group
  137. * M/S HF3-15A
  138. * 5200 N.E. Elam Young Parkway
  139. * Hillsboro, Oregon 97124-6497
  140. * +1 (503) 696-2000
  141. * +1 (800) 433-5177
  142. */
  143. /*
  144. * Design issues :
  145. * The cumulative latency needed to propagate a read/write request
  146. * through the file system, buffer cache, driver stacks, SCSI host, and
  147. * SCSI device is ultimately the limiting factor in throughput once we
  148. * have a sufficiently fast host adapter.
  149. *
  150. * So, to maximize performance we want to keep the ratio of latency to data
  151. * transfer time to a minimum by
  152. * 1. Minimizing the total number of commands sent (typical command latency
  153. * including drive and bus mastering host overhead is as high as 4.5ms)
  154. * to transfer a given amount of data.
  155. *
  156. * This is accomplished by placing no arbitrary limit on the number
  157. * of scatter/gather buffers supported, since we can transfer 1K
  158. * per scatter/gather buffer without Eric's cluster patches,
  159. * 4K with.
  160. *
  161. * 2. Minimizing the number of fatal interrupts serviced, since
  162. * fatal interrupts halt the SCSI I/O processor. Basically,
  163. * this means offloading the practical maximum amount of processing
  164. * to the SCSI chip.
  165. *
  166. * On the NCR53c810/820/720, this is accomplished by using
  167. * interrupt-on-the-fly signals when commands complete,
  168. * and only handling fatal errors and SDTR / WDTR messages
  169. * in the host code.
  170. *
  171. * On the NCR53c710, interrupts are generated as on the NCR53c8x0,
  172. * only the lack of a interrupt-on-the-fly facility complicates
  173. * things. Also, SCSI ID registers and commands are
  174. * bit fielded rather than binary encoded.
  175. *
  176. * On the NCR53c700 and NCR53c700-66, operations that are done via
  177. * indirect, table mode on the more advanced chips must be
  178. * replaced by calls through a jump table which
  179. * acts as a surrogate for the DSA. Unfortunately, this
  180. * will mean that we must service an interrupt for each
  181. * disconnect/reconnect.
  182. *
  183. * 3. Eliminating latency by pipelining operations at the different levels.
  184. *
  185. * This driver allows a configurable number of commands to be enqueued
  186. * for each target/lun combination (experimentally, I have discovered
  187. * that two seems to work best) and will ultimately allow for
  188. * SCSI-II tagged queuing.
  189. *
  190. *
  191. * Architecture :
  192. * This driver is built around a Linux queue of commands waiting to
  193. * be executed, and a shared Linux/NCR array of commands to start. Commands
  194. * are transferred to the array by the run_process_issue_queue() function
  195. * which is called whenever a command completes.
  196. *
  197. * As commands are completed, the interrupt routine is triggered,
  198. * looks for commands in the linked list of completed commands with
  199. * valid status, removes these commands from a list of running commands,
  200. * calls the done routine, and flags their target/luns as not busy.
  201. *
  202. * Due to limitations in the intelligence of the NCR chips, certain
  203. * concessions are made. In many cases, it is easier to dynamically
  204. * generate/fix-up code rather than calculate on the NCR at run time.
  205. * So, code is generated or fixed up for
  206. *
  207. * - Handling data transfers, using a variable number of MOVE instructions
  208. * interspersed with CALL MSG_IN, WHEN MSGIN instructions.
  209. *
  210. * The DATAIN and DATAOUT routines are separate, so that an incorrect
  211. * direction can be trapped, and space isn't wasted.
  212. *
  213. * It may turn out that we're better off using some sort
  214. * of table indirect instruction in a loop with a variable
  215. * sized table on the NCR53c710 and newer chips.
  216. *
  217. * - Checking for reselection (NCR53c710 and better)
  218. *
  219. * - Handling the details of SCSI context switches (NCR53c710 and better),
  220. * such as reprogramming appropriate synchronous parameters,
  221. * removing the dsa structure from the NCR's queue of outstanding
  222. * commands, etc.
  223. *
  224. */
  225. #include <linux/module.h>
  226. #include <linux/config.h>
  227. #include <linux/types.h>
  228. #include <asm/setup.h>
  229. #include <asm/dma.h>
  230. #include <asm/io.h>
  231. #include <asm/system.h>
  232. #include <linux/delay.h>
  233. #include <linux/signal.h>
  234. #include <linux/sched.h>
  235. #include <linux/errno.h>
  236. #include <linux/string.h>
  237. #include <linux/slab.h>
  238. #include <linux/vmalloc.h>
  239. #include <linux/mm.h>
  240. #include <linux/ioport.h>
  241. #include <linux/time.h>
  242. #include <linux/blkdev.h>
  243. #include <linux/spinlock.h>
  244. #include <linux/interrupt.h>
  245. #include <asm/pgtable.h>
  246. #ifdef CONFIG_AMIGA
  247. #include <asm/amigahw.h>
  248. #include <asm/amigaints.h>
  249. #include <asm/irq.h>
  250. #define BIG_ENDIAN
  251. #define NO_IO_SPACE
  252. #endif
  253. #ifdef CONFIG_MVME16x
  254. #include <asm/mvme16xhw.h>
  255. #define BIG_ENDIAN
  256. #define NO_IO_SPACE
  257. #define VALID_IDS
  258. #endif
  259. #ifdef CONFIG_BVME6000
  260. #include <asm/bvme6000hw.h>
  261. #define BIG_ENDIAN
  262. #define NO_IO_SPACE
  263. #define VALID_IDS
  264. #endif
  265. #include "scsi.h"
  266. #include <scsi/scsi_dbg.h>
  267. #include <scsi/scsi_host.h>
  268. #include <scsi/scsi_transport_spi.h>
  269. #include "53c7xx.h"
  270. #include <linux/stat.h>
  271. #include <linux/stddef.h>
  272. #ifdef NO_IO_SPACE
  273. /*
  274. * The following make the definitions in 53c7xx.h (write8, etc) smaller,
  275. * we don't have separate i/o space anyway.
  276. */
  277. #undef inb
  278. #undef outb
  279. #undef inw
  280. #undef outw
  281. #undef inl
  282. #undef outl
  283. #define inb(x) 1
  284. #define inw(x) 1
  285. #define inl(x) 1
  286. #define outb(x,y) 1
  287. #define outw(x,y) 1
  288. #define outl(x,y) 1
  289. #endif
  290. static int check_address (unsigned long addr, int size);
  291. static void dump_events (struct Scsi_Host *host, int count);
  292. static Scsi_Cmnd * return_outstanding_commands (struct Scsi_Host *host,
  293. int free, int issue);
  294. static void hard_reset (struct Scsi_Host *host);
  295. static void ncr_scsi_reset (struct Scsi_Host *host);
  296. static void print_lots (struct Scsi_Host *host);
  297. static void set_synchronous (struct Scsi_Host *host, int target, int sxfer,
  298. int scntl3, int now_connected);
  299. static int datapath_residual (struct Scsi_Host *host);
  300. static const char * sbcl_to_phase (int sbcl);
  301. static void print_progress (Scsi_Cmnd *cmd);
  302. static void print_queues (struct Scsi_Host *host);
  303. static void process_issue_queue (unsigned long flags);
  304. static int shutdown (struct Scsi_Host *host);
  305. static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int result);
  306. static int disable (struct Scsi_Host *host);
  307. static int NCR53c7xx_run_tests (struct Scsi_Host *host);
  308. static irqreturn_t NCR53c7x0_intr(int irq, void *dev_id, struct pt_regs * regs);
  309. static void NCR53c7x0_intfly (struct Scsi_Host *host);
  310. static int ncr_halt (struct Scsi_Host *host);
  311. static void intr_phase_mismatch (struct Scsi_Host *host, struct NCR53c7x0_cmd
  312. *cmd);
  313. static void intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd);
  314. static void print_dsa (struct Scsi_Host *host, u32 *dsa,
  315. const char *prefix);
  316. static int print_insn (struct Scsi_Host *host, const u32 *insn,
  317. const char *prefix, int kernel);
  318. static void NCR53c7xx_dsa_fixup (struct NCR53c7x0_cmd *cmd);
  319. static void NCR53c7x0_init_fixup (struct Scsi_Host *host);
  320. static int NCR53c7x0_dstat_sir_intr (struct Scsi_Host *host, struct
  321. NCR53c7x0_cmd *cmd);
  322. static void NCR53c7x0_soft_reset (struct Scsi_Host *host);
  323. /* Size of event list (per host adapter) */
  324. static int track_events = 0;
  325. static struct Scsi_Host *first_host = NULL; /* Head of list of NCR boards */
  326. static struct scsi_host_template *the_template = NULL;
  327. /* NCR53c710 script handling code */
  328. #include "53c7xx_d.h"
  329. #ifdef A_int_debug_sync
  330. #define DEBUG_SYNC_INTR A_int_debug_sync
  331. #endif
  332. int NCR53c7xx_script_len = sizeof (SCRIPT);
  333. int NCR53c7xx_dsa_len = A_dsa_end + Ent_dsa_zero - Ent_dsa_code_template;
  334. #ifdef FORCE_DSA_ALIGNMENT
  335. int CmdPageStart = (0 - Ent_dsa_zero - sizeof(struct NCR53c7x0_cmd)) & 0xff;
  336. #endif
  337. static char *setup_strings[] =
  338. {"","","","","","","",""};
  339. #define MAX_SETUP_STRINGS (sizeof(setup_strings) / sizeof(char *))
  340. #define SETUP_BUFFER_SIZE 200
  341. static char setup_buffer[SETUP_BUFFER_SIZE];
  342. static char setup_used[MAX_SETUP_STRINGS];
  343. void ncr53c7xx_setup (char *str, int *ints)
  344. {
  345. int i;
  346. char *p1, *p2;
  347. p1 = setup_buffer;
  348. *p1 = '\0';
  349. if (str)
  350. strncpy(p1, str, SETUP_BUFFER_SIZE - strlen(setup_buffer));
  351. setup_buffer[SETUP_BUFFER_SIZE - 1] = '\0';
  352. p1 = setup_buffer;
  353. i = 0;
  354. while (*p1 && (i < MAX_SETUP_STRINGS)) {
  355. p2 = strchr(p1, ',');
  356. if (p2) {
  357. *p2 = '\0';
  358. if (p1 != p2)
  359. setup_strings[i] = p1;
  360. p1 = p2 + 1;
  361. i++;
  362. }
  363. else {
  364. setup_strings[i] = p1;
  365. break;
  366. }
  367. }
  368. for (i=0; i<MAX_SETUP_STRINGS; i++)
  369. setup_used[i] = 0;
  370. }
  371. /* check_setup_strings() returns index if key found, 0 if not
  372. */
  373. static int check_setup_strings(char *key, int *flags, int *val, char *buf)
  374. {
  375. int x;
  376. char *cp;
  377. for (x=0; x<MAX_SETUP_STRINGS; x++) {
  378. if (setup_used[x])
  379. continue;
  380. if (!strncmp(setup_strings[x], key, strlen(key)))
  381. break;
  382. if (!strncmp(setup_strings[x], "next", strlen("next")))
  383. return 0;
  384. }
  385. if (x == MAX_SETUP_STRINGS)
  386. return 0;
  387. setup_used[x] = 1;
  388. cp = setup_strings[x] + strlen(key);
  389. *val = -1;
  390. if (*cp != ':')
  391. return ++x;
  392. cp++;
  393. if ((*cp >= '0') && (*cp <= '9')) {
  394. *val = simple_strtoul(cp,NULL,0);
  395. }
  396. return ++x;
  397. }
  398. /*
  399. * KNOWN BUGS :
  400. * - There is some sort of conflict when the PPP driver is compiled with
  401. * support for 16 channels?
  402. *
  403. * - On systems which predate the 1.3.x initialization order change,
  404. * the NCR driver will cause Cannot get free page messages to appear.
  405. * These are harmless, but I don't know of an easy way to avoid them.
  406. *
  407. * - With OPTION_DISCONNECT, on two systems under unknown circumstances,
  408. * we get a PHASE MISMATCH with DSA set to zero (suggests that we
  409. * are occurring somewhere in the reselection code) where
  410. * DSP=some value DCMD|DBC=same value.
  411. *
  412. * Closer inspection suggests that we may be trying to execute
  413. * some portion of the DSA?
  414. * scsi0 : handling residual transfer (+ 0 bytes from DMA FIFO)
  415. * scsi0 : handling residual transfer (+ 0 bytes from DMA FIFO)
  416. * scsi0 : no current command : unexpected phase MSGIN.
  417. * DSP=0x1c46cc, DCMD|DBC=0x1c46ac, DSA=0x0
  418. * DSPS=0x0, TEMP=0x1c3e70, DMODE=0x80
  419. * scsi0 : DSP->
  420. * 001c46cc : 0x001c46cc 0x00000000
  421. * 001c46d4 : 0x001c5ea0 0x000011f8
  422. *
  423. * Changed the print code in the phase_mismatch handler so
  424. * that we call print_lots to try to diagnose this.
  425. *
  426. */
  427. /*
  428. * Possible future direction of architecture for max performance :
  429. *
  430. * We're using a single start array for the NCR chip. This is
  431. * sub-optimal, because we cannot add a command which would conflict with
  432. * an executing command to this start queue, and therefore must insert the
  433. * next command for a given I/T/L combination after the first has completed;
  434. * incurring our interrupt latency between SCSI commands.
  435. *
  436. * To allow further pipelining of the NCR and host CPU operation, we want
  437. * to set things up so that immediately on termination of a command destined
  438. * for a given LUN, we get that LUN busy again.
  439. *
  440. * To do this, we need to add a 32 bit pointer to which is jumped to
  441. * on completion of a command. If no new command is available, this
  442. * would point to the usual DSA issue queue select routine.
  443. *
  444. * If one were, it would point to a per-NCR53c7x0_cmd select routine
  445. * which starts execution immediately, inserting the command at the head
  446. * of the start queue if the NCR chip is selected or reselected.
  447. *
  448. * We would change so that we keep a list of outstanding commands
  449. * for each unit, rather than a single running_list. We'd insert
  450. * a new command into the right running list; if the NCR didn't
  451. * have something running for that yet, we'd put it in the
  452. * start queue as well. Some magic needs to happen to handle the
  453. * race condition between the first command terminating before the
  454. * new one is written.
  455. *
  456. * Potential for profiling :
  457. * Call do_gettimeofday(struct timeval *tv) to get 800ns resolution.
  458. */
  459. /*
  460. * TODO :
  461. * 1. To support WIDE transfers, not much needs to happen. We
  462. * should do CHMOVE instructions instead of MOVEs when
  463. * we have scatter/gather segments of uneven length. When
  464. * we do this, we need to handle the case where we disconnect
  465. * between segments.
  466. *
  467. * 2. Currently, when Icky things happen we do a FATAL(). Instead,
  468. * we want to do an integrity check on the parts of the NCR hostdata
  469. * structure which were initialized at boot time; FATAL() if that
  470. * fails, and otherwise try to recover. Keep track of how many
  471. * times this has happened within a single SCSI command; if it
  472. * gets excessive, then FATAL().
  473. *
  474. * 3. Parity checking is currently disabled, and a few things should
  475. * happen here now that we support synchronous SCSI transfers :
  476. * 1. On soft-reset, we shoould set the EPC (Enable Parity Checking)
  477. * and AAP (Assert SATN/ on parity error) bits in SCNTL0.
  478. *
  479. * 2. We should enable the parity interrupt in the SIEN0 register.
  480. *
  481. * 3. intr_phase_mismatch() needs to believe that message out is
  482. * always an "acceptable" phase to have a mismatch in. If
  483. * the old phase was MSG_IN, we should send a MESSAGE PARITY
  484. * error. If the old phase was something else, we should send
  485. * a INITIATOR_DETECTED_ERROR message. Note that this could
  486. * cause a RESTORE POINTERS message; so we should handle that
  487. * correctly first. Instead, we should probably do an
  488. * initiator_abort.
  489. *
  490. * 4. MPEE bit of CTEST4 should be set so we get interrupted if
  491. * we detect an error.
  492. *
  493. *
  494. * 5. The initial code has been tested on the NCR53c810. I don't
  495. * have access to NCR53c700, 700-66 (Forex boards), NCR53c710
  496. * (NCR Pentium systems), NCR53c720, NCR53c820, or NCR53c825 boards to
  497. * finish development on those platforms.
  498. *
  499. * NCR53c820/825/720 - need to add wide transfer support, including WDTR
  500. * negotiation, programming of wide transfer capabilities
  501. * on reselection and table indirect selection.
  502. *
  503. * NCR53c710 - need to add fatal interrupt or GEN code for
  504. * command completion signaling. Need to modify all
  505. * SDID, SCID, etc. registers, and table indirect select code
  506. * since these use bit fielded (ie 1<<target) instead of
  507. * binary encoded target ids. Need to accommodate
  508. * different register mappings, probably scan through
  509. * the SCRIPT code and change the non SFBR register operand
  510. * of all MOVE instructions.
  511. *
  512. * It is rather worse than this actually, the 710 corrupts
  513. * both TEMP and DSA when you do a MOVE MEMORY. This
  514. * screws you up all over the place. MOVE MEMORY 4 with a
  515. * destination of DSA seems to work OK, which helps some.
  516. * Richard Hirst richard@sleepie.demon.co.uk
  517. *
  518. * NCR53c700/700-66 - need to add code to refix addresses on
  519. * every nexus change, eliminate all table indirect code,
  520. * very messy.
  521. *
  522. * 6. The NCR53c7x0 series is very popular on other platforms that
  523. * could be running Linux - ie, some high performance AMIGA SCSI
  524. * boards use it.
  525. *
  526. * So, I should include #ifdef'd code so that it is
  527. * compatible with these systems.
  528. *
  529. * Specifically, the little Endian assumptions I made in my
  530. * bit fields need to change, and if the NCR doesn't see memory
  531. * the right way, we need to provide options to reverse words
  532. * when the scripts are relocated.
  533. *
  534. * 7. Use vremap() to access memory mapped boards.
  535. */
  536. /*
  537. * Allow for simultaneous existence of multiple SCSI scripts so we
  538. * can have a single driver binary for all of the family.
  539. *
  540. * - one for NCR53c700 and NCR53c700-66 chips (not yet supported)
  541. * - one for rest (only the NCR53c810, 815, 820, and 825 are currently
  542. * supported)
  543. *
  544. * So that we only need two SCSI scripts, we need to modify things so
  545. * that we fixup register accesses in READ/WRITE instructions, and
  546. * we'll also have to accommodate the bit vs. binary encoding of IDs
  547. * with the 7xx chips.
  548. */
  549. #define ROUNDUP(adr,type) \
  550. ((void *) (((long) (adr) + sizeof(type) - 1) & ~(sizeof(type) - 1)))
  551. /*
  552. * Function: issue_to_cmd
  553. *
  554. * Purpose: convert jump instruction in issue array to NCR53c7x0_cmd
  555. * structure pointer.
  556. *
  557. * Inputs; issue - pointer to start of NOP or JUMP instruction
  558. * in issue array.
  559. *
  560. * Returns: pointer to command on success; 0 if opcode is NOP.
  561. */
  562. static inline struct NCR53c7x0_cmd *
  563. issue_to_cmd (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata,
  564. u32 *issue)
  565. {
  566. return (issue[0] != hostdata->NOP_insn) ?
  567. /*
  568. * If the IF TRUE bit is set, it's a JUMP instruction. The
  569. * operand is a bus pointer to the dsa_begin routine for this DSA. The
  570. * dsa field of the NCR53c7x0_cmd structure starts with the
  571. * DSA code template. By converting to a virtual address,
  572. * subtracting the code template size, and offset of the
  573. * dsa field, we end up with a pointer to the start of the
  574. * structure (alternatively, we could use the
  575. * dsa_cmnd field, an anachronism from when we weren't
  576. * sure what the relationship between the NCR structures
  577. * and host structures were going to be.
  578. */
  579. (struct NCR53c7x0_cmd *) ((char *) bus_to_virt (issue[1]) -
  580. (hostdata->E_dsa_code_begin - hostdata->E_dsa_code_template) -
  581. offsetof(struct NCR53c7x0_cmd, dsa))
  582. /* If the IF TRUE bit is not set, it's a NOP */
  583. : NULL;
  584. }
  585. /*
  586. * FIXME: we should junk these, in favor of synchronous_want and
  587. * wide_want in the NCR53c7x0_hostdata structure.
  588. */
  589. /* Template for "preferred" synchronous transfer parameters. */
  590. static const unsigned char sdtr_message[] = {
  591. #ifdef CONFIG_SCSI_NCR53C7xx_FAST
  592. EXTENDED_MESSAGE, 3 /* length */, EXTENDED_SDTR, 25 /* *4ns */, 8 /* off */
  593. #else
  594. EXTENDED_MESSAGE, 3 /* length */, EXTENDED_SDTR, 50 /* *4ns */, 8 /* off */
  595. #endif
  596. };
  597. /* Template to request asynchronous transfers */
  598. static const unsigned char async_message[] = {
  599. EXTENDED_MESSAGE, 3 /* length */, EXTENDED_SDTR, 0, 0 /* asynchronous */
  600. };
  601. /* Template for "preferred" WIDE transfer parameters */
  602. static const unsigned char wdtr_message[] = {
  603. EXTENDED_MESSAGE, 2 /* length */, EXTENDED_WDTR, 1 /* 2^1 bytes */
  604. };
  605. #if 0
  606. /*
  607. * Function : struct Scsi_Host *find_host (int host)
  608. *
  609. * Purpose : KGDB support function which translates a host number
  610. * to a host structure.
  611. *
  612. * Inputs : host - number of SCSI host
  613. *
  614. * Returns : NULL on failure, pointer to host structure on success.
  615. */
  616. static struct Scsi_Host *
  617. find_host (int host) {
  618. struct Scsi_Host *h;
  619. for (h = first_host; h && h->host_no != host; h = h->next);
  620. if (!h) {
  621. printk (KERN_ALERT "scsi%d not found\n", host);
  622. return NULL;
  623. } else if (h->hostt != the_template) {
  624. printk (KERN_ALERT "scsi%d is not a NCR board\n", host);
  625. return NULL;
  626. }
  627. return h;
  628. }
  629. #if 0
  630. /*
  631. * Function : request_synchronous (int host, int target)
  632. *
  633. * Purpose : KGDB interface which will allow us to negotiate for
  634. * synchronous transfers. This ill be replaced with a more
  635. * integrated function; perhaps a new entry in the scsi_host
  636. * structure, accessible via an ioctl() or perhaps /proc/scsi.
  637. *
  638. * Inputs : host - number of SCSI host; target - number of target.
  639. *
  640. * Returns : 0 when negotiation has been setup for next SCSI command,
  641. * -1 on failure.
  642. */
  643. static int
  644. request_synchronous (int host, int target) {
  645. struct Scsi_Host *h;
  646. struct NCR53c7x0_hostdata *hostdata;
  647. unsigned long flags;
  648. if (target < 0) {
  649. printk (KERN_ALERT "target %d is bogus\n", target);
  650. return -1;
  651. }
  652. if (!(h = find_host (host)))
  653. return -1;
  654. else if (h->this_id == target) {
  655. printk (KERN_ALERT "target %d is host ID\n", target);
  656. return -1;
  657. }
  658. else if (target > h->max_id) {
  659. printk (KERN_ALERT "target %d exceeds maximum of %d\n", target,
  660. h->max_id);
  661. return -1;
  662. }
  663. hostdata = (struct NCR53c7x0_hostdata *)h->hostdata[0];
  664. local_irq_save(flags);
  665. if (hostdata->initiate_sdtr & (1 << target)) {
  666. local_irq_restore(flags);
  667. printk (KERN_ALERT "target %d already doing SDTR\n", target);
  668. return -1;
  669. }
  670. hostdata->initiate_sdtr |= (1 << target);
  671. local_irq_restore(flags);
  672. return 0;
  673. }
  674. #endif
  675. /*
  676. * Function : request_disconnect (int host, int on_or_off)
  677. *
  678. * Purpose : KGDB support function, tells us to allow or disallow
  679. * disconnections.
  680. *
  681. * Inputs : host - number of SCSI host; on_or_off - non-zero to allow,
  682. * zero to disallow.
  683. *
  684. * Returns : 0 on success, * -1 on failure.
  685. */
  686. static int
  687. request_disconnect (int host, int on_or_off) {
  688. struct Scsi_Host *h;
  689. struct NCR53c7x0_hostdata *hostdata;
  690. if (!(h = find_host (host)))
  691. return -1;
  692. hostdata = (struct NCR53c7x0_hostdata *) h->hostdata[0];
  693. if (on_or_off)
  694. hostdata->options |= OPTION_DISCONNECT;
  695. else
  696. hostdata->options &= ~OPTION_DISCONNECT;
  697. return 0;
  698. }
  699. #endif
  700. /*
  701. * Function : static void NCR53c7x0_driver_init (struct Scsi_Host *host)
  702. *
  703. * Purpose : Initialize internal structures, as required on startup, or
  704. * after a SCSI bus reset.
  705. *
  706. * Inputs : host - pointer to this host adapter's structure
  707. */
  708. static void
  709. NCR53c7x0_driver_init (struct Scsi_Host *host) {
  710. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  711. host->hostdata[0];
  712. int i, j;
  713. u32 *ncrcurrent;
  714. for (i = 0; i < 16; ++i) {
  715. hostdata->request_sense[i] = 0;
  716. for (j = 0; j < 8; ++j)
  717. hostdata->busy[i][j] = 0;
  718. set_synchronous (host, i, /* sxfer */ 0, hostdata->saved_scntl3, 0);
  719. }
  720. hostdata->issue_queue = NULL;
  721. hostdata->running_list = hostdata->finished_queue =
  722. hostdata->ncrcurrent = NULL;
  723. for (i = 0, ncrcurrent = (u32 *) hostdata->schedule;
  724. i < host->can_queue; ++i, ncrcurrent += 2) {
  725. ncrcurrent[0] = hostdata->NOP_insn;
  726. ncrcurrent[1] = 0xdeadbeef;
  727. }
  728. ncrcurrent[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24) | DBC_TCI_TRUE;
  729. ncrcurrent[1] = (u32) virt_to_bus (hostdata->script) +
  730. hostdata->E_wait_reselect;
  731. hostdata->reconnect_dsa_head = 0;
  732. hostdata->addr_reconnect_dsa_head = (u32)
  733. virt_to_bus((void *) &(hostdata->reconnect_dsa_head));
  734. hostdata->expecting_iid = 0;
  735. hostdata->expecting_sto = 0;
  736. if (hostdata->options & OPTION_ALWAYS_SYNCHRONOUS)
  737. hostdata->initiate_sdtr = 0xffff;
  738. else
  739. hostdata->initiate_sdtr = 0;
  740. hostdata->talked_to = 0;
  741. hostdata->idle = 1;
  742. }
  743. /*
  744. * Function : static int clock_to_ccf_710 (int clock)
  745. *
  746. * Purpose : Return the clock conversion factor for a given SCSI clock.
  747. *
  748. * Inputs : clock - SCSI clock expressed in Hz.
  749. *
  750. * Returns : ccf on success, -1 on failure.
  751. */
  752. static int
  753. clock_to_ccf_710 (int clock) {
  754. if (clock <= 16666666)
  755. return -1;
  756. if (clock <= 25000000)
  757. return 2; /* Divide by 1.0 */
  758. else if (clock <= 37500000)
  759. return 1; /* Divide by 1.5 */
  760. else if (clock <= 50000000)
  761. return 0; /* Divide by 2.0 */
  762. else if (clock <= 66000000)
  763. return 3; /* Divide by 3.0 */
  764. else
  765. return -1;
  766. }
  767. /*
  768. * Function : static int NCR53c7x0_init (struct Scsi_Host *host)
  769. *
  770. * Purpose : initialize the internal structures for a given SCSI host
  771. *
  772. * Inputs : host - pointer to this host adapter's structure
  773. *
  774. * Preconditions : when this function is called, the chip_type
  775. * field of the hostdata structure MUST have been set.
  776. *
  777. * Returns : 0 on success, -1 on failure.
  778. */
  779. int
  780. NCR53c7x0_init (struct Scsi_Host *host) {
  781. NCR53c7x0_local_declare();
  782. int i, ccf;
  783. unsigned char revision;
  784. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  785. host->hostdata[0];
  786. /*
  787. * There are some things which we need to know about in order to provide
  788. * a semblance of support. Print 'em if they aren't what we expect,
  789. * otherwise don't add to the noise.
  790. *
  791. * -1 means we don't know what to expect.
  792. */
  793. int val, flags;
  794. char buf[32];
  795. int expected_id = -1;
  796. int expected_clock = -1;
  797. int uninitialized = 0;
  798. #ifdef NO_IO_SPACE
  799. int expected_mapping = OPTION_MEMORY_MAPPED;
  800. #else
  801. int expected_mapping = OPTION_IO_MAPPED;
  802. #endif
  803. for (i=0;i<7;i++)
  804. hostdata->valid_ids[i] = 1; /* Default all ID's to scan */
  805. /* Parse commandline flags */
  806. if (check_setup_strings("noasync",&flags,&val,buf))
  807. {
  808. hostdata->options |= OPTION_NO_ASYNC;
  809. hostdata->options &= ~(OPTION_SYNCHRONOUS | OPTION_ALWAYS_SYNCHRONOUS);
  810. }
  811. if (check_setup_strings("nosync",&flags,&val,buf))
  812. {
  813. hostdata->options &= ~(OPTION_SYNCHRONOUS | OPTION_ALWAYS_SYNCHRONOUS);
  814. }
  815. if (check_setup_strings("nodisconnect",&flags,&val,buf))
  816. hostdata->options &= ~OPTION_DISCONNECT;
  817. if (check_setup_strings("validids",&flags,&val,buf))
  818. {
  819. for (i=0;i<7;i++)
  820. hostdata->valid_ids[i] = val & (1<<i);
  821. }
  822. if ((i = check_setup_strings("next",&flags,&val,buf)))
  823. {
  824. while (i)
  825. setup_used[--i] = 1;
  826. }
  827. if (check_setup_strings("opthi",&flags,&val,buf))
  828. hostdata->options = (long long)val << 32;
  829. if (check_setup_strings("optlo",&flags,&val,buf))
  830. hostdata->options |= val;
  831. NCR53c7x0_local_setup(host);
  832. switch (hostdata->chip) {
  833. case 710:
  834. case 770:
  835. hostdata->dstat_sir_intr = NCR53c7x0_dstat_sir_intr;
  836. hostdata->init_save_regs = NULL;
  837. hostdata->dsa_fixup = NCR53c7xx_dsa_fixup;
  838. hostdata->init_fixup = NCR53c7x0_init_fixup;
  839. hostdata->soft_reset = NCR53c7x0_soft_reset;
  840. hostdata->run_tests = NCR53c7xx_run_tests;
  841. expected_clock = hostdata->scsi_clock;
  842. expected_id = 7;
  843. break;
  844. default:
  845. printk ("scsi%d : chip type of %d is not supported yet, detaching.\n",
  846. host->host_no, hostdata->chip);
  847. scsi_unregister (host);
  848. return -1;
  849. }
  850. /* Assign constants accessed by NCR */
  851. hostdata->NCR53c7xx_zero = 0;
  852. hostdata->NCR53c7xx_msg_reject = MESSAGE_REJECT;
  853. hostdata->NCR53c7xx_msg_abort = ABORT;
  854. hostdata->NCR53c7xx_msg_nop = NOP;
  855. hostdata->NOP_insn = (DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24;
  856. if (expected_mapping == -1 ||
  857. (hostdata->options & (OPTION_MEMORY_MAPPED)) !=
  858. (expected_mapping & OPTION_MEMORY_MAPPED))
  859. printk ("scsi%d : using %s mapped access\n", host->host_no,
  860. (hostdata->options & OPTION_MEMORY_MAPPED) ? "memory" :
  861. "io");
  862. hostdata->dmode = (hostdata->chip == 700 || hostdata->chip == 70066) ?
  863. DMODE_REG_00 : DMODE_REG_10;
  864. hostdata->istat = ((hostdata->chip / 100) == 8) ?
  865. ISTAT_REG_800 : ISTAT_REG_700;
  866. /* We have to assume that this may be the first access to the chip, so
  867. * we must set EA in DCNTL. */
  868. NCR53c7x0_write8 (DCNTL_REG, DCNTL_10_EA|DCNTL_10_COM);
  869. /* Only the ISTAT register is readable when the NCR is running, so make
  870. sure it's halted. */
  871. ncr_halt(host);
  872. /*
  873. * XXX - the NCR53c700 uses bitfielded registers for SCID, SDID, etc,
  874. * as does the 710 with one bit per SCSI ID. Conversely, the NCR
  875. * uses a normal, 3 bit binary representation of these values.
  876. *
  877. * Get the rest of the NCR documentation, and FIND OUT where the change
  878. * was.
  879. */
  880. #if 0
  881. /* May not be able to do this - chip my not have been set up yet */
  882. tmp = hostdata->this_id_mask = NCR53c7x0_read8(SCID_REG);
  883. for (host->this_id = 0; tmp != 1; tmp >>=1, ++host->this_id);
  884. #else
  885. host->this_id = 7;
  886. #endif
  887. /*
  888. * Note : we should never encounter a board setup for ID0. So,
  889. * if we see ID0, assume that it was uninitialized and set it
  890. * to the industry standard 7.
  891. */
  892. if (!host->this_id) {
  893. printk("scsi%d : initiator ID was %d, changing to 7\n",
  894. host->host_no, host->this_id);
  895. host->this_id = 7;
  896. hostdata->this_id_mask = 1 << 7;
  897. uninitialized = 1;
  898. };
  899. if (expected_id == -1 || host->this_id != expected_id)
  900. printk("scsi%d : using initiator ID %d\n", host->host_no,
  901. host->this_id);
  902. /*
  903. * Save important registers to allow a soft reset.
  904. */
  905. /*
  906. * CTEST7 controls cache snooping, burst mode, and support for
  907. * external differential drivers. This isn't currently used - the
  908. * default value may not be optimal anyway.
  909. * Even worse, it may never have been set up since reset.
  910. */
  911. hostdata->saved_ctest7 = NCR53c7x0_read8(CTEST7_REG) & CTEST7_SAVE;
  912. revision = (NCR53c7x0_read8(CTEST8_REG) & 0xF0) >> 4;
  913. switch (revision) {
  914. case 1: revision = 0; break;
  915. case 2: revision = 1; break;
  916. case 4: revision = 2; break;
  917. case 8: revision = 3; break;
  918. default: revision = 255; break;
  919. }
  920. printk("scsi%d: Revision 0x%x\n",host->host_no,revision);
  921. if ((revision == 0 || revision == 255) && (hostdata->options & (OPTION_SYNCHRONOUS|OPTION_DISCONNECT|OPTION_ALWAYS_SYNCHRONOUS)))
  922. {
  923. printk ("scsi%d: Disabling sync working and disconnect/reselect\n",
  924. host->host_no);
  925. hostdata->options &= ~(OPTION_SYNCHRONOUS|OPTION_DISCONNECT|OPTION_ALWAYS_SYNCHRONOUS);
  926. }
  927. /*
  928. * On NCR53c700 series chips, DCNTL controls the SCSI clock divisor,
  929. * on 800 series chips, it allows for a totem-pole IRQ driver.
  930. * NOTE saved_dcntl currently overwritten in init function.
  931. * The value read here may be garbage anyway, MVME16x board at least
  932. * does not initialise chip if kernel arrived via tftp.
  933. */
  934. hostdata->saved_dcntl = NCR53c7x0_read8(DCNTL_REG);
  935. /*
  936. * DMODE controls DMA burst length, and on 700 series chips,
  937. * 286 mode and bus width
  938. * NOTE: On MVME16x, chip may have been reset, so this could be a
  939. * power-on/reset default value.
  940. */
  941. hostdata->saved_dmode = NCR53c7x0_read8(hostdata->dmode);
  942. /*
  943. * Now that burst length and enabled/disabled status is known,
  944. * clue the user in on it.
  945. */
  946. ccf = clock_to_ccf_710 (expected_clock);
  947. for (i = 0; i < 16; ++i)
  948. hostdata->cmd_allocated[i] = 0;
  949. if (hostdata->init_save_regs)
  950. hostdata->init_save_regs (host);
  951. if (hostdata->init_fixup)
  952. hostdata->init_fixup (host);
  953. if (!the_template) {
  954. the_template = host->hostt;
  955. first_host = host;
  956. }
  957. /*
  958. * Linux SCSI drivers have always been plagued with initialization
  959. * problems - some didn't work with the BIOS disabled since they expected
  960. * initialization from it, some didn't work when the networking code
  961. * was enabled and registers got scrambled, etc.
  962. *
  963. * To avoid problems like this, in the future, we will do a soft
  964. * reset on the SCSI chip, taking it back to a sane state.
  965. */
  966. hostdata->soft_reset (host);
  967. #if 1
  968. hostdata->debug_count_limit = -1;
  969. #else
  970. hostdata->debug_count_limit = 1;
  971. #endif
  972. hostdata->intrs = -1;
  973. hostdata->resets = -1;
  974. memcpy ((void *) hostdata->synchronous_want, (void *) sdtr_message,
  975. sizeof (hostdata->synchronous_want));
  976. NCR53c7x0_driver_init (host);
  977. if (request_irq(host->irq, NCR53c7x0_intr, SA_SHIRQ, "53c7xx", host))
  978. {
  979. printk("scsi%d : IRQ%d not free, detaching\n",
  980. host->host_no, host->irq);
  981. goto err_unregister;
  982. }
  983. if ((hostdata->run_tests && hostdata->run_tests(host) == -1) ||
  984. (hostdata->options & OPTION_DEBUG_TESTS_ONLY)) {
  985. /* XXX Should disable interrupts, etc. here */
  986. goto err_free_irq;
  987. } else {
  988. if (host->io_port) {
  989. host->n_io_port = 128;
  990. if (!request_region (host->io_port, host->n_io_port, "ncr53c7xx"))
  991. goto err_free_irq;
  992. }
  993. }
  994. if (NCR53c7x0_read8 (SBCL_REG) & SBCL_BSY) {
  995. printk ("scsi%d : bus wedge, doing SCSI reset\n", host->host_no);
  996. hard_reset (host);
  997. }
  998. return 0;
  999. err_free_irq:
  1000. free_irq(host->irq, NCR53c7x0_intr);
  1001. err_unregister:
  1002. scsi_unregister(host);
  1003. return -1;
  1004. }
  1005. /*
  1006. * Function : int ncr53c7xx_init(struct scsi_host_template *tpnt, int board, int chip,
  1007. * unsigned long base, int io_port, int irq, int dma, long long options,
  1008. * int clock);
  1009. *
  1010. * Purpose : initializes a NCR53c7,8x0 based on base addresses,
  1011. * IRQ, and DMA channel.
  1012. *
  1013. * Inputs : tpnt - Template for this SCSI adapter, board - board level
  1014. * product, chip - 710
  1015. *
  1016. * Returns : 0 on success, -1 on failure.
  1017. *
  1018. */
  1019. int
  1020. ncr53c7xx_init (struct scsi_host_template *tpnt, int board, int chip,
  1021. unsigned long base, int io_port, int irq, int dma,
  1022. long long options, int clock)
  1023. {
  1024. struct Scsi_Host *instance;
  1025. struct NCR53c7x0_hostdata *hostdata;
  1026. char chip_str[80];
  1027. int script_len = 0, dsa_len = 0, size = 0, max_cmd_size = 0,
  1028. schedule_size = 0, ok = 0;
  1029. void *tmp;
  1030. unsigned long page;
  1031. switch (chip) {
  1032. case 710:
  1033. case 770:
  1034. schedule_size = (tpnt->can_queue + 1) * 8 /* JUMP instruction size */;
  1035. script_len = NCR53c7xx_script_len;
  1036. dsa_len = NCR53c7xx_dsa_len;
  1037. options |= OPTION_INTFLY;
  1038. sprintf (chip_str, "NCR53c%d", chip);
  1039. break;
  1040. default:
  1041. printk("scsi-ncr53c7xx : unsupported SCSI chip %d\n", chip);
  1042. return -1;
  1043. }
  1044. printk("scsi-ncr53c7xx : %s at memory 0x%lx, io 0x%x, irq %d",
  1045. chip_str, base, io_port, irq);
  1046. if (dma == DMA_NONE)
  1047. printk("\n");
  1048. else
  1049. printk(", dma %d\n", dma);
  1050. if (options & OPTION_DEBUG_PROBE_ONLY) {
  1051. printk ("scsi-ncr53c7xx : probe only enabled, aborting initialization\n");
  1052. return -1;
  1053. }
  1054. max_cmd_size = sizeof(struct NCR53c7x0_cmd) + dsa_len +
  1055. /* Size of dynamic part of command structure : */
  1056. 2 * /* Worst case : we don't know if we need DATA IN or DATA out */
  1057. ( 2 * /* Current instructions per scatter/gather segment */
  1058. tpnt->sg_tablesize +
  1059. 3 /* Current startup / termination required per phase */
  1060. ) *
  1061. 8 /* Each instruction is eight bytes */;
  1062. /* Allocate fixed part of hostdata, dynamic part to hold appropriate
  1063. SCSI SCRIPT(tm) plus a single, maximum-sized NCR53c7x0_cmd structure.
  1064. We need a NCR53c7x0_cmd structure for scan_scsis() when we are
  1065. not loaded as a module, and when we're loaded as a module, we
  1066. can't use a non-dynamically allocated structure because modules
  1067. are vmalloc()'d, which can allow structures to cross page
  1068. boundaries and breaks our physical/virtual address assumptions
  1069. for DMA.
  1070. So, we stick it past the end of our hostdata structure.
  1071. ASSUMPTION :
  1072. Regardless of how many simultaneous SCSI commands we allow,
  1073. the probe code only executes a _single_ instruction at a time,
  1074. so we only need one here, and don't need to allocate NCR53c7x0_cmd
  1075. structures for each target until we are no longer in scan_scsis
  1076. and kmalloc() has become functional (memory_init() happens
  1077. after all device driver initialization).
  1078. */
  1079. size = sizeof(struct NCR53c7x0_hostdata) + script_len +
  1080. /* Note that alignment will be guaranteed, since we put the command
  1081. allocated at probe time after the fixed-up SCSI script, which
  1082. consists of 32 bit words, aligned on a 32 bit boundary. But
  1083. on a 64bit machine we need 8 byte alignment for hostdata->free, so
  1084. we add in another 4 bytes to take care of potential misalignment
  1085. */
  1086. (sizeof(void *) - sizeof(u32)) + max_cmd_size + schedule_size;
  1087. page = __get_free_pages(GFP_ATOMIC,1);
  1088. if(page==0)
  1089. {
  1090. printk(KERN_ERR "53c7xx: out of memory.\n");
  1091. return -ENOMEM;
  1092. }
  1093. #ifdef FORCE_DSA_ALIGNMENT
  1094. /*
  1095. * 53c710 rev.0 doesn't have an add-with-carry instruction.
  1096. * Ensure we allocate enough memory to force DSA alignment.
  1097. */
  1098. size += 256;
  1099. #endif
  1100. /* Size should be < 8K, so we can fit it in two pages. */
  1101. if (size > 8192) {
  1102. printk(KERN_ERR "53c7xx: hostdata > 8K\n");
  1103. return -1;
  1104. }
  1105. instance = scsi_register (tpnt, 4);
  1106. if (!instance)
  1107. {
  1108. free_page(page);
  1109. return -1;
  1110. }
  1111. instance->hostdata[0] = page;
  1112. memset((void *)instance->hostdata[0], 0, 8192);
  1113. cache_push(virt_to_phys((void *)(instance->hostdata[0])), 8192);
  1114. cache_clear(virt_to_phys((void *)(instance->hostdata[0])), 8192);
  1115. kernel_set_cachemode((void *)instance->hostdata[0], 8192, IOMAP_NOCACHE_SER);
  1116. /* FIXME : if we ever support an ISA NCR53c7xx based board, we
  1117. need to check if the chip is running in a 16 bit mode, and if so
  1118. unregister it if it is past the 16M (0x1000000) mark */
  1119. hostdata = (struct NCR53c7x0_hostdata *)instance->hostdata[0];
  1120. hostdata->size = size;
  1121. hostdata->script_count = script_len / sizeof(u32);
  1122. hostdata->board = board;
  1123. hostdata->chip = chip;
  1124. /*
  1125. * Being memory mapped is more desirable, since
  1126. *
  1127. * - Memory accesses may be faster.
  1128. *
  1129. * - The destination and source address spaces are the same for
  1130. * all instructions, meaning we don't have to twiddle dmode or
  1131. * any other registers.
  1132. *
  1133. * So, we try for memory mapped, and if we don't get it,
  1134. * we go for port mapped, and that failing we tell the user
  1135. * it can't work.
  1136. */
  1137. if (base) {
  1138. instance->base = base;
  1139. /* Check for forced I/O mapping */
  1140. if (!(options & OPTION_IO_MAPPED)) {
  1141. options |= OPTION_MEMORY_MAPPED;
  1142. ok = 1;
  1143. }
  1144. } else {
  1145. options &= ~OPTION_MEMORY_MAPPED;
  1146. }
  1147. if (io_port) {
  1148. instance->io_port = io_port;
  1149. options |= OPTION_IO_MAPPED;
  1150. ok = 1;
  1151. } else {
  1152. options &= ~OPTION_IO_MAPPED;
  1153. }
  1154. if (!ok) {
  1155. printk ("scsi%d : not initializing, no I/O or memory mapping known \n",
  1156. instance->host_no);
  1157. scsi_unregister (instance);
  1158. return -1;
  1159. }
  1160. instance->irq = irq;
  1161. instance->dma_channel = dma;
  1162. hostdata->options = options;
  1163. hostdata->dsa_len = dsa_len;
  1164. hostdata->max_cmd_size = max_cmd_size;
  1165. hostdata->num_cmds = 1;
  1166. hostdata->scsi_clock = clock;
  1167. /* Initialize single command */
  1168. tmp = (hostdata->script + hostdata->script_count);
  1169. #ifdef FORCE_DSA_ALIGNMENT
  1170. {
  1171. void *t = ROUNDUP(tmp, void *);
  1172. if (((u32)t & 0xff) > CmdPageStart)
  1173. t = (void *)((u32)t + 255);
  1174. t = (void *)(((u32)t & ~0xff) + CmdPageStart);
  1175. hostdata->free = t;
  1176. #if 0
  1177. printk ("scsi: Registered size increased by 256 to %d\n", size);
  1178. printk ("scsi: CmdPageStart = 0x%02x\n", CmdPageStart);
  1179. printk ("scsi: tmp = 0x%08x, hostdata->free set to 0x%08x\n",
  1180. (u32)tmp, (u32)t);
  1181. #endif
  1182. }
  1183. #else
  1184. hostdata->free = ROUNDUP(tmp, void *);
  1185. #endif
  1186. hostdata->free->real = tmp;
  1187. hostdata->free->size = max_cmd_size;
  1188. hostdata->free->free = NULL;
  1189. hostdata->free->next = NULL;
  1190. hostdata->extra_allocate = 0;
  1191. /* Allocate command start code space */
  1192. hostdata->schedule = (chip == 700 || chip == 70066) ?
  1193. NULL : (u32 *) ((char *)hostdata->free + max_cmd_size);
  1194. /*
  1195. * For diagnostic purposes, we don't really care how fast things blaze.
  1196. * For profiling, we want to access the 800ns resolution system clock,
  1197. * using a 'C' call on the host processor.
  1198. *
  1199. * Therefore, there's no need for the NCR chip to directly manipulate
  1200. * this data, and we should put it wherever is most convenient for
  1201. * Linux.
  1202. */
  1203. if (track_events)
  1204. hostdata->events = (struct NCR53c7x0_event *) (track_events ?
  1205. vmalloc (sizeof (struct NCR53c7x0_event) * track_events) : NULL);
  1206. else
  1207. hostdata->events = NULL;
  1208. if (hostdata->events) {
  1209. memset ((void *) hostdata->events, 0, sizeof(struct NCR53c7x0_event) *
  1210. track_events);
  1211. hostdata->event_size = track_events;
  1212. hostdata->event_index = 0;
  1213. } else
  1214. hostdata->event_size = 0;
  1215. return NCR53c7x0_init(instance);
  1216. }
  1217. /*
  1218. * Function : static void NCR53c7x0_init_fixup (struct Scsi_Host *host)
  1219. *
  1220. * Purpose : copy and fixup the SCSI SCRIPTS(tm) code for this device.
  1221. *
  1222. * Inputs : host - pointer to this host adapter's structure
  1223. *
  1224. */
  1225. static void
  1226. NCR53c7x0_init_fixup (struct Scsi_Host *host) {
  1227. NCR53c7x0_local_declare();
  1228. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  1229. host->hostdata[0];
  1230. unsigned char tmp;
  1231. int i, ncr_to_memory, memory_to_ncr;
  1232. u32 base;
  1233. NCR53c7x0_local_setup(host);
  1234. /* XXX - NOTE : this code MUST be made endian aware */
  1235. /* Copy code into buffer that was allocated at detection time. */
  1236. memcpy ((void *) hostdata->script, (void *) SCRIPT,
  1237. sizeof(SCRIPT));
  1238. /* Fixup labels */
  1239. for (i = 0; i < PATCHES; ++i)
  1240. hostdata->script[LABELPATCHES[i]] +=
  1241. virt_to_bus(hostdata->script);
  1242. /* Fixup addresses of constants that used to be EXTERNAL */
  1243. patch_abs_32 (hostdata->script, 0, NCR53c7xx_msg_abort,
  1244. virt_to_bus(&(hostdata->NCR53c7xx_msg_abort)));
  1245. patch_abs_32 (hostdata->script, 0, NCR53c7xx_msg_reject,
  1246. virt_to_bus(&(hostdata->NCR53c7xx_msg_reject)));
  1247. patch_abs_32 (hostdata->script, 0, NCR53c7xx_zero,
  1248. virt_to_bus(&(hostdata->NCR53c7xx_zero)));
  1249. patch_abs_32 (hostdata->script, 0, NCR53c7xx_sink,
  1250. virt_to_bus(&(hostdata->NCR53c7xx_sink)));
  1251. patch_abs_32 (hostdata->script, 0, NOP_insn,
  1252. virt_to_bus(&(hostdata->NOP_insn)));
  1253. patch_abs_32 (hostdata->script, 0, schedule,
  1254. virt_to_bus((void *) hostdata->schedule));
  1255. /* Fixup references to external variables: */
  1256. for (i = 0; i < EXTERNAL_PATCHES_LEN; ++i)
  1257. hostdata->script[EXTERNAL_PATCHES[i].offset] +=
  1258. virt_to_bus(EXTERNAL_PATCHES[i].address);
  1259. /*
  1260. * Fixup absolutes set at boot-time.
  1261. *
  1262. * All non-code absolute variables suffixed with "dsa_" and "int_"
  1263. * are constants, and need no fixup provided the assembler has done
  1264. * it for us (I don't know what the "real" NCR assembler does in
  1265. * this case, my assembler does the right magic).
  1266. */
  1267. patch_abs_rwri_data (hostdata->script, 0, dsa_save_data_pointer,
  1268. Ent_dsa_code_save_data_pointer - Ent_dsa_zero);
  1269. patch_abs_rwri_data (hostdata->script, 0, dsa_restore_pointers,
  1270. Ent_dsa_code_restore_pointers - Ent_dsa_zero);
  1271. patch_abs_rwri_data (hostdata->script, 0, dsa_check_reselect,
  1272. Ent_dsa_code_check_reselect - Ent_dsa_zero);
  1273. /*
  1274. * Just for the hell of it, preserve the settings of
  1275. * Burst Length and Enable Read Line bits from the DMODE
  1276. * register. Make sure SCRIPTS start automagically.
  1277. */
  1278. #if defined(CONFIG_MVME16x) || defined(CONFIG_BVME6000)
  1279. /* We know better what we want than 16xBug does! */
  1280. tmp = DMODE_10_BL_8 | DMODE_10_FC2;
  1281. #else
  1282. tmp = NCR53c7x0_read8(DMODE_REG_10);
  1283. tmp &= (DMODE_BL_MASK | DMODE_10_FC2 | DMODE_10_FC1 | DMODE_710_PD |
  1284. DMODE_710_UO);
  1285. #endif
  1286. if (!(hostdata->options & OPTION_MEMORY_MAPPED)) {
  1287. base = (u32) host->io_port;
  1288. memory_to_ncr = tmp|DMODE_800_DIOM;
  1289. ncr_to_memory = tmp|DMODE_800_SIOM;
  1290. } else {
  1291. base = virt_to_bus((void *)host->base);
  1292. memory_to_ncr = ncr_to_memory = tmp;
  1293. }
  1294. /* SCRATCHB_REG_10 == SCRATCHA_REG_800, as it happens */
  1295. patch_abs_32 (hostdata->script, 0, addr_scratch, base + SCRATCHA_REG_800);
  1296. patch_abs_32 (hostdata->script, 0, addr_temp, base + TEMP_REG);
  1297. patch_abs_32 (hostdata->script, 0, addr_dsa, base + DSA_REG);
  1298. /*
  1299. * I needed some variables in the script to be accessible to
  1300. * both the NCR chip and the host processor. For these variables,
  1301. * I made the arbitrary decision to store them directly in the
  1302. * hostdata structure rather than in the RELATIVE area of the
  1303. * SCRIPTS.
  1304. */
  1305. patch_abs_rwri_data (hostdata->script, 0, dmode_memory_to_memory, tmp);
  1306. patch_abs_rwri_data (hostdata->script, 0, dmode_memory_to_ncr, memory_to_ncr);
  1307. patch_abs_rwri_data (hostdata->script, 0, dmode_ncr_to_memory, ncr_to_memory);
  1308. patch_abs_32 (hostdata->script, 0, msg_buf,
  1309. virt_to_bus((void *)&(hostdata->msg_buf)));
  1310. patch_abs_32 (hostdata->script, 0, reconnect_dsa_head,
  1311. virt_to_bus((void *)&(hostdata->reconnect_dsa_head)));
  1312. patch_abs_32 (hostdata->script, 0, addr_reconnect_dsa_head,
  1313. virt_to_bus((void *)&(hostdata->addr_reconnect_dsa_head)));
  1314. patch_abs_32 (hostdata->script, 0, reselected_identify,
  1315. virt_to_bus((void *)&(hostdata->reselected_identify)));
  1316. /* reselected_tag is currently unused */
  1317. #if 0
  1318. patch_abs_32 (hostdata->script, 0, reselected_tag,
  1319. virt_to_bus((void *)&(hostdata->reselected_tag)));
  1320. #endif
  1321. patch_abs_32 (hostdata->script, 0, test_dest,
  1322. virt_to_bus((void*)&hostdata->test_dest));
  1323. patch_abs_32 (hostdata->script, 0, test_src,
  1324. virt_to_bus(&hostdata->test_source));
  1325. patch_abs_32 (hostdata->script, 0, saved_dsa,
  1326. virt_to_bus((void *)&hostdata->saved2_dsa));
  1327. patch_abs_32 (hostdata->script, 0, emulfly,
  1328. virt_to_bus((void *)&hostdata->emulated_intfly));
  1329. patch_abs_rwri_data (hostdata->script, 0, dsa_check_reselect,
  1330. (unsigned char)(Ent_dsa_code_check_reselect - Ent_dsa_zero));
  1331. /* These are for event logging; the ncr_event enum contains the
  1332. actual interrupt numbers. */
  1333. #ifdef A_int_EVENT_SELECT
  1334. patch_abs_32 (hostdata->script, 0, int_EVENT_SELECT, (u32) EVENT_SELECT);
  1335. #endif
  1336. #ifdef A_int_EVENT_DISCONNECT
  1337. patch_abs_32 (hostdata->script, 0, int_EVENT_DISCONNECT, (u32) EVENT_DISCONNECT);
  1338. #endif
  1339. #ifdef A_int_EVENT_RESELECT
  1340. patch_abs_32 (hostdata->script, 0, int_EVENT_RESELECT, (u32) EVENT_RESELECT);
  1341. #endif
  1342. #ifdef A_int_EVENT_COMPLETE
  1343. patch_abs_32 (hostdata->script, 0, int_EVENT_COMPLETE, (u32) EVENT_COMPLETE);
  1344. #endif
  1345. #ifdef A_int_EVENT_IDLE
  1346. patch_abs_32 (hostdata->script, 0, int_EVENT_IDLE, (u32) EVENT_IDLE);
  1347. #endif
  1348. #ifdef A_int_EVENT_SELECT_FAILED
  1349. patch_abs_32 (hostdata->script, 0, int_EVENT_SELECT_FAILED,
  1350. (u32) EVENT_SELECT_FAILED);
  1351. #endif
  1352. #ifdef A_int_EVENT_BEFORE_SELECT
  1353. patch_abs_32 (hostdata->script, 0, int_EVENT_BEFORE_SELECT,
  1354. (u32) EVENT_BEFORE_SELECT);
  1355. #endif
  1356. #ifdef A_int_EVENT_RESELECT_FAILED
  1357. patch_abs_32 (hostdata->script, 0, int_EVENT_RESELECT_FAILED,
  1358. (u32) EVENT_RESELECT_FAILED);
  1359. #endif
  1360. /*
  1361. * Make sure the NCR and Linux code agree on the location of
  1362. * certain fields.
  1363. */
  1364. hostdata->E_accept_message = Ent_accept_message;
  1365. hostdata->E_command_complete = Ent_command_complete;
  1366. hostdata->E_cmdout_cmdout = Ent_cmdout_cmdout;
  1367. hostdata->E_data_transfer = Ent_data_transfer;
  1368. hostdata->E_debug_break = Ent_debug_break;
  1369. hostdata->E_dsa_code_template = Ent_dsa_code_template;
  1370. hostdata->E_dsa_code_template_end = Ent_dsa_code_template_end;
  1371. hostdata->E_end_data_transfer = Ent_end_data_transfer;
  1372. hostdata->E_initiator_abort = Ent_initiator_abort;
  1373. hostdata->E_msg_in = Ent_msg_in;
  1374. hostdata->E_other_transfer = Ent_other_transfer;
  1375. hostdata->E_other_in = Ent_other_in;
  1376. hostdata->E_other_out = Ent_other_out;
  1377. hostdata->E_reject_message = Ent_reject_message;
  1378. hostdata->E_respond_message = Ent_respond_message;
  1379. hostdata->E_select = Ent_select;
  1380. hostdata->E_select_msgout = Ent_select_msgout;
  1381. hostdata->E_target_abort = Ent_target_abort;
  1382. #ifdef Ent_test_0
  1383. hostdata->E_test_0 = Ent_test_0;
  1384. #endif
  1385. hostdata->E_test_1 = Ent_test_1;
  1386. hostdata->E_test_2 = Ent_test_2;
  1387. #ifdef Ent_test_3
  1388. hostdata->E_test_3 = Ent_test_3;
  1389. #endif
  1390. hostdata->E_wait_reselect = Ent_wait_reselect;
  1391. hostdata->E_dsa_code_begin = Ent_dsa_code_begin;
  1392. hostdata->dsa_cmdout = A_dsa_cmdout;
  1393. hostdata->dsa_cmnd = A_dsa_cmnd;
  1394. hostdata->dsa_datain = A_dsa_datain;
  1395. hostdata->dsa_dataout = A_dsa_dataout;
  1396. hostdata->dsa_end = A_dsa_end;
  1397. hostdata->dsa_msgin = A_dsa_msgin;
  1398. hostdata->dsa_msgout = A_dsa_msgout;
  1399. hostdata->dsa_msgout_other = A_dsa_msgout_other;
  1400. hostdata->dsa_next = A_dsa_next;
  1401. hostdata->dsa_select = A_dsa_select;
  1402. hostdata->dsa_start = Ent_dsa_code_template - Ent_dsa_zero;
  1403. hostdata->dsa_status = A_dsa_status;
  1404. hostdata->dsa_jump_dest = Ent_dsa_code_fix_jump - Ent_dsa_zero +
  1405. 8 /* destination operand */;
  1406. /* sanity check */
  1407. if (A_dsa_fields_start != Ent_dsa_code_template_end -
  1408. Ent_dsa_zero)
  1409. printk("scsi%d : NCR dsa_fields start is %d not %d\n",
  1410. host->host_no, A_dsa_fields_start, Ent_dsa_code_template_end -
  1411. Ent_dsa_zero);
  1412. printk("scsi%d : NCR code relocated to 0x%lx (virt 0x%p)\n", host->host_no,
  1413. virt_to_bus(hostdata->script), hostdata->script);
  1414. }
  1415. /*
  1416. * Function : static int NCR53c7xx_run_tests (struct Scsi_Host *host)
  1417. *
  1418. * Purpose : run various verification tests on the NCR chip,
  1419. * including interrupt generation, and proper bus mastering
  1420. * operation.
  1421. *
  1422. * Inputs : host - a properly initialized Scsi_Host structure
  1423. *
  1424. * Preconditions : the NCR chip must be in a halted state.
  1425. *
  1426. * Returns : 0 if all tests were successful, -1 on error.
  1427. *
  1428. */
  1429. static int
  1430. NCR53c7xx_run_tests (struct Scsi_Host *host) {
  1431. NCR53c7x0_local_declare();
  1432. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  1433. host->hostdata[0];
  1434. unsigned long timeout;
  1435. u32 start;
  1436. int failed, i;
  1437. unsigned long flags;
  1438. NCR53c7x0_local_setup(host);
  1439. /* The NCR chip _must_ be idle to run the test scripts */
  1440. local_irq_save(flags);
  1441. if (!hostdata->idle) {
  1442. printk ("scsi%d : chip not idle, aborting tests\n", host->host_no);
  1443. local_irq_restore(flags);
  1444. return -1;
  1445. }
  1446. /*
  1447. * Check for functional interrupts, this could work as an
  1448. * autoprobe routine.
  1449. */
  1450. if ((hostdata->options & OPTION_DEBUG_TEST1) &&
  1451. hostdata->state != STATE_DISABLED) {
  1452. hostdata->idle = 0;
  1453. hostdata->test_running = 1;
  1454. hostdata->test_completed = -1;
  1455. hostdata->test_dest = 0;
  1456. hostdata->test_source = 0xdeadbeef;
  1457. start = virt_to_bus (hostdata->script) + hostdata->E_test_1;
  1458. hostdata->state = STATE_RUNNING;
  1459. printk ("scsi%d : test 1", host->host_no);
  1460. NCR53c7x0_write32 (DSP_REG, start);
  1461. if (hostdata->options & OPTION_DEBUG_TRACE)
  1462. NCR53c7x0_write8 (DCNTL_REG, hostdata->saved_dcntl | DCNTL_SSM |
  1463. DCNTL_STD);
  1464. printk (" started\n");
  1465. local_irq_restore(flags);
  1466. /*
  1467. * This is currently a .5 second timeout, since (in theory) no slow
  1468. * board will take that long. In practice, we've seen one
  1469. * pentium which occassionally fails with this, but works with
  1470. * 10 times as much?
  1471. */
  1472. timeout = jiffies + 5 * HZ / 10;
  1473. while ((hostdata->test_completed == -1) && time_before(jiffies, timeout))
  1474. barrier();
  1475. failed = 1;
  1476. if (hostdata->test_completed == -1)
  1477. printk ("scsi%d : driver test 1 timed out%s\n",host->host_no ,
  1478. (hostdata->test_dest == 0xdeadbeef) ?
  1479. " due to lost interrupt.\n"
  1480. " Please verify that the correct IRQ is being used for your board,\n"
  1481. : "");
  1482. else if (hostdata->test_completed != 1)
  1483. printk ("scsi%d : test 1 bad interrupt value (%d)\n",
  1484. host->host_no, hostdata->test_completed);
  1485. else
  1486. failed = (hostdata->test_dest != 0xdeadbeef);
  1487. if (hostdata->test_dest != 0xdeadbeef) {
  1488. printk ("scsi%d : driver test 1 read 0x%x instead of 0xdeadbeef indicating a\n"
  1489. " probable cache invalidation problem. Please configure caching\n"
  1490. " as write-through or disabled\n",
  1491. host->host_no, hostdata->test_dest);
  1492. }
  1493. if (failed) {
  1494. printk ("scsi%d : DSP = 0x%p (script at 0x%p, start at 0x%x)\n",
  1495. host->host_no, bus_to_virt(NCR53c7x0_read32(DSP_REG)),
  1496. hostdata->script, start);
  1497. printk ("scsi%d : DSPS = 0x%x\n", host->host_no,
  1498. NCR53c7x0_read32(DSPS_REG));
  1499. local_irq_restore(flags);
  1500. return -1;
  1501. }
  1502. hostdata->test_running = 0;
  1503. }
  1504. if ((hostdata->options & OPTION_DEBUG_TEST2) &&
  1505. hostdata->state != STATE_DISABLED) {
  1506. u32 dsa[48];
  1507. unsigned char identify = IDENTIFY(0, 0);
  1508. unsigned char cmd[6];
  1509. unsigned char data[36];
  1510. unsigned char status = 0xff;
  1511. unsigned char msg = 0xff;
  1512. cmd[0] = INQUIRY;
  1513. cmd[1] = cmd[2] = cmd[3] = cmd[5] = 0;
  1514. cmd[4] = sizeof(data);
  1515. dsa[2] = 1;
  1516. dsa[3] = virt_to_bus(&identify);
  1517. dsa[4] = 6;
  1518. dsa[5] = virt_to_bus(&cmd);
  1519. dsa[6] = sizeof(data);
  1520. dsa[7] = virt_to_bus(&data);
  1521. dsa[8] = 1;
  1522. dsa[9] = virt_to_bus(&status);
  1523. dsa[10] = 1;
  1524. dsa[11] = virt_to_bus(&msg);
  1525. for (i = 0; i < 6; ++i) {
  1526. #ifdef VALID_IDS
  1527. if (!hostdata->valid_ids[i])
  1528. continue;
  1529. #endif
  1530. local_irq_disable();
  1531. if (!hostdata->idle) {
  1532. printk ("scsi%d : chip not idle, aborting tests\n", host->host_no);
  1533. local_irq_restore(flags);
  1534. return -1;
  1535. }
  1536. /* 710: bit mapped scsi ID, async */
  1537. dsa[0] = (1 << i) << 16;
  1538. hostdata->idle = 0;
  1539. hostdata->test_running = 2;
  1540. hostdata->test_completed = -1;
  1541. start = virt_to_bus(hostdata->script) + hostdata->E_test_2;
  1542. hostdata->state = STATE_RUNNING;
  1543. NCR53c7x0_write32 (DSA_REG, virt_to_bus(dsa));
  1544. NCR53c7x0_write32 (DSP_REG, start);
  1545. if (hostdata->options & OPTION_DEBUG_TRACE)
  1546. NCR53c7x0_write8 (DCNTL_REG, hostdata->saved_dcntl |
  1547. DCNTL_SSM | DCNTL_STD);
  1548. local_irq_restore(flags);
  1549. timeout = jiffies + 5 * HZ; /* arbitrary */
  1550. while ((hostdata->test_completed == -1) && time_before(jiffies, timeout))
  1551. barrier();
  1552. NCR53c7x0_write32 (DSA_REG, 0);
  1553. if (hostdata->test_completed == 2) {
  1554. data[35] = 0;
  1555. printk ("scsi%d : test 2 INQUIRY to target %d, lun 0 : %s\n",
  1556. host->host_no, i, data + 8);
  1557. printk ("scsi%d : status ", host->host_no);
  1558. scsi_print_status (status);
  1559. printk ("\nscsi%d : message ", host->host_no);
  1560. spi_print_msg(&msg);
  1561. printk ("\n");
  1562. } else if (hostdata->test_completed == 3) {
  1563. printk("scsi%d : test 2 no connection with target %d\n",
  1564. host->host_no, i);
  1565. if (!hostdata->idle) {
  1566. printk("scsi%d : not idle\n", host->host_no);
  1567. local_irq_restore(flags);
  1568. return -1;
  1569. }
  1570. } else if (hostdata->test_completed == -1) {
  1571. printk ("scsi%d : test 2 timed out\n", host->host_no);
  1572. local_irq_restore(flags);
  1573. return -1;
  1574. }
  1575. hostdata->test_running = 0;
  1576. }
  1577. }
  1578. local_irq_restore(flags);
  1579. return 0;
  1580. }
  1581. /*
  1582. * Function : static void NCR53c7xx_dsa_fixup (struct NCR53c7x0_cmd *cmd)
  1583. *
  1584. * Purpose : copy the NCR53c8xx dsa structure into cmd's dsa buffer,
  1585. * performing all necessary relocation.
  1586. *
  1587. * Inputs : cmd, a NCR53c7x0_cmd structure with a dsa area large
  1588. * enough to hold the NCR53c8xx dsa.
  1589. */
  1590. static void
  1591. NCR53c7xx_dsa_fixup (struct NCR53c7x0_cmd *cmd) {
  1592. Scsi_Cmnd *c = cmd->cmd;
  1593. struct Scsi_Host *host = c->device->host;
  1594. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  1595. host->hostdata[0];
  1596. int i;
  1597. memcpy (cmd->dsa, hostdata->script + (hostdata->E_dsa_code_template / 4),
  1598. hostdata->E_dsa_code_template_end - hostdata->E_dsa_code_template);
  1599. /*
  1600. * Note : within the NCR 'C' code, dsa points to the _start_
  1601. * of the DSA structure, and _not_ the offset of dsa_zero within
  1602. * that structure used to facilitate shorter signed offsets
  1603. * for the 8 bit ALU.
  1604. *
  1605. * The implications of this are that
  1606. *
  1607. * - 32 bit A_dsa_* absolute values require an additional
  1608. * dsa_zero added to their value to be correct, since they are
  1609. * relative to dsa_zero which is in essentially a separate
  1610. * space from the code symbols.
  1611. *
  1612. * - All other symbols require no special treatment.
  1613. */
  1614. patch_abs_tci_data (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1615. dsa_temp_lun, c->device->lun);
  1616. patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1617. dsa_temp_addr_next, virt_to_bus(&cmd->dsa_next_addr));
  1618. patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1619. dsa_temp_next, virt_to_bus(cmd->dsa) + Ent_dsa_zero -
  1620. Ent_dsa_code_template + A_dsa_next);
  1621. patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1622. dsa_temp_sync, virt_to_bus((void *)hostdata->sync[c->device->id].script));
  1623. patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1624. dsa_sscf_710, virt_to_bus((void *)&hostdata->sync[c->device->id].sscf_710));
  1625. patch_abs_tci_data (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1626. dsa_temp_target, 1 << c->device->id);
  1627. /* XXX - new pointer stuff */
  1628. patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1629. dsa_temp_addr_saved_pointer, virt_to_bus(&cmd->saved_data_pointer));
  1630. patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1631. dsa_temp_addr_saved_residual, virt_to_bus(&cmd->saved_residual));
  1632. patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1633. dsa_temp_addr_residual, virt_to_bus(&cmd->residual));
  1634. /* XXX - new start stuff */
  1635. patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1636. dsa_temp_addr_dsa_value, virt_to_bus(&cmd->dsa_addr));
  1637. }
  1638. /*
  1639. * Function : run_process_issue_queue (void)
  1640. *
  1641. * Purpose : insure that the coroutine is running and will process our
  1642. * request. process_issue_queue_running is checked/set here (in an
  1643. * inline function) rather than in process_issue_queue itself to reduce
  1644. * the chances of stack overflow.
  1645. *
  1646. */
  1647. static volatile int process_issue_queue_running = 0;
  1648. static __inline__ void
  1649. run_process_issue_queue(void) {
  1650. unsigned long flags;
  1651. local_irq_save(flags);
  1652. if (!process_issue_queue_running) {
  1653. process_issue_queue_running = 1;
  1654. process_issue_queue(flags);
  1655. /*
  1656. * process_issue_queue_running is cleared in process_issue_queue
  1657. * once it can't do more work, and process_issue_queue exits with
  1658. * interrupts disabled.
  1659. */
  1660. }
  1661. local_irq_restore(flags);
  1662. }
  1663. /*
  1664. * Function : static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int
  1665. * result)
  1666. *
  1667. * Purpose : mark SCSI command as finished, OR'ing the host portion
  1668. * of the result word into the result field of the corresponding
  1669. * Scsi_Cmnd structure, and removing it from the internal queues.
  1670. *
  1671. * Inputs : cmd - command, result - entire result field
  1672. *
  1673. * Preconditions : the NCR chip should be in a halted state when
  1674. * abnormal_finished is run, since it modifies structures which
  1675. * the NCR expects to have exclusive access to.
  1676. */
  1677. static void
  1678. abnormal_finished (struct NCR53c7x0_cmd *cmd, int result) {
  1679. Scsi_Cmnd *c = cmd->cmd;
  1680. struct Scsi_Host *host = c->device->host;
  1681. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  1682. host->hostdata[0];
  1683. unsigned long flags;
  1684. int left, found;
  1685. volatile struct NCR53c7x0_cmd * linux_search;
  1686. volatile struct NCR53c7x0_cmd * volatile *linux_prev;
  1687. volatile u32 *ncr_prev, *ncrcurrent, ncr_search;
  1688. #if 0
  1689. printk ("scsi%d: abnormal finished\n", host->host_no);
  1690. #endif
  1691. local_irq_save(flags);
  1692. found = 0;
  1693. /*
  1694. * Traverse the NCR issue array until we find a match or run out
  1695. * of instructions. Instructions in the NCR issue array are
  1696. * either JUMP or NOP instructions, which are 2 words in length.
  1697. */
  1698. for (found = 0, left = host->can_queue, ncrcurrent = hostdata->schedule;
  1699. left > 0; --left, ncrcurrent += 2)
  1700. {
  1701. if (issue_to_cmd (host, hostdata, (u32 *) ncrcurrent) == cmd)
  1702. {
  1703. ncrcurrent[0] = hostdata->NOP_insn;
  1704. ncrcurrent[1] = 0xdeadbeef;
  1705. ++found;
  1706. break;
  1707. }
  1708. }
  1709. /*
  1710. * Traverse the NCR reconnect list of DSA structures until we find
  1711. * a pointer to this dsa or have found too many command structures.
  1712. * We let prev point at the next field of the previous element or
  1713. * head of the list, so we don't do anything different for removing
  1714. * the head element.
  1715. */
  1716. for (left = host->can_queue,
  1717. ncr_search = hostdata->reconnect_dsa_head,
  1718. ncr_prev = &hostdata->reconnect_dsa_head;
  1719. left >= 0 && ncr_search &&
  1720. ((char*)bus_to_virt(ncr_search) + hostdata->dsa_start)
  1721. != (char *) cmd->dsa;
  1722. ncr_prev = (u32*) ((char*)bus_to_virt(ncr_search) +
  1723. hostdata->dsa_next), ncr_search = *ncr_prev, --left);
  1724. if (left < 0)
  1725. printk("scsi%d: loop detected in ncr reconncect list\n",
  1726. host->host_no);
  1727. else if (ncr_search) {
  1728. if (found)
  1729. printk("scsi%d: scsi %ld in ncr issue array and reconnect lists\n",
  1730. host->host_no, c->pid);
  1731. else {
  1732. volatile u32 * next = (u32 *)
  1733. ((char *)bus_to_virt(ncr_search) + hostdata->dsa_next);
  1734. *ncr_prev = *next;
  1735. /* If we're at the tail end of the issue queue, update that pointer too. */
  1736. found = 1;
  1737. }
  1738. }
  1739. /*
  1740. * Traverse the host running list until we find this command or discover
  1741. * we have too many elements, pointing linux_prev at the next field of the
  1742. * linux_previous element or head of the list, search at this element.
  1743. */
  1744. for (left = host->can_queue, linux_search = hostdata->running_list,
  1745. linux_prev = &hostdata->running_list;
  1746. left >= 0 && linux_search && linux_search != cmd;
  1747. linux_prev = &(linux_search->next),
  1748. linux_search = linux_search->next, --left);
  1749. if (left < 0)
  1750. printk ("scsi%d: loop detected in host running list for scsi pid %ld\n",
  1751. host->host_no, c->pid);
  1752. else if (linux_search) {
  1753. *linux_prev = linux_search->next;
  1754. --hostdata->busy[c->device->id][c->device->lun];
  1755. }
  1756. /* Return the NCR command structure to the free list */
  1757. cmd->next = hostdata->free;
  1758. hostdata->free = cmd;
  1759. c->host_scribble = NULL;
  1760. /* And return */
  1761. c->result = result;
  1762. c->scsi_done(c);
  1763. local_irq_restore(flags);
  1764. run_process_issue_queue();
  1765. }
  1766. /*
  1767. * Function : static void intr_break (struct Scsi_Host *host,
  1768. * struct NCR53c7x0_cmd *cmd)
  1769. *
  1770. * Purpose : Handler for breakpoint interrupts from a SCSI script
  1771. *
  1772. * Inputs : host - pointer to this host adapter's structure,
  1773. * cmd - pointer to the command (if any) dsa was pointing
  1774. * to.
  1775. *
  1776. */
  1777. static void
  1778. intr_break (struct Scsi_Host *host, struct
  1779. NCR53c7x0_cmd *cmd) {
  1780. NCR53c7x0_local_declare();
  1781. struct NCR53c7x0_break *bp;
  1782. #if 0
  1783. Scsi_Cmnd *c = cmd ? cmd->cmd : NULL;
  1784. #endif
  1785. u32 *dsp;
  1786. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  1787. host->hostdata[0];
  1788. unsigned long flags;
  1789. NCR53c7x0_local_setup(host);
  1790. /*
  1791. * Find the break point corresponding to this address, and
  1792. * dump the appropriate debugging information to standard
  1793. * output.
  1794. */
  1795. local_irq_save(flags);
  1796. dsp = (u32 *) bus_to_virt(NCR53c7x0_read32(DSP_REG));
  1797. for (bp = hostdata->breakpoints; bp && bp->address != dsp;
  1798. bp = bp->next);
  1799. if (!bp)
  1800. panic("scsi%d : break point interrupt from %p with no breakpoint!",
  1801. host->host_no, dsp);
  1802. /*
  1803. * Configure the NCR chip for manual start mode, so that we can
  1804. * point the DSP register at the instruction that follows the
  1805. * INT int_debug_break instruction.
  1806. */
  1807. NCR53c7x0_write8 (hostdata->dmode,
  1808. NCR53c7x0_read8(hostdata->dmode)|DMODE_MAN);
  1809. /*
  1810. * And update the DSP register, using the size of the old
  1811. * instruction in bytes.
  1812. */
  1813. local_irq_restore(flags);
  1814. }
  1815. /*
  1816. * Function : static void print_synchronous (const char *prefix,
  1817. * const unsigned char *msg)
  1818. *
  1819. * Purpose : print a pretty, user and machine parsable representation
  1820. * of a SDTR message, including the "real" parameters, data
  1821. * clock so we can tell transfer rate at a glance.
  1822. *
  1823. * Inputs ; prefix - text to prepend, msg - SDTR message (5 bytes)
  1824. */
  1825. static void
  1826. print_synchronous (const char *prefix, const unsigned char *msg) {
  1827. if (msg[4]) {
  1828. int Hz = 1000000000 / (msg[3] * 4);
  1829. int integer = Hz / 1000000;
  1830. int fraction = (Hz - (integer * 1000000)) / 10000;
  1831. printk ("%speriod %dns offset %d %d.%02dMHz %s SCSI%s\n",
  1832. prefix, (int) msg[3] * 4, (int) msg[4], integer, fraction,
  1833. (((msg[3] * 4) < 200) ? "FAST" : "synchronous"),
  1834. (((msg[3] * 4) < 200) ? "-II" : ""));
  1835. } else
  1836. printk ("%sasynchronous SCSI\n", prefix);
  1837. }
  1838. /*
  1839. * Function : static void set_synchronous (struct Scsi_Host *host,
  1840. * int target, int sxfer, int scntl3, int now_connected)
  1841. *
  1842. * Purpose : reprogram transfers between the selected SCSI initiator and
  1843. * target with the given register values; in the indirect
  1844. * select operand, reselection script, and chip registers.
  1845. *
  1846. * Inputs : host - NCR53c7,8xx SCSI host, target - number SCSI target id,
  1847. * sxfer and scntl3 - NCR registers. now_connected - if non-zero,
  1848. * we should reprogram the registers now too.
  1849. *
  1850. * NOTE: For 53c710, scntl3 is actually used for SCF bits from
  1851. * SBCL, as we don't have a SCNTL3.
  1852. */
  1853. static void
  1854. set_synchronous (struct Scsi_Host *host, int target, int sxfer, int scntl3,
  1855. int now_connected) {
  1856. NCR53c7x0_local_declare();
  1857. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  1858. host->hostdata[0];
  1859. u32 *script;
  1860. NCR53c7x0_local_setup(host);
  1861. /* These are eight bit registers */
  1862. sxfer &= 0xff;
  1863. scntl3 &= 0xff;
  1864. hostdata->sync[target].sxfer_sanity = sxfer;
  1865. hostdata->sync[target].scntl3_sanity = scntl3;
  1866. /*
  1867. * HARD CODED : synchronous script is EIGHT words long. This
  1868. * must agree with 53c7.8xx.h
  1869. */
  1870. if ((hostdata->chip != 700) && (hostdata->chip != 70066)) {
  1871. hostdata->sync[target].select_indirect = (1 << target) << 16 |
  1872. (sxfer << 8);
  1873. hostdata->sync[target].sscf_710 = scntl3;
  1874. script = (u32 *) hostdata->sync[target].script;
  1875. /* XXX - add NCR53c7x0 code to reprogram SCF bits if we want to */
  1876. script[0] = ((DCMD_TYPE_RWRI | DCMD_RWRI_OPC_MODIFY |
  1877. DCMD_RWRI_OP_MOVE) << 24) |
  1878. (SBCL_REG << 16) | (scntl3 << 8);
  1879. script[1] = 0;
  1880. script += 2;
  1881. script[0] = ((DCMD_TYPE_RWRI | DCMD_RWRI_OPC_MODIFY |
  1882. DCMD_RWRI_OP_MOVE) << 24) |
  1883. (SXFER_REG << 16) | (sxfer << 8);
  1884. script[1] = 0;
  1885. script += 2;
  1886. #ifdef DEBUG_SYNC_INTR
  1887. if (hostdata->options & OPTION_DEBUG_DISCONNECT) {
  1888. script[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_INT) << 24) | DBC_TCI_TRUE;
  1889. script[1] = DEBUG_SYNC_INTR;
  1890. script += 2;
  1891. }
  1892. #endif
  1893. script[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_RETURN) << 24) | DBC_TCI_TRUE;
  1894. script[1] = 0;
  1895. script += 2;
  1896. }
  1897. if (hostdata->options & OPTION_DEBUG_SYNCHRONOUS)
  1898. printk ("scsi%d : target %d sync parameters are sxfer=0x%x, scntl3=0x%x\n",
  1899. host->host_no, target, sxfer, scntl3);
  1900. if (now_connected) {
  1901. NCR53c7x0_write8(SBCL_REG, scntl3);
  1902. NCR53c7x0_write8(SXFER_REG, sxfer);
  1903. }
  1904. }
  1905. /*
  1906. * Function : static int asynchronous (struct Scsi_Host *host, int target)
  1907. *
  1908. * Purpose : reprogram between the selected SCSI Host adapter and target
  1909. * (assumed to be currently connected) for asynchronous transfers.
  1910. *
  1911. * Inputs : host - SCSI host structure, target - numeric target ID.
  1912. *
  1913. * Preconditions : the NCR chip should be in one of the halted states
  1914. */
  1915. static void
  1916. asynchronous (struct Scsi_Host *host, int target) {
  1917. NCR53c7x0_local_declare();
  1918. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  1919. host->hostdata[0];
  1920. NCR53c7x0_local_setup(host);
  1921. set_synchronous (host, target, /* no offset */ 0, hostdata->saved_scntl3,
  1922. 1);
  1923. printk ("scsi%d : setting target %d to asynchronous SCSI\n",
  1924. host->host_no, target);
  1925. }
  1926. /*
  1927. * XXX - do we want to go out of our way (ie, add extra code to selection
  1928. * in the NCR53c710/NCR53c720 script) to reprogram the synchronous
  1929. * conversion bits, or can we be content in just setting the
  1930. * sxfer bits? I chose to do so [richard@sleepie.demon.co.uk]
  1931. */
  1932. /* Table for NCR53c8xx synchronous values */
  1933. /* This table is also correct for 710, allowing that scf=4 is equivalent
  1934. * of SSCF=0 (ie use DCNTL, divide by 3) for a 50.01-66.00MHz clock.
  1935. * For any other clock values, we cannot use entries with SCF values of
  1936. * 4. I guess that for a 66MHz clock, the slowest it will set is 2MHz,
  1937. * and for a 50MHz clock, the slowest will be 2.27Mhz. Should check
  1938. * that a device doesn't try and negotiate sync below these limits!
  1939. */
  1940. static const struct {
  1941. int div; /* Total clock divisor * 10 */
  1942. unsigned char scf; /* */
  1943. unsigned char tp; /* 4 + tp = xferp divisor */
  1944. } syncs[] = {
  1945. /* div scf tp div scf tp div scf tp */
  1946. { 40, 1, 0}, { 50, 1, 1}, { 60, 1, 2},
  1947. { 70, 1, 3}, { 75, 2, 1}, { 80, 1, 4},
  1948. { 90, 1, 5}, { 100, 1, 6}, { 105, 2, 3},
  1949. { 110, 1, 7}, { 120, 2, 4}, { 135, 2, 5},
  1950. { 140, 3, 3}, { 150, 2, 6}, { 160, 3, 4},
  1951. { 165, 2, 7}, { 180, 3, 5}, { 200, 3, 6},
  1952. { 210, 4, 3}, { 220, 3, 7}, { 240, 4, 4},
  1953. { 270, 4, 5}, { 300, 4, 6}, { 330, 4, 7}
  1954. };
  1955. /*
  1956. * Function : static void synchronous (struct Scsi_Host *host, int target,
  1957. * char *msg)
  1958. *
  1959. * Purpose : reprogram transfers between the selected SCSI initiator and
  1960. * target for synchronous SCSI transfers such that the synchronous
  1961. * offset is less than that requested and period at least as long
  1962. * as that requested. Also modify *msg such that it contains
  1963. * an appropriate response.
  1964. *
  1965. * Inputs : host - NCR53c7,8xx SCSI host, target - number SCSI target id,
  1966. * msg - synchronous transfer request.
  1967. */
  1968. static void
  1969. synchronous (struct Scsi_Host *host, int target, char *msg) {
  1970. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  1971. host->hostdata[0];
  1972. int desire, divisor, i, limit;
  1973. unsigned char scntl3, sxfer;
  1974. /* The diagnostic message fits on one line, even with max. width integers */
  1975. char buf[80];
  1976. /* Desired transfer clock in Hz */
  1977. desire = 1000000000L / (msg[3] * 4);
  1978. /* Scale the available SCSI clock by 10 so we get tenths */
  1979. divisor = (hostdata->scsi_clock * 10) / desire;
  1980. /* NCR chips can handle at most an offset of 8 */
  1981. if (msg[4] > 8)
  1982. msg[4] = 8;
  1983. if (hostdata->options & OPTION_DEBUG_SDTR)
  1984. printk("scsi%d : optimal synchronous divisor of %d.%01d\n",
  1985. host->host_no, divisor / 10, divisor % 10);
  1986. limit = (sizeof(syncs) / sizeof(syncs[0]) -1);
  1987. for (i = 0; (i < limit) && (divisor > syncs[i].div); ++i);
  1988. if (hostdata->options & OPTION_DEBUG_SDTR)
  1989. printk("scsi%d : selected synchronous divisor of %d.%01d\n",
  1990. host->host_no, syncs[i].div / 10, syncs[i].div % 10);
  1991. msg[3] = ((1000000000L / hostdata->scsi_clock) * syncs[i].div / 10 / 4);
  1992. if (hostdata->options & OPTION_DEBUG_SDTR)
  1993. printk("scsi%d : selected synchronous period of %dns\n", host->host_no,
  1994. msg[3] * 4);
  1995. scntl3 = syncs[i].scf;
  1996. sxfer = (msg[4] << SXFER_MO_SHIFT) | (syncs[i].tp << 4);
  1997. if (hostdata->options & OPTION_DEBUG_SDTR)
  1998. printk ("scsi%d : sxfer=0x%x scntl3=0x%x\n",
  1999. host->host_no, (int) sxfer, (int) scntl3);
  2000. set_synchronous (host, target, sxfer, scntl3, 1);
  2001. sprintf (buf, "scsi%d : setting target %d to ", host->host_no, target);
  2002. print_synchronous (buf, msg);
  2003. }
  2004. /*
  2005. * Function : static int NCR53c7x0_dstat_sir_intr (struct Scsi_Host *host,
  2006. * struct NCR53c7x0_cmd *cmd)
  2007. *
  2008. * Purpose : Handler for INT generated instructions for the
  2009. * NCR53c810/820 SCSI SCRIPT
  2010. *
  2011. * Inputs : host - pointer to this host adapter's structure,
  2012. * cmd - pointer to the command (if any) dsa was pointing
  2013. * to.
  2014. *
  2015. */
  2016. static int
  2017. NCR53c7x0_dstat_sir_intr (struct Scsi_Host *host, struct
  2018. NCR53c7x0_cmd *cmd) {
  2019. NCR53c7x0_local_declare();
  2020. int print;
  2021. Scsi_Cmnd *c = cmd ? cmd->cmd : NULL;
  2022. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  2023. host->hostdata[0];
  2024. u32 dsps,*dsp; /* Argument of the INT instruction */
  2025. NCR53c7x0_local_setup(host);
  2026. dsps = NCR53c7x0_read32(DSPS_REG);
  2027. dsp = (u32 *) bus_to_virt(NCR53c7x0_read32(DSP_REG));
  2028. /* RGH 150597: Frig. Commands which fail with Check Condition are
  2029. * Flagged as successful - hack dsps to indicate check condition */
  2030. #if 0
  2031. /* RGH 200597: Need to disable for BVME6000, as it gets Check Conditions
  2032. * and then dies. Seems to handle Check Condition at startup, but
  2033. * not mid kernel build. */
  2034. if (dsps == A_int_norm_emulateintfly && cmd && cmd->result == 2)
  2035. dsps = A_int_err_check_condition;
  2036. #endif
  2037. if (hostdata->options & OPTION_DEBUG_INTR)
  2038. printk ("scsi%d : DSPS = 0x%x\n", host->host_no, dsps);
  2039. switch (dsps) {
  2040. case A_int_msg_1:
  2041. print = 1;
  2042. switch (hostdata->msg_buf[0]) {
  2043. /*
  2044. * Unless we've initiated synchronous negotiation, I don't
  2045. * think that this should happen.
  2046. */
  2047. case MESSAGE_REJECT:
  2048. hostdata->dsp = hostdata->script + hostdata->E_accept_message /
  2049. sizeof(u32);
  2050. hostdata->dsp_changed = 1;
  2051. if (cmd && (cmd->flags & CMD_FLAG_SDTR)) {
  2052. printk ("scsi%d : target %d rejected SDTR\n", host->host_no,
  2053. c->device->id);
  2054. cmd->flags &= ~CMD_FLAG_SDTR;
  2055. asynchronous (host, c->device->id);
  2056. print = 0;
  2057. }
  2058. break;
  2059. case INITIATE_RECOVERY:
  2060. printk ("scsi%d : extended contingent allegiance not supported yet, rejecting\n",
  2061. host->host_no);
  2062. /* Fall through to default */
  2063. hostdata->dsp = hostdata->script + hostdata->E_reject_message /
  2064. sizeof(u32);
  2065. hostdata->dsp_changed = 1;
  2066. break;
  2067. default:
  2068. printk ("scsi%d : unsupported message, rejecting\n",
  2069. host->host_no);
  2070. hostdata->dsp = hostdata->script + hostdata->E_reject_message /
  2071. sizeof(u32);
  2072. hostdata->dsp_changed = 1;
  2073. }
  2074. if (print) {
  2075. printk ("scsi%d : received message", host->host_no);
  2076. if (c)
  2077. printk (" from target %d lun %d ", c->device->id, c->device->lun);
  2078. spi_print_msg((unsigned char *) hostdata->msg_buf);
  2079. printk("\n");
  2080. }
  2081. return SPECIFIC_INT_NOTHING;
  2082. case A_int_msg_sdtr:
  2083. /*
  2084. * At this point, hostdata->msg_buf contains
  2085. * 0 EXTENDED MESSAGE
  2086. * 1 length
  2087. * 2 SDTR
  2088. * 3 period * 4ns
  2089. * 4 offset
  2090. */
  2091. if (cmd) {
  2092. char buf[80];
  2093. sprintf (buf, "scsi%d : target %d %s ", host->host_no, c->device->id,
  2094. (cmd->flags & CMD_FLAG_SDTR) ? "accepting" : "requesting");
  2095. print_synchronous (buf, (unsigned char *) hostdata->msg_buf);
  2096. /*
  2097. * Initiator initiated, won't happen unless synchronous
  2098. * transfers are enabled. If we get a SDTR message in
  2099. * response to our SDTR, we should program our parameters
  2100. * such that
  2101. * offset <= requested offset
  2102. * period >= requested period
  2103. */
  2104. if (cmd->flags & CMD_FLAG_SDTR) {
  2105. cmd->flags &= ~CMD_FLAG_SDTR;
  2106. if (hostdata->msg_buf[4])
  2107. synchronous (host, c->device->id, (unsigned char *)
  2108. hostdata->msg_buf);
  2109. else
  2110. asynchronous (host, c->device->id);
  2111. hostdata->dsp = hostdata->script + hostdata->E_accept_message /
  2112. sizeof(u32);
  2113. hostdata->dsp_changed = 1;
  2114. return SPECIFIC_INT_NOTHING;
  2115. } else {
  2116. if (hostdata->options & OPTION_SYNCHRONOUS) {
  2117. cmd->flags |= CMD_FLAG_DID_SDTR;
  2118. synchronous (host, c->device->id, (unsigned char *)
  2119. hostdata->msg_buf);
  2120. } else {
  2121. hostdata->msg_buf[4] = 0; /* 0 offset = async */
  2122. asynchronous (host, c->device->id);
  2123. }
  2124. patch_dsa_32 (cmd->dsa, dsa_msgout_other, 0, 5);
  2125. patch_dsa_32 (cmd->dsa, dsa_msgout_other, 1, (u32)
  2126. virt_to_bus ((void *)&hostdata->msg_buf));
  2127. hostdata->dsp = hostdata->script +
  2128. hostdata->E_respond_message / sizeof(u32);
  2129. hostdata->dsp_changed = 1;
  2130. }
  2131. return SPECIFIC_INT_NOTHING;
  2132. }
  2133. /* Fall through to abort if we couldn't find a cmd, and
  2134. therefore a dsa structure to twiddle */
  2135. case A_int_msg_wdtr:
  2136. hostdata->dsp = hostdata->script + hostdata->E_reject_message /
  2137. sizeof(u32);
  2138. hostdata->dsp_changed = 1;
  2139. return SPECIFIC_INT_NOTHING;
  2140. case A_int_err_unexpected_phase:
  2141. if (hostdata->options & OPTION_DEBUG_INTR)
  2142. printk ("scsi%d : unexpected phase\n", host->host_no);
  2143. return SPECIFIC_INT_ABORT;
  2144. case A_int_err_selected:
  2145. if ((hostdata->chip / 100) == 8)
  2146. printk ("scsi%d : selected by target %d\n", host->host_no,
  2147. (int) NCR53c7x0_read8(SDID_REG_800) &7);
  2148. else
  2149. printk ("scsi%d : selected by target LCRC=0x%02x\n", host->host_no,
  2150. (int) NCR53c7x0_read8(LCRC_REG_10));
  2151. hostdata->dsp = hostdata->script + hostdata->E_target_abort /
  2152. sizeof(u32);
  2153. hostdata->dsp_changed = 1;
  2154. return SPECIFIC_INT_NOTHING;
  2155. case A_int_err_unexpected_reselect:
  2156. if ((hostdata->chip / 100) == 8)
  2157. printk ("scsi%d : unexpected reselect by target %d lun %d\n",
  2158. host->host_no, (int) NCR53c7x0_read8(SDID_REG_800) & 7,
  2159. hostdata->reselected_identify & 7);
  2160. else
  2161. printk ("scsi%d : unexpected reselect LCRC=0x%02x\n", host->host_no,
  2162. (int) NCR53c7x0_read8(LCRC_REG_10));
  2163. hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
  2164. sizeof(u32);
  2165. hostdata->dsp_changed = 1;
  2166. return SPECIFIC_INT_NOTHING;
  2167. /*
  2168. * Since contingent allegiance conditions are cleared by the next
  2169. * command issued to a target, we must issue a REQUEST SENSE
  2170. * command after receiving a CHECK CONDITION status, before
  2171. * another command is issued.
  2172. *
  2173. * Since this NCR53c7x0_cmd will be freed after use, we don't
  2174. * care if we step on the various fields, so modify a few things.
  2175. */
  2176. case A_int_err_check_condition:
  2177. #if 0
  2178. if (hostdata->options & OPTION_DEBUG_INTR)
  2179. #endif
  2180. printk ("scsi%d : CHECK CONDITION\n", host->host_no);
  2181. if (!c) {
  2182. printk("scsi%d : CHECK CONDITION with no SCSI command\n",
  2183. host->host_no);
  2184. return SPECIFIC_INT_PANIC;
  2185. }
  2186. /*
  2187. * FIXME : this uses the normal one-byte selection message.
  2188. * We may want to renegotiate for synchronous & WIDE transfers
  2189. * since these could be the crux of our problem.
  2190. *
  2191. hostdata->NOP_insn* FIXME : once SCSI-II tagged queuing is implemented, we'll
  2192. * have to set this up so that the rest of the DSA
  2193. * agrees with this being an untagged queue'd command.
  2194. */
  2195. patch_dsa_32 (cmd->dsa, dsa_msgout, 0, 1);
  2196. /*
  2197. * Modify the table indirect for COMMAND OUT phase, since
  2198. * Request Sense is a six byte command.
  2199. */
  2200. patch_dsa_32 (cmd->dsa, dsa_cmdout, 0, 6);
  2201. /*
  2202. * The CDB is now mirrored in our local non-cached
  2203. * structure, but keep the old structure up to date as well,
  2204. * just in case anyone looks at it.
  2205. */
  2206. /*
  2207. * XXX Need to worry about data buffer alignment/cache state
  2208. * XXX here, but currently never get A_int_err_check_condition,
  2209. * XXX so ignore problem for now.
  2210. */
  2211. cmd->cmnd[0] = c->cmnd[0] = REQUEST_SENSE;
  2212. cmd->cmnd[0] = c->cmnd[1] &= 0xe0; /* Zero all but LUN */
  2213. cmd->cmnd[0] = c->cmnd[2] = 0;
  2214. cmd->cmnd[0] = c->cmnd[3] = 0;
  2215. cmd->cmnd[0] = c->cmnd[4] = sizeof(c->sense_buffer);
  2216. cmd->cmnd[0] = c->cmnd[5] = 0;
  2217. /*
  2218. * Disable dataout phase, and program datain to transfer to the
  2219. * sense buffer, and add a jump to other_transfer after the
  2220. * command so overflow/underrun conditions are detected.
  2221. */
  2222. patch_dsa_32 (cmd->dsa, dsa_dataout, 0,
  2223. virt_to_bus(hostdata->script) + hostdata->E_other_transfer);
  2224. patch_dsa_32 (cmd->dsa, dsa_datain, 0,
  2225. virt_to_bus(cmd->data_transfer_start));
  2226. cmd->data_transfer_start[0] = (((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I |
  2227. DCMD_BMI_IO)) << 24) | sizeof(c->sense_buffer);
  2228. cmd->data_transfer_start[1] = (u32) virt_to_bus(c->sense_buffer);
  2229. cmd->data_transfer_start[2] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP)
  2230. << 24) | DBC_TCI_TRUE;
  2231. cmd->data_transfer_start[3] = (u32) virt_to_bus(hostdata->script) +
  2232. hostdata->E_other_transfer;
  2233. /*
  2234. * Currently, this command is flagged as completed, ie
  2235. * it has valid status and message data. Reflag it as
  2236. * incomplete. Q - need to do something so that original
  2237. * status, etc are used.
  2238. */
  2239. cmd->result = cmd->cmd->result = 0xffff;
  2240. /*
  2241. * Restart command as a REQUEST SENSE.
  2242. */
  2243. hostdata->dsp = (u32 *) hostdata->script + hostdata->E_select /
  2244. sizeof(u32);
  2245. hostdata->dsp_changed = 1;
  2246. return SPECIFIC_INT_NOTHING;
  2247. case A_int_debug_break:
  2248. return SPECIFIC_INT_BREAK;
  2249. case A_int_norm_aborted:
  2250. hostdata->dsp = (u32 *) hostdata->schedule;
  2251. hostdata->dsp_changed = 1;
  2252. if (cmd)
  2253. abnormal_finished (cmd, DID_ERROR << 16);
  2254. return SPECIFIC_INT_NOTHING;
  2255. case A_int_norm_emulateintfly:
  2256. NCR53c7x0_intfly(host);
  2257. return SPECIFIC_INT_NOTHING;
  2258. case A_int_test_1:
  2259. case A_int_test_2:
  2260. hostdata->idle = 1;
  2261. hostdata->test_completed = (dsps - A_int_test_1) / 0x00010000 + 1;
  2262. if (hostdata->options & OPTION_DEBUG_INTR)
  2263. printk("scsi%d : test%d complete\n", host->host_no,
  2264. hostdata->test_completed);
  2265. return SPECIFIC_INT_NOTHING;
  2266. #ifdef A_int_debug_reselected_ok
  2267. case A_int_debug_reselected_ok:
  2268. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
  2269. OPTION_DEBUG_DISCONNECT)) {
  2270. /*
  2271. * Note - this dsa is not based on location relative to
  2272. * the command structure, but to location relative to the
  2273. * DSA register
  2274. */
  2275. u32 *dsa;
  2276. dsa = (u32 *) bus_to_virt (NCR53c7x0_read32(DSA_REG));
  2277. printk("scsi%d : reselected_ok (DSA = 0x%x (virt 0x%p)\n",
  2278. host->host_no, NCR53c7x0_read32(DSA_REG), dsa);
  2279. printk("scsi%d : resume address is 0x%x (virt 0x%p)\n",
  2280. host->host_no, cmd->saved_data_pointer,
  2281. bus_to_virt(cmd->saved_data_pointer));
  2282. print_insn (host, hostdata->script + Ent_reselected_ok /
  2283. sizeof(u32), "", 1);
  2284. if ((hostdata->chip / 100) == 8)
  2285. printk ("scsi%d : sxfer=0x%x, scntl3=0x%x\n",
  2286. host->host_no, NCR53c7x0_read8(SXFER_REG),
  2287. NCR53c7x0_read8(SCNTL3_REG_800));
  2288. else
  2289. printk ("scsi%d : sxfer=0x%x, cannot read SBCL\n",
  2290. host->host_no, NCR53c7x0_read8(SXFER_REG));
  2291. if (c) {
  2292. print_insn (host, (u32 *)
  2293. hostdata->sync[c->device->id].script, "", 1);
  2294. print_insn (host, (u32 *)
  2295. hostdata->sync[c->device->id].script + 2, "", 1);
  2296. }
  2297. }
  2298. return SPECIFIC_INT_RESTART;
  2299. #endif
  2300. #ifdef A_int_debug_reselect_check
  2301. case A_int_debug_reselect_check:
  2302. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
  2303. u32 *dsa;
  2304. #if 0
  2305. u32 *code;
  2306. #endif
  2307. /*
  2308. * Note - this dsa is not based on location relative to
  2309. * the command structure, but to location relative to the
  2310. * DSA register
  2311. */
  2312. dsa = bus_to_virt (NCR53c7x0_read32(DSA_REG));
  2313. printk("scsi%d : reselected_check_next (DSA = 0x%lx (virt 0x%p))\n",
  2314. host->host_no, virt_to_bus(dsa), dsa);
  2315. if (dsa) {
  2316. printk("scsi%d : resume address is 0x%x (virt 0x%p)\n",
  2317. host->host_no, cmd->saved_data_pointer,
  2318. bus_to_virt (cmd->saved_data_pointer));
  2319. #if 0
  2320. printk("scsi%d : template code :\n", host->host_no);
  2321. for (code = dsa + (Ent_dsa_code_check_reselect - Ent_dsa_zero)
  2322. / sizeof(u32); code < (dsa + Ent_dsa_zero / sizeof(u32));
  2323. code += print_insn (host, code, "", 1));
  2324. #endif
  2325. }
  2326. print_insn (host, hostdata->script + Ent_reselected_ok /
  2327. sizeof(u32), "", 1);
  2328. }
  2329. return SPECIFIC_INT_RESTART;
  2330. #endif
  2331. #ifdef A_int_debug_dsa_schedule
  2332. case A_int_debug_dsa_schedule:
  2333. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
  2334. u32 *dsa;
  2335. /*
  2336. * Note - this dsa is not based on location relative to
  2337. * the command structure, but to location relative to the
  2338. * DSA register
  2339. */
  2340. dsa = (u32 *) bus_to_virt (NCR53c7x0_read32(DSA_REG));
  2341. printk("scsi%d : dsa_schedule (old DSA = 0x%lx (virt 0x%p))\n",
  2342. host->host_no, virt_to_bus(dsa), dsa);
  2343. if (dsa)
  2344. printk("scsi%d : resume address is 0x%x (virt 0x%p)\n"
  2345. " (temp was 0x%x (virt 0x%p))\n",
  2346. host->host_no, cmd->saved_data_pointer,
  2347. bus_to_virt (cmd->saved_data_pointer),
  2348. NCR53c7x0_read32 (TEMP_REG),
  2349. bus_to_virt (NCR53c7x0_read32(TEMP_REG)));
  2350. }
  2351. return SPECIFIC_INT_RESTART;
  2352. #endif
  2353. #ifdef A_int_debug_scheduled
  2354. case A_int_debug_scheduled:
  2355. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
  2356. printk("scsi%d : new I/O 0x%x (virt 0x%p) scheduled\n",
  2357. host->host_no, NCR53c7x0_read32(DSA_REG),
  2358. bus_to_virt(NCR53c7x0_read32(DSA_REG)));
  2359. }
  2360. return SPECIFIC_INT_RESTART;
  2361. #endif
  2362. #ifdef A_int_debug_idle
  2363. case A_int_debug_idle:
  2364. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
  2365. printk("scsi%d : idle\n", host->host_no);
  2366. }
  2367. return SPECIFIC_INT_RESTART;
  2368. #endif
  2369. #ifdef A_int_debug_cmd
  2370. case A_int_debug_cmd:
  2371. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
  2372. printk("scsi%d : command sent\n");
  2373. }
  2374. return SPECIFIC_INT_RESTART;
  2375. #endif
  2376. #ifdef A_int_debug_dsa_loaded
  2377. case A_int_debug_dsa_loaded:
  2378. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
  2379. printk("scsi%d : DSA loaded with 0x%x (virt 0x%p)\n", host->host_no,
  2380. NCR53c7x0_read32(DSA_REG),
  2381. bus_to_virt(NCR53c7x0_read32(DSA_REG)));
  2382. }
  2383. return SPECIFIC_INT_RESTART;
  2384. #endif
  2385. #ifdef A_int_debug_reselected
  2386. case A_int_debug_reselected:
  2387. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
  2388. OPTION_DEBUG_DISCONNECT)) {
  2389. if ((hostdata->chip / 100) == 8)
  2390. printk("scsi%d : reselected by target %d lun %d\n",
  2391. host->host_no, (int) NCR53c7x0_read8(SDID_REG_800) & ~0x80,
  2392. (int) hostdata->reselected_identify & 7);
  2393. else
  2394. printk("scsi%d : reselected by LCRC=0x%02x lun %d\n",
  2395. host->host_no, (int) NCR53c7x0_read8(LCRC_REG_10),
  2396. (int) hostdata->reselected_identify & 7);
  2397. print_queues(host);
  2398. }
  2399. return SPECIFIC_INT_RESTART;
  2400. #endif
  2401. #ifdef A_int_debug_disconnect_msg
  2402. case A_int_debug_disconnect_msg:
  2403. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
  2404. if (c)
  2405. printk("scsi%d : target %d lun %d disconnecting\n",
  2406. host->host_no, c->device->id, c->device->lun);
  2407. else
  2408. printk("scsi%d : unknown target disconnecting\n",
  2409. host->host_no);
  2410. }
  2411. return SPECIFIC_INT_RESTART;
  2412. #endif
  2413. #ifdef A_int_debug_disconnected
  2414. case A_int_debug_disconnected:
  2415. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
  2416. OPTION_DEBUG_DISCONNECT)) {
  2417. printk ("scsi%d : disconnected, new queues are\n",
  2418. host->host_no);
  2419. print_queues(host);
  2420. #if 0
  2421. /* Not valid on ncr53c710! */
  2422. printk ("scsi%d : sxfer=0x%x, scntl3=0x%x\n",
  2423. host->host_no, NCR53c7x0_read8(SXFER_REG),
  2424. NCR53c7x0_read8(SCNTL3_REG_800));
  2425. #endif
  2426. if (c) {
  2427. print_insn (host, (u32 *)
  2428. hostdata->sync[c->device->id].script, "", 1);
  2429. print_insn (host, (u32 *)
  2430. hostdata->sync[c->device->id].script + 2, "", 1);
  2431. }
  2432. }
  2433. return SPECIFIC_INT_RESTART;
  2434. #endif
  2435. #ifdef A_int_debug_panic
  2436. case A_int_debug_panic:
  2437. printk("scsi%d : int_debug_panic received\n", host->host_no);
  2438. print_lots (host);
  2439. return SPECIFIC_INT_PANIC;
  2440. #endif
  2441. #ifdef A_int_debug_saved
  2442. case A_int_debug_saved:
  2443. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
  2444. OPTION_DEBUG_DISCONNECT)) {
  2445. printk ("scsi%d : saved data pointer 0x%x (virt 0x%p)\n",
  2446. host->host_no, cmd->saved_data_pointer,
  2447. bus_to_virt (cmd->saved_data_pointer));
  2448. print_progress (c);
  2449. }
  2450. return SPECIFIC_INT_RESTART;
  2451. #endif
  2452. #ifdef A_int_debug_restored
  2453. case A_int_debug_restored:
  2454. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
  2455. OPTION_DEBUG_DISCONNECT)) {
  2456. if (cmd) {
  2457. int size;
  2458. printk ("scsi%d : restored data pointer 0x%x (virt 0x%p)\n",
  2459. host->host_no, cmd->saved_data_pointer, bus_to_virt (
  2460. cmd->saved_data_pointer));
  2461. size = print_insn (host, (u32 *)
  2462. bus_to_virt(cmd->saved_data_pointer), "", 1);
  2463. size = print_insn (host, (u32 *)
  2464. bus_to_virt(cmd->saved_data_pointer) + size, "", 1);
  2465. print_progress (c);
  2466. }
  2467. #if 0
  2468. printk ("scsi%d : datapath residual %d\n",
  2469. host->host_no, datapath_residual (host)) ;
  2470. #endif
  2471. }
  2472. return SPECIFIC_INT_RESTART;
  2473. #endif
  2474. #ifdef A_int_debug_sync
  2475. case A_int_debug_sync:
  2476. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
  2477. OPTION_DEBUG_DISCONNECT|OPTION_DEBUG_SDTR)) {
  2478. unsigned char sxfer = NCR53c7x0_read8 (SXFER_REG), scntl3;
  2479. if ((hostdata->chip / 100) == 8) {
  2480. scntl3 = NCR53c7x0_read8 (SCNTL3_REG_800);
  2481. if (c) {
  2482. if (sxfer != hostdata->sync[c->device->id].sxfer_sanity ||
  2483. scntl3 != hostdata->sync[c->device->id].scntl3_sanity) {
  2484. printk ("scsi%d : sync sanity check failed sxfer=0x%x, scntl3=0x%x",
  2485. host->host_no, sxfer, scntl3);
  2486. NCR53c7x0_write8 (SXFER_REG, sxfer);
  2487. NCR53c7x0_write8 (SCNTL3_REG_800, scntl3);
  2488. }
  2489. } else
  2490. printk ("scsi%d : unknown command sxfer=0x%x, scntl3=0x%x\n",
  2491. host->host_no, (int) sxfer, (int) scntl3);
  2492. } else {
  2493. if (c) {
  2494. if (sxfer != hostdata->sync[c->device->id].sxfer_sanity) {
  2495. printk ("scsi%d : sync sanity check failed sxfer=0x%x",
  2496. host->host_no, sxfer);
  2497. NCR53c7x0_write8 (SXFER_REG, sxfer);
  2498. NCR53c7x0_write8 (SBCL_REG,
  2499. hostdata->sync[c->device->id].sscf_710);
  2500. }
  2501. } else
  2502. printk ("scsi%d : unknown command sxfer=0x%x\n",
  2503. host->host_no, (int) sxfer);
  2504. }
  2505. }
  2506. return SPECIFIC_INT_RESTART;
  2507. #endif
  2508. #ifdef A_int_debug_datain
  2509. case A_int_debug_datain:
  2510. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
  2511. OPTION_DEBUG_DISCONNECT|OPTION_DEBUG_SDTR)) {
  2512. int size;
  2513. if ((hostdata->chip / 100) == 8)
  2514. printk ("scsi%d : In do_datain (%s) sxfer=0x%x, scntl3=0x%x\n"
  2515. " datapath residual=%d\n",
  2516. host->host_no, sbcl_to_phase (NCR53c7x0_read8 (SBCL_REG)),
  2517. (int) NCR53c7x0_read8(SXFER_REG),
  2518. (int) NCR53c7x0_read8(SCNTL3_REG_800),
  2519. datapath_residual (host)) ;
  2520. else
  2521. printk ("scsi%d : In do_datain (%s) sxfer=0x%x\n"
  2522. " datapath residual=%d\n",
  2523. host->host_no, sbcl_to_phase (NCR53c7x0_read8 (SBCL_REG)),
  2524. (int) NCR53c7x0_read8(SXFER_REG),
  2525. datapath_residual (host)) ;
  2526. print_insn (host, dsp, "", 1);
  2527. size = print_insn (host, (u32 *) bus_to_virt(dsp[1]), "", 1);
  2528. print_insn (host, (u32 *) bus_to_virt(dsp[1]) + size, "", 1);
  2529. }
  2530. return SPECIFIC_INT_RESTART;
  2531. #endif
  2532. #ifdef A_int_debug_check_dsa
  2533. case A_int_debug_check_dsa:
  2534. if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON) {
  2535. int sdid;
  2536. int tmp;
  2537. char *where;
  2538. if (hostdata->chip / 100 == 8)
  2539. sdid = NCR53c7x0_read8 (SDID_REG_800) & 15;
  2540. else {
  2541. tmp = NCR53c7x0_read8 (SDID_REG_700);
  2542. if (!tmp)
  2543. panic ("SDID_REG_700 = 0");
  2544. tmp >>= 1;
  2545. sdid = 0;
  2546. while (tmp) {
  2547. tmp >>= 1;
  2548. sdid++;
  2549. }
  2550. }
  2551. where = dsp - NCR53c7x0_insn_size(NCR53c7x0_read8
  2552. (DCMD_REG)) == hostdata->script +
  2553. Ent_select_check_dsa / sizeof(u32) ?
  2554. "selection" : "reselection";
  2555. if (c && sdid != c->device->id) {
  2556. printk ("scsi%d : SDID target %d != DSA target %d at %s\n",
  2557. host->host_no, sdid, c->device->id, where);
  2558. print_lots(host);
  2559. dump_events (host, 20);
  2560. return SPECIFIC_INT_PANIC;
  2561. }
  2562. }
  2563. return SPECIFIC_INT_RESTART;
  2564. #endif
  2565. default:
  2566. if ((dsps & 0xff000000) == 0x03000000) {
  2567. printk ("scsi%d : misc debug interrupt 0x%x\n",
  2568. host->host_no, dsps);
  2569. return SPECIFIC_INT_RESTART;
  2570. } else if ((dsps & 0xff000000) == 0x05000000) {
  2571. if (hostdata->events) {
  2572. struct NCR53c7x0_event *event;
  2573. ++hostdata->event_index;
  2574. if (hostdata->event_index >= hostdata->event_size)
  2575. hostdata->event_index = 0;
  2576. event = (struct NCR53c7x0_event *) hostdata->events +
  2577. hostdata->event_index;
  2578. event->event = (enum ncr_event) dsps;
  2579. event->dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
  2580. if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON) {
  2581. if (hostdata->chip / 100 == 8)
  2582. event->target = NCR53c7x0_read8(SSID_REG_800);
  2583. else {
  2584. unsigned char tmp, sdid;
  2585. tmp = NCR53c7x0_read8 (SDID_REG_700);
  2586. if (!tmp)
  2587. panic ("SDID_REG_700 = 0");
  2588. tmp >>= 1;
  2589. sdid = 0;
  2590. while (tmp) {
  2591. tmp >>= 1;
  2592. sdid++;
  2593. }
  2594. event->target = sdid;
  2595. }
  2596. }
  2597. else
  2598. event->target = 255;
  2599. if (event->event == EVENT_RESELECT)
  2600. event->lun = hostdata->reselected_identify & 0xf;
  2601. else if (c)
  2602. event->lun = c->device->lun;
  2603. else
  2604. event->lun = 255;
  2605. do_gettimeofday(&(event->time));
  2606. if (c) {
  2607. event->pid = c->pid;
  2608. memcpy ((void *) event->cmnd, (void *) c->cmnd,
  2609. sizeof (event->cmnd));
  2610. } else {
  2611. event->pid = -1;
  2612. }
  2613. }
  2614. return SPECIFIC_INT_RESTART;
  2615. }
  2616. printk ("scsi%d : unknown user interrupt 0x%x\n",
  2617. host->host_no, (unsigned) dsps);
  2618. return SPECIFIC_INT_PANIC;
  2619. }
  2620. }
  2621. /*
  2622. * XXX - the stock NCR assembler won't output the scriptu.h file,
  2623. * which undefine's all #define'd CPP symbols from the script.h
  2624. * file, which will create problems if you use multiple scripts
  2625. * with the same symbol names.
  2626. *
  2627. * If you insist on using NCR's assembler, you could generate
  2628. * scriptu.h from script.h using something like
  2629. *
  2630. * grep #define script.h | \
  2631. * sed 's/#define[ ][ ]*\([_a-zA-Z][_a-zA-Z0-9]*\).*$/#undefine \1/' \
  2632. * > scriptu.h
  2633. */
  2634. #include "53c7xx_u.h"
  2635. /* XXX - add alternate script handling code here */
  2636. /*
  2637. * Function : static void NCR537xx_soft_reset (struct Scsi_Host *host)
  2638. *
  2639. * Purpose : perform a soft reset of the NCR53c7xx chip
  2640. *
  2641. * Inputs : host - pointer to this host adapter's structure
  2642. *
  2643. * Preconditions : NCR53c7x0_init must have been called for this
  2644. * host.
  2645. *
  2646. */
  2647. static void
  2648. NCR53c7x0_soft_reset (struct Scsi_Host *host) {
  2649. NCR53c7x0_local_declare();
  2650. unsigned long flags;
  2651. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  2652. host->hostdata[0];
  2653. NCR53c7x0_local_setup(host);
  2654. local_irq_save(flags);
  2655. /* Disable scsi chip and s/w level 7 ints */
  2656. #ifdef CONFIG_MVME16x
  2657. if (MACH_IS_MVME16x)
  2658. {
  2659. volatile unsigned long v;
  2660. v = *(volatile unsigned long *)0xfff4006c;
  2661. v &= ~0x8000;
  2662. *(volatile unsigned long *)0xfff4006c = v;
  2663. v = *(volatile unsigned long *)0xfff4202c;
  2664. v &= ~0x10;
  2665. *(volatile unsigned long *)0xfff4202c = v;
  2666. }
  2667. #endif
  2668. /* Anything specific for your hardware? */
  2669. /*
  2670. * Do a soft reset of the chip so that everything is
  2671. * reinitialized to the power-on state.
  2672. *
  2673. * Basically follow the procedure outlined in the NCR53c700
  2674. * data manual under Chapter Six, How to Use, Steps Necessary to
  2675. * Start SCRIPTS, with the exception of actually starting the
  2676. * script and setting up the synchronous transfer gunk.
  2677. */
  2678. /* Should we reset the scsi bus here??????????????????? */
  2679. NCR53c7x0_write8(ISTAT_REG_700, ISTAT_10_SRST);
  2680. NCR53c7x0_write8(ISTAT_REG_700, 0);
  2681. /*
  2682. * saved_dcntl is set up in NCR53c7x0_init() before it is overwritten
  2683. * here. We should have some better way of working out the CF bit
  2684. * setting..
  2685. */
  2686. hostdata->saved_dcntl = DCNTL_10_EA|DCNTL_10_COM;
  2687. if (hostdata->scsi_clock > 50000000)
  2688. hostdata->saved_dcntl |= DCNTL_700_CF_3;
  2689. else
  2690. if (hostdata->scsi_clock > 37500000)
  2691. hostdata->saved_dcntl |= DCNTL_700_CF_2;
  2692. #if 0
  2693. else
  2694. /* Any clocks less than 37.5MHz? */
  2695. #endif
  2696. if (hostdata->options & OPTION_DEBUG_TRACE)
  2697. NCR53c7x0_write8(DCNTL_REG, hostdata->saved_dcntl | DCNTL_SSM);
  2698. else
  2699. NCR53c7x0_write8(DCNTL_REG, hostdata->saved_dcntl);
  2700. /* Following disables snooping - snooping is not required, as non-
  2701. * cached pages are used for shared data, and appropriate use is
  2702. * made of cache_push/cache_clear. Indeed, for 68060
  2703. * enabling snooping causes disk corruption of ext2fs free block
  2704. * bitmaps and the like. If you have a 68060 with snooping hardwared
  2705. * on, then you need to enable CONFIG_060_WRITETHROUGH.
  2706. */
  2707. NCR53c7x0_write8(CTEST7_REG, CTEST7_10_TT1|CTEST7_STD);
  2708. /* Actually burst of eight, according to my 53c710 databook */
  2709. NCR53c7x0_write8(hostdata->dmode, DMODE_10_BL_8 | DMODE_10_FC2);
  2710. NCR53c7x0_write8(SCID_REG, 1 << host->this_id);
  2711. NCR53c7x0_write8(SBCL_REG, 0);
  2712. NCR53c7x0_write8(SCNTL1_REG, SCNTL1_ESR_700);
  2713. NCR53c7x0_write8(SCNTL0_REG, ((hostdata->options & OPTION_PARITY) ?
  2714. SCNTL0_EPC : 0) | SCNTL0_EPG_700 | SCNTL0_ARB1 | SCNTL0_ARB2);
  2715. /*
  2716. * Enable all interrupts, except parity which we only want when
  2717. * the user requests it.
  2718. */
  2719. NCR53c7x0_write8(DIEN_REG, DIEN_700_BF |
  2720. DIEN_ABRT | DIEN_SSI | DIEN_SIR | DIEN_700_OPC);
  2721. NCR53c7x0_write8(SIEN_REG_700, ((hostdata->options & OPTION_PARITY) ?
  2722. SIEN_PAR : 0) | SIEN_700_STO | SIEN_RST | SIEN_UDC |
  2723. SIEN_SGE | SIEN_MA);
  2724. #ifdef CONFIG_MVME16x
  2725. if (MACH_IS_MVME16x)
  2726. {
  2727. volatile unsigned long v;
  2728. /* Enable scsi chip and s/w level 7 ints */
  2729. v = *(volatile unsigned long *)0xfff40080;
  2730. v = (v & ~(0xf << 28)) | (4 << 28);
  2731. *(volatile unsigned long *)0xfff40080 = v;
  2732. v = *(volatile unsigned long *)0xfff4006c;
  2733. v |= 0x8000;
  2734. *(volatile unsigned long *)0xfff4006c = v;
  2735. v = *(volatile unsigned long *)0xfff4202c;
  2736. v = (v & ~0xff) | 0x10 | 4;
  2737. *(volatile unsigned long *)0xfff4202c = v;
  2738. }
  2739. #endif
  2740. /* Anything needed for your hardware? */
  2741. local_irq_restore(flags);
  2742. }
  2743. /*
  2744. * Function static struct NCR53c7x0_cmd *allocate_cmd (Scsi_Cmnd *cmd)
  2745. *
  2746. * Purpose : Return the first free NCR53c7x0_cmd structure (which are
  2747. * reused in a LIFO manner to minimize cache thrashing).
  2748. *
  2749. * Side effects : If we haven't yet scheduled allocation of NCR53c7x0_cmd
  2750. * structures for this device, do so. Attempt to complete all scheduled
  2751. * allocations using get_zeroed_page(), putting NCR53c7x0_cmd structures on
  2752. * the free list. Teach programmers not to drink and hack.
  2753. *
  2754. * Inputs : cmd - SCSI command
  2755. *
  2756. * Returns : NCR53c7x0_cmd structure allocated on behalf of cmd;
  2757. * NULL on failure.
  2758. */
  2759. static void
  2760. my_free_page (void *addr, int dummy)
  2761. {
  2762. /* XXX This assumes default cache mode to be IOMAP_FULL_CACHING, which
  2763. * XXX may be invalid (CONFIG_060_WRITETHROUGH)
  2764. */
  2765. kernel_set_cachemode((void *)addr, 4096, IOMAP_FULL_CACHING);
  2766. free_page ((u32)addr);
  2767. }
  2768. static struct NCR53c7x0_cmd *
  2769. allocate_cmd (Scsi_Cmnd *cmd) {
  2770. struct Scsi_Host *host = cmd->device->host;
  2771. struct NCR53c7x0_hostdata *hostdata =
  2772. (struct NCR53c7x0_hostdata *) host->hostdata[0];
  2773. u32 real; /* Real address */
  2774. int size; /* Size of *tmp */
  2775. struct NCR53c7x0_cmd *tmp;
  2776. unsigned long flags;
  2777. if (hostdata->options & OPTION_DEBUG_ALLOCATION)
  2778. printk ("scsi%d : num_cmds = %d, can_queue = %d\n"
  2779. " target = %d, lun = %d, %s\n",
  2780. host->host_no, hostdata->num_cmds, host->can_queue,
  2781. cmd->device->id, cmd->device->lun, (hostdata->cmd_allocated[cmd->device->id] &
  2782. (1 << cmd->device->lun)) ? "already allocated" : "not allocated");
  2783. /*
  2784. * If we have not yet reserved commands for this I_T_L nexus, and
  2785. * the device exists (as indicated by permanent Scsi_Cmnd structures
  2786. * being allocated under 1.3.x, or being outside of scan_scsis in
  2787. * 1.2.x), do so now.
  2788. */
  2789. if (!(hostdata->cmd_allocated[cmd->device->id] & (1 << cmd->device->lun)) &&
  2790. cmd->device && cmd->device->has_cmdblocks) {
  2791. if ((hostdata->extra_allocate + hostdata->num_cmds) < host->can_queue)
  2792. hostdata->extra_allocate += host->cmd_per_lun;
  2793. hostdata->cmd_allocated[cmd->device->id] |= (1 << cmd->device->lun);
  2794. }
  2795. for (; hostdata->extra_allocate > 0 ; --hostdata->extra_allocate,
  2796. ++hostdata->num_cmds) {
  2797. /* historically, kmalloc has returned unaligned addresses; pad so we
  2798. have enough room to ROUNDUP */
  2799. size = hostdata->max_cmd_size + sizeof (void *);
  2800. #ifdef FORCE_DSA_ALIGNMENT
  2801. /*
  2802. * 53c710 rev.0 doesn't have an add-with-carry instruction.
  2803. * Ensure we allocate enough memory to force alignment.
  2804. */
  2805. size += 256;
  2806. #endif
  2807. /* FIXME: for ISA bus '7xx chips, we need to or GFP_DMA in here */
  2808. if (size > 4096) {
  2809. printk (KERN_ERR "53c7xx: allocate_cmd size > 4K\n");
  2810. return NULL;
  2811. }
  2812. real = get_zeroed_page(GFP_ATOMIC);
  2813. if (real == 0)
  2814. return NULL;
  2815. memset((void *)real, 0, 4096);
  2816. cache_push(virt_to_phys((void *)real), 4096);
  2817. cache_clear(virt_to_phys((void *)real), 4096);
  2818. kernel_set_cachemode((void *)real, 4096, IOMAP_NOCACHE_SER);
  2819. tmp = ROUNDUP(real, void *);
  2820. #ifdef FORCE_DSA_ALIGNMENT
  2821. {
  2822. if (((u32)tmp & 0xff) > CmdPageStart)
  2823. tmp = (struct NCR53c7x0_cmd *)((u32)tmp + 255);
  2824. tmp = (struct NCR53c7x0_cmd *)(((u32)tmp & ~0xff) + CmdPageStart);
  2825. #if 0
  2826. printk ("scsi: size = %d, real = 0x%08x, tmp set to 0x%08x\n",
  2827. size, real, (u32)tmp);
  2828. #endif
  2829. }
  2830. #endif
  2831. tmp->real = (void *)real;
  2832. tmp->size = size;
  2833. tmp->free = ((void (*)(void *, int)) my_free_page);
  2834. local_irq_save(flags);
  2835. tmp->next = hostdata->free;
  2836. hostdata->free = tmp;
  2837. local_irq_restore(flags);
  2838. }
  2839. local_irq_save(flags);
  2840. tmp = (struct NCR53c7x0_cmd *) hostdata->free;
  2841. if (tmp) {
  2842. hostdata->free = tmp->next;
  2843. }
  2844. local_irq_restore(flags);
  2845. if (!tmp)
  2846. printk ("scsi%d : can't allocate command for target %d lun %d\n",
  2847. host->host_no, cmd->device->id, cmd->device->lun);
  2848. return tmp;
  2849. }
  2850. /*
  2851. * Function static struct NCR53c7x0_cmd *create_cmd (Scsi_Cmnd *cmd)
  2852. *
  2853. *
  2854. * Purpose : allocate a NCR53c7x0_cmd structure, initialize it based on the
  2855. * Scsi_Cmnd structure passed in cmd, including dsa and Linux field
  2856. * initialization, and dsa code relocation.
  2857. *
  2858. * Inputs : cmd - SCSI command
  2859. *
  2860. * Returns : NCR53c7x0_cmd structure corresponding to cmd,
  2861. * NULL on failure.
  2862. */
  2863. static struct NCR53c7x0_cmd *
  2864. create_cmd (Scsi_Cmnd *cmd) {
  2865. NCR53c7x0_local_declare();
  2866. struct Scsi_Host *host = cmd->device->host;
  2867. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  2868. host->hostdata[0];
  2869. struct NCR53c7x0_cmd *tmp; /* NCR53c7x0_cmd structure for this command */
  2870. int datain, /* Number of instructions per phase */
  2871. dataout;
  2872. int data_transfer_instructions, /* Count of dynamic instructions */
  2873. i; /* Counter */
  2874. u32 *cmd_datain, /* Address of datain/dataout code */
  2875. *cmd_dataout; /* Incremented as we assemble */
  2876. #ifdef notyet
  2877. unsigned char *msgptr; /* Current byte in select message */
  2878. int msglen; /* Length of whole select message */
  2879. #endif
  2880. unsigned long flags;
  2881. u32 exp_select_indirect; /* Used in sanity check */
  2882. NCR53c7x0_local_setup(cmd->device->host);
  2883. if (!(tmp = allocate_cmd (cmd)))
  2884. return NULL;
  2885. /*
  2886. * Copy CDB and initialised result fields from Scsi_Cmnd to NCR53c7x0_cmd.
  2887. * We do this because NCR53c7x0_cmd may have a special cache mode
  2888. * selected to cope with lack of bus snooping, etc.
  2889. */
  2890. memcpy(tmp->cmnd, cmd->cmnd, 12);
  2891. tmp->result = cmd->result;
  2892. /*
  2893. * Decide whether we need to generate commands for DATA IN,
  2894. * DATA OUT, neither, or both based on the SCSI command
  2895. */
  2896. switch (cmd->cmnd[0]) {
  2897. /* These commands do DATA IN */
  2898. case INQUIRY:
  2899. case MODE_SENSE:
  2900. case READ_6:
  2901. case READ_10:
  2902. case READ_CAPACITY:
  2903. case REQUEST_SENSE:
  2904. case READ_BLOCK_LIMITS:
  2905. case READ_TOC:
  2906. datain = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
  2907. dataout = 0;
  2908. break;
  2909. /* These commands do DATA OUT */
  2910. case MODE_SELECT:
  2911. case WRITE_6:
  2912. case WRITE_10:
  2913. #if 0
  2914. printk("scsi%d : command is ", host->host_no);
  2915. __scsi_print_command(cmd->cmnd);
  2916. #endif
  2917. #if 0
  2918. printk ("scsi%d : %d scatter/gather segments\n", host->host_no,
  2919. cmd->use_sg);
  2920. #endif
  2921. datain = 0;
  2922. dataout = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
  2923. #if 0
  2924. hostdata->options |= OPTION_DEBUG_INTR;
  2925. #endif
  2926. break;
  2927. /*
  2928. * These commands do no data transfer, we should force an
  2929. * interrupt if a data phase is attempted on them.
  2930. */
  2931. case TEST_UNIT_READY:
  2932. case ALLOW_MEDIUM_REMOVAL:
  2933. case START_STOP:
  2934. datain = dataout = 0;
  2935. break;
  2936. /*
  2937. * We don't know about these commands, so generate code to handle
  2938. * both DATA IN and DATA OUT phases. More efficient to identify them
  2939. * and add them to the above cases.
  2940. */
  2941. default:
  2942. printk("scsi%d : datain+dataout for command ", host->host_no);
  2943. __scsi_print_command(cmd->cmnd);
  2944. datain = dataout = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
  2945. }
  2946. /*
  2947. * New code : so that active pointers work correctly regardless
  2948. * of where the saved data pointer is at, we want to immediately
  2949. * enter the dynamic code after selection, and on a non-data
  2950. * phase perform a CALL to the non-data phase handler, with
  2951. * returns back to this address.
  2952. *
  2953. * If a phase mismatch is encountered in the middle of a
  2954. * Block MOVE instruction, we want to _leave_ that instruction
  2955. * unchanged as the current case is, modify a temporary buffer,
  2956. * and point the active pointer (TEMP) at that.
  2957. *
  2958. * Furthermore, we want to implement a saved data pointer,
  2959. * set by the SAVE_DATA_POINTERs message.
  2960. *
  2961. * So, the data transfer segments will change to
  2962. * CALL data_transfer, WHEN NOT data phase
  2963. * MOVE x, x, WHEN data phase
  2964. * ( repeat )
  2965. * JUMP other_transfer
  2966. */
  2967. data_transfer_instructions = datain + dataout;
  2968. /*
  2969. * When we perform a request sense, we overwrite various things,
  2970. * including the data transfer code. Make sure we have enough
  2971. * space to do that.
  2972. */
  2973. if (data_transfer_instructions < 2)
  2974. data_transfer_instructions = 2;
  2975. /*
  2976. * The saved data pointer is set up so that a RESTORE POINTERS message
  2977. * will start the data transfer over at the beginning.
  2978. */
  2979. tmp->saved_data_pointer = virt_to_bus (hostdata->script) +
  2980. hostdata->E_data_transfer;
  2981. /*
  2982. * Initialize Linux specific fields.
  2983. */
  2984. tmp->cmd = cmd;
  2985. tmp->next = NULL;
  2986. tmp->flags = 0;
  2987. tmp->dsa_next_addr = virt_to_bus(tmp->dsa) + hostdata->dsa_next -
  2988. hostdata->dsa_start;
  2989. tmp->dsa_addr = virt_to_bus(tmp->dsa) - hostdata->dsa_start;
  2990. /*
  2991. * Calculate addresses of dynamic code to fill in DSA
  2992. */
  2993. tmp->data_transfer_start = tmp->dsa + (hostdata->dsa_end -
  2994. hostdata->dsa_start) / sizeof(u32);
  2995. tmp->data_transfer_end = tmp->data_transfer_start +
  2996. 2 * data_transfer_instructions;
  2997. cmd_datain = datain ? tmp->data_transfer_start : NULL;
  2998. cmd_dataout = dataout ? (datain ? cmd_datain + 2 * datain : tmp->
  2999. data_transfer_start) : NULL;
  3000. /*
  3001. * Fill in the NCR53c7x0_cmd structure as follows
  3002. * dsa, with fixed up DSA code
  3003. * datain code
  3004. * dataout code
  3005. */
  3006. /* Copy template code into dsa and perform all necessary fixups */
  3007. if (hostdata->dsa_fixup)
  3008. hostdata->dsa_fixup(tmp);
  3009. patch_dsa_32(tmp->dsa, dsa_next, 0, 0);
  3010. /*
  3011. * XXX is this giving 53c710 access to the Scsi_Cmnd in some way?
  3012. * Do we need to change it for caching reasons?
  3013. */
  3014. patch_dsa_32(tmp->dsa, dsa_cmnd, 0, virt_to_bus(cmd));
  3015. if (hostdata->options & OPTION_DEBUG_SYNCHRONOUS) {
  3016. exp_select_indirect = ((1 << cmd->device->id) << 16) |
  3017. (hostdata->sync[cmd->device->id].sxfer_sanity << 8);
  3018. if (hostdata->sync[cmd->device->id].select_indirect !=
  3019. exp_select_indirect) {
  3020. printk ("scsi%d : sanity check failed select_indirect=0x%x\n",
  3021. host->host_no, hostdata->sync[cmd->device->id].select_indirect);
  3022. FATAL(host);
  3023. }
  3024. }
  3025. patch_dsa_32(tmp->dsa, dsa_select, 0,
  3026. hostdata->sync[cmd->device->id].select_indirect);
  3027. /*
  3028. * Right now, we'll do the WIDE and SYNCHRONOUS negotiations on
  3029. * different commands; although it should be trivial to do them
  3030. * both at the same time.
  3031. */
  3032. if (hostdata->initiate_wdtr & (1 << cmd->device->id)) {
  3033. memcpy ((void *) (tmp->select + 1), (void *) wdtr_message,
  3034. sizeof(wdtr_message));
  3035. patch_dsa_32(tmp->dsa, dsa_msgout, 0, 1 + sizeof(wdtr_message));
  3036. local_irq_save(flags);
  3037. hostdata->initiate_wdtr &= ~(1 << cmd->device->id);
  3038. local_irq_restore(flags);
  3039. } else if (hostdata->initiate_sdtr & (1 << cmd->device->id)) {
  3040. memcpy ((void *) (tmp->select + 1), (void *) sdtr_message,
  3041. sizeof(sdtr_message));
  3042. patch_dsa_32(tmp->dsa, dsa_msgout, 0, 1 + sizeof(sdtr_message));
  3043. tmp->flags |= CMD_FLAG_SDTR;
  3044. local_irq_save(flags);
  3045. hostdata->initiate_sdtr &= ~(1 << cmd->device->id);
  3046. local_irq_restore(flags);
  3047. }
  3048. #if 1
  3049. else if (!(hostdata->talked_to & (1 << cmd->device->id)) &&
  3050. !(hostdata->options & OPTION_NO_ASYNC)) {
  3051. memcpy ((void *) (tmp->select + 1), (void *) async_message,
  3052. sizeof(async_message));
  3053. patch_dsa_32(tmp->dsa, dsa_msgout, 0, 1 + sizeof(async_message));
  3054. tmp->flags |= CMD_FLAG_SDTR;
  3055. }
  3056. #endif
  3057. else
  3058. patch_dsa_32(tmp->dsa, dsa_msgout, 0, 1);
  3059. hostdata->talked_to |= (1 << cmd->device->id);
  3060. tmp->select[0] = (hostdata->options & OPTION_DISCONNECT) ?
  3061. IDENTIFY (1, cmd->device->lun) : IDENTIFY (0, cmd->device->lun);
  3062. patch_dsa_32(tmp->dsa, dsa_msgout, 1, virt_to_bus(tmp->select));
  3063. patch_dsa_32(tmp->dsa, dsa_cmdout, 0, cmd->cmd_len);
  3064. patch_dsa_32(tmp->dsa, dsa_cmdout, 1, virt_to_bus(tmp->cmnd));
  3065. patch_dsa_32(tmp->dsa, dsa_dataout, 0, cmd_dataout ?
  3066. virt_to_bus (cmd_dataout)
  3067. : virt_to_bus (hostdata->script) + hostdata->E_other_transfer);
  3068. patch_dsa_32(tmp->dsa, dsa_datain, 0, cmd_datain ?
  3069. virt_to_bus (cmd_datain)
  3070. : virt_to_bus (hostdata->script) + hostdata->E_other_transfer);
  3071. /*
  3072. * XXX - need to make endian aware, should use separate variables
  3073. * for both status and message bytes.
  3074. */
  3075. patch_dsa_32(tmp->dsa, dsa_msgin, 0, 1);
  3076. /*
  3077. * FIXME : these only works for little endian. We probably want to
  3078. * provide message and status fields in the NCR53c7x0_cmd
  3079. * structure, and assign them to cmd->result when we're done.
  3080. */
  3081. #ifdef BIG_ENDIAN
  3082. patch_dsa_32(tmp->dsa, dsa_msgin, 1, virt_to_bus(&tmp->result) + 2);
  3083. patch_dsa_32(tmp->dsa, dsa_status, 0, 1);
  3084. patch_dsa_32(tmp->dsa, dsa_status, 1, virt_to_bus(&tmp->result) + 3);
  3085. #else
  3086. patch_dsa_32(tmp->dsa, dsa_msgin, 1, virt_to_bus(&tmp->result) + 1);
  3087. patch_dsa_32(tmp->dsa, dsa_status, 0, 1);
  3088. patch_dsa_32(tmp->dsa, dsa_status, 1, virt_to_bus(&tmp->result));
  3089. #endif
  3090. patch_dsa_32(tmp->dsa, dsa_msgout_other, 0, 1);
  3091. patch_dsa_32(tmp->dsa, dsa_msgout_other, 1,
  3092. virt_to_bus(&(hostdata->NCR53c7xx_msg_nop)));
  3093. /*
  3094. * Generate code for zero or more of the DATA IN, DATA OUT phases
  3095. * in the format
  3096. *
  3097. * CALL data_transfer, WHEN NOT phase
  3098. * MOVE first buffer length, first buffer address, WHEN phase
  3099. * ...
  3100. * MOVE last buffer length, last buffer address, WHEN phase
  3101. * JUMP other_transfer
  3102. */
  3103. /*
  3104. * See if we're getting to data transfer by generating an unconditional
  3105. * interrupt.
  3106. */
  3107. #if 0
  3108. if (datain) {
  3109. cmd_datain[0] = 0x98080000;
  3110. cmd_datain[1] = 0x03ffd00d;
  3111. cmd_datain += 2;
  3112. }
  3113. #endif
  3114. /*
  3115. * XXX - I'm undecided whether all of this nonsense is faster
  3116. * in the long run, or whether I should just go and implement a loop
  3117. * on the NCR chip using table indirect mode?
  3118. *
  3119. * In any case, this is how it _must_ be done for 53c700/700-66 chips,
  3120. * so this stays even when we come up with something better.
  3121. *
  3122. * When we're limited to 1 simultaneous command, no overlapping processing,
  3123. * we're seeing 630K/sec, with 7% CPU usage on a slow Syquest 45M
  3124. * drive.
  3125. *
  3126. * Not bad, not good. We'll see.
  3127. */
  3128. tmp->bounce.len = 0; /* Assume aligned buffer */
  3129. for (i = 0; cmd->use_sg ? (i < cmd->use_sg) : !i; cmd_datain += 4,
  3130. cmd_dataout += 4, ++i) {
  3131. u32 vbuf = cmd->use_sg
  3132. ? (u32)page_address(((struct scatterlist *)cmd->buffer)[i].page)+
  3133. ((struct scatterlist *)cmd->buffer)[i].offset
  3134. : (u32)(cmd->request_buffer);
  3135. u32 bbuf = virt_to_bus((void *)vbuf);
  3136. u32 count = cmd->use_sg ?
  3137. ((struct scatterlist *)cmd->buffer)[i].length :
  3138. cmd->request_bufflen;
  3139. /*
  3140. * If we have buffers which are not aligned with 16 byte cache
  3141. * lines, then we just hope nothing accesses the other parts of
  3142. * those cache lines while the transfer is in progress. That would
  3143. * fill the cache, and subsequent reads of the dma data would pick
  3144. * up the wrong thing.
  3145. * XXX We need a bounce buffer to handle that correctly.
  3146. */
  3147. if (((bbuf & 15) || (count & 15)) && (datain || dataout))
  3148. {
  3149. /* Bounce buffer needed */
  3150. if (cmd->use_sg)
  3151. printk ("53c7xx: Non-aligned buffer with use_sg\n");
  3152. else if (datain && dataout)
  3153. printk ("53c7xx: Non-aligned buffer with datain && dataout\n");
  3154. else if (count > 256)
  3155. printk ("53c7xx: Non-aligned transfer > 256 bytes\n");
  3156. else
  3157. {
  3158. if (datain)
  3159. {
  3160. tmp->bounce.len = count;
  3161. tmp->bounce.addr = vbuf;
  3162. bbuf = virt_to_bus(tmp->bounce.buf);
  3163. tmp->bounce.buf[0] = 0xff;
  3164. tmp->bounce.buf[1] = 0xfe;
  3165. tmp->bounce.buf[2] = 0xfd;
  3166. tmp->bounce.buf[3] = 0xfc;
  3167. }
  3168. if (dataout)
  3169. {
  3170. memcpy ((void *)tmp->bounce.buf, (void *)vbuf, count);
  3171. bbuf = virt_to_bus(tmp->bounce.buf);
  3172. }
  3173. }
  3174. }
  3175. if (datain) {
  3176. cache_clear(virt_to_phys((void *)vbuf), count);
  3177. /* CALL other_in, WHEN NOT DATA_IN */
  3178. cmd_datain[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL |
  3179. DCMD_TCI_IO) << 24) |
  3180. DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE;
  3181. cmd_datain[1] = virt_to_bus (hostdata->script) +
  3182. hostdata->E_other_in;
  3183. /* MOVE count, buf, WHEN DATA_IN */
  3184. cmd_datain[2] = ((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I | DCMD_BMI_IO)
  3185. << 24) | count;
  3186. cmd_datain[3] = bbuf;
  3187. #if 0
  3188. print_insn (host, cmd_datain, "dynamic ", 1);
  3189. print_insn (host, cmd_datain + 2, "dynamic ", 1);
  3190. #endif
  3191. }
  3192. if (dataout) {
  3193. cache_push(virt_to_phys((void *)vbuf), count);
  3194. /* CALL other_out, WHEN NOT DATA_OUT */
  3195. cmd_dataout[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL) << 24) |
  3196. DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE;
  3197. cmd_dataout[1] = virt_to_bus(hostdata->script) +
  3198. hostdata->E_other_out;
  3199. /* MOVE count, buf, WHEN DATA+OUT */
  3200. cmd_dataout[2] = ((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I) << 24)
  3201. | count;
  3202. cmd_dataout[3] = bbuf;
  3203. #if 0
  3204. print_insn (host, cmd_dataout, "dynamic ", 1);
  3205. print_insn (host, cmd_dataout + 2, "dynamic ", 1);
  3206. #endif
  3207. }
  3208. }
  3209. /*
  3210. * Install JUMP instructions after the data transfer routines to return
  3211. * control to the do_other_transfer routines.
  3212. */
  3213. if (datain) {
  3214. cmd_datain[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) << 24) |
  3215. DBC_TCI_TRUE;
  3216. cmd_datain[1] = virt_to_bus(hostdata->script) +
  3217. hostdata->E_other_transfer;
  3218. #if 0
  3219. print_insn (host, cmd_datain, "dynamic jump ", 1);
  3220. #endif
  3221. cmd_datain += 2;
  3222. }
  3223. #if 0
  3224. if (datain) {
  3225. cmd_datain[0] = 0x98080000;
  3226. cmd_datain[1] = 0x03ffdeed;
  3227. cmd_datain += 2;
  3228. }
  3229. #endif
  3230. if (dataout) {
  3231. cmd_dataout[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) << 24) |
  3232. DBC_TCI_TRUE;
  3233. cmd_dataout[1] = virt_to_bus(hostdata->script) +
  3234. hostdata->E_other_transfer;
  3235. #if 0
  3236. print_insn (host, cmd_dataout, "dynamic jump ", 1);
  3237. #endif
  3238. cmd_dataout += 2;
  3239. }
  3240. return tmp;
  3241. }
  3242. /*
  3243. * Function : int NCR53c7xx_queue_command (Scsi_Cmnd *cmd,
  3244. * void (*done)(Scsi_Cmnd *))
  3245. *
  3246. * Purpose : enqueues a SCSI command
  3247. *
  3248. * Inputs : cmd - SCSI command, done - function called on completion, with
  3249. * a pointer to the command descriptor.
  3250. *
  3251. * Returns : 0
  3252. *
  3253. * Side effects :
  3254. * cmd is added to the per instance driver issue_queue, with major
  3255. * twiddling done to the host specific fields of cmd. If the
  3256. * process_issue_queue coroutine isn't running, it is restarted.
  3257. *
  3258. * NOTE : we use the host_scribble field of the Scsi_Cmnd structure to
  3259. * hold our own data, and pervert the ptr field of the SCp field
  3260. * to create a linked list.
  3261. */
  3262. int
  3263. NCR53c7xx_queue_command (Scsi_Cmnd *cmd, void (* done)(Scsi_Cmnd *)) {
  3264. struct Scsi_Host *host = cmd->device->host;
  3265. struct NCR53c7x0_hostdata *hostdata =
  3266. (struct NCR53c7x0_hostdata *) host->hostdata[0];
  3267. unsigned long flags;
  3268. Scsi_Cmnd *tmp;
  3269. cmd->scsi_done = done;
  3270. cmd->host_scribble = NULL;
  3271. cmd->SCp.ptr = NULL;
  3272. cmd->SCp.buffer = NULL;
  3273. #ifdef VALID_IDS
  3274. /* Ignore commands on invalid IDs */
  3275. if (!hostdata->valid_ids[cmd->device->id]) {
  3276. printk("scsi%d : ignoring target %d lun %d\n", host->host_no,
  3277. cmd->device->id, cmd->device->lun);
  3278. cmd->result = (DID_BAD_TARGET << 16);
  3279. done(cmd);
  3280. return 0;
  3281. }
  3282. #endif
  3283. local_irq_save(flags);
  3284. if ((hostdata->options & (OPTION_DEBUG_INIT_ONLY|OPTION_DEBUG_PROBE_ONLY))
  3285. || ((hostdata->options & OPTION_DEBUG_TARGET_LIMIT) &&
  3286. !(hostdata->debug_lun_limit[cmd->device->id] & (1 << cmd->device->lun)))
  3287. #ifdef LINUX_1_2
  3288. || cmd->device->id > 7
  3289. #else
  3290. || cmd->device->id > host->max_id
  3291. #endif
  3292. || cmd->device->id == host->this_id
  3293. || hostdata->state == STATE_DISABLED) {
  3294. printk("scsi%d : disabled or bad target %d lun %d\n", host->host_no,
  3295. cmd->device->id, cmd->device->lun);
  3296. cmd->result = (DID_BAD_TARGET << 16);
  3297. done(cmd);
  3298. local_irq_restore(flags);
  3299. return 0;
  3300. }
  3301. if ((hostdata->options & OPTION_DEBUG_NCOMMANDS_LIMIT) &&
  3302. (hostdata->debug_count_limit == 0)) {
  3303. printk("scsi%d : maximum commands exceeded\n", host->host_no);
  3304. cmd->result = (DID_BAD_TARGET << 16);
  3305. done(cmd);
  3306. local_irq_restore(flags);
  3307. return 0;
  3308. }
  3309. if (hostdata->options & OPTION_DEBUG_READ_ONLY) {
  3310. switch (cmd->cmnd[0]) {
  3311. case WRITE_6:
  3312. case WRITE_10:
  3313. printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n",
  3314. host->host_no);
  3315. cmd->result = (DID_BAD_TARGET << 16);
  3316. done(cmd);
  3317. local_irq_restore(flags);
  3318. return 0;
  3319. }
  3320. }
  3321. if ((hostdata->options & OPTION_DEBUG_TARGET_LIMIT) &&
  3322. hostdata->debug_count_limit != -1)
  3323. --hostdata->debug_count_limit;
  3324. cmd->result = 0xffff; /* The NCR will overwrite message
  3325. and status with valid data */
  3326. cmd->host_scribble = (unsigned char *) tmp = create_cmd (cmd);
  3327. /*
  3328. * REQUEST SENSE commands are inserted at the head of the queue
  3329. * so that we do not clear the contingent allegiance condition
  3330. * they may be looking at.
  3331. */
  3332. if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
  3333. cmd->SCp.ptr = (unsigned char *) hostdata->issue_queue;
  3334. hostdata->issue_queue = cmd;
  3335. } else {
  3336. for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->SCp.ptr;
  3337. tmp = (Scsi_Cmnd *) tmp->SCp.ptr);
  3338. tmp->SCp.ptr = (unsigned char *) cmd;
  3339. }
  3340. local_irq_restore(flags);
  3341. run_process_issue_queue();
  3342. return 0;
  3343. }
  3344. /*
  3345. * Function : void to_schedule_list (struct Scsi_Host *host,
  3346. * struct NCR53c7x0_hostdata * hostdata, Scsi_Cmnd *cmd)
  3347. *
  3348. * Purpose : takes a SCSI command which was just removed from the
  3349. * issue queue, and deals with it by inserting it in the first
  3350. * free slot in the schedule list or by terminating it immediately.
  3351. *
  3352. * Inputs :
  3353. * host - SCSI host adapter; hostdata - hostdata structure for
  3354. * this adapter; cmd - a pointer to the command; should have
  3355. * the host_scribble field initialized to point to a valid
  3356. *
  3357. * Side effects :
  3358. * cmd is added to the per instance schedule list, with minor
  3359. * twiddling done to the host specific fields of cmd.
  3360. *
  3361. */
  3362. static __inline__ void
  3363. to_schedule_list (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata,
  3364. struct NCR53c7x0_cmd *cmd) {
  3365. NCR53c7x0_local_declare();
  3366. Scsi_Cmnd *tmp = cmd->cmd;
  3367. unsigned long flags;
  3368. /* dsa start is negative, so subtraction is used */
  3369. volatile u32 *ncrcurrent;
  3370. int i;
  3371. NCR53c7x0_local_setup(host);
  3372. #if 0
  3373. printk("scsi%d : new dsa is 0x%lx (virt 0x%p)\n", host->host_no,
  3374. virt_to_bus(hostdata->dsa), hostdata->dsa);
  3375. #endif
  3376. local_irq_save(flags);
  3377. /*
  3378. * Work around race condition : if an interrupt fired and we
  3379. * got disabled forget about this command.
  3380. */
  3381. if (hostdata->state == STATE_DISABLED) {
  3382. printk("scsi%d : driver disabled\n", host->host_no);
  3383. tmp->result = (DID_BAD_TARGET << 16);
  3384. cmd->next = (struct NCR53c7x0_cmd *) hostdata->free;
  3385. hostdata->free = cmd;
  3386. tmp->scsi_done(tmp);
  3387. local_irq_restore(flags);
  3388. return;
  3389. }
  3390. for (i = host->can_queue, ncrcurrent = hostdata->schedule;
  3391. i > 0 && ncrcurrent[0] != hostdata->NOP_insn;
  3392. --i, ncrcurrent += 2 /* JUMP instructions are two words */);
  3393. if (i > 0) {
  3394. ++hostdata->busy[tmp->device->id][tmp->device->lun];
  3395. cmd->next = hostdata->running_list;
  3396. hostdata->running_list = cmd;
  3397. /* Restore this instruction to a NOP once the command starts */
  3398. cmd->dsa [(hostdata->dsa_jump_dest - hostdata->dsa_start) /
  3399. sizeof(u32)] = (u32) virt_to_bus ((void *)ncrcurrent);
  3400. /* Replace the current jump operand. */
  3401. ncrcurrent[1] =
  3402. virt_to_bus ((void *) cmd->dsa) + hostdata->E_dsa_code_begin -
  3403. hostdata->E_dsa_code_template;
  3404. /* Replace the NOP instruction with a JUMP */
  3405. ncrcurrent[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24) |
  3406. DBC_TCI_TRUE;
  3407. } else {
  3408. printk ("scsi%d: no free slot\n", host->host_no);
  3409. disable(host);
  3410. tmp->result = (DID_ERROR << 16);
  3411. cmd->next = (struct NCR53c7x0_cmd *) hostdata->free;
  3412. hostdata->free = cmd;
  3413. tmp->scsi_done(tmp);
  3414. local_irq_restore(flags);
  3415. return;
  3416. }
  3417. /*
  3418. * If the NCR chip is in an idle state, start it running the scheduler
  3419. * immediately. Otherwise, signal the chip to jump to schedule as
  3420. * soon as it is idle.
  3421. */
  3422. if (hostdata->idle) {
  3423. hostdata->idle = 0;
  3424. hostdata->state = STATE_RUNNING;
  3425. NCR53c7x0_write32 (DSP_REG, virt_to_bus ((void *)hostdata->schedule));
  3426. if (hostdata->options & OPTION_DEBUG_TRACE)
  3427. NCR53c7x0_write8 (DCNTL_REG, hostdata->saved_dcntl |
  3428. DCNTL_SSM | DCNTL_STD);
  3429. } else {
  3430. NCR53c7x0_write8(hostdata->istat, ISTAT_10_SIGP);
  3431. }
  3432. local_irq_restore(flags);
  3433. }
  3434. /*
  3435. * Function : busyp (struct Scsi_Host *host, struct NCR53c7x0_hostdata
  3436. * *hostdata, Scsi_Cmnd *cmd)
  3437. *
  3438. * Purpose : decide if we can pass the given SCSI command on to the
  3439. * device in question or not.
  3440. *
  3441. * Returns : non-zero when we're busy, 0 when we aren't.
  3442. */
  3443. static __inline__ int
  3444. busyp (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata,
  3445. Scsi_Cmnd *cmd) {
  3446. /* FIXME : in the future, this needs to accommodate SCSI-II tagged
  3447. queuing, and we may be able to play with fairness here a bit.
  3448. */
  3449. return hostdata->busy[cmd->device->id][cmd->device->lun];
  3450. }
  3451. /*
  3452. * Function : process_issue_queue (void)
  3453. *
  3454. * Purpose : transfer commands from the issue queue to NCR start queue
  3455. * of each NCR53c7/8xx in the system, avoiding kernel stack
  3456. * overflows when the scsi_done() function is invoked recursively.
  3457. *
  3458. * NOTE : process_issue_queue exits with interrupts *disabled*, so the
  3459. * caller must reenable them if it desires.
  3460. *
  3461. * NOTE : process_issue_queue should be called from both
  3462. * NCR53c7x0_queue_command() and from the interrupt handler
  3463. * after command completion in case NCR53c7x0_queue_command()
  3464. * isn't invoked again but we've freed up resources that are
  3465. * needed.
  3466. */
  3467. static void
  3468. process_issue_queue (unsigned long flags) {
  3469. Scsi_Cmnd *tmp, *prev;
  3470. struct Scsi_Host *host;
  3471. struct NCR53c7x0_hostdata *hostdata;
  3472. int done;
  3473. /*
  3474. * We run (with interrupts disabled) until we're sure that none of
  3475. * the host adapters have anything that can be done, at which point
  3476. * we set process_issue_queue_running to 0 and exit.
  3477. *
  3478. * Interrupts are enabled before doing various other internal
  3479. * instructions, after we've decided that we need to run through
  3480. * the loop again.
  3481. *
  3482. */
  3483. do {
  3484. local_irq_disable(); /* Freeze request queues */
  3485. done = 1;
  3486. for (host = first_host; host && host->hostt == the_template;
  3487. host = host->next) {
  3488. hostdata = (struct NCR53c7x0_hostdata *) host->hostdata[0];
  3489. local_irq_disable();
  3490. if (hostdata->issue_queue) {
  3491. if (hostdata->state == STATE_DISABLED) {
  3492. tmp = (Scsi_Cmnd *) hostdata->issue_queue;
  3493. hostdata->issue_queue = (Scsi_Cmnd *) tmp->SCp.ptr;
  3494. tmp->result = (DID_BAD_TARGET << 16);
  3495. if (tmp->host_scribble) {
  3496. ((struct NCR53c7x0_cmd *)tmp->host_scribble)->next =
  3497. hostdata->free;
  3498. hostdata->free =
  3499. (struct NCR53c7x0_cmd *)tmp->host_scribble;
  3500. tmp->host_scribble = NULL;
  3501. }
  3502. tmp->scsi_done (tmp);
  3503. done = 0;
  3504. } else
  3505. for (tmp = (Scsi_Cmnd *) hostdata->issue_queue,
  3506. prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *)
  3507. tmp->SCp.ptr)
  3508. if (!tmp->host_scribble ||
  3509. !busyp (host, hostdata, tmp)) {
  3510. if (prev)
  3511. prev->SCp.ptr = tmp->SCp.ptr;
  3512. else
  3513. hostdata->issue_queue = (Scsi_Cmnd *)
  3514. tmp->SCp.ptr;
  3515. tmp->SCp.ptr = NULL;
  3516. if (tmp->host_scribble) {
  3517. if (hostdata->options & OPTION_DEBUG_QUEUES)
  3518. printk ("scsi%d : moving command for target %d lun %d to start list\n",
  3519. host->host_no, tmp->device->id, tmp->device->lun);
  3520. to_schedule_list (host, hostdata,
  3521. (struct NCR53c7x0_cmd *)
  3522. tmp->host_scribble);
  3523. } else {
  3524. if (((tmp->result & 0xff) == 0xff) ||
  3525. ((tmp->result & 0xff00) == 0xff00)) {
  3526. printk ("scsi%d : danger Will Robinson!\n",
  3527. host->host_no);
  3528. tmp->result = DID_ERROR << 16;
  3529. disable (host);
  3530. }
  3531. tmp->scsi_done(tmp);
  3532. }
  3533. done = 0;
  3534. } /* if target/lun is not busy */
  3535. } /* if hostdata->issue_queue */
  3536. if (!done)
  3537. local_irq_restore(flags);
  3538. } /* for host */
  3539. } while (!done);
  3540. process_issue_queue_running = 0;
  3541. }
  3542. /*
  3543. * Function : static void intr_scsi (struct Scsi_Host *host,
  3544. * struct NCR53c7x0_cmd *cmd)
  3545. *
  3546. * Purpose : handle all SCSI interrupts, indicated by the setting
  3547. * of the SIP bit in the ISTAT register.
  3548. *
  3549. * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
  3550. * may be NULL.
  3551. */
  3552. static void
  3553. intr_scsi (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
  3554. NCR53c7x0_local_declare();
  3555. struct NCR53c7x0_hostdata *hostdata =
  3556. (struct NCR53c7x0_hostdata *) host->hostdata[0];
  3557. unsigned char sstat0_sist0, sist1, /* Registers */
  3558. fatal; /* Did a fatal interrupt
  3559. occur ? */
  3560. NCR53c7x0_local_setup(host);
  3561. fatal = 0;
  3562. sstat0_sist0 = NCR53c7x0_read8(SSTAT0_REG);
  3563. sist1 = 0;
  3564. if (hostdata->options & OPTION_DEBUG_INTR)
  3565. printk ("scsi%d : SIST0 0x%0x, SIST1 0x%0x\n", host->host_no,
  3566. sstat0_sist0, sist1);
  3567. /* 250ms selection timeout */
  3568. if (sstat0_sist0 & SSTAT0_700_STO) {
  3569. fatal = 1;
  3570. if (hostdata->options & OPTION_DEBUG_INTR) {
  3571. printk ("scsi%d : Selection Timeout\n", host->host_no);
  3572. if (cmd) {
  3573. printk("scsi%d : target %d, lun %d, command ",
  3574. host->host_no, cmd->cmd->device->id, cmd->cmd->device->lun);
  3575. __scsi_print_command (cmd->cmd->cmnd);
  3576. printk("scsi%d : dsp = 0x%x (virt 0x%p)\n", host->host_no,
  3577. NCR53c7x0_read32(DSP_REG),
  3578. bus_to_virt(NCR53c7x0_read32(DSP_REG)));
  3579. } else {
  3580. printk("scsi%d : no command\n", host->host_no);
  3581. }
  3582. }
  3583. /*
  3584. * XXX - question : how do we want to handle the Illegal Instruction
  3585. * interrupt, which may occur before or after the Selection Timeout
  3586. * interrupt?
  3587. */
  3588. if (1) {
  3589. hostdata->idle = 1;
  3590. hostdata->expecting_sto = 0;
  3591. if (hostdata->test_running) {
  3592. hostdata->test_running = 0;
  3593. hostdata->test_completed = 3;
  3594. } else if (cmd) {
  3595. abnormal_finished(cmd, DID_BAD_TARGET << 16);
  3596. }
  3597. #if 0
  3598. hostdata->intrs = 0;
  3599. #endif
  3600. }
  3601. }
  3602. /*
  3603. * FIXME : in theory, we can also get a UDC when a STO occurs.
  3604. */
  3605. if (sstat0_sist0 & SSTAT0_UDC) {
  3606. fatal = 1;
  3607. if (cmd) {
  3608. printk("scsi%d : target %d lun %d unexpected disconnect\n",
  3609. host->host_no, cmd->cmd->device->id, cmd->cmd->device->lun);
  3610. print_lots (host);
  3611. abnormal_finished(cmd, DID_ERROR << 16);
  3612. } else
  3613. printk("scsi%d : unexpected disconnect (no command)\n",
  3614. host->host_no);
  3615. hostdata->dsp = (u32 *) hostdata->schedule;
  3616. hostdata->dsp_changed = 1;
  3617. }
  3618. /* SCSI PARITY error */
  3619. if (sstat0_sist0 & SSTAT0_PAR) {
  3620. fatal = 1;
  3621. if (cmd && cmd->cmd) {
  3622. printk("scsi%d : target %d lun %d parity error.\n",
  3623. host->host_no, cmd->cmd->device->id, cmd->cmd->device->lun);
  3624. abnormal_finished (cmd, DID_PARITY << 16);
  3625. } else
  3626. printk("scsi%d : parity error\n", host->host_no);
  3627. /* Should send message out, parity error */
  3628. /* XXX - Reduce synchronous transfer rate! */
  3629. hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
  3630. sizeof(u32);
  3631. hostdata->dsp_changed = 1;
  3632. /* SCSI GROSS error */
  3633. }
  3634. if (sstat0_sist0 & SSTAT0_SGE) {
  3635. fatal = 1;
  3636. printk("scsi%d : gross error, saved2_dsa = 0x%x\n", host->host_no,
  3637. (unsigned int)hostdata->saved2_dsa);
  3638. print_lots (host);
  3639. /*
  3640. * A SCSI gross error may occur when we have
  3641. *
  3642. * - A synchronous offset which causes the SCSI FIFO to be overwritten.
  3643. *
  3644. * - A REQ which causes the maximum synchronous offset programmed in
  3645. * the SXFER register to be exceeded.
  3646. *
  3647. * - A phase change with an outstanding synchronous offset.
  3648. *
  3649. * - Residual data in the synchronous data FIFO, with a transfer
  3650. * other than a synchronous receive is started.$#
  3651. */
  3652. /* XXX Should deduce synchronous transfer rate! */
  3653. hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
  3654. sizeof(u32);
  3655. hostdata->dsp_changed = 1;
  3656. /* Phase mismatch */
  3657. }
  3658. if (sstat0_sist0 & SSTAT0_MA) {
  3659. fatal = 1;
  3660. if (hostdata->options & OPTION_DEBUG_INTR)
  3661. printk ("scsi%d : SSTAT0_MA\n", host->host_no);
  3662. intr_phase_mismatch (host, cmd);
  3663. }
  3664. #if 0
  3665. if (sstat0_sist0 & SIST0_800_RSL)
  3666. printk ("scsi%d : Oh no Mr. Bill!\n", host->host_no);
  3667. #endif
  3668. /*
  3669. * If a fatal SCSI interrupt occurs, we must insure that the DMA and
  3670. * SCSI FIFOs were flushed.
  3671. */
  3672. if (fatal) {
  3673. if (!hostdata->dstat_valid) {
  3674. hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
  3675. hostdata->dstat_valid = 1;
  3676. }
  3677. if (!(hostdata->dstat & DSTAT_DFE)) {
  3678. printk ("scsi%d : DMA FIFO not empty\n", host->host_no);
  3679. /*
  3680. * Really need to check this code for 710 RGH.
  3681. * Havn't seen any problems, but maybe we should FLUSH before
  3682. * clearing sometimes.
  3683. */
  3684. NCR53c7x0_write8 (CTEST8_REG, CTEST8_10_CLF);
  3685. while (NCR53c7x0_read8 (CTEST8_REG) & CTEST8_10_CLF)
  3686. ;
  3687. hostdata->dstat |= DSTAT_DFE;
  3688. }
  3689. }
  3690. }
  3691. #ifdef CYCLIC_TRACE
  3692. /*
  3693. * The following implements a cyclic log of instructions executed, if you turn
  3694. * TRACE on. It will also print the log for you. Very useful when debugging
  3695. * 53c710 support, possibly not really needed any more.
  3696. */
  3697. u32 insn_log[4096];
  3698. u32 insn_log_index = 0;
  3699. void log1 (u32 i)
  3700. {
  3701. insn_log[insn_log_index++] = i;
  3702. if (insn_log_index == 4096)
  3703. insn_log_index = 0;
  3704. }
  3705. void log_insn (u32 *ip)
  3706. {
  3707. log1 ((u32)ip);
  3708. log1 (*ip);
  3709. log1 (*(ip+1));
  3710. if (((*ip >> 24) & DCMD_TYPE_MASK) == DCMD_TYPE_MMI)
  3711. log1 (*(ip+2));
  3712. }
  3713. void dump_log(void)
  3714. {
  3715. int cnt = 0;
  3716. int i = insn_log_index;
  3717. int size;
  3718. struct Scsi_Host *host = first_host;
  3719. while (cnt < 4096) {
  3720. printk ("%08x (+%6x): ", insn_log[i], (insn_log[i] - (u32)&(((struct NCR53c7x0_hostdata *)host->hostdata[0])->script))/4);
  3721. if (++i == 4096)
  3722. i = 0;
  3723. cnt++;
  3724. if (((insn_log[i] >> 24) & DCMD_TYPE_MASK) == DCMD_TYPE_MMI)
  3725. size = 3;
  3726. else
  3727. size = 2;
  3728. while (size--) {
  3729. printk ("%08x ", insn_log[i]);
  3730. if (++i == 4096)
  3731. i = 0;
  3732. cnt++;
  3733. }
  3734. printk ("\n");
  3735. }
  3736. }
  3737. #endif
  3738. /*
  3739. * Function : static void NCR53c7x0_intfly (struct Scsi_Host *host)
  3740. *
  3741. * Purpose : Scan command queue for specified host, looking for completed
  3742. * commands.
  3743. *
  3744. * Inputs : Scsi_Host pointer.
  3745. *
  3746. * This is called from the interrupt handler, when a simulated INTFLY
  3747. * interrupt occurs.
  3748. */
  3749. static void
  3750. NCR53c7x0_intfly (struct Scsi_Host *host)
  3751. {
  3752. NCR53c7x0_local_declare();
  3753. struct NCR53c7x0_hostdata *hostdata; /* host->hostdata[0] */
  3754. struct NCR53c7x0_cmd *cmd, /* command which halted */
  3755. **cmd_prev_ptr;
  3756. unsigned long flags;
  3757. char search_found = 0; /* Got at least one ? */
  3758. hostdata = (struct NCR53c7x0_hostdata *) host->hostdata[0];
  3759. NCR53c7x0_local_setup(host);
  3760. if (hostdata->options & OPTION_DEBUG_INTR)
  3761. printk ("scsi%d : INTFLY\n", host->host_no);
  3762. /*
  3763. * Traverse our list of running commands, and look
  3764. * for those with valid (non-0xff ff) status and message
  3765. * bytes encoded in the result which signify command
  3766. * completion.
  3767. */
  3768. local_irq_save(flags);
  3769. restart:
  3770. for (cmd_prev_ptr = (struct NCR53c7x0_cmd **)&(hostdata->running_list),
  3771. cmd = (struct NCR53c7x0_cmd *) hostdata->running_list; cmd ;
  3772. cmd_prev_ptr = (struct NCR53c7x0_cmd **) &(cmd->next),
  3773. cmd = (struct NCR53c7x0_cmd *) cmd->next)
  3774. {
  3775. Scsi_Cmnd *tmp;
  3776. if (!cmd) {
  3777. printk("scsi%d : very weird.\n", host->host_no);
  3778. break;
  3779. }
  3780. if (!(tmp = cmd->cmd)) {
  3781. printk("scsi%d : weird. NCR53c7x0_cmd has no Scsi_Cmnd\n",
  3782. host->host_no);
  3783. continue;
  3784. }
  3785. /* Copy the result over now; may not be complete,
  3786. * but subsequent tests may as well be done on
  3787. * cached memory.
  3788. */
  3789. tmp->result = cmd->result;
  3790. if (((tmp->result & 0xff) == 0xff) ||
  3791. ((tmp->result & 0xff00) == 0xff00))
  3792. continue;
  3793. search_found = 1;
  3794. if (cmd->bounce.len)
  3795. memcpy ((void *)cmd->bounce.addr,
  3796. (void *)cmd->bounce.buf, cmd->bounce.len);
  3797. /* Important - remove from list _before_ done is called */
  3798. if (cmd_prev_ptr)
  3799. *cmd_prev_ptr = (struct NCR53c7x0_cmd *) cmd->next;
  3800. --hostdata->busy[tmp->device->id][tmp->device->lun];
  3801. cmd->next = hostdata->free;
  3802. hostdata->free = cmd;
  3803. tmp->host_scribble = NULL;
  3804. if (hostdata->options & OPTION_DEBUG_INTR) {
  3805. printk ("scsi%d : command complete : pid %lu, id %d,lun %d result 0x%x ",
  3806. host->host_no, tmp->pid, tmp->device->id, tmp->device->lun, tmp->result);
  3807. __scsi_print_command (tmp->cmnd);
  3808. }
  3809. tmp->scsi_done(tmp);
  3810. goto restart;
  3811. }
  3812. local_irq_restore(flags);
  3813. if (!search_found) {
  3814. printk ("scsi%d : WARNING : INTFLY with no completed commands.\n",
  3815. host->host_no);
  3816. } else {
  3817. run_process_issue_queue();
  3818. }
  3819. return;
  3820. }
  3821. /*
  3822. * Function : static irqreturn_t NCR53c7x0_intr (int irq, void *dev_id, struct pt_regs * regs)
  3823. *
  3824. * Purpose : handle NCR53c7x0 interrupts for all NCR devices sharing
  3825. * the same IRQ line.
  3826. *
  3827. * Inputs : Since we're using the SA_INTERRUPT interrupt handler
  3828. * semantics, irq indicates the interrupt which invoked
  3829. * this handler.
  3830. *
  3831. * On the 710 we simualte an INTFLY with a script interrupt, and the
  3832. * script interrupt handler will call back to this function.
  3833. */
  3834. static irqreturn_t
  3835. NCR53c7x0_intr (int irq, void *dev_id, struct pt_regs * regs)
  3836. {
  3837. NCR53c7x0_local_declare();
  3838. struct Scsi_Host *host; /* Host we are looking at */
  3839. unsigned char istat; /* Values of interrupt regs */
  3840. struct NCR53c7x0_hostdata *hostdata; /* host->hostdata[0] */
  3841. struct NCR53c7x0_cmd *cmd; /* command which halted */
  3842. u32 *dsa; /* DSA */
  3843. int handled = 0;
  3844. #ifdef NCR_DEBUG
  3845. char buf[80]; /* Debugging sprintf buffer */
  3846. size_t buflen; /* Length of same */
  3847. #endif
  3848. host = (struct Scsi_Host *)dev_id;
  3849. hostdata = (struct NCR53c7x0_hostdata *) host->hostdata[0];
  3850. NCR53c7x0_local_setup(host);
  3851. /*
  3852. * Only read istat once per loop, since reading it again will unstack
  3853. * interrupts
  3854. */
  3855. while ((istat = NCR53c7x0_read8(hostdata->istat)) & (ISTAT_SIP|ISTAT_DIP)) {
  3856. handled = 1;
  3857. hostdata->dsp_changed = 0;
  3858. hostdata->dstat_valid = 0;
  3859. hostdata->state = STATE_HALTED;
  3860. if (NCR53c7x0_read8 (SSTAT2_REG) & SSTAT2_FF_MASK)
  3861. printk ("scsi%d : SCSI FIFO not empty\n", host->host_no);
  3862. /*
  3863. * NCR53c700 and NCR53c700-66 change the current SCSI
  3864. * process, hostdata->ncrcurrent, in the Linux driver so
  3865. * cmd = hostdata->ncrcurrent.
  3866. *
  3867. * With other chips, we must look through the commands
  3868. * executing and find the command structure which
  3869. * corresponds to the DSA register.
  3870. */
  3871. if (hostdata->options & OPTION_700) {
  3872. cmd = (struct NCR53c7x0_cmd *) hostdata->ncrcurrent;
  3873. } else {
  3874. dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
  3875. for (cmd = (struct NCR53c7x0_cmd *) hostdata->running_list;
  3876. cmd && (dsa + (hostdata->dsa_start / sizeof(u32))) != cmd->dsa;
  3877. cmd = (struct NCR53c7x0_cmd *)(cmd->next))
  3878. ;
  3879. }
  3880. if (hostdata->options & OPTION_DEBUG_INTR) {
  3881. if (cmd) {
  3882. printk("scsi%d : interrupt for pid %lu, id %d, lun %d ",
  3883. host->host_no, cmd->cmd->pid, (int) cmd->cmd->device->id,
  3884. (int) cmd->cmd->device->lun);
  3885. __scsi_print_command (cmd->cmd->cmnd);
  3886. } else {
  3887. printk("scsi%d : no active command\n", host->host_no);
  3888. }
  3889. }
  3890. if (istat & ISTAT_SIP) {
  3891. if (hostdata->options & OPTION_DEBUG_INTR)
  3892. printk ("scsi%d : ISTAT_SIP\n", host->host_no);
  3893. intr_scsi (host, cmd);
  3894. }
  3895. if (istat & ISTAT_DIP) {
  3896. if (hostdata->options & OPTION_DEBUG_INTR)
  3897. printk ("scsi%d : ISTAT_DIP\n", host->host_no);
  3898. intr_dma (host, cmd);
  3899. }
  3900. if (!hostdata->dstat_valid) {
  3901. hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
  3902. hostdata->dstat_valid = 1;
  3903. }
  3904. if (!(hostdata->dstat & DSTAT_DFE)) {
  3905. printk ("scsi%d : DMA FIFO not empty\n", host->host_no);
  3906. /* Really need to check this out for 710 RGH */
  3907. NCR53c7x0_write8 (CTEST8_REG, CTEST8_10_CLF);
  3908. while (NCR53c7x0_read8 (CTEST8_REG) & CTEST8_10_CLF)
  3909. ;
  3910. hostdata->dstat |= DSTAT_DFE;
  3911. }
  3912. if (!hostdata->idle && hostdata->state == STATE_HALTED) {
  3913. if (!hostdata->dsp_changed)
  3914. hostdata->dsp = (u32 *)bus_to_virt(NCR53c7x0_read32(DSP_REG));
  3915. #if 0
  3916. printk("scsi%d : new dsp is 0x%lx (virt 0x%p)\n",
  3917. host->host_no, virt_to_bus(hostdata->dsp), hostdata->dsp);
  3918. #endif
  3919. hostdata->state = STATE_RUNNING;
  3920. NCR53c7x0_write32 (DSP_REG, virt_to_bus(hostdata->dsp));
  3921. if (hostdata->options & OPTION_DEBUG_TRACE) {
  3922. #ifdef CYCLIC_TRACE
  3923. log_insn (hostdata->dsp);
  3924. #else
  3925. print_insn (host, hostdata->dsp, "t ", 1);
  3926. #endif
  3927. NCR53c7x0_write8 (DCNTL_REG,
  3928. hostdata->saved_dcntl | DCNTL_SSM | DCNTL_STD);
  3929. }
  3930. }
  3931. }
  3932. return IRQ_HANDLED;
  3933. }
  3934. /*
  3935. * Function : static int abort_connected (struct Scsi_Host *host)
  3936. *
  3937. * Purpose : Assuming that the NCR SCSI processor is currently
  3938. * halted, break the currently established nexus. Clean
  3939. * up of the NCR53c7x0_cmd and Scsi_Cmnd structures should
  3940. * be done on receipt of the abort interrupt.
  3941. *
  3942. * Inputs : host - SCSI host
  3943. *
  3944. */
  3945. static int
  3946. abort_connected (struct Scsi_Host *host) {
  3947. #ifdef NEW_ABORT
  3948. NCR53c7x0_local_declare();
  3949. #endif
  3950. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  3951. host->hostdata[0];
  3952. /* FIXME : this probably should change for production kernels; at the
  3953. least, counter should move to a per-host structure. */
  3954. static int counter = 5;
  3955. #ifdef NEW_ABORT
  3956. int sstat, phase, offset;
  3957. u32 *script;
  3958. NCR53c7x0_local_setup(host);
  3959. #endif
  3960. if (--counter <= 0) {
  3961. disable(host);
  3962. return 0;
  3963. }
  3964. printk ("scsi%d : DANGER : abort_connected() called \n",
  3965. host->host_no);
  3966. #ifdef NEW_ABORT
  3967. /*
  3968. * New strategy : Rather than using a generic abort routine,
  3969. * we'll specifically try to source or sink the appropriate
  3970. * amount of data for the phase we're currently in (taking into
  3971. * account the current synchronous offset)
  3972. */
  3973. sstat = (NCR53c8x0_read8 (SSTAT2_REG);
  3974. offset = OFFSET (sstat & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT;
  3975. phase = sstat & SSTAT2_PHASE_MASK;
  3976. /*
  3977. * SET ATN
  3978. * MOVE source_or_sink, WHEN CURRENT PHASE
  3979. * < repeat for each outstanding byte >
  3980. * JUMP send_abort_message
  3981. */
  3982. script = hostdata->abort_script = kmalloc (
  3983. 8 /* instruction size */ * (
  3984. 1 /* set ATN */ +
  3985. (!offset ? 1 : offset) /* One transfer per outstanding byte */ +
  3986. 1 /* send abort message */),
  3987. GFP_ATOMIC);
  3988. #else /* def NEW_ABORT */
  3989. hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
  3990. sizeof(u32);
  3991. #endif /* def NEW_ABORT */
  3992. hostdata->dsp_changed = 1;
  3993. /* XXX - need to flag the command as aborted after the abort_connected
  3994. code runs
  3995. */
  3996. return 0;
  3997. }
  3998. /*
  3999. * Function : static int datapath_residual (Scsi_Host *host)
  4000. *
  4001. * Purpose : return residual data count of what's in the chip.
  4002. *
  4003. * Inputs : host - SCSI host
  4004. */
  4005. static int
  4006. datapath_residual (struct Scsi_Host *host) {
  4007. NCR53c7x0_local_declare();
  4008. int count, synchronous, sstat;
  4009. unsigned int ddir;
  4010. NCR53c7x0_local_setup(host);
  4011. /* COMPAT : the 700 and 700-66 need to use DFIFO_00_BO_MASK */
  4012. count = ((NCR53c7x0_read8 (DFIFO_REG) & DFIFO_10_BO_MASK) -
  4013. (NCR53c7x0_read32 (DBC_REG) & DFIFO_10_BO_MASK)) & DFIFO_10_BO_MASK;
  4014. synchronous = NCR53c7x0_read8 (SXFER_REG) & SXFER_MO_MASK;
  4015. /* COMPAT : DDIR is elsewhere on non-'8xx chips. */
  4016. ddir = NCR53c7x0_read8 (CTEST0_REG_700) & CTEST0_700_DDIR;
  4017. if (ddir) {
  4018. /* Receive */
  4019. if (synchronous)
  4020. count += (NCR53c7x0_read8 (SSTAT2_REG) & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT;
  4021. else
  4022. if (NCR53c7x0_read8 (SSTAT1_REG) & SSTAT1_ILF)
  4023. ++count;
  4024. } else {
  4025. /* Send */
  4026. sstat = NCR53c7x0_read8 (SSTAT1_REG);
  4027. if (sstat & SSTAT1_OLF)
  4028. ++count;
  4029. if (synchronous && (sstat & SSTAT1_ORF))
  4030. ++count;
  4031. }
  4032. return count;
  4033. }
  4034. /*
  4035. * Function : static const char * sbcl_to_phase (int sbcl)_
  4036. *
  4037. * Purpose : Convert SBCL register to user-parsable phase representation
  4038. *
  4039. * Inputs : sbcl - value of sbcl register
  4040. */
  4041. static const char *
  4042. sbcl_to_phase (int sbcl) {
  4043. switch (sbcl & SBCL_PHASE_MASK) {
  4044. case SBCL_PHASE_DATAIN:
  4045. return "DATAIN";
  4046. case SBCL_PHASE_DATAOUT:
  4047. return "DATAOUT";
  4048. case SBCL_PHASE_MSGIN:
  4049. return "MSGIN";
  4050. case SBCL_PHASE_MSGOUT:
  4051. return "MSGOUT";
  4052. case SBCL_PHASE_CMDOUT:
  4053. return "CMDOUT";
  4054. case SBCL_PHASE_STATIN:
  4055. return "STATUSIN";
  4056. default:
  4057. return "unknown";
  4058. }
  4059. }
  4060. /*
  4061. * Function : static const char * sstat2_to_phase (int sstat)_
  4062. *
  4063. * Purpose : Convert SSTAT2 register to user-parsable phase representation
  4064. *
  4065. * Inputs : sstat - value of sstat register
  4066. */
  4067. static const char *
  4068. sstat2_to_phase (int sstat) {
  4069. switch (sstat & SSTAT2_PHASE_MASK) {
  4070. case SSTAT2_PHASE_DATAIN:
  4071. return "DATAIN";
  4072. case SSTAT2_PHASE_DATAOUT:
  4073. return "DATAOUT";
  4074. case SSTAT2_PHASE_MSGIN:
  4075. return "MSGIN";
  4076. case SSTAT2_PHASE_MSGOUT:
  4077. return "MSGOUT";
  4078. case SSTAT2_PHASE_CMDOUT:
  4079. return "CMDOUT";
  4080. case SSTAT2_PHASE_STATIN:
  4081. return "STATUSIN";
  4082. default:
  4083. return "unknown";
  4084. }
  4085. }
  4086. /*
  4087. * Function : static void intr_phase_mismatch (struct Scsi_Host *host,
  4088. * struct NCR53c7x0_cmd *cmd)
  4089. *
  4090. * Purpose : Handle phase mismatch interrupts
  4091. *
  4092. * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
  4093. * may be NULL.
  4094. *
  4095. * Side effects : The abort_connected() routine is called or the NCR chip
  4096. * is restarted, jumping to the command_complete entry point, or
  4097. * patching the address and transfer count of the current instruction
  4098. * and calling the msg_in entry point as appropriate.
  4099. */
  4100. static void
  4101. intr_phase_mismatch (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
  4102. NCR53c7x0_local_declare();
  4103. u32 dbc_dcmd, *dsp, *dsp_next;
  4104. unsigned char dcmd, sbcl;
  4105. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  4106. host->hostdata[0];
  4107. int residual;
  4108. enum {ACTION_ABORT, ACTION_ABORT_PRINT, ACTION_CONTINUE} action =
  4109. ACTION_ABORT_PRINT;
  4110. const char *where = NULL;
  4111. NCR53c7x0_local_setup(host);
  4112. /*
  4113. * Corrective action is based on where in the SCSI SCRIPT(tm) the error
  4114. * occurred, as well as which SCSI phase we are currently in.
  4115. */
  4116. dsp_next = bus_to_virt(NCR53c7x0_read32(DSP_REG));
  4117. /*
  4118. * Fetch the current instruction, and remove the operands for easier
  4119. * interpretation.
  4120. */
  4121. dbc_dcmd = NCR53c7x0_read32(DBC_REG);
  4122. dcmd = (dbc_dcmd & 0xff000000) >> 24;
  4123. /*
  4124. * Like other processors, the NCR adjusts the instruction pointer before
  4125. * instruction decode. Set the DSP address back to what it should
  4126. * be for this instruction based on its size (2 or 3 32 bit words).
  4127. */
  4128. dsp = dsp_next - NCR53c7x0_insn_size(dcmd);
  4129. /*
  4130. * Read new SCSI phase from the SBCL lines. Since all of our code uses
  4131. * a WHEN conditional instead of an IF conditional, we don't need to
  4132. * wait for a new REQ.
  4133. */
  4134. sbcl = NCR53c7x0_read8(SBCL_REG) & SBCL_PHASE_MASK;
  4135. if (!cmd) {
  4136. action = ACTION_ABORT_PRINT;
  4137. where = "no current command";
  4138. /*
  4139. * The way my SCSI SCRIPTS(tm) are architected, recoverable phase
  4140. * mismatches should only occur where we're doing a multi-byte
  4141. * BMI instruction. Specifically, this means
  4142. *
  4143. * - select messages (a SCSI-I target may ignore additional messages
  4144. * after the IDENTIFY; any target may reject a SDTR or WDTR)
  4145. *
  4146. * - command out (targets may send a message to signal an error
  4147. * condition, or go into STATUSIN after they've decided
  4148. * they don't like the command.
  4149. *
  4150. * - reply_message (targets may reject a multi-byte message in the
  4151. * middle)
  4152. *
  4153. * - data transfer routines (command completion with buffer space
  4154. * left, disconnect message, or error message)
  4155. */
  4156. } else if (((dsp >= cmd->data_transfer_start &&
  4157. dsp < cmd->data_transfer_end)) || dsp == (cmd->residual + 2)) {
  4158. if ((dcmd & (DCMD_TYPE_MASK|DCMD_BMI_OP_MASK|DCMD_BMI_INDIRECT|
  4159. DCMD_BMI_MSG|DCMD_BMI_CD)) == (DCMD_TYPE_BMI|
  4160. DCMD_BMI_OP_MOVE_I)) {
  4161. residual = datapath_residual (host);
  4162. if (hostdata->options & OPTION_DEBUG_DISCONNECT)
  4163. printk ("scsi%d : handling residual transfer (+ %d bytes from DMA FIFO)\n",
  4164. host->host_no, residual);
  4165. /*
  4166. * The first instruction is a CALL to the alternate handler for
  4167. * this data transfer phase, so we can do calls to
  4168. * munge_msg_restart as we would if control were passed
  4169. * from normal dynamic code.
  4170. */
  4171. if (dsp != cmd->residual + 2) {
  4172. cmd->residual[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL |
  4173. ((dcmd & DCMD_BMI_IO) ? DCMD_TCI_IO : 0)) << 24) |
  4174. DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE;
  4175. cmd->residual[1] = virt_to_bus(hostdata->script)
  4176. + ((dcmd & DCMD_BMI_IO)
  4177. ? hostdata->E_other_in : hostdata->E_other_out);
  4178. }
  4179. /*
  4180. * The second instruction is the a data transfer block
  4181. * move instruction, reflecting the pointer and count at the
  4182. * time of the phase mismatch.
  4183. */
  4184. cmd->residual[2] = dbc_dcmd + residual;
  4185. cmd->residual[3] = NCR53c7x0_read32(DNAD_REG) - residual;
  4186. /*
  4187. * The third and final instruction is a jump to the instruction
  4188. * which follows the instruction which had to be 'split'
  4189. */
  4190. if (dsp != cmd->residual + 2) {
  4191. cmd->residual[4] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP)
  4192. << 24) | DBC_TCI_TRUE;
  4193. cmd->residual[5] = virt_to_bus(dsp_next);
  4194. }
  4195. /*
  4196. * For the sake of simplicity, transfer control to the
  4197. * conditional CALL at the start of the residual buffer.
  4198. */
  4199. hostdata->dsp = cmd->residual;
  4200. hostdata->dsp_changed = 1;
  4201. action = ACTION_CONTINUE;
  4202. } else {
  4203. where = "non-BMI dynamic DSA code";
  4204. action = ACTION_ABORT_PRINT;
  4205. }
  4206. } else if (dsp == (hostdata->script + hostdata->E_select_msgout / 4 + 2)) {
  4207. /* RGH 290697: Added +2 above, to compensate for the script
  4208. * instruction which disables the selection timer. */
  4209. /* Release ATN */
  4210. NCR53c7x0_write8 (SOCL_REG, 0);
  4211. switch (sbcl) {
  4212. /*
  4213. * Some devices (SQ555 come to mind) grab the IDENTIFY message
  4214. * sent on selection, and decide to go into COMMAND OUT phase
  4215. * rather than accepting the rest of the messages or rejecting
  4216. * them. Handle these devices gracefully.
  4217. */
  4218. case SBCL_PHASE_CMDOUT:
  4219. hostdata->dsp = dsp + 2 /* two _words_ */;
  4220. hostdata->dsp_changed = 1;
  4221. printk ("scsi%d : target %d ignored SDTR and went into COMMAND OUT\n",
  4222. host->host_no, cmd->cmd->device->id);
  4223. cmd->flags &= ~CMD_FLAG_SDTR;
  4224. action = ACTION_CONTINUE;
  4225. break;
  4226. case SBCL_PHASE_MSGIN:
  4227. hostdata->dsp = hostdata->script + hostdata->E_msg_in /
  4228. sizeof(u32);
  4229. hostdata->dsp_changed = 1;
  4230. action = ACTION_CONTINUE;
  4231. break;
  4232. default:
  4233. where="select message out";
  4234. action = ACTION_ABORT_PRINT;
  4235. }
  4236. /*
  4237. * Some SCSI devices will interpret a command as they read the bytes
  4238. * off the SCSI bus, and may decide that the command is Bogus before
  4239. * they've read the entire command off the bus.
  4240. */
  4241. } else if (dsp == hostdata->script + hostdata->E_cmdout_cmdout / sizeof
  4242. (u32)) {
  4243. hostdata->dsp = hostdata->script + hostdata->E_data_transfer /
  4244. sizeof (u32);
  4245. hostdata->dsp_changed = 1;
  4246. action = ACTION_CONTINUE;
  4247. /* FIXME : we need to handle message reject, etc. within msg_respond. */
  4248. #ifdef notyet
  4249. } else if (dsp == hostdata->script + hostdata->E_reply_message) {
  4250. switch (sbcl) {
  4251. /* Any other phase mismatches abort the currently executing command. */
  4252. #endif
  4253. } else {
  4254. where = "unknown location";
  4255. action = ACTION_ABORT_PRINT;
  4256. }
  4257. /* Flush DMA FIFO */
  4258. if (!hostdata->dstat_valid) {
  4259. hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
  4260. hostdata->dstat_valid = 1;
  4261. }
  4262. if (!(hostdata->dstat & DSTAT_DFE)) {
  4263. /* Really need to check this out for 710 RGH */
  4264. NCR53c7x0_write8 (CTEST8_REG, CTEST8_10_CLF);
  4265. while (NCR53c7x0_read8 (CTEST8_REG) & CTEST8_10_CLF);
  4266. hostdata->dstat |= DSTAT_DFE;
  4267. }
  4268. switch (action) {
  4269. case ACTION_ABORT_PRINT:
  4270. printk("scsi%d : %s : unexpected phase %s.\n",
  4271. host->host_no, where ? where : "unknown location",
  4272. sbcl_to_phase(sbcl));
  4273. print_lots (host);
  4274. /* Fall through to ACTION_ABORT */
  4275. case ACTION_ABORT:
  4276. abort_connected (host);
  4277. break;
  4278. case ACTION_CONTINUE:
  4279. break;
  4280. }
  4281. #if 0
  4282. if (hostdata->dsp_changed) {
  4283. printk("scsi%d: new dsp 0x%p\n", host->host_no, hostdata->dsp);
  4284. print_insn (host, hostdata->dsp, "", 1);
  4285. }
  4286. #endif
  4287. }
  4288. /*
  4289. * Function : static void intr_bf (struct Scsi_Host *host,
  4290. * struct NCR53c7x0_cmd *cmd)
  4291. *
  4292. * Purpose : handle BUS FAULT interrupts
  4293. *
  4294. * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
  4295. * may be NULL.
  4296. */
  4297. static void
  4298. intr_bf (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
  4299. NCR53c7x0_local_declare();
  4300. u32 *dsp,
  4301. *next_dsp, /* Current dsp */
  4302. *dsa,
  4303. dbc_dcmd; /* DCMD (high eight bits) + DBC */
  4304. char *reason = NULL;
  4305. /* Default behavior is for a silent error, with a retry until we've
  4306. exhausted retries. */
  4307. enum {MAYBE, ALWAYS, NEVER} retry = MAYBE;
  4308. int report = 0;
  4309. NCR53c7x0_local_setup(host);
  4310. dbc_dcmd = NCR53c7x0_read32 (DBC_REG);
  4311. next_dsp = bus_to_virt (NCR53c7x0_read32(DSP_REG));
  4312. dsp = next_dsp - NCR53c7x0_insn_size ((dbc_dcmd >> 24) & 0xff);
  4313. /* FIXME - check chip type */
  4314. dsa = bus_to_virt (NCR53c7x0_read32(DSA_REG));
  4315. /*
  4316. * Bus faults can be caused by either a Bad Address or
  4317. * Target Abort. We should check the Received Target Abort
  4318. * bit of the PCI status register and Master Abort Bit.
  4319. *
  4320. * - Master Abort bit indicates that no device claimed
  4321. * the address with DEVSEL within five clocks
  4322. *
  4323. * - Target Abort bit indicates that a target claimed it,
  4324. * but changed its mind once it saw the byte enables.
  4325. *
  4326. */
  4327. /* 53c710, not PCI system */
  4328. report = 1;
  4329. reason = "Unknown";
  4330. #ifndef notyet
  4331. report = 1;
  4332. #endif
  4333. if (report && reason)
  4334. {
  4335. printk(KERN_ALERT "scsi%d : BUS FAULT reason = %s\n",
  4336. host->host_no, reason ? reason : "unknown");
  4337. print_lots (host);
  4338. }
  4339. #ifndef notyet
  4340. retry = NEVER;
  4341. #endif
  4342. /*
  4343. * TODO : we should attempt to recover from any spurious bus
  4344. * faults. After X retries, we should figure that things are
  4345. * sufficiently wedged, and call NCR53c7xx_reset.
  4346. *
  4347. * This code should only get executed once we've decided that we
  4348. * cannot retry.
  4349. */
  4350. if (retry == NEVER) {
  4351. printk(KERN_ALERT " mail richard@sleepie.demon.co.uk\n");
  4352. FATAL (host);
  4353. }
  4354. }
  4355. /*
  4356. * Function : static void intr_dma (struct Scsi_Host *host,
  4357. * struct NCR53c7x0_cmd *cmd)
  4358. *
  4359. * Purpose : handle all DMA interrupts, indicated by the setting
  4360. * of the DIP bit in the ISTAT register.
  4361. *
  4362. * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
  4363. * may be NULL.
  4364. */
  4365. static void
  4366. intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
  4367. NCR53c7x0_local_declare();
  4368. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  4369. host->hostdata[0];
  4370. unsigned char dstat; /* DSTAT */
  4371. u32 *dsp,
  4372. *next_dsp, /* Current dsp */
  4373. *dsa,
  4374. dbc_dcmd; /* DCMD (high eight bits) + DBC */
  4375. int tmp;
  4376. unsigned long flags;
  4377. NCR53c7x0_local_setup(host);
  4378. if (!hostdata->dstat_valid) {
  4379. hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
  4380. hostdata->dstat_valid = 1;
  4381. }
  4382. dstat = hostdata->dstat;
  4383. if (hostdata->options & OPTION_DEBUG_INTR)
  4384. printk("scsi%d : DSTAT=0x%x\n", host->host_no, (int) dstat);
  4385. dbc_dcmd = NCR53c7x0_read32 (DBC_REG);
  4386. next_dsp = bus_to_virt(NCR53c7x0_read32(DSP_REG));
  4387. dsp = next_dsp - NCR53c7x0_insn_size ((dbc_dcmd >> 24) & 0xff);
  4388. /* XXX - check chip type */
  4389. dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
  4390. /*
  4391. * DSTAT_ABRT is the aborted interrupt. This is set whenever the
  4392. * SCSI chip is aborted.
  4393. *
  4394. * With NCR53c700 and NCR53c700-66 style chips, we should only
  4395. * get this when the chip is currently running the accept
  4396. * reselect/select code and we have set the abort bit in the
  4397. * ISTAT register.
  4398. *
  4399. */
  4400. if (dstat & DSTAT_ABRT) {
  4401. #if 0
  4402. /* XXX - add code here to deal with normal abort */
  4403. if ((hostdata->options & OPTION_700) && (hostdata->state ==
  4404. STATE_ABORTING)) {
  4405. } else
  4406. #endif
  4407. {
  4408. printk(KERN_ALERT "scsi%d : unexpected abort interrupt at\n"
  4409. " ", host->host_no);
  4410. print_insn (host, dsp, KERN_ALERT "s ", 1);
  4411. FATAL (host);
  4412. }
  4413. }
  4414. /*
  4415. * DSTAT_SSI is the single step interrupt. Should be generated
  4416. * whenever we have single stepped or are tracing.
  4417. */
  4418. if (dstat & DSTAT_SSI) {
  4419. if (hostdata->options & OPTION_DEBUG_TRACE) {
  4420. /* Don't print instr. until we write DSP at end of intr function */
  4421. } else if (hostdata->options & OPTION_DEBUG_SINGLE) {
  4422. print_insn (host, dsp, "s ", 0);
  4423. local_irq_save(flags);
  4424. /* XXX - should we do this, or can we get away with writing dsp? */
  4425. NCR53c7x0_write8 (DCNTL_REG, (NCR53c7x0_read8(DCNTL_REG) &
  4426. ~DCNTL_SSM) | DCNTL_STD);
  4427. local_irq_restore(flags);
  4428. } else {
  4429. printk(KERN_ALERT "scsi%d : unexpected single step interrupt at\n"
  4430. " ", host->host_no);
  4431. print_insn (host, dsp, KERN_ALERT "", 1);
  4432. printk(KERN_ALERT " mail drew@PoohSticks.ORG\n");
  4433. FATAL (host);
  4434. }
  4435. }
  4436. /*
  4437. * DSTAT_IID / DSTAT_OPC (same bit, same meaning, only the name
  4438. * is different) is generated whenever an illegal instruction is
  4439. * encountered.
  4440. *
  4441. * XXX - we may want to emulate INTFLY here, so we can use
  4442. * the same SCSI SCRIPT (tm) for NCR53c710 through NCR53c810
  4443. * chips.
  4444. */
  4445. if (dstat & DSTAT_OPC) {
  4446. /*
  4447. * Ascertain if this IID interrupts occurred before or after a STO
  4448. * interrupt. Since the interrupt handling code now leaves
  4449. * DSP unmodified until _after_ all stacked interrupts have been
  4450. * processed, reading the DSP returns the original DSP register.
  4451. * This means that if dsp lies between the select code, and
  4452. * message out following the selection code (where the IID interrupt
  4453. * would have to have occurred by due to the implicit wait for REQ),
  4454. * we have an IID interrupt resulting from a STO condition and
  4455. * can ignore it.
  4456. */
  4457. if (((dsp >= (hostdata->script + hostdata->E_select / sizeof(u32))) &&
  4458. (dsp <= (hostdata->script + hostdata->E_select_msgout /
  4459. sizeof(u32) + 8))) || (hostdata->test_running == 2)) {
  4460. if (hostdata->options & OPTION_DEBUG_INTR)
  4461. printk ("scsi%d : ignoring DSTAT_IID for SSTAT_STO\n",
  4462. host->host_no);
  4463. if (hostdata->expecting_iid) {
  4464. hostdata->expecting_iid = 0;
  4465. hostdata->idle = 1;
  4466. if (hostdata->test_running == 2) {
  4467. hostdata->test_running = 0;
  4468. hostdata->test_completed = 3;
  4469. } else if (cmd)
  4470. abnormal_finished (cmd, DID_BAD_TARGET << 16);
  4471. } else {
  4472. hostdata->expecting_sto = 1;
  4473. }
  4474. /*
  4475. * We can't guarantee we'll be able to execute the WAIT DISCONNECT
  4476. * instruction within the 3.4us of bus free and arbitration delay
  4477. * that a target can RESELECT in and assert REQ after we've dropped
  4478. * ACK. If this happens, we'll get an illegal instruction interrupt.
  4479. * Doing away with the WAIT DISCONNECT instructions broke everything,
  4480. * so instead I'll settle for moving one WAIT DISCONNECT a few
  4481. * instructions closer to the CLEAR ACK before it to minimize the
  4482. * chances of this happening, and handle it if it occurs anyway.
  4483. *
  4484. * Simply continue with what we were doing, and control should
  4485. * be transferred to the schedule routine which will ultimately
  4486. * pass control onto the reselection or selection (not yet)
  4487. * code.
  4488. */
  4489. } else if (dbc_dcmd == 0x48000000 && (NCR53c7x0_read8 (SBCL_REG) &
  4490. SBCL_REQ)) {
  4491. if (!(hostdata->options & OPTION_NO_PRINT_RACE))
  4492. {
  4493. printk("scsi%d: REQ before WAIT DISCONNECT IID\n",
  4494. host->host_no);
  4495. hostdata->options |= OPTION_NO_PRINT_RACE;
  4496. }
  4497. } else {
  4498. printk(KERN_ALERT "scsi%d : invalid instruction\n", host->host_no);
  4499. print_lots (host);
  4500. printk(KERN_ALERT " mail Richard@sleepie.demon.co.uk with ALL\n"
  4501. " boot messages and diagnostic output\n");
  4502. FATAL (host);
  4503. }
  4504. }
  4505. /*
  4506. * DSTAT_BF are bus fault errors. DSTAT_800_BF is valid for 710 also.
  4507. */
  4508. if (dstat & DSTAT_800_BF) {
  4509. intr_bf (host, cmd);
  4510. }
  4511. /*
  4512. * DSTAT_SIR interrupts are generated by the execution of
  4513. * the INT instruction. Since the exact values available
  4514. * are determined entirely by the SCSI script running,
  4515. * and are local to a particular script, a unique handler
  4516. * is called for each script.
  4517. */
  4518. if (dstat & DSTAT_SIR) {
  4519. if (hostdata->options & OPTION_DEBUG_INTR)
  4520. printk ("scsi%d : DSTAT_SIR\n", host->host_no);
  4521. switch ((tmp = hostdata->dstat_sir_intr (host, cmd))) {
  4522. case SPECIFIC_INT_NOTHING:
  4523. case SPECIFIC_INT_RESTART:
  4524. break;
  4525. case SPECIFIC_INT_ABORT:
  4526. abort_connected(host);
  4527. break;
  4528. case SPECIFIC_INT_PANIC:
  4529. printk(KERN_ALERT "scsi%d : failure at ", host->host_no);
  4530. print_insn (host, dsp, KERN_ALERT "", 1);
  4531. printk(KERN_ALERT " dstat_sir_intr() returned SPECIFIC_INT_PANIC\n");
  4532. FATAL (host);
  4533. break;
  4534. case SPECIFIC_INT_BREAK:
  4535. intr_break (host, cmd);
  4536. break;
  4537. default:
  4538. printk(KERN_ALERT "scsi%d : failure at ", host->host_no);
  4539. print_insn (host, dsp, KERN_ALERT "", 1);
  4540. printk(KERN_ALERT" dstat_sir_intr() returned unknown value %d\n",
  4541. tmp);
  4542. FATAL (host);
  4543. }
  4544. }
  4545. }
  4546. /*
  4547. * Function : static int print_insn (struct Scsi_Host *host,
  4548. * u32 *insn, int kernel)
  4549. *
  4550. * Purpose : print numeric representation of the instruction pointed
  4551. * to by insn to the debugging or kernel message buffer
  4552. * as appropriate.
  4553. *
  4554. * If desired, a user level program can interpret this
  4555. * information.
  4556. *
  4557. * Inputs : host, insn - host, pointer to instruction, prefix -
  4558. * string to prepend, kernel - use printk instead of debugging buffer.
  4559. *
  4560. * Returns : size, in u32s, of instruction printed.
  4561. */
  4562. /*
  4563. * FIXME: should change kernel parameter so that it takes an ENUM
  4564. * specifying severity - either KERN_ALERT or KERN_PANIC so
  4565. * all panic messages are output with the same severity.
  4566. */
  4567. static int
  4568. print_insn (struct Scsi_Host *host, const u32 *insn,
  4569. const char *prefix, int kernel) {
  4570. char buf[160], /* Temporary buffer and pointer. ICKY
  4571. arbitrary length. */
  4572. *tmp;
  4573. unsigned char dcmd; /* dcmd register for *insn */
  4574. int size;
  4575. /*
  4576. * Check to see if the instruction pointer is not bogus before
  4577. * indirecting through it; avoiding red-zone at start of
  4578. * memory.
  4579. *
  4580. * FIXME: icky magic needs to happen here on non-intel boxes which
  4581. * don't have kernel memory mapped in like this. Might be reasonable
  4582. * to use vverify()?
  4583. */
  4584. if (virt_to_phys((void *)insn) < PAGE_SIZE ||
  4585. virt_to_phys((void *)(insn + 8)) > virt_to_phys(high_memory) ||
  4586. ((((dcmd = (insn[0] >> 24) & 0xff) & DCMD_TYPE_MMI) == DCMD_TYPE_MMI) &&
  4587. virt_to_phys((void *)(insn + 12)) > virt_to_phys(high_memory))) {
  4588. size = 0;
  4589. sprintf (buf, "%s%p: address out of range\n",
  4590. prefix, insn);
  4591. } else {
  4592. /*
  4593. * FIXME : (void *) cast in virt_to_bus should be unnecessary, because
  4594. * it should take const void * as argument.
  4595. */
  4596. #if !defined(CONFIG_MVME16x) && !defined(CONFIG_BVME6000)
  4597. sprintf(buf, "%s0x%lx (virt 0x%p) : 0x%08x 0x%08x (virt 0x%p)",
  4598. (prefix ? prefix : ""), virt_to_bus((void *) insn), insn,
  4599. insn[0], insn[1], bus_to_virt (insn[1]));
  4600. #else
  4601. /* Remove virtual addresses to reduce output, as they are the same */
  4602. sprintf(buf, "%s0x%x (+%x) : 0x%08x 0x%08x",
  4603. (prefix ? prefix : ""), (u32)insn, ((u32)insn -
  4604. (u32)&(((struct NCR53c7x0_hostdata *)host->hostdata[0])->script))/4,
  4605. insn[0], insn[1]);
  4606. #endif
  4607. tmp = buf + strlen(buf);
  4608. if ((dcmd & DCMD_TYPE_MASK) == DCMD_TYPE_MMI) {
  4609. #if !defined(CONFIG_MVME16x) && !defined(CONFIG_BVME6000)
  4610. sprintf (tmp, " 0x%08x (virt 0x%p)\n", insn[2],
  4611. bus_to_virt(insn[2]));
  4612. #else
  4613. /* Remove virtual addr to reduce output, as it is the same */
  4614. sprintf (tmp, " 0x%08x\n", insn[2]);
  4615. #endif
  4616. size = 3;
  4617. } else {
  4618. sprintf (tmp, "\n");
  4619. size = 2;
  4620. }
  4621. }
  4622. if (kernel)
  4623. printk ("%s", buf);
  4624. #ifdef NCR_DEBUG
  4625. else {
  4626. size_t len = strlen(buf);
  4627. debugger_kernel_write(host, buf, len);
  4628. }
  4629. #endif
  4630. return size;
  4631. }
  4632. /*
  4633. * Function : int NCR53c7xx_abort (Scsi_Cmnd *cmd)
  4634. *
  4635. * Purpose : Abort an errant SCSI command, doing all necessary
  4636. * cleanup of the issue_queue, running_list, shared Linux/NCR
  4637. * dsa issue and reconnect queues.
  4638. *
  4639. * Inputs : cmd - command to abort, code - entire result field
  4640. *
  4641. * Returns : 0 on success, -1 on failure.
  4642. */
  4643. int
  4644. NCR53c7xx_abort (Scsi_Cmnd *cmd) {
  4645. NCR53c7x0_local_declare();
  4646. struct Scsi_Host *host = cmd->device->host;
  4647. struct NCR53c7x0_hostdata *hostdata = host ? (struct NCR53c7x0_hostdata *)
  4648. host->hostdata[0] : NULL;
  4649. unsigned long flags;
  4650. struct NCR53c7x0_cmd *curr, **prev;
  4651. Scsi_Cmnd *me, **last;
  4652. #if 0
  4653. static long cache_pid = -1;
  4654. #endif
  4655. if (!host) {
  4656. printk ("Bogus SCSI command pid %ld; no host structure\n",
  4657. cmd->pid);
  4658. return SCSI_ABORT_ERROR;
  4659. } else if (!hostdata) {
  4660. printk ("Bogus SCSI host %d; no hostdata\n", host->host_no);
  4661. return SCSI_ABORT_ERROR;
  4662. }
  4663. NCR53c7x0_local_setup(host);
  4664. /*
  4665. * CHECK : I don't think that reading ISTAT will unstack any interrupts,
  4666. * since we need to write the INTF bit to clear it, and SCSI/DMA
  4667. * interrupts don't clear until we read SSTAT/SIST and DSTAT registers.
  4668. *
  4669. * See that this is the case. Appears to be correct on the 710, at least.
  4670. *
  4671. * I suspect that several of our failures may be coming from a new fatal
  4672. * interrupt (possibly due to a phase mismatch) happening after we've left
  4673. * the interrupt handler, but before the PIC has had the interrupt condition
  4674. * cleared.
  4675. */
  4676. if (NCR53c7x0_read8(hostdata->istat) & (ISTAT_DIP|ISTAT_SIP)) {
  4677. printk ("scsi%d : dropped interrupt for command %ld\n", host->host_no,
  4678. cmd->pid);
  4679. NCR53c7x0_intr (host->irq, NULL, NULL);
  4680. return SCSI_ABORT_BUSY;
  4681. }
  4682. local_irq_save(flags);
  4683. #if 0
  4684. if (cache_pid == cmd->pid)
  4685. panic ("scsi%d : bloody fetus %d\n", host->host_no, cmd->pid);
  4686. else
  4687. cache_pid = cmd->pid;
  4688. #endif
  4689. /*
  4690. * The command could be hiding in the issue_queue. This would be very
  4691. * nice, as commands can't be moved from the high level driver's issue queue
  4692. * into the shared queue until an interrupt routine is serviced, and this
  4693. * moving is atomic.
  4694. *
  4695. * If this is the case, we don't have to worry about anything - we simply
  4696. * pull the command out of the old queue, and call it aborted.
  4697. */
  4698. for (me = (Scsi_Cmnd *) hostdata->issue_queue,
  4699. last = (Scsi_Cmnd **) &(hostdata->issue_queue);
  4700. me && me != cmd; last = (Scsi_Cmnd **)&(me->SCp.ptr),
  4701. me = (Scsi_Cmnd *)me->SCp.ptr);
  4702. if (me) {
  4703. *last = (Scsi_Cmnd *) me->SCp.ptr;
  4704. if (me->host_scribble) {
  4705. ((struct NCR53c7x0_cmd *)me->host_scribble)->next = hostdata->free;
  4706. hostdata->free = (struct NCR53c7x0_cmd *) me->host_scribble;
  4707. me->host_scribble = NULL;
  4708. }
  4709. cmd->result = DID_ABORT << 16;
  4710. cmd->scsi_done(cmd);
  4711. printk ("scsi%d : found command %ld in Linux issue queue\n",
  4712. host->host_no, me->pid);
  4713. local_irq_restore(flags);
  4714. run_process_issue_queue();
  4715. return SCSI_ABORT_SUCCESS;
  4716. }
  4717. /*
  4718. * That failing, the command could be in our list of already executing
  4719. * commands. If this is the case, drastic measures are called for.
  4720. */
  4721. for (curr = (struct NCR53c7x0_cmd *) hostdata->running_list,
  4722. prev = (struct NCR53c7x0_cmd **) &(hostdata->running_list);
  4723. curr && curr->cmd != cmd; prev = (struct NCR53c7x0_cmd **)
  4724. &(curr->next), curr = (struct NCR53c7x0_cmd *) curr->next);
  4725. if (curr) {
  4726. if ((curr->result & 0xff) != 0xff && (curr->result & 0xff00) != 0xff00) {
  4727. cmd->result = curr->result;
  4728. if (prev)
  4729. *prev = (struct NCR53c7x0_cmd *) curr->next;
  4730. curr->next = (struct NCR53c7x0_cmd *) hostdata->free;
  4731. cmd->host_scribble = NULL;
  4732. hostdata->free = curr;
  4733. cmd->scsi_done(cmd);
  4734. printk ("scsi%d : found finished command %ld in running list\n",
  4735. host->host_no, cmd->pid);
  4736. local_irq_restore(flags);
  4737. return SCSI_ABORT_NOT_RUNNING;
  4738. } else {
  4739. printk ("scsi%d : DANGER : command running, can not abort.\n",
  4740. cmd->device->host->host_no);
  4741. local_irq_restore(flags);
  4742. return SCSI_ABORT_BUSY;
  4743. }
  4744. }
  4745. /*
  4746. * And if we couldn't find it in any of our queues, it must have been
  4747. * a dropped interrupt.
  4748. */
  4749. curr = (struct NCR53c7x0_cmd *) cmd->host_scribble;
  4750. if (curr) {
  4751. curr->next = hostdata->free;
  4752. hostdata->free = curr;
  4753. cmd->host_scribble = NULL;
  4754. }
  4755. if (curr == NULL || ((curr->result & 0xff00) == 0xff00) ||
  4756. ((curr->result & 0xff) == 0xff)) {
  4757. printk ("scsi%d : did this command ever run?\n", host->host_no);
  4758. cmd->result = DID_ABORT << 16;
  4759. } else {
  4760. printk ("scsi%d : probably lost INTFLY, normal completion\n",
  4761. host->host_no);
  4762. cmd->result = curr->result;
  4763. /*
  4764. * FIXME : We need to add an additional flag which indicates if a
  4765. * command was ever counted as BUSY, so if we end up here we can
  4766. * decrement the busy count if and only if it is necessary.
  4767. */
  4768. --hostdata->busy[cmd->device->id][cmd->device->lun];
  4769. }
  4770. local_irq_restore(flags);
  4771. cmd->scsi_done(cmd);
  4772. /*
  4773. * We need to run process_issue_queue since termination of this command
  4774. * may allow another queued command to execute first?
  4775. */
  4776. return SCSI_ABORT_NOT_RUNNING;
  4777. }
  4778. /*
  4779. * Function : int NCR53c7xx_reset (Scsi_Cmnd *cmd)
  4780. *
  4781. * Purpose : perform a hard reset of the SCSI bus and NCR
  4782. * chip.
  4783. *
  4784. * Inputs : cmd - command which caused the SCSI RESET
  4785. *
  4786. * Returns : 0 on success.
  4787. */
  4788. int
  4789. NCR53c7xx_reset (Scsi_Cmnd *cmd, unsigned int reset_flags) {
  4790. NCR53c7x0_local_declare();
  4791. unsigned long flags;
  4792. int found = 0;
  4793. struct NCR53c7x0_cmd * c;
  4794. Scsi_Cmnd *tmp;
  4795. /*
  4796. * When we call scsi_done(), it's going to wake up anything sleeping on the
  4797. * resources which were in use by the aborted commands, and we'll start to
  4798. * get new commands.
  4799. *
  4800. * We can't let this happen until after we've re-initialized the driver
  4801. * structures, and can't reinitialize those structures until after we've
  4802. * dealt with their contents.
  4803. *
  4804. * So, we need to find all of the commands which were running, stick
  4805. * them on a linked list of completed commands (we'll use the host_scribble
  4806. * pointer), do our reinitialization, and then call the done function for
  4807. * each command.
  4808. */
  4809. Scsi_Cmnd *nuke_list = NULL;
  4810. struct Scsi_Host *host = cmd->device->host;
  4811. struct NCR53c7x0_hostdata *hostdata =
  4812. (struct NCR53c7x0_hostdata *) host->hostdata[0];
  4813. NCR53c7x0_local_setup(host);
  4814. local_irq_save(flags);
  4815. ncr_halt (host);
  4816. print_lots (host);
  4817. dump_events (host, 30);
  4818. ncr_scsi_reset (host);
  4819. for (tmp = nuke_list = return_outstanding_commands (host, 1 /* free */,
  4820. 0 /* issue */ ); tmp; tmp = (Scsi_Cmnd *) tmp->SCp.buffer)
  4821. if (tmp == cmd) {
  4822. found = 1;
  4823. break;
  4824. }
  4825. /*
  4826. * If we didn't find the command which caused this reset in our running
  4827. * list, then we've lost it. See that it terminates normally anyway.
  4828. */
  4829. if (!found) {
  4830. c = (struct NCR53c7x0_cmd *) cmd->host_scribble;
  4831. if (c) {
  4832. cmd->host_scribble = NULL;
  4833. c->next = hostdata->free;
  4834. hostdata->free = c;
  4835. } else
  4836. printk ("scsi%d: lost command %ld\n", host->host_no, cmd->pid);
  4837. cmd->SCp.buffer = (struct scatterlist *) nuke_list;
  4838. nuke_list = cmd;
  4839. }
  4840. NCR53c7x0_driver_init (host);
  4841. hostdata->soft_reset (host);
  4842. if (hostdata->resets == 0)
  4843. disable(host);
  4844. else if (hostdata->resets != -1)
  4845. --hostdata->resets;
  4846. local_irq_restore(flags);
  4847. for (; nuke_list; nuke_list = tmp) {
  4848. tmp = (Scsi_Cmnd *) nuke_list->SCp.buffer;
  4849. nuke_list->result = DID_RESET << 16;
  4850. nuke_list->scsi_done (nuke_list);
  4851. }
  4852. local_irq_restore(flags);
  4853. return SCSI_RESET_SUCCESS;
  4854. }
  4855. /*
  4856. * The NCR SDMS bios follows Annex A of the SCSI-CAM draft, and
  4857. * therefore shares the scsicam_bios_param function.
  4858. */
  4859. /*
  4860. * Function : int insn_to_offset (Scsi_Cmnd *cmd, u32 *insn)
  4861. *
  4862. * Purpose : convert instructions stored at NCR pointer into data
  4863. * pointer offset.
  4864. *
  4865. * Inputs : cmd - SCSI command; insn - pointer to instruction. Either current
  4866. * DSP, or saved data pointer.
  4867. *
  4868. * Returns : offset on success, -1 on failure.
  4869. */
  4870. static int
  4871. insn_to_offset (Scsi_Cmnd *cmd, u32 *insn) {
  4872. struct NCR53c7x0_hostdata *hostdata =
  4873. (struct NCR53c7x0_hostdata *) cmd->device->host->hostdata[0];
  4874. struct NCR53c7x0_cmd *ncmd =
  4875. (struct NCR53c7x0_cmd *) cmd->host_scribble;
  4876. int offset = 0, buffers;
  4877. struct scatterlist *segment;
  4878. char *ptr;
  4879. int found = 0;
  4880. /*
  4881. * With the current code implementation, if the insn is inside dynamically
  4882. * generated code, the data pointer will be the instruction preceding
  4883. * the next transfer segment.
  4884. */
  4885. if (!check_address ((unsigned long) ncmd, sizeof (struct NCR53c7x0_cmd)) &&
  4886. ((insn >= ncmd->data_transfer_start &&
  4887. insn < ncmd->data_transfer_end) ||
  4888. (insn >= ncmd->residual &&
  4889. insn < (ncmd->residual +
  4890. sizeof(ncmd->residual))))) {
  4891. ptr = bus_to_virt(insn[3]);
  4892. if ((buffers = cmd->use_sg)) {
  4893. for (offset = 0,
  4894. segment = (struct scatterlist *) cmd->buffer;
  4895. buffers && !((found = ((ptr >= (char *)page_address(segment->page)+segment->offset) &&
  4896. (ptr < ((char *)page_address(segment->page)+segment->offset+segment->length)))));
  4897. --buffers, offset += segment->length, ++segment)
  4898. #if 0
  4899. printk("scsi%d: comparing 0x%p to 0x%p\n",
  4900. cmd->device->host->host_no, saved, page_address(segment->page+segment->offset);
  4901. #else
  4902. ;
  4903. #endif
  4904. offset += ptr - ((char *)page_address(segment->page)+segment->offset);
  4905. } else {
  4906. found = 1;
  4907. offset = ptr - (char *) (cmd->request_buffer);
  4908. }
  4909. } else if ((insn >= hostdata->script +
  4910. hostdata->E_data_transfer / sizeof(u32)) &&
  4911. (insn <= hostdata->script +
  4912. hostdata->E_end_data_transfer / sizeof(u32))) {
  4913. found = 1;
  4914. offset = 0;
  4915. }
  4916. return found ? offset : -1;
  4917. }
  4918. /*
  4919. * Function : void print_progress (Scsi_Cmnd *cmd)
  4920. *
  4921. * Purpose : print the current location of the saved data pointer
  4922. *
  4923. * Inputs : cmd - command we are interested in
  4924. *
  4925. */
  4926. static void
  4927. print_progress (Scsi_Cmnd *cmd) {
  4928. NCR53c7x0_local_declare();
  4929. struct NCR53c7x0_cmd *ncmd =
  4930. (struct NCR53c7x0_cmd *) cmd->host_scribble;
  4931. int offset, i;
  4932. char *where;
  4933. u32 *ptr;
  4934. NCR53c7x0_local_setup (cmd->device->host);
  4935. if (check_address ((unsigned long) ncmd,sizeof (struct NCR53c7x0_cmd)) == 0)
  4936. {
  4937. printk("\nNCR53c7x0_cmd fields:\n");
  4938. printk(" bounce.len=0x%x, addr=0x%0x, buf[]=0x%02x %02x %02x %02x\n",
  4939. ncmd->bounce.len, ncmd->bounce.addr, ncmd->bounce.buf[0],
  4940. ncmd->bounce.buf[1], ncmd->bounce.buf[2], ncmd->bounce.buf[3]);
  4941. printk(" result=%04x, cdb[0]=0x%02x\n", ncmd->result, ncmd->cmnd[0]);
  4942. }
  4943. for (i = 0; i < 2; ++i) {
  4944. if (check_address ((unsigned long) ncmd,
  4945. sizeof (struct NCR53c7x0_cmd)) == -1)
  4946. continue;
  4947. if (!i) {
  4948. where = "saved";
  4949. ptr = bus_to_virt(ncmd->saved_data_pointer);
  4950. } else {
  4951. where = "active";
  4952. ptr = bus_to_virt (NCR53c7x0_read32 (DSP_REG) -
  4953. NCR53c7x0_insn_size (NCR53c7x0_read8 (DCMD_REG)) *
  4954. sizeof(u32));
  4955. }
  4956. offset = insn_to_offset (cmd, ptr);
  4957. if (offset != -1)
  4958. printk ("scsi%d : %s data pointer at offset %d\n",
  4959. cmd->device->host->host_no, where, offset);
  4960. else {
  4961. int size;
  4962. printk ("scsi%d : can't determine %s data pointer offset\n",
  4963. cmd->device->host->host_no, where);
  4964. if (ncmd) {
  4965. size = print_insn (cmd->device->host,
  4966. bus_to_virt(ncmd->saved_data_pointer), "", 1);
  4967. print_insn (cmd->device->host,
  4968. bus_to_virt(ncmd->saved_data_pointer) + size * sizeof(u32),
  4969. "", 1);
  4970. }
  4971. }
  4972. }
  4973. }
  4974. static void
  4975. print_dsa (struct Scsi_Host *host, u32 *dsa, const char *prefix) {
  4976. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  4977. host->hostdata[0];
  4978. int i, len;
  4979. char *ptr;
  4980. Scsi_Cmnd *cmd;
  4981. if (check_address ((unsigned long) dsa, hostdata->dsa_end -
  4982. hostdata->dsa_start) == -1) {
  4983. printk("scsi%d : bad dsa virt 0x%p\n", host->host_no, dsa);
  4984. return;
  4985. }
  4986. printk("%sscsi%d : dsa at phys 0x%lx (virt 0x%p)\n"
  4987. " + %d : dsa_msgout length = %u, data = 0x%x (virt 0x%p)\n" ,
  4988. prefix ? prefix : "",
  4989. host->host_no, virt_to_bus (dsa), dsa, hostdata->dsa_msgout,
  4990. dsa[hostdata->dsa_msgout / sizeof(u32)],
  4991. dsa[hostdata->dsa_msgout / sizeof(u32) + 1],
  4992. bus_to_virt (dsa[hostdata->dsa_msgout / sizeof(u32) + 1]));
  4993. /*
  4994. * Only print messages if they're sane in length so we don't
  4995. * blow the kernel printk buffer on something which won't buy us
  4996. * anything.
  4997. */
  4998. if (dsa[hostdata->dsa_msgout / sizeof(u32)] <
  4999. sizeof (hostdata->free->select))
  5000. for (i = dsa[hostdata->dsa_msgout / sizeof(u32)],
  5001. ptr = bus_to_virt (dsa[hostdata->dsa_msgout / sizeof(u32) + 1]);
  5002. i > 0 && !check_address ((unsigned long) ptr, 1);
  5003. ptr += len, i -= len) {
  5004. printk(" ");
  5005. len = spi_print_msg(ptr);
  5006. printk("\n");
  5007. if (!len)
  5008. break;
  5009. }
  5010. printk(" + %d : select_indirect = 0x%x\n",
  5011. hostdata->dsa_select, dsa[hostdata->dsa_select / sizeof(u32)]);
  5012. cmd = (Scsi_Cmnd *) bus_to_virt(dsa[hostdata->dsa_cmnd / sizeof(u32)]);
  5013. printk(" + %d : dsa_cmnd = 0x%x ", hostdata->dsa_cmnd,
  5014. (u32) virt_to_bus(cmd));
  5015. /* XXX Maybe we should access cmd->host_scribble->result here. RGH */
  5016. if (cmd) {
  5017. printk(" result = 0x%x, target = %d, lun = %d, cmd = ",
  5018. cmd->result, cmd->device->id, cmd->device->lun);
  5019. __scsi_print_command(cmd->cmnd);
  5020. } else
  5021. printk("\n");
  5022. printk(" + %d : dsa_next = 0x%x\n", hostdata->dsa_next,
  5023. dsa[hostdata->dsa_next / sizeof(u32)]);
  5024. if (cmd) {
  5025. printk("scsi%d target %d : sxfer_sanity = 0x%x, scntl3_sanity = 0x%x\n"
  5026. " script : ",
  5027. host->host_no, cmd->device->id,
  5028. hostdata->sync[cmd->device->id].sxfer_sanity,
  5029. hostdata->sync[cmd->device->id].scntl3_sanity);
  5030. for (i = 0; i < (sizeof(hostdata->sync[cmd->device->id].script) / 4); ++i)
  5031. printk ("0x%x ", hostdata->sync[cmd->device->id].script[i]);
  5032. printk ("\n");
  5033. print_progress (cmd);
  5034. }
  5035. }
  5036. /*
  5037. * Function : void print_queues (Scsi_Host *host)
  5038. *
  5039. * Purpose : print the contents of the NCR issue and reconnect queues
  5040. *
  5041. * Inputs : host - SCSI host we are interested in
  5042. *
  5043. */
  5044. static void
  5045. print_queues (struct Scsi_Host *host) {
  5046. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  5047. host->hostdata[0];
  5048. u32 *dsa, *next_dsa;
  5049. volatile u32 *ncrcurrent;
  5050. int left;
  5051. Scsi_Cmnd *cmd, *next_cmd;
  5052. unsigned long flags;
  5053. printk ("scsi%d : issue queue\n", host->host_no);
  5054. for (left = host->can_queue, cmd = (Scsi_Cmnd *) hostdata->issue_queue;
  5055. left >= 0 && cmd;
  5056. cmd = next_cmd) {
  5057. next_cmd = (Scsi_Cmnd *) cmd->SCp.ptr;
  5058. local_irq_save(flags);
  5059. if (cmd->host_scribble) {
  5060. if (check_address ((unsigned long) (cmd->host_scribble),
  5061. sizeof (cmd->host_scribble)) == -1)
  5062. printk ("scsi%d: scsi pid %ld bad pointer to NCR53c7x0_cmd\n",
  5063. host->host_no, cmd->pid);
  5064. /* print_dsa does sanity check on address, no need to check */
  5065. else
  5066. print_dsa (host, ((struct NCR53c7x0_cmd *) cmd->host_scribble)
  5067. -> dsa, "");
  5068. } else
  5069. printk ("scsi%d : scsi pid %ld for target %d lun %d has no NCR53c7x0_cmd\n",
  5070. host->host_no, cmd->pid, cmd->device->id, cmd->device->lun);
  5071. local_irq_restore(flags);
  5072. }
  5073. if (left <= 0) {
  5074. printk ("scsi%d : loop detected in issue queue\n",
  5075. host->host_no);
  5076. }
  5077. /*
  5078. * Traverse the NCR reconnect and start DSA structures, printing out
  5079. * each element until we hit the end or detect a loop. Currently,
  5080. * the reconnect structure is a linked list; and the start structure
  5081. * is an array. Eventually, the reconnect structure will become a
  5082. * list as well, since this simplifies the code.
  5083. */
  5084. printk ("scsi%d : schedule dsa array :\n", host->host_no);
  5085. for (left = host->can_queue, ncrcurrent = hostdata->schedule;
  5086. left > 0; ncrcurrent += 2, --left)
  5087. if (ncrcurrent[0] != hostdata->NOP_insn)
  5088. /* FIXME : convert pointer to dsa_begin to pointer to dsa. */
  5089. print_dsa (host, bus_to_virt (ncrcurrent[1] -
  5090. (hostdata->E_dsa_code_begin -
  5091. hostdata->E_dsa_code_template)), "");
  5092. printk ("scsi%d : end schedule dsa array\n", host->host_no);
  5093. printk ("scsi%d : reconnect_dsa_head :\n", host->host_no);
  5094. for (left = host->can_queue,
  5095. dsa = bus_to_virt (hostdata->reconnect_dsa_head);
  5096. left >= 0 && dsa;
  5097. dsa = next_dsa) {
  5098. local_irq_save(flags);
  5099. if (check_address ((unsigned long) dsa, sizeof(dsa)) == -1) {
  5100. printk ("scsi%d: bad DSA pointer 0x%p", host->host_no,
  5101. dsa);
  5102. next_dsa = NULL;
  5103. }
  5104. else
  5105. {
  5106. next_dsa = bus_to_virt(dsa[hostdata->dsa_next / sizeof(u32)]);
  5107. print_dsa (host, dsa, "");
  5108. }
  5109. local_irq_restore(flags);
  5110. }
  5111. printk ("scsi%d : end reconnect_dsa_head\n", host->host_no);
  5112. if (left < 0)
  5113. printk("scsi%d: possible loop in ncr reconnect list\n",
  5114. host->host_no);
  5115. }
  5116. static void
  5117. print_lots (struct Scsi_Host *host) {
  5118. NCR53c7x0_local_declare();
  5119. struct NCR53c7x0_hostdata *hostdata =
  5120. (struct NCR53c7x0_hostdata *) host->hostdata[0];
  5121. u32 *dsp_next, *dsp, *dsa, dbc_dcmd;
  5122. unsigned char dcmd, sbcl;
  5123. int i, size;
  5124. NCR53c7x0_local_setup(host);
  5125. if ((dsp_next = bus_to_virt(NCR53c7x0_read32 (DSP_REG)))) {
  5126. dbc_dcmd = NCR53c7x0_read32(DBC_REG);
  5127. dcmd = (dbc_dcmd & 0xff000000) >> 24;
  5128. dsp = dsp_next - NCR53c7x0_insn_size(dcmd);
  5129. dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
  5130. sbcl = NCR53c7x0_read8 (SBCL_REG);
  5131. /*
  5132. * For the 53c710, the following will report value 0 for SCNTL3
  5133. * and STEST0 - we don't have these registers.
  5134. */
  5135. printk ("scsi%d : DCMD|DBC=0x%x, DNAD=0x%x (virt 0x%p)\n"
  5136. " DSA=0x%lx (virt 0x%p)\n"
  5137. " DSPS=0x%x, TEMP=0x%x (virt 0x%p), DMODE=0x%x\n"
  5138. " SXFER=0x%x, SCNTL3=0x%x\n"
  5139. " %s%s%sphase=%s, %d bytes in SCSI FIFO\n"
  5140. " SCRATCH=0x%x, saved2_dsa=0x%0lx\n",
  5141. host->host_no, dbc_dcmd, NCR53c7x0_read32(DNAD_REG),
  5142. bus_to_virt(NCR53c7x0_read32(DNAD_REG)),
  5143. virt_to_bus(dsa), dsa,
  5144. NCR53c7x0_read32(DSPS_REG), NCR53c7x0_read32(TEMP_REG),
  5145. bus_to_virt (NCR53c7x0_read32(TEMP_REG)),
  5146. (int) NCR53c7x0_read8(hostdata->dmode),
  5147. (int) NCR53c7x0_read8(SXFER_REG),
  5148. ((hostdata->chip / 100) == 8) ?
  5149. (int) NCR53c7x0_read8(SCNTL3_REG_800) : 0,
  5150. (sbcl & SBCL_BSY) ? "BSY " : "",
  5151. (sbcl & SBCL_SEL) ? "SEL " : "",
  5152. (sbcl & SBCL_REQ) ? "REQ " : "",
  5153. sstat2_to_phase(NCR53c7x0_read8 (((hostdata->chip / 100) == 8) ?
  5154. SSTAT1_REG : SSTAT2_REG)),
  5155. (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ?
  5156. SSTAT1_REG : SSTAT2_REG) & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT,
  5157. ((hostdata->chip / 100) == 8) ? NCR53c7x0_read8 (STEST0_REG_800) :
  5158. NCR53c7x0_read32(SCRATCHA_REG_800),
  5159. hostdata->saved2_dsa);
  5160. printk ("scsi%d : DSP 0x%lx (virt 0x%p) ->\n", host->host_no,
  5161. virt_to_bus(dsp), dsp);
  5162. for (i = 6; i > 0; --i, dsp += size)
  5163. size = print_insn (host, dsp, "", 1);
  5164. if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON) {
  5165. if ((hostdata->chip / 100) == 8)
  5166. printk ("scsi%d : connected (SDID=0x%x, SSID=0x%x)\n",
  5167. host->host_no, NCR53c7x0_read8 (SDID_REG_800),
  5168. NCR53c7x0_read8 (SSID_REG_800));
  5169. else
  5170. printk ("scsi%d : connected (SDID=0x%x)\n",
  5171. host->host_no, NCR53c7x0_read8 (SDID_REG_700));
  5172. print_dsa (host, dsa, "");
  5173. }
  5174. #if 1
  5175. print_queues (host);
  5176. #endif
  5177. }
  5178. }
  5179. /*
  5180. * Function : static int shutdown (struct Scsi_Host *host)
  5181. *
  5182. * Purpose : does a clean (we hope) shutdown of the NCR SCSI
  5183. * chip. Use prior to dumping core, unloading the NCR driver,
  5184. *
  5185. * Returns : 0 on success
  5186. */
  5187. static int
  5188. shutdown (struct Scsi_Host *host) {
  5189. NCR53c7x0_local_declare();
  5190. unsigned long flags;
  5191. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  5192. host->hostdata[0];
  5193. NCR53c7x0_local_setup(host);
  5194. local_irq_save(flags);
  5195. /* Get in a state where we can reset the SCSI bus */
  5196. ncr_halt (host);
  5197. ncr_scsi_reset (host);
  5198. hostdata->soft_reset(host);
  5199. disable (host);
  5200. local_irq_restore(flags);
  5201. return 0;
  5202. }
  5203. /*
  5204. * Function : void ncr_scsi_reset (struct Scsi_Host *host)
  5205. *
  5206. * Purpose : reset the SCSI bus.
  5207. */
  5208. static void
  5209. ncr_scsi_reset (struct Scsi_Host *host) {
  5210. NCR53c7x0_local_declare();
  5211. unsigned long flags;
  5212. NCR53c7x0_local_setup(host);
  5213. local_irq_save(flags);
  5214. NCR53c7x0_write8(SCNTL1_REG, SCNTL1_RST);
  5215. udelay(25); /* Minimum amount of time to assert RST */
  5216. NCR53c7x0_write8(SCNTL1_REG, 0);
  5217. local_irq_restore(flags);
  5218. }
  5219. /*
  5220. * Function : void hard_reset (struct Scsi_Host *host)
  5221. *
  5222. */
  5223. static void
  5224. hard_reset (struct Scsi_Host *host) {
  5225. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  5226. host->hostdata[0];
  5227. unsigned long flags;
  5228. local_irq_save(flags);
  5229. ncr_scsi_reset(host);
  5230. NCR53c7x0_driver_init (host);
  5231. if (hostdata->soft_reset)
  5232. hostdata->soft_reset (host);
  5233. local_irq_restore(flags);
  5234. }
  5235. /*
  5236. * Function : Scsi_Cmnd *return_outstanding_commands (struct Scsi_Host *host,
  5237. * int free, int issue)
  5238. *
  5239. * Purpose : return a linked list (using the SCp.buffer field as next,
  5240. * so we don't perturb hostdata. We don't use a field of the
  5241. * NCR53c7x0_cmd structure since we may not have allocated one
  5242. * for the command causing the reset.) of Scsi_Cmnd structures that
  5243. * had propagated below the Linux issue queue level. If free is set,
  5244. * free the NCR53c7x0_cmd structures which are associated with
  5245. * the Scsi_Cmnd structures, and clean up any internal
  5246. * NCR lists that the commands were on. If issue is set,
  5247. * also return commands in the issue queue.
  5248. *
  5249. * Returns : linked list of commands
  5250. *
  5251. * NOTE : the caller should insure that the NCR chip is halted
  5252. * if the free flag is set.
  5253. */
  5254. static Scsi_Cmnd *
  5255. return_outstanding_commands (struct Scsi_Host *host, int free, int issue) {
  5256. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  5257. host->hostdata[0];
  5258. struct NCR53c7x0_cmd *c;
  5259. int i;
  5260. u32 *ncrcurrent;
  5261. Scsi_Cmnd *list = NULL, *tmp;
  5262. for (c = (struct NCR53c7x0_cmd *) hostdata->running_list; c;
  5263. c = (struct NCR53c7x0_cmd *) c->next) {
  5264. if (c->cmd->SCp.buffer) {
  5265. printk ("scsi%d : loop detected in running list!\n", host->host_no);
  5266. break;
  5267. } else {
  5268. printk ("Duh? Bad things happening in the NCR driver\n");
  5269. break;
  5270. }
  5271. c->cmd->SCp.buffer = (struct scatterlist *) list;
  5272. list = c->cmd;
  5273. if (free) {
  5274. c->next = hostdata->free;
  5275. hostdata->free = c;
  5276. }
  5277. }
  5278. if (free) {
  5279. for (i = 0, ncrcurrent = (u32 *) hostdata->schedule;
  5280. i < host->can_queue; ++i, ncrcurrent += 2) {
  5281. ncrcurrent[0] = hostdata->NOP_insn;
  5282. ncrcurrent[1] = 0xdeadbeef;
  5283. }
  5284. hostdata->ncrcurrent = NULL;
  5285. }
  5286. if (issue) {
  5287. for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp; tmp = tmp->next) {
  5288. if (tmp->SCp.buffer) {
  5289. printk ("scsi%d : loop detected in issue queue!\n",
  5290. host->host_no);
  5291. break;
  5292. }
  5293. tmp->SCp.buffer = (struct scatterlist *) list;
  5294. list = tmp;
  5295. }
  5296. if (free)
  5297. hostdata->issue_queue = NULL;
  5298. }
  5299. return list;
  5300. }
  5301. /*
  5302. * Function : static int disable (struct Scsi_Host *host)
  5303. *
  5304. * Purpose : disables the given NCR host, causing all commands
  5305. * to return a driver error. Call this so we can unload the
  5306. * module during development and try again. Eventually,
  5307. * we should be able to find clean workarounds for these
  5308. * problems.
  5309. *
  5310. * Inputs : host - hostadapter to twiddle
  5311. *
  5312. * Returns : 0 on success.
  5313. */
  5314. static int
  5315. disable (struct Scsi_Host *host) {
  5316. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  5317. host->hostdata[0];
  5318. unsigned long flags;
  5319. Scsi_Cmnd *nuke_list, *tmp;
  5320. local_irq_save(flags);
  5321. if (hostdata->state != STATE_HALTED)
  5322. ncr_halt (host);
  5323. nuke_list = return_outstanding_commands (host, 1 /* free */, 1 /* issue */);
  5324. hard_reset (host);
  5325. hostdata->state = STATE_DISABLED;
  5326. local_irq_restore(flags);
  5327. printk ("scsi%d : nuking commands\n", host->host_no);
  5328. for (; nuke_list; nuke_list = tmp) {
  5329. tmp = (Scsi_Cmnd *) nuke_list->SCp.buffer;
  5330. nuke_list->result = DID_ERROR << 16;
  5331. nuke_list->scsi_done(nuke_list);
  5332. }
  5333. printk ("scsi%d : done. \n", host->host_no);
  5334. printk (KERN_ALERT "scsi%d : disabled. Unload and reload\n",
  5335. host->host_no);
  5336. return 0;
  5337. }
  5338. /*
  5339. * Function : static int ncr_halt (struct Scsi_Host *host)
  5340. *
  5341. * Purpose : halts the SCSI SCRIPTS(tm) processor on the NCR chip
  5342. *
  5343. * Inputs : host - SCSI chip to halt
  5344. *
  5345. * Returns : 0 on success
  5346. */
  5347. static int
  5348. ncr_halt (struct Scsi_Host *host) {
  5349. NCR53c7x0_local_declare();
  5350. unsigned long flags;
  5351. unsigned char istat, tmp;
  5352. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  5353. host->hostdata[0];
  5354. int stage;
  5355. NCR53c7x0_local_setup(host);
  5356. local_irq_save(flags);
  5357. /* Stage 0 : eat all interrupts
  5358. Stage 1 : set ABORT
  5359. Stage 2 : eat all but abort interrupts
  5360. Stage 3 : eat all interrupts
  5361. */
  5362. for (stage = 0;;) {
  5363. if (stage == 1) {
  5364. NCR53c7x0_write8(hostdata->istat, ISTAT_ABRT);
  5365. ++stage;
  5366. }
  5367. istat = NCR53c7x0_read8 (hostdata->istat);
  5368. if (istat & ISTAT_SIP) {
  5369. tmp = NCR53c7x0_read8(SSTAT0_REG);
  5370. } else if (istat & ISTAT_DIP) {
  5371. tmp = NCR53c7x0_read8(DSTAT_REG);
  5372. if (stage == 2) {
  5373. if (tmp & DSTAT_ABRT) {
  5374. NCR53c7x0_write8(hostdata->istat, 0);
  5375. ++stage;
  5376. } else {
  5377. printk(KERN_ALERT "scsi%d : could not halt NCR chip\n",
  5378. host->host_no);
  5379. disable (host);
  5380. }
  5381. }
  5382. }
  5383. if (!(istat & (ISTAT_SIP|ISTAT_DIP))) {
  5384. if (stage == 0)
  5385. ++stage;
  5386. else if (stage == 3)
  5387. break;
  5388. }
  5389. }
  5390. hostdata->state = STATE_HALTED;
  5391. local_irq_restore(flags);
  5392. #if 0
  5393. print_lots (host);
  5394. #endif
  5395. return 0;
  5396. }
  5397. /*
  5398. * Function: event_name (int event)
  5399. *
  5400. * Purpose: map event enum into user-readable strings.
  5401. */
  5402. static const char *
  5403. event_name (int event) {
  5404. switch (event) {
  5405. case EVENT_NONE: return "none";
  5406. case EVENT_ISSUE_QUEUE: return "to issue queue";
  5407. case EVENT_START_QUEUE: return "to start queue";
  5408. case EVENT_SELECT: return "selected";
  5409. case EVENT_DISCONNECT: return "disconnected";
  5410. case EVENT_RESELECT: return "reselected";
  5411. case EVENT_COMPLETE: return "completed";
  5412. case EVENT_IDLE: return "idle";
  5413. case EVENT_SELECT_FAILED: return "select failed";
  5414. case EVENT_BEFORE_SELECT: return "before select";
  5415. case EVENT_RESELECT_FAILED: return "reselect failed";
  5416. default: return "unknown";
  5417. }
  5418. }
  5419. /*
  5420. * Function : void dump_events (struct Scsi_Host *host, count)
  5421. *
  5422. * Purpose : print last count events which have occurred.
  5423. */
  5424. static void
  5425. dump_events (struct Scsi_Host *host, int count) {
  5426. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  5427. host->hostdata[0];
  5428. struct NCR53c7x0_event event;
  5429. int i;
  5430. unsigned long flags;
  5431. if (hostdata->events) {
  5432. if (count > hostdata->event_size)
  5433. count = hostdata->event_size;
  5434. for (i = hostdata->event_index; count > 0;
  5435. i = (i ? i - 1 : hostdata->event_size -1), --count) {
  5436. /*
  5437. * By copying the event we're currently examining with interrupts
  5438. * disabled, we can do multiple printk(), etc. operations and
  5439. * still be guaranteed that they're happening on the same
  5440. * event structure.
  5441. */
  5442. local_irq_save(flags);
  5443. #if 0
  5444. event = hostdata->events[i];
  5445. #else
  5446. memcpy ((void *) &event, (void *) &(hostdata->events[i]),
  5447. sizeof(event));
  5448. #endif
  5449. local_irq_restore(flags);
  5450. printk ("scsi%d : %s event %d at %ld secs %ld usecs target %d lun %d\n",
  5451. host->host_no, event_name (event.event), count,
  5452. (long) event.time.tv_sec, (long) event.time.tv_usec,
  5453. event.target, event.lun);
  5454. if (event.dsa)
  5455. printk (" event for dsa 0x%lx (virt 0x%p)\n",
  5456. virt_to_bus(event.dsa), event.dsa);
  5457. if (event.pid != -1) {
  5458. printk (" event for pid %ld ", event.pid);
  5459. __scsi_print_command (event.cmnd);
  5460. }
  5461. }
  5462. }
  5463. }
  5464. /*
  5465. * Function: check_address
  5466. *
  5467. * Purpose: Check to see if a possibly corrupt pointer will fault the
  5468. * kernel.
  5469. *
  5470. * Inputs: addr - address; size - size of area
  5471. *
  5472. * Returns: 0 if area is OK, -1 on error.
  5473. *
  5474. * NOTES: should be implemented in terms of vverify on kernels
  5475. * that have it.
  5476. */
  5477. static int
  5478. check_address (unsigned long addr, int size) {
  5479. return (virt_to_phys((void *)addr) < PAGE_SIZE || virt_to_phys((void *)(addr + size)) > virt_to_phys(high_memory) ? -1 : 0);
  5480. }
  5481. #ifdef MODULE
  5482. int
  5483. NCR53c7x0_release(struct Scsi_Host *host) {
  5484. struct NCR53c7x0_hostdata *hostdata =
  5485. (struct NCR53c7x0_hostdata *) host->hostdata[0];
  5486. struct NCR53c7x0_cmd *cmd, *tmp;
  5487. shutdown (host);
  5488. if (host->irq != SCSI_IRQ_NONE)
  5489. {
  5490. int irq_count;
  5491. struct Scsi_Host *tmp;
  5492. for (irq_count = 0, tmp = first_host; tmp; tmp = tmp->next)
  5493. if (tmp->hostt == the_template && tmp->irq == host->irq)
  5494. ++irq_count;
  5495. if (irq_count == 1)
  5496. free_irq(host->irq, NULL);
  5497. }
  5498. if (host->dma_channel != DMA_NONE)
  5499. free_dma(host->dma_channel);
  5500. if (host->io_port)
  5501. release_region(host->io_port, host->n_io_port);
  5502. for (cmd = (struct NCR53c7x0_cmd *) hostdata->free; cmd; cmd = tmp,
  5503. --hostdata->num_cmds) {
  5504. tmp = (struct NCR53c7x0_cmd *) cmd->next;
  5505. /*
  5506. * If we're going to loop, try to stop it to get a more accurate
  5507. * count of the leaked commands.
  5508. */
  5509. cmd->next = NULL;
  5510. if (cmd->free)
  5511. cmd->free ((void *) cmd->real, cmd->size);
  5512. }
  5513. if (hostdata->num_cmds)
  5514. printk ("scsi%d : leaked %d NCR53c7x0_cmd structures\n",
  5515. host->host_no, hostdata->num_cmds);
  5516. vfree(hostdata->events);
  5517. /* XXX This assumes default cache mode to be IOMAP_FULL_CACHING, which
  5518. * XXX may be invalid (CONFIG_060_WRITETHROUGH)
  5519. */
  5520. kernel_set_cachemode((void *)hostdata, 8192, IOMAP_FULL_CACHING);
  5521. free_pages ((u32)hostdata, 1);
  5522. return 1;
  5523. }
  5524. #endif /* def MODULE */