53c7xx.c 191 KB

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